Improve CAN bus error handling and logging

- Enhanced error detection with detailed status logging for bus-off, error warning, and passive errors.
- Added line breaks to CAN and RX error logs for better readability.
- Refined CAN transmission logic and error feedback, including buffer overflow handling.
- Simplified firmware timestamp frame creation and ensured successful sending.
This commit is contained in:
2026-05-01 13:11:37 +02:00
parent 2ee3615dcd
commit a30d59605d

View File

@@ -415,7 +415,7 @@ async fn can_task(
Timer::after_millis(100).await; Timer::after_millis(100).await;
} }
let mut msg: heapless::String<128> = heapless::String::new(); let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(&mut msg, "rx err {:?}", err); let _ = write!(&mut msg, "rx err {:?} \r\n", err);
log(msg); log(msg);
} }
} }
@@ -429,14 +429,25 @@ async fn can_task(
} }
} }
// Check CAN error status register for bus-off condition
if hal::pac::CAN1.errsr().read().boff() {
blink_error_loop(info, warn, 3, 3).await; // Bus-off error
}
while let Ok(mut frame) = CAN_TX_CH.try_receive() { while let Ok(mut frame) = CAN_TX_CH.try_receive() {
match can.transmit(&mut frame) { match can.transmit(&mut frame) {
Ok(..) => { Ok(_ok) => {
let status = hal::pac::CAN1.errsr().read();
// Check CAN error status register for bus-off condition
if status.boff() || status.ewgf() || status.epvf() {
let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(&mut msg, "canbus status {} {} {} \r\n", status.boff(), status.ewgf(), status.epvf());
log(msg);
for _ in 0..2 {
warn.set_high();
Timer::after_millis(100).await;
warn.set_low();
Timer::after_millis(100).await;
}
}
} }
Err(nb::Error::WouldBlock) => { Err(nb::Error::WouldBlock) => {
for _ in 0..2 { for _ in 0..2 {
@@ -446,7 +457,7 @@ async fn can_task(
Timer::after_millis(100).await; Timer::after_millis(100).await;
} }
let mut msg: heapless::String<128> = heapless::String::new(); let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(&mut msg, "canbus out buffer full"); let _ = write!(&mut msg, "canbus out buffer full \r\n");
log(msg); log(msg);
} }
Err(nb::Error::Other(err)) => { Err(nb::Error::Other(err)) => {
@@ -457,7 +468,7 @@ async fn can_task(
Timer::after_millis(100).await; Timer::after_millis(100).await;
} }
let mut msg: heapless::String<128> = heapless::String::new(); let mut msg: heapless::String<128> = heapless::String::new();
let _ = write!(&mut msg, "tx err {:?}", err); let _ = write!(&mut msg, "tx err {:?} \r\n", err);
log(msg); log(msg);
} }
} }
@@ -516,7 +527,7 @@ async fn worker(
loop { loop {
let mut total_pulses: u32 = 0; let mut total_pulses: u32 = 0;
for _ in 0..AVG_WINDOWS { for _window in 0..AVG_WINDOWS {
// Count rising edges of Q in a 100 ms window // Count rising edges of Q in a 100 ms window
let start = Instant::now(); let start = Instant::now();
let mut pulses: u32 = 0; let mut pulses: u32 = 0;
@@ -586,9 +597,8 @@ async fn worker(
// Send firmware build timestamp after each measurement so the controller // Send firmware build timestamp after each measurement so the controller
// always has up-to-date build info without requiring an identify request. // always has up-to-date build info without requiring an identify request.
if let Some(build_frame) = CanFrame::new(firmware_build_id, &FIRMWARE_BUILD_MINUTES.to_be_bytes()) { let firmware = CanFrame::new(firmware_build_id, &FIRMWARE_BUILD_MINUTES.to_be_bytes()).unwrap();
CAN_TX_CH.send(build_frame).await; CAN_TX_CH.send(firmware).await;
}
// Wait for the other slot to measure, plus gaps to ensure no overlap // Wait for the other slot to measure, plus gaps to ensure no overlap
// After A finishes measuring: wait 50ms (gap) + 400ms (B measures) + 50ms (gap) = 500ms // After A finishes measuring: wait 50ms (gap) + 400ms (B measures) + 50ms (gap) = 500ms