72
72
inner : RefCell :: new ( SdCardInner {
73
73
spi,
74
74
cs,
75
- card_type : CardType :: Unknown ,
75
+ card_type : None ,
76
76
options,
77
77
} ) ,
78
78
}
85
85
F : FnOnce ( & mut SPI ) -> T ,
86
86
{
87
87
let mut inner = self . inner . borrow_mut ( ) ;
88
- let result = func ( & mut inner. spi ) ;
89
- result
88
+ func ( & mut inner. spi )
90
89
}
91
90
92
91
/// Return the usable size of this SD card in bytes.
@@ -108,7 +107,24 @@ where
108
107
/// The next operation will assume the card has been freshly inserted.
109
108
pub fn mark_card_uninit ( & self ) {
110
109
let mut inner = self . inner . borrow_mut ( ) ;
111
- inner. card_type = CardType :: Unknown ;
110
+ inner. card_type = None ;
111
+ }
112
+
113
+ /// Get the card type.
114
+ pub fn get_card_type ( & self ) -> Option < CardType > {
115
+ let inner = self . inner . borrow ( ) ;
116
+ inner. card_type
117
+ }
118
+
119
+ /// Tell the driver the card has been initialised.
120
+ ///
121
+ /// # Safety
122
+ ///
123
+ /// Only do this if the card has actually been initialised and is of the
124
+ /// indicated type, otherwise corruption may occur.
125
+ pub unsafe fn mark_card_as_init ( & self , card_type : CardType ) {
126
+ let mut inner = self . inner . borrow_mut ( ) ;
127
+ inner. card_type = Some ( card_type) ;
112
128
}
113
129
}
114
130
@@ -165,7 +181,7 @@ where
165
181
{
166
182
spi : SPI ,
167
183
cs : CS ,
168
- card_type : CardType ,
184
+ card_type : Option < CardType > ,
169
185
options : AcquireOpts ,
170
186
}
171
187
@@ -178,9 +194,9 @@ where
178
194
/// Read one or more blocks, starting at the given block index.
179
195
fn read ( & mut self , blocks : & mut [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Error > {
180
196
let start_idx = match self . card_type {
181
- CardType :: SD1 | CardType :: SD2 => start_block_idx. 0 * 512 ,
182
- CardType :: Sdhc => start_block_idx. 0 ,
183
- CardType :: Unknown => return Err ( Error :: CardNotFound ) ,
197
+ Some ( CardType :: SD1 | CardType :: SD2 ) => start_block_idx. 0 * 512 ,
198
+ Some ( CardType :: SDHC ) => start_block_idx. 0 ,
199
+ None => return Err ( Error :: CardNotFound ) ,
184
200
} ;
185
201
self . with_chip_select ( |s| {
186
202
if blocks. len ( ) == 1 {
@@ -203,9 +219,9 @@ where
203
219
/// Write one or more blocks, starting at the given block index.
204
220
fn write ( & mut self , blocks : & [ Block ] , start_block_idx : BlockIdx ) -> Result < ( ) , Error > {
205
221
let start_idx = match self . card_type {
206
- CardType :: SD1 | CardType :: SD2 => start_block_idx. 0 * 512 ,
207
- CardType :: Sdhc => start_block_idx. 0 ,
208
- CardType :: Unknown => return Err ( Error :: CardNotFound ) ,
222
+ Some ( CardType :: SD1 | CardType :: SD2 ) => start_block_idx. 0 * 512 ,
223
+ Some ( CardType :: SDHC ) => start_block_idx. 0 ,
224
+ None => return Err ( Error :: CardNotFound ) ,
209
225
} ;
210
226
self . with_chip_select ( |s| {
211
227
if blocks. len ( ) == 1 {
@@ -273,23 +289,23 @@ where
273
289
/// Read the 'card specific data' block.
274
290
fn read_csd ( & mut self ) -> Result < Csd , Error > {
275
291
match self . card_type {
276
- CardType :: SD1 => {
292
+ Some ( CardType :: SD1 ) => {
277
293
let mut csd = CsdV1 :: new ( ) ;
278
294
if self . card_command ( CMD9 , 0 ) ? != 0 {
279
295
return Err ( Error :: RegisterReadError ) ;
280
296
}
281
297
self . read_data ( & mut csd. data ) ?;
282
298
Ok ( Csd :: V1 ( csd) )
283
299
}
284
- CardType :: SD2 | CardType :: Sdhc => {
300
+ Some ( CardType :: SD2 | CardType :: SDHC ) => {
285
301
let mut csd = CsdV2 :: new ( ) ;
286
302
if self . card_command ( CMD9 , 0 ) ? != 0 {
287
303
return Err ( Error :: RegisterReadError ) ;
288
304
}
289
305
self . read_data ( & mut csd. data ) ?;
290
306
Ok ( Csd :: V2 ( csd) )
291
307
}
292
- CardType :: Unknown => Err ( Error :: CardNotFound ) ,
308
+ None => Err ( Error :: CardNotFound ) ,
293
309
}
294
310
}
295
311
@@ -353,7 +369,7 @@ where
353
369
354
370
/// Check the card is initialised.
355
371
fn check_init ( & mut self ) -> Result < ( ) , Error > {
356
- if self . card_type == CardType :: Unknown {
372
+ if self . card_type . is_none ( ) {
357
373
// If we don't know what the card type is, try and initialise the
358
374
// card. This will tell us what type of card it is.
359
375
self . acquire ( )
@@ -444,14 +460,14 @@ where
444
460
return Err ( Error :: Cmd58Error ) ;
445
461
}
446
462
if ( s. receive ( ) ? & 0xC0 ) == 0xC0 {
447
- card_type = CardType :: Sdhc ;
463
+ card_type = CardType :: SDHC ;
448
464
}
449
465
// Discard other three bytes
450
466
s. receive ( ) ?;
451
467
s. receive ( ) ?;
452
468
s. receive ( ) ?;
453
469
}
454
- s. card_type = card_type;
470
+ s. card_type = Some ( card_type) ;
455
471
Ok ( ( ) )
456
472
} ;
457
473
let result = f ( self ) ;
@@ -597,12 +613,20 @@ pub enum Error {
597
613
598
614
/// The different types of card we support.
599
615
#[ cfg_attr( feature = "defmt-log" , derive( defmt:: Format ) ) ]
600
- #[ derive( Debug , Copy , Clone , PartialEq ) ]
601
- enum CardType {
602
- Unknown ,
616
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
617
+ pub enum CardType {
618
+ /// An standard-capacity SD Card supporting v1.x of the standard.
619
+ ///
620
+ /// Uses byte-addressing internally, so limited to 2GiB in size.
603
621
SD1 ,
622
+ /// An standard-capacity SD Card supporting v2.x of the standard.
623
+ ///
624
+ /// Uses byte-addressing internally, so limited to 2GiB in size.
604
625
SD2 ,
605
- Sdhc ,
626
+ /// An high-capacity 'SDHC' Card.
627
+ ///
628
+ /// Uses block-addressing internally to support capacities above 2GiB.
629
+ SDHC ,
606
630
}
607
631
608
632
/// A terrible hack for busy-waiting the CPU while we wait for the card to
0 commit comments