Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "rt-thread"]
path = rt-thread
url = https://github.com/RT-Thread/rt-thread.git
url = git@github.com:RT-Thread/rt-thread.git
1 change: 1 addition & 0 deletions machines/qemu-virt-riscv64/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ PKGS_DIR := packages
source "$(RTT_DIR)/Kconfig"
osource "$PKGS_DIR/Kconfig"
rsource "driver/Kconfig"
rsource "rust/Kconfig"

config BOARD_QEMU_VIRT_RV64
bool
Expand Down
37 changes: 37 additions & 0 deletions machines/qemu-virt-riscv64/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Rust build artifacts (now in build/rust)
/target/
**/*.rs.bk
*.pdb

# Cargo lock file (optional - uncomment if you want to exclude it)
# Cargo.lock

# Build directories
/build/
*.o
*.a
*.so
*.dylib
*.dll

# IDE specific files
.idea/
.vscode/
*.swp
*.swo
*~
.DS_Store

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Backup files
*.bak
*.tmp
*.temp

# Log files
*.log
30 changes: 30 additions & 0 deletions machines/qemu-virt-riscv64/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "rt-rust"
version = "0.1.0"
edition = "2021"

[lib]
name = "rt_rust"
crate-type = ["staticlib"]

[dependencies]

[features]
default = []
example_hello = []

example_memory = []
example_string = []
example_printf = []
example_thread = []
example_dl = []
example_vec = []

[profile.release]
lto = true
opt-level = "z"
codegen-units = 1
panic = "abort"

[profile.dev]
panic = "abort"
64 changes: 64 additions & 0 deletions machines/qemu-virt-riscv64/rust/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
menuconfig RT_USING_RUST
bool "Enable Rust component support"
default n
help
Enable Rust programming language support for RT-Thread.
This allows you to write RT-Thread components using Rust.

if RT_USING_RUST

config RUST_DEBUG_BUILD
bool "Build Rust code in debug mode"
default n
help
Build Rust code with debug symbols and without optimizations.
This increases binary size but helps with debugging.

config RUST_EXAMPLE_HELLO
bool "Enable hello example"
default y
help
Enable the basic hello world example function.
config RUST_EXAMPLE_MEMORY
bool "Enable memory operations examples"
default y
help
Enable memory allocation and operation examples.

config RUST_EXAMPLE_STRING
bool "Enable string operations examples"
default y
help
Enable string manipulation examples.

config RUST_EXAMPLE_PRINTF
bool "Enable printf examples"
default y
help
Enable formatted output examples.

config RUST_EXAMPLE_THREAD
bool "Enable RT-Thread threading examples"
default y
help
Enable RT-Thread threading API examples.

config RUST_EXAMPLE_VEC
bool "Enable vector examples"
default y
help
Enable vector examples.

config RUST_EXAMPLE_DL
bool "Enable libdl examples"
default y
help
Enable libdl examples.

config RUST_INIT_COMPONENT
bool "Auto-initialize Rust component"
default y
help
Automatically initialize Rust component during RT-Thread startup.

endif
168 changes: 168 additions & 0 deletions machines/qemu-virt-riscv64/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
# RT-Thread Rust Component

A general-purpose Rust component for RT-Thread RTOS, supporting automatic multi-architecture detection.

## Features

- **Multi-architecture support**: Automatically detects ARM, AArch64, and RISC-V target architectures
- **Zero configuration**: No manual platform setup required
- **Modular design**: Core modules and example code are clearly separated
- **RT-Thread integration**: Full access to RT-Thread kernel APIs

## Project Structure

```
rust/
├── Cargo.toml # Rust project configuration
├── src/
│ ├── lib.rs # Main library entry point
│ ├── libc.rs # Standard C library bindings
│ ├── librt.rs # RT-Thread kernel API bindings
│ ├── init.rs # Component initialization
│ └── examples/ # Example demos
│ ├── hello.rs # Basic hello world
│ ├── printf_demo.rs # Printf formatting
│ ├── string_demo.rs # String operations
│ ├── memory_demo.rs # Memory management
│ ├── vec_demo.rs # Vec implementation
│ ├── thread_demo.rs # RT-Thread threads
│ └── dlmodule_demo.rs # Dynamic module loading
├── rust_cmd.c # MSH command registration
├── SConscript # Build script with auto-detection
├── Kconfig # Configuration options
```

## Quick Start

### Prerequisites

1. **Install Rust**
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

2. **Add target platforms** (according to your architecture)
```bash
# RISC-V64 (soft-float)
rustup target add riscv64imac-unknown-none-elf

# ARM Cortex-M4
rustup target add thumbv7em-none-eabi

# For other targets, add the corresponding Rust target based on your toolchain/ABI
```

### Build

```bash
# Enable Rust component in menuconfig
scons --menuconfig
# Navigate to: Rust Component Support → Enable

# For dynamic modules, enable dynamic module config and file system:
# 1. RT-Thread online packages → system packages: enable lwext4 file system
# 2. RT-Thread Components → C/C++ and POSIX layer
# → POSIX (Portable Operating System Interface) layer
# → Enable dynamic module APIs, dlopen()/dlsym()/dlclose() etc

# Build
scons

# Clean
scons -c
```

## Supported Architectures

| Architecture | Target | Auto Detection |
|--------------|--------|---------------|
| Cortex-M3 | thumbv7m-none-eabi | ✓ |
| Cortex-M4/M7 | thumbv7em-none-eabi | ✓ |
| Cortex-M4F/M7F | thumbv7em-none-eabihf | ✓ |
| ARMv7-A | armv7a-none-eabi | ✓ |
| AArch64 | aarch64-unknown-none | ✓ |
| RISC-V32 | riscv32ima[f]c-unknown-none-elf | ✓ |
| RISC-V64 | riscv64[gc/imac]-unknown-none-elf | ✓ |

The build system automatically detects the correct target from RT-Thread configuration.

## MSH Commands

| Command | Description |
|---------|-------------|
| `rust_hello` | Print greeting message |
| `rust_add <a> <b>` | Add two numbers |
| `rust_mul <a> <b>` | Multiply two numbers |
| `rust_strlen <str>` | Calculate string length |
| `rust_printf_demo` | Printf formatting demo |
| `rust_memory_demo` | Memory operation demo |
| `rust_thread_demo` | Thread demo |
| `rust_vec_demo` | Vec container demo |
| `rust_dl_demo` | Dynamic module loading demo |

## Configuration Options

Configure via `menuconfig`:

- `RT_USING_RUST` - Enable/disable Rust component
- `RUST_DEBUG_BUILD` - Build with debug symbols
- `RUST_EXAMPLE_*` - Enable specific examples
- `RUST_INIT_COMPONENT` - Auto-initialize at startup

## Technical Details

- **No-std**: Embedded-friendly `#![no_std]` environment
- **FFI**: Seamless C/Rust interoperability
- **Static linking**: Generates `.a` library files
- **Memory safety**: Compile-time guarantees from Rust
- **Zero cost**: Performance equivalent to C

## Extending Components

To add new features:

1. Create a module in `src/`
2. Add an example in `src/examples/`
3. Register the command in `rust_cmd.c`

## Application Scenarios

Rust components are especially suitable for:

- **Safety-critical code**: Leverage Rust's memory safety guarantees
- **Complex algorithms**: Utilize Rust's advanced abstractions
- **Device drivers**: Type-safe hardware abstraction
- **Network protocol stacks**: Safe packet processing
- **Cryptography libraries**: Secure implementations preventing memory leaks

## Troubleshooting

### Link Errors

If you encounter "can't link double-float modules with soft-float modules" error:
- The build system should automatically detect the correct ABI
- Check if the compiler's `-mabi` flag matches the Rust target

### Target Not Installed

If prompted that the target is not installed:
```bash
rustup target add <target-name>
```

### Detection Failure

If target architecture cannot be detected:
- Check if RT-Thread configuration is correct
- Review compiler flags in rtconfig.py

## License

Apache-2.0

## References

- [Rust Embedded Programming Book](https://docs.rust-embedded.org/)
- [RT-Thread Documentation Center](https://www.rt-thread.org/document/site/)
- [Rust FFI Programming](https://doc.rust-lang.org/nomicon/ffi.html)
- [RISC-V Specification](https://riscv.org/technical/specifications/)
Loading