@@ -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 >
3838where
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 >
4748where
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 >
122130where
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 >
167176where
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 >
179190where
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
628642impl 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