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.


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 state
For 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.

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.

Output1

ResumingTaskFromISR.png
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.

Downloads

Download the complete project and libraries from here.


Have an opinion, suggestion , question or feedback about the article let it out here!