SD Card Driver
Status | Requirements Review |
Owner | @Zhilin Jiang |
|---|---|
Approver | @Ravi Shah @Lakshay Gupta |
Due date | Nov 1, 2025 |
GitHub |
Due Dates:
SPI Peripheral Setup - 10/4/2025
Basic SD Read/Write -
Full Implementation (RTOS) -
Documentation -
Description/Purpose
The Leader System-On-Module (LSOM) used on several critical boards on the car has a SPI-based SD card reader connected to the STM32 microcontroller on the board. This driver provides a thread-safe, robust interface to interact with this SD card reader.
Requirements
Basic functionality - initialization, reading, writing from SD card
Formatting?
Thread safety - multiple tasks should be able to read/write the SD card without causing data races or corruption
Application Note
Steps for Debug
Test for SPI Peripheral Setup using logic analyzer:
SPI2 GPIO Configuration
SPI2_SCK: PB10
SPI2_MISO: PC2
SPI2_MOSI: PC3CS: PB12
Test for command send
Test for SD_Init()
Requirements:
To enter SPI mode, the host must send ≥74 dummy clocks with CS = high
Send CMD0, puts the SD card into idle state (Expected response = 0x01 = idle)
- Send CMD0
Send CMD8 (CHECK VOLTAGE / VERSION); send argument 0x000001AA and expect the same pattern back.
- Send CMD8
CMD8 checks:
Voltage range support (card must support 2.7–3.6V)
SD version (SDv2 (R1 response is 0x01) vs SDv1/MMC (R1 response is 0x05))
Echo-back check (lower 12 bits must be 0x1AA)
When you send CMD8, the SD card returns R7 (r7[4])
r7[0] = 0x00 (always)
r7[1] = 0x00 or 0x01 something (voltage) -> supports 2.7-3.6V
r7[2] = 0x01 (upper byte of 0x1AA) -> match echo-back patter
r7[3] = 0xAA (lower byte of 0x1AA) -> match echo-back patter
Loop sending ACMD41 until the card leaves idle
Send CMD55 (prefix command), Send ACMD41 (init command) until card returns: 0x00, meaning: “Initialization complete. Ready.”Send CMD58 to read Operating Conditions Register (supported voltage)
Meaning for different errors:
Error | Meaning |
|---|---|
-1 | CMD0 failed (card not idle) |
-2 | Failed reading R7 bytes |
-3 | R7 pattern wrong (bad CMD8) |
-4 | Illegal command → SD v1 not supported |
-5 | Unexpected CMD8 response |
-6 | CMD55 failed |
-7 | ACMD41 didn’t return ready |
-8 | CMD58 failed |
-9 | OCR read failed |
Command Response:
Command | Response Type | Meaning |
|---|---|---|
CMD0 | R1 | Only 1 byte |
CMD8 | R7 | R1 + 4 bytes |
CMD55 | R1 | 1 byte |
CMD41 | R1 | 1 byte |
CMD58 | R3 | R1 + 4 bytes |
CMD9 | R2 | 2 bytes |
CMD17 | R1 + Data | R1, then token, then 512 bytes |
Test for FATFS