Tuto pour code non bloquant et FreeRTOS

Je continue à découvrir FreeRTOS.

Quelques fonction utiles pour gérer le temps :

  • xTaskGetTickCount() : renvoie le nombre de ticks écoulés depuis le lancement du RTOS. C'est aussi un moyen de suivre l'exécution des tâches dans le temps.
  • vTaskDelayUntil : permet de délayer une tâche d'un nombre de ticks donné.

Le nouveau code : 2 tâches. La première n'a pas changé, elle fait clignoter la LED toutes les secondes. Elle affiche le temps écoulé en ms, en multipliantle nombre de ticks par la durée du tick. La seconde affiche aussi les ticks écoulés mais elle est délayée de 100 ticks à chaque exécution.

#include <Arduino_FreeRTOS.h>
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif

// define two tasks for Blink & AnalogRead
void TaskBlink1 ( void *pvParameters );
void PrintTask( void * );

void setup() {
  Serial.begin(115200);
  while (!Serial) ;

  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink1
    ,  (const portCHAR *)"Blink1"   // A name just for humans
    ,  128  // This stack size can be checked & adjusted by reading the Stack Highwater
    ,  NULL
    ,  2  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    ,  NULL );

  xTaskCreate(
    PrintTask
    ,  (const portCHAR *) "Print"
    ,  128  // Stack size
    ,  NULL
    ,  1  // Priority
    ,  NULL );

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink1(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  // initialize digital LED_BUILTIN on pin 13 as an output.
  pinMode(LED_BUILTIN, OUTPUT);

  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    Serial.println(xTaskGetTickCount()*portTICK_PERIOD_MS);
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    Serial.println(xTaskGetTickCount()*portTICK_PERIOD_MS);
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void PrintTask( void * )
{
    TickType_t xLastWakeTime = xTaskGetTickCount();

    for( ;; )
    {
        Serial.print("\tPrint : ");
        Serial.println(xTaskGetTickCount());
        vTaskDelayUntil(&xLastWakeTime,100);
    }
}

La console :

16:29:33.012 -> 0
16:29:33.012 -> 	Print : 0
16:29:34.071 -> 992
16:29:34.690 -> 	Print : 100
16:29:35.101 -> 1984
16:29:36.164 -> 2976
16:29:36.402 -> 	Print : 200
16:29:37.226 -> 3968
16:29:38.111 -> 	Print : 300
16:29:38.284 -> 4960
16:29:39.346 -> 5952
16:29:39.793 -> 	Print : 400
16:29:40.372 -> 6944
16:29:41.434 -> 7936
16:29:41.502 -> 	Print : 500
16:29:42.495 -> 8928
16:29:43.214 -> 	Print : 600
16:29:43.557 -> 9920
16:29:44.617 -> 10912
16:29:44.924 -> 	Print : 700
16:29:45.677 -> 11904
16:29:46.600 -> 	Print : 800
16:29:46.704 -> 12896
16:29:47.769 -> 13888
16:29:48.318 -> 	Print : 900
16:29:48.832 -> 14880