Friday 6 July 2018

Enabling FreeRTOS on STM32 Nucleo

Do visit and support me by subscribing to my youtube channel - https://www.youtube.com/channel/UC5zezp0q2gn4oAdKJ4svsNA

In my previous post, I had shown how to use cubemx and Atollic TrueStudio to configure and generate HALs and write a simple application using super loop architecture. In this blog, I shall show you how to use enable FreeRTOS on CUBEMX, create basic tasks and run it on the hardware.

Lets define the goal of application to be built first. Here the goal assumed is
1) The application will be running under a task managed by FreeRTOS kernel.
2) The application shall toggle the onboard LED- LD2 when the onboard button-B1 is pressed.
3) The application shall print the button press status on UART @ 115200 baudrate.

Disclaimer - This post will show only the steps to be followed to enable FreeRTOS using CubeMX. The concepts of RTOS will not be discussed here. These concepts will be explained and demonstrated in future posts.

Lets switch back to our task.

The first 3 steps are same as the first three steps of my previous post.

Go ahead and follow till step 3 under Configuration of Button, LED and UART Peripheral in STM32CUBEMX from my previous post.

Step 4- Now under configuration tab, at the left corner you see a node by name Middlewares under which you see FREERTOS. Go ahead and select that. The moment you enable that option you will see that the FREERTOS appears under the MIDDLEWARES BOX in the center. The same has been shown below.

Figure 1

Step 5- Click on the FREERTOS 'BOX' . When you select 'Task and Queues' Tab, you will be presented the below window


Figure 2
As you can see the tool has already created a default task. Click on 'defaultTask' and you will be presented the below window.

Figure 3
The window shows the properties of the default task. As shown above, the properties are self explanatory. The default task's entry function StartDefaultTask should have the implemetation of the intended application's goal. The stack size if of 128 words. In other words, the stack size is 256 bytes. Click OK to close the edit task window. 

To add more task, you can click on Add option in Tasks table and configure the task parameters shown in figure3.

Click OK again on FREERTOS Configuration window to close it.

Step 6- Go to project menu and select Settings. Give a project name and a suitable path. Also select toolchain option as TrueSTUDIO, ensure that 'Generate Under Root' is selected and click OK. For example

Figure 4
Step 7 - Go to project and select Generate Code option. After completion of code generation, click on Open project.
Step 8 - Let us examine the main function the tool has generated for us.
As marked below in black you can see that the tool has put initialization code of the FREERTOS Kernel and has created a task using CMSIS APIs. If confiugred correctly, the code execution control should never go to the section marked in RED. The same has been mentioned in the comment as well.

Figure 5

These CMSIS APIs intenally get mapped to FREERTOS APIS. To know more refer this LINK. Note that this document refers to F4 series MCU but the CMSIS & FreeRTOS concept are valid for F0 as well.

Step 9- As shown in figure 5, the StartDefaultTask function is given as an argument when to osThreadDef. This is the same name which was given in figure 3, while the task was created. This function is called from the OS kernel, when the kernel is started by the osKernelStart API. Lets implement our intended functionality in the StartDefaultTask function as shown below. The entire implementation should be within the infinite for loop.

Figure 6


Lets go ahead and build it.

Step 10- After successful build, start debug session by pressing F11. Place a breakpoint in the if condition by double clicking the line number. Connect the terminal and select resume option.
When you press the onboard blue button you can see that the code breaks at the break point you have placed. Select resume again. You can see that the MCU has printed "BUTTON PRESSED" message at 115200 baud, and the LED has toggled from OFF to ON. Remove break point and press & hold the blue button. You can see that the LED toggles its state and the see messages on terminal every 500 milliseconds (400 ms + 100 ms). 

Figure 7
Two things worth noting are
1) The delay function. Here we are using osDelay function instead of HW provided HAL_Delay function to create delays(refer the same implementation done in my previous post). This is because when a task needs a delay, it must inform the RTOS that it is going to sleep for some duration and thus will not be needing the CPU. The RTOS can then allocate resources to other tasks till the required duration is expired and the RTOS can schedule back the task. This is achieved by osDelay function, which inturn calls the FreeRTOS API vTaskDelay function as shown below.

Figure 8
2) Observe the call stack. You can clearly see that the function StartDefaultTask is called from the OS. As we will discuss in future blogs, the RTOS will be having a 'TIME TICKER' which periodically interrupts the CPU and invokes the scheduler. The scheduler inturn finds out which ready task to be run and calls the task's function. 

This concludes the tutorial.  I have uploaded the project in the link if in case you are not able to follow above mentioned steps. 

What next? Go ahead and configure another GPIO as output, connect an LED to it, create a task and try toggling it. 

We are now equipped with all the basic tools to start our journey into deep understanding of various RTOS concepts and its demonstration on FREERTOS. These include


  1. task priorites
  2. semaphores
  3. mutex
  4. interrupt handling
  5. Preemptive and Cooperative scheduling.

Also since we have setup our enviornment we will also be learning some advance concepts like


  1. Interfacing SD card and incorporating file system
  2. Understanding Linker scripts
  3. Running code from RAM
  4. Bootloaders
  5. Updating your FW by implementing a simple bootloader

Watch out for these updates.

If you have any comments, feel free to drop them in the comment section.

Happy Learning!

No comments:

Post a Comment