|
| 1 | +# RT-Thread Rust Component |
| 2 | + |
| 3 | +RT-Thread's general-purpose Rust component for the RTOS, with automatic detection of multiple architectures. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Multi-architecture support: automatically detects ARM, AArch64, and RISC-V target architectures. |
| 8 | +- Zero configuration: no manual platform target setup required. |
| 9 | +- Modular design: core modules and example code are clearly separated. |
| 10 | +- RT-Thread integration: full access to RT-Thread kernel APIs. |
| 11 | + |
| 12 | +## Project Layout |
| 13 | + |
| 14 | +``` |
| 15 | +rust/ |
| 16 | +├── README.md # Project documentation |
| 17 | +├── Kconfig # Configuration options |
| 18 | +├── SConscript # Top-level build script |
| 19 | +├── core/ # Core Rust library |
| 20 | +│ ├── Cargo.toml # Rust project config |
| 21 | +│ ├── SConscript # Core library build script |
| 22 | +│ ├── rust_cmd.c # MSH command registration |
| 23 | +│ └── src/ # Source directory |
| 24 | +│ ├── lib.rs # Library entry |
| 25 | +│ ├── init.rs # Component initialization |
| 26 | +│ ├── allocator.rs # Memory allocator |
| 27 | +│ ├── panic.rs # Panic handler |
| 28 | +│ ├── bindings/ # RT-Thread API FFI bindings |
| 29 | +│ ├── api/ # RT-Thread API Rust wrappers |
| 30 | +│ ├── prelude/ # Common imports |
| 31 | +│ ├── thread.rs # Thread operations |
| 32 | +│ ├── mutex.rs # Mutex |
| 33 | +│ ├── sem.rs # Semaphore |
| 34 | +│ ├── queue.rs # Message queue |
| 35 | +│ ├── time.rs # Time functions |
| 36 | +│ ├── fs.rs # Filesystem |
| 37 | +│ ├── libloader.rs # Dynamic library loading |
| 38 | +│ ├── param.rs # Parameter passing |
| 39 | +│ └── out.rs # Output functions |
| 40 | +├── rt_macros/ # Rust procedural macros |
| 41 | +│ ├── Cargo.toml # Macros crate config |
| 42 | +│ └── src/ # Macros source |
| 43 | +│ ├── lib.rs # Macros crate entry |
| 44 | +│ └── macros/ # Macro implementations |
| 45 | +│ ├── mod.rs # Module definitions |
| 46 | +│ ├── main.rs # main macro |
| 47 | +│ ├── component.rs # component export macro |
| 48 | +│ ├── app.rs # application export macro |
| 49 | +│ └── cmd.rs # command export macro |
| 50 | +├── examples/ # Example code |
| 51 | +│ ├── Kconfig # Examples config |
| 52 | +│ ├── SConscript # Examples build scripts |
| 53 | +│ ├── application/ # Application examples |
| 54 | +│ ├── component/ # Component examples |
| 55 | +│ └── modules/ # Dynamic module examples |
| 56 | +├── docs/ # Detailed documentation |
| 57 | +└── tools/ # Build tools |
| 58 | + ├── build_support.py # Build support functions |
| 59 | + ├── build_component.py # Component build tool |
| 60 | + ├── build_usrapp.py # User app build tool |
| 61 | + ├── feature_config_component.py # Component feature config |
| 62 | + └── feature_config_examples.py # Example feature config |
| 63 | +``` |
| 64 | + |
| 65 | +## Quick Start |
| 66 | + |
| 67 | +### Prerequisites |
| 68 | + |
| 69 | +1. Install Rust: |
| 70 | + |
| 71 | +```bash |
| 72 | +curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
| 73 | +``` |
| 74 | + |
| 75 | +2. Add target platforms (choose according to your architecture): |
| 76 | + |
| 77 | +```bash |
| 78 | +# RISC-V64 (soft-float) |
| 79 | +rustup target add riscv64imac-unknown-none-elf |
| 80 | + |
| 81 | +# ARM Cortex-M4 |
| 82 | +rustup target add thumbv7em-none-eabi |
| 83 | + |
| 84 | +# Add other targets that match your toolchain/ABI as needed |
| 85 | +``` |
| 86 | + |
| 87 | +### Build |
| 88 | + |
| 89 | +```bash |
| 90 | +# Enable the Rust component in menuconfig |
| 91 | +scons --menuconfig |
| 92 | +# Navigate to: Rust Component Support → Enable |
| 93 | + |
| 94 | +# Build |
| 95 | +scons |
| 96 | + |
| 97 | +# Clean |
| 98 | +scons -c |
| 99 | +``` |
| 100 | + |
| 101 | +## Supported Architectures |
| 102 | + |
| 103 | +| Architecture | Target | Auto-detect | |
| 104 | +| -------------- | --------------------------------- | ----------- | |
| 105 | +| Cortex-M3 | thumbv7m-none-eabi | ✓ | |
| 106 | +| Cortex-M4/M7 | thumbv7em-none-eabi | ✓ | |
| 107 | +| Cortex-M4F/M7F | thumbv7em-none-eabihf | ✓ | |
| 108 | +| ARMv7-A | armv7a-none-eabi | ✓ | |
| 109 | +| AArch64 | aarch64-unknown-none | ✓ | |
| 110 | +| RISC-V32 | riscv32ima[f]c-unknown-none-elf | ✓ | |
| 111 | +| RISC-V64 | riscv64[gc/imac]-unknown-none-elf | ✓ | |
| 112 | + |
| 113 | +The build system will automatically determine the correct target from the RT-Thread configuration. |
| 114 | + |
| 115 | +## Example MSH Commands |
| 116 | + |
| 117 | +- `rust_param_demo` — Parameter passing demo |
| 118 | +- `rust_thread_demo` — Thread demo |
| 119 | +- `rust_mutex_demo` — Mutex demo |
| 120 | +- `rust_queue_demo` — Queue demo |
| 121 | +- `rust_sem_demo` — Semaphore demo |
| 122 | +- `rust_dl_demo` — Dynamic module loading demo |
| 123 | +- `rust_fs_demo` — File and logging operations demo (requires logging component) |
| 124 | + |
| 125 | +## Configuration Options |
| 126 | + |
| 127 | +Available in menuconfig: |
| 128 | + |
| 129 | +- `RT_USING_RUST` - Enable/disable the Rust component |
| 130 | +- `RT_RUST_CORE` - Enable/disable the core support library |
| 131 | +- `RUST_INIT_COMPONENT` - Initialize automatically at startup |
| 132 | +- `RT_USING_RUST_EXAMPLES` |
| 133 | + - `RT_RUST_BUILD_APPLICATIONS`: Enable/disable user applications |
| 134 | + - `RT_RUST_BUILD_COMPONENTS`: Enable/disable components |
| 135 | + - `RT_RUST_BUILD_MODULES`: Enable/disable building dynamic modules |
| 136 | +- `RUST_DEBUG_BUILD` - Enable/disable debug build |
| 137 | + |
| 138 | +## Technical Details |
| 139 | + |
| 140 | +- No-std: embedded-friendly `#![no_std]` environment. |
| 141 | +- FFI: seamless C/Rust interoperability. |
| 142 | +- Static linking: produces `.a` library files. |
| 143 | +- Memory safety: compile-time guarantees from Rust. |
| 144 | +- Zero-cost abstractions: performance comparable to C. |
| 145 | + |
| 146 | +## Use Cases |
| 147 | + |
| 148 | +- Safety-critical code: leverage Rust's memory-safety guarantees. |
| 149 | +- Complex algorithms: use Rust's advanced abstraction capabilities. |
| 150 | +- Device drivers: type-safe hardware abstractions. |
| 151 | +- Network protocol stacks: safe packet handling. |
| 152 | +- Cryptography libraries: secure implementations that help prevent memory leaks. |
| 153 | + |
| 154 | +## Troubleshooting |
| 155 | + |
| 156 | +### Linker Error |
| 157 | + |
| 158 | +If you encounter the error "can't link double-float modules with soft-float modules": |
| 159 | + |
| 160 | +- The build system should auto-detect the correct ABI. |
| 161 | +- Check whether the compiler `-mabi` flag matches the Rust target. |
| 162 | + |
| 163 | +### Target Not Installed |
| 164 | + |
| 165 | +If a target is reported as not installed: |
| 166 | + |
| 167 | +```bash |
| 168 | +rustup target add <target-name> |
| 169 | +``` |
| 170 | + |
| 171 | +### Detection Failed |
| 172 | + |
| 173 | +If the target architecture cannot be detected: |
| 174 | + |
| 175 | +- Verify that the RT-Thread configuration is correct. |
| 176 | +- Inspect compiler flags in `rtconfig.py`. |
| 177 | + |
| 178 | +## License |
| 179 | + |
| 180 | +Apache-2.0 |
| 181 | + |
| 182 | +## References |
| 183 | + |
| 184 | +- https://docs.rust-embedded.org/ |
| 185 | +- https://www.rt-thread.org/document/site/ |
| 186 | +- https://doc.rust-lang.org/nomicon/ffi.html |
| 187 | +- https://riscv.org/technical/specifications/ |
0 commit comments