Add block-device-driver impl for use with embedded-fatfs (#2607)

This commit is contained in:
Scott Mabin 2024-08-26 19:45:57 +01:00 committed by GitHub
parent 9347571fea
commit f0a8607051
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 0 deletions

View File

@ -88,6 +88,8 @@ static_assertions = { version = "1.1" }
volatile-register = { version = "0.2.1" } volatile-register = { version = "0.2.1" }
bitflags = "2.4.2" bitflags = "2.4.2"
block-device-driver = { version = "0.2" }
aligned = "0.4.1"
[dev-dependencies] [dev-dependencies]
critical-section = { version = "1.1", features = ["std"] } critical-section = { version = "1.1", features = ["std"] }

View File

@ -1511,3 +1511,44 @@ foreach_peripheral!(
} }
}; };
); );
impl<'d, T: Instance, Dma: SdmmcDma<T> + 'd> block_device_driver::BlockDevice<512> for Sdmmc<'d, T, Dma> {
type Error = Error;
type Align = aligned::A4;
async fn read(
&mut self,
mut block_address: u32,
buf: &mut [aligned::Aligned<Self::Align, [u8; 512]>],
) -> Result<(), Self::Error> {
// FIXME/TODO because of missing read_blocks multiple we have to do this one block at a time
for block in buf.iter_mut() {
// safety aligned by block device
let block = unsafe { &mut *(block as *mut _ as *mut crate::sdmmc::DataBlock) };
self.read_block(block_address, block).await?;
block_address += 1;
}
Ok(())
}
async fn write(
&mut self,
mut block_address: u32,
buf: &[aligned::Aligned<Self::Align, [u8; 512]>],
) -> Result<(), Self::Error> {
// FIXME/TODO because of missing read_blocks multiple we have to do this one block at a time
for block in buf.iter() {
// safety aligned by block device
let block = unsafe { &*(block as *const _ as *const crate::sdmmc::DataBlock) };
self.write_block(block_address, block).await?;
block_address += 1;
}
Ok(())
}
async fn size(&mut self) -> Result<u64, Self::Error> {
Ok(self.card()?.size())
}
}