Skip to content

Commit

Permalink
SAI example with wolfson 8960 codec
Browse files Browse the repository at this point in the history
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
SpinFast committed Nov 23, 2023
1 parent e5410c0 commit 21caff6
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
22 changes: 22 additions & 0 deletions board/src/imxrt1010evk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ pub type Console = hal::lpuart::Lpuart<ConsolePins, 1>;
/// host and the MCU.
pub type ConsolePins = crate::hal::lpuart::Pins<iomuxc::gpio::GPIO_10, iomuxc::gpio::GPIO_09>;

pub type Sai1MclkPin = iomuxc::gpio::GPIO_08;

pub type Sai1TxPins =
hal::sai::Pins<iomuxc::gpio::GPIO_07, iomuxc::gpio::GPIO_06, iomuxc::gpio::GPIO_04>;

pub type Sai1RxPins =
hal::sai::Pins<iomuxc::gpio::GPIO_02, iomuxc::gpio::GPIO_01, iomuxc::gpio::GPIO_03>;

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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() };
Expand Down Expand Up @@ -248,6 +269,7 @@ impl Specifics {
console,
spi,
i2c,
sai1,
pwm,
tp34: iomuxc.gpio_sd.p02,
tp31: iomuxc.gpio_sd.p01,
Expand Down
123 changes: 123 additions & 0 deletions examples/rtic_sai_wm8960.rs
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
}
}

0 comments on commit 21caff6

Please sign in to comment.