diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/nrf/src/bin/i2s.rs | 48 |
2 files changed, 49 insertions, 1 deletions
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index c633f82f5..a79044e8e 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -14,7 +14,7 @@ embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | |||
| 14 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } | 14 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } |
| 15 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 15 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } | 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "i2s", "unstable-pac"] } |
| 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } | 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } |
| 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } | 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } |
| 20 | embedded-io = "0.3.1" | 20 | embedded-io = "0.3.1" |
diff --git a/examples/nrf/src/bin/i2s.rs b/examples/nrf/src/bin/i2s.rs new file mode 100644 index 000000000..556f6b2e2 --- /dev/null +++ b/examples/nrf/src/bin/i2s.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | // Example inspired by RTIC's I2S demo: https://github.com/nrf-rs/nrf-hal/blob/master/examples/i2s-controller-demo/src/main.rs | ||
| 2 | |||
| 3 | #![no_std] | ||
| 4 | #![no_main] | ||
| 5 | #![feature(type_alias_impl_trait)] | ||
| 6 | |||
| 7 | use defmt::*; | ||
| 8 | use embassy_executor::Spawner; | ||
| 9 | use embassy_nrf::{i2s}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | #[repr(align(4))] | ||
| 13 | pub struct Aligned<T: ?Sized>(T); | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async fn main(_spawner: Spawner) { | ||
| 17 | let p = embassy_nrf::init(Default::default()); | ||
| 18 | let config = i2s::Config::default(); | ||
| 19 | |||
| 20 | let mut i2s = i2s::I2s::new(p.I2S, p.P0_28, p.P0_29, p.P0_31, p.P0_11, p.P0_30, config); | ||
| 21 | |||
| 22 | let mut signal_buf: Aligned<[i16; 32]> = Aligned([0i16; 32]); | ||
| 23 | let len = signal_buf.0.len() / 2; | ||
| 24 | for x in 0..len { | ||
| 25 | signal_buf.0[2 * x] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; | ||
| 26 | signal_buf.0[2 * x + 1] = triangle_wave(x as i32, len, 2048, 0, 1) as i16; | ||
| 27 | } | ||
| 28 | |||
| 29 | let ptr = &signal_buf.0 as *const i16 as *const u8; | ||
| 30 | let len = signal_buf.0.len() * core::mem::size_of::<i16>(); | ||
| 31 | |||
| 32 | i2s.start(); | ||
| 33 | i2s.set_tx_enabled(true); | ||
| 34 | |||
| 35 | loop { | ||
| 36 | i2s.tx(ptr, len).await; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | fn triangle_wave(x: i32, length: usize, amplitude: i32, phase: i32, periods: i32) -> i32 { | ||
| 41 | let length = length as i32; | ||
| 42 | amplitude | ||
| 43 | - ((2 * periods * (x + phase + length / (4 * periods)) * amplitude / length) | ||
| 44 | % (2 * amplitude) | ||
| 45 | - amplitude) | ||
| 46 | .abs() | ||
| 47 | - amplitude / 2 | ||
| 48 | } | ||
