The RCC (“Reset and Clock Control”) handles all aspects of the MCU’s system clocks and peripheral clock gating. Most MCU peripherals will not function unless their clock is enabled via these RCC functions.
| Function Name | Purpose | When to use |
|---|---|---|
RCC_DeInit(void) | Reset all RCC clock configuration registers to their default (power-on) state. | You want to fully reinitialise the chip’s clock system (rare in user code; usually in startup/system code). |
RCC_HSEConfig(uint32_t RCC_HSE) | Configures the external high speed oscillator. The possible values are:
| Selecting/initialising the external oscillator |
RCC_WaitForHSEStartUp(void) | Waits (with timeout) until the HSE oscillator is stable and ready to use. It returns:
| After enabling HSE, before switching to it as a clock source. |
RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue) | Fine-tunes the frequency of the internal HSI oscillator using a calibration value (factory pre-trimmed, but user-adjustable). | You need more accurate internal clock, e.g. for peripherals requiring precise baud rates. |
RCC_HSICmd(FunctionalState NewState) | Enable or disable the internal high-speed oscillator. | Power-down HSI if not needed, or enable HSI for use as a clock source or backup. |
RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t RCC_PLLMul) | Set the PLL source and multiplication factor. PLL allows you to run the core and peripherals at frequencies above the base oscillator.
| Setting high clock speeds (multiplying HSE/HSI to desired SYSCLK), usually during system clock initialisation. |
RCC_PLLCmd(FunctionalState NewState) | Enable or disable the PLL. | Enable after configuring; must be on before using PLL as a clock source. |
RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource) | Switch the main system (CPU) clock source:
| After confirming a clock source is stable, use this to switch the MCU’s clocks over. |
RCC_GetSYSCLKSource(void) | Read the current system clock source. Returns a hex (0x00: HSI, 0x04: HSE, 0x08: PLL). | Checking which source the MCU is currently using. |
RCC_HCLKConfig(uint32_t RCC_SYSCLK) | Sets the AHB (High-speed bus) clock divider. | Changing performance/power characteristics AND/OR configuring for high clock speeds to stay within peripheral limits |
RCC_PCLK1Config(uint32_t RCC_HCLK) && RCC_PCLK2Config(uint32_t RCC_HCLK) | Sets APB1 (Low-speed bus) and APB2 (High-speed) bus clocks as dividers of HCLK (e.g., peripherals like timers, USARTs). | When wanting to operate low-speed and high-speed peripheral blocks at optimal frequencies |
RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState) | Enable or disable RCC-related interrupts (for oscillator ready, PLL lock, etc). | If your firmware wants interrupts when clocks are ready or encounter a fault. |
RCC_USBCLKConfig(uint32_t RCC_USBCLKSource) | Selects/sets the clock source for the USB peripheral from the PLL, with different division ratios. | Configuring MCU for USB use (must supply 48 MHz to USB). |
RCC_ADCCLKConfig(uint32_t RCC_PCLK2) | Sets the ADC peripheral clock divider. | Before using ADC, ensure proper clock frequency (too high = inaccuracy, too low = slow). |
RCC_LSEConfig(uint8_t RCC_LSE) | Configures the external low speed oscillator (LSE, usually for RTC):
| Using RTC, calibrating, or switching clock sources. |
RCC_LSICmd(FunctionalState NewState) | Enable or disable internal low speed oscillator (LSI; mainly for the independent watchdog or RTC fallback). | Required for the independent watchdog AND/OR used as backup clock for RTC if LSE not present. |
RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource) | Choose clock source for the RTC (LSI, LSE, or HSE divided). | Configuring the real-time clock. It needs to be set before enabling RTC. |
RCC_RTCCLKCmd(FunctionalState NewState) | Enable or disable the RTC clock after you’ve selected the source. | Once the clock input has been selected (see above), use this to start the RTC. |
RCC_GetClocksFreq(RCC_ClocksTypeDef\* RCC_Clocks) | Reads the actual (run-time) frequencies for SYSCLK, HCLK, PCLK1, PCLK2, ADCCLK, and fills the provided struct. | Debugging or if you need to dynamically adjust timings in your code (e.g., baud rates, calibration routines, etc). |
RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState) | Enable or disable (gate) the clock for AHB peripherals (DMA, SRAM, CRC, USBFS, etc). | Before initialising a peripheral that lives on the AHB bus (e.g., use before DMA config: RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE)) |
RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState) | Enable/disable clocks for APB2 (High-speed) peripherals (GPIO’s, ADCs, SPI1, USART1, etc). | Before initialising or using these peripherals |
RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState) | Enable/disable clocks for APB1 (Low-speed) peripherals (Timers 2-5, SPI2, USART2/3/4, CAN, USB, etc). | Before initialising or using these peripherals |
RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState) && RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState) | Force or release reset for a peripheral attached to APB2 or APB1, respectively. | To forcibly reset a peripheral to power-on state before/after use. |
RCC_BackupResetCmd(FunctionalState NewState) | Resets the backup (RTC/backup registers) subsystem. | If you need to reconfigure backup domain or change RTC clock source. |
RCC_ClockSecuritySystemCmd(FunctionalState NewState) | Enable/disable Clock Security System (detects HSE failures). | To increase robustness in critical systems using HSE, e.g., for safety requirements. Not usually enabled by default. |
RCC_MCOConfig(uint8_t RCC_MCO) | Outputs a clock (system, HSI, HSE, PLL/2) to an external pin (MCO = Microcontroller Clock Output). | For debugging, testing, or providing a clock output to external devices. |
RCC_GetFlagStatus(uint8_t RCC_FLAG) | Checks if a specific RCC flag (oscillator stable, reset cause, etc) is currently active. | Polling for oscillator stability before switching clocks AND/OR diagnosing system reset causes |
RCC_ClearFlag(void) | Clears all RCC reset flags (pin, power-on, software, watchdog, low-power). | After reading reset cause or at system startup to reset system state. |
RCC_GetITStatus(uint8_t RCC_IT) | Check whether a given RCC interrupt has fired. | Used in interrupt handlers or polling code for oscillator/PLL ready flags, etc. |
RCC_ClearITPendingBit(uint8_t RCC_IT) | Clear a pending RCC interrupt. | In an interrupt handler, after handling the event. |
RCC_ADCCLKADJcmd(FunctionalState NewState) | Enable/disable duty cycle adjustment for the ADC clock. | Required in special ADC clocking scenarios (check your MCU’s reference manual). |
RCC_ETHDIVConfig(uint32_t RCC_ETHPRE_Div) | Set the divider for the Ethernet clock (ETHCLK). | If your design uses the Ethernet controller, set the proper clock frequency. |
RCC_USB5PRE_JUDGE(void) | Determine if this MCU supports dividing PLL clock by 5 for USB (present on some chip models). | During USB clock setup on D8/D8W series. |
Summary Table#
| Function | Purpose | Typical Use |
|---|---|---|
| …PeriphClockCmd | Enable/disable clock to peripheral | Always before using a peripheral |
| …Config | Choose clock source or divider for SYSCLK, peripherals | During system/clock init |
| …ResetCmd | Reset peripheral/backup domains | Peripheral reinit, RTC changes |
| …GetClocksFreq | Fill struct with current clock speeds | Baud rate, timebase, debugging |
| …ITConfig, …GetITStatus, …ClearITPendingBit | Interrupts for clock events | RTOS/advanced event handling |
| …GetFlagStatus, …ClearFlag | Oscillator ready and reset cause flags | Clock startup, reset diagnosis |
Most-used functions in most projects:
RCC_APB2PeriphClockCmd()/RCC_APB1PeriphClockCmd()— before using any peripheral.RCC_PLLConfig()/RCC_PLLCmd()— for clock/tree configuration.RCC_HSEConfig()/RCC_HSICmd()— selecting your clock sources.RCC_SYSCLKConfig()— switching to desired clock.RCC_GetClocksFreq()— timebase calculations in firmware (timers, UART).