Line 25: Line 25:
  
 
=Example1=
 
=Example1=
 +
In this example, we will be creating for Tasks with priorities 1-4.<br>
 +
Task4 which is the higher priority will run and 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.<br>
 +
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 selected and the only idle task will run.
 +
 
<html><script src="https://gist.github.com/SaheblalBagwan/2cf7a0ec1f6924484722ec15409d6e4b.js"></script></html>
 
<html><script src="https://gist.github.com/SaheblalBagwan/2cf7a0ec1f6924484722ec15409d6e4b.js"></script></html>
  

Revision as of 19:10, 30 June 2016

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 for Tasks with priorities 1-4.
Task4 which is the higher priority will run and 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 selected and the only idle task will run.

Output1

ResumingTaskFromISR.png
0. Serial port is initialised Task3 is created with priority 3.

  1. Task3 starts executing as it has higher priority. NowTask3 creates Task2 and Task4 with priorities 2 and 4. Since Task4 has higher priority compared to Task3, Task4 starts running preempting Task3
  2. Now Task4 Suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
  3. 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.
  4. Once the INT0 interrupt is generated, it will resume Task2.
  5. Task2 will run for some time and deletes itself.
  6. Again IDLE task keeps running till interrupt is generated and tasks are resumed.
  7. Once the INT0 interrupt is generated, it will resume Task3.
  8. Task3 will run for some time and deletes itself.
  9. Again IDLE task keeps running till interrupt is generated and tasks are resumed.
  10. Once the INT0 interrupt is generated, it will resume Task3.
  11. Task3 will run for some time and deletes itself.
  12. Only IDLE task is left and it keeps running.