Skip to content

Commit

Permalink
Merge pull request #29 from dhalbert/avoid-longint
Browse files Browse the repository at this point in the history
Avoid a longint as a cmd arg
  • Loading branch information
dhalbert committed Nov 18, 2019
2 parents 5ad33e4 + f143cf7 commit dd0fe85
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions adafruit_sdcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ def _init_card_v2(self, card):
time.sleep(.050)
self._cmd(card, 58, 0, 0xfd, response_buf=ocr, data_block=False)
self._cmd(card, 55, 0, 0x65)
if self._cmd(card, 41, 0x40000000, 0x77) == 0:
# On non-longint builds, we cannot use 0x40000000 directly as the arg
# so break it into bytes, which are interpreted by self._cmd().
if self._cmd(card, 41, b'\x40\x00\x00\x00', 0x77) == 0:
self._cmd(card, 58, 0, 0xfd, response_buf=ocr, data_block=False)

# Check for block addressing
Expand Down Expand Up @@ -226,18 +228,24 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait
:param busio.SPI card: The locked SPI bus.
:param int cmd: The command number.
:param int arg: The command argument.
:param int|buf(4) arg: The command argument
:param int crc: The crc to allow the card to verify the command and argument.
:param bytearray response_buf: Buffer to read a data block response into.
:param bool data_block: True if the response data is in a data block.
"""
# create and send the command
buf = self._cmdbuf
buf[0] = 0x40 | cmd
buf[1] = (arg >> 24) & 0xff
buf[2] = (arg >> 16) & 0xff
buf[3] = (arg >> 8) & 0xff
buf[4] = arg & 0xff
if isinstance(arg, int):
buf[1] = (arg >> 24) & 0xff
buf[2] = (arg >> 16) & 0xff
buf[3] = (arg >> 8) & 0xff
buf[4] = arg & 0xff
elif len(arg) == 4:
# arg can be a 4-byte buf
buf[1:5] = arg
else:
raise ValueError()

if (crc == 0):
buf[5] = calculate_crc(buf[:-1])
Expand Down

0 comments on commit dd0fe85

Please sign in to comment.