FreeRTOS-rust: A Rust Embedded Development Library Simplifying the Use of FreeRTOS

In the field of embedded system development, the Rust language is increasingly favored for its advantages in safety, performance, and concurrency. However, seamlessly integrating Rust with the mature real-time operating system (RTOS) FreeRTOS has always been a challenge.<span>FreeRTOS-rust</span> project has emerged to provide developers with a convenient way to build efficient and reliable FreeRTOS applications using Rust.

FreeRTOS-rust: A Rust Embedded Development Library Simplifying the Use of FreeRTOS

What is FreeRTOS-rust?

<span>FreeRTOS-rust</span> is a Rust library that simplifies the process of using FreeRTOS in Rust. It is based on the <span>freertos.rs</span> project and has been improved to make it easier to use and address some shortcomings in <span>freertos.rs</span>. The main differences between <span>FreeRTOS-rust</span> and <span>freertos.rs</span> are:

  • Rust Main Function: The entry point of the application’s <span>main()</span> is written in Rust instead of C.

  • Rust Scheduler Startup: The FreeRTOS scheduler can be started from Rust code.

  • Global Memory Allocator: Uses FreeRTOS heap memory management (<span>MemMang/heap/heap_x.c</span>) as the global memory allocator for Rust.

  • No Clang Skeleton Project Required: Simplifies the project build process without the need for complex Clang auxiliary projects.

How FreeRTOS-rust Works

<span>FreeRTOS-rust</span> relies on the <span>freertos-cargo-build</span> build dependency. This dependency is responsible for compiling the C source code of FreeRTOS into a static library and linking it to your Rust application. The internal implementation uses the <span>cc</span> library and requires you to provide the following information in your <span>build.rs</span> file:

  • • Path to FreeRTOS source code

  • • Path to application-specific <span>FreeRTOSConfig.h</span> file

  • • Path to FreeRTOS port (e.g., port for ARM Cortex-M3 core)

  • • Optional: Additional C code to be compiled

How to Use FreeRTOS-rust

  1. 1. Get FreeRTOS Source Code: Clone the FreeRTOS kernel repository from GitHub: https://github.com/FreeRTOS/FreeRTOS-Kernel

  2. 2. Add Dependencies: Add the following dependencies to your Rust project’s <span>Cargo.toml</span> file:

[dependencies]
freertos-rust = "*"

[build-dependencies]
freertos-cargo-build = "*"
  1. 3. Configure Build Script: Add the following code snippet to your application’s <span>build.rs</span> file and modify it according to your actual paths:

fn main(){
let mut b = freertos_cargo_build::Builder::new();

// Path to FreeRTOS kernel or set ENV "FREERTOS_SRC" instead
    b.freertos("path/to/FreeRTOS-Kernel");
    b.freertos_config("src");// Location of `FreeRTOSConfig.h` 
    b.freertos_port("GCC/ARM_CM3");// Port dir relative to 'FreeRTOS-Kernel/portable' 
    b.heap("heap_4.c");// Set the heap_?.c allocator to use from 
// 'FreeRTOS-Kernel/portable/MemMang' (Default: heap_4.c)       

// b.get_cc().file("More.c");   // Optional additional C-Code to be compiled

    b.compile().unwrap_or_else(|e|{panic!("{}", e.to_string())});
}
  1. 4. Select C Compiler: <span>freertos-cargo-build</span> depends on the <span>cc</span> library. You can specify the C compiler by setting the <span>CC</span> environment variable. For ARM architecture, it is usually <span>arm-none-eabi-gcc</span>.

Features and Examples

<span>FreeRTOS-rust</span> provides an interface to access all FreeRTOS features, allowing you to create tasks, use semaphores, message queues, etc., in Rust. It offers rich functionality, making it easy for developers to build various embedded applications.

The project includes multiple examples covering different microcontroller architectures, such as:

  • • Cortex M33 (nRF9160)

  • • Cortex M3 (STM32L151CBU6A)

  • • Cortex M4 (STM32F411CEU6)

  • • Windows (simulated environment)

Conclusion

<span>FreeRTOS-rust</span> provides Rust developers with an efficient and safe way to develop FreeRTOS applications. Its simplified API and comprehensive example code lower the learning curve, enabling even less experienced developers to get started quickly. With it, you can fully leverage the advantages of Rust to build high-performance, reliable embedded systems.

Project address: https://github.com/lobaro/FreeRTOS-rust

Leave a Comment