0
0
Embedded Cprogramming~7 mins

Bare-metal vs RTOS execution model in Embedded C

Choose your learning style9 modes available
Introduction

Understanding how programs run on small devices helps you choose the right way to control hardware. Bare-metal and RTOS are two ways to run code on embedded systems.

When you want simple control of hardware without extra software layers.
When you need to run multiple tasks at the same time in an organized way.
When your device has limited memory and you want to keep things small.
When your project needs precise timing and task management.
When you want to learn how embedded systems work at a low level.
Syntax
Embedded C
/* Bare-metal main loop example */
int main() {
    while(1) {
        // Run tasks one by one
    }
    return 0;
}

/* RTOS task example */
void Task1(void *params) {
    while(1) {
        // Task code
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

int main() {
    xTaskCreate(Task1, "Task 1", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}

Bare-metal code runs in a simple loop without an operating system.

RTOS uses tasks and a scheduler to manage multiple jobs at once.

Examples
This code runs forever, turning an LED on and off with a delay.
Embedded C
/* Bare-metal example: toggle LED */
int main() {
    while(1) {
        toggleLED();
        delay(500);
    }
}
This RTOS code runs two tasks blinking different LEDs at different speeds.
Embedded C
/* RTOS example: two tasks blinking LEDs */
void Task1(void *params) {
    while(1) {
        toggleLED1();
        vTaskDelay(500 / portTICK_PERIOD_MS);
    }
}

void Task2(void *params) {
    while(1) {
        toggleLED2();
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

int main() {
    xTaskCreate(Task1, "LED1", 1000, NULL, 1, NULL);
    xTaskCreate(Task2, "LED2", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    return 0;
}
Sample Program

This program counts from 0 to 4 and prints each number. It shows a simple bare-metal style loop without multitasking.

Embedded C
/* Simple bare-metal loop printing numbers */
#include <stdio.h>

int main() {
    int count = 0;
    while(count < 5) {
        printf("Count: %d\n", count);
        count++;
    }
    return 0;
}
OutputSuccess
Important Notes

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

RTOS helps organize multiple tasks but needs more memory and setup.

Choosing depends on your project size, complexity, and hardware limits.

Summary

Bare-metal runs one task in a loop, simple and direct.

RTOS runs many tasks with a scheduler, better for complex jobs.

Pick bare-metal for small, simple projects; pick RTOS for multitasking and timing needs.