From 5ef267736ba15ac663dc8b349a24ffb7162de32d Mon Sep 17 00:00:00 2001 From: Frederik Menke Date: Sun, 14 Jul 2024 21:00:42 +0200 Subject: [PATCH] Move sd card work to task --- src/main.rs | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index ba39ca8..b9e92f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,8 +10,10 @@ use byteorder::ByteOrder; use defmt::*; use embassy_embedded_hal::SetConfig; use embassy_executor::Spawner; +use embassy_rp::peripherals::{PIN_10, PIN_11, PIN_12, PIN_16, SPI1}; use embassy_rp::spi::Spi; use embassy_rp::{gpio, spi}; +use embassy_time::{Duration, Timer}; use embedded_hal_bus::spi::ExclusiveDevice; use embedded_sdmmc::sdcard::{DummyCsPin, SdCard}; use embedded_sdmmc::{Block, BlockDevice, BlockIdx, VolumeIdx}; @@ -34,20 +36,32 @@ impl embedded_sdmmc::TimeSource for DummyTimesource { } #[embassy_executor::main] -async fn main(_spawner: Spawner) { +async fn main(spawner: Spawner) { embassy_rp::pac::SIO.spinlock(31).write_value(1); let p = embassy_rp::init(Default::default()); + spawner + .spawn(write_to_sd(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, p.PIN_16)) + .unwrap(); + + debug!("TASK ENDED: Main") +} + +#[embassy_executor::task] +async fn write_to_sd(spi1: SPI1, pin10: PIN_10, pin11: PIN_11, pin12: PIN_12, pin16: PIN_16) { + Timer::after(Duration::from_secs(2)).await; + // SPI clock needs to be running at <= 400kHz during initialization let mut config = spi::Config::default(); config.frequency = 400_000; - let spi = Spi::new_blocking(p.SPI1, p.PIN_10, p.PIN_11, p.PIN_12, config); + let spi = Spi::new_blocking(spi1, pin10, pin11, pin12, config); // Use a dummy cs pin here, for embedded-hal SpiDevice compatibility reasons let spi_dev = ExclusiveDevice::new_no_delay(spi, DummyCsPin); // Real cs pin - let cs = Output::new(p.PIN_16, Level::High); + let cs = Output::new(pin16, Level::High); let sdcard = SdCard::new(spi_dev, cs, embassy_time::Delay); + info!("Card size is {} bytes", sdcard.num_bytes().unwrap()); // Now that the card is initialized, the SPI clock can go faster @@ -102,8 +116,7 @@ async fn main(_spawner: Spawner) { my_file.write(b"Another one\n").unwrap(); } - - info!("Program end") + debug!("TASK ENDED: SdCard") } pub fn valid_volumes(block_device: &mut D) -> [Option<(VolumeIdx, [u8; 11])>; 4] @@ -111,12 +124,12 @@ where D: BlockDevice, { let mut res = [None; 4]; - for i in 0..4 { + (0..4).for_each(|i| { let index_and_label = read_volume_label(block_device, VolumeIdx(i)) .ok() .map(|label| (VolumeIdx(i), label)); res[i] = index_and_label; - } + }); res } /// Read out the volume label a partition