diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-08-11 13:03:01 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-11 13:03:01 +0200 |
| commit | cae93c5a27d596c9371b81c896598ce1a7fdaa83 (patch) | |
| tree | 2a672a9c133867883db10da5485fe5bfa2a99c65 /embassy-stm32 | |
| parent | 76e369b8aa5641b6f3f4440824f559def6cf6192 (diff) | |
| parent | 4a3cf3231666bf137900066c961e906ce7e0bb78 (diff) | |
Merge pull request #4524 from leftger/feat/add-vrefbuf-driver
Feat/add vrefbuf driver
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/rcc/wba.rs | 14 | ||||
| -rw-r--r-- | embassy-stm32/src/vrefbuf/mod.rs | 71 |
4 files changed, 88 insertions, 3 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 145a89b77..39e6f13f0 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -81,7 +81,7 @@ futures-util = { version = "0.3.30", default-features = false } | |||
| 81 | sdio-host = "0.9.0" | 81 | sdio-host = "0.9.0" |
| 82 | critical-section = "1.1" | 82 | critical-section = "1.1" |
| 83 | #stm32-metapac = { version = "16" } | 83 | #stm32-metapac = { version = "16" } |
| 84 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-cad609e02a866422ffdbb8e07be26311cfdd07d9" } | 84 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9941f338734a2e6c1652267f64b13f7b35d8c9db" } |
| 85 | 85 | ||
| 86 | vcell = "0.1.3" | 86 | vcell = "0.1.3" |
| 87 | nb = "1.0.0" | 87 | nb = "1.0.0" |
| @@ -110,7 +110,7 @@ proc-macro2 = "1.0.36" | |||
| 110 | quote = "1.0.15" | 110 | quote = "1.0.15" |
| 111 | 111 | ||
| 112 | #stm32-metapac = { version = "16", default-features = false, features = ["metadata"]} | 112 | #stm32-metapac = { version = "16", default-features = false, features = ["metadata"]} |
| 113 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-cad609e02a866422ffdbb8e07be26311cfdd07d9", default-features = false, features = ["metadata"] } | 113 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9941f338734a2e6c1652267f64b13f7b35d8c9db", default-features = false, features = ["metadata"] } |
| 114 | 114 | ||
| 115 | [features] | 115 | [features] |
| 116 | default = ["rt"] | 116 | default = ["rt"] |
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index a676677e1..e4a8ff0ab 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -125,6 +125,8 @@ pub mod uid; | |||
| 125 | pub mod usart; | 125 | pub mod usart; |
| 126 | #[cfg(any(usb, otg))] | 126 | #[cfg(any(usb, otg))] |
| 127 | pub mod usb; | 127 | pub mod usb; |
| 128 | #[cfg(vrefbuf)] | ||
| 129 | pub mod vrefbuf; | ||
| 128 | #[cfg(iwdg)] | 130 | #[cfg(iwdg)] |
| 129 | pub mod wdg; | 131 | pub mod wdg; |
| 130 | #[cfg(xspi)] | 132 | #[cfg(xspi)] |
diff --git a/embassy-stm32/src/rcc/wba.rs b/embassy-stm32/src/rcc/wba.rs index 56ba7b58b..481437939 100644 --- a/embassy-stm32/src/rcc/wba.rs +++ b/embassy-stm32/src/rcc/wba.rs | |||
| @@ -5,7 +5,7 @@ pub use crate::pac::rcc::vals::Otghssel; | |||
| 5 | use crate::pac::rcc::vals::Pllrge; | 5 | use crate::pac::rcc::vals::Pllrge; |
| 6 | pub use crate::pac::rcc::vals::{ | 6 | pub use crate::pac::rcc::vals::{ |
| 7 | Hdiv5, Hpre as AHBPrescaler, Hpre5 as AHB5Prescaler, Hsepre as HsePrescaler, Plldiv as PllDiv, Pllm as PllPreDiv, | 7 | Hdiv5, Hpre as AHBPrescaler, Hpre5 as AHB5Prescaler, Hsepre as HsePrescaler, Plldiv as PllDiv, Pllm as PllPreDiv, |
| 8 | Plln as PllMul, Pllsrc as PllSource, Ppre as APBPrescaler, Sw as Sysclk, | 8 | Plln as PllMul, Pllsrc as PllSource, Ppre as APBPrescaler, Sai1sel, Sw as Sysclk, |
| 9 | }; | 9 | }; |
| 10 | #[cfg(all(peri_usb_otg_hs))] | 10 | #[cfg(all(peri_usb_otg_hs))] |
| 11 | pub use crate::pac::{syscfg::vals::Usbrefcksel, SYSCFG}; | 11 | pub use crate::pac::{syscfg::vals::Usbrefcksel, SYSCFG}; |
| @@ -254,6 +254,16 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 254 | w.set_clksel(usb_refck_sel); | 254 | w.set_clksel(usb_refck_sel); |
| 255 | }); | 255 | }); |
| 256 | 256 | ||
| 257 | #[cfg(sai_v4_2pdm)] | ||
| 258 | let audioclk = match config.mux.sai1sel { | ||
| 259 | Sai1sel::HSI => Some(HSI_FREQ), | ||
| 260 | Sai1sel::PLL1_Q => Some(pll1.q.expect("PLL1.Q not configured")), | ||
| 261 | Sai1sel::PLL1_P => Some(pll1.p.expect("PLL1.P not configured")), | ||
| 262 | Sai1sel::SYS => panic!("SYS not supported yet"), | ||
| 263 | Sai1sel::AUDIOCLK => panic!("AUDIOCLK not supported yet"), | ||
| 264 | _ => None, | ||
| 265 | }; | ||
| 266 | |||
| 257 | let lsi = config.ls.lsi.then_some(LSI_FREQ); | 267 | let lsi = config.ls.lsi.then_some(LSI_FREQ); |
| 258 | 268 | ||
| 259 | config.mux.init(); | 269 | config.mux.init(); |
| @@ -279,6 +289,8 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 279 | 289 | ||
| 280 | // TODO | 290 | // TODO |
| 281 | lse: None, | 291 | lse: None, |
| 292 | #[cfg(sai_v4_2pdm)] | ||
| 293 | audioclk: audioclk, | ||
| 282 | ); | 294 | ); |
| 283 | } | 295 | } |
| 284 | 296 | ||
diff --git a/embassy-stm32/src/vrefbuf/mod.rs b/embassy-stm32/src/vrefbuf/mod.rs new file mode 100644 index 000000000..f614fcf99 --- /dev/null +++ b/embassy-stm32/src/vrefbuf/mod.rs | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | //! Voltage Reference Buffer (VREFBUF) | ||
| 2 | // use core::ptr::{read_volatile, write_volatile}; | ||
| 3 | use core::marker::PhantomData; | ||
| 4 | |||
| 5 | use embassy_hal_internal::PeripheralType; | ||
| 6 | use stm32_metapac::vrefbuf::vals::*; | ||
| 7 | |||
| 8 | use crate::Peri; | ||
| 9 | |||
| 10 | /// Voltage Reference (VREFBUF) driver. | ||
| 11 | pub struct VoltageReferenceBuffer<'d, T: Instance> { | ||
| 12 | vrefbuf: PhantomData<&'d mut T>, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl<'d, T: Instance> VoltageReferenceBuffer<'d, T> { | ||
| 16 | /// Creates an VREFBUF (Voltage Reference) instance with a voltage scale and impedance mode. | ||
| 17 | /// | ||
| 18 | /// [Self] has to be started with [Self::new()]. | ||
| 19 | pub fn new(_instance: Peri<'d, T>, voltage_scale: Vrs, impedance_mode: Hiz) -> Self { | ||
| 20 | #[cfg(rcc_wba)] | ||
| 21 | { | ||
| 22 | use crate::pac::RCC; | ||
| 23 | RCC.apb7enr().modify(|w| w.set_vrefen(true)); | ||
| 24 | } | ||
| 25 | #[cfg(any(rcc_u5, rcc_h50, rcc_h5))] | ||
| 26 | { | ||
| 27 | use crate::pac::RCC; | ||
| 28 | RCC.apb3enr().modify(|w| w.set_vrefen(true)); | ||
| 29 | } | ||
| 30 | #[cfg(any(rcc_h7rs, rcc_h7rm0433, rcc_h7ab, rcc_h7))] | ||
| 31 | { | ||
| 32 | use crate::pac::RCC; | ||
| 33 | RCC.apb4enr().modify(|w| w.set_vrefen(true)); | ||
| 34 | } | ||
| 35 | let vrefbuf = T::regs(); | ||
| 36 | vrefbuf.csr().modify(|w| { | ||
| 37 | w.set_hiz(impedance_mode); | ||
| 38 | w.set_envr(true); | ||
| 39 | w.set_vrs(voltage_scale); | ||
| 40 | }); | ||
| 41 | while vrefbuf.csr().read().vrr() != false { | ||
| 42 | // wait... | ||
| 43 | } | ||
| 44 | trace!( | ||
| 45 | "Vrefbuf configured with voltage scale {} and impedance mode {}", | ||
| 46 | voltage_scale, | ||
| 47 | impedance_mode | ||
| 48 | ); | ||
| 49 | VoltageReferenceBuffer { vrefbuf: PhantomData } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | trait SealedInstance { | ||
| 54 | fn regs() -> crate::pac::vrefbuf::Vrefbuf; | ||
| 55 | } | ||
| 56 | |||
| 57 | /// VREFBUF instance trait. | ||
| 58 | #[allow(private_bounds)] | ||
| 59 | pub trait Instance: SealedInstance + PeripheralType {} | ||
| 60 | |||
| 61 | foreach_peripheral!( | ||
| 62 | (vrefbuf, $inst:ident) => { | ||
| 63 | impl SealedInstance for crate::peripherals::$inst { | ||
| 64 | fn regs() -> crate::pac::vrefbuf::Vrefbuf { | ||
| 65 | crate::pac::$inst | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | impl Instance for crate::peripherals::$inst {} | ||
| 70 | }; | ||
| 71 | ); | ||
