Skip to content

Commit cb825d8

Browse files
committed
drivers/sdcard/crc7.py: added check for whether viper mode is available
Added a try/except to crc7.py to provide a fallback if viper mode does not work right or does not exist. Also, slightly modified API to not require a buffer length. Instead, created a memoryview on the command buffer in sdcard.py that only sees the first five bytes. Signed-off-by: Marcus Mendenhall, mendenmh@gmail.com
1 parent 90b92ba commit cb825d8

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

micropython/drivers/storage/sdcard/crc7.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
crc7_be_syndrome_table = bytearray(256) # pre-allocate so it is low in memory
5151

52-
# avoid ruff long-line requirements by just filling out the table
52+
# avoid ruff long-line requirements of a static string for this by filling out the table
5353
_poly = const(0x89)
5454
for i in range(256):
5555
u = i
@@ -59,14 +59,25 @@
5959
u = u << 1
6060
crc7_be_syndrome_table[i] = u
6161

62-
63-
@micropython.viper
64-
def crc7(buf, n: int) -> int:
65-
table = ptr8(addressof(crc7_be_syndrome_table))
66-
bp = ptr8(addressof(buf))
67-
idx = 0
68-
crc = 0
69-
while idx < n:
70-
crc = table[crc ^ bp[idx]]
71-
idx = idx + 1
72-
return crc
62+
try:
63+
@micropython.viper
64+
def crc7(buf) -> int:
65+
n = int(len(buf))
66+
table = ptr8(addressof(crc7_be_syndrome_table))
67+
bp = ptr8(addressof(buf))
68+
idx = 0
69+
crc = 0
70+
while idx < n:
71+
crc = table[crc ^ bp[idx]]
72+
idx = idx + 1
73+
return crc
74+
#test to make sure this works!
75+
#print(f"{crc7('abcde'):02x}")
76+
assert crc7(b'abcde') == 0x34
77+
except:
78+
#non-viper version if viper can'r be built
79+
def crc7(buf) -> int:
80+
crc = 0
81+
for b in buf:
82+
crc = crc7_be_syndrome_table[crc ^ b]
83+
return crc

micropython/drivers/storage/sdcard/sd_card_spi_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ def sdtest():
7878
result2 = f.read()
7979
print(len(result2), "bytes read")
8080

81-
os.umount("/fc")
82-
8381
print()
8482
print("Verifying data read back")
8583
success = True
@@ -99,3 +97,6 @@ def sdtest():
9997

10098

10199
sdtest()
100+
101+
os.umount("/fc")
102+

micropython/drivers/storage/sdcard/sdcard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, spi, cs, baudrate=1320000, crc16_function=None):
5353
self.cs = cs
5454

5555
self.cmdbuf = bytearray(6)
56+
self.cmdbuf5 = memoryview(self.cmdbuf)[:5] #for crc7 generation
5657
self.tokenbuf = bytearray(1)
5758
self.crcbuf = bytearray(2)
5859
self.crc16 = None # during init
@@ -167,7 +168,7 @@ def cmd(self, cmd, arg, crc, final=0, release=True, skip1=False):
167168
buf[2] = arg >> 16
168169
buf[3] = arg >> 8
169170
buf[4] = arg
170-
buf[5] = (crc if crc is not None else crc7(buf, 5)) | 1
171+
buf[5] = (crc if crc is not None else crc7(self.cmdbuf5)) | 1
171172
w(buf)
172173

173174
if skip1:

0 commit comments

Comments
 (0)