aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThales Fragoso <[email protected]>2021-05-12 21:59:48 -0300
committerThales Fragoso <[email protected]>2021-05-14 23:43:09 -0300
commit359aaa5aeb83cc073c9a1ebc41318e79e00c1669 (patch)
tree6fc81bef76db3d201474057208f3b95c10ee136e
parenta130499c9a1b999ac267c2107205898e2002534b (diff)
Implement embedded-sdmmc traits
-rw-r--r--embassy-stm32/Cargo.toml5
-rw-r--r--embassy-stm32/gen.py1
-rw-r--r--embassy-stm32/src/sdmmc_v2.rs63
3 files changed, 68 insertions, 1 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index c2e6c7a5f..4735a34f0 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -17,7 +17,8 @@ cortex-m = "0.7.1"
17embedded-hal = { version = "0.2.4" } 17embedded-hal = { version = "0.2.4" }
18futures = { version = "0.3.5", default-features = false, features = ["async-await"] } 18futures = { version = "0.3.5", default-features = false, features = ["async-await"] }
19rand_core = { version = "0.6.2", optional = true } 19rand_core = { version = "0.6.2", optional = true }
20sdio-host = "0.5.0" 20sdio-host = { version = "0.5.0", optional = true }
21embedded-sdmmc = { git = "https://github.com/thalesfragoso/embedded-sdmmc-rs", branch = "async", optional = true }
21 22
22[build-dependencies] 23[build-dependencies]
23regex = "1.4.6" 24regex = "1.4.6"
@@ -333,6 +334,8 @@ _rng_v1 = []
333_spi = [] 334_spi = []
334_spi_v1 = [] 335_spi_v1 = []
335_spi_v2 = [] 336_spi_v2 = []
337_sdmmc = [ "sdio-host",]
338_sdmmc_v2 = []
336_stm32f4 = [] 339_stm32f4 = []
337_stm32l4 = [] 340_stm32l4 = []
338_stm32l4p = [] 341_stm32l4p = []
diff --git a/embassy-stm32/gen.py b/embassy-stm32/gen.py
index 23a4c7eb8..96275df9a 100644
--- a/embassy-stm32/gen.py
+++ b/embassy-stm32/gen.py
@@ -248,6 +248,7 @@ for chip in chips.values():
248 248
249feature_optional_deps = {} 249feature_optional_deps = {}
250feature_optional_deps['_rng'] = ['rand_core'] 250feature_optional_deps['_rng'] = ['rand_core']
251feature_optional_deps['_sdmmc'] = ['sdio-host']
251 252
252features = {} 253features = {}
253extra_features = set() 254extra_features = set()
diff --git a/embassy-stm32/src/sdmmc_v2.rs b/embassy-stm32/src/sdmmc_v2.rs
index 182add6f6..ba849008d 100644
--- a/embassy-stm32/src/sdmmc_v2.rs
+++ b/embassy-stm32/src/sdmmc_v2.rs
@@ -1,9 +1,11 @@
1use core::future::Future;
1use core::marker::PhantomData; 2use core::marker::PhantomData;
2use core::task::Poll; 3use core::task::Poll;
3 4
4use embassy::interrupt::InterruptExt; 5use embassy::interrupt::InterruptExt;
5use embassy::util::{AtomicWaker, OnDrop, Unborrow}; 6use embassy::util::{AtomicWaker, OnDrop, Unborrow};
6use embassy_extras::unborrow; 7use embassy_extras::unborrow;
8use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx};
7use futures::future::poll_fn; 9use futures::future::poll_fn;
8use sdio_host::{BusWidth, CardCapacity, CardStatus, CurrentState, SDStatus, CID, CSD, OCR, SCR}; 10use sdio_host::{BusWidth, CardCapacity, CardStatus, CurrentState, SDStatus, CID, CSD, OCR, SCR};
9 11
@@ -1356,3 +1358,64 @@ macro_rules! impl_sdmmc_pin {
1356 impl crate::sdmmc_v2::$func<peripherals::$inst> for peripherals::$pin {} 1358 impl crate::sdmmc_v2::$func<peripherals::$inst> for peripherals::$pin {}
1357 }; 1359 };
1358} 1360}
1361
1362impl<'d, T: Instance, P: Pins<T>> BlockDevice for Sdmmc<'d, T, P> {
1363 type Error = Error;
1364 #[rustfmt::skip]
1365 type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
1366 #[rustfmt::skip]
1367 type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
1368
1369 fn read<'a>(
1370 &'a mut self,
1371 blocks: &'a mut [Block],
1372 start_block_idx: BlockIdx,
1373 _reason: &str,
1374 ) -> Self::ReadFuture<'a> {
1375 async move {
1376 let card_capacity = self.card()?.card_type;
1377 let inner = T::inner();
1378 let state = T::state();
1379 let mut address = start_block_idx.0;
1380
1381 for block in blocks.iter_mut() {
1382 let block: &mut [u8; 512] = &mut block.contents;
1383
1384 // NOTE(unsafe) Block uses align(4)
1385 let buf = unsafe { &mut *(block as *mut [u8; 512] as *mut [u32; 128]) };
1386 inner.read_block(address, buf, card_capacity, state).await?;
1387 address += 1;
1388 }
1389 Ok(())
1390 }
1391 }
1392
1393 fn write<'a>(
1394 &'a mut self,
1395 blocks: &'a [Block],
1396 start_block_idx: BlockIdx,
1397 ) -> Self::WriteFuture<'a> {
1398 async move {
1399 let card = self.card.as_mut().ok_or(Error::NoCard)?;
1400 let inner = T::inner();
1401 let state = T::state();
1402 let mut address = start_block_idx.0;
1403
1404 for block in blocks.iter() {
1405 let block: &[u8; 512] = &block.contents;
1406
1407 // NOTE(unsafe) DataBlock uses align 4
1408 let buf = unsafe { &*(block as *const [u8; 512] as *const [u32; 128]) };
1409 inner.write_block(address, buf, card, state).await?;
1410 address += 1;
1411 }
1412 Ok(())
1413 }
1414 }
1415
1416 fn num_blocks(&self) -> Result<BlockCount, Self::Error> {
1417 let card = self.card()?;
1418 let count = card.csd.block_count();
1419 Ok(BlockCount(count))
1420 }
1421}