1# Test for sdcard block protocol 2# Peter hinch 30th Jan 2016 3import os, sdcard, machine 4 5 6def sdtest(): 7 spi = machine.SPI(1) 8 spi.init() # Ensure right baudrate 9 sd = sdcard.SDCard(spi, machine.Pin.board.X21) # Compatible with PCB 10 vfs = os.VfsFat(sd) 11 os.mount(vfs, "/fc") 12 print("Filesystem check") 13 print(os.listdir("/fc")) 14 15 line = "abcdefghijklmnopqrstuvwxyz\n" 16 lines = line * 200 # 5400 chars 17 short = "1234567890\n" 18 19 fn = "/fc/rats.txt" 20 print() 21 print("Multiple block read/write") 22 with open(fn, "w") as f: 23 n = f.write(lines) 24 print(n, "bytes written") 25 n = f.write(short) 26 print(n, "bytes written") 27 n = f.write(lines) 28 print(n, "bytes written") 29 30 with open(fn, "r") as f: 31 result1 = f.read() 32 print(len(result1), "bytes read") 33 34 fn = "/fc/rats1.txt" 35 print() 36 print("Single block read/write") 37 with open(fn, "w") as f: 38 n = f.write(short) # one block 39 print(n, "bytes written") 40 41 with open(fn, "r") as f: 42 result2 = f.read() 43 print(len(result2), "bytes read") 44 45 os.umount("/fc") 46 47 print() 48 print("Verifying data read back") 49 success = True 50 if result1 == "".join((lines, short, lines)): 51 print("Large file Pass") 52 else: 53 print("Large file Fail") 54 success = False 55 if result2 == short: 56 print("Small file Pass") 57 else: 58 print("Small file Fail") 59 success = False 60 print() 61 print("Tests", "passed" if success else "failed") 62