diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index 6f43d3dc..d859ee3a 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -969,7 +969,7 @@ impl Device for WifiDevice<'_> { ) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { critical_section::with(|cs| { let rx = DATA_QUEUE_RX.borrow_ref_mut(cs); - if !rx.is_empty() && esp_wifi_can_send() { + if !rx.is_empty() && esp_wifi_can_send(cs) { Some((WifiRxToken::default(), WifiTxToken::default())) } else { None @@ -978,12 +978,13 @@ impl Device for WifiDevice<'_> { } fn transmit(&mut self, _instant: smoltcp::time::Instant) -> Option> { - if esp_wifi_can_send() { - Some(WifiTxToken::default()) - } else { - warn!("no Tx token available"); - None - } + critical_section::with(|cs| { + if esp_wifi_can_send(cs) { + Some(WifiTxToken::default()) + } else { + None + } + }) } fn capabilities(&self) -> smoltcp::phy::DeviceCapabilities { @@ -1061,8 +1062,9 @@ where res } -fn esp_wifi_can_send() -> bool { +fn esp_wifi_can_send(cs: critical_section::CriticalSection) -> bool { WIFI_TX_INFLIGHT.load(Ordering::SeqCst) < TX_QUEUE_SIZE + && crate::HEAP.borrow_ref(cs).free() > 2 * DATA_FRAME_SIZE } // FIXME data here has to be &mut because of `esp_wifi_internal_tx` signature, requiring a *mut ptr to the buffer @@ -1310,7 +1312,7 @@ pub(crate) mod embassy { critical_section::with(|cs| { let rx = DATA_QUEUE_RX.borrow_ref_mut(cs); - if !rx.is_empty() && esp_wifi_can_send() { + if !rx.is_empty() && esp_wifi_can_send(cs) { Some((WifiRxToken::default(), WifiTxToken::default())) } else { None @@ -1320,11 +1322,14 @@ pub(crate) mod embassy { fn transmit(&mut self, cx: &mut core::task::Context) -> Option> { TRANSMIT_WAKER.register(cx.waker()); - if esp_wifi_can_send() { - Some(WifiTxToken::default()) - } else { - None - } + + critical_section::with(|cs| { + if esp_wifi_can_send(cs) { + Some(WifiTxToken::default()) + } else { + None + } + }) } fn link_state(&mut self, cx: &mut core::task::Context) -> embassy_net_driver::LinkState {