From c4c3429bf6a53e4c78f1d47e3b18df8b33e59745 Mon Sep 17 00:00:00 2001
From: Benjamin Koch <bbbsnowball@gmail.com>
Date: Wed, 24 May 2023 01:18:38 +0200
Subject: [PATCH] some small changes

---
 firmware/rust1/src/rs485.rs | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/firmware/rust1/src/rs485.rs b/firmware/rust1/src/rs485.rs
index 314a006..38915c8 100644
--- a/firmware/rust1/src/rs485.rs
+++ b/firmware/rust1/src/rs485.rs
@@ -248,7 +248,7 @@ impl RS485 {
 
             // We have a long 1, i.e. UART is idle. Wait for start bit.
             "have_long_high:",
-            "irq set 1",          // notify MCU in case they want to know about this (start of Modbus frame)
+            "irq set 1",          // notify MCU in case they want to know about this (end of Modbus frame)
             "wait 0 pin 0",
             "nop [4]",
             "jmp pin wait_idle",  // abort if zero was just a glitch
@@ -324,8 +324,8 @@ impl RS485 {
         let mut cfg = Config::default();
         let loaded_program = common.load_program(&relocated);
         cfg.use_program(&loaded_program, &[]);
-        const SM_FREQ: u32 = 125_000_000;
-        cfg.clock_divider = (U56F8!(125_000_000) / U56F8!(125_000_000 /* SM_FREQ */)).to_fixed();
+        const SM_FREQ: u32 = 25_000_000;
+        cfg.clock_divider = (U56F8!(125_000_000) / U56F8!(25_000_000 /* SM_FREQ */)).to_fixed();
         cfg.shift_in = ShiftConfig {
             auto_fill: true,
             threshold: 32,
@@ -343,9 +343,10 @@ impl RS485 {
         sm.set_config(&cfg);
         sm.set_enable(true);
 
-        // Timeout: Modbus wants 1.5 ms between frames so we make this a bit smaller. SM runs at 25 MHz
-        // but we need two clocks per loop.
-        let timeout_start_value = (SM_FREQ as f32 * 1.4e-3 / 2.) as u32;
+        // Timeout: Modbus wants 3.5 char times between frames but we should detect the end of a frame
+        // after 1.5 char times. This is 0.86 ms at 19200 baud (8e1, i.e. 11 baud times per char).
+        // We need two clocks for each loop.
+        let timeout_start_value = (SM_FREQ as f32 * 0.86e-3 / 2.) as u32;
         sm.tx().push(timeout_start_value);
         info!("timeout_start_value: {} = 0x{:08x}", timeout_start_value, timeout_start_value);
 
@@ -395,7 +396,6 @@ impl RS485 {
         let mut rxcrc = CRC.digest();
         let mut rx_expected_bytes = ModbusFrameLength::NeedMoreData(3);
         let mut rx_received_bytes = 0u16;
-        let mut rx_char_prev: u8 = 0;
 
         const TX_BUF_LENGTH: usize = 32;
         let mut txbuf = Vec::<u8, TX_BUF_LENGTH>::new();
@@ -560,19 +560,19 @@ impl RS485 {
                                                 tx_data[i + 1] = x as u32 | (((parity as u32) & 1) << 8);
                                             }
 
+                                            info!("Modbus reply: {:?}", txbuf);
+
                                             tx_future = DontAbort::new(sm1.tx().dma_push(dma_tx_ref.reborrow(),
                                                 &tx_data[0..(txbuf.len()+1)]), HangIfReused);
                                         }
                                     }
 
                                     rxbuf.clear();
-                                    rxcrc = CRC.digest();
+                                    //rxcrc = CRC.digest();  // re-init already done above, don't repeat to avoid warning
                                     rx_expected_bytes = ModbusFrameLength::NeedMoreData(3);
                                     rx_received_bytes = 0;
                                 }
                             }
-
-                            rx_char_prev = rx_char;
                         },
                         Result::Err(e) => {
                             info!("RX error {:?}", e);
-- 
GitLab