Driver for reading and writing sd-cards via spi interface. More...
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. | |
file | sdcard_spi_params.h |
Default parameters for sdcard_spi driver. | |
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, uint8_t *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 uint8_t *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... | |
enum sd_rw_response_t |
sdcard_spi r/w-operation return values
Definition at line 161 of file sdcard_spi.h.
enum 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.
uint64_t sdcard_spi_get_capacity | ( | sdcard_spi_t * | card | ) |
Gets the capacity of the card.
[in] | card | Initialized sd-card struct |
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 necessary 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.
[out] | card | the device descriptor |
[in] | params | parameters for this device (pins and spi device are initialized by this driver) |
int sdcard_spi_read_blocks | ( | sdcard_spi_t * | card, |
int | blockaddr, | ||
uint8_t * | data, | ||
int | blocksize, | ||
int | nblocks, | ||
sd_rw_response_t * | state | ||
) |
Reads data blocks (usually multiples of 512 Bytes) from card to buffer.
[in] | card | Initialized sd-card struct |
[in] | blockaddr | Start address to read from. Independent of the actual addressing scheme of the used card the address needs to be given as block address (e.g. 0, 1, 2... NOT: 0, 512... ). The driver takes care of mapping to byte addressing if needed. |
[out] | data | Buffer to store the read data in. The user is responsible for providing a suitable buffer size. |
[in] | blocksize | Size 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] | nblocks | Number of blocks to read |
[out] | state | Contains information about the error state if something went wrong (if return value is lower than nblocks). |
int sdcard_spi_write_blocks | ( | sdcard_spi_t * | card, |
int | blockaddr, | ||
const uint8_t * | data, | ||
int | blocksize, | ||
int | nblocks, | ||
sd_rw_response_t * | state | ||
) |
Writes data blocks (usually multiples of 512 Bytes) from buffer to card.
[in] | card | Initialized sd-card struct |
[in] | blockaddr | Start address to read from. Independent of the actual addressing scheme of the used card the address needs to be given as block address (e.g. 0, 1, 2... NOT: 0, 512... ). The driver takes care of mapping to byte addressing if needed. |
[out] | data | Buffer that contains the data to be sent. |
[in] | blocksize | Size 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] | nblocks | Number of blocks to write |
[out] | state | Contains information about the error state if something went wrong (if return value is lower than nblocks). |