Detailed explanation of STM32 clock

Publisher:心灵捕手Latest update time:2015-10-09 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
In STM32, there are five clock sources, HSI, HSE, LSI, LSE, and PLL. In fact, there are four clock sources, as shown in the figure below (gray-blue). PLL is a phase-locked loop circuit that multiplies the frequency.
Get the PLL clock.
①. HSI is the high-speed internal clock, RC oscillator, with a frequency of 8MHz.
②. HSE is a high-speed external clock that can be connected to a quartz/ceramic resonator or an external clock source with a frequency range of 4MHz~16MHz.
③. LSI is a low-speed internal clock, RC oscillator, with a frequency of 40kHz.
④. LSE is a low-speed external clock connected to a quartz crystal with a frequency of 32.768kHz.
⑤、PLL is a phase-locked loop frequency multiplication output, and its clock input source can be selected as HSI/2, HSE or HSE/2. The frequency multiplication can be selected as 2~16 times, but its maximum output frequency shall not exceed
Over 72MHz.
Detailed explanation of STM32 clock
The 40kHz LSI is used by the independent watchdog IWDG. It can also be selected as the clock source of the real-time clock RTC.
LSE, or HSE divided by 128. The clock source of RTC is selected by RTCSEL[1:0].
The STM32 has a full-speed USB module, and its serial interface engine requires a 48MHz clock source. This clock source can only be obtained from the PLL output.
Select 1.5 division or 1 division, that is, when the USB module needs to be used, the PLL must be enabled and the clock frequency must be configured as 48MHz or 72MHz.
In addition, STM32 can also select a clock signal to output to the MCO pin (PA8), which can be selected as the 2-division output of the PLL, HSI, HSE, or system clock.
The system clock SYSCLK is the clock source for most components in the STM32. The system clock can be selected as PLL output, HSI or HSE. The maximum frequency of the system clock
The AHB divider can select 1, 2, 4, 8, 16, 64, 128, 256, or 512 divisions.
The output clock is sent to 5 major modules:
①. HCLK clock sent to AHB bus, core, memory and DMA.
②. The system timer clock is sent to Cortex after being divided by 8.
③. Send the idle running clock FCLK directly to Cortex.
④, sent to APB1 divider. APB1 divider can select 1, 2, 4, 8, 16 frequency division, one of its outputs is used for APB1 peripherals (PCLK1, maximum frequency 36MHz), and the other is used for APB1 peripherals (PCLK1, maximum frequency 36MHz).
The clock output is sent to the timer 2, 3, 4 frequency multiplier. The frequency multiplier can select 1 or 2 times, and the clock output is used by timer 2, 3, 4.
⑤, sent to APB2 divider. APB2 divider can select 1, 2, 4, 8, 16 frequency division, one of its outputs is used for APB2 peripherals (PCLK2, maximum frequency 72MHz), and the other is used for APB2 peripherals (PCLK2, maximum frequency 72MHz).
The APB2 divider has one output for the Timer 1 frequency multiplier. The frequency multiplier can select 1 or 2 times the frequency, and the clock output is used by Timer 1.
The ADC frequency divider is used and the divided frequency is sent to the ADC module for use. The ADC frequency divider can be selected as 2, 4, 6, or 8 divisions.
Among the above clock outputs, many have enable control, such as AHB bus clock, core clock, various APB1 peripherals, APB2 peripherals, etc.
When you use the module, remember to enable the corresponding clock first.
It should be noted that the timer multiplier, when the APB frequency division is 1, its multiplier value is 1, otherwise its multiplier value is 2.
The devices connected to APB1 (low-speed peripherals) are: power interface, backup interface, CAN, USB, I2C1, I2C2, UART2, UART3, SPI2, window watchdog, Timer2,
Timer3, Timer4. Note that although the USB module requires a separate 48MHz clock signal, it should not be the clock for the USB module to work, but only for the serial interface.
The clock used by the engine (SIE). The clock for the USB module to work should be provided by APB1.
The devices connected to APB2 (high-speed peripherals) are: UART1, SPI1, Timer1, ADC1, ADC2, all common IO ports (PA~PE), and second function IO ports. [page]
For a single-chip microcomputer system, the clock settings of the CPU, bus, and peripherals are very important, because without a clock there is no timing.
Since the clock is an inside-out thing, the specific setting starts from the register.
The RCC register structure, RCC_TypeDeff, is defined in the file "stm32f10x.h" as follows: (v3.4 library)
Line 1059->Line 1081.
1. typedef struct
2. {
3. __IO uint32_t CR;
4. __IO uint32_t CFGR;
5. __IO uint32_t CIR;
6. __IO uint32_t APB2RSTR;
7. __IO uint32_t APB1RSTR;
8. __IO uint32_t AHBENR;
9. __IO uint32_t APB2ENR;
10. __IO uint32_t APB1ENR;
11. __IO uint32_t BDCR;
12. __IO uint32_t CSR;
13.
14. #ifdef STM32F10X_CL
15. __IO uint32_t AHBRSTR;
16. __IO uint32_t CFGR2;
17. #endif
18.
19. #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
20. uint32_t RESERVED0;
21. __IO uint32_t CFGR2;
22. #endif
23. }RCC_TypeDef;
Generally, there is only an 8Mhz crystal oscillator on the board, but the maximum operating frequency of the enhanced version is 72Mhz, so it is obviously necessary to use PLL to multiply the frequency by 9 times. These settings need to be completed during the initialization phase.
Using HSE clock, the program sets the clock parameters process:
1. Reset the RCC register to the default value RCC_DeInit;
2. Turn on the external high-speed clock crystal HSE RCC_HSEConfig (RCC_HSE_ON);
3. Wait for the external high-speed clock crystal to work HSEStartUpStatus = RCC_WaitForHSEStartUp();
4. Set AHB clock RCC_HCLKConfig;
5. Set high-speed AHB clock RCC_PCLK2Config;
6. Set the low speed AHB clock RCC_PCLK1Config;
7. Set PLL RCC_PLLConfig;
8. Enable PLL RCC_PLLCmd(ENABLE);
9. Wait for PLL to work while (RCC_GetFlagStatus (RCC_FLAG_PLLRDY) == RESET)
10. Set the system clock RCC_SYSCLKConfig;
11. Determine whether PLL is the system clock while (RCC_GetSYSCLKSource() != 0x08)
12. Open the peripheral clock RCC_APB2PeriphClockCmd()/RCC_APB1PeriphClockCmd() to be used
For the convenience of explanation, we will use the RCC setting function of the following routine and explain it in the form of Chinese comments:
1. static void RCC_Config(void)
2. {
3.
4.
5. RCC_DeInit();
6.
7.
8. RCC_HSEConfig(RCC_HSE_ON);
9.
10.
11. HSEStartUpStatus = RCC_WaitForHSEStartUp();
12.
13. if (HSEStartUpStatus == SUCCESS)
14.
15.
16. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
17.
18.
19. FLASH_SetLatency(FLASH_Latency_2);
20.
twenty one.
22. RCC_HCLKConfig(RCC_SYSCLK_Div1);
twenty three.
twenty four.
25. RCC_PCLK2Config(RCC_HCLK_Div1);
26.
27.
28. RCC_PCLK1Config(RCC_HCLK_Div2);
29.
30.
31. RCC_ADCCLKConfig(RCC_PCLK2_Div6);
32.
33.
34. //This sentence is very important
35.
36. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
37.
38.
39. RCC_PLLCmd(ENABLE);
40.
41.
42.
43. while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
44. {}
45.
46.
47. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
48.
49.
50. while (RCC_GetSYSCLKSource() != 0x08)
51. {}
52. }
53.
54.
55. // Enable the peripheral interface bus clock. Pay attention to the affiliation of each peripheral. Different chips have different allocations. You can check the manual when the time comes.
Can
56. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
57.
58. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
59. RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG |
60. RCC_APB2Periph_AFIO, ENABLE);
61. }
From the above program, we can see that the system clock setting is relatively complicated. The more peripherals there are, the more factors need to be considered. At the same time, this setting also has rules to follow.
There are also sequential specifications, which should be noted in the application. For example, the PLL settings need to be made before enabling, and the parameters cannot be changed once the PLL is enabled.
After this setting, since my circuit board has an 8Mhz crystal oscillator, the system clock is 72Mhz, the high-speed bus and low-speed bus 2 are both 72Mhz, and the low-speed bus 1 is 36Mhz.
The ADC clock is 12Mhz, and the USB clock can be divided by 1.5 to achieve 48Mhz data transmission.
For general clock settings, you need to first consider the source of the system clock, whether it is an internal RC, an external crystal, or an external oscillator, and whether a PLL is required. Then consider the internal bus and external
The bus, and finally the peripheral clock signal. The principle of multiplying the frequency first as the CPU clock, and then dividing the frequency from the inside out, and the lower level accommodating the upper level is somewhat similar to the specification of PCB drawing.
The same applies here.
Note:
In STM32, the devices connected to APB1 (low-speed peripherals) are: power interface, backup interface, CAN, USB, I2C1, I2C2, UART2, UART3, SPI2, window watchdog, Timer2,
Timer3, Timer4.
The devices connected to APB2 (high-speed peripherals) are: GPIO_A-E, USART1, ADC1, ADC2, ADC3, TIM1, TIM8, SPI1, ALL.
Program example:
APB1 (low-speed peripherals)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
APB2 (High-speed peripherals)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

