embassy-rs/embassy · error

Invalid value

Error message

Invalid value {}

What it means

This From<u8> impl converts the LED-function field of the ADIN1110 LED control register into the LedFunc enum; a value outside the 0–28 documented mapping panics with "Invalid value". The library throws it because the hardware field was expected to contain only datasheet-defined LED function codes.

Solutions

  1. Only pass values 0–28 into LedFunc::from; validate the field before converting.
  2. Check bit-extraction code — ensure you are masking/shifting to the correct LED function field.
  3. If a new silicon revision defines extra codes, extend the match table or switch to a fallible TryFrom<u8>.

Example fix

// before
let func = LedFunc::from(value); // panics if value > 28
// after
let func = LedFunc::try_from(value).unwrap_or(LedFunc::Off); // or handle the error
Defensive patterns

Strategy: validation

Validate before calling

// Rust: check the LED-function field range before conversion
fn is_valid_led_func(v: u8) -> bool { v <= 28 }

let field = (led_ctrl >> shift) & mask;
assert!(is_valid_led_func(field), "LED func {} out of range 0..=28", field);

Type guard

// Rust: fallible conversion guard
fn to_led_func(v: u8) -> Option<LedFunc> {
    (v <= 28).then(|| LedFunc::from(v))
}

Prevention

When it happens

Trigger: Reading the LED control register and converting its LED-function bits via LedFunc::from when the field holds a code > 28 (or a reserved value), or writing an out-of-range value into the field and echoing it back through the conversion.

Common situations: Constructing LED configuration from user-provided numbers; reading back reserved codes on newer silicon revisions; accidentally shifting the LED-function field and feeding the wrong bits into the conversion.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of embassy-rs/embassy@463a07b963 (2026-09-10). Data as JSON: /api/errors/45a4f9743fa39b3c. Report an issue: GitHub.

Appendix: source

Thrown at embassy-net-adin1110/src/regs.rs:355

            11 => LedFunc::TxSop,
            12 => LedFunc::RxSop,
            13 => LedFunc::On,
            14 => LedFunc::Off,
            15 => LedFunc::Blink,
            16 => LedFunc::TxLevel2P4,
            17 => LedFunc::TxLevel1P0,
            18 => LedFunc::Master,
            19 => LedFunc::Slave,
            20 => LedFunc::IncompatiableLinkCfg,
            21 => LedFunc::AnLinkGood,
            22 => LedFunc::AnComplete,
            23 => LedFunc::TsTimer,
            24 => LedFunc::LocRcvrStatus,
            25 => LedFunc::RemRcvrStatus,
            26 => LedFunc::Clk25Ref,
            27 => LedFunc::TxTCLK,
            28 => LedFunc::Clk120MHz,
            e => panic!("Invalid value {}", e),
        }
    }
}

/// LED Control Register
#[cfg_attr(not(feature = "generic-spi"), allow(dead_code))]
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct LedCntrl(pub u16);
bitfield_bitrange! {struct LedCntrl(u16)}

impl LedCntrl {
    bitfield_fields! {
        u8;
        /// LED 0 Pin Function
        pub from into LedFunc, led0_function, set_led0_function: 4, 0;
        /// LED 0 Mode Selection
        pub led0_mode, set_led0_mode: 5;
        /// Qualify Certain LED 0 Options with Link Status.

View on GitHub (pinned to 463a07b963)