STM32 Clock Configuration.
At first, to simplify writing programs for STM32, I didn't go into the details of configuring the clock system; instead I selected the system bus frequency in the system_stm32f10x.c file by simply uncommenting the desired line.

But when it came to configuring the clock frequency of individual blocks (for example the ADC), I used CUBE.
The whole clock system is in front of your eyes, which is very convenient and lets you quickly figure out which coefficient is responsible for what.
The method of setting the system bus frequency described above only allows you to debug the program on real hardware, because when debugging in the simulator nobody is there to set the ready flag of HSE or PLL.
Why am I writing about some flags?
Because there is actually no magic: by uncommenting the define with the desired clock frequency, we tell the preprocessor that it should include a certain piece of code in the program. If you uncomment the line
#define SYSCLK_FREQ_56MHz 56000000
The preprocessor will include the following piece of code in the program
The second limitation of configuring the system frequency this way is that you can't overclock the controller, since overclocking is done by configuring the PLL, but so far there has been no need for that.

So, three different sources can be used to generate the system clock (SYSCLK). (We are considering SYSCLK generation because all the peripherals are clocked from it through prescalers.)
HSI is an internal RC oscillator with a frequency of 8MHz. It can serve as the system clock source, or be divided by two and fed to the PLL input. It should be noted that the RC oscillator frequency depends on temperature and can deviate from the nominal value by –1.9 to +1.3% as the microcontroller temperature changes from 0 to 70°C. When the supply voltages are applied, the MCU automatically starts from it.
As for HSE, two options are possible:

PLL is just a multiplier with an adjustable multiplication factor. That is, you can feed 8MHz to its input and get a multiple of 8 at the output, for example 56MHz. This understanding is enough to configure the PLL, although I must say that the PLL itself is quite interesting, and I plan to publish an article about how it works here.
Also, the PLL coefficients can only be changed while the PLL is off; they can't be changed while the PLL is running.
I'd also like to note that the MCU has a pin through which you can clock various devices or simply see what frequency the MCU started at; it's called MCO.
Another interesting feature is the protection system against unstable operation and failure of the HSE oscillator, called CSS (Clock security system).
CSS can detect failure or unstable operation of the HSE oscillator, automatically switch the clock to the internal HSI oscillator and trigger non-maskable interrupts — NMI (Non-Maskable Interrupts). Let me remind you that an NMI is an interrupt that can't be ignored without handling it; when it occurs, it interrupts program execution regardless of any conditions.
RCC_CR control register :
PLLRDY(PLL clock ready flag) — the PLL ready flag, set by hardware.
PLLON( PLL enable) — setting this bit to one enables the PLL.
CSSON(Clock security system enable) — setting this bit to one enables the HSE clock detector.
HSEBYP(External high-speed clock bypass) — setting this bit to one enables the use of an external clock source and is only possible when HSEON is cleared.
HSERDY(External high-speed clock ready flag) — a one in this bit indicates that the external oscillator has stabilized.
HSEON(HSE clock enable) — setting this bit to one turns on the external oscillator.
HSICAL[7:0](Internal high-speed clock calibration) — these bits are initialized at startup.
HSITRIM[4:0](Internal high-speed clock trimming) — the value of these bits is added to the HSICAL value, making it possible to correct the MCU frequency, which can drift depending on the supply voltage and ambient temperature. The trimming step between two values is about 40KHz.
HSIRDY(Internal high-speed clock ready flag) – this flag is set by hardware when the internal 8 MHz RC oscillator has stabilized.
HSION(Internal high-speed clock enable) — setting this bit to one forces the internal 8 MHz RC oscillator to start. This bit cannot be cleared if HSI is used to generate the system clock.
RCC_CFGR configuration register:
MCO(Microcontroller clock output) — the value of these bits determines how the MCO pin works.

USBPRE(USB prescaler) — a zero in this bit enables a prescaler with a factor of 1.5 for USB. The prescaler must be configured before enabling the USB clock.
PLLMUL(PLL multiplication factor) — the value of these bits determines the PLL multiplication factor.

