diff options
| author | klownfish <[email protected]> | 2024-09-17 18:42:26 +0200 |
|---|---|---|
| committer | klownfish <[email protected]> | 2024-09-17 18:42:26 +0200 |
| commit | 0fe9fa631a8f905809424bd7b31a7b74568eb5b6 (patch) | |
| tree | 5f3b6139500c4a7a6f14a63b17c71c1d40795f3d | |
| parent | f0a86d1a344eac1528ce8653c573d1fe840b4083 (diff) | |
WIP: add u5 adc
| -rw-r--r-- | embassy-stm32/src/adc/mod.rs | 20 | ||||
| -rw-r--r-- | embassy-stm32/src/adc/u5.rs | 45 |
2 files changed, 26 insertions, 39 deletions
diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 9bf840f7c..3bd7c793d 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs | |||
| @@ -4,7 +4,7 @@ | |||
| 4 | #![allow(missing_docs)] // TODO | 4 | #![allow(missing_docs)] // TODO |
| 5 | #![cfg_attr(adc_f3_v2, allow(unused))] | 5 | #![cfg_attr(adc_f3_v2, allow(unused))] |
| 6 | 6 | ||
| 7 | #[cfg(not(any(adc_f3_v2, adc_u5)))] | 7 | #[cfg(not(any(adc_f3_v2)))] |
| 8 | #[cfg_attr(adc_f1, path = "f1.rs")] | 8 | #[cfg_attr(adc_f1, path = "f1.rs")] |
| 9 | #[cfg_attr(adc_f3, path = "f3.rs")] | 9 | #[cfg_attr(adc_f3, path = "f3.rs")] |
| 10 | #[cfg_attr(adc_f3_v1_1, path = "f3_v1_1.rs")] | 10 | #[cfg_attr(adc_f3_v1_1, path = "f3_v1_1.rs")] |
| @@ -20,16 +20,14 @@ mod _version; | |||
| 20 | use core::marker::PhantomData; | 20 | use core::marker::PhantomData; |
| 21 | 21 | ||
| 22 | #[allow(unused)] | 22 | #[allow(unused)] |
| 23 | #[cfg(not(any(adc_f3_v2, adc_u5)))] | 23 | #[cfg(not(any(adc_f3_v2)))] |
| 24 | pub use _version::*; | 24 | 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(not(any(adc_u5)))] | ||
| 29 | pub use crate::pac::adc::vals; | 28 | pub use crate::pac::adc::vals; |
| 30 | #[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))] | 29 | #[cfg(not(any(adc_f1, adc_f3_v2)))] |
| 31 | pub use crate::pac::adc::vals::Res as Resolution; | 30 | pub use crate::pac::adc::vals::Res as Resolution; |
| 32 | #[cfg(not(any(adc_u5)))] | ||
| 33 | pub use crate::pac::adc::vals::SampleTime; | 31 | pub use crate::pac::adc::vals::SampleTime; |
| 34 | use crate::peripherals; | 32 | use crate::peripherals; |
| 35 | 33 | ||
| @@ -39,7 +37,7 @@ dma_trait!(RxDma, Instance); | |||
| 39 | pub struct Adc<'d, T: Instance> { | 37 | pub struct Adc<'d, T: Instance> { |
| 40 | #[allow(unused)] | 38 | #[allow(unused)] |
| 41 | adc: crate::PeripheralRef<'d, T>, | 39 | adc: crate::PeripheralRef<'d, T>, |
| 42 | #[cfg(not(any(adc_f3_v2, adc_f3_v1_1, adc_u5)))] | 40 | #[cfg(not(any(adc_f3_v2, adc_f3_v1_1)))] |
| 43 | sample_time: SampleTime, | 41 | sample_time: SampleTime, |
| 44 | } | 42 | } |
| 45 | 43 | ||
| @@ -60,7 +58,7 @@ impl State { | |||
| 60 | trait SealedInstance { | 58 | trait SealedInstance { |
| 61 | #[allow(unused)] | 59 | #[allow(unused)] |
| 62 | fn regs() -> crate::pac::adc::Adc; | 60 | fn regs() -> crate::pac::adc::Adc; |
| 63 | #[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))] | 61 | #[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))] |
| 64 | #[allow(unused)] | 62 | #[allow(unused)] |
| 65 | fn common_regs() -> crate::pac::adccommon::AdcCommon; | 63 | fn common_regs() -> crate::pac::adccommon::AdcCommon; |
| 66 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] | 64 | #[cfg(any(adc_f1, adc_f3, adc_v1, adc_l0, adc_f3_v1_1))] |
| @@ -168,7 +166,7 @@ foreach_adc!( | |||
| 168 | crate::pac::$inst | 166 | crate::pac::$inst |
| 169 | } | 167 | } |
| 170 | 168 | ||
| 171 | #[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0, adc_u5)))] | 169 | #[cfg(not(any(adc_f1, adc_v1, adc_l0, adc_f3_v2, adc_f3_v1_1, adc_g0)))] |
| 172 | fn common_regs() -> crate::pac::adccommon::AdcCommon { | 170 | fn common_regs() -> crate::pac::adccommon::AdcCommon { |
| 173 | return crate::pac::$common_inst | 171 | return crate::pac::$common_inst |
| 174 | } | 172 | } |
| @@ -205,12 +203,12 @@ macro_rules! impl_adc_pin { | |||
| 205 | /// Get the maximum reading value for this resolution. | 203 | /// Get the maximum reading value for this resolution. |
| 206 | /// | 204 | /// |
| 207 | /// This is `2**n - 1`. | 205 | /// This is `2**n - 1`. |
| 208 | #[cfg(not(any(adc_f1, adc_f3_v2, adc_u5)))] | 206 | #[cfg(not(any(adc_f1, adc_f3_v2)))] |
| 209 | pub const fn resolution_to_max_count(res: Resolution) -> u32 { | 207 | pub const fn resolution_to_max_count(res: Resolution) -> u32 { |
| 210 | match res { | 208 | match res { |
| 211 | #[cfg(adc_v4)] | 209 | #[cfg(adc_v4)] |
| 212 | Resolution::BITS16 => (1 << 16) - 1, | 210 | Resolution::BITS16 => (1 << 16) - 1, |
| 213 | #[cfg(adc_v4)] | 211 | #[cfg(any(adc_v4, adc_u5))] |
| 214 | Resolution::BITS14 => (1 << 14) - 1, | 212 | Resolution::BITS14 => (1 << 14) - 1, |
| 215 | #[cfg(adc_v4)] | 213 | #[cfg(adc_v4)] |
| 216 | Resolution::BITS14V => (1 << 14) - 1, | 214 | Resolution::BITS14V => (1 << 14) - 1, |
| @@ -224,4 +222,4 @@ pub const fn resolution_to_max_count(res: Resolution) -> u32 { | |||
| 224 | #[allow(unreachable_patterns)] | 222 | #[allow(unreachable_patterns)] |
| 225 | _ => core::unreachable!(), | 223 | _ => core::unreachable!(), |
| 226 | } | 224 | } |
| 227 | } | 225 | } \ No newline at end of file |
diff --git a/embassy-stm32/src/adc/u5.rs b/embassy-stm32/src/adc/u5.rs index 9e6a94e5d..a86638a60 100644 --- a/embassy-stm32/src/adc/u5.rs +++ b/embassy-stm32/src/adc/u5.rs | |||
| @@ -1,19 +1,19 @@ | |||
| 1 | #[allow(unused)] | 1 | #[allow(unused)] |
| 2 | use pac::adc::vals::{Difsel, Exten, Pcsel}; | 2 | use pac::adc::vals::{Difsel, Exten, Pcsel}; |
| 3 | use pac::adccommon::vals::Presc; | 3 | use pac::adccommon::vals::Presc; |
| 4 | use crate::peripherals::ADC4; | 4 | use pac::PWR; |
| 5 | 5 | ||
| 6 | use super::{ | 6 | use super::{ |
| 7 | blocking_delay_us, Adc, AdcChannel, AnyAdcChannel, Instance, Resolution, RxDma, SampleTime, SealedAdcChannel | 7 | blocking_delay_us, Adc, AdcChannel, Instance, Resolution, SampleTime, SealedAdcChannel |
| 8 | }; | 8 | }; |
| 9 | use crate::time::Hertz; | 9 | use crate::time::Hertz; |
| 10 | use crate::{pac, rcc, Peripheral}; | 10 | use crate::{pac, rcc, Peripheral}; |
| 11 | 11 | ||
| 12 | // TODO: not correct | ||
| 13 | const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55); | 12 | const MAX_ADC_CLK_FREQ: Hertz = Hertz::mhz(55); |
| 14 | const VREF_CHANNEL: u8 = 19; | 13 | |
| 15 | const TEMP_CHANNEL: u8 = 18; | 14 | const VREF_CHANNEL: u8 = 1; |
| 16 | const VBAT_CHANNEL: u8 = 17; | 15 | const VBAT_CHANNEL: u8 = 18; |
| 16 | const TEMP_CHANNEL: u8 = 19; | ||
| 17 | 17 | ||
| 18 | /// Default VREF voltage used for sample conversion to millivolts. | 18 | /// Default VREF voltage used for sample conversion to millivolts. |
| 19 | pub const VREF_DEFAULT_MV: u32 = 3300; | 19 | pub const VREF_DEFAULT_MV: u32 = 3300; |
| @@ -140,9 +140,17 @@ pub enum Averaging { | |||
| 140 | impl<'d, T: Instance> Adc<'d, T> { | 140 | impl<'d, T: Instance> Adc<'d, T> { |
| 141 | /// Create a new ADC driver. | 141 | /// Create a new ADC driver. |
| 142 | pub fn new(adc: impl Peripheral<P = T> + 'd) -> Self { | 142 | 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 | |||
| 143 | embassy_hal_internal::into_ref!(adc); | 152 | embassy_hal_internal::into_ref!(adc); |
| 144 | rcc::enable_and_reset::<T>(); | 153 | rcc::enable_and_reset::<T>(); |
| 145 | |||
| 146 | let prescaler = Prescaler::from_ker_ck(T::frequency()); | 154 | let prescaler = Prescaler::from_ker_ck(T::frequency()); |
| 147 | 155 | ||
| 148 | T::common_regs().ccr().modify(|w| w.set_presc(prescaler.presc())); | 156 | T::common_regs().ccr().modify(|w| w.set_presc(prescaler.presc())); |
| @@ -158,26 +166,15 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 158 | adc, | 166 | adc, |
| 159 | sample_time: SampleTime::from_bits(0), | 167 | sample_time: SampleTime::from_bits(0), |
| 160 | }; | 168 | }; |
| 161 | crate::pac::RCC.ahb2enr1().modify(|w| { | ||
| 162 | w.set_adc12en(true); | ||
| 163 | }); | ||
| 164 | blocking_delay_us(100); | ||
| 165 | 169 | ||
| 166 | info!("chungus {}", line!()); | ||
| 167 | s.power_up(); | 170 | s.power_up(); |
| 168 | info!("chungus {}", line!()); | ||
| 169 | s.configure_differential_inputs(); | 171 | s.configure_differential_inputs(); |
| 170 | 172 | ||
| 171 | info!("chungus {}", line!()); | ||
| 172 | s.calibrate(); | 173 | s.calibrate(); |
| 173 | info!("chungus {}", line!()); | ||
| 174 | blocking_delay_us(1); | 174 | blocking_delay_us(1); |
| 175 | 175 | ||
| 176 | info!("chungus {}", line!()); | ||
| 177 | s.enable(); | 176 | s.enable(); |
| 178 | info!("chungus {}", line!()); | ||
| 179 | s.configure(); | 177 | s.configure(); |
| 180 | info!("chungus {}", line!()); | ||
| 181 | 178 | ||
| 182 | s | 179 | s |
| 183 | } | 180 | } |
| @@ -186,19 +183,11 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 186 | T::regs().isr().modify(|reg| { | 183 | T::regs().isr().modify(|reg| { |
| 187 | reg.set_ldordy(true); | 184 | reg.set_ldordy(true); |
| 188 | }); | 185 | }); |
| 189 | info!("yummmum {}", T::regs().cr().as_ptr() as u32); | ||
| 190 | T::regs().cr().modify(|reg| { | 186 | T::regs().cr().modify(|reg| { |
| 191 | info!("bajssis {}", reg.0); | ||
| 192 | reg.set_deeppwd(false); | 187 | reg.set_deeppwd(false); |
| 193 | info!("bajssis {}", reg.0); | ||
| 194 | reg.set_advregen(true); | 188 | reg.set_advregen(true); |
| 195 | info!("bajssis {}", reg.0); | ||
| 196 | }); | 189 | }); |
| 197 | info!("kissis {}", T::regs().as_ptr() as u32); | 190 | while !T::regs().isr().read().ldordy() { }; |
| 198 | info!("basdsadasadjsisssss{}", T::regs().isr().as_ptr() as u32); | ||
| 199 | while !T::regs().isr().read().ldordy() { | ||
| 200 | // info!("bajsisssss{}", T::regs().isr().read().0); | ||
| 201 | }; | ||
| 202 | 191 | ||
| 203 | T::regs().isr().modify(|reg| { | 192 | T::regs().isr().modify(|reg| { |
| 204 | reg.set_ldordy(true); | 193 | reg.set_ldordy(true); |
| @@ -372,4 +361,4 @@ impl<'d, T: Instance> Adc<'d, T> { | |||
| 372 | while T::regs().cr().read().adstart() {} | 361 | while T::regs().cr().read().adstart() {} |
| 373 | } | 362 | } |
| 374 | } | 363 | } |
| 375 | } | 364 | } \ No newline at end of file |
