Hello World in FreeRTOS

What is a Real-Time Operating System (RTOS)

A Real-Time Operating System (RTOS) is an operating system specifically designed for real-time applications. Real-time applications require predictable responses within specific time constraints, so RTOS focuses on providing an emphasis on timing constraints to ensure that the system can meet real-time performance requirements.

1. Real-Time Performance:

  • Hard Real-Time and Soft Real-Time: RTOS can be classified into hard real-time and soft real-time based on the requirements for real-time performance. Hard real-time systems have very strict requirements for task response times, and any task that fails to complete within the specified time is considered a failure. Soft real-time systems also have time requirements but are relatively flexible; failing to complete a task within the specified time does not lead to system failure.

2. Task Scheduling:

  • Real-Time Scheduling Algorithms: RTOS uses real-time scheduling algorithms to determine which task will run on the processor. These algorithms consider task priority, deadlines, and other real-time requirements.
  • Time-Slicing: In some cases, RTOS may use time-slicing scheduling to ensure that each task has a chance to run, preventing any single task from monopolizing the processor for an extended period.

3. Task Management:

  • Task Creation and Deletion: RTOS allows for the creation and deletion of tasks. Tasks can be independent units that perform specific functions.
  • Task Synchronization and Communication: Mechanisms are provided for tasks to synchronize and communicate, such as semaphores, mutexes, message queues, etc.

4. Memory Management:

  • Dynamic Memory Allocation: Some RTOS support dynamic memory allocation, allowing memory to be allocated and released at runtime.
  • Static Memory Allocation: To improve predictability, some RTOS may support static memory allocation, which allocates memory for tasks at compile time.

5. Interrupt Handling:

  • Interrupt Service Routines (ISR): RTOS allows developers to write interrupt service routines to handle hardware interrupt events.
  • Interrupt Priority: Interrupt service routines can have different priorities, and RTOS ensures that high-priority interrupts can interrupt lower-priority interrupts or tasks.

6. Real-Time Clocks and Timers:

  • Real-Time Clock: Support for real-time clocks is provided so that tasks can be scheduled and synchronized based on actual time.
  • Timers: Allows for the setting and management of timers to trigger tasks or events at specified times.

7. Error Handling:

  • Error Detection and Handling: RTOS typically provides error detection and handling mechanisms to ensure that the system can take appropriate actions in the event of an error.

8. Application Areas of RTOS:

  • Embedded Systems: RTOS is widely used in embedded systems, such as automotive control units, medical devices, industrial automation, etc.
  • Communication Systems: In communication systems, RTOS is used to manage network devices, routers, and switches.

9. Examples of RTOS:

  • FreeRTOS: An open-source real-time operating system widely used in embedded systems.
  • VxWorks: A commercial RTOS for embedded real-time systems.
  • RTOS-32: A real-time operating system for the Windows platform.

10. Differences Between RTOS and General-Purpose Operating Systems:

  • Real-Time Performance: RTOS focuses on real-time performance, while general-purpose operating systems typically emphasize throughput and response time.
  • Kernel Size: RTOS usually has a smaller, more streamlined kernel to ensure fast startup and response.
  • Task Scheduling: RTOS uses real-time scheduling algorithms to ensure tasks are completed within specified time limits.

Real-time operating systems play a critical role in applications with strict real-time performance requirements. By providing support for task scheduling, synchronization, communication, and real-time clocks, RTOS enables developers to design and implement systems with high timing demands. In the fields of embedded systems and real-time control, RTOS has become an indispensable tool.

FreeRTOS[1]

FreeRTOS is an open-source real-time operating system widely used in embedded systems. FreeRTOS provides support for task scheduling, synchronization, communication, and real-time clocks, enabling developers to design and implement systems with high timing demands. FreeRTOS is distributed for free under the MIT open-source license, including a kernel and a continuously expanding set of IoT libraries suitable for all industry sectors. The build of FreeRTOS emphasizes reliability and ease of use.

