Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add I2S module #201

Merged
merged 17 commits into from
Aug 28, 2020
Merged
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"examples/lpcomp-demo",
"examples/qdec-demo",
"examples/comp-demo",
"examples/i2s-controller-demo",
"examples/i2s-peripheral-demo",
"examples/pwm-demo",
"examples/twim-demo",
"examples/twis-demo",
Expand Down
20 changes: 20 additions & 0 deletions examples/i2s-controller-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "i2s-controller-demo"
version = "0.1.0"
authors = ["Henrik Alsér"]
edition = "2018"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.6.2"
cortex-m-rtic = "0.5.3"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
heapless = "0.5.5"
small_morse = "0.1.0"

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
21 changes: 21 additions & 0 deletions examples/i2s-controller-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[default.probe]
protocol = "Swd"

[default.flashing]
enabled = true
halt_afterwards = false
restore_unwritten_bytes = false

[default.general]
chip = "nRF52840"
chip_descriptions = []
log_level = "Warn"

[default.rtt]
enabled = true
channels = []
timeout = 3000
show_timestamps = true

[default.gdb]
enabled = false
229 changes: 229 additions & 0 deletions examples/i2s-controller-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
#![no_std]
#![no_main]

// I2S `controller mode` demo
// Generates Morse code audio signals for text from UART, playing back over I2S
// Tested with nRF52840-DK and a UDA1334a DAC

use embedded_hal::digital::v2::{InputPin, OutputPin};
use heapless::{
consts::*,
spsc::{Consumer, Producer, Queue},
};
use small_morse::{encode, State};
use {
core::{
panic::PanicInfo,
sync::atomic::{compiler_fence, Ordering},
},
hal::{
gpio::{Input, Level, Output, Pin, PullUp, PushPull},
gpiote::*,
i2s::*,
pac::{TIMER0, UARTE0},
timer::Timer,
uarte::*,
},
nrf52840_hal as hal,
rtic::cyccnt::U32Ext,
rtt_target::{rprintln, rtt_init_print},
};

