0
0
FreeRTOSprogramming~5 mins

Hard real-time vs soft real-time in FreeRTOS

Choose your learning style9 modes available
Introduction

Understanding hard and soft real-time helps you design systems that respond on time. This is important for safety and performance.

When building a system that controls a car's brakes (hard real-time).
When making a video player that should avoid delays but can tolerate some (soft real-time).
When programming a robot arm that must move exactly on schedule (hard real-time).
When creating a sensor data logger where occasional delays are okay (soft real-time).
Syntax
FreeRTOS
// FreeRTOS does not have special syntax for hard or soft real-time.
// It depends on how you design tasks and priorities.

// Example task creation:
xTaskCreate(TaskFunction, "TaskName", stackSize, parameters, priority, &taskHandle);

FreeRTOS uses task priorities to help meet timing needs.

Hard real-time means missing a deadline is a failure; soft real-time means occasional misses are allowed.

Examples
This task runs with the highest priority to meet strict deadlines.
FreeRTOS
// Hard real-time task example
void HardRealTimeTask(void *pvParameters) {
    TickType_t xLastWakeTime = xTaskGetTickCount();
    const TickType_t xFrequency = pdMS_TO_TICKS(50);
    for(;;) {
        // Do critical work
        vTaskDelayUntil(&xLastWakeTime, xFrequency);
    }
}

// Created with high priority
xTaskCreate(HardRealTimeTask, "HardTask", 1000, NULL, configMAX_PRIORITIES - 1, NULL);
This task runs with lower priority and can tolerate some delay.
FreeRTOS
// Soft real-time task example
void SoftRealTimeTask(void *pvParameters) {
    for(;;) {
        // Do less critical work
        vTaskDelay(pdMS_TO_TICKS(100));
    }
}

// Created with lower priority
xTaskCreate(SoftRealTimeTask, "SoftTask", 1000, NULL, 1, NULL);
Sample Program

This program creates two tasks: one hard real-time with high priority running every 50 ms, and one soft real-time with lower priority running every 200 ms.

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

void HardRealTimeTask(void *pvParameters) {
    TickType_t xLastWakeTime = xTaskGetTickCount();
    const TickType_t xFrequency = pdMS_TO_TICKS(50); // 50 ms
    for(;;) {
        printf("Hard real-time task running\n");
        vTaskDelayUntil(&xLastWakeTime, xFrequency);
    }
}

void SoftRealTimeTask(void *pvParameters) {
    for(;;) {
        printf("Soft real-time task running\n");
        vTaskDelay(pdMS_TO_TICKS(200)); // 200 ms
    }
}

int main(void) {
    xTaskCreate(HardRealTimeTask, "HardTask", 1000, NULL, configMAX_PRIORITIES - 1, NULL);
    xTaskCreate(SoftRealTimeTask, "SoftTask", 1000, NULL, 1, NULL);
    vTaskStartScheduler();
    for(;;); // Should never reach here
    return 0;
}
OutputSuccess
Important Notes

Hard real-time tasks must finish work before their deadline every time.

Soft real-time tasks can miss deadlines sometimes without big problems.

Use task priorities and timing functions in FreeRTOS to manage real-time behavior.

Summary

Hard real-time means strict deadlines that must never be missed.

Soft real-time means deadlines are important but can be missed occasionally.

FreeRTOS helps by letting you set task priorities and delays to control timing.