diff options
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-stm32/src/vrefbuf/mod.rs | 69 |
2 files changed, 71 insertions, 0 deletions
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 | ); | ||
