Skip to content

Commit 4d10647

Browse files
jonathanpallanteldruin
authored andcommitted
Use the DelayUs trait.
1 parent a92e932 commit 4d10647

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//! # struct DummyCsPin;
2222
//! # struct DummyUart;
2323
//! # struct DummyTimeSource;
24+
//! # struct DummyDelayer;
2425
//! # impl embedded_hal::blocking::spi::Transfer<u8> for DummySpi {
2526
//! # type Error = ();
2627
//! # fn transfer<'w>(&mut self, data: &'w mut [u8]) -> Result<&'w [u8], ()> { Ok(&[0]) }
@@ -33,14 +34,18 @@
3334
//! # impl embedded_sdmmc::TimeSource for DummyTimeSource {
3435
//! # fn get_timestamp(&self) -> embedded_sdmmc::Timestamp { embedded_sdmmc::Timestamp::from_fat(0, 0) }
3536
//! # }
37+
//! # impl embedded_hal::blocking::delay::DelayUs<u8> for DummyDelayer {
38+
//! # fn delay_us(&mut self, us: u8) {}
39+
//! # }
3640
//! # impl std::fmt::Write for DummyUart { fn write_str(&mut self, s: &str) -> std::fmt::Result { Ok(()) } }
3741
//! # use std::fmt::Write;
3842
//! # use embedded_sdmmc::VolumeManager;
3943
//! # fn main() -> Result<(), embedded_sdmmc::Error<embedded_sdmmc::SdCardError>> {
4044
//! # let mut sdmmc_spi = DummySpi;
4145
//! # let mut sdmmc_cs = DummyCsPin;
4246
//! # let time_source = DummyTimeSource;
43-
//! let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, sdmmc_cs);
47+
//! # let delayer = DummyDelayer;
48+
//! let sdcard = embedded_sdmmc::SdCard::new(sdmmc_spi, sdmmc_cs, delayer);
4449
//! println!("Card size {} bytes", sdcard.num_bytes()?);
4550
//! let mut volume_mgr = VolumeManager::new(sdcard, time_source);
4651
//! println!("Card size is still {} bytes", volume_mgr.device().num_bytes()?);

src/sdcard/mod.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,42 @@ const DEFAULT_DELAY_COUNT: u32 = 32_000;
3434
/// (which puts the card into SPI mode).
3535
///
3636
/// All the APIs take `&self` - mutability is handled using an inner `RefCell`.
37-
pub struct SdCard<SPI, CS>
37+
pub struct SdCard<SPI, CS, DELAYER>
3838
where
3939
SPI: embedded_hal::blocking::spi::Transfer<u8>,
4040
CS: embedded_hal::digital::v2::OutputPin,
4141
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
42+
DELAYER: embedded_hal::blocking::delay::DelayUs<u8>,
4243
{
43-
inner: RefCell<SdCardInner<SPI, CS>>,
44+
inner: RefCell<SdCardInner<SPI, CS, DELAYER>>,
4445
}
4546

