From 88bbc238b7da95162e4959a62d15c79bfef05149 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Wed, 26 Oct 2022 16:45:12 -0500 Subject: [PATCH] Set resolution directly --- embassy-stm32/src/adc/v2.rs | 7 +++---- embassy-stm32/src/adc/v3.rs | 15 ++++++--------- embassy-stm32/src/adc/v4.rs | 9 +++------ 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/embassy-stm32/src/adc/v2.rs b/embassy-stm32/src/adc/v2.rs index 30ac5d87..2cdaa00d 100644 --- a/embassy-stm32/src/adc/v2.rs +++ b/embassy-stm32/src/adc/v2.rs @@ -94,7 +94,6 @@ impl Prescaler { pub struct Adc<'d, T: Instance> { sample_time: SampleTime, - resolution: Resolution, phantom: PhantomData<&'d mut T>, } @@ -120,7 +119,6 @@ where Self { sample_time: Default::default(), - resolution: Resolution::default(), phantom: PhantomData, } } @@ -130,7 +128,9 @@ where } pub fn set_resolution(&mut self, resolution: Resolution) { - self.resolution = resolution; + unsafe { + T::regs().cr1().modify(|reg| reg.set_res(resolution.into())); + } } /// Enables internal voltage reference and returns [VrefInt], which can be used in @@ -214,7 +214,6 @@ where unsafe fn read_channel(&mut self, channel: u8) -> u16 { // Configure ADC - T::regs().cr1().modify(|reg| reg.set_res(self.resolution.into())); // Select channel T::regs().sqr3().write(|reg| reg.set_sq(0, channel)); diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs index 7962f241..c6898f54 100644 --- a/embassy-stm32/src/adc/v3.rs +++ b/embassy-stm32/src/adc/v3.rs @@ -62,7 +62,6 @@ impl super::sealed::AdcPin for Vbat { pub struct Adc<'d, T: Instance> { sample_time: SampleTime, - resolution: Resolution, phantom: PhantomData<&'d mut T>, } @@ -99,7 +98,6 @@ impl<'d, T: Instance> Adc<'d, T> { Self { sample_time: Default::default(), - resolution: Resolution::default(), phantom: PhantomData, } } @@ -145,7 +143,12 @@ impl<'d, T: Instance> Adc<'d, T> { } pub fn set_resolution(&mut self, resolution: Resolution) { - self.resolution = resolution; + unsafe { + #[cfg(not(stm32g0))] + T::regs().cfgr().modify(|reg| reg.set_res(resolution.into())); + #[cfg(stm32g0)] + T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); + } } /* @@ -197,12 +200,6 @@ impl<'d, T: Instance> Adc<'d, T> { // spin } - // Configure ADC - #[cfg(not(stm32g0))] - T::regs().cfgr().modify(|reg| reg.set_res(self.resolution.into())); - #[cfg(stm32g0)] - T::regs().cfgr1().modify(|reg| reg.set_res(self.resolution.into())); - // Configure channel Self::set_channel_sample_time(pin.channel(), self.sample_time); diff --git a/embassy-stm32/src/adc/v4.rs b/embassy-stm32/src/adc/v4.rs index dc5f62d3..750f3480 100644 --- a/embassy-stm32/src/adc/v4.rs +++ b/embassy-stm32/src/adc/v4.rs @@ -227,7 +227,6 @@ impl Prescaler { pub struct Adc<'d, T: Instance> { sample_time: SampleTime, - resolution: Resolution, phantom: PhantomData<&'d mut T>, } @@ -264,7 +263,6 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { let mut s = Self { sample_time: Default::default(), - resolution: Resolution::default(), phantom: PhantomData, }; s.power_up(delay); @@ -367,7 +365,9 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { } pub fn set_resolution(&mut self, resolution: Resolution) { - self.resolution = resolution; + unsafe { + T::regs().cfgr().modify(|reg| reg.set_res(resolution.into())); + } } /// Perform a single conversion. @@ -408,9 +408,6 @@ impl<'d, T: Instance + crate::rcc::RccPeripheral> Adc<'d, T> { } unsafe fn read_channel(&mut self, channel: u8) -> u16 { - // Configure ADC - T::regs().cfgr().modify(|reg| reg.set_res(self.resolution.into())); - // Configure channel Self::set_channel_sample_time(channel, self.sample_time);