Skip to content

Commit 421e68c

Browse files
committed
User-selectable number of retries for card acquisition
1 parent 33642eb commit 421e68c

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/sdcard/mod.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ where
401401
// Assert CS
402402
s.cs_low()?;
403403
// Enter SPI mode.
404-
let mut delay = Delay::new_command();
404+
let mut delay = Delay::new(s.options.acquire_retries);
405405
for attempts in 1.. {
406406
trace!("Enter SPI mode, attempt: {}..", attempts);
407407
match s.card_command(CMD0, 0) {
@@ -586,11 +586,18 @@ pub struct AcquireOpts {
586586
/// On by default because without it you might get silent data corruption on
587587
/// your card.
588588
pub use_crc: bool,
589+
590+
/// Sets the number of times we will retry to acquire the card before giving up and returning
591+
/// `Err(Error::CardNotFound)`. By default, card acquisition will be retried 50 times.
592+
pub acquire_retries: u32,
589593
}
590594

591595
impl Default for AcquireOpts {
592596
fn default() -> Self {
593-
AcquireOpts { use_crc: true }
597+
AcquireOpts {
598+
use_crc: true,
599+
acquire_retries: 50,
600+
}
594601
}
595602
}
596603

@@ -708,12 +715,13 @@ impl Delay {
708715
T: embedded_hal::blocking::delay::DelayUs<u8>,
709716
{
710717
if self.retries_left == 0 {
711-
Err(err)
718+
return Err(err);
712719
} else {
713720
delayer.delay_us(10);
714721
self.retries_left -= 1;
715-
Ok(())
716722
}
723+
724+
Ok(())
717725
}
718726
}
719727

0 commit comments

Comments
 (0)