Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating point operation crashes with "Exception occurred 'Cp0Disabled'" while connected to wifi #2222

Closed
ByteMaDo opened this issue Sep 24, 2024 · 5 comments
Labels
bug Something isn't working status:needs-attention This should be prioritized

Comments

@ByteMaDo
Copy link

ByteMaDo commented Sep 24, 2024

Bug description

The code below crashes on an ESP32 (ESP-WROOM-32) with the exception below.
The issue seems to be the f32/float operation in the "read_sensor_data()" function, while being connected to a wifi.
If I remove the wifi part or set invalid wifi connection data, it performs the floating point operations normally.


Exception occurred 'Cp0Disabled (Access to the floating point coprocessor is not allowed. You may want to enable the `float-save-restore` feature of the `xtensa-lx-rt` crate.)'
Context
PC=0x400f00b7       PS=0x00060930
0x400f00b7 - core::f32::<impl f32>::abs_private
    at /home/foobar/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/num/f32.rs:533
0x00060930 - PS_WOE
    at ??:??
A0=0x800ee5d5       A1=0x3ffdab00       A2=0x3ffdb638       A3=0x3ffdab30       A4=0x00000000
0x800ee5d5 - _rtc_slow_bss_start
    at ??:??
0x3ffdab00 - __stack_chk_guard
    at ??:??
0x3ffdb638 - __stack_chk_guard
    at ??:??
0x3ffdab30 - __stack_chk_guard
    at ??:??
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
A5=0x0006ffff       A6=0x00000000       A7=0x00000001       A8=0x00000001       A9=0x4442accd
0x0006ffff - PS_WOE
    at ??:??
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000001 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000001 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x4442accd - wDev_GetNoiseFloor
    at ??:??
A10=0x7fffffff      A11=0x00000000      A12=0x00000000      A13=0x3ffdbd50      A14=0x00000010
0x7fffffff - _rtc_slow_bss_start
    at ??:??
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x3ffdbd50 - __stack_chk_guard
    at ??:??
0x00000010 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1534
A15=0x00000001
0x00000001 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
SAR=00000004
EXCCAUSE=0x00000020 EXCVADDR=0x00000000
0x00000020 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1535
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
LBEG=0x4000c28c     LEND=0x4000c296     LCOUNT=0x00000000
0x4000c28c - memcmp
    at ??:??
0x4000c296 - memcmp
    at ??:??
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
THREADPTR=0x00000000
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
SCOMPARE1=0x00000003
0x00000003 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
BR=0x00000000
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
ACCLO=0x00000000    ACCHI=0x00000000
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
M0=0x00000000       M1=0x00000000       M2=0x00000000       M3=0x00000000
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
F64R_LO=0x3ffdab10  F64R_HI=0x632a07e3  F64S=0x00000002
0x3ffdab10 - __stack_chk_guard
    at ??:??
0x632a07e3 - _rtc_slow_bss_start
    at ??:??
0x00000002 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
FCR=0x3f409e3e      FSR=0x00000005
0x3f409e3e - anon.98cb325b9f2c8a025c2833ff4d43dbdd.7.llvm.9682373318355679123
    at ??:??
0x00000005 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
F0=0x3f4014c1       F1=0x00000004       F2=0x3f409e54       F3=0x00000005       F4=0x3ffdaab8
0x3f4014c1 - anon.9515259f68c3fedf326c00a7d8f5849f.4.llvm.2516482696797543646
    at ??:??
0x00000004 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x3f409e54 - anon.98cb325b9f2c8a025c2833ff4d43dbdd.7.llvm.9682373318355679123
    at ??:??
0x00000005 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x3ffdaab8 - __stack_chk_guard
    at ??:??
F5=0x00000004       F6=0x00000000       F7=0x00000000       F8=0x3ffdaa90       F9=0x400ef64c
0x00000004 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x00000000 - <esp32::RMT as core::fmt::Debug>::fmt
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.33.0/src/lib.rs:1533
0x3ffdaa90 - __stack_chk_guard
    at ??:??
0x400ef64c - <&T as core::fmt::Display>::fmt
    at /home/foobar/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2368
F10=0x3ffdaadc      F11=0x400e35a0      F12=0x3ffdaae0      F13=0x400e332c      F14=0x3ffdaa98
0x3ffdaadc - __stack_chk_guard
    at ??:??
