@@ -153,7 +153,7 @@ def _init_card(self):
153
153
# get the number of sectors
154
154
# CMD9: response R2 (R1 byte + 16-byte block read)
155
155
csd = bytearray (16 )
156
- if self ._cmd (9 , response_buf = csd ) != 0 :
156
+ if self ._cmd (9 , 0 , 0xaf , response_buf = csd ) != 0 :
157
157
raise OSError ("no response from SD card" )
158
158
#self.readinto(csd)
159
159
csd_version = (csd [0 ] & 0xc0 ) >> 6
@@ -169,7 +169,7 @@ def _init_card(self):
169
169
self ._sectors = block_length // 512 * mult * (c_size + 1 )
170
170
171
171
# CMD16: set block length to 512 bytes
172
- if self ._cmd (16 , 512 , 0 ) != 0 :
172
+ if self ._cmd (16 , 512 , 0x15 ) != 0 :
173
173
raise OSError ("can't set 512 block size" )
174
174
175
175
# set to high data rate now that it's initialised
@@ -190,10 +190,10 @@ def _init_card_v2(self):
190
190
ocr = bytearray (4 )
191
191
for _ in range (_CMD_TIMEOUT ):
192
192
time .sleep (.050 )
193
- self ._cmd (58 , response_buf = ocr , data_block = False )
194
- self ._cmd (55 )
195
- if self ._block_cmd (41 , 0x200000 , 0 ) == 0 :
196
- self ._cmd (58 , response_buf = ocr , data_block = False )
193
+ self ._cmd (58 , 0 , 0xfd , response_buf = ocr , data_block = False )
194
+ self ._cmd (55 , 0 , 0x65 )
195
+ if self ._cmd (41 , 0x40000000 , 0x77 ) == 0 :
196
+ self ._cmd (58 , 0 , 0xfd , response_buf = ocr , data_block = False )
197
197
198
198
# Check for block addressing
199
199
if (ocr [0 ] & 0x40 ) != 0 :
@@ -234,7 +234,11 @@ def _cmd(self, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True)
234
234
buf [2 ] = (arg >> 16 ) & 0xff
235
235
buf [3 ] = (arg >> 8 ) & 0xff
236
236
buf [4 ] = arg & 0xff
237
- buf [5 ] = crc
237
+
238
+ if (crc == 0 ):
239
+ buf [5 ] = calculate_crc (buf [:- 1 ])
240
+ else :
241
+ buf [5 ] = crc
238
242
239
243
with self ._spi as spi :
240
244
if wait :
@@ -281,7 +285,11 @@ def _block_cmd(self, cmd, block, crc, response_buf=None):
281
285
buf [2 ] = (block >> 7 ) & 0xff
282
286
buf [3 ] = (block << 1 ) & 0xff
283
287
buf [4 ] = 0
284
- buf [5 ] = crc
288
+
289
+ if (crc == 0 ):
290
+ buf [5 ] = calculate_crc (buf [:- 1 ])
291
+ else :
292
+ buf [5 ] = crc
285
293
286
294
result = - 1
287
295
with self ._spi as spi :
@@ -416,7 +424,7 @@ def readblocks(self, start_block, buf):
416
424
self ._readinto (buf , start = offset , end = (offset + 512 ))
417
425
offset += 512
418
426
nblocks -= 1
419
- return self ._cmd (12 , wait = False )
427
+ return self ._cmd (12 , 0 , 0x61 , wait = False )
420
428
return 0
421
429
422
430
def writeblocks (self , start_block , buf ):
@@ -447,3 +455,34 @@ def writeblocks(self, start_block, buf):
447
455
nblocks -= 1
448
456
self ._cmd_nodata (_TOKEN_STOP_TRAN , 0x0 )
449
457
return 0
458
+
459
+ def calculate_crc (message ):
460
+ """
461
+ Calculate the CRC of a message.
462
+ :param bytearray message: Where each index is a byte
463
+ """
464
+ # Code converted from https://github.com/hazelnusse/crc7/blob/master/crc7.cc by devoh747
465
+ # With permission from Dale Lukas Peterson <[email protected] >
466
+ # 8/6/2019
467
+
468
+ crc_table = bytearray (256 )
469
+
470
+ crc_poly = const (0x89 ) # the value of our CRC-7 polynomial
471
+
472
+ # generate a table value for all 256 possible byte values
473
+ for i in range (256 ):
474
+ if (i & 0x80 ):
475
+ crc_table [i ] = i ^ crc_poly
476
+ else :
477
+ crc_table [i ] = i
478
+ for _ in range (1 , 8 ):
479
+ crc_table [i ] = crc_table [i ] << 1
480
+ if (crc_table [i ] & 0x80 ):
481
+ crc_table [i ] = crc_table [i ] ^ crc_poly
482
+
483
+ crc = 0
484
+ # All messages in _cmd are 5 bytes including the cmd.. The 6th byte is the crc value.
485
+ for i in range (0 , 5 ):
486
+ crc = crc_table [(crc << 1 ) ^ message [i ]]
487
+
488
+ return ((crc << 1 ) | 1 )
0 commit comments