uCOSIII real-time operating system 6 Internal tasks (idle function)

Table of Contents

Idle tasks:

Clock beat tasks:

Statistical tasks:

Scheduled tasks:

Interrupt service management tasks:

Hook function:

Hook function for idle tasks:

Idle task hook function experiment:

Hook functions for other tasks:


Idle task:

Introduction: When a task needs to be delayed and enters a blocking state, what does the CPU do?

If there are no other tasks that can be run, the RTOS will create an idle task for the CPU. At this time, the CPU will run the idle task. In uCOSIII, the idle task is the lowest priority task created by the system during initialization. The idle task OSTaskIdle() does nothing but keeps giving a 32-bit OSIdleCtr counter. plus 1. Use this counter to determine the actual CPU time consumed by the current application. In view of this characteristic of the idle task, in actual applications, when the system enters the idle task, the microcontroller can be put into sleep or low power consumption operations during the idle task.

Features:

  1. The idle task is the first task created by UCOSIII.
  2. The idle task is created by UCOSIII and does not need to be created manually. It will be created when OS_Init() is called to initialize UCOS.
  3. The idle task priority is always OS_CFG_PRIO_MAK-1.
  4. Any function that causes the idle task to wait cannot be called in an idle task (such as a delay function)! ! !

Function:

  1. Idle tasks can reduce CPU usage.
  2. Hook functions can be used to implement low-power related functions.

In order to better achieve low power consumption, idle tasks are also necessary. We can implement low-power consumption measures such as sleep and standby in idle tasks.

Analysis of source code related to idle tasks:

First enter the OS initialization function. You can see that a lot of initialization is performed in this function.

Find the idle function initialization:

Jump in: We can see that there is an OSTaskCreate() task creation function

Continue to jump into the function body

CPU_CRITICAL_ENTER();CPU_CRITICAL_EXIT();Critical section code protection
● OSIdleTaskCtr + + ; Each time an idle task is entered, OSIdleTaskCtr is incremented by one. When the variable increases quickly, it proves that there is more idle time and the application task consumes less resources.
● The macro OS_CFG_STAT_TASK_EN is greater than 0, indicating that the statistics task is enabled.
● Add one to OSStatTaskCtr. OSStatTaskCtr is used in statistical tasks to count CPU usage.
● OSIdleTaskHook() is called a hook function. It hooks into a user-defined hook function to implement user-defined functions. However, it should be noted that in the hook function, the user is not allowed to call any function interface that can block the idle task. The idle task It is not allowed to be blocked.

Clock Beat Task:

uCOS needs to provide users with periodic signal sources to implement time delays and confirmation timeouts. The beat frequency should be between 10 and 100 beats per second, or 10 to 100Hz. The higher the time tick rate, the heavier the additional load on the system. The actual frequency of the clock tick depends on the accuracy of the user application (important).

The clock tick task is used to track task delays and task waiting timeouts. The task function is OS_TickTask();, which is a task that must be created by UCOSIII. The task priority is defined using the macro OS_CFG_TICK_TASK_PRIO. Generally, clock tick tasks should be set to a relatively small value. High priority. A function OS_TickTaskInit() is called in OS_Init().

Source code analysis:

Jump to the created task:

● (void)OSTaskSemPend((OS_TICK )0,OSTaskSemPend() requests the built-in semaphore of the task.
● OS_TickListUpdate(); If the semaphore request is successful, call the function OS_TickListUpdate() to initialize the clock tick list.

Statistical task:

There is a task in uCOSIII that provides running time statistics, which is the statistics task. Statistics tasks are not created by default. If you want to start the statistics task, you need to do the following steps:

  1. Set macro OS_CFG_STAT_TASK_EN to 1
  2. The function OSStatTaskCPUUsageInit(); must be called in the first task and the only application task created by the main function to use the statistical task.
  3. The priority of the statistics task is set through the macro OS_CFG_STAT_TASK_PRIO. It is generally set to OS_CFG_PRIO_MAX-2, which is the penultimate priority.

According to the above code, OSStatTaskCPUUsageInit( & amp;err); is the first function to be called, and other tasks are created after this function. The total CPU usage will be stored in the variable OSStatTaskCPUUsage. We can get the CPU usage by reading this value. The value is an integer between 0 and 10000, corresponding to 0.00~100.00%.

Scheduled task:

uCOSIII provides a software timer function, and scheduled tasks are optional. Setting the macro OS_CFG_TMR_EN to 1 will enable scheduled tasks. OSTmriInit() will be called in OSInit() to create a scheduled task.

Interrupt service management task:

The priority of the interrupt service management task is always the highest 0. Setting the macro OS_CFG_ISR_POST_DEFERRED_EN in the os_cfg.h file to 1 will enable the interrupt service management task.

Hook function:

Hook function of idle task:

The hook function OSIdleTaskHook() was mentioned in the previous section about the idle task. In this section, we first use the hook function OSIdleTaskHook() of the idle task to learn the hook function. The function code is as follows:

From the above code, we can see that if you want to use the idle task hook function, you need to set the macro definition OS_CFG_APP_HOOKS_EN to 1, which means the hook function of the idle task is allowed to be used. When the hook function of the idle task is enabled, the function pointed to by the pointer OS_AppIdleTaskHookPtr will be called every time it enters the idle task.

What is OS_AppIdleTaskHookPtr? ? ?

This is a function pointer which is essentially a pointer variable pointing to this function. We can find this function below,

The code in the red box shows that the pointer OS_AppIdleTaskHookPtr points to App_OS_IdleTaskHook, so the question comes again. What is App_OS_IdleTaskHook? ? ?

By jumping into the function, we can know:

Therefore, the hook function of the idle task App_OS_IdleTaskHook is the function App_OS_IdleTaskHook() that is ultimately called in OSIdleTaskHook; that is to say, we want to call the function App_OS_IdleTaskHook() in the idle function If you want to handle some other things in the hook task, write the code in the App_OS_IdleTaskHook(); function.

Note: Any code that can put idle into a waiting state cannot be called in the hook function of the idle task.

Idle task hook function experiment:

Experimental requirements:

When the idle task is executed every 50,000 times, the string “Idle Task Running 50000 times!” is printed through the serial port to test the task.

Experimental steps:

1. Set the macro definition OS_CFG_APP_HOOKS_EN to 1 to enable the hook function.

2. Add the header file os_app_hooks.h to the main.c file, add the usart.h file to the os_app_hooks.c file, and then use the conditional compilation statement to set the App_OS_SetAllHooks() function in the start task;

#if OS_CFG_APP_HOOKS_EN //Use hook function
App_OS_SetAllHooks();
#endif

3. Write the content of the hook function:

void App_OS_IdleTaskHook (void)
{
static int num;
num + + ;
if(0==numP000)
{
num=numP000;
printf("Idle Task Running 50000 times!!!\r\
");
}
}

Experimental results:

Hook function of other tasks:

UCOSIIi has a total of eight hook functions. In addition to the hook function of the idle task above, there are seven others:

  1. OSIdleTaskHook(), this function is called by idle tasks and can be used to put the CPU into low power mode.
  2. OSInitHook(), the system initialization function OSInit() calls this function.
  3. OSStatTaskHook(), this function is called every second by the statistical task. This function allows you to add your own application functions to the statistical task.
  4. , OSTaskCreateHook(), the hook function for task creation.
  5. OSTaskDelHook(), the hook function for task deletion.
  6. OSTaskReturnHook(), a hook function called when a task returns unexpectedly, such as deleting a task
  7. OSTaskSwHook(), the hook function called when switching tasks
  8. OSTimeTickHook(), the hook function called by the tick timer