@@ -34,34 +34,42 @@ const DEFAULT_DELAY_COUNT: u32 = 32_000;
34
34
/// (which puts the card into SPI mode).
35
35
///
36
36
/// 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 >
38
38
where
39
39
SPI : embedded_hal:: blocking:: spi:: Transfer < u8 > ,
40
40
CS : embedded_hal:: digital:: v2:: OutputPin ,
41
41
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
42
+ DELAYER : embedded_hal:: blocking:: delay:: DelayUs < u8 > ,
42
43
{
43
- inner : RefCell < SdCardInner < SPI , CS > > ,
44
+ inner : RefCell < SdCardInner < SPI , CS , DELAYER > > ,
44
45
}
45
46
46
- impl < SPI , CS > SdCard < SPI , CS >
47
+ impl < SPI , CS , DELAYER > SdCard < SPI , CS , DELAYER >
47
48
where
48
49
SPI : embedded_hal:: blocking:: spi:: Transfer < u8 > ,
49
50
CS : embedded_hal:: digital:: v2:: OutputPin ,
50
51
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
52
+ DELAYER : embedded_hal:: blocking:: delay:: DelayUs < u8 > ,
51
53
{
52
54
/// Create a new SD/MMC Card driver using a raw SPI interface.
53
55
///
54
56
/// 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 ( ) )
57
59
}
58
60
59
61
/// 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 > {
61
68
SdCard {
62
69
inner : RefCell :: new ( SdCardInner {
63
70
spi,
64
71
cs,
72
+ delayer,
65
73
card_type : None ,
66
74
options,
67
75
} ) ,
@@ -118,11 +126,12 @@ where
118
126
}
119
127
}
120
128
121
- impl < SPI , CS > BlockDevice for SdCard < SPI , CS >
129
+ impl < SPI , CS , DELAYER > BlockDevice for SdCard < SPI , CS , DELAYER >
122
130
where
123
131
SPI : embedded_hal:: blocking:: spi:: Transfer < u8 > ,
124
132
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
125
133
CS : embedded_hal:: digital:: v2:: OutputPin ,
134
+ DELAYER : embedded_hal:: blocking:: delay:: DelayUs < u8 > ,
126
135
{
127
136
type Error = Error ;
128
137
@@ -163,23 +172,26 @@ where
163
172
/// Represents an SD Card on an SPI bus.
164
173
///
165
174
/// All the APIs required `&mut self`.
166
- struct SdCardInner < SPI , CS >
175
+ struct SdCardInner < SPI , CS , DELAYER >
167
176
where
168
177
SPI : embedded_hal:: blocking:: spi:: Transfer < u8 > ,
169
178
CS : embedded_hal:: digital:: v2:: OutputPin ,
170
179
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
180
+ DELAYER : embedded_hal:: blocking:: delay:: DelayUs < u8 > ,
171
181
{
172
182
spi : SPI ,
173
183
cs : CS ,
184
+ delayer : DELAYER ,
174
185
card_type : Option < CardType > ,
175
186
options : AcquireOpts ,
176
187
}
177
188
178
- impl < SPI , CS > SdCardInner < SPI , CS >
189
+ impl < SPI , CS , DELAYER > SdCardInner < SPI , CS , DELAYER >
179
190
where
180
191
SPI : embedded_hal:: blocking:: spi:: Transfer < u8 > ,
181
192
CS : embedded_hal:: digital:: v2:: OutputPin ,
182
193
<SPI as embedded_hal:: blocking:: spi:: Transfer < u8 > >:: Error : core:: fmt:: Debug ,
194
+ DELAYER : embedded_hal:: blocking:: delay:: DelayUs < u8 > ,
183
195
{
184
196
/// Read one or more blocks, starting at the given block index.
185
197
fn read ( & mut self , blocks : & mut [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Error > {
@@ -309,7 +321,7 @@ where
309
321
if s != 0xFF {
310
322
break s;
311
323
}
312
- delay. delay ( Error :: TimeoutReadBuffer ) ?;
324
+ delay. delay ( & mut self . delayer , Error :: TimeoutReadBuffer ) ?;
313
325
} ;
314
326
if status != DATA_START_BLOCK {
315
327
return Err ( Error :: ReadError ) ;
@@ -411,7 +423,7 @@ where
411
423
}
412
424
}
413
425
414
- delay. delay ( Error :: TimeoutCommand ( CMD0 ) ) ?;
426
+ delay. delay ( & mut s . delayer , Error :: TimeoutCommand ( CMD0 ) ) ?;
415
427
}
416
428
if attempts == 0 {
417
429
return Err ( Error :: CardNotFound ) ;
@@ -436,12 +448,12 @@ where
436
448
card_type = CardType :: SD2 ;
437
449
break 0x4000_0000 ;
438
450
}
439
- delay. delay ( Error :: TimeoutCommand ( CMD8 ) ) ?;
451
+ delay. delay ( & mut s . delayer , Error :: TimeoutCommand ( CMD8 ) ) ?;
440
452
} ;
441
453
442
454
let mut delay = Delay :: new ( ) ;
443
455
while s. card_acmd ( ACMD41 , arg) ? != R1_READY_STATE {
444
- delay. delay ( Error :: TimeoutACommand ( ACMD41 ) ) ?;
456
+ delay. delay ( & mut s . delayer , Error :: TimeoutACommand ( ACMD41 ) ) ?;
445
457
}
446
458
447
459
if card_type == CardType :: SD2 {
@@ -547,7 +559,7 @@ where
547
559
if s == 0xFF {
548
560
break ;
549
561
}
550
- delay. delay ( Error :: TimeoutWaitNotBusy ) ?;
562
+ delay. delay ( & mut self . delayer , Error :: TimeoutWaitNotBusy ) ?;
551
563
}
552
564
Ok ( ( ) )
553
565
}
@@ -623,22 +635,26 @@ pub enum CardType {
623
635
/// sort itself out.
624
636
///
625
637
/// @TODO replace this!
626
- struct Delay ( u32 ) ;
638
+ struct Delay {
639
+ count : u32 ,
640
+ }
627
641
628
642
impl Delay {
629
643
fn new ( ) -> Delay {
630
- Delay ( DEFAULT_DELAY_COUNT )
644
+ Delay {
645
+ count : DEFAULT_DELAY_COUNT ,
646
+ }
631
647
}
632
648
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 {
635
654
Err ( err)
636
655
} 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 ;
642
658
Ok ( ( ) )
643
659
}
644
660
}
0 commit comments