Skip to content

Commit fed4fb2

Browse files
committed
Refactor #30
1 parent a72e4c5 commit fed4fb2

File tree

3 files changed

+37
-69
lines changed

3 files changed

+37
-69
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ stm32l4x2 = ["stm32l4/stm32l4x2"]
5252
stm32l4x3 = ["stm32l4/stm32l4x3"]
5353
stm32l4x5 = ["stm32l4/stm32l4x5"]
5454
stm32l4x6 = ["stm32l4/stm32l4x6"]
55-
stm32l47x = ["stm32l4x6"]
5655
unproven = ["embedded-hal/unproven"]
5756

5857
[dev-dependencies]

src/rcc.rs

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ impl RccExt for RCC {
5454
apb2: APB2 { _0: () },
5555
bdcr: BDCR { _0: () },
5656
csr: CSR { _0: () },
57-
#[cfg(not(feature = "stm32l47x"))]
57+
#[cfg(not(feature = "stm32l4x6"))]
5858
crrcr: CRRCR { _0: () },
5959
cfgr: CFGR {
6060
hclk: None,
61-
#[cfg(not(feature = "stm32l47x"))]
6261
hsi48: false,
6362
msi: None,
6463
lsi: false,
@@ -92,7 +91,7 @@ pub struct Rcc {
9291
/// Control/Status Register
9392
pub csr: CSR,
9493
/// Clock recovery RC register
95-
#[cfg(not(feature = "stm32l47x"))]
94+
#[cfg(not(feature = "stm32l4x6"))]
9695
pub crrcr: CRRCR,
9796
}
9897

@@ -111,12 +110,12 @@ impl CSR {
111110
}
112111

113112
/// Clock recovery RC register
114-
#[cfg(not(feature = "stm32l47x"))]
113+
#[cfg(not(feature = "stm32l4x6"))]
115114
pub struct CRRCR {
116115
_0: (),
117116
}
118117

119-
#[cfg(not(feature = "stm32l47x"))]
118+
#[cfg(not(feature = "stm32l4x6"))]
120119
impl CRRCR {
121120
// TODO remove `allow`
122121
#[allow(dead_code)]
@@ -263,8 +262,6 @@ const HSI: u32 = 16_000_000; // Hz
263262
/// Clock configuration
264263
pub struct CFGR {
265264
hclk: Option<u32>,
266-
// should we use an option? it can really only be on/off
267-
#[cfg(not(feature = "stm32l47x"))]
268265
hsi48: bool,
269266
msi: Option<MsiFreq>,
270267
lsi: bool,
@@ -284,8 +281,8 @@ impl CFGR {
284281
self
285282
}
286283

287-
/// Enable the 48Mh USB, RNG, SDMMC clock source.
288-
#[cfg(not(feature = "stm32l47x"))]
284+
/// Enable the 48Mh USB, RNG, SDMMC clock source. Not available on stm32l4x6 series
285+
#[cfg(not(feature = "stm32l4x6"))]
289286
pub fn hsi48(mut self, on: bool) -> Self
290287
{
291288
self.hsi48 = on;
@@ -343,7 +340,7 @@ impl CFGR {
343340
}
344341

345342
/// Freezes the clock configuration, making it effective
346-
pub fn common_freeze(&self, acr: &mut ACR) -> (Hertz, Hertz, Hertz, u8, u8, Hertz){
343+
pub fn freeze(&self, acr: &mut ACR) -> Clocks {
347344

348345
let pllconf = if self.pllcfg.is_none() {
349346
let plln = (2 * self.sysclk.unwrap_or(HSI)) / HSI;
@@ -507,64 +504,33 @@ impl CFGR {
507504
while rcc.cr.read().msirdy().bit_is_clear() {}
508505
}
509506

510-
(Hertz(hclk), Hertz(pclk1), Hertz(pclk2), ppre1, ppre2, Hertz(sysclk))
511-
}
512-
513-
514-
#[cfg(not(feature = "stm32l47x"))]
515-
pub fn freeze(self, acr: &mut ACR) -> Clocks {
516-
517-
let (hclk, pclk1, pclk2, ppre1, ppre2, sysclk) = self.common_freeze(acr);
518-
let mut usb_rng = false;
519-
520-
let rcc = unsafe { &*RCC::ptr() };
521-
// Turn on USB, RNG Clock using the HSI48CLK source (default)
522-
if !cfg!(feature = "stm32l47x") && self.hsi48 {
523-
// p. 180 in ref-manual
524-
rcc.crrcr.modify(|_, w| w.hsi48on().set_bit());
525-
// Wait until HSI48 is running
526-
while rcc.crrcr.read().hsi48rdy().bit_is_clear() {}
527-
usb_rng = true;
528-
}
529-
530-
Clocks {
531-
hclk,
532-
lsi: self.lsi,
533-
hsi48: self.hsi48,
534-
usb_rng,
535-
msi: self.msi,
536-
pclk1,
537-
pclk2,
538-
ppre1,
539-
ppre2,
540-
sysclk,
507+
#[cfg(not(feature = "stm32l4x6"))]
508+
{
509+
// Turn on USB, RNG Clock using the HSI48 CLK source (default)
510+
if self.hsi48 {
511+
// p. 180 in ref-manual
512+
rcc.crrcr.modify(|_, w| w.hsi48on().set_bit());
513+
// Wait until HSI48 is running
514+
while rcc.crrcr.read().hsi48rdy().bit_is_clear() {}
515+
}
541516
}
542-
}
543-
544-
#[cfg(feature = "stm32l47x")]
545-
pub fn freeze(self, acr: &mut ACR) -> Clocks {
546517

547-
let (hclk, pclk1, pclk2, ppre1, ppre2, sysclk) = self.common_freeze(acr);
548-
549-
let mut usb_rng = false;
550-
551-
let rcc = unsafe { &*RCC::ptr() };
552518
// Select MSI as clock source for usb48, rng ...
553519
if let Some(MsiFreq::RANGE48M) = self.msi {
554-
unsafe { rcc.ccipr.modify(|_, w| w.clk48sel().bits(0b11)) };
555-
usb_rng = true;
520+
unsafe { rcc.ccipr.modify(|_, w| w.clk48sel().bits(MsiFreq::RANGE48M as u8)) };
556521
}
522+
//TODO proper clk48sel and other selects
557523

558524
Clocks {
559-
hclk,
525+
hclk: Hertz(hclk),
560526
lsi: self.lsi,
561-
usb_rng,
562527
msi: self.msi,
563-
pclk1,
564-
pclk2,
565-
ppre1,
566-
ppre2,
567-
sysclk,
528+
hsi48: self.hsi48,
529+
pclk1: Hertz(pclk1),
530+
pclk2: Hertz(pclk2),
531+
ppre1: ppre1,
532+
ppre2: ppre2,
533+
sysclk: Hertz(sysclk),
568534
}
569535
}
570536

@@ -587,9 +553,7 @@ pub struct PllConfig {
587553
#[derive(Clone, Copy, Debug)]
588554
pub struct Clocks {
589555
hclk: Hertz,
590-
#[cfg(not(feature = "stm32l47x"))]
591556
hsi48: bool,
592-
usb_rng: bool,
593557
msi: Option<MsiFreq>,
594558
lsi: bool,
595559
pclk1: Hertz,
@@ -608,14 +572,13 @@ impl Clocks {
608572
}
609573

610574
/// Returns status of HSI48
611-
#[cfg(not(feature = "stm32l47x"))]
612575
pub fn hsi48(&self) -> bool {
613576
self.hsi48
614577
}
615578

616-
/// Returns if usb rng clock is available
617-
pub fn usb_rng(&self) -> bool {
618-
self.usb_rng
579+
// Returns the status of the MSI
580+
pub fn msi(&self) -> Option<MsiFreq> {
581+
self.msi
619582
}
620583

621584
/// Returns status of HSI48

src/rng.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ impl RngExt for RNG {
1919
// crrcr.crrcr().modify(|_, w| w.hsi48on().set_bit()); // p. 180 in ref-manual
2020
// ...this is now supposed to be done in RCC configuration before freezing
2121

22-
// hsi48 should be turned on previously
23-
// TODO: should we return a Result instead of asserting here?
24-
assert!(clocks.usb_rng());
22+
// hsi48 should be turned on previously or msi at 48mhz
23+
let enabled = {
24+
clocks.hsi48() ||
25+
match clocks.msi() {
26+
Some(msi) => msi == crate::rcc::MsiFreq::RANGE48M,
27+
None => false,
28+
}
29+
};
30+
assert!(enabled);
2531

2632
ahb2.enr().modify(|_, w| w.rngen().set_bit());
2733
// if we don't do this... we can be "too fast", and

0 commit comments

Comments
 (0)