In the STM32F4 standard firmware library, the clock source selection and clock enable functions are declared and defined in the RCC related firmware library files stm32f4xx_rcc.h and stm32f4xx_rcc.c. When you open the stm32f4xx_rcc.h file, you can see that there are many macro definition identifiers at the beginning of the file, followed by a series of clock configuration and clock enable function declarations. These functions can be roughly divided into three categories: one is the peripheral clock enable function, one is the clock source and frequency division factor configuration function, and the other is the peripheral reset function. Of course, there are also several functions for obtaining the clock source configuration. Below we briefly introduce the use of these library functions with several common operations.
The first is the clock enable function. Clock enable related functions include peripheral setting enable and clock source enable. First, let's look at the peripheral clock enable related functions:
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState);
void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState);
void RCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState);
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
There are mainly 5 peripheral clock enable functions. The 5 functions are used to enable the peripheral clocks mounted under the 5 buses, which are: AHB1 bus, AHB2 bus, AHB3 bus, APB1 bus and APB2 bus. To enable a peripheral, call the corresponding bus peripheral clock enable function.
Here we have to explain that the clock of STM32F4 peripherals must be enabled before use. If the clock is not enabled, the peripherals will not work properly. As for which peripheral is mounted on which bus, although we can also check the manual, if you use the library function, it is actually not necessary to check the manual. Here we introduce a little trick.
For example, if we want to enable GPIOA, we only need to search for GPIOA in the stm32f4xx_rcc.h header file, and we can find that the first entry parameter of the corresponding clock enable function is RCC_AHB1Periph_GPIOA. From this macro definition identifier, we can see at a glance that GPIOA is mounted under AHB1. Similarly, for serial port 1, we can search for USART1 and find the identifier RCC_APB2Periph_USART1, so it is easy to know that serial port 1 is mounted under APB2. This knowledge is also explained in the "4.7 Quick Code Organization Techniques" section below, so I will mention it here.
If we want to enable GPIOA, we can find the macro definition identifier RCC_AHB1Periph_GPIOA in the header file stm32f4xx_rcc.h. As the name implies, GPIOA is mounted under the AHB1 bus, so we call the peripheral clock enable function RCC_AHB1PeriphClockCmd under the AHB1 bus. The specific calling method is as follows:
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //Enable GPIOA clock
Similarly, if we want to enable the clock of serial port 1, the function we call is:
void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState);
The specific calling method is:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
Another type of clock enable function is the clock source enable function. We have already explained that STM32F4 has 5 major types of clock sources. Here we list several important clock source enable functions:
void RCC_HSICmd(FunctionalState NewState);
void RCC_LSICmd(FunctionalState NewState);
void RCC_PLLCmd(FunctionalState NewState);
void RCC_PLLI2SCmd(FunctionalState NewState);
void RCC_PLLSAICmd(FunctionalState NewState);
void RCC_RTCCLKCmd(FunctionalState NewState);
These functions are used to enable the corresponding clock source. For example, if we want to enable the PLL clock, the function called is:
void RCC_PLLCmd(FunctionalState NewState);
The specific calling method is as follows:
RCC_PLLCmd(ENABLE);
We need to enable the corresponding clock source and call the corresponding function.
Next we will explain the second type of clock function: clock source selection and frequency division factor configuration functions. These functions are used to select the corresponding clock source and configure the corresponding clock frequency division factor. For example, we have explained the system clock SYSCLK before. We can choose one of the three clock sources of HSI, HSE and PLL as the system clock. So which one to choose is configurable. Here are several clock source configuration functions:
void RCC_LSEConfig(uint8_t RCC_LSE);
void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource);
void RCC_HCLKConfig(uint32_t RCC_SYSCLK);
void RCC_PCLK1Config(uint32_t RCC_HCLK);
void RCC_PCLK2Config(uint32_t RCC_HCLK);
void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource);
void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM,
uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ);
For example, if we want to set the system clock source to HSI, we can call the system clock source configuration function:
void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource);
The specific configuration method is as follows:
void RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); //Configure the clock source to HSI
For example, if we want to set the APB1 bus clock to 2 of HCLK, that is, set the division factor to 2, then if we want to enable HSI, the function to be called is:
void RCC_PCLK1Config(uint32_t RCC_HCLK);
The specific configuration method is as follows:
RCC_PCLK1Config(RCC_HCLK_Div2);
Next, let's look at the third type of peripheral reset function. As follows:
void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState);
void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState);
void RCC_AHB3PeriphResetCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState);
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
The usage of this type of function is basically the same as the peripheral clock function explained above, the difference is that one is used to enable the peripheral clock, and the other is used to reset the corresponding peripheral. Don't be confused when calling the function here.
We will not list these clock operation functions one by one. You can open the corresponding RCC file to learn more about it.
Previous article:STM32f4---Marquee Experiment Code
Next article:STM32F4 clock initialization configuration
Recommended ReadingLatest update time:2024-11-17 05:38
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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
- Decoupling capacitors
- Let's talk about out of stock issues
- What is the US energy metering chip that corresponds to ATT7021?
- 89S51 single chip microcomputer
- 28335 cannot connect to JTAG, urgent help!!!!!!
- Summary of pspice circuit simulation
- Playing with Zynq Serial 40——[ex59] Binocular vision image acquisition and display example based on Zynq
- Several reasons for electromagnetic flowmeter errors
- Summary and analysis of interference problems in circuit design
- Does the dummy pad have any impact on the electrical characteristics of the PCB?