The Stm32 clock structure diagram is as follows, ( http://www.openedv.com/posts/list/302.htm )
The analysis of the above figure is as follows:
Important clocks: The relationship between
Specific configuration process:
first step:
Reset and configure the vector table.
Function MYRCC_DeInit();
The following function is analyzed:
(1)
This register contains the reset settings of peripherals such as DAC, power reset, timer, etc. A bit of 1 indicates that the corresponding peripheral is reset. The register data is cleared when the machine is powered on.
(2)
Same as the first step of setting the peripheral reset register.
answer:
RCC->APB1RSTR
RCC->APB2RSTR
What does "reset ends" mean here? ? I commented it out and found that it can still run
1 means reset. 0 means no reset. If
no reset occurs, the reset is complete.
(3)
STm32 has three startup modes:
1. ISP mode. This mode is that after STM32 is reset, it executes the BOOTLOADER program that is fixed inside (it is fixed and we cannot read or write it.), and then waits for serial port data to realize the serial port bootloader function.
This mode will not start from the user storage area (unless it is controlled by the serial port to start from 0X08000000), so after updating the code, it needs to be set to other modes (FLASH mode).
2. FLASH startup mode. This mode starts directly from 0X08000000, which is the startup method of the code we wrote ourselves. This should be used in normal situations.
3. SRAM startup mode. I have not used this mode. It starts from 0X20000000, that is, before the sram mode starts, you must make sure that there is code in the SRAM, otherwise it will freeze.
RCC->AHBENR = 0x00000014
(4)
RCC->APB1ENR = 0x00000000;
RCC->APB2ENR = 0x00000000;
(5)
RCC->CR |=0x00000001;
The clock startup process of stm32.
The startup process is:
1. First use the internal clock (this is why you can download the code without connecting the crystal oscillator).
2. Try to turn on the external clock.
3. If it is successfully turned on, use the external clock, otherwise use the internal clock.
4. Do other things.
Of course, you need to write the code to implement the above code yourself. Of course, the internal clock is the default clock, and you don’t have to turn it on.
(6)
RCC->CFGR &= 0xF8FF0000;
What does this step mean? My understanding is that the Cfgr register is mainly used to control the clock division, see the figure below:
Through the configuration of this step:
First, configure MCO to have no output. What is MCO? It means that the internal clock of stm32 can be output through the IO port pin. As shown in the figure above, there are four types of mco outputs for the configuration of cfgr, namely, output after dividing pllclk by two, hsi (on-chip clock) output, etc.
Secondly: configure ADCPRE is the ADC of the AHB divider line surface in the above figure
Next: configure ppre2, which is the high-speed external clock APB2. Set it to no frequency division. The high-speed external clock mainly drives some high-speed peripherals. This is introduced in the APB2ENR clock control register.
Again: configure PPRE1, configure the low-speed external clock division, and set APB1 to no division.
Next: configure HPRE. These bits are mainly used to configure the frequency division coefficient of the AHB register. Here it is also set to no frequency division. That is to say, the SYSCLK in the above figure is not divided by AHB. [page]
Finally: configure SW and SWS. This means that HIS is enabled as the system clock.
At this point, after analysis, we know that RCC->CFGR &= 0xF8FF0000; is mainly used to configure the settings of various dividers such as ahb, and to use the on-chip clock as the internal clock of the system.
(6)
RCC->CR &= 0xFEF6FFFF;
By analyzing the CR register, we can see that this register mainly involves three clocks: PLL, CSS, and HSE.
(7)
RCC->CR &= 0xFFFBFFFF; What is the purpose of this step? According to page 57 of the data sheet, the external clock source HSE has two modes. When HSEBYP is set to 0, the external crystal is selected as the external clock source. This clock is more accurate and of course it is related to the external circuit. Of course, because HSEON has been set to off in step (6), HSEBYP can be set freely in this step.
(8)
RCC->CFGR &= 0xFF80FFFF;
Note: In this section you may have questions like:
RCC->CFGR &= 0xFF80FFFF;
PLLSRC=0 HSI oscillator clock is divided by 2 and used as PLL input clock
PLLXTPRE=0, HSE divider is used as PLL input, HSE does not divide
Is there no conflict?
The answer is: the last configuration shall prevail. That is, the last configuration will change the previous configuration, so the last configuration shall prevail.
That is to say, there are other codes to define it later. So why do we need to repeat the configuration?
Sometimes it is useful. For example, if you want to overclock the stm32 for a while and then resume normal operation, this is useful.
(9)
RCC->CIR = 0x00000000;
(10)
#ifndef VECT_TAB_RAM
#else
#endif
The following is an analysis of the function:
//Function: Set the vector table offset address
//NVIC_VectTab: base address
//Offset: offset
void MY_NVIC_SetVectorTable(u32 NVIC_VectTab, u32 Offset)
{
}
The first two lines are used to check the parameter validity, which will not be analyzed here. Focus on the third line
What is the use of configuring this vector table? See the explanation of the vector table on page 113 of the CortexM3 Definitive Guide
here
#define NVIC_VectTab_RAM
#define NVIC_VectTab_FLASH
The value of Offset is 0x0, which is the offset address. The address must be divisible by 64 * 4 = 256. For details, please refer to page 113 of the authoritative manual.
SCB->VTOR = NVIC_VectTab|(Offset & (u32)0x1FFFFF80); //Set the vector table offset register of NVIC.
Since it is to set the vector table offset of NVIC, why do we need to OR it with NVIC_VectTab? Isn't it enough to just set OFFSET? In addition, only BIT [28:7] of VTOR setting is effective. There won't be enough space for so many bits after ORing, right?
This is the base address.
For 7~28, can you define a 28-bit data?
VTOR setting only has BIT [28:7]. Check if (u32) 0x1FFFFF80 is [28:7].
Then read the following paragraph:
must be divisible by 64*4=256, so the legal starting address can be: 0x0, 0x100, 0x200, etc.
Answer: The authoritative guide of cortex-m3 says that
Let's go back to the Stm32_Clock_Init() function on page 61 of the book STM32:
After the above configuration is completed, start configuring the external clock.
The current implementation of the MinisTM32 development board uses a high-speed external clock as the clock source. After MYRCC_Deinit(), the external clock source is turned off first, and then after cfgr is reconfigured, the high-speed external clock is ready to be turned on.
(11)
(12)
While(!(RCC->CR>>17));
In this regard, Atom also said that it is more appropriate to write (RCC-CR>>17)&0X01, but I feel that RCC-CR>>17 is inaccurate. For example, if the 18th bit is 1, then after shifting right by 17 bits, regardless of whether the clock is ready, the result of the expression "RCC-CR>>17" is always true. In this case, while (! (RCC-CR>>17)) is meaningless, right? So writing (RCC-CR>>17)&0X01 is the most accurate.
)
(13)
RCC->CFGR = 0x00000400;
(14)
PLL -=2;
RCC->CFGR = PLL <<18;
Set PLL 9x
There is also a problem involved here, as follows
Actually, here today Lin Meimei asked a more professional question, that is, PLL is a u8 data type, why can it be right shifted 18 bits here? Isn't it already exceeded? Actually, we can understand it by looking at the assembly code, the assembly code is as follows: 219: RCC->CFGR|=PLL<<18; //Set PLL value
(15)
//For details, see "STM32 Flash Programming"
(16)
RCC->CR|=0x01000000;
(17)
while(!((RCC->CR>>25)&0x01));
(18)
RCC->CFGR |= 0x00000002;
(19)
Unsigned char Temp = 0;
While(Temp!=0x02)
{
}
In fact, this code is to judge SWS and wait for the system clock to be successfully converted to PLL clock.
Combined with the above analysis, it is clear that the STM32 clock is always configured. The main flow chart is as follows:
In fact, I personally feel that there are some unnecessary configurations of the routines in mini32, so I changed some of them myself and found that it can also run in the marquee program. Currently, I have only tested it in the marquee program:
first step:
Step 2:
Step 3: Turn off all peripheral clocks
Why is this step necessary? Because when configuring registers such as cfgr and cr, some peripheral clocks need to be turned off.
the fourth step:
Step 5: Set the frequency division register, configure the frequency division, and enable PLLSRC ON
[page]
RCC->CFGR=0X00000400; //APB1/2=DIV2;APB2=DIV1;AHB=DIV1; According to the Chinese manual,
The maximum of apb1 is 36MHZ, so it needs to be divided here. Because after this setting, the output of PLLMUL is 72MHZ, so here we need to make APB1/2=DIV2 which is 36MHZ.
PLL-=2; //Offset 2 units
RCC->CFGR|=PLL<<18;
RCC->CFGR|=1<<16;
At this time, hse is 8MHZ. Obviously, after the 9-times multiplication, the SYSCLK output to AHB is 72MHZ. Because AHB is set to not divide the frequency, the AHB output is also 72MHZ. Apb1 is divided before, so the output is 36MHZ. Apb2 is 72MHZ
Step 7:
FLASH->ACR|=0x32;
Step 8:
Step 9:
#ifdef
#else
#endif
Step 10:
Step 11: Turn on the PLL.
Step 12:
Combined with the clock configuration process of Stm32_Clock_Init(), I summarize the clock configuration as follows:
turn off all peripheral clocks,
(1) enable HSI and turn off HSE, PLL, CSS, configure the frequency division register, and set the system clock to HSI in crgr.
(2) Turn off all interrupts.
(3) Configure the vector table.
(4) Enable HSE and wait for the setting to be completed in CR.
(5) Turn on PLL and wait for PLL to turn on in CR.
(6) Wait for PLL to become the system clock in sws bit in cfgr.
Combined with the above methods, the code I rewrote is as follows:
void Stm32_Clock_Init111(u8 PLL)
{
#ifdef
#else
#endif
}
Previous article:Study on three low power modes of STM32
Next article:Study on three low power consumption modes of STM32
Recommended ReadingLatest update time:2024-11-16 17:37
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Forum member long521's niece is sick, please help her!
- EEWORLD University ---- Application of Weidmuller products in the semiconductor industry
- Linear thyristor power supply dimming and EMC issues
- Synopsys IP Resources: A look at the complete Ethernet PHY IP for high-performance computing SoCs
- [National Technology N32 MCU Development Package] --N32G4FR Series
- CC3220 Wireless MCU LaunchPad Development Kit Design
- 422 Driver Output Level
- NB-IoT application classification and technical characteristics analysis
- "Operational Amplifier Parameter Analysis and LTspice Application Simulation" 2. Chapter 2 Bias Current
- I2C Timing