Skip to content

Commit

Permalink
Try to only provide TX token if there is enough memory
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 25, 2023
1 parent a829d22 commit 0ff09fb
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -978,12 +978,13 @@ impl Device for WifiDevice<'_> {
}

fn transmit(&mut self, _instant: smoltcp::time::Instant) -> Option<Self::TxToken<'_>> {
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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -1320,11 +1322,14 @@ pub(crate) mod embassy {

fn transmit(&mut self, cx: &mut core::task::Context) -> Option<Self::TxToken<'_>> {
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 {
Expand Down

0 comments on commit 0ff09fb

Please sign in to comment.