PLLXTPRE(HSE divider for PLL entry) — the value of this bit determines whether HSE is divided by two before being fed to the PLL input. It must be set while the PLL is off.
PLLSRC(PLL entry clock source) — the value of this bit determines which source feeds the PLL input. (0 – HSI/2 source, 1 – HSE source)
ADCPRE(ADC prescaler) — the value of these bits determines the clock division factor for the ADC.

PPRE2(APB high-speed prescaler (APB2)) — the value of these bits determines the clock division factor for APB2.

PPRE1(APB low-speed prescaler (APB1)) — the value of these bits determines the clock division factor for APB1.

HPRE(AHB prescaler) — the value of these bits determines the clock division factor for AHB.

SWS(System clock switch status) — set by hardware, showing which source is used as the system clock.

SW(System clock switch) — these bits select the clock source for SYSCLK.

RCC_CIR interrupt register:
CSSC(Clock security system interrupt clear) — setting this bit to one clears the CSSF flag.
PLLRDYC(PLL ready interrupt clear) — setting this bit to one clears the PLLRDYF flag.
HSERDYC(HSE ready interrupt clear) — setting this bit to one clears the HSERDYF flag.
HSIRDYC(HSI ready interrupt clear) — setting this bit to one clears the HSIRDYF flag.
LSERDYC(LSE ready interrupt clear) — setting this bit to one clears the LSERDYF flag.
LSIRDYC(LSI ready interrupt clear) — setting this bit to one clears the LSIRDYF flag
PLLRDYIE(PLL ready interrupt enable) — a one in this bit enables the interrupt when the PLL locks.
HSERDYIE(HSE ready interrupt enable) — a one in this bit enables the interrupt when HSE is ready.
HSIRDYIE(HSI ready interrupt enable) — a one in this bit enables the interrupt when HSI is ready.
LSERDYIE(LSE ready interrupt enable) — a one in this bit enables the interrupt when LSE is ready.
LSIRDYIE(LSI ready interrupt enable) — a one in this bit enables the interrupt when LSI is ready.
CSSF(Clock security system interrupt flag) — this flag is set by hardware when HSE fails and is cleared with CSSC.
PLLRDYF(PLL ready interrupt flag) — this flag is set by hardware if the PLL has locked and the PLLRDYIE bit is set.
HSERDYF(HSE ready interrupt flag) — this flag is set by hardware if HSE has stabilized and the HSERDYIE bit is set.
HSIRDYF(HSI ready interrupt flag) — this flag is set by hardware if HSI has stabilized and the HSIRDYIE bit is set.
LSERDYF(LSE ready interrupt flag) — this flag is set by hardware if LSE has stabilized and the LSERDYIE bit is set.
LSIRDYF(LSI ready interrupt flag) — this flag is set by hardware if LSI has stabilized and the LSIRDYIE bit is set.
RCC_APB2RSTR reset register

Writing a one to the bits of this register resets the corresponding peripheral clocked from the APB2 bus. Reset here means loading the default values into the registers of the block, for example USART.
RCC_APB1RSTR reset register

Writing a one to the bits of this register resets the corresponding peripheral clocked from the APB1 bus. Reset here means loading the default values into the registers of the block, for example USART.
RCC_AHBENR AHB clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the AHB bus; writing a zero disables it.
RCC_APB2ENR APB2 clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the APB2 bus; writing a zero disables it.
RCC_APB1ENR APB1 clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the APB1 bus; writing a zero disables it.
RCC_BDCR backup domain control register:
BDRST(Backup domain software reset) — setting this bit to one resets the bits of the RCC_BDCR register.
RTCEN(RTC clock enable) — setting this bit to one enables RTC clocking.
RTCSEL[1:0](RTC clock source selection) — the value of these bits determines the RTC clock source.

