In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "what is the free task of FreeRTOS", the content is detailed, the steps are clear, and the details are handled properly. I hope this article of "what is the idle task of FreeRTOS" can help you solve your doubts.
When the RTOS scheduler starts working, in order to ensure that at least one task is running, idle tasks are automatically created with the lowest priority (priority 0).
XReturn = xTaskCreate (prvIdleTask, "IDLE", configMINIMAL_STACK_SIZE, (void *) NULL, (tskIDLE_PRIORITY | portPRIVILEGE_BIT), & xIdleTaskHandle)
Idle tasks are indispensable to FreeRTOS because the FreeRTOS design requires at least one task to be running. Let's take a look at the work to be done in our spare tasks.
1. Free memory
Starting with V9.0, if one task deletes another task, the stack and TCB of the deleted task are immediately released. If a task deletes itself, the stack of the task is deleted with the same TCB as before, through the idle task. So the idle task starts by checking to see if a task has deleted itself, and if so, the idle task is responsible for deleting the task's TCB and stack space.
two。 Handle idle priority tasks
When using a preemptive kernel, tasks with the same priority use time slices to gain CPU permissions. If there is a task that shares a priority with the idle task, and the macro configIDLE _ SHOULD_YIELD is set to 1, then the idle task does not have to wait until the time slice is exhausted before switching.
So the idle task checks whether there are multiple tasks in the ready list under the idle priority, and if so, performs a task switch to give the user task CPU privileges.
The macro configIDLE _ SHOULD_YIELD controls the behavior of the task in the idle priority. It works only if the following conditions are met.
Use a preemptive kernel to schedule user tasks using idle priorities.
Multiple tasks that share the same priority through time slices should get the same processor time if the shared priority is greater than the idle priority and assuming that there are no higher priority tasks.
However, if the idle priority is shared, the situation will be slightly different. When configIDLE_SHOULD_YIELD is 1, when other user tasks that share the idle priority are ready, the idle task immediately gives up the CPU, and the user task runs, which ensures the fastest response to the user task. Being in this mode can also have adverse effects (depending on your program needs), as described below:
The figure depicts four tasks with idle priorities, tasks A, B, and C are user tasks, and task I is idle tasks. Context switching occurs periodically at T0, T1. T6 moment. When the user task runs, the idle task immediately gives up the CPU, but the idle task has consumed a certain amount of time in the current time slice. The result is that idle task I and user task A share a time slice. User task B and user task C therefore get more processor time than user task A.
Can be avoided in the following ways:
If appropriate, put the individual tasks in the idle priority into the idle hook function; the created user task priority is higher than the idle priority; set IDLE_SHOULD_YIELD to 0
Setting configIDLE_SHOULD_YIELD to 0 prevents idle tasks from giving up CPU for user tasks until the time slice of the idle task ends. This ensures that all tasks in the idle priority are allocated the same amount of processor time, but at the expense of a higher percentage of processor time allocated to idle tasks.
3. Execute idle task hook function
The idle task hook is a function that is implemented by the user. RTOS specifies the name and parameters of the function, which is called during each idle task cycle.
To create an idle hook:
Set the configUSE_IDLE_HOOK in the FreeRTOSConfig.h file to 1; define a function with the function name and parameters as follows:
Void vApplicationIdleHook (void)
This hook function cannot call API functions (such as vTaskDelay (), queues with blocking time, and semaphore functions) that cause idle task blocking, and the use of co-routines within the hook function is allowed.
It is common to use the idle hook function to set CPU into power-saving mode.
4. Low power tickless mode
In general, FreeRTOS calls back the idle task hook function (which needs to be implemented by the designer), and sets the microprocessor to enter low-power mode in the idle task hook function to achieve the purpose of power saving. Because the system has to respond to the system beat interruption event, using this method will periodically exit and then enter the low-power state. If the system beat interrupt frequency is too fast, most of the power and CPU time will be consumed in the low power state of entry and exit.
FreeRTOS's tickless idle mode stops periodic system beat interruptions during idle cycles. Stopping the periodic system beat interruption can make the microcontroller in low power mode for a long time. The transplantation layer needs to configure an external wake-up interrupt to wake up the microcontroller from low-power mode when the wake-up event arrives. After the microcontroller wakes up, it will re-enable the system to interrupt the beat. Because the system beat counter stops after the microcontroller enters the low power consumption, but we need to know how many system beat interrupt cycles this time can be converted into, it requires an external clock source that is not affected by low power consumption, that is, it is also timing when the microprocessor is in low power mode. In this way, when the system beat is restarted, an adjustment value can be calculated according to this external timer and written into the RTOS system beat counter variable.
The source code for the idle task is shown below, where the macro portTASK _ FUNCTION is translated as: void prvIdleTask (void * pvParameters).
Static portTASK_FUNCTION (prvIdleTask,pvParameters) {/ * prevent compiler warnings * / (void) pvParameters; for (;;) {/ * check whether a task has deleted itself, and if so, the idle task is responsible for deleting the task's TCB and stack space * / prvCheckTasksWaitingTermination () # if (configUSE_PREEMPTION = = 0) {/ * if we do not use preemptive scheduling, we will force task switching to see if any other tasks become effective. If you use preemptive scheduling, this is not necessary, because when the task becomes valid, it will preempt idle tasks. * / taskYIELD ();} # endif/* configUSE_PREEMPTION * / # if ((configUSE_PREEMPTION = = 1) & & (configIDLE_SHOULD_YIELD = = 1)) {/ * when using a preemptive kernel, tasks with the same priority use time slices to obtain CPU permissions. If a task shares a priority with an idle task, then the idle task does not have to wait until the time slice is exhausted before switching. If there are multiple tasks in the ready list under the idle priority, execute the user task * / if (listCURRENT_LIST_LENGTH (& (pxReadyTasksLists [tskIDLE_PRIORITY])) > (UBaseType_t) 1) {taskYIELD ();}} # endif/* ((configUSE_PREEMPTION = = 1) & & (configIDLE_SHOULD_YIELD = = 1)) * / # if (configUSE_IDLE_HOOK = = 1) {externvoid vApplicationIdleHook (void) / * call user-defined functions. This allows designers to implement background functions without adding task overhead Note: this function is absolutely not allowed to call functions in which tasks may cause blocking. * / vApplicationIdleHook ();} # endif/* configUSE_IDLE_HOOK * / # if (configUSE_TICKLESS_IDLE! = 0) {TickType_txExpectedIdleTime / * it is difficult to be satisfied if the scheduler is suspended and then released every time the idle task is executed, so two identical comparisons (xExpectedIdleTime and configEXPECTED_IDLE_TIME_BEFORE_SLEEP) are performed here. The first comparison is to test whether the expected idle time is reached, and the scheduler is not suspended. * / xExpectedIdleTime= prvGetExpectedIdleTime (); if (xExpectedIdleTime > = configEXPECTED_IDLE_TIME_BEFORE_SLEEP) {vTaskSuspendAll () {/ * now that the scheduler is suspended, the idle time needs to be sampled again, this time idle time can be used * / configASSERT (xNextTaskUnblockTime > = xTickCount); xExpectedIdleTime= prvGetExpectedIdleTime (); if (xExpectedIdleTime > = configEXPECTED_IDLE_TIME_BEFORE_SLEEP) {portSUPPRESS_TICKS_AND_SLEEP (xExpectedIdleTime);}} (void) xTaskResumeAll () }} # endif/* configUSE_TICKLESS_IDLE * /}} here, this article "what is the idle task of FreeRTOS" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.