FreeRTOS

This tutorial demonstrates: Real-time control of RGB lights, leds, and active buzzers with FreeRTOS.

1、software-hardware

2、Brief principle

2.1、Hardware schematic diagram

image-20231115180830896

2.2、Physical connection diagram

image-20231115181055917

2.3、Principle of control

Bare metal systems:Most of our cases run on bare metal systems, where programs are usually inside the main loop or interrupts; Multi-task system:The real-time operating system divides the program into independent applets, each applet is a task, each task is independent, non-interfering with each other, and has its own priority, which is configured by the operating system.

FreeRTOS(Real Time Operating System)

Because real-time operating system involves a lot of content, the tutorial will briefly introduce the website learning materials location and real-time operating system involved in the concept.

Official website Learning websitehttps://www.freertos.org/RTOS.html

The official online resources provide FreeRTOS books, documentation about the FreeRTOS kernel, the FreeRTOS API reference manual, and more.

image-20231120152637278

If you enter from the official website by yourself, you should click "Getting Strated" → "Getting started with the FreeRTOS kernel: Learn More" in turn.

image-20231120152909332

Basics of real-time operating systems

FreeRTOSfeature
TaskThe basic unit of FreeRTOS
SchedulerManage the execution order and switching of tasks
SemaphoreIt is used for synchronizing and mutually exclusive access to shared resources between tasks
Event FlagsUsed for event notification and synchronization between tasks
QueueIt is used for data transfer and communication between tasks.
MutexIt is used to implement mutually exclusive access to resources, ensuring that only one task can access a shared resource at a time.
TimerIt can be used to periodically trigger events or perform tasks

We can use the CubeMX plugin to initialize and configure FreeRTOS, and configure FreeRTOS components such as tasks, timers, semaphores, and message queues according to our requirements. The following table is the name and function of the newly created tasks during the project configuration:

Taskfeature
myTask_RGBControl RGB
myTask_BeepControl the buzzer
myTask_KEYCheck the KEY1 status
myTask_LEDControl LED
RGB(Schematic name)Control pinfeature
LRGB-RPG1Control the red light display of the RGB light on the left
LRGB-GPE7Control the green display of the RGB light on the left
LRGB-BPG2Control the blue light display of the RGB light on the left
RRGB-RPE2Control the red light display of the RGB light on the right
RRGB-GPE3Control the green display of the RGB light on the right
RRGB-BPE4Control the blue light display of the RGB light on the right
LED(Schematic name)Control pinfeature
LED1PG14控制LED1亮灭
LED2PG15控制LED2亮灭
KEY(Schematic name)Control pinfeature
KEY1PG3Change the KEY1 pin level state
KEY2PG4Change the KEY2 pin level state
KEY3PG5Change the KEY3 pin level state
buzzer(Schematic name)Control pinfeature
BuzzerPG12Control the buzzer to sound

3、Engineering configuration

Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process

3.1、Notes

Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content

The project configuration part, which is not omitted, is the key point to configure in this tutorial.

3.2、Pin configuration

You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

image-20231115184703726

image-20231115184825359

The reference is changed to TIM1

image-20231115185001936

The four tasks differ only in task name and task function entity

image-20231115185212854

Task optionsfeature
Task NameTask name
PrioritySet priorities
Stack Size (Words)Heap space
Entry FunctionTask function entity
Code Generation OptionCode generation configuration, default is weak generation task entity
ParameterTask parameters
AllocationYou can choose between Dynamic allocation or Static allocation
Buffer NameStatically allocated buff name
Control Block NameThe statically allocated block name

4、Main Function

This section mainly introduces the functional code written by users. Detailed code can be viewed by opening the project file provided by us. .

4.1、User function

function:Task_Entity_RGB

Function prototypesvoid Task_Entity_RGB(void)
Functional DescriptionRGB task entity function: controls the left and right RGB light color switching
Input parametersNone
Return valueNone

function:Task_Entity_BEEP

Function prototypesvoid Task_Entity_BEEP(void)
Functional DescriptionBuzzer task entity function: Control the buzzer to sound
Input parametersNone
Return valueNone

function:Task_Entity_LED

Function prototypesvoid Task_Entity_LED(void)
Functional DescriptionLED task entity function: Control the left and right RGB light color switching
Input parametersNone
Return valueNone

function:Task_Entity_KEY

Function prototypesvoid Task_Entity_KEY(void)
Functional DescriptionKey task entity function: switch the state of buzzer and LED light
Input parametersNone
Return valueNone

4.2、HAL library function parsing

The HAL library functions that were covered in the previous tutorial will not be covered

function:MX_FREERTOS_Init

Function prototypesvoid MX_FREERTOS_Init(void)
Functional DescriptionTasks to initialize FreeRTOS: Create and initialize components such as tasks, queues, mutexes, etc
Input parametersNone
Return valueNone

function:osKernelStart

Function prototypesosStatus osKernelStart (void)
Functional DescriptionStart the scheduler of the RTOS kernel and start executing tasks
Input parametersNone
Return valueosStatus:The result of the function execution

function:osDelay

Function prototypesosStatus osDelay (uint32_t millisec)
Functional DescriptionThe operating system's latency function
Input parametersThe delay (in ms)
Return valueosStatus:The result of the function execution
TipsPause the thread, execute another thread, and wait until the delay is complete

5、Experimental phenomenon

After downloading the program successfully, press the RESET button of the development board to observe the phenomenon of the development board

Phenomenon:

Left and right RGB lights:toggle different colors all the time;

KEY1 button:The buzzer sound switch, sound and no sound interval of 1s (press once to open, then press once to close);

KEY2 button:The switch for LED1 (press once to be on and again to be off);

KEY3 button:The LED2 switch (press once to be on and again to be off).