- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Suspend/Resume tasks in FreeRTOS using Arduino
If you wish to suspend a task in FreeRTOS, there is a function vTaskSuspend() that can be used. The syntax is −
Syntax
void vTaskSuspend( TaskHandle_t xTaskToSuspend );
As you can see, it takes the handle of the task to suspend as the argument and returns nothing. A suspended task can be resumed using vTaskResume(). The syntax is −
Syntax
void vTaskResume( TaskHandle_t xTaskToResume );
This again takes the handle of the task to be resumed, and returns nothing.
In order to see an example, we will walk-through the code given in −https://exploreembedded.com/wiki/Task_Suspend_and_Resume
As you can see, four Task handles are declared initially, and the tasks are created in the setup.
/************************************************************************ *************************** ExploreEmbedded Copyright Notice ************************************************************************* *************************** * File: 07-TaskSuspendAndResume * 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 <Arduino_FreeRTOS.h> TaskHandle_t TaskHandle_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_3; TaskHandle_t TaskHandle_4; void setup() { Serial.begin(9600); Serial.println(F("In Setup function")); /* Create 4-tasks with priorities 1-4. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &TaskHandle_3); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); }
Note that Task1 has the lowest priority and Task4 has the highest priority.
The loop just contains a print statement and a delay.
void loop() { // Hooked to Idle Task, will run when CPU is Idle Serial.println(F("Loop function")); delay(50); }
Now, coming to the individual task codes, Task4 suspends Task2, Task3 and then itself. Task 1 resumes these tasks one by one, and then deletes itself. Tasks 2 and 3 just delete themselves after printing a statement.
/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { Serial.println(F("Task1 Resuming Task2")); vTaskResume(TaskHandle_2); Serial.println(F("Task1 Resuming Task3")); vTaskResume(TaskHandle_3); Serial.println(F("Task1 Resuming Task4")); vTaskResume(TaskHandle_4); Serial.println(F("Task1 Deleting Itself")); vTaskDelete(TaskHandle_1); } /* 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); }
Thus, Task4, with the highest priority starts first, and suspends Tasks 2, 3 and itself. Thus, Task1 gets control. Task1 resumes Task2. Since Task2 has a higher priority, it takes over control, prints a statement and deletes itself. Control goes back to Task1, which then resumes Task3. Task3 does its work, deletes itself and thus control goes back to Task1. Same thing happens with Task4, and once Task4 has been deleted, Task1 also deletes itself, and only the print statements in loop are visible on the Serial Monitor.
- Related Articles
- Semaphore and Mutex in FreeRTOS using Arduino
- Queue in FreeRTOS in Arduino
- How to retrieve tasks in Task scheduler using PowerShell?
- Tasks in C#
- LED Blinking using Arduino
- Best Words to Use in a Resume!!
- Launching parallel tasks in Python
- Using a third-party library in Arduino
- How to suspend the calling thread for few seconds in Python?
- Guide to Designing a Digital Marketing Resume in 2023
- What are the tasks in data preprocessing?
- C program that does not suspend when Ctrl+Z is pressed
- How to compile code using Arduino IDE
- Displaying data on OLED Screen using Arduino
- Getting data from Vibration sensor using Arduino
