Skip to content

File syn_sd.c

FileList > drivers > 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.

SYN_Status syn_sd_init (
    SYN_SD * sd,
    uint8_t spi_bus,
    SYN_GPIO_Pin cs
) 

Performs power-up clocking, CMD0/CMD8/ACMD41 init, type detection via CMD58, and CSD parsing for capacity.

Parameters:

  • sd Pointer to a caller-owned SYN_SD struct.
  • spi_bus SPI bus index passed to syn_port_spi_*.
  • cs Chip-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).

SYN_Status syn_sd_read (
    const SYN_SD * sd,
    uint32_t sector,
    uint8_t * buf
) 

Parameters:

  • sd Initialized SD handle.
  • sector Zero-based sector index.
  • buf Output 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.

uint32_t syn_sd_sectors (
    const SYN_SD * sd
) 

Parameters:

  • sd Initialized 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).

SYN_Status syn_sd_sync (
    const SYN_SD * sd
) 

Parameters:

  • sd Initialized SD handle.

Returns:

SYN_OK if card is idle and error-free, SYN_ERROR otherwise.


function syn_sd_type

Return the detected card type.

SYN_SD_Type syn_sd_type (
    const SYN_SD * sd
) 

Parameters:

  • sd Initialized SD handle.

Returns:

SYN_SD_SDSC or SYN_SD_SDHC.


function syn_sd_write

Write one 512-byte sector to the SD card (CMD24).

SYN_Status syn_sd_write (
    const SYN_SD * sd,
    uint32_t sector,
    const uint8_t * buf
) 

Parameters:

  • sd Initialized SD handle.
  • sector Zero-based sector index.
  • buf Data 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.

static uint8_t sd_cmd (
    const SYN_SD * sd,
    uint8_t cmd,
    uint32_t arg,
    uint8_t crc
) 

Parameters:

  • sd SD card instance.
  • cmd Command index (e.g. CMD0).
  • arg 32-bit argument.
  • crc CRC byte.

Returns:

R1 response byte.


function sd_read_csd

Read the CSD register and parse sector count.

static SYN_Status sd_read_csd (
    SYN_SD * sd
) 

Parameters:

  • sd SD card instance.

Returns:

SYN_OK on success.


function sd_wait_ready

Wait until the SD card is no longer busy (MISO = 0xFF).

static bool sd_wait_ready (
    const SYN_SD * sd
) 

Parameters:

  • sd SD card instance.

Returns:

true if card became ready within timeout.


function sd_xfer

Transfer one byte over SPI and return the received byte.

static uint8_t sd_xfer (
    const SYN_SD * sd,
    uint8_t out
) 

Parameters:

  • sd SD card instance.
  • out Byte to send.

Returns:

Received byte.


Macro Definition Documentation

define SD_ACMD41

#define SD_ACMD41 `41u`

SD_SEND_OP_COND (app)


define SD_ACMD41_RETRIES

#define SD_ACMD41_RETRIES `1000u`

Max ACMD41 init retries


define SD_BUSY_RETRIES

#define SD_BUSY_RETRIES `2000u`

Max busy wait retries


define SD_CMD0

#define SD_CMD0 `0u`

GO_IDLE_STATE


define SD_CMD13

#define SD_CMD13 `13u`

SEND_STATUS


define SD_CMD16

#define SD_CMD16 `16u`

SET_BLOCKLEN


define SD_CMD17

#define SD_CMD17 `17u`

READ_SINGLE_BLOCK


define SD_CMD24

#define SD_CMD24 `24u`

WRITE_BLOCK


define SD_CMD55

#define SD_CMD55 `55u`

APP_CMD prefix


define SD_CMD58

#define SD_CMD58 `58u`

READ_OCR


define SD_CMD8

#define SD_CMD8 `8u`

SEND_IF_COND


define SD_CMD9

#define SD_CMD9 `9u`

SEND_CSD


define SD_R1_ERR_MSK

#define SD_R1_ERR_MSK `0xFEu`

Any bit other than IDLE = error


define SD_R1_IDLE

#define SD_R1_IDLE `0x01u`

Card in idle state during init


define SD_R1_ILLCMD

#define SD_R1_ILLCMD `0x04u`

Illegal command (SDSC: no CMD8 support)


define SD_R1_POLL_RETRIES

#define SD_R1_POLL_RETRIES `8u`

Max R1 polling attempts


define SD_R1_READY

#define SD_R1_READY `0x00u`

No errors, card ready


define SD_R1_TIMEOUT

#define SD_R1_TIMEOUT `0xFFu`

No response from card


define SD_TOKEN_ACCEPTED

#define SD_TOKEN_ACCEPTED `0x05u`

Write data response: (xxx0_0101)


define SD_TOKEN_RETRIES

#define SD_TOKEN_RETRIES `2000u`

Max token wait retries


define SD_TOKEN_START

#define SD_TOKEN_START `0xFEu`

Start block for single read/write



The documentation for this class was generated from the following file src/syntropic/drivers/syn_sd.c