esp-rs / esp-hal

no_std Hardware Abstraction Layers for ESP32 microcontrollers
https://docs.esp-rs.org/esp-hal/
Apache License 2.0
716 stars 194 forks source link

TWAI was panicked for ESP32-S3 #2252

Closed TuEmb closed 23 minutes ago

TuEmb commented 3 hours ago

Bug description

image Twai driver crashes with invalid DLC. I'm using ESP32-S3 to receive CAN packets using embassy-twai example, and sometimes I encounter this error.

From my debugging, this issue occurs when TWAI receives a frame with DLC > 8 (which is unexpected because my system uses Basic CAN, where no frame should have DLC > 8). The issue is difficult to reproduce since it randomly occurs after 30 minutes or more. I will continue debugging to identify the root cause.

Do you guys have any ideas with that case ?

To Reproduce

T.B.D

Expected behavior

We need to handle the case where DLC is greater than 8 because it's possible. I checked esp-idf, and they handle the DLC by adding a bit to indicate DLC_NON_COMP in their TWAI frame struct. Therefore, I suggest adding the following:

pub enum EspTwaiError {
    /// TWAI peripheral has entered a bus-off state.
    BusOff,
    /// Data length code is larger than 8. This will break compliance with ISO 11898-1.
    DlcNoneComp,
    /// Encapsulates errors defined by the embedded-hal crate.
    EmbeddedHAL(ErrorKind),
}

Environment

bugadani commented 3 hours ago

Hello, thank you for your report. ~Please include which esp-hal version you're using.~ Current git main seems affected.

TuEmb commented 1 hour ago

Yes, current latest main branch also occurs. I also tested with esp-hal 0.20.1. I just added version 0.20.1 on description