Skip to content

Commit

Permalink
Synchronise sending and receiving I2S frames
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Aug 15, 2024
1 parent c7568f3 commit 843159f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion hil-test/tests/i2s_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ use esp_hal::{
system::SystemControl,
Async,
};
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};

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;

Expand Down Expand Up @@ -53,6 +63,7 @@ impl Iterator for SampleSource {

#[embassy_executor::task]
async fn writer(
control: &'static Signal<CriticalSectionRawMutex, ()>,
tx_buffer: &'static mut [u8],
i2s_tx: I2sTx<
'static,
Expand All @@ -69,6 +80,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() {
Expand Down Expand Up @@ -144,13 +156,16 @@ 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<CriticalSectionRawMutex, ()>, Signal::new());

spawner.must_spawn(writer(send_next_frame, tx_buffer, i2s_tx));
let mut rx_transfer = i2s_rx.read_dma_circular_async(rx_buffer).unwrap();

let mut rcv = [0u8; BUFFER_SIZE];
let mut sample_idx = 0;
let mut samples = SampleSource::new();
for _ in 0..30 {
send_next_frame.signal(());
let len = rx_transfer.pop(&mut rcv).await.unwrap();
for &b in &rcv[..len] {
let expected = samples.next().unwrap();
Expand Down

0 comments on commit 843159f

Please sign in to comment.