學習新的語言,第一門課程當然是輸出hello world
。在這節(jié)你將能非常輕松得讓單片機運行一段打印hello wolrd
的程序。
準備
在閱讀本文之前,如果您還沒有配置基礎的Rust 開發(fā)環(huán)境,您可以閱讀 環(huán)境搭建
本文將基于主板 Py32_Rust_Dev 1.2
演示,在此前你需要準備基本的硬件設備如下
- Py32_Rust_dev 1.2
- Jlink V9(或 Stlink V2)+ SWD 下載線 需要準備的軟件環(huán)境如下:
- Rust (night)
- thumbv6m-none-eabi
- probe-rs
獲取代碼
你需要獲取開發(fā)版 Py32_Rust_dev 的 Rust 驅動包,里面有芯片的 Rust 外設驅動以及提供了豐富的學習例程。
使用以下命令獲取源碼
git clone https://github.com/hysonglet/py32f030-hal.git
測試代碼
#![no_std]
#![no_main]
use py32f030_hal as _;
use {defmt_rtt as _, panic_probe as _};
#[cortex_m_rt::entry]
fn main_fun() -> ! {
defmt::info!("hello world");
loop {
cortex_m::asm::wfe();
}
}
燒錄和運行
使用以下命令即可快速編譯代碼,并且燒錄固件到芯片,然后可以在終端中看到 py32f030 的輸出打印內容
cargo run --example hello_world
? py32f030-hal git:(main) ? cargo r --example hello_world
warning: unused manifest key: dependencies.embedded-io-async.option
Finished `dev` profile [optimized + debuginfo] target(s) in 0.16s
Running `probe-rs run --chip PY32F030x8 target/thumbv6m-none-eabi/debug/examples/hello_world`
Erasing ? [00:00:00] [###############################################################] 12.00 KiB/12.00 KiB @ 92.09 KiB/s (eta 0s )
Programming ? [00:00:02] [##################################################################] 9.50 KiB/9.50 KiB @ 4.18 KiB/s (eta 0s ) Finished in 2.444s
INFO hello world
└─ hello_world::__cortex_m_rt_main_fun @ examples/hello_world.rs:10