Skip to content

The ESP32-S3 crashes after two hours when scanning for Wi-Fi networks every 5 seconds. #4499

@chenjiefang

Description

@chenjiefang

Bug description

The ESP32-S3 crashes after two hours when scanning for Wi-Fi networks every 5 seconds.

To Reproduce

  1. I used the command esp-generate --chip esp32s3 wifiscan to create a project for the ESP32-S3.
Image
  1. This file contains the configuration for my Cargo.toml.
[package]
edition      = "2024"
name         = "wifiscan"
rust-version = "1.88"
version      = "0.1.0"

[[bin]]
name = "wifiscan"
path = "./src/bin/main.rs"

[dependencies]
esp-hal = { version = "1.0.0", features = ["esp32s3", "log-04", "unstable"] }

esp-rtos = { version = "0.2.0", features = [
  "embassy",
  "esp-alloc",
  "esp-radio",
  "esp32s3",
  "log-04",
] }

esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32s3", "log-04"] }
log                    = "0.4.27"

embassy-net = { version = "0.7.1", features = [
  "dhcpv4",
  "log",
  "medium-ethernet",
  "tcp",
  "udp",
] }
embedded-io = "0.7.1"
embedded-io-async = "0.7.0"
esp-alloc = "0.9.0"
esp-println = { version = "0.16.1", features = ["esp32s3", "log-04"] }
# for more networking protocol support see https://crates.io/crates/edge-net
embassy-executor = { version = "0.9.1", features = ["log"] }
embassy-time = { version = "0.5.0", features = ["log"] }
esp-radio = { version = "0.17.0", features = [
  "esp-alloc",
  "esp32s3",
  "log-04",
  "smoltcp",
  "unstable",
  "wifi",
] }
smoltcp = { version = "0.12.0", default-features = false, features = [
  "log",
  "medium-ethernet",
  "multicast",
  "proto-dhcpv4",
  "proto-dns",
  "proto-ipv4",
  "socket-dns",
  "socket-icmp",
  "socket-raw",
  "socket-tcp",
  "socket-udp",
] }

critical-section = "1.2.0"
static_cell      = "2.1.1"


[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"

[profile.release]
codegen-units    = 1     # LLVM can perform better optimizations using a single thread
debug            = 2
debug-assertions = false
incremental      = false
lto              = 'fat'
opt-level        = 's'
overflow-checks  = false

  1. Based on the example at examples/wifi/embassy_dhcp/src/main.rs, I have modified the codebase to perform a Wi-Fi scan every 5 seconds.The corresponding code in my main.rs file is as follows:
#![no_std]
#![no_main]
#![deny(
    clippy::mem_forget,
    reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
    holding buffers for the duration of a data transfer."
)]

use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_hal::clock::CpuClock;
use esp_hal::timer::timg::TimerGroup;
use log::info;

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

extern crate alloc;

// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();

// When you are okay with using a nightly compiler it's better to use https://docs.rs/static_cell/2.1.0/static_cell/macro.make_static.html
macro_rules! mk_static {
    ($t:ty,$val:expr) => {{
        static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
        #[deny(unused_attributes)]
        let x = STATIC_CELL.uninit().write(($val));
        x
    }};
}

#[esp_rtos::main]
async fn main(spawner: Spawner) -> ! {
    // generator version: 1.0.1

    esp_println::logger::init_logger_from_env();

    let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
    let peripherals = esp_hal::init(config);

    esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 64 * 1024);
    esp_alloc::heap_allocator!(size: 36 * 1024);

    let timg0 = TimerGroup::new(peripherals.TIMG0);
    esp_rtos::start(timg0.timer0);

    info!("Embassy initialized!");

    let esp_radio_ctrl = &*mk_static!(esp_radio::Controller<'static>, esp_radio::init().unwrap());

    let (controller, interfaces) =
        esp_radio::wifi::new(&esp_radio_ctrl, peripherals.WIFI, Default::default()).unwrap();

    let wifi_interface = interfaces.sta;

    let config = embassy_net::Config::dhcpv4(Default::default());

    let rng = esp_hal::rng::Rng::new();
    let seed = (rng.random() as u64) << 32 | rng.random() as u64;

    // Init network stack
    let (_stack, runner) = embassy_net::new(
        wifi_interface,
        config,
        mk_static!(
            embassy_net::StackResources<3>,
            embassy_net::StackResources::<3>::new()
        ),
        seed,
    );

    spawner.spawn(connection(controller)).ok();
    spawner.spawn(net_task(runner)).ok();

    loop {
        Timer::after(Duration::from_secs(1)).await;
    }

    // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0/examples/src/bin
}

#[embassy_executor::task]
async fn connection(mut controller: esp_radio::wifi::WifiController<'static>) {
    info!("start connection task");
    info!("Device capabilities: {:?}", controller.capabilities());
    let client_config = esp_radio::wifi::ModeConfig::Client(
        esp_radio::wifi::ClientConfig::default()
            .with_ssid("aaaaa".into())
            .with_password("bbbbbbbbbbbbbb".into()),
    );
    let scan_config = esp_radio::wifi::ScanConfig::default().with_max(10);
    let mut count: u32 = 0;
    loop {
        match esp_radio::wifi::sta_state() {
            esp_radio::wifi::WifiStaState::Connected => {
                // wait until we're no longer connected
                controller
                    .wait_for_event(esp_radio::wifi::WifiEvent::StaDisconnected)
                    .await;
                Timer::after_secs(5).await
            }
            _ => {}
        }
        if !matches!(controller.is_started(), Ok(true)) {
            controller.set_config(&client_config).unwrap();
            info!("Starting wifi");
            controller.start_async().await.unwrap();
            info!("Wifi started!");
        }

        info!("Scan");
        count = count.wrapping_add(1);
        let result = controller
            .scan_with_config_async(scan_config)
            .await
            .unwrap();
        for ap in result {
            info!("{:?}", ap);
        }
        info!("scan count = {}", count);
        Timer::after_secs(5).await;
    }
}

#[embassy_executor::task]
async fn net_task(mut runner: embassy_net::Runner<'static, esp_radio::wifi::WifiDevice<'static>>) {
    runner.run().await
}

3.Approximately two hours after operation, the device becomes unresponsive. Log output freezes, and all functionality ceases, necessitating a hard reboot.

Expected behavior

Image

Environment

  • Target device: [ESP32-S3, ESP32-C3]
  • Crate name and version: [esp-hal 1.0.0]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpackage:esp-radioIssues related to the esp-wifi package

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions