FreeRTOS Lecture 27 – Exercise: Understanding MCU clock configuration Part-2

 

Exercise: Understanding MCU clock configuration Part-2

 

 

From Figure 1, you can see that the reset handler begins from line 42. This is the first function or subroutine, which will be called when you reset the microcontroller.

Understanding MCU clock configuration Part-2
Figure 1. Startup code.

 

If you trace this reset function, you can see that it is actually called SystemInit (Figure 2). Branch bl branch to SystemInit. The SystemInit is implemented in the system-stm32F4xx.c (Figure 3). This is added by the OpenSTM32 system workbench itself when you create a project.

Understanding MCU clock configuration Part-2
Figure 2. Call for SystemInit in reset handler.

 

 Understanding MCU clock configuration Part-2
Figure 3. system-stm32F4xx.c file.

 

Now let’s analyze function carefully. Let’s see where it is turning on the PLL. It actually turns on the PLL in the configure the system clock source, PLL multiplier, and divider factors section shown in Figure 4. So, let’s trace SetSysClock() function.

Understanding MCU clock configuration Part-2
Figure 4. Function to configure the PLL.

 

The implementation of the SetSysClock function is as shown in Figure 5. Now let’s check this function. The statement to turn on the main PLL is given in the section called to enable the main PLL (Figure 6). That means this project is by default turning on the PLL. The SetSysClock function is doing so many PLL configurations in order to raise the system clock to 180 MHz. This PLL configuration is not required in our project because our project is a very simple project to print “Hello World”. So, why do we need 180 MHz of system clock? That’s not needed.

Understanding MCU clock configuration Part-2
Figure 5. Implementation of SetSysClock.

 

Statement to turn on the main PLL
Figure 6. Statement to turn on the main PLL.

 

Now let’s have a default configuration, i.e., 16 MHz of the system clock. For that, you have to turn off the PLL. Let’s reverse all the configurations or settings, which is done by the SetSysClock. 

Steps to get into the default configuration is as follows:

  • To do that, let’s take the help of a standard peripheral driver (Figure 7). Just expand the standard peripheral driver, then expand the source and just explore the stm32f4xx_rcc.c (Figure 8). RCC stands for reset and clock control. stm32f4xx_rcc.c is a driver file to handle the RCC engine of the microcontroller. It gives you various APIs in order to manage the clock of your microcontroller.
Standard peripheral driver
Figure 7. Standard peripheral driver.

 

stm32f4xx_rcc.c
Figure 8. stm32f4xx_rcc.c.

 

  • In stm32f4xx_rcc.c, you will find an API called RCC_DeInit. First, let’s deinitialize the RCC_Init because that has been initialized with PLL by default code present in our project. Now let’s deinitialize everything, as shown in Figure 9. RCC_DeInit resets the RCC clock configuration to the default reset state. That means it will only turn on the HSI to get the 16 MHz of the system clock, and it will turn off the PLL.
RCC_DeInit()
Figure 9. RCC_DeInit().

 

  • Let’s use this RCC_DeInit function in our main function. First, reset the RCC clock configuration to the default reset state. For that, you have to call the RCC_DeInit function (Figure 10). This DeInit function turns on HSI, turns off PLL and HSE, and makes the system clock 16 MHz. Now the system clock is 16 MHz. Since you have used DeInit, the AHB prescaler value will be 1, i.e., 16/1 =16. Therefore the clock of 16 MHz will be delivered to the CPU clock. The CPU will be running at 16 MHz.
Call for RCC_DeInit() in main.c
Figure 10. Call for RCC_DeInit() in main.c.

 

But if you check in the FreeRTOSConfig.h, the SystemCoreClock variable is still pointing to 180 MHz (Figure 11), which is not correct. Now it should point to 16 MHz. That means now you have to update the SystemCoreClock variable. In order to update that variable, either you can directly assign the value for the SystemCoreClock variable, as shown in Figure 12, or you can make use of the SystemCoreClockUpdate function in the system_stm32f4xx.c (Figure 13). Just call this function. It will update the system core clock. It’s better to call the SystemCoreClockUpdate function instead of directly assigning the value.

Assigning value to the SystemCoreClock variable
Figure 11. Assigning value to the SystemCoreClock variable.

 

Direct updation of SystemCoreClock variable
Figure 12. Direct updation of SystemCoreClock variable.

 

Updation of SystemCoreClock variable through a function
Figure 13. Updation of SystemCoreClock variable through a function.

 

Let’s go to the main.c and update the SystemCoreClock variable (Figure 14).

Call for the SystemCoreClockUpdate function
Figure 14. Call for the SystemCoreClockUpdate function.

 

This is a clock initialization. One of the important initializations you have to do in your project is the microcontroller clock settings. Remember that your project will work fine without the deinitialization code, but the CPU clock will be 180 MHz, which is not necessary for this project. If you are ever required to boost the system clock, then you can turn on the PLL, and you can use different frequencies.

Build the project (Figure 15). In Figure 16, you can see that the project was built successfully.

 Building the project
Figure 15. Building the project.

 

Understanding MCU clock configuration Part-2
Figure 16. Result of project build.

 

FastBit Embedded Brain Academy Courses 

click here: https://fastbitlab.com/course1

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.