FreeRTOS (task scheduling)

Task scheduling

What is task scheduling?

The scheduler uses relevant scheduling algorithms to determine which task currently needs to be executed. The function to start task scheduling in FreeRTOS is vTaskStartScheduler(), but it is encapsulated as osKernelStart() in CubeMX.

What are the task scheduling rules of FreeRTOS?

FreeRTOS is a real-time operating system, and the scheduling rules it pursues are:

1. High priority preempts low priority tasks, and the system always executes the highest priority task (i.e., preemptive scheduling)

2. Rotational scheduling of tasks with the same priority (i.e., time slice scheduling)

There is another scheduling rule called coroutine scheduling, but the official has made it clear that it will not be updated. It is mainly used on small-capacity chips and is not used much.

Preemptive scheduling operation process 

Summary: 1. High-priority tasks are executed first;

2. High-priority tasks do not stop and low-priority tasks cannot be executed;

3. The preempted task will enter the ready state 

Time slice scheduling running process

 Summarize:

1. Tasks with the same priority are executed in turn and time slices are transferred;

2. The size of a time slice depends on the tick timer interrupt period;

3. Note that unused time slices will not be used again. The next time Task3 is executed, it will still run according to the clock beat of a time slice.

Task status

There are 4 states of tasks in FreeRTOS:

  • Running Running state When a task is in the actual running state, it is called the running state, that is, the CPU usage rights are occupied by this task (only one task is in the running state at the same time).
  • Ready Ready state Tasks in the ready state refer to those tasks that can run (not blocked or suspended), but are not currently running because tasks of the same priority or higher priority are running.
  • Blocked Blocked state If a task is in a state due to delay, or waiting for a semaphore, message queue, event flag group, etc., it is called a blocked state.
  • Suspended Suspended state is similar to suspension. The specified task is suspended by calling the function vTaskSuspend(). After the suspension, the task will not be executed. Only the function xTaskResume() can be called to resume the task from the suspended state.

 Summarize:

1. Only the ready state can be transformed into the running state

2. In order to run tasks in other states, they must first be transformed into the ready state.

Task comprehensive small experiment

Experimental requirements: Create 4 tasks: taskLED1, taskLED2, taskKEY1, taskKEY2,

The task requirements are as follows:

taskLED1: flash LED1 at an interval of 500ms;

taskLED2: flash LED2 at an interval of 1000ms;

taskKEY1: If taskLED1 exists, delete taskLED1 after pressing KEY1, otherwise create taskLED1; taskKEY2: If taskLED2 is running normally, suspend taskLED2 after pressing KEY2, otherwise restore taskLED2

cubeMX configuration

 Code

 

 

 

Guess you like

Origin blog.csdn.net/m0_70987863/article/details/131690899