RUST的std库
RUST的标准库包括:
在嵌入式中,比较有用的就是core
与alloc
。
编译
在.cargo/config.toml
增加对alloc编译:
[unstable]
build-std = ["core", "alloc"]
global_allocator
可用来实现自己的内存分配函数的属性,可参考文档std::alloc。
在lib.rs上可以搜索到newlib-alloc,可直接用。
在Cargo.toml
中增加
[dependencies]
libc = "0.2"
newlib-alloc = "0.1"
libc-print = "0.1"
编译libc时需要条件选择newlib,在.cargo/config
增加:
[build]
rustflags = [
"--cfg", "unix",
"--cfg", "target_env=\"newlib\"",
]
编写测试代码
#![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);
}
}
- 串口终端输出
- 调试过程
测试环境
硬件环境
- WeActMiniStm32H750
- MCU:STM32H750
- 说明:此工程已对接好newlib(如
malloc()
、printf()
等标准函数),可为Rust的库提供底层支持。
调试环境
- IDE:STM32CubeIDE-1.9.0
- 需要增加Rust插件:Corrosion: Rust edition in Eclipse IDE
代码
- 代码仓库:
git clone --recursive https://gitee.com/xwos/WeActMiniStm32H750.git
- commit:
f8fcad24daa4912e4de0886c30e02343b6045dab
cd XWOS
git pull
git checkout -b rustbringup f8fcad24daa4912e4de0886c30e02343b6045dab