Only read out partition volume names

This commit is contained in:
Frederik Menke 2024-07-06 13:03:44 +02:00
parent 02b4c12d5d
commit e08e6cc3b2

View file

@ -13,6 +13,7 @@ use embassy_executor::Spawner;
use embassy_rp::spi::Spi; use embassy_rp::spi::Spi;
use embassy_rp::{gpio, spi}; use embassy_rp::{gpio, spi};
use embedded_hal_bus::spi::ExclusiveDevice; use embedded_hal_bus::spi::ExclusiveDevice;
use embedded_sdmmc::fat::Bpb;
use embedded_sdmmc::sdcard::{DummyCsPin, SdCard}; use embedded_sdmmc::sdcard::{DummyCsPin, SdCard};
use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx, FatVolume, VolumeIdx, VolumeType}; use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx, FatVolume, VolumeIdx, VolumeType};
use gpio::{Level, Output}; use gpio::{Level, Output};
@ -60,8 +61,8 @@ async fn main(_spawner: Spawner) {
let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource()); let mut volume_mgr = embedded_sdmmc::VolumeManager::new(sdcard, DummyTimesource());
{ {
let device = volume_mgr.device(); let device = volume_mgr.device();
let info = read_volume_info(device, VolumeIdx(0)).unwrap(); let info = read_volume_label(device, VolumeIdx(0)).unwrap();
info!("Volume 0: name: {:?}", defmt::Debug2Format(&info)); info!("Volume 0: name: {:?}", core::str::from_utf8(&info).unwrap());
} }
// Try and access Volume 0 (i.e. the first partition). // Try and access Volume 0 (i.e. the first partition).
@ -90,6 +91,7 @@ async fn main(_spawner: Spawner) {
} }
} }
{ {
// Append some text to the end of the file
let mut my_file = root_dir let mut my_file = root_dir
.open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadWriteAppend) .open_file_in_dir("MY_FILE.TXT", embedded_sdmmc::Mode::ReadWriteAppend)
.unwrap(); .unwrap();
@ -100,10 +102,14 @@ async fn main(_spawner: Spawner) {
info!("Program end") info!("Program end")
} }
pub fn read_volume_info<D>( /// Read out the volume label a partition
///
/// I mostly pulled out some interiors of `embedded_sdmmc` and plugged
/// them back together.
pub fn read_volume_label<D>(
block_device: &mut D, block_device: &mut D,
volume_idx: VolumeIdx, volume_idx: VolumeIdx,
) -> Result<FatVolume, embedded_sdmmc::Error<D::Error>> ) -> Result<[u8; 11], embedded_sdmmc::Error<D::Error>>
where where
D: BlockDevice, D: BlockDevice,
{ {
@ -173,9 +179,19 @@ where
| PARTITION_ID_FAT32_LBA | PARTITION_ID_FAT32_LBA
| PARTITION_ID_FAT16_LBA | PARTITION_ID_FAT16_LBA
| PARTITION_ID_FAT16 => { | PARTITION_ID_FAT16 => {
let volume = embedded_sdmmc::fat::parse_volume(block_device, lba_start, num_blocks)?; let label = {
let VolumeType::Fat(fat) = volume; let mut blocks = [Block::new()];
Ok(fat) block_device
.read(&mut blocks, lba_start, "read_bpb")
.map_err(Error::DeviceError)?;
let block = &blocks[0];
let bpb = embedded_sdmmc::fat::Bpb::create_from_bytes(block)
.map_err(Error::FormatError)?;
let mut label = [0u8; 11];
label.copy_from_slice(&bpb.volume_label()[0..11]);
label
};
Ok(label)
} }
_ => Err(Error::FormatError("Partition type not supported")), _ => Err(Error::FormatError("Partition type not supported")),
} }