-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides a sample application for playing back audio without DMA directly writing to the FIFO of the SAI peripheral, which should result in an I2S signaling to the WM8960 codec on the EVK boards.
- Loading branch information
Showing
2 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
//! Audio playback using sai peripheral and imxrt-hal. | ||
//! | ||
//! Plays back a simple 440Hz (A note) simple square wave tone with the SAI peripheral | ||
//! to a Wolfson WM8960 codec on a number of the EVK boards. | ||
//! | ||
//! The audio stream itself is expected to be a 48000Hz 16bit stereo signal. | ||
#![no_main] | ||
#![no_std] | ||
#[rtic::app(device = board, peripherals = false, dispatchers = [BOARD_SWTASK0])] | ||
mod app { | ||
|
||
// | ||
// Configure the demo below. | ||
// | ||
|
||
/// How frequently (milliseconds) should we poll audio | ||
const AUDIO_POLL_MS: u32 = board::PIT_FREQUENCY / 1_000 * 250; | ||
|
||
use imxrt_hal as hal; | ||
// | ||
// End configurations. | ||
// | ||
|
||
#[local] | ||
struct Local { | ||
/// Toggle when we poll. | ||
led: board::Led, | ||
/// Serial audio interface | ||
sai1_tx: hal::sai::Tx<1, 16, 2, hal::sai::PackingNone>, | ||
|
||
/// This timer tells us how frequently work on audio. | ||
audio_pit: hal::pit::Pit<2>, | ||
} | ||
|
||
#[shared] | ||
struct Shared {} | ||
|
||
#[init] | ||
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { | ||
let mut cortex_m = cx.core; | ||
let ( | ||
board::Common { | ||
pit: (_, _, mut audio_pit, _), | ||
.. | ||
}, | ||
board::Specifics { led, mut sai1, .. }, | ||
) = board::new(); | ||
let (Some(mut sai1_tx), None) = sai1.split() else { | ||
panic!("Unexpected return from sai split"); | ||
}; | ||
|
||
let regs = sai1_tx.reg_dump(); | ||
defmt::println!( | ||
"Regdump of config: TCR1: {:b}, TCR2 {:b}, TCR3 {:b}, TCR4 {:b}, TCR5 {:b}", | ||
regs[0], | ||
regs[1], | ||
regs[2], | ||
regs[3], | ||
regs[4] | ||
); | ||
|
||
cortex_m.DCB.enable_trace(); | ||
cortex_m::peripheral::DWT::unlock(); | ||
cortex_m.DWT.enable_cycle_counter(); | ||
|
||
audio_pit.set_load_timer_value(AUDIO_POLL_MS); | ||
audio_pit.set_interrupt_enable(true); | ||
audio_pit.enable(); | ||
|
||
( | ||
Shared {}, | ||
Local { | ||
led, | ||
sai1_tx, | ||
audio_pit, | ||
}, | ||
init::Monotonics(), | ||
) | ||
} | ||
|
||
#[task(binds = BOARD_PIT, local = [led, audio_pit, sai1_tx, counter: u16 = 0], priority = 1)] | ||
fn pit_interrupt(cx: pit_interrupt::Context) { | ||
let pit_interrupt::LocalResources { | ||
audio_pit, | ||
sai1_tx, | ||
led, | ||
counter, | ||
} = cx.local; | ||
|
||
// Is it time for us to send a new log message? | ||
if audio_pit.is_elapsed() { | ||
led.toggle(); | ||
while audio_pit.is_elapsed() { | ||
audio_pit.clear_elapsed(); | ||
} | ||
|
||
sai1_tx.write_frame(0, [*counter, *counter + 1]); | ||
|
||
let count = cycles(|| {}); | ||
|
||
let (write_pos, read_pos) = sai1_tx.fifo_position(0); | ||
|
||
defmt::println!( | ||
"Audio synthesis took {=u32} cycles, tx status {:#x}, fifo write pos {}, read pos {}", | ||
count, | ||
sai1_tx.status(), | ||
write_pos, | ||
read_pos, | ||
); | ||
|
||
*counter += 2; | ||
} | ||
} | ||
|
||
/// Count the clock cycles required to execute `f` | ||
fn cycles<F: FnOnce()>(f: F) -> u32 { | ||
let start = cortex_m::peripheral::DWT::cycle_count(); | ||
f(); | ||
let end = cortex_m::peripheral::DWT::cycle_count(); | ||
end - start | ||
} | ||
} |