aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2021-05-26 13:08:14 +0200
committerUlf Lilleengen <[email protected]>2021-05-26 13:08:14 +0200
commitea67940743733bbef1d758934bb01b4e8b0be136 (patch)
tree387980bdfb4a5688c8ce30537f9c0df9986aba79
parentc501b162fc5a7aa1219689132537471b8826a638 (diff)
Refactor
-rw-r--r--embassy-stm32/src/rcc/l0/mod.rs88
1 files changed, 53 insertions, 35 deletions
diff --git a/embassy-stm32/src/rcc/l0/mod.rs b/embassy-stm32/src/rcc/l0/mod.rs
index 6c89022b8..8fbd6038e 100644
--- a/embassy-stm32/src/rcc/l0/mod.rs
+++ b/embassy-stm32/src/rcc/l0/mod.rs
@@ -1,7 +1,7 @@
1use crate::clock::Clock; 1use crate::clock::Clock;
2use crate::interrupt; 2use crate::interrupt;
3use crate::pac; 3use crate::pac;
4use crate::pac::peripherals::{self, TIM2}; 4use crate::pac::peripherals::{self, RCC, TIM2};
5use crate::time::Hertz; 5use crate::time::Hertz;
6use crate::time::U32Ext; 6use crate::time::U32Ext;
7use pac::rcc::vals; 7use pac::rcc::vals;
@@ -195,7 +195,6 @@ impl Config {
195/// RCC peripheral 195/// RCC peripheral
196pub struct Rcc { 196pub struct Rcc {
197 clocks: Clocks, 197 clocks: Clocks,
198 rb: pac::rcc::Rcc,
199} 198}
200 199
201/* 200/*
@@ -267,38 +266,47 @@ impl Rcc {
267 266
268/// Extension trait that freezes the `RCC` peripheral with provided clocks configuration 267/// Extension trait that freezes the `RCC` peripheral with provided clocks configuration
269pub trait RccExt { 268pub trait RccExt {
270 unsafe fn freeze(self, config: Config) -> Rcc; 269 fn freeze(self, config: Config) -> Rcc;
271} 270}
272 271
273impl RccExt for pac::rcc::Rcc { 272impl RccExt for RCC {
274 // `cfgr` is almost always a constant, so make sure it can be constant-propagated properly by 273 // `cfgr` is almost always a constant, so make sure it can be constant-propagated properly by
275 // marking this function and all `Config` constructors and setters as `#[inline]`. 274 // marking this function and all `Config` constructors and setters as `#[inline]`.
276 // This saves ~900 Bytes for the `pwr.rs` example. 275 // This saves ~900 Bytes for the `pwr.rs` example.
277 #[inline] 276 #[inline]
278 unsafe fn freeze(self, cfgr: Config) -> Rcc { 277 fn freeze(self, cfgr: Config) -> Rcc {
278 let rcc = pac::RCC;
279 let (sys_clk, sw) = match cfgr.mux { 279 let (sys_clk, sw) = match cfgr.mux {
280 ClockSrc::MSI(range) => { 280 ClockSrc::MSI(range) => {
281 // Set MSI range 281 // Set MSI range
282 self.icscr().write(|w| w.set_msirange(range.into())); 282 unsafe {
283 rcc.icscr().write(|w| w.set_msirange(range.into()));
284 }
283 285
284 // Enable MSI 286 // Enable MSI
285 self.cr().write(|w| w.set_msion(Pllon::ENABLED)); 287 unsafe {
286 while !self.cr().read().msirdy() {} 288 rcc.cr().write(|w| w.set_msion(Pllon::ENABLED));
289 while !rcc.cr().read().msirdy() {}
290 }
287 291
288 let freq = 32_768 * (1 << (range as u8 + 1)); 292 let freq = 32_768 * (1 << (range as u8 + 1));
289 (freq, Sw::MSI) 293 (freq, Sw::MSI)
290 } 294 }
291 ClockSrc::HSI16 => { 295 ClockSrc::HSI16 => {
292 // Enable HSI16 296 // Enable HSI16
293 self.cr().write(|w| w.set_hsi16on(Pllon::ENABLED)); 297 unsafe {
294 while !self.cr().read().hsi16rdyf() {} 298 rcc.cr().write(|w| w.set_hsi16on(Pllon::ENABLED));
299 while !rcc.cr().read().hsi16rdyf() {}
300 }
295 301
296 (HSI_FREQ, Sw::HSI16) 302 (HSI_FREQ, Sw::HSI16)
297 } 303 }
298 ClockSrc::HSE(freq) => { 304 ClockSrc::HSE(freq) => {
299 // Enable HSE 305 // Enable HSE
300 self.cr().write(|w| w.set_hseon(Pllon::ENABLED)); 306 unsafe {
301 while !self.cr().read().hserdy() {} 307 rcc.cr().write(|w| w.set_hseon(Pllon::ENABLED));
308 while !rcc.cr().read().hserdy() {}
309 }
302 310
303 (freq.0, Sw::HSE) 311 (freq.0, Sw::HSE)
304 } 312 }
@@ -306,21 +314,27 @@ impl RccExt for pac::rcc::Rcc {
306 let freq = match src { 314 let freq = match src {
307 PLLSource::HSE(freq) => { 315 PLLSource::HSE(freq) => {
308 // Enable HSE 316 // Enable HSE
309 self.cr().write(|w| w.set_hseon(Pllon::ENABLED)); 317 unsafe {
310 while !self.cr().read().hserdy() {} 318 rcc.cr().write(|w| w.set_hseon(Pllon::ENABLED));
319 while !rcc.cr().read().hserdy() {}
320 }
311 freq.0 321 freq.0
312 } 322 }
313 PLLSource::HSI16 => { 323 PLLSource::HSI16 => {
314 // Enable HSI 324 // Enable HSI
315 self.cr().write(|w| w.set_hsi16on(Pllon::ENABLED)); 325 unsafe {
316 while !self.cr().read().hsi16rdyf() {} 326 rcc.cr().write(|w| w.set_hsi16on(Pllon::ENABLED));
327 while !rcc.cr().read().hsi16rdyf() {}
328 }
317 HSI_FREQ 329 HSI_FREQ
318 } 330 }
319 }; 331 };
320 332
321 // Disable PLL 333 // Disable PLL
322 self.cr().modify(|w| w.set_pllon(Pllon::DISABLED)); 334 unsafe {
323 while self.cr().read().pllrdy() {} 335 rcc.cr().modify(|w| w.set_pllon(Pllon::DISABLED));
336 while rcc.cr().read().pllrdy() {}
337 }
324 338
325 let freq = match mul { 339 let freq = match mul {
326 PLLMul::Mul3 => freq * 3, 340 PLLMul::Mul3 => freq * 3,
@@ -341,26 +355,30 @@ impl RccExt for pac::rcc::Rcc {
341 }; 355 };
342 assert!(freq <= 32_u32.mhz().0); 356 assert!(freq <= 32_u32.mhz().0);
343 357
344 self.cfgr().write(move |w| { 358 unsafe {
345 w.set_pllmul(mul.into()); 359 rcc.cfgr().write(move |w| {
346 w.set_plldiv(div.into()); 360 w.set_pllmul(mul.into());
347 w.set_pllsrc(src.into()); 361 w.set_plldiv(div.into());
348 }); 362 w.set_pllsrc(src.into());
363 });
349 364
350 // Enable PLL 365 // Enable PLL
351 self.cr().modify(|w| w.set_pllon(Pllon::ENABLED)); 366 rcc.cr().modify(|w| w.set_pllon(Pllon::ENABLED));
352 while !self.cr().read().pllrdy() {} 367 while !rcc.cr().read().pllrdy() {}
368 }
353 369
354 (freq, Sw::PLL) 370 (freq, Sw::PLL)
355 } 371 }
356 }; 372 };
357 373
358 self.cfgr().modify(|w| { 374 unsafe {
359 w.set_sw(sw.into()); 375 rcc.cfgr().modify(|w| {
360 w.set_hpre(cfgr.ahb_pre.into()); 376 w.set_sw(sw.into());
361 w.set_ppre(0, cfgr.apb1_pre.into()); 377 w.set_hpre(cfgr.ahb_pre.into());
362 w.set_ppre(1, cfgr.apb2_pre.into()); 378 w.set_ppre(0, cfgr.apb1_pre.into());
363 }); 379 w.set_ppre(1, cfgr.apb2_pre.into());
380 });
381 }
364 382
365 let ahb_freq: u32 = match cfgr.ahb_pre { 383 let ahb_freq: u32 = match cfgr.ahb_pre {
366 AHBPrescaler::NotDivided => sys_clk, 384 AHBPrescaler::NotDivided => sys_clk,
@@ -403,7 +421,7 @@ impl RccExt for pac::rcc::Rcc {
403 apb2_pre, 421 apb2_pre,
404 }; 422 };
405 423
406 Rcc { rb: self, clocks } 424 Rcc { clocks }
407 } 425 }
408} 426}
409 427
@@ -570,7 +588,6 @@ pub type SystemClock = Clock<TIM2>;
570 588
571pub unsafe fn init(config: Config) -> SystemClock { 589pub unsafe fn init(config: Config) -> SystemClock {
572 let rcc = pac::RCC; 590 let rcc = pac::RCC;
573
574 let enabled = vals::Iophen::ENABLED; 591 let enabled = vals::Iophen::ENABLED;
575 rcc.iopenr().write(|w| { 592 rcc.iopenr().write(|w| {
576 w.set_iopaen(enabled); 593 w.set_iopaen(enabled);
@@ -581,7 +598,8 @@ pub unsafe fn init(config: Config) -> SystemClock {
581 w.set_iophen(enabled); 598 w.set_iophen(enabled);
582 }); 599 });
583 600
584 let r = rcc.freeze(config); 601 let r = <peripherals::RCC as embassy::util::Steal>::steal();
602 let r = r.freeze(config);
585 603
586 rcc.apb1enr().modify(|w| w.set_tim2en(Lptimen::ENABLED)); 604 rcc.apb1enr().modify(|w| w.set_tim2en(Lptimen::ENABLED));
587 rcc.apb1rstr().modify(|w| w.set_tim2rst(true)); 605 rcc.apb1rstr().modify(|w| w.set_tim2rst(true));