這篇文章,主要介紹搭建ESP32 Rust的開發環境,本文以Mac系統下為例。
命令安裝
- 安裝Rust
curl --proto '=https' --tlsv1.2 //sh.rustup.rs -sSf | sh
2.安裝Rust工具鏈
rustup default stable
3.安裝 cargo generate 命令
brew install openssl && cargo install cargo-generate
4.安裝 esp下載工具
cargo install espflash
Vscode 擴展插件
- rust-analyzer
提供Rust語法分析,代碼補全提示,代碼跳轉、代碼提示等功能。
- Even Batter Toml
提供 Toml文件中crate依賴分析,檢查版本更新情況,高亮顯示Toml格式。
從官方模板新建
cargo generate esp-rs/esp-template
根據提示選擇合適的配置即可。
使用Vscode編輯和運行,運行命令:cargo run
測試一個簡單的漢娜塔的問題:
#![no_std]
#![no_main]
use esp_backtrace as _;
use esp_hal::{clock::ClockControl, delay::Delay, peripherals::Peripherals, prelude::*};
static mut CNT: usize = 0;
fn n_move(n: u32, a: char, b: char) {
// log::info!("move: {} from {} to {}", n, a, b);
unsafe {
CNT += 1;
}
}
fn hannoi(n: u32, a: char, b: char, c: char) {
if n == 1 {
n_move(n, a, c);
} else {
hannoi(n - 1, a, c, b);
n_move(n, a, c);
hannoi(n - 1, b, a, c);
}
}
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::max(system.clock_control).freeze();
let delay = Delay::new(&clocks);
esp_println::logger::init_logger_from_env();
let n: u32 = 24;
hannoi(n, 'a', 'b', 'c');
log::info!("hannoi {} need move {} times", n, unsafe { CNT });
loop {
delay.delay(500.millis());
}
}
計算一個N為24時,不到4秒即完成計算。