Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created August 10, 2020 06:09
Show Gist options
  • Select an option

  • Save anecdata/f59901af2922ad2279971a57894c2a24 to your computer and use it in GitHub Desktop.

Select an option

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 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