46-
impl<SPI, CS> SdCard<SPI, CS>
47+
impl<SPI, CS, DELAYER> SdCard<SPI, CS, DELAYER>
4748
where
4849
SPI: embedded_hal::blocking::spi::Transfer<u8>,
4950
CS: embedded_hal::digital::v2::OutputPin,
5051
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
52+
DELAYER: embedded_hal::blocking::delay::DelayUs<u8>,
5153
{
5254
/// Create a new SD/MMC Card driver using a raw SPI interface.
5355
///
5456
/// Uses the default options.
55-
pub fn new(spi: SPI, cs: CS) -> SdCard<SPI, CS> {
56-
Self::new_with_options(spi, cs, AcquireOpts::default())
57+
pub fn new(spi: SPI, cs: CS, delayer: DELAYER) -> SdCard<SPI, CS, DELAYER> {
58+
Self::new_with_options(spi, cs, delayer, AcquireOpts::default())
5759
}
5860

5961
/// Construct a new SD/MMC Card driver, using a raw SPI interface and the given options.
60-
pub fn new_with_options(spi: SPI, cs: CS, options: AcquireOpts) -> SdCard<SPI, CS> {
62+
pub fn new_with_options(
63+
spi: SPI,
64+
cs: CS,
65+
delayer: DELAYER,
66+
options: AcquireOpts,
67+
) -> SdCard<SPI, CS, DELAYER> {
6168
SdCard {
6269
inner: RefCell::new(SdCardInner {
6370
spi,
6471
cs,
72+
delayer,
6573
card_type: None,
6674
options,
6775
}),
@@ -118,11 +126,12 @@ where
118126
}
119127
}
120128

121-
impl<SPI, CS> BlockDevice for SdCard<SPI, CS>
129+
impl<SPI, CS, DELAYER> BlockDevice for SdCard<SPI, CS, DELAYER>
122130
where
123131
SPI: embedded_hal::blocking::spi::Transfer<u8>,
124132
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
125133
CS: embedded_hal::digital::v2::OutputPin,
134+
DELAYER: embedded_hal::blocking::delay::DelayUs<u8>,
126135
{
127136
type Error = Error;
128137

@@ -163,23 +172,26 @@ where
163172
/// Represents an SD Card on an SPI bus.
164173
///
165174
/// All the APIs required `&mut self`.
166-
struct SdCardInner<SPI, CS>
175+
struct SdCardInner<SPI, CS, DELAYER>
167176
where
168177
SPI: embedded_hal::blocking::spi::Transfer<u8>,
169178
CS: embedded_hal::digital::v2::OutputPin,
170179
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
180+
DELAYER: embedded_hal::blocking::delay::DelayUs<u8>,
171181
{
172182
spi: SPI,
173183
cs: CS,
184+
delayer: DELAYER,
174185
card_type: Option<CardType>,
175186
options: AcquireOpts,
176187
}
177188

178-
impl<SPI, CS> SdCardInner<SPI, CS>
189+
impl<SPI, CS, DELAYER> SdCardInner<SPI, CS, DELAYER>
179190
where
180191
SPI: embedded_hal::blocking::spi::Transfer<u8>,
181192
CS: embedded_hal::digital::v2::OutputPin,
182193
<SPI as embedded_hal::blocking::spi::Transfer<u8>>::Error: core::fmt::Debug,
194+
DELAYER: embedded_hal::blocking::delay::DelayUs<u8>,
183195
{
184196
/// Read one or more blocks, starting at the given block index.
185197
fn read(&mut self, blocks: &mut [Block], start_block_idx: BlockIdx) -> Result<(), Error> {
@@ -309,7 +321,7 @@ where
309321
if s != 0xFF {
310322
break s;
311323
}
312-
delay.delay(Error::TimeoutReadBuffer)?;
324+
delay.delay(&mut self.delayer, Error::TimeoutReadBuffer)?;
313325
};
314326
if status != DATA_START_BLOCK {
315327
return Err(Error::ReadError);
@@ -411,7 +423,7 @@ where
411423
}
412424
}
413425

414-
delay.delay(Error::TimeoutCommand(CMD0))?;
426+
delay.delay(&mut s.delayer, Error::TimeoutCommand(CMD0))?;
415427
}
416428
if attempts == 0 {
417429
return Err(Error::CardNotFound);
@@ -436,12 +448,12 @@ where
436448
card_type = CardType::SD2;
437449
break 0x4000_0000;
438450
}
439-
delay.delay(Error::TimeoutCommand(CMD8))?;
451+
delay.delay(&mut s.delayer, Error::TimeoutCommand(CMD8))?;
440452
};
441453

442454
let mut delay = Delay::new();
443455
while s.card_acmd(ACMD41, arg)? != R1_READY_STATE {
444-
delay.delay(Error::TimeoutACommand(ACMD41))?;
456+
delay.delay(&mut s.delayer, Error::TimeoutACommand(ACMD41))?;
445457
}
446458

447459
if card_type == CardType::SD2 {
@@ -547,7 +559,7 @@ where
547559
if s == 0xFF {
548560
break;
549561
}
550-
delay.delay(Error::TimeoutWaitNotBusy)?;
562+
delay.delay(&mut self.delayer, Error::TimeoutWaitNotBusy)?;
551563
}
552564
Ok(())
553565
}
@@ -623,22 +635,26 @@ pub enum CardType {
623635
/// sort itself out.
624636
///
625637
/// @TODO replace this!
626-
struct Delay(u32);
638+
struct Delay {
639+
count: u32,
640+
}
627641

628642
impl Delay {
629643
fn new() -> Delay {
630-
Delay(DEFAULT_DELAY_COUNT)
644+
Delay {
645+
count: DEFAULT_DELAY_COUNT,
646+
}
631647
}
632648

633-
fn delay(&mut self, err: Error) -> Result<(), Error> {
634-
if self.0 == 0 {
649+
fn delay<T>(&mut self, delayer: &mut T, err: Error) -> Result<(), Error>
650+
where
651+
T: embedded_hal::blocking::delay::DelayUs<u8>,
652+
{
653+
if self.count == 0 {
635654
Err(err)
636655
} else {
637-
let dummy_var: u32 = 0;
638-
for _ in 0..100 {
639-
unsafe { core::ptr::read_volatile(&dummy_var) };
640-
}
641-
self.0 -= 1;
656+
delayer.delay_us(10);
657+
self.count -= 1;
642658
Ok(())
643659
}
644660
}

0 commit comments

Comments
 (0)