LSEBYP(External low-speed oscillator bypass) — set and cleared by software to bypass the LSE oscillator in debug mode.
LSERDY(External low-speed oscillator ready) — a one in this bit indicates that the 32KHz LSE has stabilized.
LSEON(External low-speed oscillator enable) — setting this bit to one enables the 32KHz LSE.
RCC_CSR control/status register:
LPWRRSTF(Low-power reset flag) — this flag is set by hardware when a low-power management reset occurs.
WWDGRSTF(Window watchdog reset flag) — this flag is set by hardware when a window watchdog reset occurs.
IWDGRSTF(Independent watchdog reset flag) — this flag is set by hardware when an independent watchdog reset occurs.
SFTRSTF(Software reset flag) — this flag is set by hardware when a software reset occurs.
PORRSTF(POR/PDR reset flag) — this flag is set by hardware on power-on/power-down.
PINRSTF(PIN reset flag) — this flag is set by hardware when a reset from the NRST pin occurs.
RMVF(Remove reset flag) — setting this bit to one clears all the flags described above in this register.
LSIRDY(Internal low-speed oscillator ready) — a one in this bit indicates that LSI has stabilized.
LSION(Internal low-speed oscillator enable) — setting this bit to one enables the internal 40 kHz RC oscillator.
Actually, I still select the system bus clock frequency in the system_stm32f10x.c file by simply uncommenting the desired line, but now I do it consciously.

