FreeRtos[for STM32 learning]
There are various RTOS for microcontrollers, but this site uses FreeRTOS for learning STM32 microcontrollers. We use the conventional type for learning STM32 microcontrollers, not the latest AMAZON type. Since it is for learning, we think it is better to start with something as simple as possible.
Table of contents
What is FreeRTOS?
FreeRTOS is a real-time operating system (RTOS) for MCUs and small microprocessors that is available as open source and can be used in commercial applications at no cost. It is currently maintained by Amazon, Inc.
Although FreeRTOS is free to use, it has the basic functions required of an RTOS and can be used to reduce code size by limiting its use to the bare essentials.
In this site, I would like to explain applications that use task management (priority setting, execution frequency setting, task switching) and queues (queuing) among FreeRTOS functions. (Binary semaphores, counting semaphores, and mutexes for inter-task communication are not covered.)
Preparation and configuration for using FreeRTOS (for MCU, only at the beginning)
All the necessary elements for FreeRTOS are contained under the "Source" folder. Copy the "Source" folder into your project and pass through the path to this folder and all subfolders belonging to it in the application's project settings.
Recent developments in FreeRTOS:
STM32CubeIDE, the latest development environment for STM32 MCUs, can automatically incorporate FreeRTOS by following the steps of the STM32CubeMX automatic generation tool.
However, the automatically generated code is based on the abstracted CMSIS (ARM Cortex standard) API (Application Programming Interface), just as the peripheral driver has been abstracted to HAL, and the RTOS code is not similar to the conventional FreeRTOS.
It is not necessary to use this new RTOS until you are familiar with it, and it seems to be better to deepen your understanding with the conventional FreeRTOS, which has a wealth of information.
Task management mechanism using FreeRTOS
Let's take a quick look at the structure of a program employing FreeRTOS.
The core part of an RTOS that manages tasks is called the kernel.
The following is an example of parallel processing (multitasking) in an application program by registering process A and process B in their respective tasks and starting the kernel (task scheduler start).
When using FreeRTOS, first register the tasks to be used. Process A and Process B are registered as task functions (TaskA and TaskB) and described in an infinite loop while(1) in each function. In this example, process A is described in the TaskA function and process B in the TaskB function.
Each process is in an infinite loop, but the RTOS kernel switches tasks by saving and restoring the CPU state. This switching process is called a context switch.
Use xTaskCreate() to register a task.
Execution Example of xTaskCreate() :
xTaskCreate(TaskA, (signed portCHAR *)”TaskA”, 192, NULL,1,NULL);
The first argument of the function is the name of the task function, and the second argument is the name of the debugging task, but as in the example, the task name is fine. The third argument is the size of the stack that will contain the task's data, the fourth argument is a pointer to a parameter to be passed to the task, the fifth argument is the priority, and the sixth argument is the handler used to manage the task.
It is difficult to explain the definition of arguments in words, so I will give an example.
Some of these arguments are related to parameters set in the basic configuration header file FreeRTOSConfig.h, which will be explained later with specific examples.
The third argument is the amount of stack space to store data used by the task and should be sufficient to prevent overflow. The minimum recommended stack area is 128 words (512 bytes), the size should be specified in words. And the standard is 192 words (768 bytes), which is usually stable. However, the larger the stack capacity, the more stable and secure it is, but if the number of tasks increases, the RAM capacity of MCU must not be exceeded. Adjust the amount while actually running the program.
Arguments 4, 5, and 6 will be explained later with examples.
After registering tasks, the vTaskStartScheduler function is executed to start the RTOS kernel (start the scheduler).
Execution Example of task Registration Function: xTaskStartScheduler();
Insert an infinite loop while(1) at the end of the main function. If necessary, error handling and other operations can be written inside this infinite loop. When the kernel starts and a task is executed, the task automatically switches (context switch) and is executed alternately in a pre-defined switching period (time slice).
The basic structure of FreeRTOS task management has been described so far in the figure. If it becomes possible to set task priorities, task execution intervals, and enable or disable tasks in actual applications, more complex and multifunctional systems can be realized easily and flexibly.
Initial configuration for using FreeRTOS: FreeRTOSConfig.h
Parameters are initially specified in the header file FreeRTOSConfig.h. You can also specify whether or not functions are used, so you can reduce the size by limiting the configuration to only those functions that are necessary. This configuration file should be placed directly under the "Source" folder. The following is an explanation of the most important parameters.
An interrupt handler name change definition has been added at the beginning of this file because the handler name used by FreeRTOS and the handler name used by ARM Cortex-M3 are different. It has been modified to the standard name used in STM32.
Since the clock is 72 MHz on the instructional board, configCPU_CLOCK_HZ is set to 72000000. To set the time slice unit to 1ms, configTICK_RATE_HZ specifies 1000 ticks per second (tick:1ms). This will cause a context switch to switch tasks every 1 ms.
The configMAX_PRIORITIES is set to 5 because we want to set the task priority to 5 levels. If you specify a larger value than necessary, it will consume more memory. configMINIMAL_STACK_SIZE should be 128. configTOTAL_HEAP_SIZE specifies the value to be reserved as the total RAM size to be allocated to RTOS. Specify the value considering the number of tasks to be registered and the amount of space to be allocated to each task.
The last configMAX_TASK_NAME_LEN (maximum task name length) should be 16 (16 characters).
The memory capacity allocated to RTOS is a little extravagant because 64k RAM is sufficient for the teaching material board, but if the RAM is small, you need to adjust it severely. In applications that use RTOS, most of the RAM capacity will be allocated to RTOS, so be aware of this setting in accordance with the specifications of the microcontroller.
Now that the initial setup of FreeRTOS is complete, we will explain how tasks can be managed according to the application.