Skip to content

Commit 69980f8

Browse files
authored
Adding Rust Language Support for RT-Thread #10910
1 parent cd1d47b commit 69980f8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+6734
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
*.pyc
2+
**/Cargo.lock
3+
**/target/
24
*.map
35
*.dblite
46
*.elf

components/Kconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ rsource "lwp/Kconfig"
4848
endif
4949

5050
rsource "legacy/Kconfig"
51-
51+
rsource "rust/Kconfig"
5252
endmenu

components/SConscript

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ from building import *
55

66
objs = []
77
cwd = GetCurrentDir()
8-
list = os.listdir(cwd)
8+
entries = os.listdir(cwd)
99

10-
for item in list:
10+
for item in entries:
1111
if item in remove_components:
1212
continue
1313

1414
if os.path.isfile(os.path.join(cwd, item, 'SConscript')):
15-
objs = objs + SConscript(os.path.join(item, 'SConscript'))
15+
result = SConscript(os.path.join(item, 'SConscript'))
16+
if isinstance(result, (list, tuple)):
17+
objs.extend(result)
18+
else:
19+
objs.append(result)
1620

1721
Return('objs')

components/rust/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
menuconfig RT_USING_RUST
2+
bool "Enable Rust component support"
3+
default n
4+
help
5+
Enable Rust programming language support for RT-Thread.
6+
This allows you to write RT-Thread components using Rust.
7+
8+
if RT_USING_RUST
9+
config RT_RUST_CORE
10+
bool "Enable Rust Core Library"
11+
default y
12+
config RUST_DEBUG_BUILD
13+
bool "Build Rust code in debug mode"
14+
default n
15+
help
16+
Build Rust code with debug symbols and without optimizations.
17+
This increases binary size but helps with debugging.
18+
config RUST_INIT_COMPONENT
19+
bool "Auto-initialize Rust component"
20+
default y
21+
help
22+
Automatically initialize Rust component during RT-Thread startup.
23+
24+
rsource "examples/Kconfig"
25+
26+
endif

components/rust/README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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

Comments
 (0)