Introduction: FreeRTOS With Arduino 05 : Resuming Task From ISR

Picture of FreeRTOS With Arduino 05 : Resuming Task From ISR

In earlier tutorials, we saw how to Suspend and Resume the tasks from other tasks. In this tutorial, we will how to resume a task from ISR using xTaskResumeFromISR(). For this, we will be using the external INTO interrupt of Arduino UNO. Check this link for more details on Arduino UNO external interrupt.

Step 1: API Details :

Here we will discuss some of the most frequently used APIs related to tasks.

xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

vTaskDelete():This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.To delete the own task we should pass NULL as the parameter. Please check this link for more details.

vTaskSuspend(): This function is used to Suspend a task, the suspended remains in the same state util it is resumed.For this, we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. Check this link for more details.

vTaskResume(): This function is used to resume a suspended task. If the Resumed task has higher priority than the running task then it will preempt the running task or else stays in ready stateFor this, we need to pass the handle of the task to be resumed. Check this link for more details.

xTaskResumeFromISR(): This function is used to resume a task from ISR.For this, we need to pass the handle of the task to be resumed. Check this link for more details.

Step 2: Example1 :

In this example, we will be creating four Tasks with priorities 1-4. Task4 which has the highest priority will run for some time and suspends all the tasks. At this point, the scheduler will be left out with the Idle task. This keeps running as long as the other tasks are not resumed. Every time Falling edge interrupt is detected, ISR will resume a task. This will cause the Cpu to preempt Idle task and run the resumed task. The resumed task will run for some time and deletes itself. This resuming of tasks continues for 3 times after which all tasks are deleted and the idle task keeps running.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 09-ResumingTaskFromISR * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task Suspend and Resume.

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

TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_3; TaskHandle_t TaskHandle_4;

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

/* Use INT0(pin2) falling edge interrupt for resuming tasks */ attachInterrupt(digitalPinToInterrupt(2), ExternalInterrupt, FALLING);

/* Create 3-tasks with priorities 2-4. Capture the Task details to respective handlers */ xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &TaskHandle_3); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); }

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

/* * Tasks are resumed every time a Falling edge interrupt is detected on PIN2. * One task is resumed at a time, a counter is used to resume 3taks and after which no tasks are resumed. * xTaskResumeFromISR() returns True if Context switch is required and accordingly we need to call portYIELD_FROM_ISR/taskYield(AVR). * Serial data is printed in ISR only for demonstarting the control flow. This should not be done as it takes long time to send data on Serial port. * Tasking to much ISR time will starve the other tasks or User application. * */ static void ExternalInterrupt() { static int count=0; BaseType_t taskYieldRequired = 0;

if(count<=3) { count++; }

switch(count) // Resume one task at a time depending on count value { case 1: Serial.println(F("ISR Resuming Task2")); taskYieldRequired = xTaskResumeFromISR(TaskHandle_2); Serial.println(F("Leaving ISR")); break;

case 2: Serial.println(F("ISR Resuming Task3")); taskYieldRequired = xTaskResumeFromISR(TaskHandle_3); Serial.println(F("Leaving ISR")); break;

case 3: Serial.println(F("ISR Resuming Task4")); taskYieldRequired = xTaskResumeFromISR(TaskHandle_4); Serial.println(F("Leaving ISR")); break;

default: //DO nothing break; }

if(taskYieldRequired == 1) // If the taskYield is reuiqred then trigger the same. { taskYIELD(); } }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { Serial.println(F("Task2, Deleting itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) }

/* Task3 with priority 3 */ static void MyTask3(void* pvParameters) { Serial.println(F("Task3, Deleting Itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_3 can also be used) }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Suspending all tasks")); vTaskSuspend(TaskHandle_2); //Suspend Task2/3 vTaskSuspend(TaskHandle_3); vTaskSuspend(NULL); //Suspend Own Task

Serial.println(F("Back in Task4, Deleting Itself")); vTaskDelete(TaskHandle_4); }

Step 3: Output1 :

Picture of Output1 :

0. Serial port is initialised 3-Tasks is created with priorities 2-4.

  1. Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
  2. Now IDLE tasks keeps running till one of the Task comes out or suspended state. In the current example, only INTO ISR can resume the tasks.
  3. Once the INT0 interrupt is generated, it will resume Task2.

  4. Task2 will run for some time and deletes itself.

  5. Again IDLE task keeps running till interrupt is generated and tasks are resumed.

  6. Once the INT0 interrupt is generated, it will resume Task3.

  7. Task3 will run for some time and deletes itself.

  8. Again IDLE task keeps running till interrupt is generated and tasks are resumed.

  9. Once the INT0 interrupt is generated, it will resume Task3.

  10. Task3 will run for some time and deletes itself.

  11. Only IDLE task is left and it keeps running.

Step 4: Downloads :

Download the complete project and libraries from here.

Comments

FreeRTOS (author)2016-10-15

Be sure to read the API documentation carefully for the xTaskResumeFromISR() function - it must NOT be used to syncrhonise a task with an ISR in the same way a direct to task notification would. See http://www.freertos.org/taskresumefromisr.html

About This Instructable

922views

6favorites

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: