ESP32 Arduino: Getting FreeRTOS tasks priority

The objective of this post is to explain how to get the priorities of FreeRTOS tasks, using the ESP32 and the Arduino environment.


Introduction

The objective of this post is to explain how to get the priorities of FreeRTOS tasks, using the ESP32 and the Arduino environment. Task priorities are a very important concept for FreeRTOS because they are used by the schedule to decide to which task the CPU is given.

In this post we will use a function to get the priorities of the setup and main loop functions and of a created task.


The code

The code for this tutorial will be very simple. As usual, we start our setup function by opening a serial connection.

After that, we will get the priority of the setup function. To do so, we simply use the uxTaskPriorityGet function. This function receives as input parameter the handle of the task to which we want to know the priority, and returns the priority value for that task [1]. If we pass NULL as input, we get the priority of the calling task [1].

In the case of the setup function, that’s precisely what we want, so we call the function with a NULL argument, as can be seen bellow.

Serial.begin(112500);
delay(1000);

Serial.print("Setup: priority = ");
Serial.println(uxTaskPriorityGet(NULL));

After that, we will declare a variable of type TaskHandle_t, so we can store the handle for a task that we will be creating.

TaskHandle_t myTask;

Then, we create a new task. We will deal with the implementing function latter. For now, the important parameters to take in consideration are the priority, to which we will assign the value 12, and the handle, to which we will pass the address of the previously declared variable. This way, we will store the handle for the task to get its priority latter.

Check bellow the code for creating the task. If you need more information about creating tasks, check this previous post.

  xTaskCreate(
                    genericTask,        /* Task function. */
                    "genericTask",      /* String with name of task. */
                    10000,              /* Stack size in words. */
                    NULL,               /* Parameter passed as input of the task */
                    12,                 /* Priority of the task. */
                    &myTask);           /* Task handle. */

To finalize the setup function, we will now print the priority of this launched task, by passing as argument of the uxTaskPriorityGet function the task handle.

Serial.print("Setup: created Task priority = ");
Serial.println(uxTaskPriorityGet(myTask));

Now, on the main loop function, we will repeat the procedure done for the setup function, in order to obtain its priority.

void loop() {
Serial.print("Main Loop: priority = ");
Serial.println(uxTaskPriorityGet(NULL));
delay(1000);
}

The final code can be seen bellow. It already includes the task function, which only does a delay and then is deleted.

void setup() {

  Serial.begin(112500);
  delay(1000);

  Serial.print("Setup: priority = ");
  Serial.println(uxTaskPriorityGet(NULL));

  TaskHandle_t myTask;

  xTaskCreate(
                    genericTask,        /* Task function. */
                    "genericTask",      /* String with name of task. */
                    10000,              /* Stack size in words. */
                    NULL,               /* Parameter passed as input of the task */
                    12,                 /* Priority of the task. */
                    &myTask);           /* Task handle. */

  Serial.print("Setup: created Task priority = ");
  Serial.println(uxTaskPriorityGet(myTask));

}

void loop() {
  Serial.print("Main Loop: priority = ");
  Serial.println(uxTaskPriorityGet(NULL));
  delay(1000);
}

void genericTask( void * parameter ){
    delay(20000);
    vTaskDelete(NULL);
}


Testing the code

To test the code, upload it from the Arduino IDE and open the serial monitor. You should get something similar to figure 1, which shows the priorities of the setup function, the created task and the main loop function.

ESP32 FreeRTOS getting tasks priorities

Figure 1 – Output of the program to get the priority of the tasks.


References

[1] http://www.freertos.org/a00128.html

Advertisements
This entry was posted in ESP32 and tagged , , , , . Bookmark the permalink.

3 Responses to ESP32 Arduino: Getting FreeRTOS tasks priority

  1. Pingback: ESP32: Running code on a specific core | techtutorialsx

  2. Pingback: ESP32: FreeRTOS counting semaphores | techtutorialsx

  3. Pingback: ESP32: Dual core execution speedup | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s