Skip to content

Commit dd0fe85

Browse files
authored
Merge pull request #29 from dhalbert/avoid-longint
Avoid a longint as a cmd arg
2 parents 5ad33e4 + f143cf7 commit dd0fe85

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

adafruit_sdcard.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ def _init_card_v2(self, card):
195195
time.sleep(.050)
196196
self._cmd(card, 58, 0, 0xfd, response_buf=ocr, data_block=False)
197197
self._cmd(card, 55, 0, 0x65)
198-
if self._cmd(card, 41, 0x40000000, 0x77) == 0:
198+
# On non-longint builds, we cannot use 0x40000000 directly as the arg
199+
# so break it into bytes, which are interpreted by self._cmd().
200+
if self._cmd(card, 41, b'\x40\x00\x00\x00', 0x77) == 0:
199201
self._cmd(card, 58, 0, 0xfd, response_buf=ocr, data_block=False)
200202

201203
# Check for block addressing
@@ -226,18 +228,24 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait
226228
227229
:param busio.SPI card: The locked SPI bus.
228230
:param int cmd: The command number.
229-
:param int arg: The command argument.
231+
:param int|buf(4) arg: The command argument
230232
:param int crc: The crc to allow the card to verify the command and argument.
231233
:param bytearray response_buf: Buffer to read a data block response into.
232234
:param bool data_block: True if the response data is in a data block.
233235
"""
234236
# create and send the command
235237
buf = self._cmdbuf
236238
buf[0] = 0x40 | cmd
237-
buf[1] = (arg >> 24) & 0xff
238-
buf[2] = (arg >> 16) & 0xff
239-
buf[3] = (arg >> 8) & 0xff
240-
buf[4] = arg & 0xff
239+
if isinstance(arg, int):
240+
buf[1] = (arg >> 24) & 0xff
241+
buf[2] = (arg >> 16) & 0xff
242+
buf[3] = (arg >> 8) & 0xff
243+
buf[4] = arg & 0xff
244+
elif len(arg) == 4:
245+
# arg can be a 4-byte buf
246+
buf[1:5] = arg
247+
else:
248+
raise ValueError()
241249

242250
if (crc == 0):
243251
buf[5] = calculate_crc(buf[:-1])

0 commit comments

Comments
 (0)