FreeRTOS Tips

GR-ROSE runs on FreeRTOS. Setup and loop are called from the main function, but this main function is also one of the tasks.

Multi-tasking is possible by FreeRTOS. Even the long and complicated processing of a loop can be simplified by cutting it out as a task. Programming can also be developed separately on a task-by-task basis.

Each tip will introduce FreeRTOS configuration and usage examples. Please refer to the FreeRTOS Reference Manual for the specifications of FreeRTOS.

FreeRTOS performs based on the various settings in the header file FreeRTOSConfig.h. Here is explanation of the basic settings, using the following sample as an example.


#include <Arduino.h>

void loop2(void *pvParameters);
static const char *pcText = "loop2 is running";
    
void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    delay(3000);
    
    uint32_t size1 = xPortGetFreeHeapSize();
    Serial.print("Heap Size 1: ");
    Serial.println(size1);
    
    xTaskCreate(loop2, "LOOP2", 512, (void*)pcText, 2, NULL);
    
    uint32_t size2 = xPortGetFreeHeapSize();
    Serial.print("Heap Size 2: ");
    Serial.println(size2);
    Serial.print("Consumption: ");
    Serial.println(size1 - size2);
}
    
void loop() {
    // put your main code here, to run repeatedly:
    Serial.println("loop is running");
    vTaskDelay(pdMS_TO_TICKS( 1000 )); // into "Block State" for 1000ms
    
}
    
void loop2(void *pvParameters) {
    while (1) {
    Serial.println((char*)pvParameters);
    delay(2000); // delay uses vTaskDelay to be into "Block State".
    }
}

The xPortGetFreeHeapSize () on the 11th line can obtain the size of HEAP memory reserved for FreeRTOS. FreeRTOS uses HEAP memory when creating tasks and queues. The GR-ROSE SDK reserves 128KiB as HEAP memory. It can be changed by configTOTAL_HEAP_SIZE in FreeRTOSConfig.h.

VTaskDelay (pdMS_TO_TICKS (1000)) on the 27th line is processing to wait for 1000ms. vTaskDelay is the process of waiting for the number of Ticks, but executing pdMS_TO_TICKS (1000) acquires the number of Ticks for 1000ms. Since 1 Tick is 1ms in the GR-ROSE SDK, even if vTaskDelay (1000) is specified, it takes 1000ms to process. The delay function is implemented using this vTaskDelay.