From c8623eb4c6c1464ffd49e83126e66d71ba5bf862 Mon Sep 17 00:00:00 2001
From: jaseg <git@jaseg.de>
Date: Mon, 2 Oct 2023 01:23:31 +0200
Subject: 8b10b issues

---
 driver_fw/Makefile   |   2 +-
 driver_fw/src/main.c | 104 ++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 88 insertions(+), 18 deletions(-)

(limited to 'driver_fw')

diff --git a/driver_fw/Makefile b/driver_fw/Makefile
index b847134..fb4be18 100644
--- a/driver_fw/Makefile
+++ b/driver_fw/Makefile
@@ -30,7 +30,7 @@ DEVICE 			:= STM32G070RB
 
 ASM_SOURCES 		:= startup.s
 
-C_SOURCES		 	:= src/main.c $(BUILDDIR)/generated/waveform_tables.c common/8b10b.c
+C_SOURCES		 	:= src/main.c $(BUILDDIR)/generated/waveform_tables.c common/8b10b.c common/xorshift.c common/crc32.c
 
 CPP_SOURCES			:= # - none -
 
diff --git a/driver_fw/src/main.c b/driver_fw/src/main.c
index 0b3c869..136e0c7 100644
--- a/driver_fw/src/main.c
+++ b/driver_fw/src/main.c
@@ -2,6 +2,9 @@
 #include <global.h>
 #include <string.h>
 #include "8b10b.h"
+#include "crc32.h"
+#include "xorshift.h"
+#include "protocol.h"
 #include "generated/waveform_tables.h"
 
 volatile uint64_t sys_time_us;
@@ -12,23 +15,27 @@ static void set_status_leds(uint32_t leds);
 static void dma_tx_constant(size_t table_size, uint16_t constant);
 static void dma_tx_waveform(size_t table_size, const uint16_t *table);
 
-static int tx_datagram[33] = {
-    /* FIXME test data */
-/*
-    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
-    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
-    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 
-    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa};
-*/
-    -K28_1,
-    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
-    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
-    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
-    0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10 };
+#define SYNC_INTERVAL 2
+
+static size_t time_to_sync = 0;
 static size_t tx_bitpos = 0;
 static size_t tx_sympos = 0;
 static int tx_last_bit = 0;
 static struct state_8b10b_enc encoder_state_8b10b;
+static uint32_t packet_rng_state;
+
+union tx_buf_union {
+    struct data_packet packet;
+    uint8_t bytes[sizeof(struct data_packet)];
+};
+
+static union tx_buf_union tx_buf[3];
+static union tx_buf_union *tx_buf_read = &tx_buf[0];
+static union tx_buf_union *tx_buf_idle = &tx_buf[1];
+static union tx_buf_union *tx_buf_write = &tx_buf[2];
+static bool idle_buf_ready = false;
+
+void update_tx_buf(void);
 
 
 int main(void) {
@@ -169,6 +176,16 @@ int main(void) {
     NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
     dma_tx_constant(COUNT_OF(waveform_zero_one), 0x00);
     xfr_8b10b_encode_reset(&encoder_state_8b10b);
+
+    memset(tx_buf_write, 0, sizeof(*tx_buf_write));
+    for (size_t i=0; i<COUNT_OF(tx_buf_write->packet.channels); i++) {
+        tx_buf_write->packet.channels[i] = 0x33;
+    }
+    for (size_t i=0; i<COUNT_OF(tx_buf_write->packet.brightness); i++) {
+        tx_buf_write->packet.brightness[i] = 0xff;
+    }
+    update_tx_buf();
+
     int i = 0;
     int j = 0;
     while (23) {
@@ -204,10 +221,14 @@ void dma_tx_constant(size_t table_size, uint16_t constant) {
 
 int8_t bit_arr[4096];
 size_t bit_pos = 0;
+int16_t sym_arr[512];
+size_t sym_pos = 0;
+uint16_t cnd_arr[512];
+size_t cnd_pos = 0;
 
 void DMA1_Channel1_IRQHandler() {
     static int transfer_errors = 0;
-    static int current_symbol = 0x2aa;
+    static int current_symbol = 0;
 
     if (DMA1->ISR & DMA_ISR_TEIF1) {
         transfer_errors ++;
@@ -233,16 +254,65 @@ void DMA1_Channel1_IRQHandler() {
 
     if (tx_bitpos == 0) {
         tx_bitpos = 9;
-        current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, tx_datagram[tx_sympos]);
-        tx_sympos ++;
-        if (tx_sympos >= COUNT_OF(tx_datagram)) {
+
+        sym_arr[sym_pos] = -255;
+
+        if (tx_sympos == sizeof(struct data_packet)) {
+            if (time_to_sync > 0) {
+                current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, -K27_7);
+                sym_arr[sym_pos] = current_symbol;
+                time_to_sync--;
+
+            } else { 
+                current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, -K28_1);
+                sym_arr[sym_pos] = current_symbol;
+                packet_rng_state = xorshift32(1);
+                time_to_sync = SYNC_INTERVAL;
+            }
+
+            if (idle_buf_ready) {
+                union tx_buf_union *tmp = tx_buf_idle;
+                tx_buf_idle = tx_buf_read;
+                tx_buf_read = tmp;
+                idle_buf_ready = false;
+            }
+
             tx_sympos = 0;
+
+        } else {
+            uint8_t b = tx_buf_read->bytes[tx_sympos];
+            packet_rng_state = xorshift32(packet_rng_state);
+            //b ^= packet_rng_state; FIXME DEBUG
+            current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, b);
+            sym_arr[sym_pos] = current_symbol;
+            tx_sympos ++;
+        }
+
+        sym_pos++;
+        if (sym_pos == COUNT_OF(sym_arr)) {
+            sym_pos = 0;
         }
+
     } else {
         tx_bitpos --;
     }
 }
 
+void update_tx_buf() {
+    uint32_t crc = crc32_reset();
+    for (size_t i=0; i<offsetof(struct data_packet, crc); i++) {
+        crc = crc32_update(crc, tx_buf_write->bytes[i]);
+    }
+    tx_buf_write->packet.crc = crc32_finalize(crc);
+
+    __disable_irq();
+    union tx_buf_union *tmp = tx_buf_idle;
+    tx_buf_idle = tx_buf_write;
+    tx_buf_write = tmp;
+    idle_buf_ready = true;
+    __enable_irq();
+}
+
 uint32_t read_fuse_monitor() {
     uint32_t idr_a = GPIOA->IDR;
     uint32_t idr_c = GPIOC->IDR;
-- 
cgit