aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/Cargo.toml4
-rw-r--r--embassy-stm32/src/rcc/l4l5.rs101
-rw-r--r--embassy-stm32/src/rcc/mod.rs3
-rw-r--r--embassy-stm32/src/rcc/wb.rs258
-rw-r--r--examples/stm32wl/src/bin/lora_lorawan.rs1
-rw-r--r--examples/stm32wl/src/bin/lora_p2p_receive.rs19
-rw-r--r--examples/stm32wl/src/bin/lora_p2p_send.rs19
-rw-r--r--examples/stm32wl/src/bin/random.rs1
-rw-r--r--examples/stm32wl/src/bin/rtc.rs26
-rw-r--r--tests/stm32/src/common.rs6
10 files changed, 146 insertions, 292 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index 568a7eeb9..992883402 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -58,7 +58,7 @@ rand_core = "0.6.3"
58sdio-host = "0.5.0" 58sdio-host = "0.5.0"
59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
60critical-section = "1.1" 60critical-section = "1.1"
61stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-4ddcb77c9d213d11eebb048f40e112bc54163cdc" } 61stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-bcc9b6bf9fa195e91625849efc4ba473d9ace4e9" }
62vcell = "0.1.3" 62vcell = "0.1.3"
63bxcan = "0.7.0" 63bxcan = "0.7.0"
64nb = "1.0.0" 64nb = "1.0.0"
@@ -76,7 +76,7 @@ critical-section = { version = "1.1", features = ["std"] }
76[build-dependencies] 76[build-dependencies]
77proc-macro2 = "1.0.36" 77proc-macro2 = "1.0.36"
78quote = "1.0.15" 78quote = "1.0.15"
79stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-4ddcb77c9d213d11eebb048f40e112bc54163cdc", default-features = false, features = ["metadata"]} 79stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-bcc9b6bf9fa195e91625849efc4ba473d9ace4e9", default-features = false, features = ["metadata"]}
80 80
81 81
82[features] 82[features]
diff --git a/embassy-stm32/src/rcc/l4l5.rs b/embassy-stm32/src/rcc/l4l5.rs
index b56962270..50128d389 100644
--- a/embassy-stm32/src/rcc/l4l5.rs
+++ b/embassy-stm32/src/rcc/l4l5.rs
@@ -1,7 +1,8 @@
1use crate::pac::rcc::regs::Cfgr; 1use crate::pac::rcc::regs::Cfgr;
2#[cfg(not(stm32wl))] 2#[cfg(any(stm32l4, stm32l5, stm32wb))]
3pub use crate::pac::rcc::vals::Clk48sel as Clk48Src; 3pub use crate::pac::rcc::vals::Clk48sel as Clk48Src;
4use crate::pac::rcc::vals::Msirgsel; 4#[cfg(any(stm32wb, stm32wl))]
5pub use crate::pac::rcc::vals::Hsepre as HsePrescaler;
5pub use crate::pac::rcc::vals::{ 6pub use crate::pac::rcc::vals::{
6 Hpre as AHBPrescaler, Msirange as MSIRange, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv, 7 Hpre as AHBPrescaler, Msirange as MSIRange, Pllm as PllPreDiv, Plln as PllMul, Pllp as PllPDiv, Pllq as PllQDiv,
7 Pllr as PllRDiv, Pllsrc as PLLSource, Ppre as APBPrescaler, Sw as ClockSrc, 8 Pllr as PllRDiv, Pllsrc as PLLSource, Ppre as APBPrescaler, Sw as ClockSrc,
@@ -27,6 +28,9 @@ pub struct Hse {
27 pub freq: Hertz, 28 pub freq: Hertz,
28 /// HSE mode. 29 /// HSE mode.
29 pub mode: HseMode, 30 pub mode: HseMode,
31 /// HSE prescaler
32 #[cfg(any(stm32wb, stm32wl))]
33 pub prescaler: HsePrescaler,
30} 34}
31 35
32#[derive(Clone, Copy)] 36#[derive(Clone, Copy)]
@@ -54,12 +58,12 @@ pub struct Config {
54 pub msi: Option<MSIRange>, 58 pub msi: Option<MSIRange>,
55 pub hsi: bool, 59 pub hsi: bool,
56 pub hse: Option<Hse>, 60 pub hse: Option<Hse>,
57 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5))] 61 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5, stm32wb))]
58 pub hsi48: bool, 62 pub hsi48: bool,
59 63
60 // pll 64 // pll
61 pub pll: Option<Pll>, 65 pub pll: Option<Pll>,
62 #[cfg(any(stm32l4, stm32l5))] 66 #[cfg(any(stm32l4, stm32l5, stm32wb))]
63 pub pllsai1: Option<Pll>, 67 pub pllsai1: Option<Pll>,
64 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))] 68 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))]
65 pub pllsai2: Option<Pll>, 69 pub pllsai2: Option<Pll>,
@@ -69,11 +73,13 @@ pub struct Config {
69 pub ahb_pre: AHBPrescaler, 73 pub ahb_pre: AHBPrescaler,
70 pub apb1_pre: APBPrescaler, 74 pub apb1_pre: APBPrescaler,
71 pub apb2_pre: APBPrescaler, 75 pub apb2_pre: APBPrescaler,
72 #[cfg(stm32wl)] 76 #[cfg(any(stm32wl5x, stm32wb))]
77 pub core2_ahb_pre: AHBPrescaler,
78 #[cfg(any(stm32wl, stm32wb))]
73 pub shared_ahb_pre: AHBPrescaler, 79 pub shared_ahb_pre: AHBPrescaler,
74 80
75 // muxes 81 // muxes
76 #[cfg(not(stm32wl))] 82 #[cfg(any(stm32l4, stm32l5, stm32wb))]
77 pub clk48_src: Clk48Src, 83 pub clk48_src: Clk48Src,
78 84
79 // low speed LSI/LSE/RTC 85 // low speed LSI/LSE/RTC
@@ -91,28 +97,63 @@ impl Default for Config {
91 ahb_pre: AHBPrescaler::DIV1, 97 ahb_pre: AHBPrescaler::DIV1,
92 apb1_pre: APBPrescaler::DIV1, 98 apb1_pre: APBPrescaler::DIV1,
93 apb2_pre: APBPrescaler::DIV1, 99 apb2_pre: APBPrescaler::DIV1,
94 #[cfg(stm32wl)] 100 #[cfg(any(stm32wl5x, stm32wb))]
101 core2_ahb_pre: AHBPrescaler::DIV1,
102 #[cfg(any(stm32wl, stm32wb))]
95 shared_ahb_pre: AHBPrescaler::DIV1, 103 shared_ahb_pre: AHBPrescaler::DIV1,
96 pll: None, 104 pll: None,
97 #[cfg(any(stm32l4, stm32l5))] 105 #[cfg(any(stm32l4, stm32l5, stm32wb))]
98 pllsai1: None, 106 pllsai1: None,
99 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))] 107 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))]
100 pllsai2: None, 108 pllsai2: None,
101 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5))] 109 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5, stm32wb))]
102 hsi48: true, 110 hsi48: true,
103 #[cfg(not(stm32wl))] 111 #[cfg(any(stm32l4, stm32l5, stm32wb))]
104 clk48_src: Clk48Src::HSI48, 112 clk48_src: Clk48Src::HSI48,
105 ls: Default::default(), 113 ls: Default::default(),
106 } 114 }
107 } 115 }
108} 116}
109 117
118#[cfg(stm32wb)]
119pub const WPAN_DEFAULT: Config = Config {
120 hse: Some(Hse {
121 freq: Hertz(32_000_000),
122 mode: HseMode::Oscillator,
123 prescaler: HsePrescaler::DIV1,
124 }),
125 mux: ClockSrc::PLL1_R,
126 hsi48: true,
127 msi: None,
128 hsi: false,
129 clk48_src: Clk48Src::PLL1_Q,
130
131 ls: super::LsConfig::default_lse(),
132
133 pll: Some(Pll {
134 source: PLLSource::HSE,
135 prediv: PllPreDiv::DIV2,
136 mul: PllMul::MUL12,
137 divp: Some(PllPDiv::DIV3), // 32 / 2 * 12 / 3 = 64Mhz
138 divq: Some(PllQDiv::DIV4), // 32 / 2 * 12 / 4 = 48Mhz
139 divr: Some(PllRDiv::DIV3), // 32 / 2 * 12 / 3 = 64Mhz
140 }),
141 pllsai1: None,
142
143 ahb_pre: AHBPrescaler::DIV1,
144 core2_ahb_pre: AHBPrescaler::DIV2,
145 shared_ahb_pre: AHBPrescaler::DIV1,
146 apb1_pre: APBPrescaler::DIV1,
147 apb2_pre: APBPrescaler::DIV1,
148};
149
110pub(crate) unsafe fn init(config: Config) { 150pub(crate) unsafe fn init(config: Config) {
111 // Switch to MSI to prevent problems with PLL configuration. 151 // Switch to MSI to prevent problems with PLL configuration.
112 if !RCC.cr().read().msion() { 152 if !RCC.cr().read().msion() {
113 // Turn on MSI and configure it to 4MHz. 153 // Turn on MSI and configure it to 4MHz.
114 RCC.cr().modify(|w| { 154 RCC.cr().modify(|w| {
115 w.set_msirgsel(Msirgsel::CR); 155 #[cfg(not(stm32wb))]
156 w.set_msirgsel(crate::pac::rcc::vals::Msirgsel::CR);
116 w.set_msirange(MSIRange::RANGE4M); 157 w.set_msirange(MSIRange::RANGE4M);
117 w.set_msipllen(false); 158 w.set_msipllen(false);
118 w.set_msion(true) 159 w.set_msion(true)
@@ -138,8 +179,9 @@ pub(crate) unsafe fn init(config: Config) {
138 let msi = config.msi.map(|range| { 179 let msi = config.msi.map(|range| {
139 // Enable MSI 180 // Enable MSI
140 RCC.cr().modify(|w| { 181 RCC.cr().modify(|w| {
182 #[cfg(not(stm32wb))]
183 w.set_msirgsel(crate::pac::rcc::vals::Msirgsel::CR);
141 w.set_msirange(range); 184 w.set_msirange(range);
142 w.set_msirgsel(Msirgsel::CR);
143 w.set_msion(true); 185 w.set_msion(true);
144 186
145 // If LSE is enabled, enable calibration of MSI 187 // If LSE is enabled, enable calibration of MSI
@@ -173,7 +215,7 @@ pub(crate) unsafe fn init(config: Config) {
173 hse.freq 215 hse.freq
174 }); 216 });
175 217
176 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5))] 218 #[cfg(any(all(stm32l4, not(any(stm32l47x, stm32l48x))), stm32l5, stm32wb))]
177 let hsi48 = config.hsi48.then(|| { 219 let hsi48 = config.hsi48.then(|| {
178 RCC.crrcr().modify(|w| w.set_hsi48on(true)); 220 RCC.crrcr().modify(|w| w.set_hsi48on(true));
179 while !RCC.crrcr().read().hsi48rdy() {} 221 while !RCC.crrcr().read().hsi48rdy() {}
@@ -185,7 +227,7 @@ pub(crate) unsafe fn init(config: Config) {
185 227
186 let _plls = [ 228 let _plls = [
187 &config.pll, 229 &config.pll,
188 #[cfg(any(stm32l4, stm32l5))] 230 #[cfg(any(stm32l4, stm32l5, stm32wb))]
189 &config.pllsai1, 231 &config.pllsai1,
190 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))] 232 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))]
191 &config.pllsai2, 233 &config.pllsai2,
@@ -214,7 +256,7 @@ pub(crate) unsafe fn init(config: Config) {
214 256
215 let pll_input = PllInput { hse, hsi, msi }; 257 let pll_input = PllInput { hse, hsi, msi };
216 let pll = init_pll(PllInstance::Pll, config.pll, &pll_input); 258 let pll = init_pll(PllInstance::Pll, config.pll, &pll_input);
217 #[cfg(any(stm32l4, stm32l5))] 259 #[cfg(any(stm32l4, stm32l5, stm32wb))]
218 let pllsai1 = init_pll(PllInstance::Pllsai1, config.pllsai1, &pll_input); 260 let pllsai1 = init_pll(PllInstance::Pllsai1, config.pllsai1, &pll_input);
219 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))] 261 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))]
220 let _pllsai2 = init_pll(PllInstance::Pllsai2, config.pllsai2, &pll_input); 262 let _pllsai2 = init_pll(PllInstance::Pllsai2, config.pllsai2, &pll_input);
@@ -230,7 +272,7 @@ pub(crate) unsafe fn init(config: Config) {
230 RCC.ccipr().modify(|w| w.set_clk48sel(config.clk48_src)); 272 RCC.ccipr().modify(|w| w.set_clk48sel(config.clk48_src));
231 #[cfg(stm32l5)] 273 #[cfg(stm32l5)]
232 RCC.ccipr1().modify(|w| w.set_clk48sel(config.clk48_src)); 274 RCC.ccipr1().modify(|w| w.set_clk48sel(config.clk48_src));
233 #[cfg(not(stm32wl))] 275 #[cfg(any(stm32l4, stm32l5, stm32wb))]
234 let _clk48 = match config.clk48_src { 276 let _clk48 = match config.clk48_src {
235 Clk48Src::HSI48 => hsi48, 277 Clk48Src::HSI48 => hsi48,
236 Clk48Src::MSI => msi, 278 Clk48Src::MSI => msi,
@@ -261,7 +303,9 @@ pub(crate) unsafe fn init(config: Config) {
261 } 303 }
262 }; 304 };
263 305
264 #[cfg(stm32wl)] 306 #[cfg(any(stm32wl5x, stm32wb))]
307 let _ahb2_freq = sys_clk / config.core2_ahb_pre;
308 #[cfg(any(stm32wl, stm32wb))]
265 let ahb3_freq = sys_clk / config.shared_ahb_pre; 309 let ahb3_freq = sys_clk / config.shared_ahb_pre;
266 310
267 // Set flash wait states 311 // Set flash wait states
@@ -290,6 +334,15 @@ pub(crate) unsafe fn init(config: Config) {
290 ..=36_000_000 => 1, 334 ..=36_000_000 => 1,
291 _ => 2, 335 _ => 2,
292 }; 336 };
337 #[cfg(stm32wb)]
338 let latency = match ahb3_freq.0 {
339 // VOS RANGE1, others TODO.
340 ..=18_000_000 => 0,
341 ..=36_000_000 => 1,
342 ..=54_000_000 => 2,
343 ..=64_000_000 => 3,
344 _ => 4,
345 };
293 346
294 FLASH.acr().modify(|w| w.set_latency(latency)); 347 FLASH.acr().modify(|w| w.set_latency(latency));
295 while FLASH.acr().read().latency() != latency {} 348 while FLASH.acr().read().latency() != latency {}
@@ -302,12 +355,16 @@ pub(crate) unsafe fn init(config: Config) {
302 }); 355 });
303 while RCC.cfgr().read().sws() != config.mux {} 356 while RCC.cfgr().read().sws() != config.mux {}
304 357
305 #[cfg(stm32wl)] 358 #[cfg(any(stm32wl, stm32wb))]
306 { 359 {
307 RCC.extcfgr().modify(|w| { 360 RCC.extcfgr().modify(|w| {
308 w.set_shdhpre(config.shared_ahb_pre); 361 w.set_shdhpre(config.shared_ahb_pre);
362 #[cfg(any(stm32wl5x, stm32wb))]
363 w.set_c2hpre(config.core2_ahb_pre);
309 }); 364 });
310 while !RCC.extcfgr().read().shdhpref() {} 365 while !RCC.extcfgr().read().shdhpref() {}
366 #[cfg(any(stm32wl5x, stm32wb))]
367 while !RCC.extcfgr().read().c2hpref() {}
311 } 368 }
312 369
313 set_freqs(Clocks { 370 set_freqs(Clocks {
@@ -387,7 +444,7 @@ struct PllOutput {
387#[derive(PartialEq, Eq, Clone, Copy)] 444#[derive(PartialEq, Eq, Clone, Copy)]
388enum PllInstance { 445enum PllInstance {
389 Pll, 446 Pll,
390 #[cfg(any(stm32l4, stm32l5))] 447 #[cfg(any(stm32l4, stm32l5, stm32wb))]
391 Pllsai1, 448 Pllsai1,
392 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))] 449 #[cfg(any(stm32l47x, stm32l48x, stm32l49x, stm32l4ax, rcc_l4plus, stm32l5))]
393 Pllsai2, 450 Pllsai2,
@@ -400,7 +457,7 @@ fn init_pll(instance: PllInstance, config: Option<Pll>, input: &PllInput) -> Pll
400 RCC.cr().modify(|w| w.set_pllon(false)); 457 RCC.cr().modify(|w| w.set_pllon(false));
401 while RCC.cr().read().pllrdy() {} 458 while RCC.cr().read().pllrdy() {}
402 } 459 }
403 #[cfg(any(stm32l4, stm32l5))] 460 #[cfg(any(stm32l4, stm32l5, stm32wb))]
404 PllInstance::Pllsai1 => { 461 PllInstance::Pllsai1 => {
405 RCC.cr().modify(|w| w.set_pllsai1on(false)); 462 RCC.cr().modify(|w| w.set_pllsai1on(false));
406 while RCC.cr().read().pllsai1rdy() {} 463 while RCC.cr().read().pllsai1rdy() {}
@@ -459,7 +516,7 @@ fn init_pll(instance: PllInstance, config: Option<Pll>, input: &PllInput) -> Pll
459 w.set_pllsrc(pll.source); 516 w.set_pllsrc(pll.source);
460 write_fields!(w); 517 write_fields!(w);
461 }), 518 }),
462 #[cfg(any(stm32l4, stm32l5))] 519 #[cfg(any(stm32l4, stm32l5, stm32wb))]
463 PllInstance::Pllsai1 => RCC.pllsai1cfgr().write(|w| { 520 PllInstance::Pllsai1 => RCC.pllsai1cfgr().write(|w| {
464 #[cfg(any(rcc_l4plus, stm32l5))] 521 #[cfg(any(rcc_l4plus, stm32l5))]
465 w.set_pllm(pll.prediv); 522 w.set_pllm(pll.prediv);
@@ -483,7 +540,7 @@ fn init_pll(instance: PllInstance, config: Option<Pll>, input: &PllInput) -> Pll
483 RCC.cr().modify(|w| w.set_pllon(true)); 540 RCC.cr().modify(|w| w.set_pllon(true));
484 while !RCC.cr().read().pllrdy() {} 541 while !RCC.cr().read().pllrdy() {}
485 } 542 }
486 #[cfg(any(stm32l4, stm32l5))] 543 #[cfg(any(stm32l4, stm32l5, stm32wb))]
487 PllInstance::Pllsai1 => { 544 PllInstance::Pllsai1 => {
488 RCC.cr().modify(|w| w.set_pllsai1on(true)); 545 RCC.cr().modify(|w| w.set_pllsai1on(true));
489 while !RCC.cr().read().pllsai1rdy() {} 546 while !RCC.cr().read().pllsai1rdy() {}
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs
index 78d54f803..4f3d5b98b 100644
--- a/embassy-stm32/src/rcc/mod.rs
+++ b/embassy-stm32/src/rcc/mod.rs
@@ -19,9 +19,8 @@ pub use mco::*;
19#[cfg_attr(rcc_g4, path = "g4.rs")] 19#[cfg_attr(rcc_g4, path = "g4.rs")]
20#[cfg_attr(any(rcc_h5, rcc_h50, rcc_h7, rcc_h7rm0433, rcc_h7ab), path = "h.rs")] 20#[cfg_attr(any(rcc_h5, rcc_h50, rcc_h7, rcc_h7rm0433, rcc_h7ab), path = "h.rs")]
21#[cfg_attr(any(rcc_l0, rcc_l0_v2, rcc_l1), path = "l0l1.rs")] 21#[cfg_attr(any(rcc_l0, rcc_l0_v2, rcc_l1), path = "l0l1.rs")]
22#[cfg_attr(any(rcc_l4, rcc_l4plus, rcc_l5, rcc_wl5, rcc_wle), path = "l4l5.rs")] 22#[cfg_attr(any(rcc_l4, rcc_l4plus, rcc_l5, rcc_wl5, rcc_wle, rcc_wb), path = "l4l5.rs")]
23#[cfg_attr(rcc_u5, path = "u5.rs")] 23#[cfg_attr(rcc_u5, path = "u5.rs")]
24#[cfg_attr(rcc_wb, path = "wb.rs")]
25#[cfg_attr(rcc_wba, path = "wba.rs")] 24#[cfg_attr(rcc_wba, path = "wba.rs")]
26mod _version; 25mod _version;
27#[cfg(feature = "low-power")] 26#[cfg(feature = "low-power")]
diff --git a/embassy-stm32/src/rcc/wb.rs b/embassy-stm32/src/rcc/wb.rs
deleted file mode 100644
index 2d0b2711a..000000000
--- a/embassy-stm32/src/rcc/wb.rs
+++ /dev/null
@@ -1,258 +0,0 @@
1pub use crate::pac::rcc::vals::{
2 Hpre as AHBPrescaler, Hsepre as HsePrescaler, Pllm, Plln, Pllp, Pllq, Pllr, Pllsrc as PllSource,
3 Ppre as APBPrescaler, Sw as Sysclk,
4};
5use crate::rcc::{set_freqs, Clocks};
6use crate::time::{mhz, Hertz};
7
8/// HSI speed
9pub const HSI_FREQ: Hertz = Hertz(16_000_000);
10
11pub struct Hse {
12 pub prediv: HsePrescaler,
13
14 pub frequency: Hertz,
15}
16
17pub struct PllMux {
18 /// Source clock selection.
19 pub source: PllSource,
20
21 /// PLL pre-divider (DIVM). Must be between 1 and 63.
22 pub prediv: Pllm,
23}
24
25pub struct Pll {
26 /// PLL multiplication factor. Must be between 4 and 512.
27 pub mul: Plln,
28
29 /// PLL P division factor. If None, PLL P output is disabled. Must be between 1 and 128.
30 /// On PLL1, it must be even (in particular, it cannot be 1.)
31 pub divp: Option<Pllp>,
32 /// PLL Q division factor. If None, PLL Q output is disabled. Must be between 1 and 128.
33 pub divq: Option<Pllq>,
34 /// PLL R division factor. If None, PLL R output is disabled. Must be between 1 and 128.
35 pub divr: Option<Pllr>,
36}
37
38/// Clocks configutation
39pub struct Config {
40 pub hse: Option<Hse>,
41 pub sys: Sysclk,
42 pub mux: Option<PllMux>,
43 pub hsi48: bool,
44
45 pub pll: Option<Pll>,
46 pub pllsai: Option<Pll>,
47
48 pub ahb1_pre: AHBPrescaler,
49 pub ahb2_pre: AHBPrescaler,
50 pub ahb3_pre: AHBPrescaler,
51 pub apb1_pre: APBPrescaler,
52 pub apb2_pre: APBPrescaler,
53
54 pub ls: super::LsConfig,
55}
56
57pub const WPAN_DEFAULT: Config = Config {
58 hse: Some(Hse {
59 frequency: mhz(32),
60 prediv: HsePrescaler::DIV1,
61 }),
62 sys: Sysclk::PLL1_R,
63 mux: Some(PllMux {
64 source: PllSource::HSE,
65 prediv: Pllm::DIV2,
66 }),
67 hsi48: true,
68
69 ls: super::LsConfig::default_lse(),
70
71 pll: Some(Pll {
72 mul: Plln::MUL12,
73 divp: Some(Pllp::DIV3),
74 divq: Some(Pllq::DIV4),
75 divr: Some(Pllr::DIV3),
76 }),
77 pllsai: None,
78
79 ahb1_pre: AHBPrescaler::DIV1,
80 ahb2_pre: AHBPrescaler::DIV2,
81 ahb3_pre: AHBPrescaler::DIV1,
82 apb1_pre: APBPrescaler::DIV1,
83 apb2_pre: APBPrescaler::DIV1,
84};
85
86impl Default for Config {
87 #[inline]
88 fn default() -> Config {
89 Config {
90 sys: Sysclk::HSI,
91 hse: None,
92 mux: None,
93 pll: None,
94 pllsai: None,
95 hsi48: true,
96
97 ls: Default::default(),
98
99 ahb1_pre: AHBPrescaler::DIV1,
100 ahb2_pre: AHBPrescaler::DIV1,
101 ahb3_pre: AHBPrescaler::DIV1,
102 apb1_pre: APBPrescaler::DIV1,
103 apb2_pre: APBPrescaler::DIV1,
104 }
105 }
106}
107
108#[cfg(stm32wb)]
109/// RCC initialization function
110pub(crate) unsafe fn init(config: Config) {
111 let hse_clk = config.hse.as_ref().map(|hse| hse.frequency / hse.prediv);
112
113 let mux_clk = config.mux.as_ref().map(|pll_mux| {
114 (match pll_mux.source {
115 PllSource::HSE => hse_clk.unwrap(),
116 PllSource::HSI => HSI_FREQ,
117 _ => unreachable!(),
118 } / pll_mux.prediv)
119 });
120
121 let (pll_r, _pll_q, _pll_p) = match &config.pll {
122 Some(pll) => {
123 let pll_vco = mux_clk.unwrap() * pll.mul as u32;
124
125 (
126 pll.divr.map(|divr| pll_vco / divr),
127 pll.divq.map(|divq| pll_vco / divq),
128 pll.divp.map(|divp| pll_vco / divp),
129 )
130 }
131 None => (None, None, None),
132 };
133
134 let sys_clk = match config.sys {
135 Sysclk::HSE => hse_clk.unwrap(),
136 Sysclk::HSI => HSI_FREQ,
137 Sysclk::PLL1_R => pll_r.unwrap(),
138 _ => unreachable!(),
139 };
140
141 let ahb1_clk = sys_clk / config.ahb1_pre;
142 let ahb2_clk = sys_clk / config.ahb2_pre;
143 let ahb3_clk = sys_clk / config.ahb3_pre;
144
145 let (apb1_clk, apb1_tim_clk) = match config.apb1_pre {
146 APBPrescaler::DIV1 => (ahb1_clk, ahb1_clk),
147 pre => {
148 let freq = ahb1_clk / pre;
149 (freq, freq * 2u32)
150 }
151 };
152
153 let (apb2_clk, apb2_tim_clk) = match config.apb2_pre {
154 APBPrescaler::DIV1 => (ahb1_clk, ahb1_clk),
155 pre => {
156 let freq = ahb1_clk / pre;
157 (freq, freq * 2u32)
158 }
159 };
160
161 let rcc = crate::pac::RCC;
162
163 let needs_hsi = if let Some(pll_mux) = &config.mux {
164 pll_mux.source == PllSource::HSI
165 } else {
166 false
167 };
168
169 if needs_hsi || config.sys == Sysclk::HSI {
170 rcc.cr().modify(|w| {
171 w.set_hsion(true);
172 });
173
174 while !rcc.cr().read().hsirdy() {}
175 }
176
177 rcc.cfgr().modify(|w| w.set_stopwuck(true));
178
179 let rtc = config.ls.init();
180
181 match &config.hse {
182 Some(hse) => {
183 rcc.cr().modify(|w| {
184 w.set_hsepre(hse.prediv);
185 w.set_hseon(true);
186 });
187
188 while !rcc.cr().read().hserdy() {}
189 }
190 _ => {}
191 }
192
193 match &config.mux {
194 Some(pll_mux) => {
195 rcc.pllcfgr().modify(|w| {
196 w.set_pllm(pll_mux.prediv);
197 w.set_pllsrc(pll_mux.source.into());
198 });
199 }
200 _ => {}
201 };
202
203 match &config.pll {
204 Some(pll) => {
205 rcc.pllcfgr().modify(|w| {
206 w.set_plln(pll.mul);
207 pll.divp.map(|divp| {
208 w.set_pllpen(true);
209 w.set_pllp(divp)
210 });
211 pll.divq.map(|divq| {
212 w.set_pllqen(true);
213 w.set_pllq(divq)
214 });
215 pll.divr.map(|divr| {
216 w.set_pllren(true);
217 w.set_pllr(divr);
218 });
219 });
220
221 rcc.cr().modify(|w| w.set_pllon(true));
222
223 while !rcc.cr().read().pllrdy() {}
224 }
225 _ => {}
226 }
227
228 let _hsi48 = config.hsi48.then(|| {
229 rcc.crrcr().modify(|w| w.set_hsi48on(true));
230 while !rcc.crrcr().read().hsi48rdy() {}
231
232 Hertz(48_000_000)
233 });
234
235 rcc.cfgr().modify(|w| {
236 w.set_sw(config.sys.into());
237 w.set_hpre(config.ahb1_pre);
238 w.set_ppre1(config.apb1_pre);
239 w.set_ppre2(config.apb2_pre);
240 });
241
242 rcc.extcfgr().modify(|w| {
243 w.set_c2hpre(config.ahb2_pre);
244 w.set_shdhpre(config.ahb3_pre);
245 });
246
247 set_freqs(Clocks {
248 sys: sys_clk,
249 hclk1: ahb1_clk,
250 hclk2: ahb2_clk,
251 hclk3: ahb3_clk,
252 pclk1: apb1_clk,
253 pclk2: apb2_clk,
254 pclk1_tim: apb1_tim_clk,
255 pclk2_tim: apb2_tim_clk,
256 rtc,
257 })
258}
diff --git a/examples/stm32wl/src/bin/lora_lorawan.rs b/examples/stm32wl/src/bin/lora_lorawan.rs
index e26c274ad..35a6a8425 100644
--- a/examples/stm32wl/src/bin/lora_lorawan.rs
+++ b/examples/stm32wl/src/bin/lora_lorawan.rs
@@ -39,6 +39,7 @@ async fn main(_spawner: Spawner) {
39 config.rcc.hse = Some(Hse { 39 config.rcc.hse = Some(Hse {
40 freq: Hertz(32_000_000), 40 freq: Hertz(32_000_000),
41 mode: HseMode::Bypass, 41 mode: HseMode::Bypass,
42 prescaler: HsePrescaler::DIV1,
42 }); 43 });
43 config.rcc.mux = ClockSrc::PLL1_R; 44 config.rcc.mux = ClockSrc::PLL1_R;
44 config.rcc.pll = Some(Pll { 45 config.rcc.pll = Some(Pll {
diff --git a/examples/stm32wl/src/bin/lora_p2p_receive.rs b/examples/stm32wl/src/bin/lora_p2p_receive.rs
index be33f39c1..1c485d739 100644
--- a/examples/stm32wl/src/bin/lora_p2p_receive.rs
+++ b/examples/stm32wl/src/bin/lora_p2p_receive.rs
@@ -11,6 +11,7 @@ use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant};
11use embassy_stm32::bind_interrupts; 11use embassy_stm32::bind_interrupts;
12use embassy_stm32::gpio::{Level, Output, Pin, Speed}; 12use embassy_stm32::gpio::{Level, Output, Pin, Speed};
13use embassy_stm32::spi::Spi; 13use embassy_stm32::spi::Spi;
14use embassy_stm32::time::Hertz;
14use embassy_time::{Delay, Timer}; 15use embassy_time::{Delay, Timer};
15use lora_phy::mod_params::*; 16use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2; 17use lora_phy::sx1261_2::SX1261_2;
@@ -26,7 +27,23 @@ bind_interrupts!(struct Irqs{
26#[embassy_executor::main] 27#[embassy_executor::main]
27async fn main(_spawner: Spawner) { 28async fn main(_spawner: Spawner) {
28 let mut config = embassy_stm32::Config::default(); 29 let mut config = embassy_stm32::Config::default();
29 config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE; 30 {
31 use embassy_stm32::rcc::*;
32 config.rcc.hse = Some(Hse {
33 freq: Hertz(32_000_000),
34 mode: HseMode::Bypass,
35 prescaler: HsePrescaler::DIV1,
36 });
37 config.rcc.mux = ClockSrc::PLL1_R;
38 config.rcc.pll = Some(Pll {
39 source: PLLSource::HSE,
40 prediv: PllPreDiv::DIV2,
41 mul: PllMul::MUL6,
42 divp: None,
43 divq: Some(PllQDiv::DIV2), // PLL1_Q clock (32 / 2 * 6 / 2), used for RNG
44 divr: Some(PllRDiv::DIV2), // sysclk 48Mhz clock (32 / 2 * 6 / 2)
45 });
46 }
30 let p = embassy_stm32::init(config); 47 let p = embassy_stm32::init(config);
31 48
32 let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2); 49 let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2);
diff --git a/examples/stm32wl/src/bin/lora_p2p_send.rs b/examples/stm32wl/src/bin/lora_p2p_send.rs
index 85f6a84b7..3afa78acb 100644
--- a/examples/stm32wl/src/bin/lora_p2p_send.rs
+++ b/examples/stm32wl/src/bin/lora_p2p_send.rs
@@ -11,6 +11,7 @@ use embassy_lora::iv::{InterruptHandler, Stm32wlInterfaceVariant};
11use embassy_stm32::bind_interrupts; 11use embassy_stm32::bind_interrupts;
12use embassy_stm32::gpio::{Level, Output, Pin, Speed}; 12use embassy_stm32::gpio::{Level, Output, Pin, Speed};
13use embassy_stm32::spi::Spi; 13use embassy_stm32::spi::Spi;
14use embassy_stm32::time::Hertz;
14use embassy_time::Delay; 15use embassy_time::Delay;
15use lora_phy::mod_params::*; 16use lora_phy::mod_params::*;
16use lora_phy::sx1261_2::SX1261_2; 17use lora_phy::sx1261_2::SX1261_2;
@@ -26,7 +27,23 @@ bind_interrupts!(struct Irqs{
26#[embassy_executor::main] 27#[embassy_executor::main]
27async fn main(_spawner: Spawner) { 28async fn main(_spawner: Spawner) {
28 let mut config = embassy_stm32::Config::default(); 29 let mut config = embassy_stm32::Config::default();
29 config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE; 30 {
31 use embassy_stm32::rcc::*;
32 config.rcc.hse = Some(Hse {
33 freq: Hertz(32_000_000),
34 mode: HseMode::Bypass,
35 prescaler: HsePrescaler::DIV1,
36 });
37 config.rcc.mux = ClockSrc::PLL1_R;
38 config.rcc.pll = Some(Pll {
39 source: PLLSource::HSE,
40 prediv: PllPreDiv::DIV2,
41 mul: PllMul::MUL6,
42 divp: None,
43 divq: Some(PllQDiv::DIV2), // PLL1_Q clock (32 / 2 * 6 / 2), used for RNG
44 divr: Some(PllRDiv::DIV2), // sysclk 48Mhz clock (32 / 2 * 6 / 2)
45 });
46 }
30 let p = embassy_stm32::init(config); 47 let p = embassy_stm32::init(config);
31 48
32 let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2); 49 let spi = Spi::new_subghz(p.SUBGHZSPI, p.DMA1_CH1, p.DMA1_CH2);
diff --git a/examples/stm32wl/src/bin/random.rs b/examples/stm32wl/src/bin/random.rs
index 2cf7ef9d0..1a8822b42 100644
--- a/examples/stm32wl/src/bin/random.rs
+++ b/examples/stm32wl/src/bin/random.rs
@@ -21,6 +21,7 @@ async fn main(_spawner: Spawner) {
21 config.rcc.hse = Some(Hse { 21 config.rcc.hse = Some(Hse {
22 freq: Hertz(32_000_000), 22 freq: Hertz(32_000_000),
23 mode: HseMode::Bypass, 23 mode: HseMode::Bypass,
24 prescaler: HsePrescaler::DIV1,
24 }); 25 });
25 config.rcc.mux = ClockSrc::PLL1_R; 26 config.rcc.mux = ClockSrc::PLL1_R;
26 config.rcc.pll = Some(Pll { 27 config.rcc.pll = Some(Pll {
diff --git a/examples/stm32wl/src/bin/rtc.rs b/examples/stm32wl/src/bin/rtc.rs
index 9ebb05f22..b3b7f9c5c 100644
--- a/examples/stm32wl/src/bin/rtc.rs
+++ b/examples/stm32wl/src/bin/rtc.rs
@@ -5,20 +5,34 @@
5use chrono::{NaiveDate, NaiveDateTime}; 5use chrono::{NaiveDate, NaiveDateTime};
6use defmt::*; 6use defmt::*;
7use embassy_executor::Spawner; 7use embassy_executor::Spawner;
8use embassy_stm32::rcc::{ClockSrc, LsConfig};
9use embassy_stm32::rtc::{Rtc, RtcConfig}; 8use embassy_stm32::rtc::{Rtc, RtcConfig};
9use embassy_stm32::time::Hertz;
10use embassy_stm32::Config; 10use embassy_stm32::Config;
11use embassy_time::Timer; 11use embassy_time::Timer;
12use {defmt_rtt as _, panic_probe as _}; 12use {defmt_rtt as _, panic_probe as _};
13 13
14#[embassy_executor::main] 14#[embassy_executor::main]
15async fn main(_spawner: Spawner) { 15async fn main(_spawner: Spawner) {
16 let p = { 16 let mut config = Config::default();
17 let mut config = Config::default(); 17 {
18 config.rcc.mux = ClockSrc::HSE; 18 use embassy_stm32::rcc::*;
19 config.rcc.ls = LsConfig::default_lse(); 19 config.rcc.ls = LsConfig::default_lse();
20 embassy_stm32::init(config) 20 config.rcc.hse = Some(Hse {
21 }; 21 freq: Hertz(32_000_000),
22 mode: HseMode::Bypass,
23 prescaler: HsePrescaler::DIV1,
24 });
25 config.rcc.mux = ClockSrc::PLL1_R;
26 config.rcc.pll = Some(Pll {
27 source: PLLSource::HSE,
28 prediv: PllPreDiv::DIV2,
29 mul: PllMul::MUL6,
30 divp: None,
31 divq: Some(PllQDiv::DIV2), // PLL1_Q clock (32 / 2 * 6 / 2), used for RNG
32 divr: Some(PllRDiv::DIV2), // sysclk 48Mhz clock (32 / 2 * 6 / 2)
33 });
34 }
35 let p = embassy_stm32::init(config);
22 info!("Hello World!"); 36 info!("Hello World!");
23 37
24 let now = NaiveDate::from_ymd_opt(2020, 5, 15) 38 let now = NaiveDate::from_ymd_opt(2020, 5, 15)
diff --git a/tests/stm32/src/common.rs b/tests/stm32/src/common.rs
index cb1738154..4f51e4f6a 100644
--- a/tests/stm32/src/common.rs
+++ b/tests/stm32/src/common.rs
@@ -227,6 +227,11 @@ pub fn config() -> Config {
227 #[allow(unused_mut)] 227 #[allow(unused_mut)]
228 let mut config = Config::default(); 228 let mut config = Config::default();
229 229
230 #[cfg(feature = "stm32wb55rg")]
231 {
232 config.rcc = embassy_stm32::rcc::WPAN_DEFAULT;
233 }
234
230 #[cfg(feature = "stm32f207zg")] 235 #[cfg(feature = "stm32f207zg")]
231 { 236 {
232 use embassy_stm32::rcc::*; 237 use embassy_stm32::rcc::*;
@@ -405,6 +410,7 @@ pub fn config() -> Config {
405 config.rcc.hse = Some(Hse { 410 config.rcc.hse = Some(Hse {
406 freq: Hertz(32_000_000), 411 freq: Hertz(32_000_000),
407 mode: HseMode::Bypass, 412 mode: HseMode::Bypass,
413 prescaler: HsePrescaler::DIV1,
408 }); 414 });
409 config.rcc.mux = ClockSrc::PLL1_R; 415 config.rcc.mux = ClockSrc::PLL1_R;
410 config.rcc.pll = Some(Pll { 416 config.rcc.pll = Some(Pll {