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

[ESP32] esp-wifi crashes when trying to connect via BLE #441

Closed
thecodechemist99 opened this issue Mar 14, 2024 · 6 comments
Closed

[ESP32] esp-wifi crashes when trying to connect via BLE #441

thecodechemist99 opened this issue Mar 14, 2024 · 6 comments

Comments

@thecodechemist99
Copy link

Context

I’m working on a project using embassy and the esp-wifi crate for BLE.
I use a Lolin32-lite board with an ESP32 Rev1.

Unexpected Behaviour

When I try to connect to the ESP32 from my Android phone, the ESP32 crashes with the following error message:

!! A panic occured in '/Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/preempt/preempt_xtensa.rs', at line 104, column 9:
index out of bounds: the len is 3 but the index is 1073503776

Backtrace:

0x40122d09
0x40122d09 - core::panicking::panic_bounds_check
    at /Users/thecodechemist99/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/panicking.rs:208
0x400ea9a1
0x400ea9a1 - esp_wifi::preempt::preempt::save_task_context
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/preempt/preempt_xtensa.rs:104
0x400ead30
0x400ead30 - esp_wifi::timer::arch_specific::__esp_hal_internal_TG1_T0_LEVEL
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/timer/xtensa.rs:99
0x40081510
0x40081510 - esp_hal::interrupt::xtensa::vectored::handle_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:452
0x400810e2
0x400810e2 - esp_hal::interrupt::xtensa::vectored::handle_interrupts
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:432
0x40080dc6
0x40080dc6 - __level_2_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:376
0x40081817
0x40081817 - __default_naked_level_2_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/xtensa-lx-rt-0.16.0/src/exception/assembly_esp32.rs:599
0x4003ffff
0x4003ffff - r_ld_sched_sniff_add
    at ??:??
0x400fd4a3
0x400fd4a3 - esp_hal::critical_section_impl::xtensa::<impl critical_section::Impl for esp_hal::critical_section_impl::CriticalSection>::release
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/lib.rs:279
0x4010e2da
0x4010e2da - critical_section::release
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200

Code Example

Here’s my code to reproduce the issue:

#![no_std]
#![no_main]
#![feature(async_closure)]
#![feature(type_alias_impl_trait)]

// Logging traits
use esp_backtrace as _;
use esp_println::println;

// Core traits
use embassy_executor::Spawner;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::Timer;
use esp_hal::{
    adc::{AdcConfig, AdcPin, Attenuation, ADC},
    clock::ClockControl,
    embassy,
    gpio::{Analog, GpioPin},
    peripherals::*,
    prelude::*,
    timer::TimerGroup,
    Rng, IO,
};

// BLE traits
use bleps::{
    ad_structure::{
        create_advertising_data, AdStructure, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE,
    },
    async_attribute_server::AttributeServer,
    asynch::Ble,
    attribute_server::NotificationData,
    gatt,
};
use esp_wifi::{
    ble::controller::asynch::BleConnector, initialize, EspWifiInitFor, EspWifiInitialization,
};

// Message channels
static SENSOR_READING: Channel<CriticalSectionRawMutex, [u8; 4], 1> = Channel::new();

#[main]
async fn main(spawner: Spawner) {
    // Set up system
    let peripherals = Peripherals::take();
    let system = peripherals.SYSTEM.split();
    let clocks = ClockControl::max(system.clock_control).freeze();
    let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
    embassy::init(&clocks, timg0);

    // Set up IO
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);

    // Set up BLE
    let timer = TimerGroup::new(peripherals.TIMG1, &clocks).timer0;
    let init = initialize(
        EspWifiInitFor::Ble,
        timer,
        Rng::new(peripherals.RNG),
        system.radio_clock_control,
        &clocks,
    )
    .unwrap();

    let bluetooth = peripherals.BT;

    // Set up ADC
    let mut adc_config = AdcConfig::new();
    let adc_pin = adc_config.enable_pin(io.pins.gpio35.into_analog(), Attenuation::Attenuation0dB);
    let adc = ADC::<ADC1>::new(peripherals.ADC1, adc_config);

    // Spawn tasks
    spawner.spawn(ble_runner(init, bluetooth)).unwrap();
    spawner.spawn(read_sensor(adc, adc_pin)).unwrap();
}