0x400e35a0 - log::Level::as_str
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/log-0.4.22/src/lib.rs:566
0x3ffdaae0 - __stack_chk_guard
    at ??:??
0x400e332c - <&T as core::fmt::Display>::fmt
    at /home/foobar/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2368
0x3ffdaa98 - __stack_chk_guard
    at ??:??
F15=0x400ef64c
0x400ef64c - <&T as core::fmt::Display>::fmt
    at /home/foobar/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:2368
0x400e3404
0x400e3404 - <&mut W as core::fmt::Write::write_fmt::SpecWriteFmt>::spec_write_fmt
    at /home/foobar/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/fmt/mod.rs:230
0x400d4b64
0x400d4b64 - test::read_sensor_data::{{closure}}
    at ??:??
0x400ede2b
0x400ede2b - embassy_executor::raw::util::SyncUnsafeCell<T>::get
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/embassy-executor-0.6.0/src/raw/util.rs:55
0x400d3d42
0x400d3d42 - esp_hal_embassy::executor::thread::Executor::run
    at /home/foobar/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-embassy-0.3.0/src/executor/thread.rs:126
0x400d44e2
0x400d44e2 - main
    at ??:??
0x400e1e6d
0x400e1e6d - Reset
    at ??:??
0x400e1582
0x400e1582 - ESP32Reset
    at ??:??


To Reproduce

  1. set SSID and SSID_PW environment variables for your wifi
  2. Use an ESP32 uC and flash the following source
  3. monitor the serial connection and wait for the exception above
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
#![feature(impl_trait_in_assoc_type)]

use embassy_executor::Spawner;
use embassy_net::{Config, Stack, StackResources};
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
    clock::ClockControl, peripherals::Peripherals, rng::Rng, system::SystemControl,
    timer::timg::TimerGroup,
};
use esp_println::println;
use esp_wifi::{
    initialize,
    wifi::{
        new_with_mode, ClientConfiguration, Configuration, WifiController, WifiDevice, WifiEvent,
        WifiStaDevice, WifiState,
    },
    EspWifiInitFor,
};

use log::info;

const SSID: &str = env!("SSID");
const PASSWORD: &str = env!("SSID_PW");

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_hal_embassy::main]
async fn main(spawner: Spawner) {
    esp_println::logger::init_logger_from_env();

    esp_println::println!("Init!");
    let peripherals = Peripherals::take();
    let system = SystemControl::new(peripherals.SYSTEM);
    let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
    let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
    esp_hal_embassy::init(&clocks, timg0.timer1);

    let init = initialize(
        EspWifiInitFor::Wifi,
        timg0.timer0,
        Rng::new(peripherals.RNG),
        peripherals.RADIO_CLK,
        &clocks,
    )
    .unwrap();

    let wifi = peripherals.WIFI;
    let (wifi_interface, controller) = new_with_mode(&init, wifi, WifiStaDevice).unwrap();

    let dhcpconfig = Config::dhcpv4(Default::default());

    let seed = 1234; // very random, very secure seed

    let stack: &'static Stack<WifiDevice<'_, WifiStaDevice>> = mk_static!(
        Stack<WifiDevice<'_, WifiStaDevice>>,
        Stack::new(
            wifi_interface,
            dhcpconfig,
            mk_static!(StackResources<3>, StackResources::<3>::new()),
            seed
        )
    );

    spawner.spawn(connection(controller)).unwrap();
    spawner.spawn(net_task(stack)).unwrap();



    read_sensor_data().await;
   
}

async fn read_sensor_data() {
    //This part crashes when there is a wifi connection.
    info!("Perform some floating point operation");
    loop {
        Timer::after(Duration::from_secs(5)).await;

        let f = 12.0;
        let g = 83u16 << 3;
        let rel_humidity = ((4 as u16) << 8 | (3 as u16)) as f32 / 10.0f32;
        println!("{:?}", f + (g as f32) + (rel_humidity as f32));
    }
}

