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

Panic on Peripheral::advertise when using esp32c6 #229

Closed
eloycoto opened this issue Jan 9, 2025 · 18 comments · Fixed by #235
Closed

Panic on Peripheral::advertise when using esp32c6 #229

eloycoto opened this issue Jan 9, 2025 · 18 comments · Fixed by #235

Comments

@eloycoto
Copy link

eloycoto commented Jan 9, 2025

When the reaching here:

host.command(LeSetAdvParams::new(
params.interval_min.into(),
params.interval_max.into(),
kind,
host.address.map(|a| a.kind).unwrap_or(AddrKind::PUBLIC),
peer.kind,
peer.addr,
params.channel_map.unwrap_or(AdvChannelMap::ALL),
params.filter_policy,
))

the BleHost:command is called, but if fails on OnceLock on initalized

    pub(crate) async fn command<C>(&self, cmd: C) -> Result<C::Return, BleHostError<T::Error>>
    where
        C: SyncCmd,
        T: ControllerCmdSync<C>,
    {
        let _ = self.initialized.get().await;

        let ret = cmd.exec(&self.controller).await?;
        Ok(ret)
    }

And the panic:

INFO - Our address = Address { kind: AddrKind(1), addr: BdAddr([65, 90, 227, 30, 131, 231]) }
INFO - Starting advertising and GATT service
INFO - [host] filter accept list size: 12
INFO - [host] setting txq to 24, fragmenting at 255
INFO - [host] configuring host buffers (8 packets of size 251)


====================== PANIC ======================
panicked at /tmp/trouble_bk/examples/apps/src/ble_bas_peripheral.rs:104:13:
[ble_task] error: BleHost(Hci(Invalid HCI Command Parameters))

Backtrace:

0x4200e91e
0x4200e91e - embassy_executor::raw::util::SyncUnsafeCell<T>::get
    at /home/eloy/.cargo/registry/src/index.crates.io-6f17d22bba15001f/embassy-executor-0.7.0/src/raw/util.rs:55
0x42016b90
0x42016b90 - main
    at /tmp/trouble_bk/examples/esp32/src/bin/ble_bas_peripheral.rs:11
0x420055e8
0x420055e8 - hal_main
    at /home/eloy/.cargo/git/checkouts/esp-hal-42ec44e8c6943228/7288d03/esp-hal/src/lib.rs:485

I might fix it, but I need to check how to use gdb with espflash, etc.. it might take a while, so any hint it's welcome :-)

Using bas_peripheral_example and esp32c6

@eloycoto
Copy link
Author

Ok, found the problem! host is not initialized, so advertise breaks, need to wait until

trouble/host/src/host.rs

Lines 831 to 833 in 419e298

let _ = host.initialized.init(InitialState {
acl_max: ret.le_acl_data_packet_length as usize,
});

@lure23
Copy link
Contributor

lure23 commented Jan 10, 2025

I noticed this yesterday, as well. Great to have you on it.

@eloycoto
Copy link
Author

ok, just for tracking it, after checking deeply, looks like that this exec is what it's failing:

trouble/host/src/host.rs

Lines 814 to 822 in 419e298

HostBufferSize::new(
host.rx_pool.mtu() as u16,
0,
config::L2CAP_RX_PACKET_POOL_SIZE as u16,
0,
)
.exec(&host.controller)
.await?;

@lulf
Copy link
Member

lulf commented Jan 10, 2025

ok, just for tracking it, after checking deeply, looks like that this exec is what it's failing:

trouble/host/src/host.rs

Lines 814 to 822 in 419e298

HostBufferSize::new(
host.rx_pool.mtu() as u16,
0,
config::L2CAP_RX_PACKET_POOL_SIZE as u16,
0,
)
.exec(&host.controller)
.await?;

@eloycoto Is this command producing an error or just hanging?

@lure23

This comment was marked as off-topic.

@eloycoto
Copy link
Author

@eloycoto Is this command producing an error or just hanging?

