FreeRTOS(STM32CubeMX)

Introduction to FreeRTOS

insert image description here

FreeRTOS (Real-Time Operating System) is an open source real-time operating system kernel designed for embedded systems and microcontrollers. It was developed by Richard Barry and originally released in 2003. FreeRTOS provides a set of feature-rich real-time scheduling and task management mechanisms that can be used to implement multitasking in embedded systems.

The main goal of FreeRTOS is to provide a lightweight, efficient and portable real-time operating system for embedded applications. It is widely used in various embedded devices and application areas, such as industrial automation, IoT devices, consumer electronics, automotive control systems, medical equipment, etc.
insert image description here

FreeRTOS main features

  1. Real-time scheduling: Allows developers to create multiple tasks and implement real-time scheduling according to the priority of the tasks to ensure that high-priority tasks are executed before low-priority tasks.

  2. Task management: support task creation, deletion, and suspension, as well as communication and synchronization between tasks, through synchronization primitives such as semaphores, queues, mutexes, and event flag groups.

  3. Memory management: Provides memory management functions, and can choose static memory allocation or dynamic memory allocation according to application requirements.

  4. Software Timer: Contains software timers that allow applications to implement periodic or one-time timing functions.

  5. Low-Power Support: Supports Hit-Tick Sleep Mode, which allows the microcontroller to enter a low-power state when idle to save energy.

  6. Portability: FreeRTOS is designed to be highly portable, supporting multiple processor architectures and development toolchains.

Using FreeRTOS, developers can implement complex real-time task processing, enabling embedded systems to perform multiple tasks efficiently and reliably. Its simplicity, portability, and open-source nature make FreeRTOS one of the most popular real-time operating system kernels for embedded development.

When to use FreeRTOS

insert image description here

  1. Multitasking: If the STM32 application needs to perform multiple tasks at the same time, and needs to schedule and manage these tasks in real time, then using FreeRTOS is a good choice. FreeRTOS allows multiple tasks to be created on a single STM32 microcontroller, and real-time scheduling is performed according to the priority of the tasks to ensure that high-priority tasks can be executed in time.

  2. Inter-task communication and synchronization: If different tasks in the application need to communicate and synchronize with each other, such as through message passing or sharing data, then the synchronization primitives provided by FreeRTOS (such as queues, semaphores, mutexes, etc.) can be easily implemented these functions.

  3. Real-time requirements: Some application scenarios have high real-time requirements, and tasks need to be performed under strict time constraints. FreeRTOS is a real-time operating system that can provide reliable task scheduling and response mechanisms to meet these real-time requirements.

  4. Energy saving and low power consumption: FreeRTOS supports Hit-Tick Sleep mode, which allows the microcontroller to enter a low-power state when idle to save energy. If your application needs to run in a low-power mode, FreeRTOS can help you do that.

  5. Complex task processing: For applications involving complex task processing, multi-thread control or state machines, using FreeRTOS can better organize the code structure and improve code readability and maintainability.

  6. Cross-platform portability: If the application needs to run on different hardware platforms, FreeRTOS provides a highly portable feature, making the application code easier to port and reuse.

When STM32 applications require multitasking, inter-task communication and synchronization, real-time guarantees, or energy-saving and low-power functions, FreeRTOS is a very suitable choice to consider. But please note that the requirements of each application are different. For simple applications or resource-constrained scenarios, you can also choose other lightweight task management solutions or write simple polling code to implement functions.

How to use FreeRTOS in STM32CubeMX

Using FreeRTOS with STM32CubeMX is very simple. STM32CubeMX is a graphical configuration tool provided by STMicroelectronics, which is used to quickly generate initialization code and configuration of STM32 microcontrollers. It supports the integration of FreeRTOS into STM32 projects, making it easy to create and manage FreeRTOS tasks and take advantage of its real-time scheduling capabilities.

The following are the basic steps to use FreeRTOS in STM32CubeMX:

  1. Open STM32CubeMX: First, open the STM32CubeMX tool and select the STM32 microcontroller model.
    insert image description here

  2. Configure the system clock and peripherals: According to the project requirements, configure the system clock and peripherals in the "Configuration" tab. This includes configuring clock sources, clock frequencies, GPIO pins, peripheral modules, etc.
    insert image description here
    insert image description here
    insert image description here
    insert image description here

  3. Configure FreeRTOS: In the "Middleware" tab, select "FreeRTOS" and enable it. You can choose to configure parameters such as stack size and task priority of FreeRTOS.
    insert image description here
    insert image description here

  4. Adding tasks: After FreeRTOS is configured, tasks can be added in the "Tasks" tab. Click the "Add Task" button to enter the task name, stack size, priority and other information.
    insert image description here
    insert image description here

  5. Configure task: After selecting the added task, you can configure the properties of the task on the right. The task's priority, stack size, and task's execution function (task function) can be specified.

  6. Generate code: After completing the configuration, click the "Project" button on the STM32CubeMX toolbar and select "Generate Code" to generate the STM32 project code integrated with FreeRTOS.

  7. Write the task function: According to the task function configured in STM32CubeMX, find the function of the corresponding task in the generated code, and write the specific implementation of the task.

  8. Compile and burn: Open the generated code project with your favorite IDE (STM32CubeIDE, Keil, IAR, etc.), compile the code and burn it to the STM32 microcontroller.

FreeRTOS lighting for STM32

  1. Open STM32CubeMX and select the STM32F4 series microcontroller model (for example, STM32F407VG).
    insert image description here

  2. Configure system clock and GPIO: In the "Configuration" tab, configure the system clock and GPIO pins to be used to control LEDs. Suppose we connect an LED to Pin 3 of the GPIOB.
    insert image description here

  3. Configure FreeRTOS: In the "Middleware" tab, select "FreeRTOS" and enable it. You can add tasks in the "Tasks" tab, here add a task to control the LED.
    insert image description here

  4. Add a task: In the "Tasks" tab, click the "Add Task" button. Name the task "LEDTask".
    insert image description here

  5. Configure the task: right-click on the added "LEDTask", select "Open Code Generation", and then enter the function name of the task in "Task Function". Name the task function "vLEDTask".
    insert image description here

  6. Generate code: Click the "Project" button on the STM32CubeMX toolbar and select "Generate Code" to generate code.
    insert image description here

  7. Write the task function: Find the "vLEDTask" function in the generated code, and write the specific implementation of the task.

Code example (based on STM32CubeF4 HAL library):
Add the following code in the main.c file:

//提示:包含了就不用写
#include "main.h"
#include "cmsis_os.h"

// Function prototypes
void vLEDTask(void *argument);

int main(void)
{
    
    
  xTaskCreate(vLEDTask, "LEDTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
  vTaskStartScheduler();
  while (1);
}

void vLEDTask(void *argument)
{
    
    
  for (;;)
  {
    
    
    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
    vTaskDelay(pdMS_TO_TICKS(500));
  }
}

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_56694518/article/details/132155463