From 7414c5fef6c06fc41c5af2b2bf7d4c168fd01fc4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Wed, 13 Nov 2019 17:00:41 -0500 Subject: [PATCH 1/2] Avoid a longint as a cmd arg --- adafruit_sdcard.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/adafruit_sdcard.py b/adafruit_sdcard.py index 465e9f6..1c0dc9a 100644 --- a/adafruit_sdcard.py +++ b/adafruit_sdcard.py @@ -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 @@ -226,7 +228,7 @@ 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. @@ -234,10 +236,16 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait # 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("bad arg") if (crc == 0): buf[5] = calculate_crc(buf[:-1]) From f143cf70ef78f97644c2cceef95c4848a68c1342 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 17 Nov 2019 00:07:44 -0500 Subject: [PATCH 2/2] don't bother with ValueError message, since it reflects an internal coding mistake --- adafruit_sdcard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_sdcard.py b/adafruit_sdcard.py index 1c0dc9a..37be9a1 100644 --- a/adafruit_sdcard.py +++ b/adafruit_sdcard.py @@ -245,7 +245,7 @@ def _cmd(self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait # arg can be a 4-byte buf buf[1:5] = arg else: - raise ValueError("bad arg") + raise ValueError() if (crc == 0): buf[5] = calculate_crc(buf[:-1])