Skip to content

Commit f101160

Browse files
authored
Merge pull request #368 from stm32-rs/test_rcc
pll enable & rcc_config_usb test
2 parents 6675fe2 + 66bf45c commit f101160

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ jobs:
3535
with:
3636
command: check
3737
args: --features=${{ matrix.mcu }},rt --examples
38+
- uses: actions-rs/cargo@v1
39+
with:
40+
command: test
41+
args: --features=${{ matrix.mcu }} --target x86_64-unknown-linux-gnu --lib

src/rcc.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const HSI: u32 = 8_000_000; // Hz
105105
///
106106
/// **NOTE**: Currently, it is not guaranteed that the exact frequencies selected will be
107107
/// used, only frequencies close to it.
108+
#[derive(Debug, Default, PartialEq)]
108109
pub struct CFGR {
109110
hse: Option<u32>,
110111
hclk: Option<u32>,
@@ -228,6 +229,19 @@ impl CFGR {
228229
while rcc.cr.read().hserdy().bit_is_clear() {}
229230
}
230231

232+
if let Some(pllmul_bits) = cfg.pllmul {
233+
// enable PLL and wait for it to be ready
234+
235+
#[allow(unused_unsafe)]
236+
rcc.cfgr.modify(|_, w| unsafe {
237+
w.pllmul().bits(pllmul_bits).pllsrc().bit(cfg.hse.is_some())
238+
});
239+
240+
rcc.cr.modify(|_, w| w.pllon().set_bit());
241+
242+
while rcc.cr.read().pllrdy().bit_is_clear() {}
243+
}
244+
231245
// set prescalers and clock source
232246
#[cfg(feature = "connectivity")]
233247
rcc.cfgr.modify(|_, w| unsafe {
@@ -335,7 +349,7 @@ impl BKP {
335349
///
336350
/// let clocks = rcc.cfgr.freeze(&mut flash.acr);
337351
/// ```
338-
#[derive(Clone, Copy)]
352+
#[derive(Clone, Copy, Debug, PartialEq)]
339353
pub struct Clocks {
340354
hclk: Hertz,
341355
pclk1: Hertz,
@@ -702,3 +716,39 @@ impl Config {
702716
}
703717
}
704718
}
719+
720+
#[test]
721+
fn rcc_config_usb() {
722+
use crate::time::U32Ext;
723+
let cfgr = CFGR::default()
724+
.use_hse(8.mhz())
725+
.sysclk(48.mhz())
726+
.pclk1(24.mhz());
727+
728+
let config = Config::from_cfgr(cfgr);
729+
let config_expected = Config {
730+
hse: Some(8_000_000),
731+
pllmul: Some(4),
732+
hpre: HPre::DIV1,
733+
ppre1: PPre::DIV2,
734+
ppre2: PPre::DIV1,
735+
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
736+
usbpre: UsbPre::DIV1,
737+
adcpre: AdcPre::DIV8,
738+
};
739+
assert_eq!(config, config_expected);
740+
741+
let clocks = config.get_clocks();
742+
let clocks_expected = Clocks {
743+
hclk: 48.mhz().into(),
744+
pclk1: 24.mhz().into(),
745+
pclk2: 48.mhz().into(),
746+
ppre1: 2,
747+
ppre2: 1,
748+
sysclk: 48.mhz().into(),
749+
adcclk: 6.mhz().into(),
750+
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
751+
usbclk_valid: true,
752+
};
753+
assert_eq!(clocks, clocks_expected);
754+
}

0 commit comments

Comments
 (0)