From 583ac10d1450610a4110849c26cc16f03154d26e Mon Sep 17 00:00:00 2001
From: jaseg <git@jaseg.de>
Date: Mon, 25 Sep 2023 12:45:42 +0200
Subject: Driver fw works

---
 driver_fw/src/main.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

(limited to 'driver_fw/src')

diff --git a/driver_fw/src/main.c b/driver_fw/src/main.c
index 6c63f4f..765c55a 100644
--- a/driver_fw/src/main.c
+++ b/driver_fw/src/main.c
@@ -1,6 +1,7 @@
 
 #include <global.h>
 #include <string.h>
+#include "8b10b.h"
 #include "generated/waveform_tables.h"
 
 volatile uint64_t sys_time_us;
@@ -11,8 +12,15 @@ 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 uint8_t tx_datagram[32] = {
+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_0,
     0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
     0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
     0x00, 0xff, 0xAA, 0x55, 0xfe, 0x18, 0xcc, 0x10,
@@ -20,6 +28,7 @@ static uint8_t tx_datagram[32] = {
 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;
 
 
 int main(void) {
@@ -144,11 +153,11 @@ int main(void) {
 
     TIM1->CCMR1 = (6<<TIM_CCMR1_OC2M_Pos) | TIM_CCMR1_OC2PE;
     TIM1->CCMR2 = (6<<TIM_CCMR2_OC3M_Pos) | TIM_CCMR2_OC3PE;
-    TIM1->CCER = TIM_CCER_CC2E | TIM_CCER_CC2NE | TIM_CCER_CC3E | TIM_CCER_CC3NE;
-    TIM1->BDTR = TIM_BDTR_MOE | (32<<TIM_BDTR_DTG_Pos) | TIM_BDTR_MOE;
+    TIM1->CCER = TIM_CCER_CC2E | TIM_CCER_CC2NE | TIM_CCER_CC2NP | TIM_CCER_CC3E | TIM_CCER_CC3NE | TIM_CCER_CC3P;
+    TIM1->BDTR = (8<<TIM_BDTR_DTG_Pos) | TIM_BDTR_MOE;
     TIM1->DCR = (14<<TIM_DCR_DBA_Pos) | (1<<TIM_DCR_DBL_Pos);
-    TIM1->PSC = 4;
-    TIM1->ARR = 256;
+    TIM1->PSC = 3;
+    TIM1->ARR = 250;
     TIM1->CCR2 = 64;
     TIM1->CCR3 = 192;
     TIM1->DIER = TIM_DIER_UDE;
@@ -159,7 +168,7 @@ int main(void) {
     NVIC_EnableIRQ(DMA1_Channel1_IRQn);
     NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
     dma_tx_constant(COUNT_OF(waveform_zero_one), 0x00);
-
+    xfr_8b10b_encode_reset(&encoder_state_8b10b);
     int i = 0;
     int j = 0;
     while (23) {
@@ -182,8 +191,9 @@ void dma_tx_waveform(size_t table_size, const uint16_t *table) {
 }
 
 void dma_tx_constant(size_t table_size, uint16_t constant) {
-    static uint16_t tx_constant;
-    tx_constant = constant;
+    static uint16_t tx_constant[2];
+    tx_constant[0] = constant;
+    tx_constant[1] = constant;
 
     DMA1_Channel1->CCR = 0;
     DMA1_Channel1->CCR = (1<<DMA_CCR_MSIZE_Pos) | (1<<DMA_CCR_PSIZE_Pos) | DMA_CCR_DIR | DMA_CCR_TCIE;
@@ -194,13 +204,15 @@ void dma_tx_constant(size_t table_size, uint16_t constant) {
 
 void DMA1_Channel1_IRQHandler() {
     static int transfer_errors = 0;
+    static int current_symbol = 0x2aa;
+
     if (DMA1->ISR & DMA_ISR_TEIF1) {
         transfer_errors ++;
     }
 
     DMA1->IFCR = DMA_IFCR_CGIF1;
 
-    int bit = !!(tx_datagram[tx_sympos] & (1<<tx_bitpos));
+    int bit = !!(current_symbol & (1<<tx_bitpos));
 
     if (tx_last_bit == bit) {
         dma_tx_constant(COUNT_OF(waveform_zero_one), bit ? WAVEFORM_CONST_ONE : WAVEFORM_CONST_ZERO);
@@ -213,9 +225,10 @@ void DMA1_Channel1_IRQHandler() {
     tx_last_bit = bit;
 
     tx_bitpos ++;
-    if (tx_bitpos >= 8) {
+    if (tx_bitpos >= 10) {
         tx_bitpos = 0;
         tx_sympos ++;
+        current_symbol = xfr_8b10b_encode(&encoder_state_8b10b, tx_datagram[tx_sympos]);
         if (tx_sympos >= COUNT_OF(tx_datagram)) {
             tx_sympos = 0;
         }
-- 
cgit