diff options
Diffstat (limited to 'embassy-stm32/src/adc/v1.rs')
| -rw-r--r-- | embassy-stm32/src/adc/v1.rs | 59 |
1 files changed, 20 insertions, 39 deletions
diff --git a/embassy-stm32/src/adc/v1.rs b/embassy-stm32/src/adc/v1.rs index d09374876..58c30935f 100644 --- a/embassy-stm32/src/adc/v1.rs +++ b/embassy-stm32/src/adc/v1.rs | |||
| @@ -5,11 +5,12 @@ use core::task::Poll; | |||
| 5 | #[cfg(adc_l0)] | 5 | #[cfg(adc_l0)] |
| 6 | use stm32_metapac::adc::vals::Ckmode; | 6 | use stm32_metapac::adc::vals::Ckmode; |
| 7 | 7 | ||
| 8 | use super::blocking_delay_us; | 8 | #[cfg(not(adc_l0))] |
| 9 | use super::Vbat; | ||
| 10 | use super::{Temperature, VrefInt, blocking_delay_us}; | ||
| 9 | use crate::adc::{Adc, AdcChannel, Instance, Resolution, SampleTime}; | 11 | use crate::adc::{Adc, AdcChannel, Instance, Resolution, SampleTime}; |
| 10 | use crate::interrupt::typelevel::Interrupt; | 12 | use crate::interrupt::typelevel::Interrupt; |
| 11 | use crate::peripherals::ADC1; | 13 | use crate::{Peri, interrupt, rcc}; |
| 12 | use crate::{interrupt, rcc, Peri}; | ||
| 13 | 14 | ||
| 14 | mod watchdog_v1; | 15 | mod watchdog_v1; |
| 15 | pub use watchdog_v1::WatchdogChannels; | 16 | pub use watchdog_v1::WatchdogChannels; |
| @@ -42,36 +43,23 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl | |||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | #[cfg(not(adc_l0))] | 45 | #[cfg(not(adc_l0))] |
| 45 | pub struct Vbat; | 46 | impl super::SealedSpecialConverter<super::Vbat> for crate::peripherals::ADC1 { |
| 46 | 47 | const CHANNEL: u8 = 18; | |
| 47 | #[cfg(not(adc_l0))] | 48 | } |
| 48 | impl AdcChannel<ADC1> for Vbat {} | ||
| 49 | 49 | ||
| 50 | #[cfg(not(adc_l0))] | 50 | #[cfg(not(adc_l0))] |
| 51 | impl super::SealedAdcChannel<ADC1> for Vbat { | 51 | impl super::SealedSpecialConverter<super::VrefInt> for crate::peripherals::ADC1 { |
| 52 | fn channel(&self) -> u8 { | 52 | const CHANNEL: u8 = 17; |
| 53 | 18 | ||
| 54 | } | ||
| 55 | } | 53 | } |
| 56 | 54 | ||
| 57 | pub struct Vref; | 55 | #[cfg(adc_l0)] |
| 58 | impl AdcChannel<ADC1> for Vref {} | 56 | impl super::SealedSpecialConverter<super::VrefInt> for crate::peripherals::ADC1 { |
| 59 | impl super::SealedAdcChannel<ADC1> for Vref { | 57 | const CHANNEL: u8 = 18; |
| 60 | fn channel(&self) -> u8 { | ||
| 61 | 17 | ||
| 62 | } | ||
| 63 | } | 58 | } |
| 64 | 59 | ||
| 65 | pub struct Temperature; | 60 | #[cfg(not(adc_l0))] |
| 66 | impl AdcChannel<ADC1> for Temperature {} | 61 | impl super::SealedSpecialConverter<super::Temperature> for crate::peripherals::ADC1 { |
| 67 | impl super::SealedAdcChannel<ADC1> for Temperature { | 62 | const CHANNEL: u8 = 16; |
| 68 | fn channel(&self) -> u8 { | ||
| 69 | if cfg!(adc_l0) { | ||
| 70 | 18 | ||
| 71 | } else { | ||
| 72 | 16 | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | 63 | } |
| 76 | 64 | ||
| 77 | impl<'d, T: Instance> Adc<'d, T> { | 65 | impl<'d, T: Instance> Adc<'d, T> { |
| @@ -118,10 +106,7 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 118 | T::Interrupt::enable(); | 106 | T::Interrupt::enable(); |
| 119 | } | 107 | } |
| 120 | 108 | ||
| 121 | Self { | 109 | Self { adc } |
| 122 | adc, | ||
| 123 | sample_time: SampleTime::from_bits(0), | ||
| 124 | } | ||
| 125 | } | 110 | } |
| 126 | 111 | ||
| 127 | #[cfg(not(adc_l0))] | 112 | #[cfg(not(adc_l0))] |
| @@ -134,12 +119,12 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 134 | Vbat | 119 | Vbat |
| 135 | } | 120 | } |
| 136 | 121 | ||
| 137 | pub fn enable_vref(&self) -> Vref { | 122 | pub fn enable_vref(&self) -> VrefInt { |
| 138 | // Table 28. Embedded internal reference voltage | 123 | // Table 28. Embedded internal reference voltage |
| 139 | // tstart = 10μs | 124 | // tstart = 10μs |
| 140 | T::regs().ccr().modify(|reg| reg.set_vrefen(true)); | 125 | T::regs().ccr().modify(|reg| reg.set_vrefen(true)); |
| 141 | blocking_delay_us(10); | 126 | blocking_delay_us(10); |
| 142 | Vref | 127 | VrefInt |
| 143 | } | 128 | } |
| 144 | 129 | ||
| 145 | pub fn enable_temperature(&self) -> Temperature { | 130 | pub fn enable_temperature(&self) -> Temperature { |
| @@ -153,10 +138,6 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 153 | Temperature | 138 | Temperature |
| 154 | } | 139 | } |
| 155 | 140 | ||
| 156 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { | ||
| 157 | self.sample_time = sample_time; | ||
| 158 | } | ||
| 159 | |||
| 160 | pub fn set_resolution(&mut self, resolution: Resolution) { | 141 | pub fn set_resolution(&mut self, resolution: Resolution) { |
| 161 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); | 142 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); |
| 162 | } | 143 | } |
| @@ -167,12 +148,13 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 167 | T::regs().cfgr2().modify(|reg| reg.set_ckmode(ckmode)); | 148 | T::regs().cfgr2().modify(|reg| reg.set_ckmode(ckmode)); |
| 168 | } | 149 | } |
| 169 | 150 | ||
| 170 | pub async fn read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 { | 151 | pub async fn read(&mut self, channel: &mut impl AdcChannel<T>, sample_time: SampleTime) -> u16 { |
| 171 | let ch_num = channel.channel(); | 152 | let ch_num = channel.channel(); |
| 172 | channel.setup(); | 153 | channel.setup(); |
| 173 | 154 | ||
| 174 | // A.7.5 Single conversion sequence code example - Software trigger | 155 | // A.7.5 Single conversion sequence code example - Software trigger |
| 175 | T::regs().chselr().write(|reg| reg.set_chsel_x(ch_num as usize, true)); | 156 | T::regs().chselr().write(|reg| reg.set_chsel_x(ch_num as usize, true)); |
| 157 | T::regs().smpr().modify(|reg| reg.set_smp(sample_time.into())); | ||
| 176 | 158 | ||
| 177 | self.convert().await | 159 | self.convert().await |
| 178 | } | 160 | } |
| @@ -183,7 +165,6 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 183 | reg.set_eosmp(true); | 165 | reg.set_eosmp(true); |
| 184 | }); | 166 | }); |
| 185 | 167 | ||
| 186 | T::regs().smpr().modify(|reg| reg.set_smp(self.sample_time.into())); | ||
| 187 | T::regs().ier().modify(|w| w.set_eocie(true)); | 168 | T::regs().ier().modify(|w| w.set_eocie(true)); |
| 188 | T::regs().cr().modify(|reg| reg.set_adstart(true)); | 169 | T::regs().cr().modify(|reg| reg.set_adstart(true)); |
| 189 | 170 | ||
