How to configure a clock

Configuring the clock system is a fundamental step in STM32 microcontroller projects, as it determines the operating frequency and timing for the entire system. STM32CubeMX2 provides a graphical interface to configure clocks easily.

Here’s a detailed guide to identify and configure common clock scenarios:

Understanding STM32 Clock System Basics

  • STM32 MCUs have multiple clock sources: - Fixed source/variable source and PLL to reach the max system frequency. - HSI (internal RC oscillator), HSE (external crystal), LSI, LSE, PLL, etc.

  • The clock tree controls CPU clock, peripheral clocks, and bus clocks (AHB, APB1, APB2).

  • Proper clock configuration ensures correct setup by detecting peripheral frequencies that exceed the maximum limit and suggests automatic fixes.

Clock Configuration access

  • Open your project in STM32CubeMX2.

  • Go to the clock configuration tab to view the visual clock tree graph (similar to the reference manual).

  • Set values for clock elements such as dividers, multipliers, or sources to configure the clock.

  • Let STM32CubeMX2 validate the configuration and warn if frequencies are out of range.

  • Click the resolve clock issue button to get a solution for an error-free clock tree.

  • Revert to the default configuration if needed.

  • Enter a desired frequency; the system will find a valid configuration or notify if none exists.

Typical Clock Configuration Scenarios

Scenario A: Use Internal Oscillator (HSI) as a system clock

  • Use case: Simple applications without external crystal.

  • Steps:

    1. In STM32CubeMX2, go to the Clock Configuration tab.

    2. Select HSI as the clock source for the system clock (SYSCLK).

    3. Configure System Clock if a higher frequency is needed.

    4. Verify bus clocks (AHB, APB1, APB2) frequencies.

    5. CubeMX will show if the configuration is valid.

Scenario B: Configure Peripheral Clocks (e.g., USB, ADC, Timers)

  • Use case: Some peripherals need specific clock frequencies.

  • Steps:

    1. In clock configuration, check peripheral clocks.

    2. Configure dedicated dividers if needed.

    3. Ensure that peripheral clock frequencies meet specifications.

Scenario C: Clock configuration with errors + resolve issue

  1. Introduce an error in the configuration.

  2. Click on Resolve Clock Issues.

Scenario D: Clock Reset to Default Value

  • If you want to return to the default configuration, click on the Reset Clock button.

Scenario E: Clock Table

  • You can use the table view to set your clock configuration.

Scenario F: lock/ unlock elements

  • You can lock or unlock clock element.

Scenario G: Enable/ disable clock elements

  • You can enable or disable clock elements in the RCC configuration tab. For example, enable HSE in the RCC configuration tab.

  • You can also enable or disable clock peripheral elements in their corresponding peripherals. For example, activate or deactivate DAC.

Tips and Best Practices

  • Always check the maximum frequency specifications of your MCU.

  • Use external crystal (HSE) for applications needing precise timing.

  • Configure low-power oscillators (LSI, LSE) for RTC and watchdog.

  • Review peripheral clock requirements (USB, ADC, etc.).

  • Use STM32CubeMX2 validation feature to avoid invalid configurations.

Additional articles