aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32
diff options
context:
space:
mode:
authorleftger <[email protected]>2025-07-27 09:38:38 -0700
committerGitHub <[email protected]>2025-07-27 09:38:38 -0700
commitb9e643d5c2d7192143e91db83b9e8377f0fbcacc (patch)
tree86a695a05ccb70be67613ea742b14094a7aca362 /embassy-stm32
parent1b3674b30ac2b7deb8e19b132d5ba15351cb8ebd (diff)
parent77a8bc27e9c34e363f321132ebb9e8d8ff684a9f (diff)
Merge branch 'main' into feat/stm32wba-rcc-pll-support
Diffstat (limited to 'embassy-stm32')
-rw-r--r--embassy-stm32/build.rs2
-rw-r--r--embassy-stm32/src/adc/v3.rs36
-rw-r--r--embassy-stm32/src/rcc/h.rs43
3 files changed, 79 insertions, 2 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index a4ed86bdf..deefb13c1 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -1599,7 +1599,7 @@ fn main() {
1599 for e in rcc_registers.ir.enums { 1599 for e in rcc_registers.ir.enums {
1600 fn is_rcc_name(e: &str) -> bool { 1600 fn is_rcc_name(e: &str) -> bool {
1601 match e { 1601 match e {
1602 "Pllp" | "Pllq" | "Pllr" | "Pllm" | "Plln" | "Prediv1" | "Prediv2" | "Hpre5" => true, 1602 "Pllp" | "Pllq" | "Pllr" | "Plldivst" | "Pllm" | "Plln" | "Prediv1" | "Prediv2" | "Hpre5" => true,
1603 "Timpre" | "Pllrclkpre" => false, 1603 "Timpre" | "Pllrclkpre" => false,
1604 e if e.ends_with("pre") || e.ends_with("pres") || e.ends_with("div") || e.ends_with("mul") => true, 1604 e if e.ends_with("pre") || e.ends_with("pres") || e.ends_with("div") || e.ends_with("mul") => true,
1605 _ => false, 1605 _ => false,
diff --git a/embassy-stm32/src/adc/v3.rs b/embassy-stm32/src/adc/v3.rs
index fd74d5318..a2e42fe52 100644
--- a/embassy-stm32/src/adc/v3.rs
+++ b/embassy-stm32/src/adc/v3.rs
@@ -95,6 +95,18 @@ cfg_if! {
95 } 95 }
96} 96}
97 97
98/// Number of samples used for averaging.
99pub enum Averaging {
100 Disabled,
101 Samples2,
102 Samples4,
103 Samples8,
104 Samples16,
105 Samples32,
106 Samples64,
107 Samples128,
108 Samples256,
109}
98impl<'d, T: Instance> Adc<'d, T> { 110impl<'d, T: Instance> Adc<'d, T> {
99 pub fn new(adc: Peri<'d, T>) -> Self { 111 pub fn new(adc: Peri<'d, T>) -> Self {
100 rcc::enable_and_reset::<T>(); 112 rcc::enable_and_reset::<T>();
@@ -225,6 +237,30 @@ impl<'d, T: Instance> Adc<'d, T> {
225 T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into())); 237 T::regs().cfgr1().modify(|reg| reg.set_res(resolution.into()));
226 } 238 }
227 239
240 pub fn set_averaging(&mut self, averaging: Averaging) {
241 let (enable, samples, right_shift) = match averaging {
242 Averaging::Disabled => (false, 0, 0),
243 Averaging::Samples2 => (true, 0, 1),
244 Averaging::Samples4 => (true, 1, 2),
245 Averaging::Samples8 => (true, 2, 3),
246 Averaging::Samples16 => (true, 3, 4),
247 Averaging::Samples32 => (true, 4, 5),
248 Averaging::Samples64 => (true, 5, 6),
249 Averaging::Samples128 => (true, 6, 7),
250 Averaging::Samples256 => (true, 7, 8),
251 };
252 T::regs().cfgr2().modify(|reg| {
253 #[cfg(not(any(adc_g0, adc_u0)))]
254 reg.set_rovse(enable);
255 #[cfg(any(adc_g0, adc_u0))]
256 reg.set_ovse(enable);
257 #[cfg(any(adc_h5, adc_h7rs))]
258 reg.set_ovsr(samples.into());
259 #[cfg(not(any(adc_h5, adc_h7rs)))]
260 reg.set_ovsr(samples.into());
261 reg.set_ovss(right_shift.into());
262 })
263 }
228 /* 264 /*
229 /// Convert a raw sample from the `Temperature` to deg C 265 /// Convert a raw sample from the `Temperature` to deg C
230 pub fn to_degrees_centigrade(sample: u16) -> f32 { 266 pub fn to_degrees_centigrade(sample: u16) -> f32 {
diff --git a/embassy-stm32/src/rcc/h.rs b/embassy-stm32/src/rcc/h.rs
index 383f48874..837210b6a 100644
--- a/embassy-stm32/src/rcc/h.rs
+++ b/embassy-stm32/src/rcc/h.rs
@@ -1,5 +1,8 @@
1use core::ops::RangeInclusive; 1use core::ops::RangeInclusive;
2 2
3#[cfg(stm32h7rs)]
4use stm32_metapac::rcc::vals::Plldivst;
5
3use crate::pac; 6use crate::pac;
4pub use crate::pac::rcc::vals::{ 7pub use crate::pac::rcc::vals::{
5 Hsidiv as HSIPrescaler, Plldiv as PllDiv, Pllm as PllPreDiv, Plln as PllMul, Pllsrc as PllSource, Sw as Sysclk, 8 Hsidiv as HSIPrescaler, Plldiv as PllDiv, Pllm as PllPreDiv, Plln as PllMul, Pllsrc as PllSource, Sw as Sysclk,
@@ -78,6 +81,12 @@ pub struct Pll {
78 pub divq: Option<PllDiv>, 81 pub divq: Option<PllDiv>,
79 /// PLL R division factor. If None, PLL R output is disabled. 82 /// PLL R division factor. If None, PLL R output is disabled.
80 pub divr: Option<PllDiv>, 83 pub divr: Option<PllDiv>,
84 #[cfg(stm32h7rs)]
85 /// PLL S division factor. If None, PLL S output is disabled.
86 pub divs: Option<Plldivst>,
87 #[cfg(stm32h7rs)]
88 /// PLL T division factor. If None, PLL T output is disabled.
89 pub divt: Option<Plldivst>,
81} 90}
82 91
83fn apb_div_tim(apb: &APBPrescaler, clk: Hertz, tim: TimerPrescaler) -> Hertz { 92fn apb_div_tim(apb: &APBPrescaler, clk: Hertz, tim: TimerPrescaler) -> Hertz {
@@ -749,6 +758,12 @@ struct PllOutput {
749 q: Option<Hertz>, 758 q: Option<Hertz>,
750 #[allow(dead_code)] 759 #[allow(dead_code)]
751 r: Option<Hertz>, 760 r: Option<Hertz>,
761 #[cfg(stm32h7rs)]
762 #[allow(dead_code)]
763 s: Option<Hertz>,
764 #[cfg(stm32h7rs)]
765 #[allow(dead_code)]
766 t: Option<Hertz>,
752} 767}
753 768
754fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput { 769fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput {
@@ -767,6 +782,10 @@ fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput {
767 p: None, 782 p: None,
768 q: None, 783 q: None,
769 r: None, 784 r: None,
785 #[cfg(stm32h7rs)]
786 s: None,
787 #[cfg(stm32h7rs)]
788 t: None,
770 }; 789 };
771 }; 790 };
772 791
@@ -814,6 +833,10 @@ fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput {
814 }); 833 });
815 let q = config.divq.map(|div| vco_clk / div); 834 let q = config.divq.map(|div| vco_clk / div);
816 let r = config.divr.map(|div| vco_clk / div); 835 let r = config.divr.map(|div| vco_clk / div);
836 #[cfg(stm32h7rs)]
837 let s = config.divs.map(|div| vco_clk / div);
838 #[cfg(stm32h7rs)]
839 let t = config.divt.map(|div| vco_clk / div);
817 840
818 #[cfg(stm32h5)] 841 #[cfg(stm32h5)]
819 RCC.pllcfgr(num).write(|w| { 842 RCC.pllcfgr(num).write(|w| {
@@ -840,6 +863,10 @@ fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput {
840 w.set_divpen(num, p.is_some()); 863 w.set_divpen(num, p.is_some());
841 w.set_divqen(num, q.is_some()); 864 w.set_divqen(num, q.is_some());
842 w.set_divren(num, r.is_some()); 865 w.set_divren(num, r.is_some());
866 #[cfg(stm32h7rs)]
867 w.set_divsen(num, s.is_some());
868 #[cfg(stm32h7rs)]
869 w.set_divten(num, t.is_some());
843 }); 870 });
844 } 871 }
845 872
@@ -850,10 +877,24 @@ fn init_pll(num: usize, config: Option<Pll>, input: &PllInput) -> PllOutput {
850 w.set_pllr(config.divr.unwrap_or(PllDiv::DIV2)); 877 w.set_pllr(config.divr.unwrap_or(PllDiv::DIV2));
851 }); 878 });
852 879
880 #[cfg(stm32h7rs)]
881 RCC.plldivr2(num).write(|w| {
882 w.set_plls(config.divs.unwrap_or(Plldivst::DIV2));
883 w.set_pllt(config.divt.unwrap_or(Plldivst::DIV2));
884 });
885
853 RCC.cr().modify(|w| w.set_pllon(num, true)); 886 RCC.cr().modify(|w| w.set_pllon(num, true));
854 while !RCC.cr().read().pllrdy(num) {} 887 while !RCC.cr().read().pllrdy(num) {}
855 888
856 PllOutput { p, q, r } 889 PllOutput {
890 p,
891 q,
892 r,
893 #[cfg(stm32h7rs)]
894 s,
895 #[cfg(stm32h7rs)]
896 t,
897 }
857} 898}
858 899
859fn flash_setup(clk: Hertz, vos: VoltageScale) { 900fn flash_setup(clk: Hertz, vos: VoltageScale) {