In this case, it's not panic (It panics in the example), just return an invalid parameter error.

@eloycoto
Copy link
Author

And the params are the following:

configuring host buffers (8 packets of size 251)

And this is the error:

BleHost(Hci(Invalid HCI Command Parameters))

@eloycoto
Copy link
Author

Was checking the doc for the command:

https://www.bluetooth.com/wp-content/uploads/Files/Specification/HTML/Core-54/out/en/host-controller-interface/host-controller-interface-functional-specification.html#UUID-99748527-de87-bf9c-935d-baf7e3f35b12

And looks like that Host_Synchronous_Data_Packet_Length should be 1, 0 is not a valid one:

Tried this, and also fails :-(

 HostBufferSize::new( 
     host.rx_pool.mtu() as u16, 
     1, 
     config::L2CAP_RX_PACKET_POOL_SIZE as u16, 
     0, 
 ) 
 .exec(&host.controller) 
 .await?; 

@sjroe
Copy link
Contributor

sjroe commented Jan 11, 2025

I've been having the same issue with the esp32c6 and I noticed that all of the other examples except from bas_peripheral_example had the following:

/// Size of L2CAP packets
#[cfg(not(feature = "esp"))]
pub const L2CAP_MTU: usize = 128;
#[cfg(feature = "esp")]
// Some esp chips only accept an MTU >= 1017
pub const L2CAP_MTU: usize = 1017;

Adding the above to the bas_peripheral_example stops it from panicing and I can now see and interact with device via bluetooth.

Note that I'm using a Waveshare ESP32-C6 Development Board that has a ESP32-C6-WROOM-1-N8 Module.

@lulf
Copy link
Member

lulf commented Jan 11, 2025

Yup, this should probably be set for ble_bas_peripheral as well, good catch! Can you create a PR with the fix?

@eloycoto
Copy link
Author

I can confirm that it's working, thanks @sjroe for the hint!

I'll let @sjroe to make the PR for the credit, if he didn't do that in the next couple of days I'll submit it!

@lulf where you can see the bigger values for MTU? I was checking the datasheet yesterday and I didn't spot it.

@lure23
Copy link
Contributor

lure23 commented Jan 11, 2025

I had always thought that esp feature affected "some" ESP32 chips - but not e.g. ESP32-C6. I think the feature name (as esp) is too vague and unfocused. It's only used for steering this one value, so how about either:

l2cap-mtu-1017 or esp32-l2cap-mtu-1017

That would give an idea already in the Cargo.toml as to what it might be about. Plus documentation to tell, which chips require it.

@lulf
Copy link
Member

lulf commented Jan 11, 2025

Agreed to renamed the feature, i prefer the first variant

@eloycoto
Copy link
Author

Just for context, sometimes when I flash/start again the ble, the init of esp_wifi::init got stuck in the interrupts (c6)

https://github.com/esp-rs/esp-hal/blob/7d7ce279d139bfd306f2dee586c198f1821bbf15/esp-wifi/src/timer/timer_esp32c6.rs#L29-L32

Mostly here:

https://github.com/esp-rs/esp-hal/blob/7d7ce279d139bfd306f2dee586c198f1821bbf15/esp-hal/src/interrupt/riscv.rs#L364-L366

When interrupt_number 4, cpu_interrupt_number 1

@jamessizeland jamessizeland reopened this Jan 13, 2025
@jamessizeland
Copy link
Collaborator

Here's my terminal output from running cargo esp32c6 --bin ble_bas_peripheral twice on both my waveshare and tinyC6.

https://gist.github.com/jamessizeland/91cd55d0e80e5995cb413a915ee6849e

@jamessizeland
Copy link
Collaborator

esp-rs/esp-hal#2883 added to esp_wifi issue

@eloycoto
Copy link
Author

Problem has been fixed upstream:

esp-rs/esp-hal#2981

I guess that esp-hal need to be updated to the latest version in examples

@eloycoto
Copy link
Author

closed by c5c2eab, thanks for fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants