From 21caff6130564d8cf603220568aeeabf7cb28297 Mon Sep 17 00:00:00 2001 From: Spinfast Date: Tue, 31 Oct 2023 16:08:56 -0500 Subject: [PATCH] SAI example with wolfson 8960 codec 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. --- board/src/imxrt1010evk.rs | 22 +++++++ examples/rtic_sai_wm8960.rs | 123 ++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 examples/rtic_sai_wm8960.rs diff --git a/board/src/imxrt1010evk.rs b/board/src/imxrt1010evk.rs index eb3b3af2..b34b8284 100644 --- a/board/src/imxrt1010evk.rs +++ b/board/src/imxrt1010evk.rs @@ -57,6 +57,16 @@ pub type Console = hal::lpuart::Lpuart; /// host and the MCU. pub type ConsolePins = crate::hal::lpuart::Pins; +pub type Sai1MclkPin = iomuxc::gpio::GPIO_08; + +pub type Sai1TxPins = + hal::sai::Pins; + +pub type Sai1RxPins = + hal::sai::Pins; + +pub type Sai1 = hal::sai::Sai<1, Sai1MclkPin, Sai1TxPins, ()>; + pub type SpiPins = hal::lpspi::Pins< iomuxc::gpio_ad::GPIO_AD_04, // SDO, J57_8 iomuxc::gpio_ad::GPIO_AD_03, // SDI, J57_10 @@ -146,6 +156,7 @@ pub struct Specifics { pub console: Console, pub spi: Spi, pub i2c: I2c, + pub sai1: Sai1, pub pwm: Pwm, pub tp34: Tp34, pub tp31: Tp31, @@ -180,6 +191,16 @@ impl Specifics { console.set_parity(None); }); + let sai1 = { + let sai1 = unsafe { ral::sai::SAI1::instance() }; + let pins = Sai1TxPins { + sync: iomuxc.gpio.p07, + bclk: iomuxc.gpio.p06, + data: iomuxc.gpio.p04, + }; + Sai1::from_tx(sai1, iomuxc.gpio.p08, pins) + }; + #[cfg(feature = "spi")] let spi = { let lpspi1 = unsafe { ral::lpspi::LPSPI1::instance() }; @@ -248,6 +269,7 @@ impl Specifics { console, spi, i2c, + sai1, pwm, tp34: iomuxc.gpio_sd.p02, tp31: iomuxc.gpio_sd.p01, diff --git a/examples/rtic_sai_wm8960.rs b/examples/rtic_sai_wm8960.rs new file mode 100644 index 00000000..c21abaab --- /dev/null +++ b/examples/rtic_sai_wm8960.rs @@ -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: F) -> u32 { + let start = cortex_m::peripheral::DWT::cycle_count(); + f(); + let end = cortex_m::peripheral::DWT::cycle_count(); + end - start + } +}