osal-rs-build
Build utilities for osal-rs - FreeRTOS type generation at compile time.
Overview
osal-rs-build is a build-time utility crate that automatically generates Rust type mappings for FreeRTOS primitives. It queries the sizes of FreeRTOS types and creates corresponding Rust type definitions, ensuring type safety and compatibility across different target platforms.
Features
- Automatic Type Detection: Detects FreeRTOS type sizes at build time
- Platform Agnostic: Works with different architectures (ARM Cortex-M, RISC-V, etc.)
- Compile-time Generation: Creates type mappings during the build process
- Zero Runtime Overhead: All processing happens at build time
Supported FreeRTOS Types
The crate generates Rust mappings for the following FreeRTOS types:
| FreeRTOS Type | Description | Rust Mapping |
|---|---|---|
TickType_t |
Timer tick counter | u8, u16, u32, or u64 |
UBaseType_t |
Unsigned base type | u8, u16, u32, or u64 |
BaseType_t |
Signed base type | i8, i16, i32, or i64 |
StackType_t |
Stack element type | u8, u16, u32, or u64 |
Installation
Add this to your Cargo.toml:
[]
= "0.3"
Usage
In your build.rs file:
use FreeRtosTypeGenerator;
With Custom FreeRTOS Config
If you have a custom FreeRTOSConfig.h location:
use FreeRtosTypeGenerator;
In Your Rust Code
After running the build script, include the generated types in your code:
// In your lib.rs or main.rs
include!;
// Now you can use the generated types
How It Works
- Build Time Detection: The generator creates a small C program to query FreeRTOS type sizes
- Compilation: Compiles and executes the query program using GCC
- Type Mapping: Maps detected sizes to appropriate Rust types
- Code Generation: Writes generated type aliases to
types_generated.rsin theOUT_DIR
Default Values
If type detection fails (e.g., no C compiler available), the generator falls back to default values suitable for 32-bit ARM Cortex-M platforms (like Raspberry Pi Pico):
TickType_t: 4 bytes →u32UBaseType_t: 4 bytes →u32BaseType_t: 4 bytes (signed) →i32StackType_t: 4 bytes →u32
Requirements
- Rust 1.85.0 or later
- GCC compiler (for type detection, optional)
- FreeRTOS headers (optional, for custom configurations)
Example Projects
This crate is used by:
- osal-rs - Operating System Abstraction Layer for Rust
- hi-happy-garden-rs - Embedded Rust project for Raspberry Pi Pico
Build Script Example
Complete build.rs example:
use FreeRtosTypeGenerator;
API Documentation
FreeRtosTypeGenerator
The main struct for generating FreeRTOS type mappings.
Methods
new() -> Self: Creates a new generator with default settingswith_config_path<P: Into<PathBuf>>(config_path: P) -> Self: Creates a generator with a custom FreeRTOS config pathset_config_path<P: Into<PathBuf>>(&mut self, config_path: P): Sets the FreeRTOS config pathgenerate_types(&self): Generates only type mappingsgenerate_all(&self): Generates types and configuration constants
License
This project is licensed under the GNU Lesser General Public License v2.1 or later - see the LICENSE file for details.
Author
Antonio Salsi - passy.linux@zresa.it
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.