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

本文介绍了如何在Rust的std库中使用core与alloc模块进行嵌入式开发,并详细讲解了全局内存分配、新lib-alloc的运用、自定义内存处理和测试代码的编写。通过实例演示了如何在STM32H750硬件上利用新lib-alloc和Cargo配置进行编译,以及如何通过STM32CubeIDE进行调试。

被折叠的 条评论
为什么被折叠?



