@@ -92,10 +92,23 @@ def init_card(self, baudrate):
9292
9393 # get the number of sectors
9494 # CMD9: response R2 (R1 byte + 16-byte block read)
95+ csd = self .get_csd ()
96+ self .parse_csd (csd )
97+
98+ # CMD16: set block length to 512 bytes
99+ if self .cmd (16 , 512 , 0 ) != 0 :
100+ raise OSError ("can't set 512 block size" )
101+
102+ # set to high data rate now that it's initialised
103+ self .init_spi (baudrate )
104+
105+ def get_csd (self ):
95106 if self .cmd (9 , 0 , 0 , 0 , False ) != 0 :
96107 raise OSError ("no response from SD card" )
97108 csd = bytearray (16 )
98109 self .readinto (csd )
110+
111+ def parse_csd (self , csd ):
99112 if csd [0 ] & 0xC0 == 0x40 : # CSD version 2.0
100113 self .sectors = ((csd [7 ] << 16 | csd [8 ] << 8 | csd [9 ]) + 1 ) * 1024
101114 elif csd [0 ] & 0xC0 == 0x00 : # CSD version 1.0 (old, <=2GB)
@@ -106,14 +119,6 @@ def init_card(self, baudrate):
106119 self .sectors = capacity // 512
107120 else :
108121 raise OSError ("SD card CSD format not supported" )
109- # print('sectors', self.sectors)
110-
111- # CMD16: set block length to 512 bytes
112- if self .cmd (16 , 512 , 0 ) != 0 :
113- raise OSError ("can't set 512 block size" )
114-
115- # set to high data rate now that it's initialised
116- self .init_spi (baudrate )
117122
118123 def init_card_v1 (self ):
119124 for i in range (_CMD_TIMEOUT ):
0 commit comments