/// Ble runner task
#[embassy_executor::task]
async fn ble_runner(init: EspWifiInitialization, mut bluetooth: BT) {
    let connector = BleConnector::new(&init, &mut bluetooth);
    let mut ble = Ble::new(connector, esp_wifi::current_millis);

    loop {
        // Configure advertising
        println!("{:?}", ble.init().await);
        println!("{:?}", ble.cmd_set_le_advertising_parameters().await);
        println!(
            "{:?}",
            ble.cmd_set_le_advertising_data(
                create_advertising_data(&[
                    AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
                    AdStructure::ServiceUuids128(&[
                        Uuid::Uuid128([
                            0x8d, 0xa4, 0xc3, 0xd7, 0x0, 0x0, 0x4b, 0x26, 0x96, 0x32, 0xae, 0x20,
                            0x7b, 0x6b, 0x75, 0x7f,
                        ]), // Custom Sensor Service
                    ]),
                    AdStructure::CompleteLocalName("ESP32"),
                ])
                .unwrap(),
            )
            .await
        );
        println!("{:?}", ble.cmd_set_le_advertise_enable(true).await);

        println!("started advertising");

        // Define service methods
        let mut read_sensor = |_offset: usize, data: &mut [u8]| {
            // Try to get last reading, otherwise send 0.0
            if let Ok(value) = SENSOR_READING.try_receive() {
                data.copy_from_slice(&value);
            } else {
                data.copy_from_slice(&[0u8; 4]);
            }
            4
        };

        gatt!([
            // Custom Sensor Service
            service {
                uuid: "8da4c3d7-0000-4b26-9632-ae207b6b757f",
                characteristics: [
                    // Normalized sensor reading
                    characteristic {
                        name: "sensor_reading",
                        uuid: "8da4c3d7-4514-0001-9632-ae207b6b757f",
                        notify: true,
                        read: read_sensor,
                    }
                ]
            }
        ]);

        // Set up attribute server
        let mut rng = bleps::no_rng::NoRng;
        let mut srv = AttributeServer::new(&mut ble, &mut gatt_attributes, &mut rng);

        let mut notifier = async || {
            let mut data = [0u8; 4];
            data.copy_from_slice(&SENSOR_READING.receive().await);
            NotificationData::new(sensor_reading_handle, &SENSOR_READING.receive().await)
        };

        // Start server
        if let Some(e) = srv.run(&mut notifier).await.err() {
            println!("gatt_server run exited with error: {:?}", e);
        }
    }
}

/// Read sensor task
#[embassy_executor::task]
async fn read_sensor(mut adc: ADC<'static, ADC1>, mut pin: AdcPin<GpioPin<Analog, 35>, ADC1>) {
    loop {
        let raw_value: u32 = nb::block!(adc.read(&mut pin)).unwrap() as u32;
        let value: f32 = normalize_adc_reading(raw_value, true);
        let bytes = value.to_be_bytes();

        // Send bytes to ble runner
        if let Ok(_) = SENSOR_READING.try_send(bytes) {
            println!("{:?}", value);
        };

        // Re-schedule the timer interrupt in 100ms
        Timer::after_millis(100).await;
    }
}

fn normalize_adc_reading(mut rdg: u32, inverted: bool) -> f32 {
    if inverted {
        rdg = 4095 - rdg;
    }

    rdg as f32 / 4096.0
}
@thecodechemist99
Copy link
Author

thecodechemist99 commented Mar 14, 2024

I have also encountered these additional errors, but cannot reliably reproduce them for now:

!! A panic occured in '/Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/ble/btdm.rs', at line 195, column 51:
already borrowed: BorrowMutError

Backtrace:

0x401297fd
0x401297fd - core::cell::panic_already_borrowed
    at /Users/thecodechemist99/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/cell.rs:761
0x400808c8
0x400808c8 - core::cell::RefCell<T>::borrow_mut
    at /Users/thecodechemist99/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/cell.rs:1051
0x400809a4
0x400809a4 - esp_wifi::ble::btdm::queue_send_from_isr
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/ble/btdm.rs:215
0x4008a7ef
0x4008a7ef - btdm_task_post_from_isr
    at ??:??
0x4008abd1
0x4008abd1 - r_rwbtdm_isr_wrapper
    at ??:??
