mirror of
				https://github.com/esp-rs/esp-hal.git
				synced 2025-11-03 22:32:57 +00:00 
			
		
		
		
	* Make the `DMA` peripheral API more consistent * Update examples for devices with `PDMA` * Update examples for devices with `GDMA` * Update `CHANGELOG.md` * Update example after rebasing
		
			
				
	
	
		
			100 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			100 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
//! This shows how to continously receive data via I2S
 | 
						|
//!
 | 
						|
//! Pins used
 | 
						|
//! MCLK    GPIO4
 | 
						|
//! BCLK    GPIO1
 | 
						|
//! WS      GPIO2
 | 
						|
//! DIN     GPIO5
 | 
						|
//!
 | 
						|
//! 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 MCLK, BCLK and WS with a logic analyzer
 | 
						|
 | 
						|
#![no_std]
 | 
						|
#![no_main]
 | 
						|
#![feature(type_alias_impl_trait)]
 | 
						|
 | 
						|
use embassy_executor::Spawner;
 | 
						|
use esp32c3_hal::{
 | 
						|
    clock::ClockControl,
 | 
						|
    dma::{Dma, DmaPriority},
 | 
						|
    dma_buffers,
 | 
						|
    embassy,
 | 
						|
    i2s::{asynch::*, DataFormat, I2s, Standard},
 | 
						|
    peripherals::Peripherals,
 | 
						|
    prelude::*,
 | 
						|
    IO,
 | 
						|
};
 | 
						|
use esp_backtrace as _;
 | 
						|
use esp_println::println;
 | 
						|
 | 
						|
#[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();
 | 
						|
 | 
						|
    #[cfg(feature = "embassy-time-systick")]
 | 
						|
    embassy::init(
 | 
						|
        &clocks,
 | 
						|
        esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
 | 
						|
    );
 | 
						|
 | 
						|
    #[cfg(feature = "embassy-time-timg0")]
 | 
						|
    embassy::init(
 | 
						|
        &clocks,
 | 
						|
        esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks),
 | 
						|
    );
 | 
						|
 | 
						|
    let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
 | 
						|
 | 
						|
    let dma = Dma::new(peripherals.DMA);
 | 
						|
    let dma_channel = dma.channel0;
 | 
						|
 | 
						|
    let (_, mut tx_descriptors, rx_buffer, mut rx_descriptors) = dma_buffers!(0, 4092 * 4);
 | 
						|
 | 
						|
    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,
 | 
						|
    )
 | 
						|
    .with_mclk(io.pins.gpio4);
 | 
						|
 | 
						|
    let i2s_rx = i2s
 | 
						|
        .i2s_rx
 | 
						|
        .with_bclk(io.pins.gpio1)
 | 
						|
        .with_ws(io.pins.gpio2)
 | 
						|
        .with_din(io.pins.gpio5)
 | 
						|
        .build();
 | 
						|
 | 
						|
    let buffer = rx_buffer;
 | 
						|
    println!("Start");
 | 
						|
 | 
						|
    let mut data = [0u8; 5000];
 | 
						|
    let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap();
 | 
						|
    loop {
 | 
						|
        let avail = transaction.available().await;
 | 
						|
        println!("available {}", avail);
 | 
						|
 | 
						|
        let count = transaction.pop(&mut data).await.unwrap();
 | 
						|
        println!(
 | 
						|
            "got {} bytes, {:x?}..{:x?}",
 | 
						|
            count,
 | 
						|
            &data[..10],
 | 
						|
            &data[count - 10..count]
 | 
						|
        );
 | 
						|
    }
 | 
						|
}
 |