|
6 | 6 |
|
7 | 7 |
|
8 | 8 | def sdtest(): |
| 9 | + """this test requires specific hardware, so is not run by CI""" |
9 | 10 | spi = machine.SPI(1) |
10 | 11 | spi.init() # Ensure right baudrate |
11 | 12 | sd = sdcard.SDCard(spi, machine.Pin.board.X21) # Compatible with PCB |
@@ -61,3 +62,48 @@ def sdtest(): |
61 | 62 | success = False |
62 | 63 | print() |
63 | 64 | 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