@@ -180,6 +180,7 @@ pub enum Error {
180
180
}
181
181
182
182
pub struct Sdio {
183
+ sdio : SDIO ,
183
184
bw : Buswidth ,
184
185
card : Option < Card > ,
185
186
}
@@ -238,8 +239,8 @@ pub struct Card {
238
239
}
239
240
240
241
impl Sdio {
241
- pub fn new < PINS : Pins > ( _pins : PINS ) -> Self {
242
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
242
+ pub fn new < PINS : Pins > ( sdio : SDIO , _pins : PINS ) -> Self {
243
+ // let sdio = unsafe { &*SDIO::ptr() };
243
244
244
245
unsafe {
245
246
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
@@ -272,6 +273,7 @@ impl Sdio {
272
273
. modify ( |_, w| unsafe { w. pwrctrl ( ) . bits ( PowerCtrl :: Off as u8 ) } ) ;
273
274
274
275
Sdio {
276
+ sdio,
275
277
bw : PINS :: BUSWIDTH ,
276
278
card : None ,
277
279
}
@@ -282,19 +284,18 @@ impl Sdio {
282
284
}
283
285
284
286
pub fn init_card ( & mut self ) -> Result < ( ) , Error > {
285
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
286
-
287
287
// Enable power to card
288
- sdio. power
288
+ self . sdio
289
+ . power
289
290
. modify ( |_, w| unsafe { w. pwrctrl ( ) . bits ( PowerCtrl :: On as u8 ) } ) ;
290
291
291
292
// Enable clock
292
- sdio. clkcr . modify ( |_, w| w. clken ( ) . set_bit ( ) ) ;
293
+ self . sdio . clkcr . modify ( |_, w| w. clken ( ) . set_bit ( ) ) ;
293
294
294
295
self . cmd ( Cmd :: idle ( ) ) ?;
295
296
296
297
self . cmd ( Cmd :: hs_send_ext_csd ( 0x1AA ) ) ?;
297
- let r1 = sdio. resp1 . read ( ) . bits ( ) ;
298
+ let r1 = self . sdio . resp1 . read ( ) . bits ( ) ;
298
299
299
300
let mut card = if r1 == 0x1AA {
300
301
/* v2 card */
@@ -315,7 +316,7 @@ impl Sdio {
315
316
Err ( Error :: Crc ) => ( ) ,
316
317
Err ( err) => return Err ( err) ,
317
318
}
318
- let ocr = sdio. resp1 . read ( ) . bits ( ) ;
319
+ let ocr = self . sdio . resp1 . read ( ) . bits ( ) ;
319
320
if ocr & 0x8000_0000 == 0 {
320
321
// Still powering up
321
322
continue ;
@@ -334,21 +335,21 @@ impl Sdio {
334
335
335
336
// Get CID
336
337
self . cmd ( Cmd :: all_send_cid ( ) ) ?;
337
- card. cid [ 0 ] = sdio. resp1 . read ( ) . bits ( ) ;
338
- card. cid [ 1 ] = sdio. resp2 . read ( ) . bits ( ) ;
339
- card. cid [ 2 ] = sdio. resp3 . read ( ) . bits ( ) ;
340
- card. cid [ 3 ] = sdio. resp4 . read ( ) . bits ( ) ;
338
+ card. cid [ 0 ] = self . sdio . resp1 . read ( ) . bits ( ) ;
339
+ card. cid [ 1 ] = self . sdio . resp2 . read ( ) . bits ( ) ;
340
+ card. cid [ 2 ] = self . sdio . resp3 . read ( ) . bits ( ) ;
341
+ card. cid [ 3 ] = self . sdio . resp4 . read ( ) . bits ( ) ;
341
342
342
343
// Get RCA
343
344
self . cmd ( Cmd :: send_rel_addr ( ) ) ?;
344
- card. rca = sdio. resp1 . read ( ) . bits ( ) >> 16 ;
345
+ card. rca = self . sdio . resp1 . read ( ) . bits ( ) >> 16 ;
345
346
346
347
// Get CSD
347
348
self . cmd ( Cmd :: send_csd ( card. rca << 16 ) ) ?;
348
- card. csd [ 0 ] = sdio. resp1 . read ( ) . bits ( ) ;
349
- card. csd [ 1 ] = sdio. resp2 . read ( ) . bits ( ) ;
350
- card. csd [ 2 ] = sdio. resp3 . read ( ) . bits ( ) ;
351
- card. csd [ 3 ] = sdio. resp4 . read ( ) . bits ( ) ;
349
+ card. csd [ 0 ] = self . sdio . resp1 . read ( ) . bits ( ) ;
350
+ card. csd [ 1 ] = self . sdio . resp2 . read ( ) . bits ( ) ;
351
+ card. csd [ 2 ] = self . sdio . resp3 . read ( ) . bits ( ) ;
352
+ card. csd [ 3 ] = self . sdio . resp4 . read ( ) . bits ( ) ;
352
353
353
354
self . select_card ( Some ( & card) ) ?;
354
355
@@ -362,17 +363,19 @@ impl Sdio {
362
363
Ok ( ( ) )
363
364
}
364
365
365
- pub fn read_block ( & self , addr : u32 , buf : & mut [ u8 ] ) -> Result < ( ) , Error > {
366
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
366
+ pub fn read_block ( & mut self , addr : u32 , buf : & mut [ u8 ] ) -> Result < ( ) , Error > {
367
367
let _card = self . card ( ) ?;
368
368
369
369
self . cmd ( Cmd :: set_blocklen ( 512 ) ) ?;
370
370
371
371
// Setup read command
372
- sdio. dtimer
372
+ self . sdio
373
+ . dtimer
373
374
. write ( |w| unsafe { w. datatime ( ) . bits ( 0xFFFF_FFFF ) } ) ;
374
- sdio. dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 512 ) } ) ;
375
- sdio. dctrl . write ( |w| unsafe {
375
+ self . sdio
376
+ . dlen
377
+ . write ( |w| unsafe { w. datalength ( ) . bits ( 512 ) } ) ;
378
+ self . sdio . dctrl . write ( |w| unsafe {
376
379
w. dblocksize ( )
377
380
. bits ( 9 ) //512
378
381
. dtdir ( )
@@ -385,7 +388,7 @@ impl Sdio {
385
388
let mut i = 0 ;
386
389
let mut sta;
387
390
while {
388
- sta = sdio. sta . read ( ) ;
391
+ sta = self . sdio . sta . read ( ) ;
389
392
!( sta. rxoverr ( ) . bit ( )
390
393
|| sta. dcrcfail ( ) . bit ( )
391
394
|| sta. dtimeout ( ) . bit ( )
@@ -394,7 +397,7 @@ impl Sdio {
394
397
} {
395
398
if sta. rxfifohf ( ) . bit ( ) {
396
399
for _ in 0 ..8 {
397
- let bytes = sdio. fifo . read ( ) . bits ( ) . to_le_bytes ( ) ;
400
+ let bytes = self . sdio . fifo . read ( ) . bits ( ) . to_le_bytes ( ) ;
398
401
buf[ i..i + 4 ] . copy_from_slice ( & bytes) ;
399
402
i += 4 ;
400
403
}
@@ -416,18 +419,19 @@ impl Sdio {
416
419
Ok ( ( ) )
417
420
}
418
421
419
- pub fn write_block ( & self , addr : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > {
420
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
421
-
422
+ pub fn write_block ( & mut self , addr : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > {
422
423
let _card = self . card ( ) ?;
423
424
424
425
self . cmd ( Cmd :: set_blocklen ( 512 ) ) ?;
425
426
426
427
// Setup write command
427
- sdio. dtimer
428
+ self . sdio
429
+ . dtimer
428
430
. write ( |w| unsafe { w. datatime ( ) . bits ( 0xFFFF_FFFF ) } ) ;
429
- sdio. dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 512 ) } ) ;
430
- sdio. dctrl . write ( |w| unsafe {
431
+ self . sdio
432
+ . dlen
433
+ . write ( |w| unsafe { w. datalength ( ) . bits ( 512 ) } ) ;
434
+ self . sdio . dctrl . write ( |w| unsafe {
431
435
w. dblocksize ( )
432
436
. bits ( 9 ) //512
433
437
. dtdir ( )
@@ -440,7 +444,7 @@ impl Sdio {
440
444
let mut i = 0 ;
441
445
let mut sta;
442
446
while {
443
- sta = sdio. sta . read ( ) ;
447
+ sta = self . sdio . sta . read ( ) ;
444
448
!( sta. txunderr ( ) . bit ( )
445
449
|| sta. dcrcfail ( ) . bit ( )
446
450
|| sta. dtimeout ( ) . bit ( )
@@ -452,7 +456,7 @@ impl Sdio {
452
456
let mut wb = [ 0u8 ; 4 ] ;
453
457
wb. copy_from_slice ( & buf[ i..i + 4 ] ) ;
454
458
let word = u32:: from_le_bytes ( wb) ;
455
- sdio. fifo . write ( |w| unsafe { w. bits ( word) } ) ;
459
+ self . sdio . fifo . write ( |w| unsafe { w. bits ( word) } ) ;
456
460
i += 4 ;
457
461
}
458
462
}
@@ -474,19 +478,18 @@ impl Sdio {
474
478
}
475
479
476
480
fn read_card_status ( & mut self ) -> Result < ( ) , Error > {
477
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
478
-
479
481
let card = self . card ( ) ?;
480
482
481
483
self . cmd ( Cmd :: set_blocklen ( 64 ) ) ?;
482
484
483
485
self . cmd ( Cmd :: app_cmd ( card. rca << 16 ) ) ?;
484
486
485
487
// Prepare the transfer
486
- sdio. dtimer
488
+ self . sdio
489
+ . dtimer
487
490
. write ( |w| unsafe { w. datatime ( ) . bits ( 0xFFFF_FFFF ) } ) ;
488
- sdio. dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 64 ) } ) ;
489
- sdio. dctrl . write ( |w| unsafe {
491
+ self . sdio . dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 64 ) } ) ;
492
+ self . sdio . dctrl . write ( |w| unsafe {
490
493
w. dblocksize ( )
491
494
. bits ( 6 ) // 64
492
495
. dtdir ( )
@@ -501,15 +504,15 @@ impl Sdio {
501
504
let mut idx = 0 ;
502
505
let mut sta;
503
506
while {
504
- sta = sdio. sta . read ( ) ;
507
+ sta = self . sdio . sta . read ( ) ;
505
508
!( sta. rxoverr ( ) . bit ( )
506
509
|| sta. dcrcfail ( ) . bit ( )
507
510
|| sta. dtimeout ( ) . bit ( )
508
511
|| sta. dbckend ( ) . bit ( ) )
509
512
} {
510
513
if sta. rxfifohf ( ) . bit ( ) {
511
514
for _ in 0 ..8 {
512
- status[ idx] = sdio. fifo . read ( ) . bits ( ) ;
515
+ status[ idx] = self . sdio . fifo . read ( ) . bits ( ) ;
513
516
idx += 1 ;
514
517
}
515
518
}
@@ -544,31 +547,31 @@ impl Sdio {
544
547
}
545
548
546
549
fn get_scr ( & self , card : & mut Card ) -> Result < ( ) , Error > {
547
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
548
-
549
550
self . cmd ( Cmd :: set_blocklen ( 8 ) ) ?;
550
551
self . cmd ( Cmd :: app_cmd ( card. rca << 16 ) ) ?;
551
552
552
- sdio. dtimer
553
+ self . sdio
554
+ . dtimer
553
555
. write ( |w| unsafe { w. datatime ( ) . bits ( 0xFFFF_FFFF ) } ) ;
554
- sdio. dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 8 ) } ) ;
555
- sdio. dctrl
556
+ self . sdio . dlen . write ( |w| unsafe { w. datalength ( ) . bits ( 8 ) } ) ;
557
+ self . sdio
558
+ . dctrl
556
559
. write ( |w| unsafe { w. dblocksize ( ) . bits ( 3 ) . dtdir ( ) . set_bit ( ) . dten ( ) . set_bit ( ) } ) ;
557
560
self . cmd ( Cmd :: cmd51 ( ) ) ?;
558
561
559
562
let mut scr = [ 0 ; 2 ] ;
560
563
let mut i = 0 ;
561
564
let mut sta;
562
565
while {
563
- sta = sdio. sta . read ( ) ;
566
+ sta = self . sdio . sta . read ( ) ;
564
567
565
568
!( sta. rxoverr ( ) . bit ( )
566
569
|| sta. dcrcfail ( ) . bit ( )
567
570
|| sta. dtimeout ( ) . bit ( )
568
571
|| sta. dbckend ( ) . bit ( ) )
569
572
} {
570
573
if sta. rxdavl ( ) . bit ( ) {
571
- scr[ i] = sdio. fifo . read ( ) . bits ( ) ;
574
+ scr[ i] = self . sdio . fifo . read ( ) . bits ( ) ;
572
575
i += 1 ;
573
576
}
574
577
@@ -600,8 +603,6 @@ impl Sdio {
600
603
601
604
/// Set bus width and clock frequency
602
605
fn set_bus ( & self , width : Buswidth , freq : ClockFreq , card : & Card ) -> Result < ( ) , Error > {
603
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
604
-
605
606
let ( width, acmd_arg) = match width {
606
607
Buswidth :: Buswidth4 if card. supports_widebus ( ) => ( width, 2 ) ,
607
608
_ => ( Buswidth :: Buswidth1 , 1 ) ,
@@ -610,7 +611,7 @@ impl Sdio {
610
611
self . cmd ( Cmd :: app_cmd ( card. rca << 16 ) ) ?;
611
612
self . cmd ( Cmd :: acmd6 ( acmd_arg) ) ?;
612
613
613
- sdio. clkcr . modify ( |_, w| unsafe {
614
+ self . sdio . clkcr . modify ( |_, w| unsafe {
614
615
w. clkdiv ( )
615
616
. bits ( freq as u8 )
616
617
. widbus ( )
@@ -622,10 +623,8 @@ impl Sdio {
622
623
}
623
624
624
625
fn cmd ( & self , cmd : Cmd ) -> Result < ( ) , Error > {
625
- let sdio = unsafe { & * SDIO :: ptr ( ) } ;
626
-
627
626
// Clear interrupts
628
- sdio. icr . modify ( |_, w| {
627
+ self . sdio . icr . modify ( |_, w| {
629
628
w. ccrcfailc ( )
630
629
. set_bit ( )
631
630
. ctimeoutc ( )
@@ -655,9 +654,9 @@ impl Sdio {
655
654
} ) ;
656
655
657
656
// Command arg
658
- sdio. arg . write ( |w| unsafe { w. cmdarg ( ) . bits ( cmd. arg ) } ) ;
657
+ self . sdio . arg . write ( |w| unsafe { w. cmdarg ( ) . bits ( cmd. arg ) } ) ;
659
658
660
- sdio. cmd . write ( |w| unsafe {
659
+ self . sdio . cmd . write ( |w| unsafe {
661
660
w. waitresp ( )
662
661
. bits ( cmd. resp as u8 )
663
662
. cmdindex ( )
@@ -674,14 +673,14 @@ impl Sdio {
674
673
if cmd. resp == Response :: None {
675
674
// Wait for command sent or a timeout
676
675
while {
677
- sta = sdio. sta . read ( ) ;
676
+ sta = self . sdio . sta . read ( ) ;
678
677
!( sta. ctimeout ( ) . bit ( ) || sta. cmdsent ( ) . bit ( ) ) && timeout > 0
679
678
} {
680
679
timeout -= 1 ;
681
680
}
682
681
} else {
683
682
while {
684
- sta = sdio. sta . read ( ) ;
683
+ sta = self . sdio . sta . read ( ) ;
685
684
!( sta. ctimeout ( ) . bit ( ) || sta. cmdrend ( ) . bit ( ) || sta. ccrcfail ( ) . bit ( ) )
686
685
&& timeout > 0
687
686
} {
0 commit comments