From 7b9fe7e6398a8bce236da904b251b4cb424150fb Mon Sep 17 00:00:00 2001 From: crispaudio Date: Tue, 9 Sep 2025 22:21:10 +0200 Subject: mspm0-adc: remove dynamic vrsel and cleanup --- embassy-mspm0/build.rs | 55 ++++++++---------------------------------------- embassy-mspm0/src/adc.rs | 53 +++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 56 deletions(-) (limited to 'embassy-mspm0') diff --git a/embassy-mspm0/build.rs b/embassy-mspm0/build.rs index ad90b5223..4bfeb5626 100644 --- a/embassy-mspm0/build.rs +++ b/embassy-mspm0/build.rs @@ -225,52 +225,15 @@ fn generate_adc_constants() -> TokenStream { let vrsel = METADATA.adc_vrsel; let memctl = METADATA.adc_memctl; - if vrsel == 3 { - quote! { - pub const ADC_VRSEL: u8 = #vrsel; - pub const ADC_MEMCTL: u8 = #memctl; - - #[derive(Clone, Copy, PartialEq, Eq, Debug)] - #[cfg_attr(feature = "defmt", derive(defmt::Format))] - /// Reference voltage (Vref) selection for the ADC channels. - pub enum Vrsel { - /// VDDA reference - VddaVssa = 0, - - /// External reference from pin - ExtrefVrefm = 1, - - /// Internal reference - IntrefVssa = 2, - } - } - } else if vrsel == 5 { - quote! { - pub const ADC_VRSEL: u8 = #vrsel; - pub const ADC_MEMCTL: u8 = #memctl; - - #[derive(Clone, Copy, PartialEq, Eq, Debug)] - #[cfg_attr(feature = "defmt", derive(defmt::Format))] - /// Reference voltage (Vref) selection for the ADC channels. - pub enum Vrsel { - /// VDDA reference - VddaVssa = 0, - - /// External reference from pin - ExtrefVrefm = 1, - - /// Internal reference - IntrefVssa = 2, - - /// VDDA and VREFM connected to VREF+ and VREF- of ADC - VddaVrefm = 3, - - /// INTREF and VREFM connected to VREF+ and VREF- of ADC - IntrefVrefm = 4, - } - } - } else { - panic!("Unsupported ADC VRSEL value: {vrsel}"); + println!("cargo::rustc-check-cfg=cfg(adc_neg_vref)"); + match vrsel { + 3 => (), + 5 => println!("cargo:rustc-cfg=adc_neg_vref"), + _ => panic!("Unsupported ADC VRSEL value: {vrsel}"), + } + quote! { + pub const ADC_VRSEL: u8 = #vrsel; + pub const ADC_MEMCTL: u8 = #memctl; } } diff --git a/embassy-mspm0/src/adc.rs b/embassy-mspm0/src/adc.rs index e4ba81139..71569aef0 100644 --- a/embassy-mspm0/src/adc.rs +++ b/embassy-mspm0/src/adc.rs @@ -58,10 +58,32 @@ impl Resolution { } } -pub use crate::_generated::Vrsel; +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +/// Reference voltage (Vref) selection for the ADC channels. +pub enum Vrsel { + /// VDDA reference + VddaVssa = 0, + + /// External reference from pin + ExtrefVrefm = 1, + + /// Internal reference + IntrefVssa = 2, + + /// VDDA and VREFM connected to VREF+ and VREF- of ADC + #[cfg(adc_neg_vref)] + VddaVrefm = 3, + + /// INTREF and VREFM connected to VREF+ and VREF- of ADC + #[cfg(adc_neg_vref)] + IntrefVrefm = 4, +} /// ADC configuration. -pub struct AdcConfig { +#[derive(Copy, Clone)] +#[non_exhaustive] +pub struct Config { /// Resolution of the ADC conversion. The number of bits used to represent an ADC measurement. pub resolution: Resolution, /// ADC voltage reference selection. @@ -73,19 +95,29 @@ pub struct AdcConfig { pub sample_time: u16, } +impl Default for Config { + fn default() -> Self { + Self { + resolution: Resolution::BIT12, + vr_select: Vrsel::VddaVssa, + sample_time: 50, + } + } +} + /// ADC (Analog to Digial Converter) Driver. pub struct Adc<'d, T: Instance, M: Mode> { #[allow(unused)] adc: crate::Peri<'d, T>, info: &'static Info, state: &'static State, - config: AdcConfig, + config: Config, _phantom: PhantomData, } impl<'d, T: Instance> Adc<'d, T, Blocking> { /// A new blocking ADC driver instance. - pub fn new_blocking(peri: Peri<'d, T>, config: AdcConfig) -> Self { + pub fn new_blocking(peri: Peri<'d, T>, config: Config) -> Self { let mut this = Self { adc: peri, info: T::info(), @@ -102,10 +134,8 @@ impl<'d, T: Instance> Adc<'d, T, Async> { /// A new asynchronous ADC driver instance. pub fn new_async( peri: Peri<'d, T>, - config: AdcConfig, - _irqs: impl interrupt::typelevel::Binding> - + interrupt::typelevel::Binding> - + 'd, + config: Config, + _irqs: impl interrupt::typelevel::Binding> + 'd, ) -> Self { let mut this = Self { adc: peri, @@ -244,6 +274,9 @@ impl<'d, T: Instance, M: Mode> Adc<'d, T, M> { } impl<'d, T: Instance> Adc<'d, T, Async> { + /// Maximum length allowed for [`Self::read_sequence`]. + pub const MAX_SEQUENCE_LEN: usize = ADC_MEMCTL as usize; + async fn wait_for_conversion(&self) { let info = self.info; let state = self.state; @@ -337,9 +370,9 @@ impl<'d, T: Instance> Adc<'d, T, Async> { "Sequence length must be equal to readings length" ); assert!( - sequence.len() <= ADC_MEMCTL as usize, + sequence.len() <= Self::MAX_SEQUENCE_LEN, "Asynchronous read sequence cannot be more than {} in length", - ADC_MEMCTL + Self::MAX_SEQUENCE_LEN ); // CTL0.ENC must be 0 to write the MEMCTL register -- cgit