Chapter 15

The Idle task and the idle task hook

Abstract

In FreeRTOS when all the tasks are in blocked state, there must be at least one task that can enter into the Running state at any time, and this task is known as the Idle task. The Idle task is automatically created when function vTaskStartScheduler() is called. The Idle task consists of a simple loop and it has the lowest priority of 0 so that it cannot prevent any other task in the system to run. The Idle task is forced out of the Running state as soon as a higher priority task enters the Ready state. The Idle task is responsible for cleaning up kernel resources after a task has been deleted. This chapter is about the Idle task. This chapter describes the reason for the existence of the Idle task. The FreeRTOS idle task application program interface (API) function is given. A project is given in the chapter describing how to use the idle task in a real-time multitasking project.

Keywords

FreeRTOS
Clicker 2 for STM32
mikroC Pro for ARM
Idle task
Idle task hook functions
free processor time
low priority tasks
saving power
background task

15.1. Overview

In the previous chapter, we have developed several projects using various application program interface (API) functions of the FreeRTOS. In this chapter, we are looking at some other important features of the FreeRTOS.

15.2. The Idle task

In FreeRTOS when all the tasks are in blocked state, there must be at least one task that can enter into the Running state at any time to keep the CPU busy, and this task is known as the Idle task. If there was no Idle task, then the list of the runnable tasks would be empty and the scheduler will probably crash. Apart from other useful benefits as we shall see shortly, the Idle task makes sure that the list of runnable tasks is never empty. The Idle task is automatically created when function vTaskStartScheduler() is called. The Idle task consists of a simple loop and it has the lowest priority of 0 so that it cannot prevent any other task in the system to run. The Idle task is forced out of the Running state as soon as a higher priority task (i.e., any other task with a priority higher than 0) enters the Ready state. The parameter configIDLE_SHOULD_YIELD in file FreeRTOSConfig.h can be used at compile time to prevent the Idle task from consuming processing time. The Idle task is responsible for cleaning up kernel resources after a task has been deleted. You can stop the Idle task, provided you do not delete any tasks at run time. Alternatively, you can create your own Idle task to run at the priority of 1. Then, the Idle task will never run as long as the task you created never blocks. That is, it is always in the Ready state.

15.3. Idle task hook functions

Idle callback (or idle hook) function is called automatically by the Idle task in every iteration of the Idle task loop. It is, therefore, possible to use the Idle task for the following reasons:
  • Executing low priority background tasks
  • Measuring the amount of free processing capacity available
  • Saving power by placing the processor into low power mode when there are no tasks running
An idle task hook function must never attempt to block or suspend, as blocking the Idle task will cause a situation where no tasks are able to enter the Running state. The idle hook function must always return to its caller within a reasonable time since the Idle task is responsible for cleaning up the kernel when function vTaskDelete() is used. If the idle hook function never returns, or it stays too long in its processing cycle, then this cleaning operation cannot be performed.
If parameter configUSE_IDLE_HOOK in file FreeRTOSConfig.h is set to 1, this ensures that the idle hook function will be called at every iteration of the Idle task. The application must then provide a hook function with the following format:
  • void vApplicationIdleHook(void);
An example project is given below, illustrating how an idle hook function can be used in a simple application.

15.4. Project 39: display the free processor time

Description: In this project, a task is used to flash an LED every 100 ms. An idle hook function is created which increments a counter every time it is called by the Idle task. The count is proportional to the available free processing time. The higher the count, the more free processing time there is.
Aim: The aim of this project is to show how the idle hook function can be used in an application.
Block diagram: In this project, the on-board LED at port pin PE12 of the Clicker 2 for STM32 development board is used for simplicity. A USB UART Click board is connected to mikroBUS 2 socket as in the previous projects using the USB UART Click board. This board is interfaced to a PC through a mini USB cable so that data can be sent to the PC and displayed on the PC screen using a terminal emulator program.
Program listing: Parameter configUSE_IDLE_HOOK must be set to 1 in file FreeRTOSConfig.h for the idle hook function to get called by the Idle task. The program listing is shown in Figure 15.1 (program: hook.c). There is only one task in the program in addition to the Idle task. At the beginning of the task, port pin PE12 is configured as an output where the on-board LED is attached to. Function vApplicationIdleHook() increments variable Count every time it is called. That is, every time there are no other tasks to run other than the Idle task. Task 1 flashes the LED every 100 ms and then displays the value of the Count on the screen. The count obtained depends on the type of hardware used and the clock speed. In this application, as shown in Figure 15.2, the count was around 1,099,600. The count is reset to 0 after being displayed.
image
image
Figure 15.1 hook.c program listing.
image
Figure 15.2 Displaying the count.
Notice that Task 1 has two delay functions with 200 ms each, making a total of 400 ms. The Idle task is active when Task 1 is waiting for 400 ms. Therefore, one can assume that approximately, the count of 1,099,600 corresponds to a time of 400 ms. That is, the Idle time is approximately 2750 count/ms for the hardware and the clock rate used in this project. A more accurate relationship between the count and Idle time can be obtained by modifying Task 1 as follows:
    while (1)
    {
        vTaskDelay(xDelay10 ms);
        LongToStr(Count, Txt);
        Count = 0;
        Ltrim(Txt);
        UART3_Write_Text(Txt);
        UART3_Write_Text(“ ”);
    }
The delay time is approximately equal to the time that Task 1 is waiting, that is, the time that the Idle task is running. The delay can be varied and the count noted. The following values were obtained:
Delay time (ms) Count
10 26,893
20 54,408
30 81,923
40 109,200
50 136,666
This gives an approximate straight line with the following equation:
  • Count = 2744 × Delay − 547
where Delay is the Idle task processing time in milliseconds.
Notice that the above equation applies only to the Clicker 2 for STM32 development board with the STM32F407 processor running at 168 MHz. The same development board with different clock rates, or different development boards will give different readings. Although this is a very simple project, it illustrates the principles of using the idle task hook function.

15.5. Summary

In this chapter, we have seen how to use the idle hook function in a project to get an estimate of the processing time in a simple application.
In the next chapter, we shall be looking at the important topic of Task Notifications.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.149.94