Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pll enable & rcc_config_usb test #368

Merged
merged 2 commits into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ jobs:
with:
command: check
args: --features=${{ matrix.mcu }},rt --examples
- uses: actions-rs/cargo@v1
with:
command: test
args: --features=${{ matrix.mcu }} --target x86_64-unknown-linux-gnu --lib
52 changes: 51 additions & 1 deletion src/rcc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ const HSI: u32 = 8_000_000; // Hz
///
/// **NOTE**: Currently, it is not guaranteed that the exact frequencies selected will be
/// used, only frequencies close to it.
#[derive(Debug, Default, PartialEq)]
pub struct CFGR {
hse: Option<u32>,
hclk: Option<u32>,
Expand Down Expand Up @@ -228,6 +229,19 @@ impl CFGR {
while rcc.cr.read().hserdy().bit_is_clear() {}
}

if let Some(pllmul_bits) = cfg.pllmul {
// enable PLL and wait for it to be ready

#[allow(unused_unsafe)]
rcc.cfgr.modify(|_, w| unsafe {
w.pllmul().bits(pllmul_bits).pllsrc().bit(cfg.hse.is_some())
});

rcc.cr.modify(|_, w| w.pllon().set_bit());

while rcc.cr.read().pllrdy().bit_is_clear() {}
}

// set prescalers and clock source
#[cfg(feature = "connectivity")]
rcc.cfgr.modify(|_, w| unsafe {
Expand Down Expand Up @@ -335,7 +349,7 @@ impl BKP {
///
/// let clocks = rcc.cfgr.freeze(&mut flash.acr);
/// ```
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Clocks {
hclk: Hertz,
pclk1: Hertz,
Expand Down Expand Up @@ -702,3 +716,39 @@ impl Config {
}
}
}

#[test]
fn rcc_config_usb() {
use crate::time::U32Ext;
let cfgr = CFGR::default()
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz());

let config = Config::from_cfgr(cfgr);
let config_expected = Config {
hse: Some(8_000_000),
pllmul: Some(4),
hpre: HPre::DIV1,
ppre1: PPre::DIV2,
ppre2: PPre::DIV1,
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
usbpre: UsbPre::DIV1,
adcpre: AdcPre::DIV8,
};
assert_eq!(config, config_expected);

let clocks = config.get_clocks();
let clocks_expected = Clocks {
hclk: 48.mhz().into(),
pclk1: 24.mhz().into(),
pclk2: 48.mhz().into(),
ppre1: 2,
ppre2: 1,
sysclk: 48.mhz().into(),
adcclk: 6.mhz().into(),
#[cfg(any(feature = "stm32f103", feature = "connectivity"))]
usbclk_valid: true,
};
assert_eq!(clocks, clocks_expected);
}