XPD Clock Management - IntergatedCircuits/STM32_XPD GitHub Wiki

The STM32 device clocks management is a complex system with broad-ranging capabilities that are all controlled by the Reset and Clock Control (RCC) module. These functions can be separated to three distinct responsibilities, each of which are explained in a chapter below.

1 - System level clock sources control

The Core Clocks (CC) submodule of the XPD RCC drivers provides the necessary API to control the system oscillators and the system core clocks which are clocking the MCU core and the internal peripheral buses. Additionally, this submodule hosts the API for the following RCC-provided features:

  • Clock Security System (CSS)
  • Reset of all peripherals of an internal bus
  • Reading of system reset source(s)

A short walkthrough of the system clocks setup is presented here.

2 - Reset and clocking control of peripherals

The peripheral reset and clocking control API serves to reset and to enable or disable the clock of individual peripherals. An RCC_POS_{PERIPH} symbol is defined for each peripheral, which specifies the (clock) control bit position - relative to the first control bit. These symbols are bound to the peripheral handles by the {PERIPH}_INST2HANDLE() macro call, therefore the XPD API user does not have to bother with the clocking control. The peripheral's clock is always enabled within the {PERIPH}_vInit(), and disabled within the {PERIPH}_vDeinit() API call.

3 - Clock source configuration of selected peripherals

In order to achieve higher clock accuracy or frequency, certain peripherals have a multitude of clock sources available to select from. The Peripheral clock source (PC) submodules of these peripherals are implemented in the xpd_rcc_pc.c source files, as the configuration is done in the RCC module. However, the API definitions are included in each peripheral's respective namespaces (i.e. visible when xpd_{periph}.h is included), as each clock source configuration is only relevant for the given peripheral. The API typically consists of two functions:

  • {PERIPH}_vClockConfig() sets the selected clock source for the peripheral instance or type
  • {PERIPH}_ulClockFreq_Hz() calculates the peripheral's input clock frequency in Hz

4 - Clock outputs control

The Master Core Output (MCO) submodule of the XPD RCC drivers provides the necessary API to control the Master Clock Output pin. It is located in the xpd_mco.h header file.