FreeRTOS Task

The implementation of the task has no return value as follows, and takes a pointer of type void as an argument. A task is implemented as an infinite loop, and when it ends, it explicitly deletes the task.


void ATaskFunction( void *pvParameters );
 
void ATaskFunction( void *pvParameters ){
    while(1){
        // do something
    }
}

GR-ROSE is a single core, so only one task can be executed at a time. The running state is "Running", and the non-running states are "Blocked", "Ready" and "Suspended" as multiple states.

Tasks are executed from the highest priority, and in order if they have the same priority, they are executed in order. If a higher priority task becomes "Ready" while executing a lower priority task, the task to be executed is switched.

When processing such as waiting for vTaskDelay (), delay (), or Queue in a task, it will be in "Blocked" state. If the lower priority task is "Ready", it will be in "Running" state.

In GR-ROSE SDK, the priority can be specified in 7 levels (0 to 6). The priority of the main task on which setup and loop are executed is 3. Based on the above, we will publish one sample.


#include <Arduino.h>
#include "FreeRTOS.h"
#include "task.h"
 
void task1(void *pvParameters);
void task6(void *pvParameters);
 
void setup(){
  Serial.begin(9600);
  delay(3000); // wait for display serial monitor
  xTaskCreate( task1, "TASK1", 512, NULL, 1, NULL );
  xTaskCreate( task6, "TASK6", 512, NULL, 6, NULL );
}
 
void loop(){
  static uint32_t ctime = millis();
  if((millis() - ctime) > 5000){
    delay(1000); // entering Block State
  }
}
 
void task1(void *pvParameters){
  while(1){
    Serial.println("task1 running");
    delay(1000); // entering Blocked State
  }
}
void task6(void *pvParameters){
  while(1){
    Serial.println("task6 running");
    delay(1000); // entering Blocked State
  }
}

The above sample generates task 1 with priority 1 and task 6 with priority 6. It is a task which performs serial output every one second each. After 5 seconds, delay () is executed in the priority 3 loop and it becomes "Blocked".

When it is executed, it will be displayed on the serial monitor as follows. The function "setup" creates task1 and task6, but only task6 which has higher priority than loop is executed for 5 seconds. After 5 seconds, execute delay() in loop and it will be in "Blocked" state, so it can be seen that task1 is also executed.

task6 running
task6 running
task6 running
task6 running
task6 running
task6 running
task1 running
task6 running
task1 running
task6 running
task1 running
task6 running
task1 running

Here is an example of deleting a task: The periodic task is executed every second, but when it is executed 5 times, vTaskDelete is executed and the task is deleted. The task consumes HEAP memory as shown in Overview, so delete it when it is not needed.


#include <Arduino.h>
#include "FreeRTOS.h"
#include "task.h"
 
void periodic(void *pvParameters);
TaskHandle_t periodicHandle = NULL;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000);
   
  xTaskCreate(periodic, "PERIODIC", 512, NULL, 2, &periodicHandle);
}
 
void loop() {
  // put your main code here, to run repeatedly:
  delay(1);
}
 
void periodic(void *pvParameters) {
  while (1) {
    static int count = 0;
    Serial.println(count);
    delay(1000);
    count++;
    if(count == 5){
      vTaskDelete( periodicHandle );
    }
  }
}