Keywords:STM32 Reference address:Detailed explanation of STM32 clock

Previous article:The process of mapping the physical address of a register to the register encapsulated by the structure
Next article:STM32 timer detailed explanation ----- shadow register, pre-loaded register

Recommended ReadingLatest update time:2024-11-16 13:48

Successful debugging based on STM32+ucos-II+DS18B20
A few days ago, I debugged the DS18B20 program. At first, when the system was running naked, the DS18B20 could read data normally. Since the ucos real-time operating system was added during the early design of the project, I thought that the naked program could be transplanted and used. As a result, tragedy happened...
[Microcontroller]
Real-time clock DS1302 reading and writing program (C51)
//Open DS1302 void TimeSpiOpen(void) { TIMECLK = 0; TIMERST = 0; //Disable DS1302 TIMEIO = 1; //Release data bus TIMERST = 1; //Enable DS1302 } //Close DS1302 void TimeSpiClose(void) { TIMERST = 0;//Disable DS1302 TIMEIO = 1;//Release data bus TIMECLK = 1; } //Read and write DS1302 unsigned char TimeSpiReadWrite(unsig
[Microcontroller]
STM32 learning notes: the voltage value sampled by adc is sent to pc using 485
Using channel 10 of adc1, the collected voltage value is sent to the PC display using 485.  First, the schematic diagram is attached.      The source code is in the attachment.  Here are a few points to note:  1. The voltage input by ad is divided by R42 and R44, so the voltage value displayed by the PC will be halve
[Microcontroller]
STM32 learning notes: the voltage value sampled by adc is sent to pc using 485
Differences between STM32 TIMER timers
The STM32F1 series has 14 timers, 1 to 14; the specific models vary. It is suitable for a variety of purposes, including measuring the pulse width of the input signal (input capture), or generating output waveforms (output comparison, PWM, complementary PWM with embedded dead time, etc.). Using the timer prescaler and
[Microcontroller]
Differences between STM32 TIMER timers
AVR-based ds1302 clock code
#include iom16v.h #include macros.h           #define uchar unsigned char   #define uint unsigned int   #define SCK_CLR PORTC&=~BIT(0) //Pull the clock line low   #define SCK_SET PORTC|=BIT(0) //Pull the clock line high #define SCK_OUT DDRC|=BIT(0) #define RST_CLR PORTC&=~BIT(2) //Pull down the RST line reset line
[Microcontroller]
STM32 serial communication sending and receiving
There are pros and cons to using STM32. There are many types --- but there are too many types, and the information is also messy. In addition, when calling the library, I often forget to use some functions ------ such as the most commonly used serial port ------ ------------------------------------------------------
[Microcontroller]
STM32 scatter loading file
By using the scatter-loading mechanism, you can specify the memory map of an image to the linker. Scatter-loading gives you complete control over the grouping and location of image components. Scatter-loading can be used for simple images, but it is usually only used for images with complex memory maps, where multiple
[Microcontroller]
STM32 scatter loading file
Solar LED Street Light Solution Using STM32 MCU
As fossil energy is decreasing and the problem of global warming caused by excessive greenhouse gas emissions is becoming more and more important, people are actively developing various types of renewable energy on the one hand, and advocating green environmental protection technologies for energy conservation and
[Power Management]
Solar LED Street Light Solution Using STM32 MCU
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号