* Unify the system peripheral Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently use them all in the same way. This PR unifies the peripheral name in the hal to `SYSTEM`. The idea is that they all do the same sort of thing, so we can collect them under the same name, and later down the line we can being to expose differences under an extended API. The benifits to this are imo quite big, the examples now are all identical, which makes things easier for esp-wifi, and paves a path towards the multichip hal. Why not do this in the PAC? Imo the pac should be as close to the hardware as possible, and the HAL is where we should abstractions such as this. * changelog
123 lines
3.2 KiB
Rust
123 lines
3.2 KiB
Rust
//! This shows how to continously receive data via I2S
|
|
//!
|
|
//! Pins used
|
|
//! BCLK GPIO12
|
|
//! WS GPIO13
|
|
//! DIN GPIO14
|
|
//!
|
|
//! Without an additional I2S source device you can connect 3V3 or GND to DIN to
|
|
//! read 0 or 0xFF or connect DIN to WS to read two different values
|
|
//!
|
|
//! You can also inspect the BCLK and WS with a logic analyzer
|
|
|
|
#![no_std]
|
|
#![no_main]
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
use esp32_hal::{
|
|
clock::ClockControl,
|
|
dma::DmaPriority,
|
|
embassy::{self, executor::Executor},
|
|
gpio::GpioPin,
|
|
i2s,
|
|
i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, NoMclk, PinsBclkWsDin, Standard},
|
|
pdma::Dma,
|
|
peripherals::Peripherals,
|
|
prelude::*,
|
|
timer::TimerGroup,
|
|
IO,
|
|
};
|
|
use esp_backtrace as _;
|
|
use static_cell::make_static;
|
|
|
|
#[embassy_executor::task]
|
|
async fn i2s_task(
|
|
i2s_rx: I2sRx<
|
|
'static,
|
|
i2s::I2sPeripheral0,
|
|
PinsBclkWsDin<
|
|
'static,
|
|
GpioPin<esp32_hal::gpio::Unknown, 12>,
|
|
GpioPin<esp32_hal::gpio::Unknown, 13>,
|
|
GpioPin<esp32_hal::gpio::Unknown, 14>,
|
|
>,
|
|
esp32_hal::pdma::I2s0DmaChannel,
|
|
>,
|
|
) {
|
|
let buffer = dma_buffer();
|
|
esp_println::println!("Start");
|
|
|
|
let mut data = [0u8; 5000];
|
|
let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap();
|
|
loop {
|
|
let avail = transaction.available().await;
|
|
esp_println::println!("available {}", avail);
|
|
|
|
let count = transaction.pop(&mut data).await.unwrap();
|
|
esp_println::println!(
|
|
"got {} bytes, {:x?}..{:x?}",
|
|
count,
|
|
&data[..10],
|
|
&data[count - 10..count]
|
|
);
|
|
}
|
|
}
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
esp_println::logger::init_logger_from_env();
|
|
esp_println::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.timer0);
|
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
let dma = Dma::new(system.dma);
|
|
let dma_channel = dma.i2s0channel;
|
|
|
|
let tx_descriptors = make_static!([0u32; 20 * 3]);
|
|
let rx_descriptors = make_static!([0u32; 8 * 3]);
|
|
|
|
let i2s = I2s::new(
|
|
peripherals.I2S0,
|
|
NoMclk {},
|
|
Standard::Philips,
|
|
DataFormat::Data16Channel16,
|
|
44100u32.Hz(),
|
|
dma_channel.configure(
|
|
false,
|
|
tx_descriptors,
|
|
rx_descriptors,
|
|
DmaPriority::Priority0,
|
|
),
|
|
&clocks,
|
|
);
|
|
|
|
let i2s_tx = i2s.i2s_rx.with_pins(PinsBclkWsDin::new(
|
|
io.pins.gpio12,
|
|
io.pins.gpio13,
|
|
io.pins.gpio14,
|
|
));
|
|
|
|
// you need to manually enable the DMA channel's interrupt!
|
|
esp32_hal::interrupt::enable(
|
|
esp32_hal::peripherals::Interrupt::I2S0,
|
|
esp32_hal::interrupt::Priority::Priority1,
|
|
)
|
|
.unwrap();
|
|
|
|
let executor = make_static!(Executor::new());
|
|
executor.run(|spawner| {
|
|
spawner.spawn(i2s_task(i2s_tx)).ok();
|
|
});
|
|
}
|
|
|
|
fn dma_buffer() -> &'static mut [u8; 4092 * 4] {
|
|
static mut BUFFER: [u8; 4092 * 4] = [0u8; 4092 * 4];
|
|
unsafe { &mut BUFFER }
|
|
}
|