[XWOS-RUST]2.Bringup

RUST的std库

RUST的标准库包括:

RUST的标准库
在嵌入式中,比较有用的就是corealloc

编译

.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);
    }
}
  • 串口终端输出
    RustBringup
  • 调试过程
    RustInEclipse

测试环境

硬件环境
  • WeActMiniStm32H750
  • MCU:STM32H750
  • 说明:此工程已对接好newlib(如malloc()printf()等标准函数),可为Rust的库提供底层支持。
调试环境

代码

  • 代码仓库:git clone --recursive https://gitee.com/xwos/WeActMiniStm32H750.git
  • commit:f8fcad24daa4912e4de0886c30e02343b6045dab
cd XWOS
git pull
git checkout -b rustbringup f8fcad24daa4912e4de0886c30e02343b6045dab
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值