#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
const APP: () = {
struct Resources {
i2s: hal::i2s::I2S,
#[init([0; 32])]
signal_buf: [i16; 32],
#[init([0; 32])]
mute_buf: [i16; 32],
#[init(None)]
queue: Option<Queue<State, U256>>,
producer: Producer<'static, State, U256>,
consumer: Consumer<'static, State, U256>,
#[init(5_000_000)]
speed: u32,
uarte: Uarte<UARTE0>,
uarte_timer: Timer<TIMER0>,
gpiote: Gpiote,
btn1: Pin<Input<PullUp>>,
btn2: Pin<Input<PullUp>>,
led: Pin<Output<PushPull>>,
}

#[init(resources = [queue, signal_buf, mute_buf], spawn = [tick])]
fn init(mut ctx: init::Context) -> init::LateResources {
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();
// Enable the monotonic timer (CYCCNT)
ctx.core.DCB.enable_trace();
ctx.core.DWT.enable_cycle_counter();
rtt_init_print!();

let p0 = hal::gpio::p0::Parts::new(ctx.device.P0);

// Configure I2S controller
let mck_pin = p0.p0_28.into_push_pull_output(Level::Low).degrade();
let sck_pin = p0.p0_29.into_push_pull_output(Level::Low).degrade();
let lrck_pin = p0.p0_31.into_push_pull_output(Level::Low).degrade();
let sdout_pin = p0.p0_30.into_push_pull_output(Level::Low).degrade();

let i2s = I2S::new_controller(
ctx.device.I2S,
Some(&mck_pin),
&sck_pin,
&lrck_pin,
None,
Some(&sdout_pin),
);
i2s.tx_buffer(&ctx.resources.mute_buf[..]).ok();
i2s.enable().start();

// Fill signal buffer with triangle waveform, 2 channels interleaved
let signal_buf = ctx.resources.signal_buf;
let len = signal_buf.len() / 2;
for x in 0..len {
signal_buf[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
signal_buf[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
}

// Configure buttons
let btn1 = p0.p0_11.into_pullup_input().degrade();
let btn2 = p0.p0_12.into_pullup_input().degrade();
let gpiote = Gpiote::new(ctx.device.GPIOTE);
gpiote.port().input_pin(&btn1).low();
gpiote.port().input_pin(&btn2).low();
gpiote.port().enable_interrupt();

// Configure the onboard USB CDC UARTE
let uarte = Uarte::new(
ctx.device.UARTE0,
Pins {
txd: p0.p0_06.into_push_pull_output(Level::High).degrade(),
rxd: p0.p0_08.into_floating_input().degrade(),
cts: None,
rts: None,
},
Parity::EXCLUDED,
Baudrate::BAUD115200,
);

*ctx.resources.queue = Some(Queue::new());
let (producer, consumer) = ctx.resources.queue.as_mut().unwrap().split();

rprintln!("Morse code generator");
rprintln!("Send me text over UART @ 115_200 baud");
rprintln!("Press button 1 to slow down or button 2 to speed up");

ctx.spawn.tick().ok();

init::LateResources {
i2s,
producer,
consumer,
gpiote,
btn1,
btn2,
led: p0.p0_13.into_push_pull_output(Level::High).degrade(),
uarte,
uarte_timer: Timer::new(ctx.device.TIMER0),
}
}

#[idle(resources=[uarte, uarte_timer, producer])]
fn idle(ctx: idle::Context) -> ! {
let idle::Resources {
uarte,
uarte_timer,
producer,
} = ctx.resources;
let uarte_rx_buf = &mut [0u8; 64][..];
loop {
match uarte.read_timeout(uarte_rx_buf, uarte_timer, 100_000) {
Ok(_) => {
if let Ok(msg) = core::str::from_utf8(&uarte_rx_buf[..]) {
rprintln!("{}", msg);
for action in encode(msg) {
for _ in 0..action.duration {
producer.enqueue(action.state).ok();
}
}
}
}
Err(hal::uarte::Error::Timeout(n)) if n > 0 => {
if let Ok(msg) = core::str::from_utf8(&uarte_rx_buf[0..n]) {
rprintln!("{}", msg);
for action in encode(msg) {
for _ in 0..action.duration {
producer.enqueue(action.state).ok();
}
}
}
}
_ => {}
}
}
}

#[task(resources = [consumer, i2s, signal_buf, mute_buf, led, speed], schedule = [tick])]
fn tick(ctx: tick::Context) {
let i2s = ctx.resources.i2s;
match ctx.resources.consumer.dequeue() {
Some(State::On) => {
// Move TX pointer to signal buffer (sound ON)
i2s.tx_buffer(&ctx.resources.signal_buf[..]).ok();
ctx.resources.led.set_low().ok();
}
_ => {
// Move TX pointer to silent buffer (sound OFF)
i2s.tx_buffer(&ctx.resources.mute_buf[..]).ok();
ctx.resources.led.set_high().ok();
}
}
ctx.schedule
.tick(ctx.scheduled + ctx.resources.speed.cycles())
.ok();
}

#[task(binds = GPIOTE, resources = [gpiote, speed], schedule = [debounce])]
fn on_gpiote(ctx: on_gpiote::Context) {
ctx.resources.gpiote.reset_events();
ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok();
}

#[task(resources = [btn1, btn2, i2s, speed])]
fn debounce(ctx: debounce::Context) {
if ctx.resources.btn1.is_low().unwrap() {
rprintln!("Go slower");
*ctx.resources.speed += 600_000;
}
if ctx.resources.btn2.is_low().unwrap() {
rprintln!("Go faster");
*ctx.resources.speed -= 600_000;
}
}

extern "C" {
fn SWI0_EGU0();
fn SWI1_EGU1();
}
};

fn triangle_wave(x: i32, length: usize, amplitude: i32, phase: i32, periods: i32) -> i32 {
let length = length as i32;
amplitude
- ((2 * periods * (x + phase + length / (4 * periods)) * amplitude / length)
% (2 * amplitude)
- amplitude)
.abs()
- amplitude / 2
}

#[inline(never)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
cortex_m::interrupt::disable();
rprintln!("{}", info);
loop {
compiler_fence(Ordering::SeqCst);
}
}
20 changes: 20 additions & 0 deletions examples/i2s-peripheral-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "i2s-peripheral-demo"
version = "0.1.0"
authors = ["Henrik Alsér"]
edition = "2018"
publish = false

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.6.2"
cortex-m-rtic = "0.5.3"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
small_morse = "0.1.0"
m = "0.1.1"

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
21 changes: 21 additions & 0 deletions examples/i2s-peripheral-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[default.probe]
protocol = "Swd"

[default.flashing]
enabled = true
halt_afterwards = false
restore_unwritten_bytes = false

[default.general]
chip = "nRF52840"
chip_descriptions = []
log_level = "Warn"

[default.rtt]
enabled = true
channels = []
timeout = 3000
show_timestamps = true

[default.gdb]
enabled = false
Loading