File syn_sd.c¶
Go to the source code of this file
SD card SPI block driver implementation. More...
#include "../util/syn_assert.h"#include "syn_sd.h"#include <string.h>
Public Functions¶
| Type | Name |
|---|---|
| SYN_Status | syn_sd_init (SYN_SD * sd, uint8_t spi_bus, SYN_GPIO_Pin cs) Initialize the SD card over SPI. |
| SYN_Status | syn_sd_read (const SYN_SD * sd, uint32_t sector, uint8_t * buf) Read one 512-byte sector from the SD card (CMD17). |
| uint32_t | syn_sd_sectors (const SYN_SD * sd) Return total sector count parsed from the CSD register. |
| SYN_Status | syn_sd_sync (const SYN_SD * sd) Flush the write pipeline — wait until card is idle (CMD13). |
| SYN_SD_Type | syn_sd_type (const SYN_SD * sd) Return the detected card type. |
| SYN_Status | syn_sd_write (const SYN_SD * sd, uint32_t sector, const uint8_t * buf) Write one 512-byte sector to the SD card (CMD24). |
Public Static Functions¶
| Type | Name |
|---|---|
| uint8_t | sd_cmd (const SYN_SD * sd, uint8_t cmd, uint32_t arg, uint8_t crc) Send a 6-byte SD command and poll for R1 response. |
| SYN_Status | sd_read_csd (SYN_SD * sd) Read the CSD register and parse sector count. |
| bool | sd_wait_ready (const SYN_SD * sd) Wait until the SD card is no longer busy (MISO = 0xFF). |
| uint8_t | sd_xfer (const SYN_SD * sd, uint8_t out) Transfer one byte over SPI and return the received byte. |
Macros¶
| Type | Name |
|---|---|
| define | SD_ACMD41 41u |
| define | SD_ACMD41_RETRIES 1000u |
| define | SD_BUSY_RETRIES 2000u |
| define | SD_CMD0 0u |
| define | SD_CMD13 13u |
| define | SD_CMD16 16u |
| define | SD_CMD17 17u |
| define | SD_CMD24 24u |
| define | SD_CMD55 55u |
| define | SD_CMD58 58u |
| define | SD_CMD8 8u |
| define | SD_CMD9 9u |
| define | SD_R1_ERR_MSK 0xFEu |
| define | SD_R1_IDLE 0x01u |
| define | SD_R1_ILLCMD 0x04u |
| define | SD_R1_POLL_RETRIES 8u |
| define | SD_R1_READY 0x00u |
| define | SD_R1_TIMEOUT 0xFFu |
| define | SD_TOKEN_ACCEPTED 0x05u |
| define | SD_TOKEN_RETRIES 2000u |
| define | SD_TOKEN_START 0xFEu |
Detailed Description¶
Implements the SD simplified SPI spec: * CMD0 (GO_IDLE_STATE) reset to SPI mode * CMD8 (SEND_IF_COND) SDHC-capable detection * CMD9 (SEND_CSD) capacity via CSD v1/v2 parse * CMD13 (SEND_STATUS) sync / flush * CMD16 (SET_BLOCKLEN) SDSC: force 512-byte blocks * CMD17 (READ_SINGLE_BLOCK) * CMD24 (WRITE_BLOCK) * CMD55 (APP_CMD) ACMD prefix * CMD58 (READ_OCR) card type confirmation * ACMD41 (SD_SEND_OP_COND) card initialization
CRC: CMD0/CMD8 use hardcoded CRC7 constants. All other commands and data blocks use 0xFF dummy bytes (CRC mode off by default in SPI).
Public Functions Documentation¶
function syn_sd_init¶
Initialize the SD card over SPI.
Performs power-up clocking, CMD0/CMD8/ACMD41 init, type detection via CMD58, and CSD parsing for capacity.
Parameters:
sdPointer to a caller-owned SYN_SD struct.spi_busSPI bus index passed to syn_port_spi_*.csChip-select GPIO pin (active-low).
Returns:
SYN_OK on success, SYN_ERROR if no card or init failed.
function syn_sd_read¶
Read one 512-byte sector from the SD card (CMD17).
Parameters:
sdInitialized SD handle.sectorZero-based sector index.bufOutput buffer — caller must provide at least 512 bytes.
Returns:
SYN_OK on success, SYN_ERROR on timeout or card error.
function syn_sd_sectors¶
Return total sector count parsed from the CSD register.
Parameters:
sdInitialized SD handle.
Returns:
Number of 512-byte sectors on the card.
function syn_sd_sync¶
Flush the write pipeline — wait until card is idle (CMD13).
Parameters:
sdInitialized SD handle.
Returns:
SYN_OK if card is idle and error-free, SYN_ERROR otherwise.
function syn_sd_type¶
Return the detected card type.
Parameters:
sdInitialized SD handle.
Returns:
SYN_SD_SDSC or SYN_SD_SDHC.
function syn_sd_write¶
Write one 512-byte sector to the SD card (CMD24).
Parameters:
sdInitialized SD handle.sectorZero-based sector index.bufData to write — must be exactly 512 bytes.
Returns:
SYN_OK on success, SYN_ERROR on card rejection or timeout.
Public Static Functions Documentation¶
function sd_cmd¶
Send a 6-byte SD command and poll for R1 response.
Parameters:
sdSD card instance.cmdCommand index (e.g. CMD0).arg32-bit argument.crcCRC byte.
Returns:
R1 response byte.
function sd_read_csd¶
Read the CSD register and parse sector count.
Parameters:
sdSD card instance.
Returns:
SYN_OK on success.
function sd_wait_ready¶
Wait until the SD card is no longer busy (MISO = 0xFF).
Parameters:
sdSD card instance.
Returns:
true if card became ready within timeout.
function sd_xfer¶
Transfer one byte over SPI and return the received byte.
Parameters:
sdSD card instance.outByte to send.
Returns:
Received byte.
Macro Definition Documentation¶
define SD_ACMD41¶
SD_SEND_OP_COND (app)
define SD_ACMD41_RETRIES¶
Max ACMD41 init retries
define SD_BUSY_RETRIES¶
Max busy wait retries
define SD_CMD0¶
GO_IDLE_STATE
define SD_CMD13¶
SEND_STATUS
define SD_CMD16¶
SET_BLOCKLEN
define SD_CMD17¶
READ_SINGLE_BLOCK
define SD_CMD24¶
WRITE_BLOCK
define SD_CMD55¶
APP_CMD prefix
define SD_CMD58¶
READ_OCR
define SD_CMD8¶
SEND_IF_COND
define SD_CMD9¶
SEND_CSD
define SD_R1_ERR_MSK¶
Any bit other than IDLE = error
define SD_R1_IDLE¶
Card in idle state during init
define SD_R1_ILLCMD¶
Illegal command (SDSC: no CMD8 support)
define SD_R1_POLL_RETRIES¶
Max R1 polling attempts
define SD_R1_READY¶
No errors, card ready
define SD_R1_TIMEOUT¶
No response from card
define SD_TOKEN_ACCEPTED¶
Write data response: (xxx0_0101)
define SD_TOKEN_RETRIES¶
Max token wait retries
define SD_TOKEN_START¶
Start block for single read/write
The documentation for this class was generated from the following file src/syntropic/drivers/syn_sd.c