Skip to content

Commit a180cc1

Browse files
committed
Use traits for I2S TX & RX
1 parent f7854aa commit a180cc1

File tree

4 files changed

+251
-76
lines changed

4 files changed

+251
-76
lines changed

examples/i2s-controller-demo/src/main.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use small_morse::{encode, State};
1414
use {
1515
core::{
1616
panic::PanicInfo,
17+
pin,
1718
sync::atomic::{compiler_fence, Ordering},
1819
},
1920
hal::{
@@ -32,11 +33,11 @@ use {
3233
#[rtic::app(device = crate::hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
3334
const APP: () = {
3435
struct Resources {
35-
i2s: hal::i2s::I2S,
36-
#[init([0; 32])]
37-
signal_buf: [i16; 32],
38-
#[init([0; 32])]
39-
mute_buf: [i16; 32],
36+
// i2s: hal::i2s::I2S,
37+
// #[init([0; 32])]
38+
signal_buf: pin::Pin<&'static [i16]>,
39+
// #[init([0; 32])]
40+
mute_buf: pin::Pin<&'static [i16]>,
4041
#[init(None)]
4142
queue: Option<Queue<State, U256>>,
4243
producer: Producer<'static, State, U256>,
@@ -49,10 +50,20 @@ const APP: () = {
4950
btn1: Pin<Input<PullUp>>,
5051
btn2: Pin<Input<PullUp>>,
5152
led: Pin<Output<PushPull>>,
53+
transfer: Option<hal::i2s::Transfer<&'static [i16]>>,
5254
}
5355

54-
#[init(resources = [queue, signal_buf, mute_buf], spawn = [tick])]
56+
#[init(resources = [queue], spawn = [tick])]
5557
fn init(mut ctx: init::Context) -> init::LateResources {
58+
static mut MUTE_BUF: [i16; 32] = [0i16; 32];
59+
static mut SIGNAL_BUF: [i16; 32] = [0i16; 32];
60+
// Fill signal buffer with triangle waveform, 2 channels interleaved
61+
let len = SIGNAL_BUF.len() / 2;
62+
for x in 0..len {
63+
SIGNAL_BUF[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
64+
SIGNAL_BUF[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
65+
}
66+
5667
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();
5768
// Enable the monotonic timer (CYCCNT)
5869
ctx.core.DCB.enable_trace();
@@ -75,16 +86,7 @@ const APP: () = {
7586
None,
7687
Some(&sdout_pin),
7788
);
78-
i2s.tx_buffer(&ctx.resources.mute_buf[..]).ok();
79-
i2s.enable().start();
80-
81-
// Fill signal buffer with triangle waveform, 2 channels interleaved
82-
let signal_buf = ctx.resources.signal_buf;
83-
let len = signal_buf.len() / 2;
84-
for x in 0..len {
85-
signal_buf[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
86-
signal_buf[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
87-
}
89+
i2s.start();
8890

8991
// Configure buttons
9092
let btn1 = p0.p0_11.into_pullup_input().degrade();
@@ -117,7 +119,6 @@ const APP: () = {
117119
ctx.spawn.tick().ok();
118120

119121
init::LateResources {
120-
i2s,
121122
producer,
122123
consumer,
123124
gpiote,
@@ -126,6 +127,9 @@ const APP: () = {
126127
led: p0.p0_13.into_push_pull_output(Level::High).degrade(),
127128
uarte,
128129
uarte_timer: Timer::new(ctx.device.TIMER0),
130+
transfer: i2s.tx(pin::Pin::new(&MUTE_BUF[..])).ok(),
131+
signal_buf: pin::Pin::new(&SIGNAL_BUF[..]),
132+
mute_buf: pin::Pin::new(&MUTE_BUF[..]),
129133
}
130134
}
131135

@@ -150,7 +154,7 @@ const APP: () = {
150154
}
151155
}
152156
Err(hal::uarte::Error::Timeout(n)) if n > 0 => {
153-
if let Ok(msg) = core::str::from_utf8(&uarte_rx_buf[0..n]) {
157+
if let Ok(msg) = core::str::from_utf8(&uarte_rx_buf[..n]) {
154158
rprintln!("{}", msg);
155159
for action in encode(msg) {
156160
for _ in 0..action.duration {
@@ -164,18 +168,18 @@ const APP: () = {
164168
}
165169
}
166170

167-
#[task(resources = [consumer, i2s, signal_buf, mute_buf, led, speed], schedule = [tick])]
171+
#[task(resources = [consumer, transfer, signal_buf, mute_buf, led, speed], schedule = [tick])]
168172
fn tick(ctx: tick::Context) {
169-
let i2s = ctx.resources.i2s;
173+
let (_buf, i2s) = ctx.resources.transfer.take().unwrap().wait();
170174
match ctx.resources.consumer.dequeue() {
171175
Some(State::On) => {
172176
// Move TX pointer to signal buffer (sound ON)
173-
i2s.tx_buffer(&ctx.resources.signal_buf[..]).ok();
177+
*ctx.resources.transfer = i2s.tx(*ctx.resources.signal_buf).ok();
174178
ctx.resources.led.set_low().ok();
175179
}
176180
_ => {
177181
// Move TX pointer to silent buffer (sound OFF)
178-
i2s.tx_buffer(&ctx.resources.mute_buf[..]).ok();
182+
*ctx.resources.transfer = i2s.tx(*ctx.resources.mute_buf).ok();
179183
ctx.resources.led.set_high().ok();
180184
}
181185
}
@@ -190,7 +194,7 @@ const APP: () = {
190194
ctx.schedule.debounce(ctx.start + 3_000_000.cycles()).ok();
191195
}
192196

193-
#[task(resources = [btn1, btn2, i2s, speed])]
197+
#[task(resources = [btn1, btn2, speed])]
194198
fn debounce(ctx: debounce::Context) {
195199
if ctx.resources.btn1.is_low().unwrap() {
196200
rprintln!("Go slower");

examples/i2s-peripheral-demo/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ cortex-m = "0.6.2"
1212
cortex-m-rtic = "0.5.3"
1313
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
1414
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }
15-
small_morse = "0.1.0"
16-
m = "0.1.1"
1715

1816
[dependencies.embedded-hal]
1917
version = "0.2.3"

examples/i2s-peripheral-demo/src/main.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#![no_main]
33

44
// I2S `peripheral mode` demo
5-
// RMS level indicator using an RGB LED (APA102 on ItsyBitsy nRF52840)
5+
// Signal average level indicator using an RGB LED (APA102 on ItsyBitsy nRF52840)
66

77
use embedded_hal::blocking::spi::Write;
8-
use m::Float;
98
use {
109
core::{
1110
panic::PanicInfo,
11+
pin,
1212
sync::atomic::{compiler_fence, Ordering},
1313
},
1414
hal::{
@@ -29,14 +29,14 @@ const RED: [u8; 9] = [0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x10, 0xFF];
2929
#[rtic::app(device = crate::hal::pac, peripherals = true)]
3030
const APP: () = {
3131
struct Resources {
32-
i2s: I2S,
33-
#[init([0; 128])]
34-
rx_buf: [i16; 128],
3532
rgb: Spim<SPIM0>,
33+
transfer: Option<hal::i2s::Transfer<&'static mut [i16]>>,
3634
}
3735

38-
#[init(resources = [rx_buf])]
36+
#[init]
3937
fn init(ctx: init::Context) -> init::LateResources {
38+
static mut RX_BUF: [i16; 128] = [0i16; 128];
39+
4040
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();
4141
rtt_init_print!();
4242
rprintln!("Play me some audio...");
@@ -56,10 +56,9 @@ const APP: () = {
5656
Some(&sdin_pin),
5757
None,
5858
);
59-
i2s.enable_interrupt(I2SEvent::RxPtrUpdated)
60-
.rx_buffer(&mut ctx.resources.rx_buf[..])
61-
.ok();
62-
i2s.enable().start();
59+
i2s.enable_interrupt(I2SEvent::RxPtrUpdated).start();
60+
let rx_buf = pin::Pin::new(&mut RX_BUF[..]);
61+
let transfer = i2s.rx(rx_buf).ok();
6362

6463
// Configure APA102 RGB LED control
6564
let p1 = hal::gpio::p1::Parts::new(ctx.device.P1);
@@ -80,28 +79,26 @@ const APP: () = {
8079
},
8180
0,
8281
);
83-
84-
init::LateResources { i2s, rgb }
82+
init::LateResources { rgb, transfer }
8583
}
8684

87-
#[task(binds = I2S, resources = [i2s, rx_buf, rgb])]
85+
#[task(binds = I2S, resources = [rgb, transfer])]
8886
fn on_i2s(ctx: on_i2s::Context) {
89-
let on_i2s::Resources { i2s, rx_buf, rgb } = ctx.resources;
87+
let (rx_buf, i2s) = ctx.resources.transfer.take().unwrap().wait();
9088
if i2s.is_event_triggered(I2SEvent::RxPtrUpdated) {
9189
i2s.reset_event(I2SEvent::RxPtrUpdated);
92-
// Calculate mono summed RMS of received buffer
93-
let rms = Float::sqrt(
94-
(rx_buf.iter().map(|x| *x as i32).map(|x| x * x).sum::<i32>() / rx_buf.len() as i32)
95-
as f32,
96-
) as u16;
97-
let color = match rms {
90+
// Calculate mono summed average of received buffer
91+
let avg = (rx_buf.iter().map(|x| (*x).abs() as u32).sum::<u32>() / rx_buf.len() as u32)
92+
as u16;
93+
let color = match avg {
9894
0..=4 => &OFF,
9995
5..=10_337 => &GREEN,
10096
10_338..=16_383 => &ORANGE,
10197
_ => &RED,
10298
};
103-
<Spim<SPIM0> as Write<u8>>::write(rgb, color).ok();
99+
<Spim<SPIM0> as Write<u8>>::write(ctx.resources.rgb, color).ok();
104100
}
101+
*ctx.resources.transfer = i2s.rx(rx_buf).ok();
105102
}
106103
};
107104

0 commit comments

Comments
 (0)