Search code examples
arduinotaskfreertosrtos

How can I get the name of a task and print it?


I am using FreeRTOS and would like to get the name of the task being executed and print it. How can I extract the name of the task being executed from the handler and print it. For example, I have the following task being executed. I tried printing out the name as follows -

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

TaskHandle_t xHandle;
TaskStatus_t xTaskDetails;


  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  for (;;) // A Task shall never return or exit.
  {
    xHandle = xTaskGetHandle( "Task_Name" );
    Serial.println(xHandle);
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

But I get the error - Compilation error: no matching function for call to 'println(TaskControlBlock_t*&)'

Edit - Apprarently, I had to update the USE_TRACE_FACILITY flag to 1 in FreeRTOSConfig.h as per this link but I now get the the error -

Blink_AnalogRead/Blink_AnalogRead.ino:80: undefined reference to `xTaskGetHandle'

Solution

  • Why the compilation error

    It seems that you have a little confusion about how the xTaskGetHandle works. Per documentation

    TaskHandle_t xTaskGetHandle( const char *pcNameToQuery );
    

    And returns:

    If a task that has the name passed in pcNameToQuery can be located then the handle of the task is returned, otherwise NULL is returned.
    

    Now, apparently you already have the name of the task (in the universal conception that the name is a sequence of characters) and you are using, so it's unclear what you want to get.

    Anyway, the function returns a TaskHandle_t which is not a string, hence when you pass such type to the println function the compilation fails because it is expecting a string like object.

    Get task name

    In order to get the task name from the handler you need the function vTaskGetInfo and the struct TaskStatus_t .

    Here's how you do it in your function:

    void TaskBlink(void *pvParameters)  // This is a task.
    {
      (void) pvParameters;
    
    TaskHandle_t xHandle;
    TaskStatus_t xTaskDetails;
    
    
      // initialize digital LED_BUILTIN on pin 13 as an output.
      pinMode(LED_BUILTIN, OUTPUT);
      for (;;) // A Task shall never return or exit.
      {
        xHandle = xTaskGetHandle( "Task_Name" );
    
        TaskStatus_t xTaskDetails;
        /* Check the handle is not NULL. */
        configASSERT( xHandle );
    
        /* Use the handle to obtain further information about the task. */
        vTaskGetInfo( /* The handle of the task being queried. */
                      xHandle,
                      /* The TaskStatus_t structure to complete with information
                      on xTask. */
                      &xTaskDetails,
                      /* Include the stack high water mark value in the
                      TaskStatus_t structure. */
                      pdTRUE,
                      /* Include the task state in the TaskStatus_t structure. */
                      eInvalid );
    
    
        Serial.println(xTaskDetails.pcTaskName);
    
        digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
        vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
        digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
        vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
      }
    }