Draft: Initial support for I2S with a working example.

Co-authored-by: @brainstorm <brainstorm@nopcode.org>
This commit is contained in:
Christian Perez Llamas
2022-11-09 19:14:43 +01:00
parent 059610a8de
commit cecd77938c
6 changed files with 460 additions and 1 deletions

View File

@ -14,7 +14,7 @@ embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] }
embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] }
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] }
embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "i2s", "unstable-pac"] }
embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true }
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true }
embedded-io = "0.3.1"

View File

@ -0,0 +1,48 @@
// Example inspired by RTIC's I2S demo: https://github.com/nrf-rs/nrf-hal/blob/master/examples/i2s-controller-demo/src/main.rs
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_nrf::{i2s};
use {defmt_rtt as _, panic_probe as _};
#[repr(align(4))]
pub struct Aligned<T: ?Sized>(T);
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let config = i2s::Config::default();
let mut i2s = i2s::I2s::new(p.I2S, p.P0_28, p.P0_29, p.P0_31, p.P0_11, p.P0_30, config);
let mut signal_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]);
let len = signal_buf.0.len() / 2;
for x in 0..len {
signal_buf.0[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
signal_buf.0[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16;
}
let ptr = &signal_buf.0 as *const i16 as *const u8;
let len = signal_buf.0.len() * core::mem::size_of::<i16>();
i2s.start();
i2s.set_tx_enabled(true);
loop {
i2s.tx(ptr, len).await;
}
}
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
}