FreeRTOS-rust: A Powerful Tool Perfectly Combining Rust’s Safety with FreeRTOS’s Real-Time Capabilities

In recent years, the Rust programming language has gained increasing attention in the embedded systems field due to its memory safety and high performance. However, efficiently using a Real-Time Operating System (RTOS) in Rust remains a challenge. FreeRTOS-rust has emerged to provide developers with a simple and user-friendly way to seamlessly integrate Rust with the widely used FreeRTOS.

1. Introduction to FreeRTOS-rust

The FreeRTOS-rust project is based on <span>freertos.rs</span> and has undergone a series of improvements aimed at simplifying the process of using FreeRTOS in Rust. Compared to <span>freertos.rs</span>, FreeRTOS-rust has the following key features:

  • Rust Main Function: The application’s <span>main()</span> function is directly written in Rust, reducing the learning curve and allowing developers to leverage Rust’s advantages.
  • Rust Scheduler Startup: The FreeRTOS scheduler can be started directly from Rust code, achieving tighter integration.
  • Global Memory Allocator: The heap memory management module of FreeRTOS (e.g., <span>heap_x.c</span>) is used as the global memory allocator for Rust, simplifying memory management.
  • No Clang Skeleton Project Required: Eliminates the cumbersome steps of creating and maintaining a Clang skeleton project, allowing direct development in Rust.

These improvements significantly enhance development efficiency and code readability, lowering the barrier to entry for embedded Rust development.

2. How It Works

The core of FreeRTOS-rust lies in the <span>freertos-cargo-build</span> build dependency. This dependency is responsible for compiling the C source code of FreeRTOS and generating an archive file for linking with Rust applications. It internally utilizes the <span>cc</span> crate and the information provided in the <span>build.rs</span> file for compilation:

  • FreeRTOS Source Code Path: Specifies the path to the FreeRTOS kernel source code.
  • FreeRTOS Configuration Path: Specifies the path to the <span>FreeRTOSConfig.h</span> file, which contains the configuration information for FreeRTOS.
  • FreeRTOS Port Path: Specifies the relative path to the FreeRTOS port, such as the port for ARM Cortex-M3.
  • Optional C Code: Allows for the compilation of additional C code.

<span>freertos-rust</span> dependency provides an interface that allows Rust applications to access all functionalities of FreeRTOS. This architecture cleverly combines the maturity of FreeRTOS with the modern features of Rust, achieving a complementary advantage of both.

3. Usage Instructions

Using FreeRTOS-rust is very straightforward and mainly involves the following steps:

  1. 1. Obtain FreeRTOS: Download the FreeRTOS kernel source code from the GitHub repository:<span>https://github.com/FreeRTOS/FreeRTOS-Kernel</span>.
  2. 2. Add Dependencies: Add the following dependencies in the Cargo.toml file:
[dependencies]
freertos-rust = "*"

[build-dependencies]
freertos-cargo-build = "*"
  1. 3. Create build.rs: Create a <span>build.rs</span> file in the root directory of the project to configure FreeRTOS compilation options:
fn main(){
let mut b = freertos_cargo_build::Builder::new();

    b.freertos("./FreeRTOS-Kernel"); // FreeRTOS source path
    b.freertos_config("src"); // FreeRTOSConfig.h path
    b.freertos_port("GCC/ARM_CM3"); // Port path
    b.heap("heap_4.c"); // Heap allocator

    b.compile().unwrap_or_else(|e|{panic!("{}", e.to_string())});
}
  1. 4. Write Rust Application: Write your Rust application code and use the interface provided by <span>freertos-rust</span> to interact with FreeRTOS.
  2. 5. Compile and Run: Use <span>cargo build</span> to compile the project, then flash the generated binary to the target hardware.

4. C Compiler

<span>freertos-cargo-build</span> depends on the <span>cc</span> crate, so the C compiler can be specified by setting the <span>CC</span> environment variable. For ARM architecture, it is typically <span>arm-none-eabi-gcc</span>.

5. Examples

FreeRTOS-rust provides several examples covering different microcontroller architectures, such as Cortex-M33 (nRF9160), Cortex-M3 (STM32L151CBU6A), Cortex-M4 (STM32F411CEU6), and Windows platform. These examples can help developers quickly get started and understand how to use FreeRTOS-rust.

6. Conclusion

FreeRTOS-rust provides embedded developers with a powerful tool that perfectly combines Rust’s safety with FreeRTOS’s real-time capabilities. By simplifying the integration process of FreeRTOS, it lowers the barrier to embedded Rust development and improves development efficiency. Its clear documentation and rich examples enable developers to quickly get started and build efficient and reliable embedded applications.

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

Leave a Comment