Skip to content

Commit c55a2d0

Browse files
authored
Merge pull request #12 from foxg1ove1/rtt-rust
Rtt riscv rust support
2 parents abdaf86 + e75bcf6 commit c55a2d0

Some content is hidden

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

90 files changed

+7012
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ vdso.lds
6363
# stm32cubemx
6464
**/CubeMX_Config/Drivers/
6565
**/CubeMX_Config/MDK-ARM/
66+
67+
# Rust
68+
target/
69+
Cargo.lock
70+
*.rs.bk
71+
*.pdb
72+
73+
# rust-analyzer
74+
**/target/rust-analyzer/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
mnt.c
22
romfs_data.c
33
opensbi
4+
build
5+
/*.config
6+
/*.lds
7+
/*.sh
8+
/rtconfig*
9+
text.txt
10+
fat
11+
applications/*
12+
/.config
13+
/link.lds
14+
/rtconfig.h
15+
/rtconfig.py
16+
/Kconfig

machines/qemu-virt-riscv64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ PKGS_DIR := packages
99
source "$(RTT_DIR)/Kconfig"
1010
osource "$PKGS_DIR/Kconfig"
1111
rsource "driver/Kconfig"
12+
rsource "rust/Kconfig"
1213

1314
config BOARD_QEMU_VIRT_RV64
1415
bool
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
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# RT-Thread Rust组件目录结构建议
2+
3+
## 一、目录结构
4+
5+
`rust/` 为以下结构:
6+
7+
```
8+
rust/
9+
├── Kconfig # 组件配置
10+
├── SConscript # 构建脚本
11+
├── README.md # 组件说明
12+
├── docs/ # 文档
13+
│ ├── api_reference.md
14+
│ ├── abi_compatibility.md
15+
│ └── performance_report.md
16+
├── core/ # 核心支持库
17+
│ ├── Cargo.toml
18+
│ ├── src/
19+
│ │ ├── lib.rs
20+
│ │ ├── bindings/ # C接口绑定
21+
│ │ │ ├── kernel.rs # 内核API
22+
│ │ │ ├── thread.rs # 线程API
23+
│ │ │ ├── ipc.rs # IPC机制
24+
│ │ │ ├── memory.rs # 内存管理
25+
│ │ │ └── device.rs # 设备API
26+
│ │ ├── rt_prelude.rs # 预导入模块
27+
│ │ ├── allocator.rs # 内存分配器
28+
│ │ ├── panic.rs # panic处理
29+
│ │ └── macros/ # 宏支持
30+
│ │ ├── thread_entry.rs # 线程入口宏
31+
│ │ └── msh_export.rs # Shell命令导出宏
32+
├── runtime/ # 运行时支持
33+
│ ├── Cargo.toml
34+
│ ├── src/
35+
│ │ ├── no_std_support.rs # no_std模式
36+
│ │ ├── start.rs # 启动代码
37+
│ │ └── lang_items.rs # 语言项
38+
│ └── linker/
39+
│ └── rust_module.ld # 链接脚本
40+
├── shell/ # Shell命令支持
41+
│ ├── Cargo.toml
42+
│ └── src/
43+
│ └── commands.rs
44+
├── examples/ # 示例代码
45+
│ ├── README.md
46+
│ ├── applications/ # 应用示例
47+
│ │ ├── hello_world/
48+
│ │ ├── thread_sync/
49+
│ │ └── device_io/
50+
│ ├── components/ # 组件示例
51+
│ │ ├── logger/
52+
│ │ ├── sensor_driver/
53+
│ │ └── protocol_stack/
54+
│ └── modules/ # 内核模块示例
55+
│ ├── simple_module/
56+
│ └── device_module/
57+
├── tools/ # 工具脚本
58+
│ ├── build_rust.py
59+
│ ├── gen_bindings.sh
60+
│ └── cargo_wrapper.py
61+
└── tests/ # 测试
62+
├── integration/
63+
└── unit/
64+
```
65+
66+
## 二、各部分功能说明
67+
68+
### 2.1 核心支持库 (core/)
69+
70+
**提供Rust本身的基础支持和RT-Thread系统服务绑定:**
71+
72+
1. **bindings/** - C接口的安全封装
73+
- 按功能模块划分(kernel、thread、ipc、memory、device)
74+
- 使用bindgen自动生成或手工编写
75+
- 提供类型安全的Rust接口
76+
77+
2. **rt_prelude.rs** - 预导入模块
78+
- 类似std::prelude,导入常用类型和trait
79+
- 简化Rust代码编写
80+
81+
3. **allocator.rs** - 全局内存分配器
82+
- 实现`GlobalAlloc` trait
83+
- 对接RT-Thread的内存管理系统
84+
85+
4. **macros/** - 宏定义
86+
- `rt_thread_main!` - 程序入口宏,标记Rust的main函数
87+
- `rt_component_export!` - 导出组件初始化入口的宏
88+
- `rt_app_export!` - 导出应用初始化入口的宏
89+
- `msh_cmd_export!` - 导出shell命令的宏
90+
- 简化在no_std模式下的开发
91+
92+
### 2.2 运行时支持 (runtime/)
93+
94+
**解决no_std模式下的运行时问题:**
95+
96+
- **no_std_support.rs** - no_std环境支持
97+
- **start.rs** - 启动代码,处理main函数到RT-Thread线程的转换
98+
- **lang_items.rs** - 必要的语言项实现
99+
- **linker/** - 链接脚本,用于模块加载
100+
101+
### 2.3 Shell命令支持 (shell/)
102+
103+
**导出Rust命令到RT-Thread shell:**
104+
105+
- 提供`MSH_CMD_EXPORT`宏的Rust版本
106+
- 自动处理参数解析
107+
- 与finsh组件集成
108+
109+
### 2.4 示例代码 (examples/)
110+
111+
**包含三类示例:**
112+
113+
1. **applications/** - 使用Rust编写应用
114+
- `hello_world/` - 基础示例
115+
- `thread_sync/` - 线程同步示例
116+
- `device_io/` - 设备IO示例
117+
118+
2. **components/** - 使用Rust编写组件/软件包
119+
- `logger/` - 日志组件
120+
- `sensor_driver/` - 传感器驱动
121+
- `protocol_stack/` - 协议栈
122+
123+
3. **modules/** - 使用Rust编写内核动态模块
124+
- `simple_module/` - 简单模块示例
125+
- `device_module/` - 设备驱动模块示例
126+
127+
## 三、构建系统集成
128+
129+
### 3.1 Kconfig配置示例
130+
131+
```kconfig
132+
menuconfig RT_USING_RUST
133+
bool "Rust Language Support"
134+
default n
135+
if RT_USING_RUST
136+
config RT_RUST_CORE
137+
bool "Enable Rust Core Library"
138+
default y
139+
config RT_RUST_RUNTIME
140+
bool "Enable Rust Runtime Support"
141+
default y
142+
select RT_RUST_CORE
143+
config RT_RUST_MSH_SUPPORT
144+
bool "Enable Rust MSH Command Support"
145+
default y
146+
depends on RT_USING_FINSH
147+
menu "Rust Examples"
148+
config RT_RUST_EXAMPLES_APPS
149+
bool "Build Application Examples"
150+
default n
151+
config RT_RUST_EXAMPLES_COMPONENTS
152+
bool "Build Component Examples"
153+
default n
154+
config RT_RUST_EXAMPLES_MODULES
155+
bool "Build Module Examples"
156+
default n
157+
depends on RT_USING_LWP && RT_USING_MODULE
158+
endmenu
159+
endif
160+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# for module compiling
2+
import os
3+
from building import *
4+
5+
cwd = GetCurrentDir()
6+
objs = []
7+
list = os.listdir(cwd)
8+
9+
for d in list:
10+
path = os.path.join(cwd, d)
11+
if os.path.isfile(os.path.join(path, 'SConscript')):
12+
objs = objs + SConscript(os.path.join(d, 'SConscript'))
13+
14+
Return('objs')
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Rust build artifacts (now in build/rust)
2+
/target/
3+
**/*.rs.bk
4+
*.pdb
5+
6+
# Cargo lock file (optional - uncomment if you want to exclude it)
7+
# Cargo.lock
8+
9+
# Build directories
10+
/build/
11+
*.o
12+
*.a
13+
*.so
14+
*.dylib
15+
*.dll
16+
17+
# IDE specific files
18+
.idea/
19+
.vscode/
20+
*.swp
21+
*.swo
22+
*~
23+
.DS_Store
24+
25+
# Debug files
26+
*.dSYM/
27+
*.su
28+
*.idb
29+
*.pdb
30+
31+
# Backup files
32+
*.bak
33+
*.tmp
34+
*.temp
35+
36+
# Log files
37+
*.log
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "rt-rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lib]
7+
name = "rt_rust"
8+
crate-type = ["rlib", "staticlib"]
9+
10+
[features]
11+
default = []
12+
smp = []
13+
14+
[profile.dev]
15+
panic = "abort"
16+
17+
[profile.release]
18+
panic = "abort"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
from building import *
3+
4+
cwd = GetCurrentDir()
5+
6+
sys.path.append(os.path.join(cwd, '../tools'))
7+
from build_support import (
8+
detect_rust_target,
9+
make_rustflags,
10+
collect_features,
11+
verify_rust_toolchain,
12+
ensure_rust_target_installed,
13+
cargo_build_staticlib,
14+
clean_rust_build,
15+
)
16+
def _has(sym: str) -> bool:
17+
try:
18+
return bool(GetDepend([sym]))
19+
except Exception:
20+
return bool(GetDepend(sym))
21+
22+
23+
# Source files – MSH command glue
24+
src = ['rust_cmd.c']
25+
LIBS = []
26+
LIBPATH = []
27+
28+
if GetOption('clean'):
29+
# Register Rust artifacts for cleaning
30+
rust_build_dir = clean_rust_build(Dir('#').abspath)
31+
if os.path.exists(rust_build_dir):
32+
print(f'Registering {rust_build_dir} for cleanup')
33+
Clean('.', rust_build_dir)
34+
else:
35+
print('No rust build artifacts to clean')
36+
else:
37+
if verify_rust_toolchain():
38+
import rtconfig
39+
40+
target = detect_rust_target(_has, rtconfig)
41+
if not target:
42+
print('Error: Unable to detect Rust target; please check configuration')
43+
else:
44+
print(f'Detected Rust target: {target}')
45+
46+
# Optional hint if target missing
47+
ensure_rust_target_installed(target)
48+
49+
# Build mode and features
50+
debug = bool(_has('RUST_DEBUG_BUILD'))
51+
features = collect_features(_has)
52+
53+
rustflags = make_rustflags(rtconfig, target)
54+
rust_lib = cargo_build_staticlib(
55+
rust_dir=cwd, target=target, features=features, debug=debug, rustflags=rustflags
56+
)
57+
58+
if rust_lib:
59+
LIBS = ['rt_rust']
60+
LIBPATH = [os.path.dirname(rust_lib)]
61+
print('Rust library linked successfully')
62+
else:
63+
print('Warning: Failed to build Rust library')
64+
else:
65+
print('Warning: Rust toolchain not found')
66+
print('Please install Rust from https://rustup.rs')
67+
68+
# Define component group for SCons
69+
group = DefineGroup('rust', src, depend=['RT_USING_RUST'], LIBS=LIBS, LIBPATH=LIBPATH)
70+
71+
Return('group')

0 commit comments

Comments
 (0)