fix wrong Alert enum indexing (#533)

* fix wrong Alert enum indexing

closes #532

* remove TryFromPrimitive from Alert enum

Its wrong to derive TryFromPrimitive in this case, since the numbers in
this cases represent bitpositions

An example:

    ```rust #[derive(Debug, EnumSetType, TryFromPrimitive)]
    #[enumset(repr = "u32")] #[repr(u32)] enum MyType { One = 1, Two =
    2, Three = 3, } ``` would produce a wrong derive like ```rust impl
    ::num_enum::TryFromPrimitive for MyType { type Primitive = u32; type
    Error = ::num_enum::TryFromPrimitiveError<Self>; const NAME:
    &'static str = "MyType"; fn try_from_primitive( number:
    Self::Primitive, ) -> ::core::result::Result<Self,
    ::num_enum::TryFromPrimitiveError<Self>> {
    #![allow(non_upper_case_globals)] const One__num_enum_0__: u32 = 1;
    const Two__num_enum_0__: u32 = 2; const Three__num_enum_0__: u32 =
    3; #[deny(unreachable_patterns)] match number { One__num_enum_0__ =>
    ::core::result::Result::Ok(Self::One), Two__num_enum_0__ =>
    ::core::result::Result::Ok(Self::Two), Three__num_enum_0__ =>
    ::core::result::Result::Ok(Self::Three),
    #[allow(unreachable_patterns)] _ => { ::core::result::Result::Err(
    ::num_enum::TryFromPrimitiveError::<Self>::new(number), ) } } } }
    impl ::core::convert::TryFrom<u32> for MyType { type Error =
    ::num_enum::TryFromPrimitiveError<Self>; #[inline] fn try_from(
    number: u32, ) -> ::core::result::Result<Self,
    ::num_enum::TryFromPrimitiveError<Self>> {
    ::num_enum::TryFromPrimitive::try_from_primitive(number) } } ```

* remove not needed num_enum dependency

* changelog
This commit is contained in:
Frederick Vollbrecht 2025-07-10 17:19:36 +02:00 committed by GitHub
parent 7117923788
commit a371755b8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 36 deletions

View File

@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix the SDMMC driver for ESP-IDF V5.5+
- Replace Arc with Rc in ledc_threads example (#514)
- Fix outdated task docs
- CAN: fix wrong Alert enum indexing / remove wrong TryFromPrimitive derive (#532)
## [0.45.2] - 2025-01-15

View File

@ -55,7 +55,6 @@ embedded-io-async = "0.6"
esp-idf-sys = { version = "0.36", default-features = false }
critical-section = { version = "1.1.1", optional = true, features = ["restore-state-none"] }
heapless = "0.8"
num_enum = { version = "0.7", default-features = false }
enumset = { version = "1.1.4", default-features = false }
log = { version = "0.4", default-features = false }
atomic-waker = { version = "1.1.1", default-features = false }

View File

@ -41,8 +41,6 @@ use enumset::{EnumSet, EnumSetType};
use esp_idf_sys::*;
use num_enum::TryFromPrimitive;
use crate::cpu::Core;
use crate::delay::{self, BLOCK, NON_BLOCK};
use crate::interrupt::InterruptType;
@ -360,28 +358,46 @@ pub mod config {
}
}
#[derive(Debug, EnumSetType, TryFromPrimitive)]
#[derive(Debug, EnumSetType)]
#[enumset(repr = "u32")]
#[repr(u32)]
pub enum Alert {
TransmitIdle = 1,
Success = 2,
Received = 3,
BelowErrorWarning = 4,
ActiveError = 5,
RecoveryInProgress = 6,
BusRecovered = 7,
ArbLost = 8,
AboveErrorWarning = 9,
BusError = 10,
TransmitFailed = 11,
ReceiveQueueFull = 12,
ErrorPass = 13,
BusOffline = 14,
ReceiveFifoOverflow = 15,
TransmitRetried = 16,
PeripheralReset = 17,
AlertAndLog = 18,
/// No more messages to transmit
TransmitIdle = 0,
/// The previous transmission was successful
Success = 1,
/// A frame has been received and added to the RX queue
Received = 2,
/// Both error counters have dropped below error warning limit
BelowErrorWarning = 3,
/// TWAI controller has become error active
ActiveError = 4,
/// TWAI controller is undergoing bus recovery
RecoveryInProgress = 5,
/// TWAI controller has successfully completed bus recovery
BusRecovered = 6,
/// The previous transmission lost arbitration
ArbLost = 7,
/// One of the error counters have exceeded the error warning limit
AboveErrorWarning = 8,
/// A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus
BusError = 9,
/// The previous transmission has failed (for single shot transmission)
TransmitFailed = 10,
/// The RX queue is full causing a frame to be lost
ReceiveQueueFull = 11,
/// TWAI controller has become error passive
ErrorPass = 12,
/// Bus-off condition occurred. TWAI controller can no longer influence bus
BusOffline = 13,
/// An RX FIFO overrun has occurred
ReceiveFifoOverflow = 14,
/// An message transmission was cancelled and retried due to an errata workaround
TransmitRetried = 15,
/// The TWAI controller was reset
PeripheralReset = 16,
/// In addition to alerting also Logs the event
AlertAndLog = 17,
}
/// CAN abstraction