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 7a439eb
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions hil-test/tests/i2s_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@
#![no_main]

use defmt_rtt as _;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
dma::{Dma, DmaChannel0, DmaPriority},
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)]
Expand Down Expand Up @@ -53,13 +63,9 @@ impl Iterator for SampleSource {

#[embassy_executor::task]
async fn writer(
control: &'static Signal<CriticalSectionRawMutex, ()>,
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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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<CriticalSectionRawMutex, ()>, 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;
Expand All @@ -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(());
}
}
}

0 comments on commit 7a439eb

Please sign in to comment.