The STM32G4xx HAL does not provide a method to calculate the clock configuration - instead, the user is expected to manually provide a suitable PLL configuration and various prescalers. This library provides functions to automatically calculate a suitable clock configuration, similar to what is provided by STM32CubeMX. The user simply has to provide their frequency requirements, and the library loops through all possible configurations, selecting that which fulfills the requirements and deviates from the desired frequencies by as little as possible.
The library is expected to be used in build.rs
and provides methods to
generate code that can be included in projects using stm32g4xx-hal
.
use std::io::stdout;
use embedded_time::rate::*;
use stm32g4xx_clkcfg::{ClockRequirements, Frequencies};
fn main() {
println!("Configuration for the highest possible sysclk with a 48MHz USB clock from a 24 MHz crystal:");
let req = ClockRequirements::new()
.hse(24_000_000.Hz())
.sysclk(170_000_000.Hz())
.usb_rng_clk(true);
match req.resolve(false) {
Ok((cfg, freqs)) => {
cfg.write_code(&mut stdout()).unwrap();
// _freqs holds the frequencies generated by the configuration.
}
Err(e) => {
println!("Error: {:?}", e);
}
}
}
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Substantial code has been taken from src/rcc/config.rs
of stm32f4xx-hal
.