From 1fa765a5d83df8139d0ec1796477c4a4ebda256a Mon Sep 17 00:00:00 2001 From: Empire Date: Wed, 1 Apr 2026 21:43:16 +0200 Subject: [PATCH] adjust can to hopefull work better --- Software/CAN_Sensor/src/main.rs | 6 +++--- Software/MainBoard/rust/src/hal/v4_hal.rs | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Software/CAN_Sensor/src/main.rs b/Software/CAN_Sensor/src/main.rs index c98b172..537f141 100644 --- a/Software/CAN_Sensor/src/main.rs +++ b/Software/CAN_Sensor/src/main.rs @@ -251,12 +251,12 @@ async fn main(spawner: Spawner) { // Improve CAN robustness for longer cables: // 1. Enable Automatic Bus-Off Management (ABOM) - // 2. Ensure No Automatic Retransmission (NART) is DISABLED (i.e. we WANT retransmission) + // 2. Disable Automatic Retransmission (NART) as we send regular measurements anyway // 3. Enable Receive FIFO Overwrite Mode (RFLM = 0, default) // 4. Increase Resync Jump Width (SJW) if possible by patching BTIMR hal::pac::CAN1.ctlr().modify(|w| { - w.set_abom(true); - w.set_nart(false); // HAL default is usually false, but let's be explicit + w.set_abom(false); + w.set_nart(true); }); // SJW is bits 24-25 of BTIMR. HAL sets it to 0 (SJW=1). diff --git a/Software/MainBoard/rust/src/hal/v4_hal.rs b/Software/MainBoard/rust/src/hal/v4_hal.rs index 5991e3d..9181e83 100644 --- a/Software/MainBoard/rust/src/hal/v4_hal.rs +++ b/Software/MainBoard/rust/src/hal/v4_hal.rs @@ -367,11 +367,13 @@ impl<'a> BoardInteraction<'a> for V4<'a> { let config = self.twai_config.take().expect("twai config not set"); let mut twai = config.into_async().start(); - let res = (async { - if twai.is_bus_off() { - bail!("Bus offline after start"); - } + if twai.is_bus_off() { + info!("Bus offline after start, attempting recovery"); + // Re-start to initiate recovery + twai = twai.stop().start(); + } + let res = (async { Timer::after_millis(10).await; let mut moistures = Moistures::default(); @@ -395,11 +397,13 @@ impl<'a> BoardInteraction<'a> for V4<'a> { let config = self.twai_config.take().expect("twai config not set"); let mut twai = config.into_async().start(); - let res = (async { - if twai.is_bus_off() { - bail!("Bus offline after start"); - } + if twai.is_bus_off() { + info!("Bus offline after start, attempting recovery"); + // Re-start to initiate recovery + twai = twai.stop().start(); + } + let res = (async { Timer::after_millis(1000).await; info!("Sending info messages now"); // Send a few test messages per potential sensor node