From 556ae0106bba66e49f62f45aa3cc458e31094dc7 Mon Sep 17 00:00:00 2001 From: Gerzain Mata Date: Fri, 8 Aug 2025 03:10:35 -0700 Subject: Working draft of VREFBUF driver --- embassy-stm32/Cargo.toml | 4 +-- embassy-stm32/src/lib.rs | 2 ++ embassy-stm32/src/vrefbuf/mod.rs | 69 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 embassy-stm32/src/vrefbuf/mod.rs 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 } sdio-host = "0.9.0" critical-section = "1.1" #stm32-metapac = { version = "16" } -stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-cad609e02a866422ffdbb8e07be26311cfdd07d9" } +stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-f0ef9106642ed869e7cfbf82d891d364c98ebb43" } vcell = "0.1.3" nb = "1.0.0" @@ -110,7 +110,7 @@ proc-macro2 = "1.0.36" quote = "1.0.15" #stm32-metapac = { version = "16", default-features = false, features = ["metadata"]} -stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-cad609e02a866422ffdbb8e07be26311cfdd07d9", default-features = false, features = ["metadata"] } +stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-f0ef9106642ed869e7cfbf82d891d364c98ebb43", default-features = false, features = ["metadata"] } [features] 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; pub mod usart; #[cfg(any(usb, otg))] pub mod usb; +#[cfg(vrefbuf)] +pub mod vrefbuf; #[cfg(iwdg)] pub mod wdg; #[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 @@ +//! Voltage Reference Buffer (VREFBUF) +// use core::ptr::{read_volatile, write_volatile}; +use core::marker::PhantomData; + +use embassy_hal_internal::PeripheralType; +use stm32_metapac::vrefbuf::vals::*; + +use crate::pac::RCC; +use crate::Peri; + +/// Voltage Reference (VREFBUF) driver. +pub struct VoltageReferenceBuffer<'d, T: Instance> { + vrefbuf: PhantomData<&'d mut T>, +} + +impl<'d, T: Instance> VoltageReferenceBuffer<'d, T> { + /// Creates an VREFBUF (Voltage Reference) instance with a voltage scale and impedance mode. + /// + /// [Self] has to be started with [Self::new()]. + pub fn new(_instance: Peri<'d, T>, voltage_scale: Vrs, impedance_mode: Hiz) -> Self { + #[cfg(rcc_wba)] + { + RCC.apb7enr().modify(|w| w.set_vrefen(true)); + } + #[cfg(any(rcc_u5, rcc_h50, rcc_h5))] + { + RCC.apb3enr().modify(|w| w.set_vrefen(true)); + } + #[cfg(any(rcc_h7rs, rcc_h7rm0433, rcc_h7ab, rcc_h7))] + { + RCC.apb4enr().modify(|w| w.set_vrefen(true)); + } + let vrefbuf = T::regs(); + vrefbuf.csr().modify(|w| { + w.set_hiz(impedance_mode); + w.set_envr(true); + w.set_vrs(voltage_scale); + }); + while vrefbuf.csr().read().vrr() != false { + // wait... + } + trace!( + "Vrefbuf configured with voltage scale {} and impedance mode {}", + voltage_scale, + impedance_mode + ); + VoltageReferenceBuffer { vrefbuf: PhantomData } + } +} + +trait SealedInstance { + fn regs() -> crate::pac::vrefbuf::Vrefbuf; +} + +/// VREFBUF instance trait. +#[allow(private_bounds)] +pub trait Instance: SealedInstance + PeripheralType {} + +foreach_peripheral!( + (vrefbuf, $inst:ident) => { + impl SealedInstance for crate::peripherals::$inst { + fn regs() -> crate::pac::vrefbuf::Vrefbuf { + crate::pac::$inst + } + } + + impl Instance for crate::peripherals::$inst {} + }; +); -- cgit