From c15509282eeb620b6ce70059ea10c4657cff7474 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Tue, 24 Sep 2024 00:13:11 -0400 Subject: pio i2s --- src/main.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 7 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 37b413c..6cf46be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,11 @@ #![no_main] #![feature(impl_trait_in_assoc_type)] +use crate::usb::COBS_DOWNLINK; use molybdos::{ embassy_embedded_hal::shared_bus::asynch::spi::SpiDevice, + embassy_executor, + embassy_rp, embassy_rp::{ i2c::{ self, @@ -11,6 +14,8 @@ use molybdos::{ }, peripherals::{ I2C0, + I2C1, + PIO0, SPI0, SPI1, }, @@ -20,8 +25,17 @@ use molybdos::{ }, }, embassy_sync::{ - blocking_mutex::raw::CriticalSectionRawMutex, + blocking_mutex::raw::{ + CriticalSectionRawMutex, + ThreadModeRawMutex, + }, + pipe::Pipe, pubsub, + pubsub::PubSubBehavior, + }, + embedded_hal_async::spi::{ + Operation, + SpiDevice as _, }, embedded_sdmmc_async::{ VolumeIdx, @@ -29,8 +43,10 @@ use molybdos::{ }, genlog, genlog::defmt, + heapless, pal::StaticOutput, }; +use ocularium::Downlink; mod bringup; mod usb; @@ -40,13 +56,15 @@ pub type BMESpiDev = SpiDevice<'static, CriticalSectionRawMutex, BMESpi, StaticO pub type SdSpi = Spi<'static, SPI0, spi::Async>; pub type SdSpiDev = SpiDevice<'static, CriticalSectionRawMutex, SdSpi, StaticOutput>; -pub type SensorI2c = I2c<'static, I2C0, i2c::Async>; +pub type SensorI2c = I2c<'static, I2C1, i2c::Async>; + +pub static I2S_PIPE: Pipe = Pipe::new(); -#[embassy_executor::main] +#[::embassy_executor::main] async fn main(spawner: embassy_executor::Spawner) { molybdos::pal::heap::init(); - genlog::info!("boot"); + defmt::info!("boot"); let bringup::Split { sd_spi, @@ -55,9 +73,13 @@ async fn main(spawner: embassy_executor::Spawner) { bme_cs, wdt, usb, + i2s, + i2s_dma, .. } = bringup::split(Default::default()); + defmt::info!("split ok"); + // spawner.must_spawn(molybdos::pal::watchdog(wdt)); let (acm,) = molybdos::lib::usb::bringup!( @@ -81,18 +103,63 @@ async fn main(spawner: embassy_executor::Spawner) { &usb::COBS_DOWNLINK ); + let mut spidev = SpiDevice::new(bme_spi, bme_cs); + + let data = &mut [0u8]; + + let result = spidev + .transaction(&mut [Operation::Write(&[0x50 | (1 << 7)]), Operation::Read(data)]) + .await; + if let Err(e) = result { + defmt::error!("reading spi dev: {}", defmt::Debug2Format(&e)); + } else { + defmt::info!("read chipid: {:x}", data[0]); + } + #[cfg(not(feature = "disable_sd"))] { let vm = molybdos::rt::sd::bringup!(sd_spi, SdSpi, sd_cs); { - let vol = VolumeManager::open_volume(vm, VolumeIdx(0)).await.expect("opening volume"); - } + let vol = match VolumeManager::open_volume(vm, VolumeIdx(0)).await { + Ok(x) => x, + Err(e) => { + defmt::error!("opening volume: {}", defmt::Debug2Format(&e)); - // spawner.must_spawn(sd::run_sd(vm, pubsub::LED.sender().into())); + loop { + molybdos::embassy_time::Timer::after( + molybdos::embassy_time::Duration::from_secs(1), + ) + .await; + } + }, + }; + } } + spawner.must_spawn(drain_pipe()); + spawner.must_spawn(run_i2s(i2s, i2s_dma)); + loop { molybdos::embassy_time::Timer::after(molybdos::embassy_time::Duration::from_secs(1)).await; } } + +#[::embassy_executor::task] +async fn run_i2s(mut i2s: bringup::StaticI2S, dma: embassy_rp::peripherals::DMA_CH5) { + i2s.run::<960>(dma, &I2S_PIPE).await +} + +#[::embassy_executor::task] +async fn drain_pipe() { + let publ = COBS_DOWNLINK.publisher().expect("getting publisher"); + + loop { + let buf = &mut [0u8; 960]; + let n = I2S_PIPE.read(buf).await; + + for elem in (&buf[..n]).chunks(48) { + publ.publish_immediate(Downlink::PDM(heapless::Vec::from_slice(elem).unwrap())); + } + } +} -- cgit v1.3.1