Skip to content

Basics of FreeRtos

What is RTOS?

Real Time Operating System, commonly known as an RTOS, is a software component that rapidly switches between tasks, giving the impression that multiple programs are being executed at the same time on a single processing core.

Most operating systems appear to allow multiple programs to execute at the same time. This is called multi-tasking. In reality, each processor core can only be running a single thread of execution at any given point in time. A part of the operating system called the scheduler is responsible for deciding which program to run when, and provides the illusion of simultaneous execution by rapidly switching between each program.

What is FreeRtos?

FreeRTOS is a real-time operating system kernel for embedded devices that has been ported to 35 microcontroller platforms. It is distributed under the MIT License.

Code Examples

platformio.ini

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
;PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html


[env:esp32dev]
platform = espressif32
board = esp32dev
; board = esp32doit-devkit-v1
framework = arduino

monitor_speed=115200 
; upload_speed=921000
; upload_port=COM40
; monitor_port = COM40

; upload_port=$common_settings.COM_PORT)
; monitor_port = $(common_settings.COM_PORT)
; debug_tool=esp-prog
; debug_init_break=tbreak setup1
; upload_protocol = esp-prog

; [env:common_settings]
; COM_PORT=COM40

main.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <Arduino.h>
#include "Common.h"
void setup()
{
  // put your setup code here, to run once:

  Serial.begin(115200);
  delay(1000);
  // setup_executionCore();
  setup_QueuesBetweenTask();
}

void loop()
{
  // put your main code here, to run repeatedly:
  // loop_getExecutionCore();
  void loop_QueuesBetweenTask();
}

Common.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#ifndef __COMMON_H
#define __COMMON_H
void taskOne( void * parameter );
void taskTwo( void * parameter);
void initiate_create_task_example();
void setup_executionCore();
void  loop_getExecutionCore();
void setup_QueuesBetweenTask();
void loop_QueuesBetweenTask();
#endif //__COMMON_H

Creating_Tasks.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <Arduino.h>
#include "Common.h"

void taskOne(void *parameter)
{

    for (int i = 0; i < 10; i++)
    {

        Serial.println("Hello from task 1");
        delay(1000);
    }

    Serial.println("Ending task 1");
    vTaskDelete(NULL);
}

void taskTwo(void *parameter)
{

    for (int i = 0; i < 10; i++)
    {

        Serial.println("Hello from task 2");
        delay(1000);
    }
    Serial.println("Ending task 2");
    vTaskDelete(NULL);
}

void initiate_create_task_example()
{

    xTaskCreate(
        taskOne,   /* Task function. */
        "TaskOne", /* String with name of task. */
        10000,   /* Stack size in bytes. */
        NULL,     /* Parameter passed as input of the task */
        1,         /* Priority of the task. */
        NULL);   /* Task handle. */

    xTaskCreate(
        taskTwo,   /* Task function. */
        "TaskTwo", /* String with name of task. */
        10000,   /* Stack size in bytes. */
        NULL,     /* Parameter passed as input of the task */
        1,         /* Priority of the task. */
        NULL);   /* Task handle. */
}

GetExecutionCore.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "Common.h"
#include <Arduino.h>
void genericTask(void *parameter)
{
    Serial.print("Created task: Executing on core ");
    Serial.println(xPortGetCoreID());
    vTaskDelete(NULL);
}

void setup_executionCore()
{
    Serial.print("Setup: Executing on core ");
    Serial.println(xPortGetCoreID());

    xTaskCreate(
        genericTask,   /* Task function. */
        "genericTask", /* String with name of task. */
        10000,         /* Stack size in words. */
        NULL,          /* Parameter passed as input of the task */
        2,             /* Priority of the task. */
        NULL);         /* Task handle. */
    delay(2000);
}

void  loop_getExecutionCore(){
  Serial.print("Main Loop: Executing on core ");
  Serial.println(xPortGetCoreID());
  delay(1000);
}

QueuesBetweenTask.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <Arduino.h>
#include "Common.h"

QueueHandle_t queue;
int queueSize = 10;

void producerTask( void * parameter )
{

    for( int i = 0;i<queueSize;i++ ){
      xQueueSend(queue, &i, portMAX_DELAY);
    }

    vTaskDelete( NULL );

}

void consumerTask( void * parameter)
{
    int element;

    for( int i = 0;i < queueSize;i++ ){

        xQueueReceive(queue, &element, portMAX_DELAY);
        Serial.print(element);
        Serial.print("|");
    }

    vTaskDelete( NULL );

}

void setup_QueuesBetweenTask()
{
    queue = xQueueCreate(queueSize, sizeof(int));

    if (queue == NULL)
    {
        Serial.println("Error creating the queue");
    }

    xTaskCreate(
        producerTask, /* Task function. */
        "Producer",   /* String with name of task. */
        10000,        /* Stack size in words. */
        NULL,         /* Parameter passed as input of the task */
        1,            /* Priority of the task. */
        NULL);        /* Task handle. */

    xTaskCreate(
        consumerTask, /* Task function. */
        "Consumer",   /* String with name of task. */
        10000,        /* Stack size in words. */
        NULL,         /* Parameter passed as input of the task */
        1,            /* Priority of the task. */
        NULL);        /* Task handle. */
}

void loop_QueuesBetweenTask()
{
    delay(100000);
}