Configuring the hardware

So far, we have not configured our hardware at all. The board is running with its initial setup on the 16MHz HSI clock, and only the debug interface is active. All other peripherals are still in their initial state.

Adding a PAC / a HAL

Several crates may be interesting to configure and access the hardware of the STM32L475VGT6 more easily:

  • cortex-m provides access to functionalities common to all Cortex-M based microcontrollers.
  • stm32-metapac is a peripheral access crate (PAC) for all STM32 microcontrollers. It provides very low-level access to the various peripherals of each microcontroller. It will be automatically depended on by the HAL (see below), you do not need to add it as an explicit dependency.
  • embassy-stm32 is a hardware abstraction layer (HAL) using both crates mentioned above to provide higher-level functionalities, this is the one we will be using.

❎ Add the following imports to your main.rs since we will use them:

use embassy_stm32::rcc::*;
use embassy_stm32::Config;

❎ Use the following code as your main() function, to configure the system clocks to run at 80MHz (the higher usable frequency) from the 16Mhz HSI clock, by multiplying it by 10 then dividing the result by 2 (using the PLL):

#[entry]
fn main() -> ! {
    defmt::info!("defmt correctly initialized");

    // Setup the clocks at 80MHz using HSI (by default since HSE/MSI
    // are not configured): HSI(16MHz)×10/2=80MHz. The flash wait
    // states will be configured accordingly.
    let mut config = Config::default();
    config.rcc.hsi = true;
    config.rcc.pll = Some(Pll {
        source: PllSource::HSI,
        prediv: PllPreDiv::DIV1,
        mul: PllMul::MUL10,
        divp: None,
        divq: None,
        divr: Some(PllRDiv::DIV2), // 16 * 10 / 2 = 80MHz
    });
    config.rcc.sys = Sysclk::PLL1_R;
    embassy_stm32::init(config);

    panic!("Everything configured");
}

Here you can see how the HAL offers high-level features. The whole clock tree can be configured through the Config structure. This will automatically setup the right registers, configure the flash memory wait states, etc.

Congratulations, you have displayed the same thing as before, but with a system running at 80MHz. 👏