#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use esp32_hal::{
clock::ClockControl,
dma::DmaPriority,
dma_buffers,
embassy::{self},
i2s::{asynch::*, DataFormat, I2s, Standard},
pdma::Dma,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
IO,
};
use esp_backtrace as _;
use esp_println::println;
const SINE: [i16; 64] = [
0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356,
32137, 32609, 32767, 32609, 32137, 31356, 30272, 28897, 27244, 25329, 23169, 20787, 18204,
15446, 12539, 9511, 6392, 3211, 0, -3211, -6392, -9511, -12539, -15446, -18204, -20787, -23169,
-25329, -27244, -28897, -30272, -31356, -32137, -32609, -32767, -32609, -32137, -31356, -30272,
-28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211,
];
#[main]
async fn main(_spawner: Spawner) {
#[cfg(feature = "log")]
esp_println::logger::init_logger_from_env();
println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timer_group0);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let dma = Dma::new(system.dma);
let dma_channel = dma.i2s0channel;
let (tx_buffer, mut tx_descriptors, _, mut rx_descriptors) = dma_buffers!(32000, 0);
let i2s = I2s::new(
peripherals.I2S0,
Standard::Philips,
DataFormat::Data16Channel16,
44100u32.Hz(),
dma_channel.configure(
false,
&mut tx_descriptors,
&mut rx_descriptors,
DmaPriority::Priority0,
),
&clocks,
);
let i2s_tx = i2s
.i2s_tx
.with_bclk(io.pins.gpio12)
.with_ws(io.pins.gpio13)
.with_dout(io.pins.gpio14)
.build();
let data =
unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) };
let buffer = tx_buffer;
let mut idx = 0;
for i in 0..usize::min(data.len(), buffer.len()) {
buffer[i] = data[idx];
idx += 1;
if idx >= data.len() {
idx = 0;
}
}
let mut filler = [0u8; 10000];
let mut idx = 32000 % data.len();
println!("Start");
let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap();
loop {
for i in 0..filler.len() {
filler[i] = data[(idx + i) % data.len()];
}
println!("Next");
let written = transaction.push(&filler).await.unwrap();
idx = (idx + written) % data.len();
println!("written {}", written);
}
}