Rust Initial Validation

Rust std Library

The Rust standard library includes:

img

In embedded systems, core and alloc are the most useful.

Although in embedded environments you can forcibly use Rust’s std library through #![feature(restricted_std)], as functionality increases, problems also increase, so we switched to using #![no_std].

Build

Add the following build configuration in .cargo/config.toml:

[unstable]
build-std = ["core", "alloc"]

global_allocator

An attribute that can be used to implement your own memory allocation functions. Refer to the documentation std::alloc.

On lib.rs, you can search for newlib-alloc, which can be used directly.

Add in Cargo.toml:

[dependencies]
libc = "0.2"
newlib-alloc = "0.1"
libc-print = "0.1"

When building libc, conditionally select newlib. Add in .cargo/config:

[build]
rustflags = [
  "--cfg", "unix",
  "--cfg", "target_env=\"newlib\"",
]

Writing Test Code

#![no_std]
#![feature(alloc_error_handler)]

use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop {}
}

use newlib_alloc;
#[global_allocator]
static GLOBAL_ALLOCATOR: newlib_alloc::Alloc = newlib_alloc::Alloc;

#[alloc_error_handler]
fn alloc_error_handler(_layout: core::alloc::Layout) -> ! {
    loop {}
}

use libc_print::std_name::println;

extern crate alloc;
use alloc::vec::Vec;

#[no_mangle]
pub unsafe extern "C" fn rust_main() {
    let mut v = Vec::new();
    v.push(1);
    v.push(2);
    v.push(3);
    v.push(4);
    println!("RUST XWOS!");
    for x in v.iter() {
        println!("x: {}", x);
    }
}
  • Serial terminal output

img

  • Debugging process

img

Test Environment

Hardware Environment

  • WeActMiniStm32H750
  • MCU: STM32H750
  • Description: This project has already been integrated with newlib (e.g., malloc(), printf() and other standard functions), providing low-level support for Rust libraries.

Debug Environment

  • IDE: STM32CubeIDE
  • Requires adding the Rust plugin: Corrosion: Rust edition in Eclipse IDE

Experimental Code

  • Code repository: git clone --recursive https://gitee.com/xwos/WeActMiniStm32H750.git
  • commit
cd XWOS
git pull
git checkout -b rust-bringup f8fcad24daa4912e4de0886c30e02343b6045dab