Applicable to FreeRTOS beginners, FreeRTOS overall knowledge framework

Write in front: I learned FreeRTOS for a period of time because of the actual use needs. From the market share of FreeRTOS, there should be a lot of online information, but in the learning process, especially when there are problems, there are not many really useful information.

So there are two purposes for writing this article. 1: Easy for beginners to get started quickly; 2: Make a phased summary of your learning at this stage. If you have time, you will share your learning achievements more.

Because the author is just a beginner for a short time, it is inevitable that there will be omissions and errors, for reference only. At the same time, we also welcome criticism and advice from leaders. Generous comments:)

Opening: dry goods

Based on several days of learning experience, the author summarizes the knowledge framework of FreeRTOS. The corresponding part will be explained below.

1, Mission
1. Basic attributes
1.1 priority
1. Each task is assigned one from 0 to configmax_ Priority of priorities-1. The lower the priority number, the lower the priority of the task;

2.High priority preemption low priority:

   When a task A Running, another task B(Priority over A)When the blocking time expires or the event trigger is in the ready state, then B Will from A The preemptive processor, B Start running, A Stop operation;

   FreeRTOS The scheduler ensures that the high priority tasks in the ready state or running state obtain processor usage rights, in other words, the highest priority tasks in the ready state will run.

3. Time slice rotation:

  When macro configUSE_TIME_SLICING When defined as 1, multiple tasks can share a priority, and the number is unlimited. At this time, tasks with the same priority in the ready state will use the time slice rotation scheduler to obtain the running time.

1.2 task control block / task stack
1) Task control block:

FreeRTOS Each task has some attributes that need to be stored, FreeRTOS These attributes are grouped together and represented by a structure called task control block.

Properties: task name, priority, task stack size, task handle, etc.

2) Task stack:

FreeRTOS The reason why the operation of a task can be restored correctly is that there is a task stack under escort, and the task scheduler will restore the current task to the scene when switching tasks (CPU Register value, etc )Save it in the task stack of this task. When this task runs next time, it will first restore the scene with the values saved in the stack,

After the site is restored, the task will continue to run from the place where it was interrupted last time.

2. Status
1) Operating state:

When a task is running, it is said that the task is running. The running task is the task currently using the processor. If you use a core processor, there will always be only one task running at any time.

2) Ready state:

Tasks in ready status are those that are ready to run (these tasks are not blocked or suspended), but the tasks in ready status are not running because a task with the same priority or higher priority is running!

3) Blocking state:

If a task is currently waiting for an external event, it is said to be in the blocking state. For example, if a task calls the function vtask delay(), it will enter the blocking state until the delay cycle is completed.

When a task is waiting for a queue, semaphore, event group, notification or mutually exclusive semaphore, it will also enter the blocking state. There will be a timeout for the task to enter the blocking state. When the timeout is exceeded, the task will exit the blocking state, even if the waiting event has not yet arrived!

4) Suspended state

Like the blocking state, a task entering the suspended state cannot be called by the scheduler to enter the running state, but the task entering the suspended state does not have a timeout. A task enters and exits the suspended state by calling the functions vtask suspend() and xtask resume()

Switching relationship between tasks:

3. Operation

Create relative to delete; suspend relative to restore

2, Mechanism
1. Queue

1) All communication and synchronization mechanisms are implemented based on queues

2) Queues can pass not only arrays, but also structures

2. Semaphore

Semaphores are queues with a depth of 1.

3. Task notification

Task notification replaces semaphores, message queues, event flag groups, etc. using task notification will be more efficient.

4. Low power consumption mode

4.1 low power consumption mode supported by stm32

1) Sleep: sleep

  stay SLEEP In mode, only the kernel stops working and the peripherals are still running.

  In entry SLEEP All interrupts after mode('External interrupt, serial port interrupt, timer interrupt, etc')Can wake up MCU,To exit SLEEP pattern.

2) Stop: stop

  stay STOP In mode, the kernel stops working and all clocks(as HCLK, PCLK1, PCLK2 etc.)Also stop working, that is, all peripherals stop working. Here is one thing to pay special attention to. At this time SYSTICK Will also be stopped. Of course, in our products RTC It continues to run because its clock source is external 32.768K Crystal oscillator.

  In entry STOP After mode, only external interrupts( EXTI)To wake up MCU because RTC The interrupt is hung on the external interrupt line, so RTC Interruptions can also wake up MCU). 

3) Standby: standby

  stay STANDBY In mode, the kernel, all clocks, and backup 1.2V The power supply stops working.

  from STANDBY After waking up in mode, the system is equivalent to performing a reset operation, and the program will start from scratch.

4.2 low power consumption of hook function implementation of idle tasks

1) Hook function

Through the idle task hook function (or callback, hook, or call back), you can directly add application related functions to the idle task. The idle task hook function will be automatically called by the idle task every cycle.

Usually, the idle task hook function is used to:

Execute low priority, background or function code that needs continuous processing.

Test the system processing margin (idle tasks can only be executed when all other tasks are not running, so measuring the processing time occupied by idle tasks can clearly know how much surplus processing time the system has)

Configure the processor to low power consumption mode - provide an automatic power saving method, so that when there is no application function to be processed, the system automatically enters the power saving mode.

FreeRTOS reduces energy consumption by setting the processor to the low-power mode when processing idle tasks. Generally, low-power related processing is performed in the hook function of idle tasks, such as setting the processor to enter the low-power mode (the three methods mentioned in 4.1 above), turning off other peripheral clocks, reducing the main frequency of the system, etc.

4.3 tickless mode

FreeRTOS The low power consumption mode provided by the system turns off the system beat interrupt when the processor enters the idle task cycle(Tick timer interrupt),The processor wakes up from the low power mode only when other interrupts occur or other tasks need to be processed.

5. Lists and list items

The list is FreeRTOS A data structure in, conceptually similar to a linked list, which is used to track FreeRTOS Tasks in.

6. Memory management

Memory management is a basic part of a system, FreeRTOS Memory management is widely used in. For example, creating tasks, semaphores, queues, etc. will automatically apply for memory from the heap. User application layer code can also FreeRTOS Provides a memory management function to request and free memory.

3, API function

The opening figure simply lists several API functions, such as task creation; Task suspended; Queue creation; Semaphore creation, etc.

Write it at the end: more detailed descriptions of each part, usage methods and routines will be written later

Thanks for reading:)

Welcome to the official account [Liu Shan Shan], dry cargo is full.

Posted on Wed, 01 Dec 2021 08:47:53 -0500 by ErcFrtz