@@ -133,6 +133,12 @@ pub enum Error {
133
133
NoCard ,
134
134
}
135
135
136
+ #[ derive( Debug , Copy , Clone ) ]
137
+ pub enum AddressMode {
138
+ Byte ,
139
+ Block512 ,
140
+ }
141
+
136
142
/// A peripheral that uses the SDIO hardware, generic over the particular type of device.
137
143
pub struct Sdio < P : SdioPeripheral > {
138
144
sdio : SDIO ,
@@ -153,7 +159,6 @@ pub struct SdCard {
153
159
154
160
/// eMMC device peripheral
155
161
pub struct Emmc {
156
- pub capacity : CardCapacity ,
157
162
pub ocr : OCR < EMMC > ,
158
163
pub rca : RCA < EMMC > , // Relative Card Address
159
164
pub cid : CID < EMMC > ,
@@ -232,9 +237,9 @@ impl<P: SdioPeripheral> Sdio<P> {
232
237
233
238
// Always read 1 block of 512 bytes
234
239
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
235
- let blockaddr = match card. get_capacity ( ) {
236
- CardCapacity :: StandardCapacity => blockaddr * 512 ,
237
- _ => blockaddr,
240
+ let blockaddr = match card. get_address_mode ( ) {
241
+ AddressMode :: Byte => blockaddr * 512 ,
242
+ AddressMode :: Block512 => blockaddr,
238
243
} ;
239
244
self . cmd ( common_cmd:: set_block_length ( 512 ) ) ?;
240
245
self . start_datapath_transfer ( 512 , 9 , true ) ;
@@ -276,9 +281,9 @@ impl<P: SdioPeripheral> Sdio<P> {
276
281
277
282
// Always write 1 block of 512 bytes
278
283
// SDSC cards are byte addressed hence the blockaddress is in multiples of 512 bytes
279
- let blockaddr = match card. get_capacity ( ) {
280
- CardCapacity :: StandardCapacity => blockaddr * 512 ,
281
- _ => blockaddr,
284
+ let blockaddr = match card. get_address_mode ( ) {
285
+ AddressMode :: Byte => blockaddr * 512 ,
286
+ AddressMode :: Block512 => blockaddr,
282
287
} ;
283
288
self . cmd ( common_cmd:: set_block_length ( 512 ) ) ?;
284
289
self . start_datapath_transfer ( 512 , 9 , false ) ;
@@ -496,7 +501,7 @@ impl Sdio<SdCard> {
496
501
// Initialize card
497
502
498
503
// 3.2-3.3V
499
- let voltage_window = 1 << 20 ;
504
+ let voltage_window = 1 << 5 ;
500
505
match self . app_cmd ( sd_cmd:: sd_send_op_cond ( true , false , true , voltage_window) ) {
501
506
Ok ( _) => ( ) ,
502
507
Err ( Error :: Crc ) => ( ) ,
@@ -653,7 +658,7 @@ impl Sdio<SdCard> {
653
658
}
654
659
655
660
impl Sdio < Emmc > {
656
- /// Initializes eMMC device (if present) and sets the bus at the specified frequency.
661
+ /// Initializes eMMC device (if present) and sets the bus at the specified frequency. eMMC device must support 512 byte blocks.
657
662
pub fn init ( & mut self , freq : ClockFreq ) -> Result < ( ) , Error > {
658
663
let card_addr: RCA < EMMC > = RCA :: from ( 1u16 ) ;
659
664
@@ -675,27 +680,20 @@ impl Sdio<Emmc> {
675
680
Err ( Error :: Crc ) => ( ) ,
676
681
Err ( err) => return Err ( err) ,
677
682
} ;
678
- let ocr = OCR :: from ( self . sdio . resp1 . read ( ) . bits ( ) ) ;
683
+ let ocr = OCR :: < EMMC > :: from ( self . sdio . resp1 . read ( ) . bits ( ) ) ;
679
684
if !ocr. is_busy ( ) {
680
685
break ocr;
681
686
}
682
687
} ;
683
688
684
- // True for SDHC and SDXC False for SDSC
685
- let capacity = if ocr. high_capacity ( ) {
686
- CardCapacity :: HighCapacity
687
- } else {
688
- CardCapacity :: StandardCapacity
689
- } ;
690
-
691
689
// Get CID
692
690
self . cmd ( common_cmd:: all_send_cid ( ) ) ?;
693
691
let mut cid = [ 0 ; 4 ] ;
694
692
cid[ 3 ] = self . sdio . resp1 . read ( ) . bits ( ) ;
695
693
cid[ 2 ] = self . sdio . resp2 . read ( ) . bits ( ) ;
696
694
cid[ 1 ] = self . sdio . resp3 . read ( ) . bits ( ) ;
697
695
cid[ 0 ] = self . sdio . resp4 . read ( ) . bits ( ) ;
698
- let cid = CID :: from ( cid) ;
696
+ let cid = CID :: < EMMC > :: from ( cid) ;
699
697
700
698
self . cmd ( emmc_cmd:: assign_relative_address ( card_addr. address ( ) ) ) ?;
701
699
@@ -706,12 +704,11 @@ impl Sdio<Emmc> {
706
704
csd[ 2 ] = self . sdio . resp2 . read ( ) . bits ( ) ;
707
705
csd[ 1 ] = self . sdio . resp3 . read ( ) . bits ( ) ;
708
706
csd[ 0 ] = self . sdio . resp4 . read ( ) . bits ( ) ;
709
- let csd = CSD :: from ( csd) ;
707
+ let csd = CSD :: < EMMC > :: from ( csd) ;
710
708
711
709
self . select_card ( card_addr. address ( ) ) ?;
712
710
713
711
let card = Emmc {
714
- capacity,
715
712
ocr,
716
713
rca : card_addr,
717
714
cid,
@@ -807,7 +804,7 @@ fn clear_all_interrupts(icr: &pac::sdio::ICR) {
807
804
808
805
impl SdCard {
809
806
/// Size in blocks
810
- pub fn block_count ( & self ) -> u32 {
807
+ pub fn block_count ( & self ) -> u64 {
811
808
self . csd . block_count ( )
812
809
}
813
810
@@ -821,21 +818,25 @@ impl SdioPeripheral for SdCard {
821
818
fn get_address ( & self ) -> u16 {
822
819
self . rca . address ( )
823
820
}
824
- fn get_capacity ( & self ) -> CardCapacity {
825
- self . capacity
821
+ fn get_address_mode ( & self ) -> AddressMode {
822
+ match self . capacity {
823
+ CardCapacity :: StandardCapacity => AddressMode :: Byte ,
824
+ CardCapacity :: HighCapacity => AddressMode :: Block512 ,
825
+ _ => AddressMode :: Block512 ,
826
+ }
826
827
}
827
828
}
828
829
829
830
impl SdioPeripheral for Emmc {
830
831
fn get_address ( & self ) -> u16 {
831
832
self . rca . address ( )
832
833
}
833
- fn get_capacity ( & self ) -> CardCapacity {
834
- self . capacity
834
+ fn get_address_mode ( & self ) -> AddressMode {
835
+ AddressMode :: Block512
835
836
}
836
837
}
837
838
838
839
pub trait SdioPeripheral {
839
840
fn get_address ( & self ) -> u16 ;
840
- fn get_capacity ( & self ) -> CardCapacity ;
841
+ fn get_address_mode ( & self ) -> AddressMode ;
841
842
}
0 commit comments