37
37
_TOKEN_STOP_TRAN = const (0xFD )
38
38
_TOKEN_DATA = const (0xFE )
39
39
40
+ ## This function computes crc7 for SDcard commands
41
+ ## It is not optimized, so do not call it where performance is critical
42
+ ## You should usually be precomputing these values and hardcode them
43
+ #def crc7( cmd:bytes ):
44
+ # crc = 0
45
+ # for value in cmd:
46
+ # for i in range(8):
47
+ # crc <<= 1
48
+ # if (value&0x80)^(crc&0x80):
49
+ # crc ^= 0x09
50
+ # value <<= 1
51
+ # return ((crc<<1)|1)&0xFF
52
+ #def cmd_args_to_bytes( cmd:int, args:int ):
53
+ # return bytes( [cmd|0x40,(args>>24)&0xFF,(args>>16)&0xFF,(args>>8)&0xFF,args&0xFF] )
40
54
41
55
class SDCard :
42
56
def __init__ (self , spi , cs , baudrate = 1320000 ):
@@ -92,7 +106,7 @@ def init_card(self, baudrate):
92
106
93
107
# get the number of sectors
94
108
# CMD9: response R2 (R1 byte + 16-byte block read)
95
- if self .cmd (9 , 0 , 0 , 0 , False ) != 0 :
109
+ if self .cmd (9 , 0 , 0xAF , 0 , False ) != 0 :
96
110
raise OSError ("no response from SD card" )
97
111
csd = bytearray (16 )
98
112
self .readinto (csd )
@@ -109,7 +123,7 @@ def init_card(self, baudrate):
109
123
# print('sectors', self.sectors)
110
124
111
125
# CMD16: set block length to 512 bytes
112
- if self .cmd (16 , 512 , 0 ) != 0 :
126
+ if self .cmd (16 , 512 , 0x15 ) != 0 :
113
127
raise OSError ("can't set 512 block size" )
114
128
115
129
# set to high data rate now that it's initialised
@@ -118,8 +132,8 @@ def init_card(self, baudrate):
118
132
def init_card_v1 (self ):
119
133
for i in range (_CMD_TIMEOUT ):
120
134
time .sleep_ms (50 )
121
- self .cmd (55 , 0 , 0 )
122
- if self .cmd (41 , 0 , 0 ) == 0 :
135
+ self .cmd (55 , 0 , 0x65 )
136
+ if self .cmd (41 , 0 , 0xE5 ) == 0 :
123
137
# SDSC card, uses byte addressing in read/write/erase commands
124
138
self .cdv = 512
125
139
# print("[SDCard] v1 card")
@@ -129,10 +143,10 @@ def init_card_v1(self):
129
143
def init_card_v2 (self ):
130
144
for i in range (_CMD_TIMEOUT ):
131
145
time .sleep_ms (50 )
132
- self .cmd (58 , 0 , 0 , 4 )
133
- self .cmd (55 , 0 , 0 )
134
- if self .cmd (41 , 0x40000000 , 0 ) == 0 :
135
- self .cmd (58 , 0 , 0 , - 4 ) # 4-byte response, negative means keep the first byte
146
+ self .cmd (58 , 0 , 0xFD , 4 )
147
+ self .cmd (55 , 0 , 0x65 )
148
+ if self .cmd (41 , 0x40000000 , 0x77 ) == 0 :
149
+ self .cmd (58 , 0 , 0xFD , - 4 ) # 4-byte response, negative means keep the first byte
136
150
ocr = self .tokenbuf [0 ] # get first byte of response, which is OCR
137
151
if not ocr & 0x40 :
138
152
# SDSC card, uses byte addressing in read/write/erase commands
@@ -250,15 +264,15 @@ def readblocks(self, block_num, buf):
250
264
assert nblocks and not len (buf ) % 512 , "Buffer length is invalid"
251
265
if nblocks == 1 :
252
266
# CMD17: set read address for single block
253
- if self .cmd (17 , block_num * self .cdv , 0 , release = False ) != 0 :
267
+ if self .cmd (17 , block_num * self .cdv , 0x7F , release = False ) != 0 :
254
268
# release the card
255
269
self .cs (1 )
256
270
raise OSError (5 ) # EIO
257
271
# receive the data and release card
258
272
self .readinto (buf )
259
273
else :
260
274
# CMD18: set read address for multiple blocks
261
- if self .cmd (18 , block_num * self .cdv , 0 , release = False ) != 0 :
275
+ if self .cmd (18 , block_num * self .cdv , 0x7F , release = False ) != 0 :
262
276
# release the card
263
277
self .cs (1 )
264
278
raise OSError (5 ) # EIO
@@ -269,7 +283,7 @@ def readblocks(self, block_num, buf):
269
283
self .readinto (mv [offset : offset + 512 ])
270
284
offset += 512
271
285
nblocks -= 1
272
- if self .cmd (12 , 0 , 0xFF , skip1 = True ):
286
+ if self .cmd (12 , 0 , 0x61 , skip1 = True ):
273
287
raise OSError (5 ) # EIO
274
288
275
289
def writeblocks (self , block_num , buf ):
0 commit comments