@@ -229,7 +229,7 @@ where
229
229
// Start a single-block write
230
230
s. card_command ( CMD24 , start_idx) ?;
231
231
s. write_data ( DATA_START_BLOCK , & blocks[ 0 ] . contents ) ?;
232
- s. wait_not_busy ( ) ?;
232
+ s. wait_not_busy ( Delay :: new_write ( ) ) ?;
233
233
if s. card_command ( CMD13 , 0 ) ? != 0x00 {
234
234
return Err ( Error :: WriteError ) ;
235
235
}
@@ -240,11 +240,11 @@ where
240
240
// Start a multi-block write
241
241
s. card_command ( CMD25 , start_idx) ?;
242
242
for block in blocks. iter ( ) {
243
- s. wait_not_busy ( ) ?;
243
+ s. wait_not_busy ( Delay :: new_write ( ) ) ?;
244
244
s. write_data ( WRITE_MULTIPLE_TOKEN , & block. contents ) ?;
245
245
}
246
246
// Stop the write
247
- s. wait_not_busy ( ) ?;
247
+ s. wait_not_busy ( Delay :: new_write ( ) ) ?;
248
248
s. write_byte ( STOP_TRAN_TOKEN ) ?;
249
249
}
250
250
Ok ( ( ) )
@@ -315,7 +315,7 @@ where
315
315
/// sure it's the right size.
316
316
fn read_data ( & mut self , buffer : & mut [ u8 ] ) -> Result < ( ) , Error > {
317
317
// Get first non-FF byte.
318
- let mut delay = Delay :: default ( ) ;
318
+ let mut delay = Delay :: new_read ( ) ;
319
319
let status = loop {
320
320
let s = self . read_byte ( ) ?;
321
321
if s != 0xFF {
@@ -401,7 +401,7 @@ where
401
401
// Assert CS
402
402
s. cs_low ( ) ?;
403
403
// Enter SPI mode.
404
- let mut delay = Delay :: default ( ) ;
404
+ let mut delay = Delay :: new_command ( ) ;
405
405
for attempts in 1 .. {
406
406
trace ! ( "Enter SPI mode, attempt: {}.." , attempts) ;
407
407
match s. card_command ( CMD0 , 0 ) {
@@ -436,7 +436,7 @@ where
436
436
return Err ( Error :: CantEnableCRC ) ;
437
437
}
438
438
// Check card version
439
- let mut delay = Delay :: default ( ) ;
439
+ let mut delay = Delay :: new_command ( ) ;
440
440
let arg = loop {
441
441
if s. card_command ( CMD8 , 0x1AA ) ? == ( R1_ILLEGAL_COMMAND | R1_IDLE_STATE ) {
442
442
card_type = CardType :: SD1 ;
@@ -452,7 +452,7 @@ where
452
452
delay. delay ( & mut s. delayer , Error :: TimeoutCommand ( CMD8 ) ) ?;
453
453
} ;
454
454
455
- let mut delay = Delay :: default ( ) ;
455
+ let mut delay = Delay :: new_command ( ) ;
456
456
while s. card_acmd ( ACMD41 , arg) ? != R1_READY_STATE {
457
457
delay. delay ( & mut s. delayer , Error :: TimeoutACommand ( ACMD41 ) ) ?;
458
458
}
@@ -499,7 +499,7 @@ where
499
499
/// Perform a command.
500
500
fn card_command ( & mut self , command : u8 , arg : u32 ) -> Result < u8 , Error > {
501
501
if command != CMD0 && command != CMD12 {
502
- self . wait_not_busy ( ) ?;
502
+ self . wait_not_busy ( Delay :: new_command ( ) ) ?;
503
503
}
504
504
505
505
let mut buf = [
@@ -519,7 +519,7 @@ where
519
519
let _result = self . read_byte ( ) ?;
520
520
}
521
521
522
- let mut delay = Delay :: default ( ) ;
522
+ let mut delay = Delay :: new_command ( ) ;
523
523
loop {
524
524
let result = self . read_byte ( ) ?;
525
525
if ( result & 0x80 ) == ERROR_OK {
@@ -562,8 +562,7 @@ where
562
562
563
563
/// Spin until the card returns 0xFF, or we spin too many times and
564
564
/// timeout.
565
- fn wait_not_busy ( & mut self ) -> Result < ( ) , Error > {
566
- let mut delay = Delay :: default ( ) ;
565
+ fn wait_not_busy ( & mut self , mut delay : Delay ) -> Result < ( ) , Error > {
567
566
loop {
568
567
let s = self . read_byte ( ) ?;
569
568
if s == 0xFF {
@@ -656,10 +655,26 @@ struct Delay {
656
655
}
657
656
658
657
impl Delay {
658
+ /// The default number of retries for a read operation.
659
+ ///
660
+ /// At ~10us each this is ~100ms.
661
+ ///
662
+ /// See `Part1_Physical_Layer_Simplified_Specification_Ver9.00-1.pdf` Section 4.6.2.1
663
+ pub const DEFAULT_READ_RETRIES : u32 = 10_000 ;
664
+
659
665
/// The default number of retries for a write operation.
660
666
///
661
- /// At 10us each this is 320ms.
662
- pub const DEFAULT_WRITE_RETRIES : u32 = 32000 ;
667
+ /// At ~10us each this is ~500ms.
668
+ ///
669
+ /// See `Part1_Physical_Layer_Simplified_Specification_Ver9.00-1.pdf` Section 4.6.2.2
670
+ pub const DEFAULT_WRITE_RETRIES : u32 = 50_000 ;
671
+
672
+ /// The default number of retries for a control command.
673
+ ///
674
+ /// At ~10us each this is ~100ms.
675
+ ///
676
+ /// No value is given in the specification, so we pick the same as the read timeout.
677
+ pub const DEFAULT_COMMAND_RETRIES : u32 = 10_000 ;
663
678
664
679
/// Create a new Delay object with the given maximum number of retries.
665
680
fn new ( max_retries : u32 ) -> Delay {
@@ -668,6 +683,21 @@ impl Delay {
668
683
}
669
684
}
670
685
686
+ /// Create a new Delay object with the maximum number of retries for a read operation.
687
+ fn new_read ( ) -> Delay {
688
+ Delay :: new ( Self :: DEFAULT_READ_RETRIES )
689
+ }
690
+
691
+ /// Create a new Delay object with the maximum number of retries for a write operation.
692
+ fn new_write ( ) -> Delay {
693
+ Delay :: new ( Self :: DEFAULT_WRITE_RETRIES )
694
+ }
695
+
696
+ /// Create a new Delay object with the maximum number of retries for a command operation.
697
+ fn new_command ( ) -> Delay {
698
+ Delay :: new ( Self :: DEFAULT_COMMAND_RETRIES )
699
+ }
700
+
671
701
/// Wait for a while.
672
702
///
673
703
/// Checks the retry counter first, and if we hit the max retry limit, the
0 commit comments