首页 > 数据库 > 数据库开发技术 > Esp32 Rust环境搭建
这篇文章,主要介绍搭建ESP32 Rust的开发环境,本文以Mac系统下为例。
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
rustup default stable
brew install openssl && cargo install cargo-generate
cargo install espflash
提供Rust语法分析,代码补全提示,代码跳转、代码提示等功能。
提供 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秒即完成计算。
原文作者:Rust嵌入式;如有侵权,请联系管理员删除。