diff options
| author | klownfish <[email protected]> | 2024-09-24 19:03:20 +0200 |
|---|---|---|
| committer | klownfish <[email protected]> | 2024-09-24 19:03:20 +0200 |
| commit | 3ce40f41fb25d7e473fc4a9584d6d9273ef08403 (patch) | |
| tree | 9471272c431e732634202eebf96a7d5e49b3816e | |
| parent | 376fc86a191e345a6d6cab23131aa8cbce90b638 (diff) | |
WIP: add u5 adc4
| -rw-r--r-- | embassy-stm32/build.rs | 9 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/mod.rs | 38 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/u5.rs | 16 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/u5_adc4.rs | 384 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 20 |
5 files changed, 450 insertions, 17 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 19cf193d9..e4cd001e6 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs | |||
| @@ -1188,13 +1188,12 @@ fn main() { | |||
| 1188 | // ======== | 1188 | // ======== |
| 1189 | // Generate dma_trait_impl! | 1189 | // Generate dma_trait_impl! |
| 1190 | 1190 | ||
| 1191 | let signals: HashMap<_, _> = [ | 1191 | let mut signals: HashMap<_, _> = [ |
| 1192 | // (kind, signal) => trait | 1192 | // (kind, signal) => trait |
| 1193 | (("adc", "ADC"), quote!(crate::adc::RxDma)), | 1193 | (("adc", "ADC"), quote!(crate::adc::RxDma)), |
| 1194 | (("adc", "ADC1"), quote!(crate::adc::RxDma)), | 1194 | (("adc", "ADC1"), quote!(crate::adc::RxDma)), |
| 1195 | (("adc", "ADC2"), quote!(crate::adc::RxDma)), | 1195 | (("adc", "ADC2"), quote!(crate::adc::RxDma)), |
| 1196 | (("adc", "ADC3"), quote!(crate::adc::RxDma)), | 1196 | (("adc", "ADC3"), quote!(crate::adc::RxDma)), |
| 1197 | (("adc", "ADC4"), quote!(crate::adc::RxDma)), | ||
| 1198 | (("ucpd", "RX"), quote!(crate::ucpd::RxDma)), | 1197 | (("ucpd", "RX"), quote!(crate::ucpd::RxDma)), |
| 1199 | (("ucpd", "TX"), quote!(crate::ucpd::TxDma)), | 1198 | (("ucpd", "TX"), quote!(crate::ucpd::TxDma)), |
| 1200 | (("usart", "RX"), quote!(crate::usart::RxDma)), | 1199 | (("usart", "RX"), quote!(crate::usart::RxDma)), |
| @@ -1228,6 +1227,12 @@ fn main() { | |||
| 1228 | ] | 1227 | ] |
| 1229 | .into(); | 1228 | .into(); |
| 1230 | 1229 | ||
| 1230 | if chip_name.starts_with("stm32u5") { | ||
| 1231 | signals.insert(("adc", "ADC4"), quote!(crate::adc::RxDma4)); | ||
| 1232 | } else { | ||
| 1233 | signals.insert(("adc", "ADC4"), quote!(crate::adc::RxDma)); | ||
| 1234 | } | ||
| 1235 | |||
| 1231 | for p in METADATA.peripherals { | 1236 | for p in METADATA.peripherals { |
| 1232 | if let Some(regs) = &p.registers { | 1237 | if let Some(regs) = &p.registers { |
| 1233 | // FIXME: stm32u5a crash on Cordic driver | 1238 | // FIXME: stm32u5a crash on Cordic driver |
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 3bd7c793d..80c942816 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs | |||
| @@ -25,6 +25,10 @@ pub use _version::*; | |||
| 25 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] | 25 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] |
| 26 | use embassy_sync::waitqueue::AtomicWaker; | 26 | use embassy_sync::waitqueue::AtomicWaker; |
| 27 | 27 | ||
| 28 | #[cfg(adc_u5)] | ||
| 29 | #[path = "u5_adc4.rs"] | ||
| 30 | pub mod adc4; | ||
| 31 | |||
| 28 | pub use crate::pac::adc::vals; | 32 | pub use crate::pac::adc::vals; |
| 29 | #[cfg(not(any(adc_f1, adc_f3_v2)))] | 33 | #[cfg(not(any(adc_f1, adc_f3_v2)))] |
| 30 | pub use crate::pac::adc::vals::Res as Resolution; | 34 | pub use crate::pac::adc::vals::Res as Resolution; |
| @@ -32,6 +36,8 @@ pub use crate::pac::adc::vals::SampleTime; | |||
| 32 | use crate::peripherals; | 36 | use crate::peripherals; |
| 33 | 37 | ||
| 34 | dma_trait!(RxDma, Instance); | 38 | dma_trait!(RxDma, Instance); |
| 39 | #[cfg(adc_u5)] | ||
| 40 | dma_trait!(RxDma4, adc4::Instance); | ||
| 35 | 41 | ||
| 36 | /// Analog to Digital driver. | 42 | /// Analog to Digital driver. |
| 37 | pub struct Adc<'d, T: Instance> { | 43 | pub struct Adc<'d, T: Instance> { |
| @@ -159,6 +165,38 @@ impl<T: Instance> SealedAdcChannel<T> for AnyAdcChannel<T> { | |||
| 159 | } | 165 | } |
| 160 | } | 166 | } |
| 161 | 167 | ||
| 168 | #[cfg(adc_u5)] | ||
| 169 | foreach_adc!( | ||
| 170 | (ADC4, $common_inst:ident, $clock:ident) => { | ||
| 171 | impl crate::adc::adc4::SealedInstance for peripherals::ADC4 { | ||
| 172 | fn regs() -> crate::pac::adc::Adc4 { | ||
| 173 | crate::pac::ADC4 | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | impl crate::adc::adc4::Instance for peripherals::ADC4 { | ||
| 178 | type Interrupt = crate::_generated::peripheral_interrupts::ADC4::GLOBAL; | ||
| 179 | } | ||
| 180 | }; | ||
| 181 | |||
| 182 | ($inst:ident, $common_inst:ident, $clock:ident) => { | ||
| 183 | impl crate::adc::SealedInstance for peripherals::$inst { | ||
| 184 | fn regs() -> crate::pac::adc::Adc { | ||
| 185 | crate::pac::$inst | ||
| 186 | } | ||
| 187 | |||
| 188 | fn common_regs() -> crate::pac::adccommon::AdcCommon { | ||
| 189 | return crate::pac::$common_inst | ||
| 190 | } | ||
| 191 | } | ||
| 192 | |||
| 193 | impl crate::adc::Instance for peripherals::$inst { | ||
| 194 | type Interrupt = crate::_generated::peripheral_interrupts::$inst::GLOBAL; | ||
| 195 | } | ||
| 196 | }; | ||
| 197 | ); | ||
| 198 | |||
| 199 | #[cfg(not(adc_u5))] | ||
| 162 | foreach_adc!( | 200 | foreach_adc!( |
| 163 | ($inst:ident, $common_inst:ident, $clock:ident) => { | 201 | ($inst:ident, $common_inst:ident, $clock:ident) => { |
| 164 | impl crate::adc::SealedInstance for peripherals::$inst { | 202 | impl crate::adc::SealedInstance for peripherals::$inst { |
diff --git a/embassy-stm32/src/adc/u5.rs b/embassy-stm32/src/adc/u5.rs index a86638a60..314cb02e2 100644 --- a/embassy-stm32/src/adc/u5.rs +++ b/embassy-stm32/src/adc/u5.rs | |||
| @@ -11,7 +11,7 @@ use crate::{pac, rcc, Peripheral}; | |||
| 11 | 11 | ||
| 12 | const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55); | 12 | const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55); |
| 13 | 13 | ||
| 14 | const VREF_CHANNEL: u8 = 1; | 14 | const VREF_CHANNEL: u8 = 0; |
| 15 | const VBAT_CHANNEL: u8 = 18; | 15 | const VBAT_CHANNEL: u8 = 18; |
| 16 | const TEMP_CHANNEL: u8 = 19; | 16 | const TEMP_CHANNEL: u8 = 19; |
| 17 | 17 | ||
| @@ -132,23 +132,9 @@ pub enum Averaging { | |||
| 132 | Samples1024, | 132 | Samples1024, |
| 133 | } | 133 | } |
| 134 | 134 | ||
| 135 | // TODO | ||
| 136 | // impl Instance for ADC4 { | ||
| 137 | |||
| 138 | // } | ||
| 139 | |||
| 140 | impl<'d, T: Instance> Adc<'d, T> { | 135 | impl<'d, T: Instance> Adc<'d, T> { |
| 141 | /// Create a new ADC driver. | 136 | /// Create a new ADC driver. |
| 142 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { | 137 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { |
| 143 | // move to u5 init (RCC)? | ||
| 144 | PWR.svmcr().modify(|w| { | ||
| 145 | w.set_avm1en(true); | ||
| 146 | }); | ||
| 147 | while !PWR.svmsr().read().vdda1rdy() {} | ||
| 148 | PWR.svmcr().modify(|w| { | ||
| 149 | w.set_asv(true); | ||
| 150 | }); | ||
| 151 | |||
| 152 | embassy_hal_internal::into_ref!(adc); | 138 | embassy_hal_internal::into_ref!(adc); |
| 153 | rcc::enable_and_reset::<T>(); | 139 | rcc::enable_and_reset::<T>(); |
| 154 | let prescaler = Prescaler::from_ker_ck(T::frequency()); | 140 | let prescaler = Prescaler::from_ker_ck(T::frequency()); |
diff --git a/embassy-stm32/src/adc/u5_adc4.rs b/embassy-stm32/src/adc/u5_adc4.rs new file mode 100644 index 000000000..5dec0caa9 --- /dev/null +++ b/embassy-stm32/src/adc/u5_adc4.rs | |||
| @@ -0,0 +1,384 @@ | |||
| 1 | pub use crate::pac::adc::vals::Adc4Res as Resolution; | ||
| 2 | pub use crate::pac::adc::vals::Adc4SampleTime as SampleTime; | ||
| 3 | pub use crate::pac::adc::vals::Adc4Presc as Presc; | ||
| 4 | pub use crate::pac::adc::regs::Adc4Chselrmod0; | ||
| 5 | |||
| 6 | #[allow(unused)] | ||
| 7 | use pac::adc::vals::{Adc4Exten, Adc4OversamplingRatio}; | ||
| 8 | |||
| 9 | use super::{ | ||
| 10 | blocking_delay_us, AdcChannel, SealedAdcChannel | ||
| 11 | }; | ||
| 12 | use crate::time::Hertz; | ||
| 13 | use crate::{pac, rcc, Peripheral}; | ||
| 14 | |||
| 15 | const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55); | ||
| 16 | |||
| 17 | /// Default VREF voltage used for sample conversion to millivolts. | ||
| 18 | pub const VREF_DEFAULT_MV: u32 = 3300; | ||
| 19 | /// VREF voltage used for factory calibration of VREFINTCAL register. | ||
| 20 | pub const VREF_CALIB_MV: u32 = 3300; | ||
| 21 | |||
| 22 | const VREF_CHANNEL: u8 = 0; | ||
| 23 | const VCORE_CHANNEL: u8 = 12; | ||
| 24 | const TEMP_CHANNEL: u8 = 13; | ||
| 25 | const VBAT_CHANNEL: u8 = 14; | ||
| 26 | const DAC_CHANNEL: u8 = 21; | ||
| 27 | |||
| 28 | // NOTE: Vrefint/Temperature/Vbat are not available on all ADCs, this currently cannot be modeled with stm32-data, so these are available from the software on all ADCs | ||
| 29 | /// Internal voltage reference channel. | ||
| 30 | pub struct VrefInt; | ||
| 31 | impl<T: Instance> AdcChannel<T> for VrefInt {} | ||
| 32 | impl<T: Instance> SealedAdcChannel<T> for VrefInt { | ||
| 33 | fn channel(&self) -> u8 { | ||
| 34 | VREF_CHANNEL | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | /// Internal temperature channel. | ||
| 39 | pub struct Temperature; | ||
| 40 | impl<T: Instance> AdcChannel<T> for Temperature {} | ||
| 41 | impl<T: Instance> SealedAdcChannel<T> for Temperature { | ||
| 42 | fn channel(&self) -> u8 { | ||
| 43 | TEMP_CHANNEL | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | /// Internal battery voltage channel. | ||
| 48 | pub struct Vbat; | ||
| 49 | impl<T: Instance> AdcChannel<T> for Vbat {} | ||
| 50 | impl<T: Instance> SealedAdcChannel<T> for Vbat { | ||
| 51 | fn channel(&self) -> u8 { | ||
| 52 | VBAT_CHANNEL | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | /// Internal DAC channel. | ||
| 57 | pub struct Dac; | ||
| 58 | impl<T: Instance> AdcChannel<T> for Dac {} | ||
| 59 | impl<T: Instance> SealedAdcChannel<T> for Dac { | ||
| 60 | fn channel(&self) -> u8 { | ||
| 61 | DAC_CHANNEL | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | /// Internal Vcore channel. | ||
| 66 | pub struct Vcore; | ||
| 67 | impl<T: Instance> AdcChannel<T> for Vcore {} | ||
| 68 | impl<T: Instance> SealedAdcChannel<T> for Vcore { | ||
| 69 | fn channel(&self) -> u8 { | ||
| 70 | VCORE_CHANNEL | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | pub enum DacChannel { | ||
| 75 | OUT1, | ||
| 76 | OUT2 | ||
| 77 | } | ||
| 78 | |||
| 79 | /// Number of samples used for averaging. | ||
| 80 | pub enum Averaging { | ||
| 81 | Disabled, | ||
| 82 | Samples2, | ||
| 83 | Samples4, | ||
| 84 | Samples8, | ||
| 85 | Samples16, | ||
| 86 | Samples32, | ||
| 87 | Samples64, | ||
| 88 | Samples128, | ||
| 89 | Samples256, | ||
| 90 | } | ||
| 91 | |||
| 92 | pub const fn resolution_to_max_count(res: Resolution) -> u32 { | ||
| 93 | match res { | ||
| 94 | Resolution::BITS12 => (1 << 12) - 1, | ||
| 95 | Resolution::BITS10 => (1 << 10) - 1, | ||
| 96 | Resolution::BITS8 => (1 << 8) - 1, | ||
| 97 | Resolution::BITS6 => (1 << 6) - 1, | ||
| 98 | #[allow(unreachable_patterns)] | ||
| 99 | _ => core::unreachable!(), | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | // NOTE (unused): The prescaler enum closely copies the hardware capabilities, | ||
| 104 | // but high prescaling doesn't make a lot of sense in the current implementation and is ommited. | ||
| 105 | #[allow(unused)] | ||
| 106 | enum Prescaler { | ||
| 107 | NotDivided, | ||
| 108 | DividedBy2, | ||
| 109 | DividedBy4, | ||
| 110 | DividedBy6, | ||
| 111 | DividedBy8, | ||
| 112 | DividedBy10, | ||
| 113 | DividedBy12, | ||
| 114 | DividedBy16, | ||
| 115 | DividedBy32, | ||
| 116 | DividedBy64, | ||
| 117 | DividedBy128, | ||
| 118 | DividedBy256, | ||
| 119 | } | ||
| 120 | |||
| 121 | impl Prescaler { | ||
| 122 | fn from_ker_ck(frequency: Hertz) -> Self { | ||
| 123 | let raw_prescaler = frequency.0 / MAX_ADC_CLK_FREQ.0; | ||
| 124 | match raw_prescaler { | ||
| 125 | 0 => Self::NotDivided, | ||
| 126 | 1 => Self::DividedBy2, | ||
| 127 | 2..=3 => Self::DividedBy4, | ||
| 128 | 4..=5 => Self::DividedBy6, | ||
| 129 | 6..=7 => Self::DividedBy8, | ||
| 130 | 8..=9 => Self::DividedBy10, | ||
| 131 | 10..=11 => Self::DividedBy12, | ||
| 132 | _ => unimplemented!(), | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | fn divisor(&self) -> u32 { | ||
| 137 | match self { | ||
| 138 | Prescaler::NotDivided => 1, | ||
| 139 | Prescaler::DividedBy2 => 2, | ||
| 140 | Prescaler::DividedBy4 => 4, | ||
| 141 | Prescaler::DividedBy6 => 6, | ||
| 142 | Prescaler::DividedBy8 => 8, | ||
| 143 | Prescaler::DividedBy10 => 10, | ||
| 144 | Prescaler::DividedBy12 => 12, | ||
| 145 | Prescaler::DividedBy16 => 16, | ||
| 146 | Prescaler::DividedBy32 => 32, | ||
| 147 | Prescaler::DividedBy64 => 64, | ||
| 148 | Prescaler::DividedBy128 => 128, | ||
| 149 | Prescaler::DividedBy256 => 256, | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | fn presc(&self) -> Presc { | ||
| 154 | match self { | ||
| 155 | Prescaler::NotDivided => Presc::DIV1, | ||
| 156 | Prescaler::DividedBy2 => Presc::DIV2, | ||
| 157 | Prescaler::DividedBy4 => Presc::DIV4, | ||
| 158 | Prescaler::DividedBy6 => Presc::DIV6, | ||
| 159 | Prescaler::DividedBy8 => Presc::DIV8, | ||
| 160 | Prescaler::DividedBy10 => Presc::DIV10, | ||
| 161 | Prescaler::DividedBy12 => Presc::DIV12, | ||
| 162 | Prescaler::DividedBy16 => Presc::DIV16, | ||
| 163 | Prescaler::DividedBy32 => Presc::DIV32, | ||
| 164 | Prescaler::DividedBy64 => Presc::DIV64, | ||
| 165 | Prescaler::DividedBy128 => Presc::DIV128, | ||
| 166 | Prescaler::DividedBy256 => Presc::DIV256, | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | pub trait SealedInstance { | ||
| 172 | #[allow(unused)] | ||
| 173 | fn regs() -> crate::pac::adc::Adc4; | ||
| 174 | } | ||
| 175 | |||
| 176 | pub trait Instance: SealedInstance + crate::Peripheral<P = Self> + crate::rcc::RccPeripheral { | ||
| 177 | type Interrupt: crate::interrupt::typelevel::Interrupt; | ||
| 178 | } | ||
| 179 | |||
| 180 | pub struct Adc4<'d, T: Instance> { | ||
| 181 | adc: crate::PeripheralRef<'d, T>, | ||
| 182 | } | ||
| 183 | |||
| 184 | impl<'d, T: Instance> Adc4<'d, T> { | ||
| 185 | /// Create a new ADC driver. | ||
| 186 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { | ||
| 187 | embassy_hal_internal::into_ref!(adc); | ||
| 188 | rcc::enable_and_reset::<T>(); | ||
| 189 | let prescaler = Prescaler::from_ker_ck(T::frequency()); | ||
| 190 | |||
| 191 | T::regs().ccr().modify(|w| w.set_presc(prescaler.presc())); | ||
| 192 | |||
| 193 | let frequency = Hertz(T::frequency().0 / prescaler.divisor()); | ||
| 194 | info!("ADC4 frequency set to {} Hz", frequency.0); | ||
| 195 | |||
| 196 | if frequency > MAX_ADC_CLK_FREQ { | ||
| 197 | panic!("Maximal allowed frequency for ADC4 is {} MHz and it varies with different packages, refer to ST docs for more information.", MAX_ADC_CLK_FREQ.0 / 1_000_000 ); | ||
| 198 | } | ||
| 199 | |||
| 200 | let mut s = Self { | ||
| 201 | adc, | ||
| 202 | }; | ||
| 203 | |||
| 204 | s.power_up(); | ||
| 205 | |||
| 206 | s.calibrate(); | ||
| 207 | blocking_delay_us(1); | ||
| 208 | |||
| 209 | s.enable(); | ||
| 210 | s.configure(); | ||
| 211 | |||
| 212 | s | ||
| 213 | } | ||
| 214 | |||
| 215 | fn power_up(&mut self) { | ||
| 216 | T::regs().isr().modify(|reg| { | ||
| 217 | reg.set_ldordy(true); | ||
| 218 | }); | ||
| 219 | T::regs().cr().modify(|reg| { | ||
| 220 | reg.set_advregen(true); | ||
| 221 | }); | ||
| 222 | while !T::regs().isr().read().ldordy() { }; | ||
| 223 | |||
| 224 | T::regs().isr().modify(|reg| { | ||
| 225 | reg.set_ldordy(true); | ||
| 226 | }); | ||
| 227 | } | ||
| 228 | |||
| 229 | fn calibrate(&mut self) { | ||
| 230 | T::regs().cr().modify(|w| w.set_adcal(true)); | ||
| 231 | while T::regs().cr().read().adcal() {} | ||
| 232 | T::regs().isr().modify(|w| w.set_eocal(true)); | ||
| 233 | } | ||
| 234 | |||
| 235 | fn enable(&mut self) { | ||
| 236 | T::regs().isr().write(|w| w.set_adrdy(true)); | ||
| 237 | T::regs().cr().modify(|w| w.set_aden(true)); | ||
| 238 | while !T::regs().isr().read().adrdy() {} | ||
| 239 | T::regs().isr().write(|w| w.set_adrdy(true)); | ||
| 240 | } | ||
| 241 | |||
| 242 | fn configure(&mut self) { | ||
| 243 | // single conversion mode, software trigger | ||
| 244 | T::regs().cfgr1().modify(|w| { | ||
| 245 | w.set_cont(false); | ||
| 246 | w.set_exten(Adc4Exten::DISABLED); | ||
| 247 | }); | ||
| 248 | |||
| 249 | // only use one channel at the moment | ||
| 250 | T::regs().smpr().modify(|w| { | ||
| 251 | for i in 0..24 { | ||
| 252 | w.set_smpsel(i, false); | ||
| 253 | } | ||
| 254 | }); | ||
| 255 | } | ||
| 256 | |||
| 257 | /// Enable reading the voltage reference internal channel. | ||
| 258 | pub fn enable_vrefint(&self) -> VrefInt { | ||
| 259 | T::regs().ccr().modify(|reg| { | ||
| 260 | reg.set_vrefen(true); | ||
| 261 | }); | ||
| 262 | |||
| 263 | VrefInt {} | ||
| 264 | } | ||
| 265 | |||
| 266 | /// Enable reading the temperature internal channel. | ||
| 267 | pub fn enable_temperature(&self) -> Temperature { | ||
| 268 | T::regs().ccr().modify(|reg| { | ||
| 269 | reg.set_vsensesel(true); | ||
| 270 | }); | ||
| 271 | |||
| 272 | Temperature {} | ||
| 273 | } | ||
| 274 | |||
| 275 | /// Enable reading the vbat internal channel. | ||
| 276 | pub fn enable_vbat(&self) -> Vbat { | ||
| 277 | T::regs().ccr().modify(|reg| { | ||
| 278 | reg.set_vbaten(true); | ||
| 279 | }); | ||
| 280 | |||
| 281 | Vbat {} | ||
| 282 | } | ||
| 283 | |||
| 284 | /// Enable reading the vbat internal channel. | ||
| 285 | pub fn enable_vcore(&self) -> Vcore { | ||
| 286 | Vcore {} | ||
| 287 | } | ||
| 288 | |||
| 289 | /// Enable reading the vbat internal channel. | ||
| 290 | pub fn enable_dac_channel(&self, dac: DacChannel) -> Dac { | ||
| 291 | let mux; | ||
| 292 | match dac { | ||
| 293 | DacChannel::OUT1 => {mux = false}, | ||
| 294 | DacChannel::OUT2 => {mux = true} | ||
| 295 | } | ||
| 296 | T::regs().or().modify(|w| w.set_chn21sel(mux)); | ||
| 297 | Dac {} | ||
| 298 | } | ||
| 299 | |||
| 300 | /// Set the ADC sample time. | ||
| 301 | pub fn set_sample_time(&mut self, sample_time: SampleTime) { | ||
| 302 | T::regs().smpr().modify(|w| { | ||
| 303 | w.set_smp(0, sample_time); | ||
| 304 | }); | ||
| 305 | } | ||
| 306 | |||
| 307 | /// Get the ADC sample time. | ||
| 308 | pub fn sample_time(&self) -> SampleTime { | ||
| 309 | T::regs().smpr().read().smp(0) | ||
| 310 | } | ||
| 311 | |||
| 312 | /// Set the ADC resolution. | ||
| 313 | pub fn set_resolution(&mut self, resolution: Resolution) { | ||
| 314 | T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); | ||
| 315 | } | ||
| 316 | |||
| 317 | /// Set hardware averaging. | ||
| 318 | pub fn set_averaging(&mut self, averaging: Averaging) { | ||
| 319 | let (enable, samples, right_shift) = match averaging { | ||
| 320 | Averaging::Disabled => (false, Adc4OversamplingRatio::OVERSAMPLE2X, 0), | ||
| 321 | Averaging::Samples2 => (true, Adc4OversamplingRatio::OVERSAMPLE2X, 1), | ||
| 322 | Averaging::Samples4 => (true, Adc4OversamplingRatio::OVERSAMPLE4X, 2), | ||
| 323 | Averaging::Samples8 => (true, Adc4OversamplingRatio::OVERSAMPLE8X, 3), | ||
| 324 | Averaging::Samples16 => (true, Adc4OversamplingRatio::OVERSAMPLE16X, 4), | ||
| 325 | Averaging::Samples32 => (true, Adc4OversamplingRatio::OVERSAMPLE32X, 5), | ||
| 326 | Averaging::Samples64 => (true, Adc4OversamplingRatio::OVERSAMPLE64X, 6), | ||
| 327 | Averaging::Samples128 => (true, Adc4OversamplingRatio::OVERSAMPLE128X, 7), | ||
| 328 | Averaging::Samples256 => (true, Adc4OversamplingRatio::OVERSAMPLE256X, 8), | ||
| 329 | }; | ||
| 330 | |||
| 331 | T::regs().cfgr2().modify(|reg| { | ||
| 332 | reg.set_ovsr(samples); | ||
| 333 | reg.set_ovss(right_shift); | ||
| 334 | reg.set_ovse(enable) | ||
| 335 | }) | ||
| 336 | } | ||
| 337 | |||
| 338 | /// Perform a single conversion. | ||
| 339 | fn convert(&mut self) -> u16 { | ||
| 340 | T::regs().isr().modify(|reg| { | ||
| 341 | reg.set_eos(true); | ||
| 342 | reg.set_eoc(true); | ||
| 343 | }); | ||
| 344 | |||
| 345 | // Start conversion | ||
| 346 | T::regs().cr().modify(|reg| { | ||
| 347 | reg.set_adstart(true); | ||
| 348 | }); | ||
| 349 | |||
| 350 | while !T::regs().isr().read().eos() { | ||
| 351 | // spin | ||
| 352 | } | ||
| 353 | |||
| 354 | T::regs().dr().read().0 as u16 | ||
| 355 | } | ||
| 356 | |||
| 357 | /// Read an ADC channel. | ||
| 358 | pub fn blocking_read(&mut self, channel: &mut impl AdcChannel<T>) -> u16 { | ||
| 359 | self.read_channel(channel) | ||
| 360 | } | ||
| 361 | |||
| 362 | fn configure_channel(channel: &mut impl AdcChannel<T>) { | ||
| 363 | channel.setup(); | ||
| 364 | T::regs().chselrmod0().write_value(Adc4Chselrmod0(0_u32)); | ||
| 365 | T::regs().chselrmod0().modify(|w| { | ||
| 366 | w.set_chsel(channel.channel() as usize, true); | ||
| 367 | }); | ||
| 368 | } | ||
| 369 | |||
| 370 | fn read_channel(&mut self, channel: &mut impl AdcChannel<T>) -> u16 { | ||
| 371 | Self::configure_channel(channel); | ||
| 372 | let ret = self.convert(); | ||
| 373 | ret | ||
| 374 | } | ||
| 375 | |||
| 376 | fn cancel_conversions() { | ||
| 377 | if T::regs().cr().read().adstart() && !T::regs().cr().read().addis() { | ||
| 378 | T::regs().cr().modify(|reg| { | ||
| 379 | reg.set_adstp(true); | ||
| 380 | }); | ||
| 381 | while T::regs().cr().read().adstart() {} | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } \ No newline at end of file | ||
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 451f595e0..232373087 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -218,6 +218,10 @@ pub struct Config { | |||
| 218 | #[cfg(any(stm32l4, stm32l5, stm32u5))] | 218 | #[cfg(any(stm32l4, stm32l5, stm32u5))] |
| 219 | pub enable_independent_io_supply: bool, | 219 | pub enable_independent_io_supply: bool, |
| 220 | 220 | ||
| 221 | /// On the U5 series all analog peripherals are powere by a separate supply. | ||
| 222 | #[cfg(stm32u5)] | ||
| 223 | pub enable_independent_analog_supply: bool, | ||
| 224 | |||
| 221 | /// BDMA interrupt priority. | 225 | /// BDMA interrupt priority. |
| 222 | /// | 226 | /// |
| 223 | /// Defaults to P0 (highest). | 227 | /// Defaults to P0 (highest). |
| @@ -257,6 +261,8 @@ impl Default for Config { | |||
| 257 | enable_debug_during_sleep: true, | 261 | enable_debug_during_sleep: true, |
| 258 | #[cfg(any(stm32l4, stm32l5, stm32u5))] | 262 | #[cfg(any(stm32l4, stm32l5, stm32u5))] |
| 259 | enable_independent_io_supply: true, | 263 | enable_independent_io_supply: true, |
| 264 | #[cfg(stm32u5)] | ||
| 265 | enable_independent_analog_supply: true, | ||
| 260 | #[cfg(bdma)] | 266 | #[cfg(bdma)] |
| 261 | bdma_interrupt_priority: Priority::P0, | 267 | bdma_interrupt_priority: Priority::P0, |
| 262 | #[cfg(dma)] | 268 | #[cfg(dma)] |
| @@ -464,6 +470,20 @@ fn init_hw(config: Config) -> Peripherals { | |||
| 464 | crate::pac::PWR.svmcr().modify(|w| { | 470 | crate::pac::PWR.svmcr().modify(|w| { |
| 465 | w.set_io2sv(config.enable_independent_io_supply); | 471 | w.set_io2sv(config.enable_independent_io_supply); |
| 466 | }); | 472 | }); |
| 473 | if config.enable_independent_analog_supply { | ||
| 474 | crate::pac::PWR.svmcr().modify(|w| { | ||
| 475 | w.set_avm1en(true); | ||
| 476 | }); | ||
| 477 | while !crate::pac::PWR.svmsr().read().vdda1rdy() {} | ||
| 478 | crate::pac::PWR.svmcr().modify(|w| { | ||
| 479 | w.set_asv(true); | ||
| 480 | }); | ||
| 481 | } else { | ||
| 482 | crate::pac::PWR.svmcr().modify(|w| { | ||
| 483 | w.set_avm1en(false); | ||
| 484 | w.set_avm2en(false); | ||
| 485 | }); | ||
| 486 | } | ||
| 467 | } | 487 | } |
| 468 | 488 | ||
| 469 | // dead battery functionality is still present on these | 489 | // dead battery functionality is still present on these |
