Skip to content

Commit c7aa2f2

Browse files
authored
Merge pull request #20 from devoh747/master
Fixed Issue #11 with Samsung EVO 32GB cards
2 parents cb8f61b + 9161d2e commit c7aa2f2

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

adafruit_sdcard.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _init_card(self):
153153
# get the number of sectors
154154
# CMD9: response R2 (R1 byte + 16-byte block read)
155155
csd = bytearray(16)
156-
if self._cmd(9, response_buf=csd) != 0:
156+
if self._cmd(9, 0, 0xaf, response_buf=csd) != 0:
157157
raise OSError("no response from SD card")
158158
#self.readinto(csd)
159159
csd_version = (csd[0] & 0xc0) >> 6
@@ -169,7 +169,7 @@ def _init_card(self):
169169
self._sectors = block_length // 512 * mult * (c_size + 1)
170170

171171
# CMD16: set block length to 512 bytes
172-
if self._cmd(16, 512, 0) != 0:
172+
if self._cmd(16, 512, 0x15) != 0:
173173
raise OSError("can't set 512 block size")
174174

175175
# set to high data rate now that it's initialised
@@ -190,10 +190,10 @@ def _init_card_v2(self):
190190
ocr = bytearray(4)
191191
for _ in range(_CMD_TIMEOUT):
192192
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)
197197

198198
# Check for block addressing
199199
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)
234234
buf[2] = (arg >> 16) & 0xff
235235
buf[3] = (arg >> 8) & 0xff
236236
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
238242

239243
with self._spi as spi:
240244
if wait:
@@ -281,7 +285,11 @@ def _block_cmd(self, cmd, block, crc, response_buf=None):
281285
buf[2] = (block >> 7) & 0xff
282286
buf[3] = (block << 1) & 0xff
283287
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
285293

286294
result = -1
287295
with self._spi as spi:
@@ -416,7 +424,7 @@ def readblocks(self, start_block, buf):
416424
self._readinto(buf, start=offset, end=(offset + 512))
417425
offset += 512
418426
nblocks -= 1
419-
return self._cmd(12, wait=False)
427+
return self._cmd(12, 0, 0x61, wait=False)
420428
return 0
421429

422430
def writeblocks(self, start_block, buf):
@@ -447,3 +455,34 @@ def writeblocks(self, start_block, buf):
447455
nblocks -= 1
448456
self._cmd_nodata(_TOKEN_STOP_TRAN, 0x0)
449457
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

Comments
 (0)