Choosing Between Bare-Metal Systems and Operating Systems in MCU Development

In the field of microcontroller (MCU) development, developers often face architectural choices: whether to adopt a bare-metal system or a real-time operating system (RTOS). Below, we will analyze the applicable scenarios for both options through C language code examples and system flowcharts.

1. Applicable Scenarios for Bare-Metal Systems

1.1 Resource-Constrained Projects

When MCU resources are limited (ROM<32KB, RAM<4KB), such as in the STM32F030 series:

// Typical bare-metal system structure
int main() {
    hardware_init();
    while(1) {
        read_sensors();  // Sensor reading
        process_data();  // Data processing
        control_actuators(); // Actuator control
        handle_interrupts(); // Interrupt handling
    }
}

Flowchart:Choosing Between Bare-Metal Systems and Operating Systems in MCU Development

1.2 Single-Task Scenarios

Suitable for simple devices controlled by state machines (e.g., thermostats):

enum states { IDLE, HEATING, COOLING };
enum states current_state = IDLE;
void state_machine() {
    switch(current_state) {
        case IDLE:
            if(temp < target) current_state = HEATING;
            break;
        case HEATING:
            // Heating logic
            break;
        // ...other states
    }
}

1.3 Hard Real-Time Requirements

Industrial control systems requiring microsecond-level response:

void TIM1_IRQHandler() { // Timer interrupt
    static uint32_t counter = 0;
    if(counter++ >= 1000) {
        emergency_stop(); // Execute safety operation precisely
        counter = 0;
    }
}

2. Applicable Scenarios for Operating Systems

2.1 Multi-Tasking Concurrent Needs

Example of a smart home gateway using FreeRTOS:

void vSensorTask(void *pvParams) {
    while(1) {
        xQueueSend(sensor_queue, &sensor_data, portMAX_DELAY);
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}
void vNetworkTask(void *pvParams) {
    while(1) {
        xQueueReceive(sensor_queue, &data, portMAX_DELAY);
        send_to_cloud(data);
    }
}
int main() {
    xTaskCreate(vSensorTask, "Sensor", 128, NULL, 2, NULL);
    xTaskCreate(vNetworkTask, "Network", 256, NULL, 1, NULL);
    vTaskStartScheduler();
}

Flowchart:Choosing Between Bare-Metal Systems and Operating Systems in MCU Development

2.2 Complex System Management

IoT devices requiring a file system and TCP/IP stack:

void http_server_task() {
    lwip_init();
    while(1) {
        process_http_request(); // Using OS-provided socket API
        vTaskDelay(10);
    }
}

2.3 Dynamic Task Creation

Industrial controllers supporting functional expansion:

void create_new_task() {
    xTaskCreate(dynamic_task, "DynTask", 256, NULL, 3, &xHandle);
}

3. Comparison Decision Matrix

Evaluation Dimension Bare-Metal System RTOS System
Memory Usage Typically <5KB Typically >10KB
Task Response Time Good determinism (μs level) Scheduling delay (ms level)
Development Complexity Simple (linear programming) Requires understanding of task model
System Scalability Limited Easy to expand
Typical Application Scenarios Power tools, simple controllers Smart home, industrial gateways

4. Hybrid Architecture Practices

4.1 Time-Critical Hybrid Solutions

void TIM2_IRQHandler() { // High priority interrupt
    critical_task();     // Bare-metal direct handling
}
void rtos_task() {       // RTOS task
    noncritical_task();
}

5. Selection Practice Recommendations

  1. Use bare-metal to validate core algorithms in the early stages of the project

  2. Consider migrating to RTOS when the following situations arise:

  • More than 5 functional modules

  • Need for third-party protocol stack integration

  • Priority conflict issues arise

  • Use SysTick for benchmarking:

  • void SysTick_Handler() {
        static uint32_t ticks = 0;
        ticks++;
        if(ticks%1000 == 0) {
            check_system_load(); // Evaluate system load
        }
    }

    Follow me for more technical insights

    Leave a Comment