0x400ea55a
0x400ea55a - esp_wifi::timer::chip_specific::__esp_hal_internal_RWBLE
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/timer/timer_esp32.rs:77
0x40081510
0x40081510 - esp_hal::interrupt::xtensa::vectored::handle_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:452
0x400810e2
0x400810e2 - esp_hal::interrupt::xtensa::vectored::handle_interrupts
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:432
0x40080db2
0x40080db2 - __level_1_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:370
0x4008176b
0x4008176b - __default_naked_exception
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/xtensa-lx-rt-0.16.0/src/exception/assembly_esp32.rs:479
Exception occured 'InstrProhibited'
Context
PC=0x00000001       PS=0x00060011
0x00000001 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00060011 - PS_WOE
    at ??:??
A0=0x800814ba       A1=0x3ffc3930       A2=0x00000001       A3=0x3ffc3ce0       A4=0x8010e20d
0x800814ba - _rtc_slow_data_end
    at ??:??
0x3ffc3930 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0x00000001 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x3ffc3ce0 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0x8010e20d - _rtc_slow_data_end
    at ??:??
A5=0x3ffc3a30       A6=0x00060700       A7=0x00000014       A8=0x800ea4ad       A9=0x00000004
0x3ffc3a30 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0x00060700 - PS_WOE
    at ??:??
0x00000014 - XT_STK_A3
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:2037
0x800ea4ad - _rtc_slow_data_end
    at ??:??
0x00000004 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
A10=0x00000000      A11=0x00000005      A12=0x800fd7fd      A13=0x3ffc3a00      A14=0x00060700
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000005 - XT_STK_PS
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:2037
0x800fd7fd - _rtc_slow_data_end
    at ??:??
0x3ffc3a00 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0x00060700 - PS_WOE
    at ??:??
A15=0x00000014
0x00000014 - XT_STK_A3
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:2037
SAR=00000019
EXCCAUSE=0x00000014 EXCVADDR=0x00000000
0x00000014 - XT_STK_A3
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:2037
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
LBEG=0x4000c2e0     LEND=0x4000c2f6     LCOUNT=0x00000000
0x4000c2e0 - memcpy
    at ??:??
0x4000c2f6 - memcpy
    at ??:??
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
THREADPTR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
SCOMPARE1=0x00000100
0x00000100 - esp_wifi::compat::timer_compat::compat_timer_disarm
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200
BR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
ACCLO=0x00000000    ACCHI=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
M0=0x00000000       M1=0x00000000       M2=0x00000000       M3=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F64R_LO=0x00000000  F64R_HI=0x00000000  F64S=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
FCR=0x00000000      FSR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F0=0x00000000       F1=0x00000000       F2=0x00000000       F3=0x00000000       F4=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F5=0x00000000       F6=0x00000000       F7=0x00000000       F8=0x00000000       F9=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F10=0x00000000      F11=0x00000000      F12=0x00000000      F13=0x00000000      F14=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F15=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521

0x40080db5
0x40080db5 - __level_1_interrupt
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-hal-0.16.1/src/interrupt/xtensa.rs:371
0x4008176e
0x4008176e - __default_naked_exception
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/xtensa-lx-rt-0.16.0/src/exception/assembly_esp32.rs:479
0x40040001
0x40040001 - r_ld_sched_sniff_add
    at ??:??
0x40080a1f
0x40080a1f - esp_wifi::ble::btdm::cause_sw_intr_to_core
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp-wifi-0.4.0/src/ble/btdm.rs:337
0x401506ab
0x401506ab - vhci_send
    at ??:??
0x4013d895
0x4013d895 - r_eif_send
    at ??:??
0x40017920
0x40017920 - ecc_point_multiplication_uint8_256
    at ??:??
0x4013e244
0x4013e244 - hci_tx_start
    at ??:??
0x4013e321
0x4013e321 - r_hci_tl_send
    at ??:??
0x400188f8
0x400188f8 - r_hci_send_2_host
    at ??:??
Exception occured 'InstrProhibited'
Context
PC=0x00000100       PS=0x00060b10
0x00000100 - esp_wifi::compat::timer_compat::compat_timer_disarm
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200
0x00060b10 - PS_WOE
    at ??:??
A0=0x8013a511       A1=0x3ffc3fd0       A2=0xffffffff       A3=0x3ffc4070       A4=0x00000000
0x8013a511 - _rtc_slow_bss_end
    at ??:??
0x3ffc3fd0 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0xffffffff - _rtc_slow_bss_end
    at ??:??
0x3ffc4070 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
A5=0x3ffc1e08       A6=0x00000100       A7=0x00060500       A8=0x800e7eb6       A9=0x3ffc3fa0
0x3ffc1e08 - _ZN8esp_wifi3ble4btdm17BT_INTERNAL_QUEUE17ha0a9867436a680faE
    at ??:??
