WOKWI ESP32在线仿真中Rust依赖版本错误求助
Rust ESP32开发:xtensa-lx v0.4.0编译时找不到
asm宏 我在用Rust开发基于ESP32的智能家居系统,通过DHT传感器控制灯泡,使用WOKWI模拟器开发。编译时遇到xtensa-lx v0.4.0找不到asm宏的错误,尝试过更换版本、替换库但未解决,附上代码和错误信息。
相关代码
main.rs
#![no_std] #![no_main] // Import necessary crates and modules use std::arch::asm; // use core::arch::asm - NOT WORKING!! use core::fmt::Write; use esp32_hal::prelude::*; use esp32_hal::dht::{Dht, DhtBuilder}; use esp32_hal::wifi::{EspWifi, WifiConfig}; use esp32_hal::mqtt::{MqttClient, MqttConfig}; use esp32_hal::gpio::Gpio; use esp32_hal::clock::Clocks; use panic_halt as _; use xtensa_lx_rt::entry; use esp32_hal::dht::DhtType; use esp32_hal::pac::Peripherals; use esp32_hal::pac::CorePeripherals; use esp32_hal::delay::delay_ms; // Wi-Fi configuration const SSID: &str = "dd-wrt"; const PASSWORD: &str = "0000000000"; // MQTT server address const MQTT_SERVER: &str = "192.168.1.103"; // DHT sensor configuration const DHT_PIN: u8 = 13; const DHT_TYPE: DhtType = DhtType::Dht22; // LED GPIO pin const LED: u8 = 12; // MQTT topics const TEMP_TOPIC: &str = "smarthome/room1/temp"; const LED_TOPIC: &str = "smarthome/room1/led"; // 1=on, 0=off #[entry] fn main() -> ! { // Initialize peripherals and core peripherals let dp = Peripherals::take().unwrap(); let cp = CorePeripherals::take().unwrap(); // Initialize system clocks let clocks = Clocks::new(cp.SYST); // Configure GPIO pins let pins = dp.GPIO.split(); let _led = pins.gpio12.into_push_pull_output(); // Initialize DHT sensor let dht = DhtBuilder::new(DHT_PIN, DHT_TYPE).init().unwrap(); // Initialize and configure Wi-Fi let wifi = EspWifi::new(dp.RTC_CNTL, dp.SPI_WIFI, dp.WIFI); let wifi_config = WifiConfig::new(SSID, PASSWORD); wifi.set_config(wifi_config); wifi.connect(); // Initialize and configure MQTT client let mqtt_config = MqttConfig::new(MQTT_SERVER); let mut mqtt_client = MqttClient::new(dp.MQTT, mqtt_config); // Main loop loop { // Read temperature from DHT sensor if let Ok(temperature) = dht.read_temperature() { // Format temperature value as a string let mut msg = [0u8; 20]; write!(&mut msg[..], "{:.2}", temperature).unwrap(); // Publish temperature to MQTT topic mqtt_client.publish(TEMP_TOPIC, &msg, 0, false).unwrap(); } // Delay for 1 minute (60000 milliseconds) delay_ms(60000); } }
Cargo.toml
[package] name = "rust_project_esp32" version = "0.1.0" authors = ["Sergio Gasquez <sergio.gasquez@gmail.com>"] edition = "2021" license = "MIT OR Apache-2.0" [dependencies] esp32-hal = "0.3.0" #xtensa-lx-rt = "0.3.0" esp-backtrace = { version = "0.7.0", features = [ "esp32", "panic-handler", "print-uart", ] } esp-println = { version = "0.5.0", features = ["esp32"] } xtensa-lx-rt = { version = "0.15.0", features = ["esp32"], optional = true } [dependencies.panic-halt] version = "0.2.0" [features] default = ["rt"] rt = ["xtensa-lx-rt"]
错误信息
Compiling xtensa-lx v0.4.0 error: cannot find macro `asm` in this scope --> /home/esp/.cargo/registry/src/github.com-1ecc6299db9ec823/xtensa-lx-0.4.0/src/interrupt.rs:99:14 | 99 | unsafe { asm!("rsr.intenable {0}", out(reg) mask) }; | ^^^ | = help: consider importing this macro: core::arch::asm error: cannot find macro `asm` in this scope --> /home/esp/.cargo/registry/src/github.com-1ecc6299db9ec823/xtensa-lx-0.4.0/src/timer.rs:7:14 | 7 | unsafe { asm!("rsr.ccompare0 {0}", out(reg) x, options(nostack)) }; | ^^^ | = help: consider importing this macro: core::arch::asm error: cannot find macro `asm` in this scope --> /home/esp/.cargo/registry/src/github.com-1ecc6299db9ec823/xtensa-lx-0.4.0/src/timer.rs:14:14 | 14 | unsafe { asm!("rsr.ccompare1 {0}", out(reg) x, options(nostack)) }; | ^^^ | = help: consider importing this macro: core::arch::asm error: cannot find macro `asm` in this scope --> /home/esp/.cargo/registry/src/github.com-1ecc6299db9ec823/xtensa-lx-0.4.0/src/timer.rs:21:14 | 21 | unsafe { asm!("rsr.ccompare2 {0}", out(reg) x, options(nostack)) }; | ^^^ | = help: consider importing this macro: core::arch::asm error: cannot find macro `asm` in this scope --> /home/esp/.cargo/registry/src/github.com-1ecc6299db9ec823/xtensa-lx-0.4.0/src/timer.rs:37:9 | 37 | asm!(" | ^^^ | = help: consider importing this macro: core::arch::asm
解决方案
1. 对齐依赖版本,解决宏冲突
错误根源是esp32-hal v0.3.0间接依赖了旧版的xtensa-lx v0.4.0,而手动指定的xtensa-lx-rt v0.15.0是新版本,两者版本不兼容。旧版xtensa-lx使用全局asm!宏,但Rust 1.59+之后该宏已迁移到core::arch::asm,旧版库未适配这个变更。
修改Cargo.toml,统一使用兼容的依赖版本:
[package] name = "rust_project_esp32" version = "0.1.0" authors = ["Sergio Gasquez <sergio.gasquez@gmail.com>"] edition = "2021" license = "MIT OR Apache-2.0" [dependencies] esp32-hal = "0.16.0" # 使用与xtensa-lx-rt兼容的新版本 xtensa-lx-rt = { version = "0.15.0", features = ["esp32"] } esp-backtrace = { version = "0.11.0", features = ["esp32", "panic-handler"] } esp-println = { version = "0.9.0", features = ["esp32"] } embedded-hal = "1.0.0" dht-sensor = "0.13.0" # 替换esp32-hal内置的DHT,更稳定 mqtt-nostd = "0.7.0" # 适配no_std的MQTT库 [dependencies.panic-halt] version = "0.2.0"
2. 修正main.rs中的API适配问题
新版本的esp32-hal API有变更,同时替换部分库以适配最新栈:
#![no_std] #![no_main] use core::fmt::Write; use esp32_hal::{ clock::Clocks, gpio::GpioPin, pac::{CorePeripherals, Peripherals}, prelude::*, delay::Delay, }; use panic_halt as _; use xtensa_lx_rt::entry; use dht_sensor::{dht22, DhtError}; use mqtt_nostd::client::MqttClient; use mqtt_nostd::config::MqttConfig; // Wi-Fi configuration const SSID: &str = "dd-wrt"; const PASSWORD: &str = "0000000000"; // MQTT server address const MQTT_SERVER: &str = "tcp://192.168.1.103:1883"; // DHT sensor configuration const DHT_PIN: u8 = 13; // LED GPIO pin const LED_PIN: u8 = 12; // MQTT topics const TEMP_TOPIC: &str = "smarthome/room1/temp"; const LED_TOPIC: &str = "smarthome/room1/led"; #[entry] fn main() -> ! { let dp = Peripherals::take().unwrap(); let cp = CorePeripherals::take().unwrap(); // Initialize clocks and delay let clocks = Clocks::new(cp.SYST).freeze(); let mut delay = Delay::new(&clocks); // Configure GPIO pins let gpio = dp.GPIO.split(); let mut led = gpio.gpio12.into_push_pull_output(); let mut dht_pin = gpio.gpio13.into_open_drain_output(); // 此处省略Wi-Fi初始化代码(需使用esp-wifi库,新版本esp32-hal不再内置Wi-Fi) // 推荐使用`esp-wifi` crate // MQTT客户端初始化(需结合Wi-Fi的网络栈) // let mut mqtt_client = MqttClient::new(MQTT_SERVER, MqttConfig::default()).unwrap(); loop { // 读取DHT22数据 match dht22::read(&mut delay, &mut dht_pin) { Ok((temperature, _humidity)) => { let mut msg = [0u8; 20]; write!(&mut msg[..], "{:.2}", temperature).unwrap(); // 发布温度到MQTT // mqtt_client.publish(TEMP_TOPIC, msg.as_ref(), 0, false).unwrap(); // 根据湿度控制LED(示例逻辑) // if humidity > 60.0 { // led.set_high().unwrap(); // } else { // led.set_low().unwrap(); // } } Err(DhtError::Timeout) => continue, Err(e) => panic!("DHT read error: {:?}", e), } delay.delay_ms(60000u32); } }
关键说明
- 旧版
esp32-hal内置的DHT、Wi-Fi、MQTT模块已拆分到独立crate,使用新版本时需要引入对应的独立库。 - 确保Rust工具链已安装xtensa架构目标:
rustup target add xtensa-esp32-none-elf。 - WOKWI模拟器需配置正确的Rust工具链和构建目标,确保编译后的二进制能被识别。
内容的提问来源于stack exchange,提问作者worrum
相关产品推荐
相关产品推荐

