1
+ //! Sdio host
2
+
1
3
use crate :: bb;
2
4
#[ allow( unused_imports) ]
3
5
use crate :: gpio:: { gpioa:: * , gpiob:: * , gpioc:: * , gpiod:: * , Alternate , AF12 } ;
@@ -135,7 +137,7 @@ enum CmdAppOper {
135
137
}
136
138
137
139
#[ derive( Eq , PartialEq , Copy , Clone ) ]
138
- pub enum Response {
140
+ enum Response {
139
141
None = 0 ,
140
142
Short = 1 ,
141
143
Long = 3 ,
@@ -179,6 +181,7 @@ pub enum Error {
179
181
NoCard ,
180
182
}
181
183
184
+ /// Sdio device
182
185
pub struct Sdio {
183
186
sdio : SDIO ,
184
187
bw : Buswidth ,
@@ -192,6 +195,7 @@ struct Cmd {
192
195
}
193
196
194
197
#[ derive( Debug , Copy , Clone , Default ) ]
198
+ /// Card identification
195
199
pub struct Cid {
196
200
pub manufacturerid : u8 ,
197
201
pub oem_applicationid : u16 ,
@@ -204,6 +208,7 @@ pub struct Cid {
204
208
}
205
209
206
210
#[ derive( Debug , Copy , Clone , Default ) ]
211
+ /// Card specific data
207
212
pub struct Csd {
208
213
pub sys_spec_version : u8 ,
209
214
pub max_bus_clk_frec : u8 ,
@@ -213,6 +218,7 @@ pub struct Csd {
213
218
}
214
219
215
220
#[ derive( Debug , Default , Copy , Clone ) ]
221
+ /// Sd card status
216
222
pub struct Status {
217
223
pub bus_width : u8 ,
218
224
pub secure_mode : u8 ,
@@ -227,6 +233,7 @@ pub struct Status {
227
233
}
228
234
229
235
#[ derive( Debug , Default ) ]
236
+ /// Sd card
230
237
pub struct Card {
231
238
pub version : CardVersion ,
232
239
pub ctype : CardType ,
@@ -239,9 +246,8 @@ pub struct Card {
239
246
}
240
247
241
248
impl Sdio {
249
+ /// Create and enable the Sdio device
242
250
pub fn new < PINS : Pins > ( sdio : SDIO , _pins : PINS ) -> Self {
243
- //let sdio = unsafe { &*SDIO::ptr() };
244
-
245
251
unsafe {
246
252
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
247
253
let rcc = & * RCC :: ptr ( ) ;
@@ -279,10 +285,7 @@ impl Sdio {
279
285
}
280
286
}
281
287
282
- pub fn card ( & self ) -> Result < & Card , Error > {
283
- self . card . as_ref ( ) . ok_or ( Error :: NoCard )
284
- }
285
-
288
+ /// initialize card
286
289
pub fn init_card ( & mut self ) -> Result < ( ) , Error > {
287
290
// Enable power to card
288
291
self . sdio
@@ -294,23 +297,26 @@ impl Sdio {
294
297
295
298
self . cmd ( Cmd :: idle ( ) ) ?;
296
299
300
+ // Check if cards supports CMD 8 (with pattern)
297
301
self . cmd ( Cmd :: hs_send_ext_csd ( 0x1AA ) ) ?;
298
302
let r1 = self . sdio . resp1 . read ( ) . bits ( ) ;
299
303
300
304
let mut card = if r1 == 0x1AA {
301
- /* v2 card */
305
+ /* Card echoed back the pattern, we have a v2 card */
302
306
Card :: default ( )
303
307
} else {
304
308
return Err ( Error :: UnsupportedCardVersion ) ;
305
309
} ;
306
310
307
311
let ocr = loop {
312
+ // Signal that next command is a app command
308
313
self . cmd ( Cmd :: app_cmd ( 0 ) ) ?;
309
314
310
315
let arg = CmdAppOper :: VOLTAGE_WINDOW_SD as u32
311
316
| CmdAppOper :: HIGH_CAPACITY as u32
312
317
| CmdAppOper :: SD_SWITCH_1_8V_CAPACITY as u32 ;
313
318
319
+ // Initialize card
314
320
match self . cmd ( Cmd :: app_op_cmd ( arg) ) {
315
321
Ok ( _) => ( ) ,
316
322
Err ( Error :: Crc ) => ( ) ,
@@ -363,6 +369,12 @@ impl Sdio {
363
369
Ok ( ( ) )
364
370
}
365
371
372
+ /// Get a reference to the initialized card
373
+ pub fn card ( & self ) -> Result < & Card , Error > {
374
+ self . card . as_ref ( ) . ok_or ( Error :: NoCard )
375
+ }
376
+
377
+ /// Read block from card. buf must be at least 512 bytes
366
378
pub fn read_block ( & mut self , addr : u32 , buf : & mut [ u8 ] ) -> Result < ( ) , Error > {
367
379
let _card = self . card ( ) ?;
368
380
@@ -419,6 +431,7 @@ impl Sdio {
419
431
Ok ( ( ) )
420
432
}
421
433
434
+ /// Write block to card. buf must be at least 512 bytes
422
435
pub fn write_block ( & mut self , addr : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > {
423
436
let _card = self . card ( ) ?;
424
437
@@ -622,6 +635,7 @@ impl Sdio {
622
635
Ok ( ( ) )
623
636
}
624
637
638
+ /// Send command to card
625
639
fn cmd ( & self , cmd : Cmd ) -> Result < ( ) , Error > {
626
640
// Clear interrupts
627
641
self . sdio . icr . modify ( |_, w| {
@@ -787,17 +801,14 @@ impl Cmd {
787
801
Cmd :: new ( 8 , arg, Response :: Short )
788
802
}
789
803
790
- // SEND_CSD
791
804
const fn send_csd ( rca : u32 ) -> Cmd {
792
805
Cmd :: new ( 9 , rca, Response :: Long )
793
806
}
794
807
795
- /// Set Status (13)
796
808
const fn send_card_status ( ) -> Cmd {
797
809
Cmd :: new ( 13 , 0 , Response :: Short )
798
810
}
799
811
800
- /// Set block length
801
812
const fn set_blocklen ( blocklen : u32 ) -> Cmd {
802
813
Cmd :: new ( 16 , blocklen, Response :: Short )
803
814
}
0 commit comments