0x00000100 - esp_wifi::compat::timer_compat::compat_timer_disarm
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200
0x00060500 - PS_WOE
    at ??:??
0x800e7eb6 - _rtc_slow_bss_end
    at ??:??
0x3ffc3fa0 - _ZN8esp_wifi7preempt10TASK_STACK17hb41e634bda03073bE
    at ??:??
A10=0x00060500      A11=0x3ffc6ca8      A12=0x3ffb8d40      A13=0x3ffc6ae8      A14=0x00000001
0x00060500 - PS_WOE
    at ??:??
0x3ffc6ca8 - btdm_slp_err
    at ??:??
0x3ffb8d40 - sw_to_hw
    at ??:??
0x3ffc6ae8 - r_osi_funcs_p
    at ??:??
0x00000001 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
A15=0x3ffc6af0
0x3ffc6af0 - r_modules_funcs_p
    at ??:??
SAR=00000003
EXCCAUSE=0x00000014 EXCVADDR=0x00000100
0x00000014 - XT_STK_A3
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:2037
0x00000100 - esp_wifi::compat::timer_compat::compat_timer_disarm
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200
LBEG=0x4000c2e0     LEND=0x4000c2f6     LCOUNT=0x00000000
0x4000c2e0 - memcpy
    at ??:??
0x4000c2f6 - memcpy
    at ??:??
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
THREADPTR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
SCOMPARE1=0x00000100
0x00000100 - esp_wifi::compat::timer_compat::compat_timer_disarm
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/critical-section-1.1.2/src/lib.rs:200
BR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
ACCLO=0x00000000    ACCHI=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
M0=0x00000000       M1=0x00000000       M2=0x00000000       M3=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F64R_LO=0x00000000  F64R_HI=0x00000000  F64S=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
FCR=0x00000000      FSR=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F0=0x00000000       F1=0x00000000       F2=0x00000000       F3=0x00000000       F4=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F5=0x00000000       F6=0x00000000       F7=0x00000000       F8=0x00000000       F9=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F10=0x00000000      F11=0x00000000      F12=0x00000000      F13=0x00000000      F14=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521
F15=0x00000000
0x00000000 - esp32::AES::steal
    at /Users/thecodechemist99/.cargo/registry/src/index.crates.io-6f17d22bba15001f/esp32-0.29.0/src/lib.rs:1521

@bjoernQ
Copy link
Contributor

bjoernQ commented Mar 14, 2024

Can you run the provided examples without issues?

both of the crashes sound a lot like stack or general memory corruption to me

@thecodechemist99
Copy link
Author

thecodechemist99 commented Mar 14, 2024

Can you run the provided examples without issues?

The ble examples work fine. I also had all parts of my own code working before, reading the sensor and connecting via bluetooth. But when I try to put it all together, I end up with these errors.

both of the crashes sound a lot like stack or general memory corruption to me
If that would be the case, what could I do about it?

Edit: I have been able to make it work, but only by stripping out all asynchronous and embassy-related code. And it’s still very shaky, e.g. once I check if the value changed before sending a notification it breaks again.

@bjoernQ
Copy link
Contributor

bjoernQ commented Mar 15, 2024

Ok looking again there is one obvious thing in the code - nothing weird and explainable.

 // Define service methods
        let mut read_sensor = |_offset: usize, data: &mut [u8]| {
            // Try to get last reading, otherwise send 0.0
            if let Ok(value) = SENSOR_READING.try_receive() {
                data.copy_from_slice(&value);
            } else {
                data.copy_from_slice(&[0u8; 4]);
            }
            4
        };

The data slice is 255 bytes so the correct thing would be

        // Define service methods
        let mut read_sensor = |_offset: usize, data: &mut [u8]| {
            // Try to get last reading, otherwise send 0.0
            if let Ok(value) = SENSOR_READING.try_receive() {
                data[..4].copy_from_slice(&value);
            } else {
                data[..4].copy_from_slice(&[0u8; 4]);
            }
            4
        };

@thecodechemist99
Copy link
Author

@bjoernQ thanks, I already found and fixed that yesterday. Didn‘t notice it was still in here.

@bjoernQ
Copy link
Contributor

bjoernQ commented Mar 15, 2024

I guess we can close this issue now

@bjoernQ bjoernQ closed this as completed Mar 15, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in esp-rs Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

2 participants