diff options
| author | Thales Fragoso <[email protected]> | 2021-05-12 21:59:48 -0300 |
|---|---|---|
| committer | Thales Fragoso <[email protected]> | 2021-05-14 23:43:09 -0300 |
| commit | 359aaa5aeb83cc073c9a1ebc41318e79e00c1669 (patch) | |
| tree | 6fc81bef76db3d201474057208f3b95c10ee136e | |
| parent | a130499c9a1b999ac267c2107205898e2002534b (diff) | |
Implement embedded-sdmmc traits
| -rw-r--r-- | embassy-stm32/Cargo.toml | 5 | ||||
| -rw-r--r-- | embassy-stm32/gen.py | 1 | ||||
| -rw-r--r-- | embassy-stm32/src/sdmmc_v2.rs | 63 |
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" | |||
| 17 | embedded-hal = { version = "0.2.4" } | 17 | embedded-hal = { version = "0.2.4" } |
| 18 | futures = { version = "0.3.5", default-features = false, features = ["async-await"] } | 18 | futures = { version = "0.3.5", default-features = false, features = ["async-await"] } |
| 19 | rand_core = { version = "0.6.2", optional = true } | 19 | rand_core = { version = "0.6.2", optional = true } |
| 20 | sdio-host = "0.5.0" | 20 | sdio-host = { version = "0.5.0", optional = true } |
| 21 | embedded-sdmmc = { git = "https://github.com/thalesfragoso/embedded-sdmmc-rs", branch = "async", optional = true } | ||
| 21 | 22 | ||
| 22 | [build-dependencies] | 23 | [build-dependencies] |
| 23 | regex = "1.4.6" | 24 | regex = "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 | ||
| 249 | feature_optional_deps = {} | 249 | feature_optional_deps = {} |
| 250 | feature_optional_deps['_rng'] = ['rand_core'] | 250 | feature_optional_deps['_rng'] = ['rand_core'] |
| 251 | feature_optional_deps['_sdmmc'] = ['sdio-host'] | ||
| 251 | 252 | ||
| 252 | features = {} | 253 | features = {} |
| 253 | extra_features = set() | 254 | extra_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 @@ | |||
| 1 | use core::future::Future; | ||
| 1 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 2 | use core::task::Poll; | 3 | use core::task::Poll; |
| 3 | 4 | ||
| 4 | use embassy::interrupt::InterruptExt; | 5 | use embassy::interrupt::InterruptExt; |
| 5 | use embassy::util::{AtomicWaker, OnDrop, Unborrow}; | 6 | use embassy::util::{AtomicWaker, OnDrop, Unborrow}; |
| 6 | use embassy_extras::unborrow; | 7 | use embassy_extras::unborrow; |
| 8 | use embedded_sdmmc::{Block, BlockCount, BlockDevice, BlockIdx}; | ||
| 7 | use futures::future::poll_fn; | 9 | use futures::future::poll_fn; |
| 8 | use sdio_host::{BusWidth, CardCapacity, CardStatus, CurrentState, SDStatus, CID, CSD, OCR, SCR}; | 10 | use 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 | |||
| 1362 | impl<'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 | } | ||
