Skip to content

File syn_sd.h

FileList > drivers > syn_sd.h

Go to the source code of this file

SD card SPI block driver. More...

  • #include "../common/syn_defs.h"
  • #include "../port/syn_port_spi.h"
  • #include <stdbool.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_SD
SD card driver handle (caller-owned, zero heap allocation).

Public Types

Type Name
enum SYN_SD_Type
SD card type, detected automatically during syn_sd_init() .

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).

Macros

Type Name
define SYN_SD_SECTOR_SIZE 512u

Detailed Description

Zero-allocation driver for SD/SDHC/SDXC cards over hardware SPI. Implements the SD simplified SPI spec: raw 512-byte sector read/write. Uses syn_port_spi.h for all bus transfers; CS is managed by the driver.

static SYN_SD sd;
if (syn_sd_init(&sd, 0, MY_SD_CS_PIN) == SYN_OK) {
    uint8_t buf[512];
    syn_sd_read(&sd, 0, buf);
    syn_sd_write(&sd, 1, buf);
    syn_sd_sync(&sd);
}

The SPI bus must support Mode 0 (CPOL=0, CPHA=0). When tx_buf is NULL, syn_port_spi_transfer() must drive MOSI HIGH (0xFF), as required by the SD simplified spec during receive phases.

Public Types Documentation

enum SYN_SD_Type

SD card type, detected automatically during syn_sd_init() .

enum SYN_SD_Type {
    SYN_SD_UNKNOWN = 0,
    SYN_SD_SDSC = 1,
    SYN_SD_SDHC = 2
};


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.


Macro Definition Documentation

define SYN_SD_SECTOR_SIZE

#define SYN_SD_SECTOR_SIZE `512u`

Fixed sector size in bytes. All SD cards expose 512-byte sectors in SPI mode.



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