FreeRTOS Lecture 20 – Understanding “SystemCoreClock” variable

 

Understanding “SystemCoreClock” variable 

 

 

While building the project, we faced the issue “SystemCoreClock” undeclared (Figure 1). That is actually there in the FreeRTOSConfig.h, which you included.

Understanding "SystemCoreClock" variable
Figure 1. Error encountered while building the project.

 

Now in Figure 2, you can see SystemCoreClock, which is a symbol. If you trace this symbol, it will take you to the microcontroller-specific file, i.e., system_stm32f4xx.c (Figure 3) given by the ST.

The system_stm32f4xx.c file contains some initialization functions in order to initialize the microcontroller clock and some other microcontroller-specific features. You can find this file under the source (Figure 4).

The SystemCoreClock is initialized to 180MHz, as shown in Figure 5. We will discuss more on this later.

Understanding "SystemCoreClock" variable
Figure 2. SystemCoreClock symbol in FreeRTOSConfig.h.

 

Understanding "SystemCoreClock" variable
Figure 3. system_stm32f4xx.c file given by the ST.

 

Understanding "SystemCoreClock" variable
Figure 4. system_stm32f4xx.c file in the source.

 

Understanding "SystemCoreClock" variable
Figure 5. Initializing SystemCoreClock to 180MHz.

 

But the problem we are facing here is, you have to extern the SystemCoreClock variable in FreeRTOSConfig.h because that variable is defined in the system_stm32f4xx.c file. So, you have to extern the SystemCoreClock variable, which we already did, as shown in Figure 6.

Now just copy the extern statement shown in Figure 7 and paste it, as shown in Figure 8.

Making SystemCoreClock variable as extern
Figure 6. Making SystemCoreClock variable as extern.

 

Copying the extern statement
Figure 7. Copying the extern statement.

 

Pasting extern statement in declaration section
Figure 8. Pasting extern statement in declaration section.

 

After that, try to rebuild the project (Figure 9).

Building the project
Figure 9. Building the project.

 

This time there are no compiler errors, but the linker is giving some errors, as shown in Figure 10. That is an undefined reference to vApplicationTickHook and an undefined reference to vApplicationStackOverflowHook.

Linker errors encountered while building the project
Figure 10. Linker errors encountered while building the project.

 

vApplicationTickHook and vApplicationStackOverflowHook are the hook functions of the FreeRTOS, which you need to implement in the main.c. This error can be easily solved by going into the FreeRTOSConfig.h and switching off that feature. Now let’s switch off that feature, causing an error.

In the FreeRTOSConfig.h, you will find various macros in order to configure the FreeRTOS source code.

In Figure 11, you can see various macros that start from the word config, which is actually a configurable item. configUSE_PREEMPTION is a configurable item to configure the preemption. If you make it 1, then preemption will be enabled. If you make it 0, preemption will be disabled. 

Various macros to configure the FreeRTOS source code
Figure 11. Various macros to configure the FreeRTOS source code.

 

Now let’s use this hook configurable items. While building, we are facing an issue that is an undefined reference to ApplicationTickHook.

Now let’s understand the meaning of this error. This error is actually telling us that please implement a function called a vApplicationTickHook in your main.c.

But our main.c is empty (Figure 12). We have not implemented any hook functions. Because we don’t need that for the HelloWorld application, so let’s make the tick hook and idle hook macro as zero, as shown in Figure 13.

main.c file
Figure 12. main.c file.

 

Understanding "SystemCoreClock" variable
Figure 13. Initializing idle hook and tick hook.

 

After that, we are facing an issue with a stack overflow. Check for stack overflow. Its initial value is 2 (Figure 14). Now let’s change that into 0 (Figure 15).

Understanding "SystemCoreClock" variable
Figure 14. Initial value of stack overflow.

 

Understanding "SystemCoreClock" variable
Figure 15. Modified stack overflow value.

 

Let’s build the project. Now the project is built successfully (Figure 16). You have successfully built a project, which consists of FreeRTOS source code.

Understanding "SystemCoreClock" variable
Figure 16. Result of building project.

 

Now you can use your main.c in order to create various threads or tasks to take the benefits of FreeRTOS services like task management, task creation, task deletion, queue management, semaphores, Mutexes, etc.

Now you can implement all these things from main.c, and you can play with the FreeRTOS source code.

 

In the following article, let’s see what is the task.

 

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.