diff options
| author | Gerzain Mata <[email protected]> | 2025-08-08 03:10:35 -0700 |
|---|---|---|
| committer | Gerzain Mata <[email protected]> | 2025-08-08 03:10:35 -0700 |
| commit | 556ae0106bba66e49f62f45aa3cc458e31094dc7 (patch) | |
| tree | 093284c4b15c275fa39c913db5a7c75825d80bdb | |
| parent | f2be66a5f94a655696407e2e7bc81c322aab3ea1 (diff) | |
Working draft of VREFBUF driver
| -rw-r--r-- | embassy-stm32/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/vrefbuf/mod.rs | 69 |
3 files changed, 73 insertions, 2 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 145a89b77..590216c21 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-f0ef9106642ed869e7cfbf82d891d364c98ebb43" } |
| 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-f0ef9106642ed869e7cfbf82d891d364c98ebb43", 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/vrefbuf/mod.rs b/embassy-stm32/src/vrefbuf/mod.rs new file mode 100644 index 000000000..2546ccfed --- /dev/null +++ b/embassy-stm32/src/vrefbuf/mod.rs | |||
| @@ -0,0 +1,69 @@ | |||
| 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::pac::RCC; | ||
| 9 | use crate::Peri; | ||
| 10 | |||
| 11 | /// Voltage Reference (VREFBUF) driver. | ||
| 12 | pub struct VoltageReferenceBuffer<'d, T: Instance> { | ||
| 13 | vrefbuf: PhantomData<&'d mut T>, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl<'d, T: Instance> VoltageReferenceBuffer<'d, T> { | ||
| 17 | /// Creates an VREFBUF (Voltage Reference) instance with a voltage scale and impedance mode. | ||
| 18 | /// | ||
| 19 | /// [Self] has to be started with [Self::new()]. | ||
| 20 | pub fn new(_instance: Peri<'d, T>, voltage_scale: Vrs, impedance_mode: Hiz) -> Self { | ||
| 21 | #[cfg(rcc_wba)] | ||
| 22 | { | ||
| 23 | RCC.apb7enr().modify(|w| w.set_vrefen(true)); | ||
| 24 | } | ||
| 25 | #[cfg(any(rcc_u5, rcc_h50, rcc_h5))] | ||
| 26 | { | ||
| 27 | RCC.apb3enr().modify(|w| w.set_vrefen(true)); | ||
| 28 | } | ||
| 29 | #[cfg(any(rcc_h7rs, rcc_h7rm0433, rcc_h7ab, rcc_h7))] | ||
| 30 | { | ||
| 31 | RCC.apb4enr().modify(|w| w.set_vrefen(true)); | ||
| 32 | } | ||
| 33 | let vrefbuf = T::regs(); | ||
| 34 | vrefbuf.csr().modify(|w| { | ||
| 35 | w.set_hiz(impedance_mode); | ||
| 36 | w.set_envr(true); | ||
| 37 | w.set_vrs(voltage_scale); | ||
| 38 | }); | ||
| 39 | while vrefbuf.csr().read().vrr() != false { | ||
| 40 | // wait... | ||
| 41 | } | ||
| 42 | trace!( | ||
| 43 | "Vrefbuf configured with voltage scale {} and impedance mode {}", | ||
| 44 | voltage_scale, | ||
| 45 | impedance_mode | ||
| 46 | ); | ||
| 47 | VoltageReferenceBuffer { vrefbuf: PhantomData } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | trait SealedInstance { | ||
| 52 | fn regs() -> crate::pac::vrefbuf::Vrefbuf; | ||
| 53 | } | ||
| 54 | |||
| 55 | /// VREFBUF instance trait. | ||
| 56 | #[allow(private_bounds)] | ||
| 57 | pub trait Instance: SealedInstance + PeripheralType {} | ||
| 58 | |||
| 59 | foreach_peripheral!( | ||
| 60 | (vrefbuf, $inst:ident) => { | ||
| 61 | impl SealedInstance for crate::peripherals::$inst { | ||
| 62 | fn regs() -> crate::pac::vrefbuf::Vrefbuf { | ||
| 63 | crate::pac::$inst | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | impl Instance for crate::peripherals::$inst {} | ||
| 68 | }; | ||
| 69 | ); | ||
