Principle and Implementation of Embedded Real-Time Operating System Based on FreeRTOS
FreeRTOS is a free embedded real-time operating system with open source. The kernel can better understand the implementation principle of embedded operating system. This paper mainly describes the task scheduling mechanism, time management mechanism and task management mechanism in FreeRTOS system. The implementation principle of the memory allocation strategy, and pointed out the advantages and disadvantages of FreeRTOS in the application.
In the embedded field, embedded real-time operating systems are getting more and more widely used. The embedded real-time operating system (RTOS) can make more reasonable and efficient use of CPU resources, simplify the design of application software, shorten system development time, and better ensure the real-time and reliability of the system. Since RTOS needs to occupy certain system resources (especially RAM resources), only a few real-time operating systems such as μC/OS-II, embOS, salvo, and FreeRTOS can run on small RAM microcontrollers. Compared with C/OS-II, embOS and other commercial operating systems, FreeRTOS operating system is a completely free operating system with open source, portable, scalable, and flexible scheduling strategy, which can be easily ported to various microcontrollers. The latest version is 2.6.
1, FreeRTOS operating system featuresAs a lightweight operating system, FreeRTOS provides functions such as task management, time management, semaphores, message queues, memory management, and logging functions to meet the needs of smaller systems. The FreeRTOS kernel supports a priority scheduling algorithm. Each task can be given a certain priority according to the degree of importance. The CPU always runs the task with the highest priority in the ready state. The FreeRT0S kernel supports the rotation scheduling algorithm at the same time. The system allows different tasks to use the same priority. When no higher priority tasks are ready, the same priority tasks share CPU usage time.
The FreeRTOS kernel can be set to a deprived kernel or an inelastic core depending on user needs. When FreeRTOS is set to a deprived kernel, the high-priority task in the ready state can deprive the CPU of low-priority tasks, thus ensuring that the system meets real-time requirements; when FreeRTOS is set to an inelastic core The high-priority task in the ready state can only be run after the current running task actively releases the usage right of the CPU, which can improve the operating efficiency of the CPU.
2. Principle and implementation of FreeRTOS operating system 2.1 Implementation of task scheduling mechanism The task scheduling mechanism is an important concept of the embedded real-time operating system and its core technology. For the deprivatable core, once the high priority task is ready, the CPU usage rights of the lower priority task can be deprived, and the real-time response capability of the system is improved. Unlike μC/OS-II, FreeRTOS has no restrictions on the number of system tasks. It supports both the priority scheduling algorithm and the rotation scheduling algorithm. Therefore, FreeRTOS uses a doubly linked list instead of a task-ready table to perform task scheduling. The system-defined linked list and linked list node data structures are as follows:
Typedef struct xLIST{ //Define the linked list structure
Unsigned portSHORPT usNumberOfItems;
//usNumberOfItems is the length of the linked list, 0 means the list is empty
volaTIle xLisTItem * pxHead;//pxHead is the head pointer of the linked list
volaTIle xLisTItem * pxIndex; //pxIndex points to the current node of the list
Volatile xListItem xListEnd; //xListEnd is the end of the list
}xList;
Struct xLIST_ITEM { //Define the structure of the linked list node
Port Tick type xItem Value;
//xItem Value value is used to implement time management
//port Tick Type is the hour hand beat data type.
/ / can be selected as 16 or 32 bits as needed
Volatile struct xLIST_ITEM * pxNext;
/ / point to the previous node of the linked list
Void * pvOwner; / / point to the task control block where the linked list node is located
Void * pvContainer; / / point to the linked list of the linked list node;
Each task in FreeRTOS corresponds to a task control block (TCB) whose definition is as follows:
Typedef struct tskTaskControlBlock {
portSTACK_TYPE * pxTopOfStack;
/ / point to the end of the task stack
portSTACK_TYPE * pxStack;
/ / point to the beginning of the task stack
Unsigned portSHORT usStackDepth; / / define the stack depth
Signed portCHAR pcTaskName[tskMAX_TASK_NAME_LEN];//task name
Unsigned portCHAR ucPriority; //task priority
xListItem xGenericListItem;
/ / used to insert TCB into the ready list or wait for the linked list
xListItem xEventListItem;
/ / Used to insert TCB into the event list (such as message queue)
Unsigned portCHAR ucTCBNumber; / / used for recording function
}tskTCB;
The FreeRTOS definition ready task list is xList pxReady—TasksLists[portMAX_PRIORITIES]. Where portMAX_PRIORITIES is the maximum priority defined by the system. If you want to make the task with priority n enter the ready state, you need to insert the node xGenericListltem in the TCB corresponding to this task into the linked list pxReadyTasksLiStS[n], and also point the pvContainer in the xGenericListItem to pxReadyTasksLists[n].
When task scheduling is performed, the scheduling algorithm first implements priority scheduling. The system looks for the first priority of usNumberOfItems that is not 0 according to the order of priority from high to low. The priority is the current highest ready priority, and priority scheduling is implemented accordingly. If there is only one ready task under this priority, the ready task enters the running state; if there are multiple ready tasks under this priority, the rotation scheduling algorithm is needed to implement multi-task rotation.
If the rotation scheduling algorithm is executed under the priority n, the system first obtains the next node pointed to by the current node by executing (pxReadyTasksLists[n])→pxIndex=( pxReadyTasks-Lists[n ]) → pxlndex→pxNext statement, and then The corresponding task control block is obtained by the pvOwner pointer of the node, and finally the task corresponding to the task control block enters the running state. It can be seen that in FreeRTOS, the switching time between the same priority tasks is one clock tick period.
Taking Figure 1 as an example, let the maximum number of tasks of the system be pottMAX_PRIORITIES. When task scheduling is performed at a certain time, pxReadyTasksLists[i] is obtained. usNumberOfItems=O(i=2...portMAX_PRIORITIES) and pxReadyTasksLists[1]. usNumberOfItems=3. Therefore, the kernel knows that the current highest ready priority is 1, and three tasks have entered the ready state under this priority. Since there are multiple ready tasks under the highest ready priority, the system needs to execute the rotation scheduling algorithm to realize the task switching; the pointer pxlndex knows that the task l is the current task, and the pxNext node of the task l points to the task 2, so the system points the pxIndex to the task 2 And perform task 2 to achieve task scheduling. When the next clock tick comes, if the highest ready priority is still 1, as shown in the figure, the system will point pxIndex to task 3 and perform task 3.
Figure 1 Scheduling of task scheduling
To speed up task scheduling, FrecRTOS tracks the highest priority currently available through the variable ucTopReadyPriotity. When a task is added to the ready list, if the priority of this task is higher than ucTopReadyPriority, the priority of this task is given to ucTopReadyPriority. Thus, when priority scheduling is performed, the scheduling algorithm does not start from portMAX_PRIORITIES but from ucTopReady-Priority. This speeds up the search and reduces the core turn-off time.
To speed up task scheduling, FrecRTOS tracks the highest priority currently available through the variable ucTopReadyPriotity. When a task is added to the ready list, if the priority of this task is higher than ucTopReadyPriority, the priority of this task is given to ucTopReadyPriority. Thus, when priority scheduling is performed, the scheduling algorithm does not start from portMAX_PRIORITIES but from ucTopReady-Priority. This speeds up the search and reduces the core turn-off time.
2.2 Implementation of time managementThe typical time management function provided by FreeRTOS is vTaskDelay(), which is called to delay the task for a specific period of time. In FreeRT0S, if a task delays xTicksToDelay clock ticks, the system kernel will add the total number of clock ticks that have been running on the current system (defined as xTickCount, 32-bit length) plus xTicksToDelay to get the number of clock ticks for the next wake-up of the task xTimeToWake . Then, the kernel deletes the task control block of this task from the ready list, assigns xTimeToWake as the node value to the xItemValue of the task, and then inserts the task control block into different linked lists in order according to the value of xTimeToWake. If xTimeToWake > xTickCount, that is, there is no overflow in the calculation, the kernel inserts the task control block into the pxDelayedTaskList list; if xTimeToWak e< xTickCount, that is, an overflow occurs during the calculation, the kernel inserts the task control block into the pxOverflowDelayed-Taskust list.
Each time a clock beat occurs, the kernel increments the current xTick-Count by one. If the result of xTickCount is 0, an overflow occurs, and the kernel will use pxOverflowDelayedTaskList as the current linked list; otherwise, the kernel uses pxDelaycdTaskList as the current linked list. The kernel compares xTickCotlrtt with xTimcToWake for each node in the list. If xTick-Count is equal to or greater than xTimeToWake, indicating that the delay time has expired, the task should be removed from the waiting list and added to the ready list.
It can be seen that, unlike μC/OS-II, FreeRTOS adopts the "add" method to realize time management. The advantage is that the execution time of the time beat function is basically independent of the number of tasks, and the execution time of the OSTMcTick() of μC/OS-II is proportional to the number of tasks established in the application. Therefore, when there are many tasks, the time management mode adopted by FreeRTOS can effectively speed up the execution of the clock tick interrupt program.
Fiber Optic Cable,Mpo Connector,Splicing Fiber Optic Cable,St Fiber Connector
CIXI LANGUANG PHOTOELECTRIC TECHNOLOGY CO..LTD , https://www.cxblueray.com