Skip to content

Commit

Permalink
Fix async SPI on ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 30, 2025
1 parent 2ff28b1 commit c115645
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ impl<'d> Spi<'d, Async> {
return Ok(());
}

SpiFuture::new(&driver).await;
let future = SpiFuture::setup(&driver).await;
future.await;

Ok(())
}
Expand Down Expand Up @@ -3270,10 +3271,11 @@ impl Driver {

/// Starts the operation and waits for it to complete.
async fn execute_operation_async(&self) {
self.enable_listen(SpiInterrupt::TransferDone.into(), false);
self.clear_interrupts(SpiInterrupt::TransferDone.into());
// On ESP32, the interrupt seems to not fire in specific circumstances, when
// `listen` is called after `start_operation`. Let's call it before, to be sure.
let future = SpiFuture::setup(self).await;
self.start_operation();
SpiFuture::new(self).await;
future.await;
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -3576,8 +3578,13 @@ struct SpiFuture<'a> {
}

impl<'a> SpiFuture<'a> {
pub fn new(driver: &'a Driver) -> Self {
Self { driver }
fn setup(driver: &'a Driver) -> impl Future<Output = Self> {
core::future::poll_fn(move |cx| {
driver.state.waker.register(cx.waker());
driver.clear_interrupts(SpiInterrupt::TransferDone.into());
driver.enable_listen(SpiInterrupt::TransferDone.into(), true);
Poll::Ready(Self { driver })
})
}
}

Expand All @@ -3590,7 +3597,7 @@ use core::{
impl Future for SpiFuture<'_> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
if self
.driver
.interrupts()
Expand All @@ -3601,9 +3608,6 @@ impl Future for SpiFuture<'_> {
return Poll::Ready(());
}

self.driver.state.waker.register(cx.waker());
self.driver
.enable_listen(SpiInterrupt::TransferDone.into(), true);
Poll::Pending
}
}
Expand Down

0 comments on commit c115645

Please sign in to comment.