Skip to content

Commit ab4a935

Browse files
committed
sdcard: Adding tests for csd parsing.
Signed-off-by: Ben Wynn <bwynn@glowie.com>
1 parent a7c805c commit ab4a935

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

micropython/drivers/storage/sdcard/sdcard.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,23 @@ def init_card(self, baudrate):
9292

9393
# get the number of sectors
9494
# CMD9: response R2 (R1 byte + 16-byte block read)
95+
csd = self.get_csd()
96+
self.parse_csd(csd)
97+
98+
# CMD16: set block length to 512 bytes
99+
if self.cmd(16, 512, 0) != 0:
100+
raise OSError("can't set 512 block size")
101+
102+
# set to high data rate now that it's initialised
103+
self.init_spi(baudrate)
104+
105+
def get_csd(self):
95106
if self.cmd(9, 0, 0, 0, False) != 0:
96107
raise OSError("no response from SD card")
97108
csd = bytearray(16)
98109
self.readinto(csd)
110+
111+
def parse_csd(self, csd):
99112
if csd[0] & 0xC0 == 0x40: # CSD version 2.0
100113
self.sectors = ((csd[7] << 16 | csd[8] << 8 | csd[9]) + 1) * 1024
101114
elif csd[0] & 0xC0 == 0x00: # CSD version 1.0 (old, <=2GB)
@@ -106,14 +119,6 @@ def init_card(self, baudrate):
106119
self.sectors = capacity // 512
107120
else:
108121
raise OSError("SD card CSD format not supported")
109-
# print('sectors', self.sectors)
110-
111-
# CMD16: set block length to 512 bytes
112-
if self.cmd(16, 512, 0) != 0:
113-
raise OSError("can't set 512 block size")
114-
115-
# set to high data rate now that it's initialised
116-
self.init_spi(baudrate)
117122

118123
def init_card_v1(self):
119124
for i in range(_CMD_TIMEOUT):

micropython/drivers/storage/sdcard/sdtest.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
def sdtest():
9+
"""this test requires specific hardware, so is not run by CI"""
910
spi = machine.SPI(1)
1011
spi.init() # Ensure right baudrate
1112
sd = sdcard.SDCard(spi, machine.Pin.board.X21) # Compatible with PCB
@@ -61,3 +62,48 @@ def sdtest():
6162
success = False
6263
print()
6364
print("Tests", "passed" if success else "failed")
65+
66+
67+
class MockSDCard(sdcard.SDCard):
68+
"""instantiates a sdcard object without talking to hardware"""
69+
70+
def __init__(self, spi, cs, baudrate=100_000):
71+
super().__init__(spi, cs, baudrate=baudrate)
72+
73+
def init_card(self, baudrate):
74+
"""yeah, this is where we're just going to skip the whole hardware initalization thing"""
75+
self.baudrate = baudrate
76+
77+
78+
def test_parse_csd(mockSD):
79+
"""test the parse_csd function"""
80+
csd_tests = [
81+
{
82+
"name": "64g card",
83+
"sectors": 122191872,
84+
"csd": bytearray(b"@\x0e\x002[Y\x00\x01\xd2\x1f\x7f\x80\n@\x00S"),
85+
},
86+
{
87+
"name": "128g card",
88+
"sectors": 244277248,
89+
"csd": bytearray(b"@\x0e\x002[Y\x00\x03\xa3\xd7\x7f\x80\n@\x00A"),
90+
},
91+
]
92+
93+
for test in csd_tests:
94+
mockSD.parse_csd(test["csd"])
95+
sectors = mockSD.ioctl(4, None)
96+
assert sectors == test["sectors"], "Failed to parse csd for test {}, {} != {}".format(
97+
test["name"], sectors, test["sectors"]
98+
)
99+
print("CSD Passed: {}".format(test["name"]))
100+
101+
102+
def run_tests():
103+
print("Running tests for SDCard...")
104+
mockSD = MockSDCard(None, None)
105+
test_parse_csd(mockSD)
106+
print("SDCard tests done")
107+
108+
109+
run_tests()

0 commit comments

Comments
 (0)