{"record":{"id":"017b0d97946495ae","repo":"embassy-rs/embassy","slug":"invalid-channel-to-sample","errorCode":null,"errorMessage":"Invalid channel to sample","messagePattern":"Invalid channel to sample","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"embassy-stm32/src/adc/l1.rs","lineNumber":188,"sourceCode":"        for (i, ((ch, _), sample_time)) in sequence.enumerate() {\n            if !injected {\n                match i {\n                    0..=5 => sqr5.set_sq(i, ch),\n                    6..=11 => sqr4.set_sq(i - 6, ch),\n                    12..=15 => sqr3.set_sq(i - 12, ch),\n                    18..=23 => sqr2.set_sq(i - 18, ch),\n                    24..=27 => sqr1.set_sq(i - 24, ch),\n                    _ => unreachable!(),\n                }\n            }\n\n            let sample_time = sample_time.into();\n            match ch {\n                0..=9 => smpr3.set_smp(ch as _, sample_time),\n                10..=19 => smpr2.set_smp(ch as usize - 10, sample_time),\n                20..=29 => smpr1.set_smp(ch as usize - 20, sample_time),\n                30..=31 => smpr0.set_smp(ch as usize - 30, sample_time),\n                _ => panic!(\"Invalid channel to sample\"),\n            }\n        }\n\n        if !injected {\n            self.sqr1().write_value(sqr1);\n            self.sqr2().write_value(sqr2);\n            self.sqr3().write_value(sqr3);\n            self.sqr4().write_value(sqr4);\n            self.sqr5().write_value(sqr5);\n        }\n\n        self.smpr0().write_value(smpr0);\n        self.smpr1().write_value(smpr1);\n        self.smpr2().write_value(smpr2);\n        self.smpr3().write_value(smpr3);\n    }\n}\n","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/embassy-rs/embassy/blob/463a07b963419a1bfe61d5d597c44acb810afb8b/embassy-stm32/src/adc/l1.rs#L170-L206","documentation":"The L1 ADC sequence configurator writes sample-time fields across four registers (SMPR3..SMPR0) that collectively cover only channels 0..=31. A channel number outside that range has no SMPR slot, so the driver panics rather than silently misconfiguring the ADC.","triggerScenarios":"Calling `Adc::configure_sequence` (via `sample_sequence`/`read_sequence`) with a sequence containing a channel number > 31, or a channel value that failed conversion to a valid L1 ADC channel.","commonSituations":"Passing a raw `u8` channel number instead of a typed channel; accidentally using channel enums/indices from another STM32 family (e.g. H7 has higher channel numbers); off-by-one or wrong pin-to-channel mapping.","solutions":["Use only channel values 0..=31 that exist on your L1 device (derive them from embassy's Channel types per pin)","Check the pin-to-channel mapping in the L1 datasheet; fix wrong pin assignments","Ensure you are using embassy-stm32's L1 ADC channel types, not hand-crafted numeric channel IDs"],"exampleFix":"// before\nadc.sample_sequence(&[Some(unsafe { AnyChannel::new(40) })]); // invalid channel 40\n// after\nadc.sample_sequence(&[p.PA0]); // valid L1 ADC pin/channel","handlingStrategy":"validation","validationCode":"fn valid_channel(ch: u8) -> bool { ch <= 31 }\n// call before configure_sequence\nassert!(sequence.iter().all(|ch| valid_channel(*ch)), \"L1 ADC channels must be 0..=31\");","typeGuard":"fn is_valid_l1_adc_channel(ch: u8) -> bool { ch <= 31 }","tryCatchPattern":null,"preventionTips":["Use embassy's typed Channel/Pin types instead of raw numbers","Double-check pin-to-channel mapping in the L1 datasheet","Never copy channel numbers from other STM32 families","Write a small unit test validating sequence channels before flashing"],"tags":["embedded","stm32","adc","rust"],"backgroundTag":"invalid-argument-value","analyzedSha":"463a07b963419a1bfe61d5d597c44acb810afb8b","analyzedAt":"2026-09-10T13:38:26.660Z","contentChangedAt":"2026-09-10T13:38:26.660Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}