diff --git a/hil-test/tests/i2s_async.rs b/hil-test/tests/i2s_async.rs index 9addb294519..44909642bd1 100644 --- a/hil-test/tests/i2s_async.rs +++ b/hil-test/tests/i2s_async.rs @@ -11,6 +11,7 @@ #![no_main] use defmt_rtt as _; +use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}; use esp_backtrace as _; use esp_hal::{ clock::ClockControl, @@ -18,12 +19,21 @@ use esp_hal::{ gpio::Io, i2s::{asynch::*, DataFormat, I2s, I2sTx, Standard}, peripheral::Peripheral, - peripherals::{I2S0, Peripherals}, + peripherals::{Peripherals, I2S0}, prelude::*, system::SystemControl, Async, }; +macro_rules! mk_static { + ($t:ty,$val:expr) => {{ + static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new(); + #[deny(unused_attributes)] + let x = STATIC_CELL.uninit().write(($val)); + x + }}; +} + const BUFFER_SIZE: usize = 2000; #[derive(Clone)] @@ -53,13 +63,9 @@ impl Iterator for SampleSource { #[embassy_executor::task] async fn writer( + control: &'static Signal, tx_buffer: &'static mut [u8], - i2s_tx: I2sTx< - 'static, - I2S0, - DmaChannel0, - Async - >, + i2s_tx: I2sTx<'static, I2S0, DmaChannel0, Async>, ) { let mut samples = SampleSource::new(); for b in tx_buffer.iter_mut() { @@ -69,6 +75,7 @@ async fn writer( let mut tx_transfer = i2s_tx.write_dma_circular_async(tx_buffer).unwrap(); loop { + control.wait().await; tx_transfer .push_with(|buffer| { for b in buffer.iter_mut() { @@ -144,8 +151,10 @@ mod tests { i2s.rx_conf().modify(|_, w| w.rx_update().set_bit()); } - spawner.must_spawn(writer(tx_buffer, i2s_tx)); + let send_next_frame = mk_static!(Signal, Signal::new()); + let mut rx_transfer = i2s_rx.read_dma_circular_async(rx_buffer).unwrap(); + spawner.must_spawn(writer(send_next_frame, tx_buffer, i2s_tx)); let mut rcv = [0u8; BUFFER_SIZE]; let mut sample_idx = 0; @@ -154,9 +163,14 @@ mod tests { let len = rx_transfer.pop(&mut rcv).await.unwrap(); for &b in &rcv[..len] { let expected = samples.next().unwrap(); - assert_eq!(b, expected, "Sample #{} does not match ({} != {})", sample_idx, b, expected); + assert_eq!( + b, expected, + "Sample #{} does not match ({} != {})", + sample_idx, b, expected + ); sample_idx += 1; } + send_next_frame.signal(()); } } }