FreeRTOS Library not working (for me)

FreeRTOS Lib causing memory? issue just by including it. Anybody an idea?

#include <Arduino_FreeRTOS.h> // 'the INCLUDE'

int count = 0;

void setup()
{  
    Serial.begin(9600);
    while (!Serial);
    Serial.write("\nStarted\n");
    count = 0;
}

void loop()
{
    Serial.write("Loop '");
    Serial.print((int)(++count));
    Serial.write("'\n");

    Serial.write("**********************************\n");
    Serial.write("**********************************\n");

    delay(1000);
}

Output without the INCLUDE:

Started
Loop '1'
**********************************
**********************************
Loop '2'
**********************************
**********************************
Loop '3'
**********************************
**********************************
Loop '4'
**********************************
**********************************
Loop '5'
**********************************
**********************************

Output with INCLUDE:

Started
Loop '1'
******%�*W�ѕ�)Loop '1'
*****�%�*W�ѕ�)Loop '1'
*******
Started
Loop '1'
******%�*W�ѕ�)Loop '1'
*****�%�*W�ѕ�)Loop '1'
*******
Started
Loop '1'
******%�*W�ѕ�)Loop '1'
*****�%�*W�ѕ�)Loop '1'
*******

Output with INCLUDE and 115200 baud:

Started
Loop '1'
********��
Started
Loop '1'
********��
Started
Loop '1'
********��

If you want to learn about FreeRTOS and programming in a multi-tasking environment, do yourself a favor and get an ESP32. They have FreeRTOS built in. So you can concentrate on learning to use the OS rather than fighting installation issues for a reduced-feature version that's cobbled together to run on a low-resource processor.

What are some good resource materials to learn how to use the esp32 FreeRTOS API?

I highly recommend the book Mastering-the-FreeRTOS-Real-Time-Kernel. You can skip chapters 2 & 3 for now.

That gets you going with vanilla (single Core) FreeRTOS. Then, you'll want to check out Espressif's additions to support running on dual Cores.

I think the problem is that you are putting code in loop(). With FreeRTOS it appears loop() should be empty, and all code should run in a task. Look at the examples.

ETA: I tried the original sketch, and it produced corrupted output.

Here is the sketch converted to a "FreeRTOS style" :

#include <Arduino_FreeRTOS.h>

int count = 0;

void myLoop ( void *pvParameters );

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial);
  Serial.write("\nStarted\n");
  count = 0;
    
  xTaskCreate(
    myLoop
    ,  "loop"   // 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 );
    
}

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

  while (true) // a task shoud not return
  {
    Serial.write("Loop '");
    Serial.print((int)(++count));
    Serial.write("'\n");

    Serial.write("**********************************\n");
    Serial.write("**********************************\n");

    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void loop() {
  // nothing
}

I tested on an Uno, it produces the expected output.

So how much memory is used with and w/o the lib?

I'll hijack that question because I'm interested in the answer :slight_smile:

Without FreeRTOS:

Sketch uses 1972 bytes (6%) of program storage space. Maximum is 32256 bytes.
Global variables use 242 bytes (11%) of dynamic memory, leaving 1806 bytes for local variables. Maximum is 2048 bytes.

With FreeRTOS (my version) :

Sketch uses 8142 bytes (25%) of program storage space. Maximum is 32256 bytes.
Global variables use 405 bytes (19%) of dynamic memory, leaving 1643 bytes for local variables. Maximum is 2048 bytes.

It's a significant overhead. I've found that adding more tasks, mutexes etc quickly chews up memory. I guess as long as you are aware of the overhead, and are not going to be building a complex sketch, then FreeRTOS provides a nice abstraction. Knowledge gained could be applied to more platforms platforms such as ESP32.

And if you just include the lib, no calls to functions?

Original sketch (compiled for Uno):

Sketch uses 9728 bytes (30%) of program storage space. Maximum is 32256 bytes.
Global variables use 399 bytes (19%) of dynamic memory, leaving 1649 bytes for local variables. Maximum is 2048 bytes.

Weird.

FreeRTOS replace some functions? OP got weird chars when the lib is included, a new Serial function?

I didn't really look into what goes on under the hood, apart from seeing that FreeRTOS uses loop() as the idle task, and generally you should not put code there "unless you know what you are doing".

And you don't have to, I think it's clear that a library can maneuver (you know what I mean) it's way into the code by #include and yet no calls. Perhaps libraries are checked first for the presence of a function?

I disagree with the first part, but I think I see what you are getting at. FreeRTOS overrides Arduino startup code to inject it's own scheduler. That will then invoke a lot of functions for task management etc. So even if you don't call any RTOS functions, there is a lot of code that will not be stripped by the linker as "unused".

If anyone is interested, one could read the documentation, e.g. Arduino FreeRTOS | Gone Bush

The memory use reports from the compiler aren't the full story. Each FreeRTOS task is assigned its own stack space. That RAM is allocated dynamically at run time by the xTaskCreate() function. The compiler has no idea about it.

You'r right, it's fixing my issue. Thanks a lot!
Still strange, as it was working in previous version, even find samples of FreeROTS around the internet using code in loop. But whatever, I have a solution, thanks!!