Skip to content

Commit 4743501

Browse files
committed
Move PLL2/3 config to before PLL
1 parent 3ba94c0 commit 4743501

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

embassy-stm32/src/rcc/f013.rs

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,28 @@ pub(crate) unsafe fn init(config: Config) {
209209
#[cfg(not(crs))]
210210
let hsi48: Option<Hertz> = None;
211211

212+
// PLL2 and PLL3
213+
// Configure this before PLL since PLL2 can be the source for PLL.
214+
#[cfg(stm32f107)]
215+
{
216+
// Common prediv for PLL2 and PLL3
217+
RCC.cfgr2().modify(|w| w.set_prediv2(config.prediv2));
218+
219+
// Configure PLL2
220+
if let Some(pll2) = config.pll2 {
221+
RCC.cfgr2().modify(|w| w.set_pll2mul(pll2.mul));
222+
RCC.cr().modify(|w| w.set_pll2on(true));
223+
while !RCC.cr().read().pll2rdy() {}
224+
}
225+
226+
// Configure PLL3
227+
if let Some(pll3) = config.pll3 {
228+
RCC.cfgr2().modify(|w| w.set_pll3mul(pll3.mul));
229+
RCC.cr().modify(|w| w.set_pll3on(true));
230+
while !RCC.cr().read().pll3rdy() {}
231+
}
232+
}
233+
212234
// Enable PLL
213235
let pll = config.pll.map(|pll| {
214236
let (src_val, src_freq) = match pll.src {
@@ -221,17 +243,25 @@ pub(crate) unsafe fn init(config: Config) {
221243
}
222244
(Pllsrc::HSI_DIV2, unwrap!(hsi))
223245
}
224-
PllSource::HSE => (Pllsrc::HSE_DIV_PREDIV, unwrap!(hse)),
246+
PllSource::HSE => {
247+
#[cfg(stm32f107)]
248+
RCC.cfgr2().modify(|w| w.set_prediv1src(Prediv1src::HSE));
249+
250+
(Pllsrc::HSE_DIV_PREDIV, unwrap!(hse))
251+
}
225252
#[cfg(rcc_f0v4)]
226253
PllSource::HSI48 => (Pllsrc::HSI48_DIV_PREDIV, unwrap!(hsi48)),
227254
#[cfg(stm32f107)]
228255
PllSource::PLL2 => {
229256
if config.pll2.is_none() {
230257
panic!("if PLL source is PLL2, Config::pll2 must also be set.");
231258
}
259+
RCC.cfgr2().modify(|w| w.set_prediv1src(Prediv1src::PLL2));
260+
232261
let pll2 = unwrap!(config.pll2);
233262
let in_freq = hse.unwrap() / config.prediv2;
234263
let pll2freq = in_freq * pll2.mul;
264+
235265
(Pllsrc::HSE_DIV_PREDIV, pll2freq)
236266
}
237267
};
@@ -259,34 +289,6 @@ pub(crate) unsafe fn init(config: Config) {
259289
out_freq
260290
});
261291

262-
#[cfg(stm32f107)]
263-
match config.pll.map(|pll| pll.src) {
264-
Some(PllSource::HSE) => RCC.cfgr2().modify(|w| w.set_prediv1src(Prediv1src::HSE)),
265-
Some(PllSource::PLL2) => RCC.cfgr2().modify(|w| w.set_prediv1src(Prediv1src::PLL2)),
266-
_ => {}
267-
}
268-
269-
// pll2 and pll3
270-
#[cfg(stm32f107)]
271-
{
272-
// Common prediv for PLL2 and PLL3
273-
RCC.cfgr2().modify(|w| w.set_prediv2(config.prediv2));
274-
275-
// Configure PLL2
276-
if let Some(pll2) = config.pll2 {
277-
RCC.cfgr2().modify(|w| w.set_pll2mul(pll2.mul));
278-
RCC.cr().modify(|w| w.set_pll2on(true));
279-
while !RCC.cr().read().pll2rdy() {}
280-
}
281-
282-
// Configure PLL3
283-
if let Some(pll3) = config.pll3 {
284-
RCC.cfgr2().modify(|w| w.set_pll3mul(pll3.mul));
285-
RCC.cr().modify(|w| w.set_pll3on(true));
286-
while !RCC.cr().read().pll3rdy() {}
287-
}
288-
}
289-
290292
#[cfg(stm32f3)]
291293
let pll_mul_2 = pll.map(|pll| pll * 2u32);
292294

0 commit comments

Comments
 (0)