SPI SD-Card driver

Driver for reading and writing sd-cards via spi interface. More...

Detailed Description

Driver for reading and writing sd-cards via spi interface.

Files

file  sdcard_spi.h
 Public interface for the sdcard_spi driver.
 
file  sdcard_spi_internal.h
 Interface for issuing commands on sd-cards via SPI.
 

Data Structures

struct  cid_t
 CID register see section 5.2 in SD-Spec v5.00. More...
 
struct  csd_v1_t
 CSD register with csd structure version 1.0 see section 5.3.2 in SD-Spec v5.00. More...
 
struct  csd_v2_t
 CSD register with csd structure version 2.0 see section 5.3.3 in SD-Spec v5.00. More...
 
union  csd_t
 CSD register (see section 5.3 in SD-Spec v5.00) More...
 
struct  sd_status_t
 SD status register (see section 4.10.2 in SD-Spec v5.00) More...
 
struct  sdcard_spi_params_t
 sdcard_spi device params More...
 
struct  sdcard_spi_t
 Device descriptor for sdcard_spi. More...
 

Macros

#define SD_HC_BLOCK_SIZE   (512)
 size of a single block on SDHC cards
 
#define SDCARD_SPI_INIT_ERROR   (-1)
 returned on failed init
 
#define SDCARD_SPI_OK   (0)
 returned on successful init
 
#define SD_SIZE_OF_OID   2
 OID (OEM/application ID field in CID reg)
 
#define SD_SIZE_OF_PNM   5
 PNM (product name field in CID reg)
 

Enumerations

enum  sd_version_t { SD_V2, SD_V1, MMC_V3, SD_UNKNOWN }
 version type of SD-card More...
 
enum  sd_rw_response_t {
  SD_RW_OK = 0, SD_RW_NO_TOKEN, SD_RW_TIMEOUT, SD_RW_RX_TX_ERROR,
  SD_RW_WRITE_ERROR, SD_RW_CRC_MISMATCH, SD_RW_NOT_SUPPORTED
}
 sdcard_spi r/w-operation return values More...
 

Functions

int sdcard_spi_init (sdcard_spi_t *card, const sdcard_spi_params_t *params)
 Initializes the sd-card with the given parameters in sdcard_spi_t structure. More...
 
int sdcard_spi_read_blocks (sdcard_spi_t *card, int blockaddr, char *data, int blocksize, int nblocks, sd_rw_response_t *state)
 Reads data blocks (usually multiples of 512 Bytes) from card to buffer. More...
 
int sdcard_spi_write_blocks (sdcard_spi_t *card, int blockaddr, const char *data, int blocksize, int nblocks, sd_rw_response_t *state)
 Writes data blocks (usually multiples of 512 Bytes) from buffer to card. More...
 
uint64_t sdcard_spi_get_capacity (sdcard_spi_t *card)
 Gets the capacity of the card. More...
 

Enumeration Type Documentation

◆ sd_rw_response_t

sdcard_spi r/w-operation return values

Enumerator
SD_RW_OK 

no error

SD_RW_NO_TOKEN 

no token was received (on block read)

SD_RW_TIMEOUT 

cmd timed out (not-busy-state wasn't entered)

SD_RW_RX_TX_ERROR 

error while performing SPI read/write

SD_RW_WRITE_ERROR 

data-packet response indicates error

SD_RW_CRC_MISMATCH 

CRC-mismatch of received data.

SD_RW_NOT_SUPPORTED 

operation not supported on used card

Definition at line 161 of file sdcard_spi.h.

◆ sd_version_t

version type of SD-card

Enumerator
SD_V2 

SD version 2.

SD_V1 

SD version 1.

MMC_V3 

MMC version 3.

SD_UNKNOWN 

SD-version unknown.

Definition at line 151 of file sdcard_spi.h.

Function Documentation

◆ sdcard_spi_get_capacity()

uint64_t sdcard_spi_get_capacity ( sdcard_spi_t card)

Gets the capacity of the card.

Parameters
[in]cardInitialized sd-card struct
Returns
capacity of the card in bytes

◆ sdcard_spi_init()

int sdcard_spi_init ( sdcard_spi_t card,
const sdcard_spi_params_t params 
)

Initializes the sd-card with the given parameters in sdcard_spi_t structure.

The init procedure also takes care of initializing the spi peripheral to master mode and performing all neccecary steps to set the sd-card to spi-mode. Reading the CID and CSD registers is also done within this routine and their values are copied to the given sdcard_spi_t struct.

Parameters
[out]cardthe device descriptor
[in]paramsparameters for this device (pins and spi device are initialized by this driver)
Returns
0 if the card could be initialized successfully
false if an error occured while initializing the card

◆ sdcard_spi_read_blocks()

int sdcard_spi_read_blocks ( sdcard_spi_t card,
int  blockaddr,
char *  data,
int  blocksize,
int  nblocks,
sd_rw_response_t state 
)

Reads data blocks (usually multiples of 512 Bytes) from card to buffer.

Parameters
[in]cardInitialized sd-card struct
[in]blockaddrStart adress to read from. Independet of the actual adressing scheme of the used card the adress needs to be given as block address (e.g. 0, 1, 2... NOT: 0, 512... ). The driver takes care of mapping to byte adressing if needed.
[out]dataBuffer to store the read data in. The user is responsible for providing a suitable buffer size.
[in]blocksizeSize of data blocks. For now only 512 byte blocks are supported because only older (SDSC) cards support variable blocksizes anyway. With SDHC/SDXC-cards this is always fixed to 512 bytes. SDSC cards are automatically forced to use 512 byte as blocksize by the init procedure.
[in]nblocksNumber of blocks to read
[out]stateContains information about the error state if something went wrong (if return value is lower than nblocks).
Returns
number of successfully read blocks (0 if no block was read).

◆ sdcard_spi_write_blocks()

int sdcard_spi_write_blocks ( sdcard_spi_t card,
int  blockaddr,
const char *  data,
int  blocksize,
int  nblocks,
sd_rw_response_t state 
)

Writes data blocks (usually multiples of 512 Bytes) from buffer to card.

Parameters
[in]cardInitialized sd-card struct
[in]blockaddrStart adress to read from. Independet of the actual adressing scheme of the used card the adress needs to be given as block address (e.g. 0, 1, 2... NOT: 0, 512... ). The driver takes care of mapping to byte adressing if needed.
[out]dataBuffer that contains the data to be sent.
[in]blocksizeSize of data blocks. For now only 512 byte blocks are supported because only older (SDSC) cards support variable blocksizes anyway. With SDHC/SDXC-cards this is always fixed to 512 bytes. SDSC cards are automatically forced to use 512 byte as blocksize by the init procedure.
[in]nblocksNumber of blocks to write
[out]stateContains information about the error state if something went wrong (if return value is lower than nblocks).
Returns
number of successfully written blocks (0 if no block was written).