The development activity of FreeRTOS has migrated from SVN to GitHub, and it can now be found directly on the FreeRTOS Github[2] page. Download the standard zip (.zip) file or self-extracting zip file (.exe) of FreeRTOS early versions[3] from GitHub. Unzip the source code while ensuring that the folder structure is not altered.

The official FreeRTOS download page has two versions of the download package: one is the latest version, and the other is the Long-Term Support (LTS) version. The latest version download package includes the latest FreeRTOS kernel[4], FreeRTOS-Plus libraries[5], and AWS IoT libraries[6], along with example projects. The Long-Term Support version download package contains the FreeRTOS LTS libraries, which include the FreeRTOS kernel and IoT libraries but do not include example projects. You can choose to download based on your needs.

Since I happen to have an ESP32-C3 small flight control board, the following playground will use this small board as an example. FreeRTOS is already integrated into the ESP-IDF, so we can directly use ESP-IDF to develop FreeRTOS applications.

Hello World

After configuring the ESP-IDF development environment, follow these steps to configure and implement this example project:

Step 1: Install ESP-IDF

Make sure you have installed ESP-IDF. You can follow Espressif’s official documentation ESP-IDF Installation Guide[7] for installation.

Step 2: Create a New Project

  1. Open a terminal or command prompt window and navigate to the directory where you want to create the project.

  2. Run the following command to create a new ESP-IDF project:

idf.py create-project hello_world

This will create a new directory with the default project structure.

Step 3: Enter the Project Directory

cd <project-name>

Step 4: Configure the Project

  1. Run the following command to configure the project:
idf.py menuconfig

This will open a menu configuration interface where you can configure various options for the project, including serial port settings, Wi-Fi configuration, component options, etc. After configuration, save and exit.

  1. (Optional) If you want to use VSCode or another IDE for development, you can run the following command to generate IDE project files:
idf.py vscode

This will generate configuration files compatible with Visual Studio Code.

Step 5: Compile and Flash

In the main directory, find the hello_world_main.c file and write the following content:

#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"

void app_main(void)
{
    printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100;
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 10; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
    printf("Restarting now.\n");
    fflush(stdout);
    esp_restart();
}

This code will output “Hello World!” and print some basic chip information.

  1. Use the following command to compile the project:
idf.py build
  1. Use the following command to flash the firmware to the ESP32:
idf.py -p <PORT> flash

Where <PORT> is the port your ESP32 development board is connected to your computer. You can use ls /dev/tty* (on Linux/macOS) or mode command (on Windows) to find the port.

Step 6: Monitor Serial Output

  1. Use the following command to start monitoring the serial output:
idf.py -p <PORT> monitor

This will open a window displaying the serial output from the ESP32. You can see the program’s debug information and logs.

  1. If you modify the code and recompile, you can use the following command to reflash and start monitoring:
idf.py -p <PORT> flash monitor

If successful, you should see output similar to the following in the console:

Hello world!
This is esp32c3 chip with 1 CPU core(s), WiFi/BLE, silicon revision v0.4, 2MB external flash
Minimum free heap size: 330392 bytes
Restarting in 10 seconds...
Restarting in 9 seconds...
Restarting in 8 seconds...
Restarting in 7 seconds...
Restarting in 6 seconds...
Restarting in 5 seconds...
Restarting in 4 seconds...
Restarting in 3 seconds...
Restarting in 2 seconds...
Restarting in 1 seconds...
Restarting in 0 seconds...
Restarting now.

References

[1]

FreeRTOS: https://www.freertos.org/

[2]

Github: https://github.com/FreeRTOS

[3]

FreeRTOS Early Versions: https://github.com/FreeRTOS/FreeRTOS/releases

[4]

FreeRTOS Kernel: https://www.freertos.org/zh-cn-cmn-s/RTOS.html

[5]

FreeRTOS-Plus Libraries: https://www.freertos.org/zh-cn-cmn-s/FreeRTOS-Plus/index.html

[6]

AWS IoT Libraries: https://www.freertos.org/zh-cn-cmn-s/iot-libraries.html

[7]

ESP-IDF Installation Guide: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html

Leave a Comment