Introduction: FreeRTOS With Arduino 07 : Changing Priority of Tasks

n earlier tutorials, we saw how to create, use and then delete the tasks. In this tutorial, we will see how to read the priority of task and also change it dynamically.

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 details.

vTaskPrioritySet(): This function is used to change/Set the priority of a task. For this we need to pass the handle of the task and new priority to vTaskPrioritySet() function. Check this link for more details.

vTaskPriorityGet(): This function is used to get the priority of a task.For this, we need to pass the handle of the task and it will return the task. Check this link for more details.

Step 2: Example1 :

In this example, we will be changing the priority of Task1 from low(1) to medium(3).This task will delete all the tasks which have priorities lower than it. The task with higher priorities will continue to run ahead of Task1, once their job is done they delete themselves. So we will be able to see the output of the tasks with priorities higher than Task1.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 05-TaskPriorityChange * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task priority change.

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_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_4;

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

/* Create three tasks with priorities 1,2 and 3. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 120, NULL, 1, &TaskHandle_1); }

void loop() { // put your main code here, to run repeatedly: Serial.println(F("Loop function")); delay(50); }

/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { while(1) { Serial.print(F("Task1 with Priority:")); Serial.print(uxTaskPriorityGet(TaskHandle_1)); Serial.println(F(" Creating Task2")); xTaskCreate(MyTask2, "Task2", 100, NULL, 3, &TaskHandle_2); Serial.print(F("Task1 with Priority:")); Serial.print(uxTaskPriorityGet(TaskHandle_1)); Serial.println(F(" Deleting All")); vTaskDelete(TaskHandle_2); // Delete task2 and task4 using their handles vTaskDelete(TaskHandle_4); vTaskDelete(TaskHandle_1); // Delete the task using the TaskHandle_1 } }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { while(1) { Serial.println(F("Task2 Running, Creating Task4")); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); Serial.println(F("Back in Task2, Deleting itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) } }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Changing Priority of Task1 from 1-3")); vTaskPrioritySet(TaskHandle_1,3); //Change the priority of task1 to 3 which is greater than task2

while(1) { Serial.println(F("Back in Task4 ")); vTaskDelay(100/portTICK_PERIOD_MS); } }

Step 3: Output1 :

0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 3 which is greater than Task2 but less than Task4. Hence Task4 still continues to run and enters waiting/blocked state.
  4. Now we have Task1(Prio:3), Task2(Prio:2), IDLE(Prio:0) ready to execute. Since Task1 has higher priority, it starts running and prints its priority as 3 and deletes all the task. The remaining part of Task2 and Task4 will never get executed.

Step 4: Example2 :

In earlier example, we changed the priority of Task1 from 1 to 3, because of which the Tasks with priority greater than 3 were able to run.In this example, we will change the priority of Task1 from low(1) to highest(5). This task will delete all the tasks and deletes itself after completing its job.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 06-TaskPriorityChange * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task priority change.

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_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_4;

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

/* Create three tasks with priorities 1,2 and 3. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 120, NULL, 1, &TaskHandle_1);

}

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 with Priority:")); Serial.print(uxTaskPriorityGet(TaskHandle_1)); Serial.print(F("Creating Task2")); xTaskCreate(MyTask2, "Task2", 100, NULL, 3, &TaskHandle_2); Serial.println(F("Task1 with Priority:")); Serial.print(uxTaskPriorityGet(TaskHandle_1)); Serial.print(F(" Deleting All")); vTaskDelete(TaskHandle_2); // Delete task2 and task4 using their handles vTaskDelete(TaskHandle_4); vTaskDelete(TaskHandle_1); // Delete the task using the TaskHandle_1 } }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { while(1) { Serial.println(F("Task2 Running, Creating Task4")); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); Serial.println(F("Back in Task2, Deleting itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) } }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Changing Priority of Task1 from 1-3")); vTaskPrioritySet(TaskHandle_1,5); //Change the priority of task1 to 5 which is hisghest

while(1) { Serial.println(F("Back in Task4 ")); vTaskDelay(100/portTICK_PERIOD_MS); } }

Step 5: Output2 :

0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preempt Task4 and starts running.
  4. Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.

Step 6: Downloads :

Download the complete project and libraries from here .

Comments

About This Instructable

652views

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: