//! 使用 `esp-hal` 在 ESP32 上运行 Rust 的示例(裸机 / no_std 模式) //! 闪烁 LED 并通过 UART 串口发送消息,同时提供内存安全保障。 //! //! Cargo.toml: //! esp-hal = { version = "0.22", features = ["esp32s3"] } //! esp-println = { version = "0.12", features = ["esp32s3", "log"] } #![no_std] #![no_main] use esp_backtrace as _; use esp_hal::{ clock::ClockControl, gpio::{Io, Level, Output}, peripherals::Peripherals, system::SystemControl, timer::timg::TimerGroup, delay::Delay, prelude::*, }; use esp_println::println; #[entry] fn main() -> ! { // 在编译期检查的前提下初始化 SoC 外设 let peripherals = Peripherals::take(); let system = SystemControl::new(peripherals.SYSTEM); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); // 配置 I/O 引脚 let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); let mut led = Output::new(io.pins.gpio2, Level::Low); let delay = Delay::new(&clocks); println!("🦀 Rust bare-metal firmware initialized on ESP32!"); let mut counter: u32 = 0; loop { // 在保证互斥的前提下翻转 LED 状态 led.toggle(); counter += 1; println!("Rust cycle #{}: LED is {:?}", counter, led.is_set_high()); delay.delay_millis(500); } }