0
0
FreeRTOSprogramming~5 mins

Why RTOS over bare-metal in FreeRTOS

Choose your learning style9 modes available
Introduction

An RTOS helps manage many tasks easily and reliably, while bare-metal means you handle everything yourself. RTOS makes your program organized and responsive.

When you need to run multiple tasks at the same time, like reading sensors and controlling motors.
When your program must respond quickly to events, such as button presses or alarms.
When you want your code to be easier to maintain and update over time.
When you need to share resources safely between different parts of your program.
When your project grows bigger and bare-metal code becomes too complex to manage.
Syntax
FreeRTOS
No specific syntax for this concept, but using FreeRTOS involves creating tasks, starting the scheduler, and using synchronization tools.

RTOS stands for Real-Time Operating System.

Bare-metal means programming directly on hardware without an operating system.

Examples
In bare-metal, you write a loop that runs tasks one after another.
FreeRTOS
// Bare-metal example: simple loop
while(1) {
  // do task 1
  // do task 2
}
In FreeRTOS, you create tasks and start the scheduler to run them concurrently.
FreeRTOS
// FreeRTOS example: create two tasks
xTaskCreate(Task1, "Task 1", 1000, NULL, 1, NULL);
xTaskCreate(Task2, "Task 2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
Sample Program

This program creates two tasks that print messages at different intervals. The RTOS scheduler runs them so both tasks appear to run at the same time.

FreeRTOS
#include "FreeRTOS.h"
#include "task.h"
#include <stdio.h>

void Task1(void *pvParameters) {
    while(1) {
        printf("Task 1 is running\n");
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

void Task2(void *pvParameters) {
    while(1) {
        printf("Task 2 is running\n");
        vTaskDelay(pdMS_TO_TICKS(1500));
    }
}

int main(void) {
    xTaskCreate(Task1, "Task 1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "Task 2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    for(;;); // Should never reach here
    return 0;
}
OutputSuccess
Important Notes

RTOS helps your program handle many things without missing important events.

Bare-metal is simpler but can get messy with many tasks.

Using RTOS can make your code easier to read and maintain.

Summary

RTOS manages multiple tasks smoothly and reliably.

Bare-metal requires manual task management and can be complex.

RTOS improves responsiveness and code organization.