#[embassy_executor::task]
pub async fn connection(mut controller: WifiController<'static>) {
    println!("start connection task");
    println!("Device capabilities: {:?}", controller.get_capabilities());
    loop {
        match esp_wifi::wifi::get_wifi_state() {
            WifiState::StaConnected => {
                // wait until we're no longer connected
                controller.wait_for_event(WifiEvent::StaDisconnected).await;
                Timer::after(Duration::from_millis(5000)).await
            }
            _ => {}
        }
        if !matches!(controller.is_started(), Ok(true)) {
            let client_config = Configuration::Client(ClientConfiguration {
                ssid: SSID.try_into().unwrap(),
                password: PASSWORD.try_into().unwrap(),
                ..Default::default()
            });
            controller.set_configuration(&client_config).unwrap();
            println!("Starting wifi");
            controller.start().await.unwrap();
            println!("Wifi started!");
        }
        println!("About to connect...");

        match controller.connect().await {
            Ok(_) => println!("Wifi connected!"),
            Err(e) => {
                println!("Failed to connect to wifi: {e:?}");
                Timer::after(Duration::from_millis(5000)).await
            }
        }
    }
}

#[embassy_executor::task]
pub async fn net_task(stack: &'static embassy_net::Stack<WifiDevice<'static, WifiStaDevice>>) {
    stack.run().await
}

Expected behavior

The floating-point operation succeeds without an exception.

Environment

  • Target device: [e.g. ESP-Wroom-32] esp32 (revision v1.0)
[package]
name = "test"
version = "0.1.0"
edition = "2021"

[dependencies]
esp-backtrace = { version = "0.14.0", features = [
    "esp32",
    "exception-handler",
    "panic-handler",
    "println",
] }
esp-hal = { version = "0.20.1", features = [ "esp32","async" ] }
esp-hal-embassy = { version = "0.3.0", features = [
           # Compatible with all chips
    "esp32"
] }
embassy-time = "0.3"
embassy-executor = { version = "0.6", features = ["integrated-timers","executor-thread", "nightly"] }

esp-println = { version = "0.11.0", features = ["esp32", "log"] }
log = { version = "0.4.21" }
esp-alloc = { version = "0.4.0" }
embedded-svc = { version = "0.28.0", default-features = false, features = [] }
embedded-io = "0.6.1"
esp-wifi = { version = "0.8.0", features = [
    "esp32",
    "utils",
    "wifi",
    "embassy-net",
] }
heapless = { version = "0.8.0", default-features = false }
smoltcp = { version = "0.11.0", default-features = false, features = [
    "medium-ethernet",
    "proto-dhcpv4",
    "proto-igmp",
    "proto-ipv4",
    "socket-dhcpv4",
    "socket-icmp",
    "socket-raw",
    "socket-tcp",
    "socket-udp",
] }
embassy-net = { version = "0.4.0", features = ["dns", "tcp", "medium-ethernet", "dhcpv4-hostname"] }
embassy-futures = "0.1.1"
embassy-sync = "0.6.0"
embedded-nal-async = "0.7.1"
embedded-io-async   = "0.6.1"

static_cell = {version="2.1.0", features = ["nightly"]}
 
xtensa-lx = {version="0.9.0"}
xtensa-lx-rt = {version="0.17.1",feature = ["esp32", "float-save-restore"]}

critical-section    = "1.1.2"

rust-mqtt = {version="0.3.0", default-features = false}
libm = "0.2.8"
postcard = "1.0.10"
serde-json-core = "0.6.0"


[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

@ByteMaDo ByteMaDo added bug Something isn't working status:needs-attention This should be prioritized labels Sep 24, 2024
@ByteMaDo ByteMaDo changed the title Floating point operation crashes with "Exception occurred 'Cp0Disabled'" while connected to a Wifi Floating point operation crashes with "Exception occurred 'Cp0Disabled'" while connected to wifi Sep 24, 2024
@Dominaezzz
Copy link
Collaborator

See #2050 (comment)

@ByteMaDo
Copy link
Author

The feature 'float-save-restore' flag is set. as you can see in the Cargo.toml above.

@Dominaezzz
Copy link
Collaborator

Ah I see. I'm out of ideas then 🙂

@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 24, 2024

There is a typo here xtensa-lx-rt = {version="0.17.1",features = ["esp32", "float-save-restore"]}

In your code above it's "feature" but needs to be "features"

@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Sep 24, 2024
@ByteMaDo
Copy link
Author

Woa, I've wasted three hours on this and double-checked the Cargo.toml three times - but missed this completely...
Now it's working like a charm :-D
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status:needs-attention This should be prioritized
Projects
Archived in project
Development

No branches or pull requests

4 participants