Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created August 10, 2020 06:09
Show Gist options
  • Save anecdata/f59901af2922ad2279971a57894c2a24 to your computer and use it in GitHub Desktop.
Save anecdata/f59901af2922ad2279971a57894c2a24 to your computer and use it in GitHub Desktop.
SD Card read >- 1024 characters
# variation on https://github.com/gmeader/pyportal/blob/master/bug_demo.py
import adafruit_sdcard
import busio
import digitalio
import board
import storage
import os
# Connect to the card and mount the filesystem.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
cs = digitalio.DigitalInOut(board.SD_CS)
sdcard = adafruit_sdcard.SDCard(spi, cs)
vfs = storage.VfsFat(sdcard)
storage.mount(vfs, "/sd")
# pre-existing files on CIRCUITPY root
for filename in ('/1023.txt', '/1024.txt', '/1025.txt'):
sdfilename = ''.join(('/sd', filename))
print(filename, os.stat(filename)[6], end=' ')
f = open(filename)
fdata = f.read()
print(len(fdata))
f.close()
fsd = open(sdfilename, 'w')
fsd.write(fdata)
fsd.close()
print(sdfilename, os.stat(sdfilename)[6], end=' ')
fsd = open(sdfilename)
fdata = fsd.read() # 1024 character file: OSError: [Errno 5] Input/output error
print(len(fdata))
fsd.close()
@anecdata
Copy link
Author

anecdata commented Aug 10, 2020

Adafruit PyPortal with samd51j20
CircuitPython 6.0.0-alpha.1 on 2020-07-03
CircuitPython Library Bundle 20200810

code.py output:
/1023.txt 1023 1023
/sd/1023.txt 1023 1023
/1024.txt 1024 1024
/sd/1024.txt 1024 Traceback (most recent call last):
  File "code.py", line 33, in <module>
OSError: [Errno 5] Input/output error

@anecdata
Copy link
Author

anecdata commented Aug 18, 2020

re-testing on Adafruit CircuitPython 6.0.0-alpha.2-296-g6e5c2b3fe on 2020-08-17; Adafruit PyPortal with samd51j20
Lib=6.x-mpy-20200811

quirky sequence...

Press any key to enter the REPL. Use CTRL-D to reload.soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
/1023.txt 1023 1023
/sd/1023.txt 1023 1023
/1024.txt 1024 1024
Traceback (most recent call last):
  File "code.py", line 29, in <module>
OSError: [Errno 5] Input/output error
OSError: 



Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 6.0.0-alpha.2-296-g6e5c2b3fe on 2020-08-17; Adafruit PyPortal with samd51j20
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
  File "code.py", line 13, in <module>
  File "adafruit_sdcard.py", line 116, in __init__
  File "adafruit_sdcard.py", line 177, in _init_card
  File "adafruit_sdcard.py", line 145, in _init_card
OSError: no SD card

ejected and re-seated the SD card here

Press any key to enter the REPL. Use CTRL-D to reload.soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
/1023.txt 1023 1023
/sd/1023.txt 1023 1023
/1024.txt 1024 1024

here it seemed to read and write the 1024-character file from CIRCUITPY, but then hangs here on the SD write close until I ctrl-c (but oddly there's no evidence of a keyboard interrupt in the trace)

Traceback (most recent call last):
  File "code.py", line 29, in <module>
OSError: [Errno 5] Input/output error
OSError: 

@anecdata
Copy link
Author

anecdata commented Aug 18, 2020

Backing up to 6.0.0-alpha.1 to re-establish baseline...

Same behavior as the newer S3 build Now seems to read and write the 1024 from CIRCUITPY, but hangs on the SD write close until ctrl-c, then finds no SD card until card is re-seated, then hangs again on 1024 close until ctrl-c.

I had reformatted the SD card during the initial testing.

@anecdata
Copy link
Author

anecdata commented Aug 18, 2020

Content of the 1024.txt file (one line with 1024 numeric characters, no line ending):
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234
A file with 1024 characters including the line ending has the same behavior. No difference between read() and readline().

@anecdata
Copy link
Author

anecdata commented Aug 18, 2020

Processing many 1023-character files is not a problem. With a 1024-character file, it hangs as above. With a 1025-character file, it doesn't hang but still errors on the SD write close.

Reset button, power cycle make no difference.

I can't account for the difference between the initial observations, and today's. Even reverting to 6.x-mpy-20200810 keeps behavior same as the other tests today.

Some change in the state of the card? Trying new card, newly-formatted with official utility (macOS)...

New card works fine:

code.py output:
/1023.txt 1023 1023
/sd/1023.txt 1023 1023
/1024.txt 1024 1024
/sd/1024.txt 1024 1024
/1025.txt 1025 1025
/sd/1025.txt 1025 1025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment