stm32/rcc: refactor f7.
This commit is contained in:
parent
361fde35cf
commit
67010d123c
@ -1,5 +1,7 @@
|
|||||||
use crate::pac::pwr::vals::Vos;
|
pub use crate::pac::rcc::vals::{
|
||||||
use crate::pac::rcc::vals::{Hpre, Pllm, Plln, Pllp, Pllq, Pllsrc, Ppre, Sw};
|
Hpre as AHBPrescaler, Pllm as PllPreDiv, Plln as PllMul, Pllp, Pllq, Pllr, Pllsrc as PllSource,
|
||||||
|
Ppre as APBPrescaler, Sw as Sysclk,
|
||||||
|
};
|
||||||
use crate::pac::{FLASH, PWR, RCC};
|
use crate::pac::{FLASH, PWR, RCC};
|
||||||
use crate::rcc::{set_freqs, Clocks};
|
use crate::rcc::{set_freqs, Clocks};
|
||||||
use crate::time::Hertz;
|
use crate::time::Hertz;
|
||||||
@ -7,299 +9,304 @@ use crate::time::Hertz;
|
|||||||
/// HSI speed
|
/// HSI speed
|
||||||
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
pub const HSI_FREQ: Hertz = Hertz(16_000_000);
|
||||||
|
|
||||||
/// Clocks configuration
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||||
#[non_exhaustive]
|
pub enum HseMode {
|
||||||
#[derive(Default)]
|
/// crystal/ceramic oscillator (HSEBYP=0)
|
||||||
pub struct Config {
|
Oscillator,
|
||||||
pub hse: Option<Hertz>,
|
/// external analog clock (low swing) (HSEBYP=1)
|
||||||
pub bypass_hse: bool,
|
Bypass,
|
||||||
pub hclk: Option<Hertz>,
|
}
|
||||||
pub sys_ck: Option<Hertz>,
|
|
||||||
pub pclk1: Option<Hertz>,
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
||||||
pub pclk2: Option<Hertz>,
|
pub struct Hse {
|
||||||
|
/// HSE frequency.
|
||||||
|
pub freq: Hertz,
|
||||||
|
/// HSE mode.
|
||||||
|
pub mode: HseMode,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub struct Pll {
|
||||||
|
/// PLL pre-divider (DIVM).
|
||||||
|
pub prediv: PllPreDiv,
|
||||||
|
|
||||||
|
/// PLL multiplication factor.
|
||||||
|
pub mul: PllMul,
|
||||||
|
|
||||||
|
/// PLL P division factor. If None, PLL P output is disabled.
|
||||||
|
pub divp: Option<Pllp>,
|
||||||
|
/// PLL Q division factor. If None, PLL Q output is disabled.
|
||||||
|
pub divq: Option<Pllq>,
|
||||||
|
/// PLL R division factor. If None, PLL R output is disabled.
|
||||||
|
pub divr: Option<Pllr>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configuration of the core clocks
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub struct Config {
|
||||||
|
pub hsi: bool,
|
||||||
|
pub hse: Option<Hse>,
|
||||||
|
pub sys: Sysclk,
|
||||||
|
|
||||||
|
pub pll_src: PllSource,
|
||||||
|
|
||||||
|
pub pll: Option<Pll>,
|
||||||
|
pub plli2s: Option<Pll>,
|
||||||
|
pub pllsai: Option<Pll>,
|
||||||
|
|
||||||
|
pub ahb_pre: AHBPrescaler,
|
||||||
|
pub apb1_pre: APBPrescaler,
|
||||||
|
pub apb2_pre: APBPrescaler,
|
||||||
|
|
||||||
pub pll48: bool,
|
|
||||||
pub ls: super::LsConfig,
|
pub ls: super::LsConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup_pll(pllsrcclk: u32, use_hse: bool, pllsysclk: Option<u32>, pll48clk: bool) -> PllResults {
|
impl Default for Config {
|
||||||
let sysclk = pllsysclk.unwrap_or(pllsrcclk);
|
fn default() -> Self {
|
||||||
if pllsysclk.is_none() && !pll48clk {
|
Self {
|
||||||
RCC.pllcfgr().modify(|w| w.set_pllsrc(Pllsrc::from_bits(use_hse as u8)));
|
hsi: true,
|
||||||
|
hse: None,
|
||||||
|
sys: Sysclk::HSI,
|
||||||
|
pll_src: PllSource::HSI,
|
||||||
|
pll: None,
|
||||||
|
plli2s: None,
|
||||||
|
pllsai: None,
|
||||||
|
|
||||||
return PllResults {
|
ahb_pre: AHBPrescaler::DIV1,
|
||||||
use_pll: false,
|
apb1_pre: APBPrescaler::DIV1,
|
||||||
pllsysclk: None,
|
apb2_pre: APBPrescaler::DIV1,
|
||||||
pll48clk: None,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
// Input divisor from PLL source clock, must result to frequency in
|
|
||||||
// the range from 1 to 2 MHz
|
|
||||||
let pllm_min = (pllsrcclk + 1_999_999) / 2_000_000;
|
|
||||||
let pllm_max = pllsrcclk / 1_000_000;
|
|
||||||
|
|
||||||
// Sysclk output divisor must be one of 2, 4, 6 or 8
|
ls: Default::default(),
|
||||||
let sysclk_div = core::cmp::min(8, (432_000_000 / sysclk) & !1);
|
|
||||||
|
|
||||||
let target_freq = if pll48clk { 48_000_000 } else { sysclk * sysclk_div };
|
|
||||||
|
|
||||||
// Find the lowest pllm value that minimize the difference between
|
|
||||||
// target frequency and the real vco_out frequency.
|
|
||||||
let pllm = unwrap!((pllm_min..=pllm_max).min_by_key(|pllm| {
|
|
||||||
let vco_in = pllsrcclk / pllm;
|
|
||||||
let plln = target_freq / vco_in;
|
|
||||||
target_freq - vco_in * plln
|
|
||||||
}));
|
|
||||||
|
|
||||||
let vco_in = pllsrcclk / pllm;
|
|
||||||
assert!((1_000_000..=2_000_000).contains(&vco_in));
|
|
||||||
|
|
||||||
// Main scaler, must result in >= 100MHz (>= 192MHz for F401)
|
|
||||||
// and <= 432MHz, min 50, max 432
|
|
||||||
let plln = if pll48clk {
|
|
||||||
// try the different valid pllq according to the valid
|
|
||||||
// main scaller values, and take the best
|
|
||||||
let pllq = unwrap!((4..=9).min_by_key(|pllq| {
|
|
||||||
let plln = 48_000_000 * pllq / vco_in;
|
|
||||||
let pll48_diff = 48_000_000 - vco_in * plln / pllq;
|
|
||||||
let sysclk_diff = (sysclk as i32 - (vco_in * plln / sysclk_div) as i32).abs();
|
|
||||||
(pll48_diff, sysclk_diff)
|
|
||||||
}));
|
|
||||||
48_000_000 * pllq / vco_in
|
|
||||||
} else {
|
|
||||||
sysclk * sysclk_div / vco_in
|
|
||||||
};
|
|
||||||
|
|
||||||
let pllp = (sysclk_div / 2) - 1;
|
|
||||||
|
|
||||||
let pllq = (vco_in * plln + 47_999_999) / 48_000_000;
|
|
||||||
let real_pll48clk = vco_in * plln / pllq;
|
|
||||||
|
|
||||||
RCC.pllcfgr().modify(|w| {
|
|
||||||
w.set_pllm(Pllm::from_bits(pllm as u8));
|
|
||||||
w.set_plln(Plln::from_bits(plln as u16));
|
|
||||||
w.set_pllp(Pllp::from_bits(pllp as u8));
|
|
||||||
w.set_pllq(Pllq::from_bits(pllq as u8));
|
|
||||||
w.set_pllsrc(Pllsrc::from_bits(use_hse as u8));
|
|
||||||
});
|
|
||||||
|
|
||||||
let real_pllsysclk = vco_in * plln / sysclk_div;
|
|
||||||
|
|
||||||
PllResults {
|
|
||||||
use_pll: true,
|
|
||||||
pllsysclk: Some(real_pllsysclk),
|
|
||||||
pll48clk: if pll48clk { Some(real_pll48clk) } else { None },
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flash_setup(sysclk: u32) {
|
|
||||||
use crate::pac::flash::vals::Latency;
|
|
||||||
|
|
||||||
// Be conservative with voltage ranges
|
|
||||||
const FLASH_LATENCY_STEP: u32 = 30_000_000;
|
|
||||||
|
|
||||||
critical_section::with(|_| {
|
|
||||||
FLASH
|
|
||||||
.acr()
|
|
||||||
.modify(|w| w.set_latency(Latency::from_bits(((sysclk - 1) / FLASH_LATENCY_STEP) as u8)));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) unsafe fn init(config: Config) {
|
pub(crate) unsafe fn init(config: Config) {
|
||||||
if let Some(hse) = config.hse {
|
// always enable overdrive for now. Make it configurable in the future.
|
||||||
if config.bypass_hse {
|
|
||||||
assert!((max::HSE_BYPASS_MIN..=max::HSE_BYPASS_MAX).contains(&hse.0));
|
|
||||||
} else {
|
|
||||||
assert!((max::HSE_OSC_MIN..=max::HSE_OSC_MAX).contains(&hse.0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let pllsrcclk = config.hse.map(|hse| hse.0).unwrap_or(HSI_FREQ.0);
|
|
||||||
let sysclk = config.sys_ck.map(|sys| sys.0).unwrap_or(pllsrcclk);
|
|
||||||
let sysclk_on_pll = sysclk != pllsrcclk;
|
|
||||||
|
|
||||||
assert!((max::SYSCLK_MIN..=max::SYSCLK_MAX).contains(&sysclk));
|
|
||||||
|
|
||||||
let plls = setup_pll(
|
|
||||||
pllsrcclk,
|
|
||||||
config.hse.is_some(),
|
|
||||||
if sysclk_on_pll { Some(sysclk) } else { None },
|
|
||||||
config.pll48,
|
|
||||||
);
|
|
||||||
|
|
||||||
if config.pll48 {
|
|
||||||
let freq = unwrap!(plls.pll48clk);
|
|
||||||
|
|
||||||
assert!((max::PLL_48_CLK as i32 - freq as i32).abs() <= max::PLL_48_TOLERANCE as i32);
|
|
||||||
}
|
|
||||||
|
|
||||||
let sysclk = if sysclk_on_pll { unwrap!(plls.pllsysclk) } else { sysclk };
|
|
||||||
|
|
||||||
// AHB prescaler
|
|
||||||
let hclk = config.hclk.map(|h| h.0).unwrap_or(sysclk);
|
|
||||||
let (hpre_bits, hpre_div) = match (sysclk + hclk - 1) / hclk {
|
|
||||||
0 => unreachable!(),
|
|
||||||
1 => (Hpre::DIV1, 1),
|
|
||||||
2 => (Hpre::DIV2, 2),
|
|
||||||
3..=5 => (Hpre::DIV4, 4),
|
|
||||||
6..=11 => (Hpre::DIV8, 8),
|
|
||||||
12..=39 => (Hpre::DIV16, 16),
|
|
||||||
40..=95 => (Hpre::DIV64, 64),
|
|
||||||
96..=191 => (Hpre::DIV128, 128),
|
|
||||||
192..=383 => (Hpre::DIV256, 256),
|
|
||||||
_ => (Hpre::DIV512, 512),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Calculate real AHB clock
|
|
||||||
let hclk = sysclk / hpre_div;
|
|
||||||
|
|
||||||
assert!(hclk <= max::HCLK_MAX);
|
|
||||||
|
|
||||||
let pclk1 = config
|
|
||||||
.pclk1
|
|
||||||
.map(|p| p.0)
|
|
||||||
.unwrap_or_else(|| core::cmp::min(max::PCLK1_MAX, hclk));
|
|
||||||
|
|
||||||
let (ppre1_bits, ppre1) = match (hclk + pclk1 - 1) / pclk1 {
|
|
||||||
0 => unreachable!(),
|
|
||||||
1 => (0b000, 1),
|
|
||||||
2 => (0b100, 2),
|
|
||||||
3..=5 => (0b101, 4),
|
|
||||||
6..=11 => (0b110, 8),
|
|
||||||
_ => (0b111, 16),
|
|
||||||
};
|
|
||||||
let timer_mul1 = if ppre1 == 1 { 1 } else { 2 };
|
|
||||||
|
|
||||||
// Calculate real APB1 clock
|
|
||||||
let pclk1 = hclk / ppre1;
|
|
||||||
assert!((max::PCLK1_MIN..=max::PCLK1_MAX).contains(&pclk1));
|
|
||||||
|
|
||||||
let pclk2 = config
|
|
||||||
.pclk2
|
|
||||||
.map(|p| p.0)
|
|
||||||
.unwrap_or_else(|| core::cmp::min(max::PCLK2_MAX, hclk));
|
|
||||||
let (ppre2_bits, ppre2) = match (hclk + pclk2 - 1) / pclk2 {
|
|
||||||
0 => unreachable!(),
|
|
||||||
1 => (0b000, 1),
|
|
||||||
2 => (0b100, 2),
|
|
||||||
3..=5 => (0b101, 4),
|
|
||||||
6..=11 => (0b110, 8),
|
|
||||||
_ => (0b111, 16),
|
|
||||||
};
|
|
||||||
let timer_mul2 = if ppre2 == 1 { 1 } else { 2 };
|
|
||||||
|
|
||||||
// Calculate real APB2 clock
|
|
||||||
let pclk2 = hclk / ppre2;
|
|
||||||
assert!((max::PCLK2_MIN..=max::PCLK2_MAX).contains(&pclk2));
|
|
||||||
|
|
||||||
flash_setup(sysclk);
|
|
||||||
|
|
||||||
if config.hse.is_some() {
|
|
||||||
RCC.cr().modify(|w| {
|
|
||||||
w.set_hsebyp(config.bypass_hse);
|
|
||||||
w.set_hseon(true);
|
|
||||||
});
|
|
||||||
while !RCC.cr().read().hserdy() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if plls.use_pll {
|
|
||||||
RCC.cr().modify(|w| w.set_pllon(false));
|
|
||||||
|
|
||||||
// setup VOSScale
|
|
||||||
let vos_scale = if sysclk <= 144_000_000 {
|
|
||||||
3
|
|
||||||
} else if sysclk <= 168_000_000 {
|
|
||||||
2
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
};
|
|
||||||
PWR.cr1().modify(|w| {
|
|
||||||
w.set_vos(match vos_scale {
|
|
||||||
3 => Vos::SCALE3,
|
|
||||||
2 => Vos::SCALE2,
|
|
||||||
1 => Vos::SCALE1,
|
|
||||||
_ => panic!("Invalid VOS Scale."),
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
RCC.cr().modify(|w| w.set_pllon(true));
|
|
||||||
|
|
||||||
if hclk > max::HCLK_OVERDRIVE_FREQUENCY {
|
|
||||||
PWR.cr1().modify(|w| w.set_oden(true));
|
PWR.cr1().modify(|w| w.set_oden(true));
|
||||||
while !PWR.csr1().read().odrdy() {}
|
while !PWR.csr1().read().odrdy() {}
|
||||||
|
|
||||||
PWR.cr1().modify(|w| w.set_odswen(true));
|
PWR.cr1().modify(|w| w.set_odswen(true));
|
||||||
while !PWR.csr1().read().odswrdy() {}
|
while !PWR.csr1().read().odswrdy() {}
|
||||||
|
|
||||||
|
// Configure HSI
|
||||||
|
let hsi = match config.hsi {
|
||||||
|
false => {
|
||||||
|
RCC.cr().modify(|w| w.set_hsion(false));
|
||||||
|
None
|
||||||
|
}
|
||||||
|
true => {
|
||||||
|
RCC.cr().modify(|w| w.set_hsion(true));
|
||||||
|
while !RCC.cr().read().hsirdy() {}
|
||||||
|
Some(HSI_FREQ)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Configure HSE
|
||||||
|
let hse = match config.hse {
|
||||||
|
None => {
|
||||||
|
RCC.cr().modify(|w| w.set_hseon(false));
|
||||||
|
None
|
||||||
|
}
|
||||||
|
Some(hse) => {
|
||||||
|
match hse.mode {
|
||||||
|
HseMode::Bypass => assert!(max::HSE_BYP.contains(&hse.freq)),
|
||||||
|
HseMode::Oscillator => assert!(max::HSE_OSC.contains(&hse.freq)),
|
||||||
}
|
}
|
||||||
|
|
||||||
while !RCC.cr().read().pllrdy() {}
|
RCC.cr().modify(|w| w.set_hsebyp(hse.mode != HseMode::Oscillator));
|
||||||
|
RCC.cr().modify(|w| w.set_hseon(true));
|
||||||
|
while !RCC.cr().read().hserdy() {}
|
||||||
|
Some(hse.freq)
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
RCC.cfgr().modify(|w| {
|
// Configure PLLs.
|
||||||
w.set_ppre2(Ppre::from_bits(ppre2_bits));
|
let pll_input = PllInput {
|
||||||
w.set_ppre1(Ppre::from_bits(ppre1_bits));
|
hse,
|
||||||
w.set_hpre(hpre_bits);
|
hsi,
|
||||||
});
|
source: config.pll_src,
|
||||||
|
};
|
||||||
|
let pll = init_pll(PllInstance::Pll, config.pll, &pll_input);
|
||||||
|
let _plli2s = init_pll(PllInstance::Plli2s, config.plli2s, &pll_input);
|
||||||
|
let _pllsai = init_pll(PllInstance::Pllsai, config.pllsai, &pll_input);
|
||||||
|
|
||||||
// Wait for the new prescalers to kick in
|
// Configure sysclk
|
||||||
// "The clocks are divided with the new prescaler factor from 1 to 16 AHB cycles after write"
|
let sys = match config.sys {
|
||||||
cortex_m::asm::delay(16);
|
Sysclk::HSI => unwrap!(hsi),
|
||||||
|
Sysclk::HSE => unwrap!(hse),
|
||||||
|
Sysclk::PLL1_P => unwrap!(pll.p),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
RCC.cfgr().modify(|w| {
|
let hclk = sys / config.ahb_pre;
|
||||||
w.set_sw(if sysclk_on_pll {
|
let (pclk1, pclk1_tim) = calc_pclk(hclk, config.apb1_pre);
|
||||||
Sw::PLL1_P
|
let (pclk2, pclk2_tim) = calc_pclk(hclk, config.apb2_pre);
|
||||||
} else if config.hse.is_some() {
|
|
||||||
Sw::HSE
|
assert!(max::SYSCLK.contains(&sys));
|
||||||
} else {
|
assert!(max::HCLK.contains(&hclk));
|
||||||
Sw::HSI
|
assert!(max::PCLK1.contains(&pclk1));
|
||||||
})
|
assert!(max::PCLK2.contains(&pclk2));
|
||||||
});
|
|
||||||
|
|
||||||
let rtc = config.ls.init();
|
let rtc = config.ls.init();
|
||||||
|
|
||||||
|
flash_setup(hclk);
|
||||||
|
|
||||||
|
RCC.cfgr().modify(|w| {
|
||||||
|
w.set_sw(config.sys);
|
||||||
|
w.set_hpre(config.ahb_pre);
|
||||||
|
w.set_ppre1(config.apb1_pre);
|
||||||
|
w.set_ppre2(config.apb2_pre);
|
||||||
|
});
|
||||||
|
while RCC.cfgr().read().sws() != config.sys {}
|
||||||
|
|
||||||
set_freqs(Clocks {
|
set_freqs(Clocks {
|
||||||
sys: Hertz(sysclk),
|
sys,
|
||||||
pclk1: Hertz(pclk1),
|
hclk1: hclk,
|
||||||
pclk2: Hertz(pclk2),
|
hclk2: hclk,
|
||||||
|
hclk3: hclk,
|
||||||
pclk1_tim: Hertz(pclk1 * timer_mul1),
|
pclk1,
|
||||||
pclk2_tim: Hertz(pclk2 * timer_mul2),
|
pclk2,
|
||||||
|
pclk1_tim,
|
||||||
hclk1: Hertz(hclk),
|
pclk2_tim,
|
||||||
hclk2: Hertz(hclk),
|
|
||||||
hclk3: Hertz(hclk),
|
|
||||||
|
|
||||||
pll1_q: plls.pll48clk.map(Hertz),
|
|
||||||
|
|
||||||
rtc,
|
rtc,
|
||||||
|
pll1_q: pll.q,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PllResults {
|
struct PllInput {
|
||||||
use_pll: bool,
|
source: PllSource,
|
||||||
pllsysclk: Option<u32>,
|
hsi: Option<Hertz>,
|
||||||
pll48clk: Option<u32>,
|
hse: Option<Hertz>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
#[allow(unused)]
|
||||||
|
struct PllOutput {
|
||||||
|
p: Option<Hertz>,
|
||||||
|
q: Option<Hertz>,
|
||||||
|
r: Option<Hertz>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
||||||
|
enum PllInstance {
|
||||||
|
Pll,
|
||||||
|
Plli2s,
|
||||||
|
Pllsai,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pll_enable(instance: PllInstance, enabled: bool) {
|
||||||
|
match instance {
|
||||||
|
PllInstance::Pll => {
|
||||||
|
RCC.cr().modify(|w| w.set_pllon(enabled));
|
||||||
|
while RCC.cr().read().pllrdy() != enabled {}
|
||||||
|
}
|
||||||
|
PllInstance::Plli2s => {
|
||||||
|
RCC.cr().modify(|w| w.set_plli2son(enabled));
|
||||||
|
while RCC.cr().read().plli2srdy() != enabled {}
|
||||||
|
}
|
||||||
|
PllInstance::Pllsai => {
|
||||||
|
RCC.cr().modify(|w| w.set_pllsaion(enabled));
|
||||||
|
while RCC.cr().read().pllsairdy() != enabled {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_pll(instance: PllInstance, config: Option<Pll>, input: &PllInput) -> PllOutput {
|
||||||
|
// Disable PLL
|
||||||
|
pll_enable(instance, false);
|
||||||
|
|
||||||
|
let Some(pll) = config else { return PllOutput::default() };
|
||||||
|
|
||||||
|
let pll_src = match input.source {
|
||||||
|
PllSource::HSE => input.hse,
|
||||||
|
PllSource::HSI => input.hsi,
|
||||||
|
};
|
||||||
|
|
||||||
|
let pll_src = pll_src.unwrap();
|
||||||
|
|
||||||
|
let in_freq = pll_src / pll.prediv;
|
||||||
|
assert!(max::PLL_IN.contains(&in_freq));
|
||||||
|
let vco_freq = in_freq * pll.mul;
|
||||||
|
assert!(max::PLL_VCO.contains(&vco_freq));
|
||||||
|
|
||||||
|
let p = pll.divp.map(|div| vco_freq / div);
|
||||||
|
let q = pll.divq.map(|div| vco_freq / div);
|
||||||
|
let r = pll.divr.map(|div| vco_freq / div);
|
||||||
|
|
||||||
|
macro_rules! write_fields {
|
||||||
|
($w:ident) => {
|
||||||
|
$w.set_plln(pll.mul);
|
||||||
|
if let Some(divp) = pll.divp {
|
||||||
|
$w.set_pllp(divp);
|
||||||
|
}
|
||||||
|
if let Some(divq) = pll.divq {
|
||||||
|
$w.set_pllq(divq);
|
||||||
|
}
|
||||||
|
if let Some(divr) = pll.divr {
|
||||||
|
$w.set_pllr(divr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
match instance {
|
||||||
|
PllInstance::Pll => RCC.pllcfgr().write(|w| {
|
||||||
|
w.set_pllm(pll.prediv);
|
||||||
|
w.set_pllsrc(input.source);
|
||||||
|
write_fields!(w);
|
||||||
|
}),
|
||||||
|
PllInstance::Plli2s => RCC.plli2scfgr().write(|w| {
|
||||||
|
write_fields!(w);
|
||||||
|
}),
|
||||||
|
PllInstance::Pllsai => RCC.pllsaicfgr().write(|w| {
|
||||||
|
write_fields!(w);
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable PLL
|
||||||
|
pll_enable(instance, true);
|
||||||
|
|
||||||
|
PllOutput { p, q, r }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flash_setup(clk: Hertz) {
|
||||||
|
use crate::pac::flash::vals::Latency;
|
||||||
|
|
||||||
|
// Be conservative with voltage ranges
|
||||||
|
const FLASH_LATENCY_STEP: u32 = 30_000_000;
|
||||||
|
|
||||||
|
let latency = (clk.0 - 1) / FLASH_LATENCY_STEP;
|
||||||
|
debug!("flash: latency={}", latency);
|
||||||
|
|
||||||
|
let latency = Latency::from_bits(latency as u8);
|
||||||
|
FLASH.acr().write(|w| {
|
||||||
|
w.set_latency(latency);
|
||||||
|
});
|
||||||
|
while FLASH.acr().read().latency() != latency {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn calc_pclk<D>(hclk: Hertz, ppre: D) -> (Hertz, Hertz)
|
||||||
|
where
|
||||||
|
Hertz: core::ops::Div<D, Output = Hertz>,
|
||||||
|
{
|
||||||
|
let pclk = hclk / ppre;
|
||||||
|
let pclk_tim = if hclk == pclk { pclk } else { pclk * 2u32 };
|
||||||
|
(pclk, pclk_tim)
|
||||||
}
|
}
|
||||||
|
|
||||||
mod max {
|
mod max {
|
||||||
pub(crate) const HSE_OSC_MIN: u32 = 4_000_000;
|
use core::ops::RangeInclusive;
|
||||||
pub(crate) const HSE_OSC_MAX: u32 = 26_000_000;
|
|
||||||
pub(crate) const HSE_BYPASS_MIN: u32 = 1_000_000;
|
|
||||||
pub(crate) const HSE_BYPASS_MAX: u32 = 50_000_000;
|
|
||||||
|
|
||||||
pub(crate) const HCLK_MAX: u32 = 216_000_000;
|
use crate::time::Hertz;
|
||||||
pub(crate) const HCLK_OVERDRIVE_FREQUENCY: u32 = 180_000_000;
|
|
||||||
|
|
||||||
pub(crate) const SYSCLK_MIN: u32 = 12_500_000;
|
pub(crate) const HSE_OSC: RangeInclusive<Hertz> = Hertz(4_000_000)..=Hertz(26_000_000);
|
||||||
pub(crate) const SYSCLK_MAX: u32 = 216_000_000;
|
pub(crate) const HSE_BYP: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(50_000_000);
|
||||||
|
|
||||||
pub(crate) const PCLK1_MIN: u32 = SYSCLK_MIN;
|
pub(crate) const SYSCLK: RangeInclusive<Hertz> = Hertz(12_500_000)..=Hertz(216_000_000);
|
||||||
pub(crate) const PCLK1_MAX: u32 = SYSCLK_MAX / 4;
|
pub(crate) const HCLK: RangeInclusive<Hertz> = Hertz(12_500_000)..=Hertz(216_000_000);
|
||||||
|
pub(crate) const PCLK1: RangeInclusive<Hertz> = Hertz(12_500_000)..=Hertz(216_000_000 / 4);
|
||||||
|
pub(crate) const PCLK2: RangeInclusive<Hertz> = Hertz(12_500_000)..=Hertz(216_000_000 / 2);
|
||||||
|
|
||||||
pub(crate) const PCLK2_MIN: u32 = SYSCLK_MIN;
|
pub(crate) const PLL_IN: RangeInclusive<Hertz> = Hertz(1_000_000)..=Hertz(2_100_000);
|
||||||
pub(crate) const PCLK2_MAX: u32 = SYSCLK_MAX / 2;
|
pub(crate) const PLL_VCO: RangeInclusive<Hertz> = Hertz(100_000_000)..=Hertz(432_000_000);
|
||||||
|
|
||||||
// USB specification allows +-0.25%
|
|
||||||
pub(crate) const PLL_48_CLK: u32 = 48_000_000;
|
|
||||||
pub(crate) const PLL_48_TOLERANCE: u32 = 120_000;
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ use embassy_stm32::eth::generic_smi::GenericSMI;
|
|||||||
use embassy_stm32::eth::{Ethernet, PacketQueue};
|
use embassy_stm32::eth::{Ethernet, PacketQueue};
|
||||||
use embassy_stm32::peripherals::ETH;
|
use embassy_stm32::peripherals::ETH;
|
||||||
use embassy_stm32::rng::Rng;
|
use embassy_stm32::rng::Rng;
|
||||||
use embassy_stm32::time::mhz;
|
use embassy_stm32::time::Hertz;
|
||||||
use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
|
use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
|
||||||
use embassy_time::Timer;
|
use embassy_time::Timer;
|
||||||
use embedded_io_async::Write;
|
use embedded_io_async::Write;
|
||||||
@ -33,7 +33,25 @@ async fn net_task(stack: &'static Stack<Device>) -> ! {
|
|||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(spawner: Spawner) -> ! {
|
async fn main(spawner: Spawner) -> ! {
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
config.rcc.sys_ck = Some(mhz(200));
|
{
|
||||||
|
use embassy_stm32::rcc::*;
|
||||||
|
config.rcc.hse = Some(Hse {
|
||||||
|
freq: Hertz(8_000_000),
|
||||||
|
mode: HseMode::Bypass,
|
||||||
|
});
|
||||||
|
config.rcc.pll_src = PllSource::HSE;
|
||||||
|
config.rcc.pll = Some(Pll {
|
||||||
|
prediv: PllPreDiv::DIV4,
|
||||||
|
mul: PllMul::MUL216,
|
||||||
|
divp: Some(Pllp::DIV2), // 8mhz / 4 * 216 / 2 = 216Mhz
|
||||||
|
divq: None,
|
||||||
|
divr: None,
|
||||||
|
});
|
||||||
|
config.rcc.ahb_pre = AHBPrescaler::DIV1;
|
||||||
|
config.rcc.apb1_pre = APBPrescaler::DIV4;
|
||||||
|
config.rcc.apb2_pre = APBPrescaler::DIV2;
|
||||||
|
config.rcc.sys = Sysclk::PLL1_P;
|
||||||
|
}
|
||||||
let p = embassy_stm32::init(config);
|
let p = embassy_stm32::init(config);
|
||||||
|
|
||||||
info!("Hello World!");
|
info!("Hello World!");
|
||||||
|
@ -4,15 +4,13 @@
|
|||||||
|
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::time::Hertz;
|
|
||||||
use embassy_stm32::Config;
|
use embassy_stm32::Config;
|
||||||
use embassy_time::Timer;
|
use embassy_time::Timer;
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(_spawner: Spawner) -> ! {
|
async fn main(_spawner: Spawner) -> ! {
|
||||||
let mut config = Config::default();
|
let config = Config::default();
|
||||||
config.rcc.sys_ck = Some(Hertz(84_000_000));
|
|
||||||
let _p = embassy_stm32::init(config);
|
let _p = embassy_stm32::init(config);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::sdmmc::Sdmmc;
|
use embassy_stm32::sdmmc::Sdmmc;
|
||||||
use embassy_stm32::time::mhz;
|
use embassy_stm32::time::{mhz, Hertz};
|
||||||
use embassy_stm32::{bind_interrupts, peripherals, sdmmc, Config};
|
use embassy_stm32::{bind_interrupts, peripherals, sdmmc, Config};
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
@ -16,8 +16,25 @@ bind_interrupts!(struct Irqs {
|
|||||||
#[embassy_executor::main]
|
#[embassy_executor::main]
|
||||||
async fn main(_spawner: Spawner) {
|
async fn main(_spawner: Spawner) {
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
config.rcc.sys_ck = Some(mhz(200));
|
{
|
||||||
config.rcc.pll48 = true;
|
use embassy_stm32::rcc::*;
|
||||||
|
config.rcc.hse = Some(Hse {
|
||||||
|
freq: Hertz(8_000_000),
|
||||||
|
mode: HseMode::Bypass,
|
||||||
|
});
|
||||||
|
config.rcc.pll_src = PllSource::HSE;
|
||||||
|
config.rcc.pll = Some(Pll {
|
||||||
|
prediv: PllPreDiv::DIV4,
|
||||||
|
mul: PllMul::MUL216,
|
||||||
|
divp: Some(Pllp::DIV2), // 8mhz / 4 * 216 / 2 = 216Mhz
|
||||||
|
divq: Some(Pllq::DIV9), // 8mhz / 4 * 216 / 9 = 48Mhz
|
||||||
|
divr: None,
|
||||||
|
});
|
||||||
|
config.rcc.ahb_pre = AHBPrescaler::DIV1;
|
||||||
|
config.rcc.apb1_pre = APBPrescaler::DIV4;
|
||||||
|
config.rcc.apb2_pre = APBPrescaler::DIV2;
|
||||||
|
config.rcc.sys = Sysclk::PLL1_P;
|
||||||
|
}
|
||||||
let p = embassy_stm32::init(config);
|
let p = embassy_stm32::init(config);
|
||||||
|
|
||||||
info!("Hello World!");
|
info!("Hello World!");
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
use defmt::{panic, *};
|
use defmt::{panic, *};
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_stm32::time::mhz;
|
use embassy_stm32::time::Hertz;
|
||||||
use embassy_stm32::usb_otg::{Driver, Instance};
|
use embassy_stm32::usb_otg::{Driver, Instance};
|
||||||
use embassy_stm32::{bind_interrupts, peripherals, usb_otg, Config};
|
use embassy_stm32::{bind_interrupts, peripherals, usb_otg, Config};
|
||||||
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
|
use embassy_usb::class::cdc_acm::{CdcAcmClass, State};
|
||||||
@ -22,10 +22,25 @@ async fn main(_spawner: Spawner) {
|
|||||||
info!("Hello World!");
|
info!("Hello World!");
|
||||||
|
|
||||||
let mut config = Config::default();
|
let mut config = Config::default();
|
||||||
config.rcc.hse = Some(mhz(8));
|
{
|
||||||
config.rcc.pll48 = true;
|
use embassy_stm32::rcc::*;
|
||||||
config.rcc.sys_ck = Some(mhz(200));
|
config.rcc.hse = Some(Hse {
|
||||||
|
freq: Hertz(8_000_000),
|
||||||
|
mode: HseMode::Bypass,
|
||||||
|
});
|
||||||
|
config.rcc.pll_src = PllSource::HSE;
|
||||||
|
config.rcc.pll = Some(Pll {
|
||||||
|
prediv: PllPreDiv::DIV4,
|
||||||
|
mul: PllMul::MUL216,
|
||||||
|
divp: Some(Pllp::DIV2), // 8mhz / 4 * 216 / 2 = 216Mhz
|
||||||
|
divq: Some(Pllq::DIV9), // 8mhz / 4 * 216 / 9 = 48Mhz
|
||||||
|
divr: None,
|
||||||
|
});
|
||||||
|
config.rcc.ahb_pre = AHBPrescaler::DIV1;
|
||||||
|
config.rcc.apb1_pre = APBPrescaler::DIV4;
|
||||||
|
config.rcc.apb2_pre = APBPrescaler::DIV2;
|
||||||
|
config.rcc.sys = Sysclk::PLL1_P;
|
||||||
|
}
|
||||||
let p = embassy_stm32::init(config);
|
let p = embassy_stm32::init(config);
|
||||||
|
|
||||||
// Create the driver, from the HAL.
|
// Create the driver, from the HAL.
|
||||||
|
@ -233,7 +233,23 @@ pub fn config() -> Config {
|
|||||||
|
|
||||||
#[cfg(feature = "stm32f767zi")]
|
#[cfg(feature = "stm32f767zi")]
|
||||||
{
|
{
|
||||||
config.rcc.sys_ck = Some(Hertz(200_000_000));
|
use embassy_stm32::rcc::*;
|
||||||
|
config.rcc.hse = Some(Hse {
|
||||||
|
freq: Hertz(8_000_000),
|
||||||
|
mode: HseMode::Bypass,
|
||||||
|
});
|
||||||
|
config.rcc.pll_src = PllSource::HSE;
|
||||||
|
config.rcc.pll = Some(Pll {
|
||||||
|
prediv: PllPreDiv::DIV4,
|
||||||
|
mul: PllMul::MUL216,
|
||||||
|
divp: Some(Pllp::DIV2), // 8mhz / 4 * 216 / 2 = 216Mhz.
|
||||||
|
divq: None,
|
||||||
|
divr: None,
|
||||||
|
});
|
||||||
|
config.rcc.ahb_pre = AHBPrescaler::DIV1;
|
||||||
|
config.rcc.apb1_pre = APBPrescaler::DIV4;
|
||||||
|
config.rcc.apb2_pre = APBPrescaler::DIV2;
|
||||||
|
config.rcc.sys = Sysclk::PLL1_P;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "stm32h563zi")]
|
#[cfg(feature = "stm32h563zi")]
|
||||||
|
Loading…
Reference in New Issue
Block a user