But when it came to configuring the clock frequency of individual blocks (for example the ADC), I used CUBE.
The whole clock system is in front of your eyes, which is very convenient and lets you quickly figure out which coefficient is responsible for what.
The method of setting the system bus frequency described above only allows you to debug the program on real hardware, because when debugging in the simulator nobody is there to set the ready flag of HSE or PLL.
Why am I writing about some flags?
Because there is actually no magic: by uncommenting the define with the desired clock frequency, we tell the preprocessor that it should include a certain piece of code in the program. If you uncomment the line
#define SYSCLK_FREQ_56MHz 56000000
The preprocessor will include the following piece of code in the program
Show / Hide text
#elif defined SYSCLK_FREQ_56MHz
/**
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2
* and PCLK1 prescalers.
* @note This function should be used only after reset.
* @param None
* @retval None
*/
static void SetSysClockTo56(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSE */
RCC->CR |= ((uint32_t)RCC_CR_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CR & RCC_CR_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CR & RCC_CR_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer */
FLASH->ACR |= FLASH_ACR_PRFTBE;
/* Flash 2 wait state */
FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY);
FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2;
/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2;
#ifdef STM32F10X_CL
/* Configure PLLs ------------------------------------------------------*/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
/* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */
RCC->CFGR2 &= (uint32_t)~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL |
RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC);
RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 |
RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5);
/* Enable PLL2 */
RCC->CR |= RCC_CR_PLL2ON;
/* Wait till PLL2 is ready */
while((RCC->CR & RCC_CR_PLL2RDY) == 0)
{
}
/* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */
RCC->CFGR &= (uint32_t)~(RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL);
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 |
RCC_CFGR_PLLMULL7);
#else
/* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7);
#endif /* STM32F10X_CL */
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait till PLL is ready */
while((RCC->CR & RCC_CR_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_SW));
RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08)
{
}
}
else
{ /* If HSE fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
The second limitation of configuring the system frequency this way is that you can't overclock the controller, since overclocking is done by configuring the PLL, but so far there has been no need for that.

So, three different sources can be used to generate the system clock (SYSCLK). (We are considering SYSCLK generation because all the peripherals are clocked from it through prescalers.)
- HSI(high speed internal) oscillator clock
- HSE(high speed external) oscillator clock
- PLL clock
HSI is an internal RC oscillator with a frequency of 8MHz. It can serve as the system clock source, or be divided by two and fed to the PLL input. It should be noted that the RC oscillator frequency depends on temperature and can deviate from the nominal value by –1.9 to +1.3% as the microcontroller temperature changes from 0 to 70°C. When the supply voltages are applied, the MCU automatically starts from it.
As for HSE, two options are possible:
- connecting an external ceramic/crystal resonator with a frequency of 4-16 MHz
- using an external clock source with a duty cycle of about 50%

PLL is just a multiplier with an adjustable multiplication factor. That is, you can feed 8MHz to its input and get a multiple of 8 at the output, for example 56MHz. This understanding is enough to configure the PLL, although I must say that the PLL itself is quite interesting, and I plan to publish an article about how it works here.
Also, the PLL coefficients can only be changed while the PLL is off; they can't be changed while the PLL is running.
I'd also like to note that the MCU has a pin through which you can clock various devices or simply see what frequency the MCU started at; it's called MCO.
Another interesting feature is the protection system against unstable operation and failure of the HSE oscillator, called CSS (Clock security system).
CSS can detect failure or unstable operation of the HSE oscillator, automatically switch the clock to the internal HSI oscillator and trigger non-maskable interrupts — NMI (Non-Maskable Interrupts). Let me remind you that an NMI is an interrupt that can't be ignored without handling it; when it occurs, it interrupts program execution regardless of any conditions.
RCC_CR control register :
PLLRDY(PLL clock ready flag) — the PLL ready flag, set by hardware.
PLLON( PLL enable) — setting this bit to one enables the PLL.
CSSON(Clock security system enable) — setting this bit to one enables the HSE clock detector.
HSEBYP(External high-speed clock bypass) — setting this bit to one enables the use of an external clock source and is only possible when HSEON is cleared.
HSERDY(External high-speed clock ready flag) — a one in this bit indicates that the external oscillator has stabilized.
HSEON(HSE clock enable) — setting this bit to one turns on the external oscillator.
HSICAL[7:0](Internal high-speed clock calibration) — these bits are initialized at startup.
HSITRIM[4:0](Internal high-speed clock trimming) — the value of these bits is added to the HSICAL value, making it possible to correct the MCU frequency, which can drift depending on the supply voltage and ambient temperature. The trimming step between two values is about 40KHz.
HSIRDY(Internal high-speed clock ready flag) – this flag is set by hardware when the internal 8 MHz RC oscillator has stabilized.
HSION(Internal high-speed clock enable) — setting this bit to one forces the internal 8 MHz RC oscillator to start. This bit cannot be cleared if HSI is used to generate the system clock.
RCC_CFGR configuration register:
MCO(Microcontroller clock output) — the value of these bits determines how the MCO pin works.

USBPRE(USB prescaler) — a zero in this bit enables a prescaler with a factor of 1.5 for USB. The prescaler must be configured before enabling the USB clock.
PLLMUL(PLL multiplication factor) — the value of these bits determines the PLL multiplication factor.

PLLXTPRE(HSE divider for PLL entry) — the value of this bit determines whether HSE is divided by two before being fed to the PLL input. It must be set while the PLL is off.
PLLSRC(PLL entry clock source) — the value of this bit determines which source feeds the PLL input. (0 – HSI/2 source, 1 – HSE source)
ADCPRE(ADC prescaler) — the value of these bits determines the clock division factor for the ADC.

PPRE2(APB high-speed prescaler (APB2)) — the value of these bits determines the clock division factor for APB2.

PPRE1(APB low-speed prescaler (APB1)) — the value of these bits determines the clock division factor for APB1.

HPRE(AHB prescaler) — the value of these bits determines the clock division factor for AHB.

SWS(System clock switch status) — set by hardware, showing which source is used as the system clock.

SW(System clock switch) — these bits select the clock source for SYSCLK.

RCC_CIR interrupt register:
CSSC(Clock security system interrupt clear) — setting this bit to one clears the CSSF flag.
PLLRDYC(PLL ready interrupt clear) — setting this bit to one clears the PLLRDYF flag.
HSERDYC(HSE ready interrupt clear) — setting this bit to one clears the HSERDYF flag.
HSIRDYC(HSI ready interrupt clear) — setting this bit to one clears the HSIRDYF flag.
LSERDYC(LSE ready interrupt clear) — setting this bit to one clears the LSERDYF flag.
LSIRDYC(LSI ready interrupt clear) — setting this bit to one clears the LSIRDYF flag
PLLRDYIE(PLL ready interrupt enable) — a one in this bit enables the interrupt when the PLL locks.
HSERDYIE(HSE ready interrupt enable) — a one in this bit enables the interrupt when HSE is ready.
HSIRDYIE(HSI ready interrupt enable) — a one in this bit enables the interrupt when HSI is ready.
LSERDYIE(LSE ready interrupt enable) — a one in this bit enables the interrupt when LSE is ready.
LSIRDYIE(LSI ready interrupt enable) — a one in this bit enables the interrupt when LSI is ready.
CSSF(Clock security system interrupt flag) — this flag is set by hardware when HSE fails and is cleared with CSSC.
PLLRDYF(PLL ready interrupt flag) — this flag is set by hardware if the PLL has locked and the PLLRDYIE bit is set.
HSERDYF(HSE ready interrupt flag) — this flag is set by hardware if HSE has stabilized and the HSERDYIE bit is set.
HSIRDYF(HSI ready interrupt flag) — this flag is set by hardware if HSI has stabilized and the HSIRDYIE bit is set.
LSERDYF(LSE ready interrupt flag) — this flag is set by hardware if LSE has stabilized and the LSERDYIE bit is set.
LSIRDYF(LSI ready interrupt flag) — this flag is set by hardware if LSI has stabilized and the LSIRDYIE bit is set.
RCC_APB2RSTR reset register

Writing a one to the bits of this register resets the corresponding peripheral clocked from the APB2 bus. Reset here means loading the default values into the registers of the block, for example USART.
RCC_APB1RSTR reset register

Writing a one to the bits of this register resets the corresponding peripheral clocked from the APB1 bus. Reset here means loading the default values into the registers of the block, for example USART.
RCC_AHBENR AHB clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the AHB bus; writing a zero disables it.
RCC_APB2ENR APB2 clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the APB2 bus; writing a zero disables it.
RCC_APB1ENR APB1 clock enable register

Writing a one to the bits of this register enables the clock of the corresponding peripheral connected to the APB1 bus; writing a zero disables it.
RCC_BDCR backup domain control register:
BDRST(Backup domain software reset) — setting this bit to one resets the bits of the RCC_BDCR register.
RTCEN(RTC clock enable) — setting this bit to one enables RTC clocking.
RTCSEL[1:0](RTC clock source selection) — the value of these bits determines the RTC clock source.

LSEBYP(External low-speed oscillator bypass) — set and cleared by software to bypass the LSE oscillator in debug mode.
LSERDY(External low-speed oscillator ready) — a one in this bit indicates that the 32KHz LSE has stabilized.
LSEON(External low-speed oscillator enable) — setting this bit to one enables the 32KHz LSE.
RCC_CSR control/status register:
LPWRRSTF(Low-power reset flag) — this flag is set by hardware when a low-power management reset occurs.
WWDGRSTF(Window watchdog reset flag) — this flag is set by hardware when a window watchdog reset occurs.
IWDGRSTF(Independent watchdog reset flag) — this flag is set by hardware when an independent watchdog reset occurs.
SFTRSTF(Software reset flag) — this flag is set by hardware when a software reset occurs.
PORRSTF(POR/PDR reset flag) — this flag is set by hardware on power-on/power-down.
PINRSTF(PIN reset flag) — this flag is set by hardware when a reset from the NRST pin occurs.
RMVF(Remove reset flag) — setting this bit to one clears all the flags described above in this register.
LSIRDY(Internal low-speed oscillator ready) — a one in this bit indicates that LSI has stabilized.
LSION(Internal low-speed oscillator enable) — setting this bit to one enables the internal 40 kHz RC oscillator.
Actually, I still select the system bus clock frequency in the system_stm32f10x.c file by simply uncommenting the desired line, but now I do it consciously.
