Introduction: FreeRTOS With Arduino 03 : Task Idle Hook

Picture of FreeRTOS With Arduino 03 : Task Idle Hook

In the earlier example, we created 2 high priority task and an Idle task. In this tutorial, we will see how to use the scheduler Idle task to run the user function.

This can be used to monitor the system when there is no other work or tasks running. It can also be used to handle data given out by high priority tasks.

Step 1: What Is Idle Task :

Idle task is automatically created when the scheduler starts. When no tasks are available for the CPU then this task will run. When a task is deleted, the memory used by it will not be free immediately. This will be done in the idle task.

Step 2: Using Idle Task Hook :

To use the Idle task, configUSE_IDLE_HOOK should be set to 1.In Arduino the loop() function is hooked to freeRtos Idle Task and will be called whenever the scheduler runs its Idle Task.

Step 3: Example :

In this example, we are going to create 2-Tasks with different priorities.Each task will run for some time and then goes to blocked state allowing the other tasks to run. Once the waiting time is elapsed the scheduler will bring the task to ready state and eventually to the run state, if its priority is higher compared to the currently running task. The loop() function will run whenever the CPU is idle.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 02-TaskIdleHook * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte the task switching along with task hook function.

This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php

EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES.

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. **************************************************************************************************/

#include

void setup() { Serial.begin(9600); Serial.println(F("In Setup function"));

/* Create two tasks with priorities 1 and 2. * Enable the Idle Task Hook by setting configUSE_IDLE_HOOK to 1, by this the loop function can be used as Idle task*/

xTaskCreate(MyTask1, "Task1", 100, NULL, 1, NULL); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, NULL); }

void loop() { // Hooked to IDle task, it will run whenever CPU is idle Serial.println(F("Loop function")); delay(50); }

/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { while(1) { Serial.println(F("Task1")); vTaskDelay(100/portTICK_PERIOD_MS); } }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { while(1) { Serial.println(F("Task2")); vTaskDelay(150/portTICK_PERIOD_MS); } }

Step 4: Output :

Picture of Output :

  • Note:: Here loop() function is hooked to scheduler Idle task and it will be considered as Idle task in below summary.

  1. The controller starts the execution from setup function. The Serial port is initialized at 9600 baud rate and setup message is printed.
  2. Later 3-Tasks(Task1, Task2 and Idle) are created in order with priorities 2,1,0. At the end of the Setup function, scheduler/kernel takes the control.
  3. here are 3-tasks in the Ready state and since Task2 has the highest priority it will run first and goes to block state for 150ms.

  4. Now 2-tasks are available for scheduler and it chooses Task1 as it is the available higher priority task.

  5. Now Task1 runs for some time and then goes to blocked state for 100ms.

  6. CPU is left out with the Idle task and it starts running.

  7. Task2 will be in blocked state for 150ms and task1 will 100ms. So task1 will come out of blocked state first.

  8. After Task1 waiting time is elapsed, it comes to the Ready state. Since it has got higher priority compared to the Idle task, it will preempt Idle task and starts running. It again goes to blocked state for 100ms.

  9. Now CPU is left out with IDLE task and it starts running it.

  10. After Task2 waiting time is elapsed, it comes to the Ready state. Since it has got higher priority compared to the Idle task, it will preempt Idle task and starts running. It again goes to blocked state for 150ms.

  11. Same thing continues.

Step 5: Downlaods :

Download the complete project and libraries from here .

Comments

About This Instructable

610views

7favorites

License:

Bio: We make Embedded Design tools, boards, sensors, peripherals, DIY kits and awesome tutorials. We also help companies and individuals prototype embedded electronics.
More by ExploreEmbedded:Power and Temperature Data Logger With ESP32 and AWS IOTSecure IOT With AWS and Hornbill ESP32 Using Arduino.ESP32 Bluetooth Ambience Light Controller With WS2812 LED Strips.
Add instructable to: