Interface for issuing commands on sd-cards via SPI. More...
Interface for issuing commands on sd-cards via SPI.
For details of the sd card standard and the spi mode refer to "SD Specifications Part 1 Physical Layer Simplified Specification". References to the sd specs in this file apply to Version 5.00 from August 10, 2016. For further details see https://www.sdcard.org/downloads/pls/
Definition in file sdcard_spi_internal.h.
#include "periph/spi.h"
#include "periph/gpio.h"
#include "stdbool.h"
#include "sdcard_spi.h"
#include "timex.h"
#include "board.h"
Go to the source code of this file.
Macros | |
#define | SD_CSD_V2_C_SIZE_BLOCK_MULT 1024 |
memory capacity in bytes = (C_SIZE+1) * SD_CSD_V2_C_SIZE_BLOCK_MULT * BLOCK_LEN | |
#define | SD_CARD_SPI_MODE SPI_MODE_0 |
SPI mode used for SD card. | |
#define | SD_CARD_SPI_SPEED_PREINIT SPI_CLK_400KHZ |
this speed setting is only used while the init procedure is performed | |
#define | SD_CARD_SPI_SPEED_POSTINIT SPI_CLK_10MHZ |
after init procedure is finished the driver auto sets the card to this speed | |
#define | SD_CARD_DUMMY_BYTE (0xFF) |
Dummy Byte. | |
#define | SDCARD_SPI_IEC_KIBI (1024L) |
1 kiB in Bytes | |
#define | SDCARD_SPI_SI_KILO (1000L) |
1 kB in Bytes | |
Functions | |
uint8_t | sdcard_spi_send_cmd (sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument, uint32_t retry_us) |
Sends a cmd to the sd card. | |
uint8_t | sdcard_spi_send_acmd (sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument, uint32_t retry_us) |
Sends an acmd to the sd card. | |
uint32_t | sdcard_spi_get_sector_count (sdcard_spi_t *card) |
Gets the sector count of the card. | |
uint32_t | sdcard_spi_get_au_size (sdcard_spi_t *card) |
Gets the allocation unit size of the card. | |
sd_rw_response_t | sdcard_spi_read_sds (sdcard_spi_t *card, sd_status_t *sd_status) |
Gets the SD status of the card. | |
#define | SD_POWERSEQUENCE_CLOCK_COUNT 74 |
#define | SD_CARD_PREINIT_CLOCK_PERIOD_US 10 /* used to generate 100 kHz clock in init phase*/ |
#define | SD_CARD_WAIT_AFTER_POWER_UP_US 1000 |
#define | SD_R1_RESPONSE_PARAM_ERROR (1<<6) |
#define | SD_R1_RESPONSE_ADDR_ERROR (1<<5) |
#define | SD_R1_RESPONSE_ERASE_SEQ_ERROR (1<<4) |
#define | SD_R1_RESPONSE_CMD_CRC_ERROR (1<<3) |
#define | SD_R1_RESPONSE_ILLEGAL_CMD_ERROR (1<<2) |
#define | SD_R1_RESPONSE_ERASE_RESET (1<<1) |
#define | SD_R1_RESPONSE_IN_IDLE_STATE (0x01) |
#define | SD_INVALID_R1_RESPONSE (1<<7) |
#define | R1_VALID(X) (((X) >> 7) == 0) |
#define | R1_PARAM_ERR(X) ((((X) &SD_R1_RESPONSE_PARAM_ERROR) != 0)) |
#define | R1_ADDR_ERR(X) ((((X) &SD_R1_RESPONSE_ADDR_ERROR) != 0)) |
#define | R1_ERASE_ERR(X) ((((X) &SD_R1_RESPONSE_ERASE_SEQ_ERROR) != 0)) |
#define | R1_CMD_CRC_ERR(X) ((((X) &SD_R1_RESPONSE_CMD_CRC_ERROR) != 0)) |
#define | R1_ILL_CMD_ERR(X) ((((X) &SD_R1_RESPONSE_ILLEGAL_CMD_ERROR) != 0)) |
#define | R1_IDLE_BIT_SET(X) (((X) &SD_R1_RESPONSE_IN_IDLE_STATE) != 0) |
#define | R1_ERROR(X) |
#define | DATA_RESPONSE_IS_VALID(X) (((X) & 0x11) == 0x01) |
#define | DATA_RESPONSE_ACCEPTED(X) (((X) & 0x0E) == (1<<2)) |
#define | DATA_RESPONSE_CRC_ERR(X) (((X) & 0x0E) == 0x0A) |
#define | DATA_RESPONSE_WRITE_ERR(X) (((X) & 0x0E) == 0x0C) |
#define | OCR_VOLTAGE_3_2_TO_3_3 (1L << 20) |
#define | OCR_VOLTAGE_3_3_TO_3_4 (1L << 21) |
#define | OCR_CCS (1L << 30) |
#define | OCR_POWER_UP_STATUS (1L << 31) |
#define | SYSTEM_VOLTAGE (OCR_VOLTAGE_3_2_TO_3_3 | OCR_VOLTAGE_3_2_TO_3_3) |
#define | SD_CMD_PREFIX_MASK (1<<6) |
#define | SD_CMD_0 0 /* Resets the SD Memory Card */ |
#define | SD_CMD_1 1 /* Sends host capacity support info and starts the cards init process */ |
#define | SD_CMD_8 8 /* Sends SD Card interface condition incl. host supply voltage info */ |
#define | SD_CMD_9 9 /* Asks the selected card to send its card-specific data (CSD) */ |
#define | SD_CMD_10 10 /* Asks the selected card to send its card identification (CID) */ |
#define | SD_CMD_12 12 /* Forces the card to stop transmission in Multiple Block Read Operation */ |
#define | SD_CMD_13 13 /* Sent as ACMD13 asks the card to send it's SD status */ |
#define | SD_CMD_16 16 /* In case of SDSC Card, block length is set by this command */ |
#define | SD_CMD_17 17 /* Reads a block of the size selected by the SET_BLOCKLEN command */ |
#define | SD_CMD_18 |
#define | SD_CMD_24 24 /* Writes a block of the size selected by the SET_BLOCKLEN command */ |
#define | SD_CMD_25 25 /* Continuously writes blocks of data until 'Stop Tran'token is sent */ |
#define | SD_CMD_41 41 /* Reserved (used for ACMD41) */ |
#define | SD_CMD_55 |
#define | SD_CMD_58 58 /* Reads the OCR register of a card */ |
#define | SD_CMD_59 59 /* Turns the CRC option on or off. Argument: 1:on; 0:off */ |
#define | SD_CMD_8_VHS_2_7_V_TO_3_6_V 0x01 |
#define | SD_CMD_8_CHECK_PATTERN 0xB5 |
#define | SD_CMD_NO_ARG 0x00000000 |
#define | SD_ACMD_41_ARG_HC 0x40000000 |
#define | SD_CMD_59_ARG_EN 0x00000001 |
#define | SD_CMD_59_ARG_DIS 0x00000000 |
#define | SD_DATA_TOKEN_CMD_17_18_24 0xFE |
#define | SD_DATA_TOKEN_CMD_25 0xFC |
#define | SD_DATA_TOKEN_CMD_25_STOP 0xFD |
#define | SD_SIZE_OF_CID_AND_CSD_REG 16 |
#define | SD_SIZE_OF_SD_STATUS 64 |
#define | SD_BLOCKS_FOR_REG_READ 1 |
#define | SD_GET_CSD_STRUCTURE(CSD_RAW_DATA) ((CSD_RAW_DATA)[0] >> 6) |
#define | SD_CSD_V1 0 |
#define | SD_CSD_V2 1 |
#define | SD_CSD_VUNSUPPORTED -1 |
Set retry parameters for specific actions | |
Retry timeouts in microseconds for specific actions. The value interpretation is uint32_t. A value of 0 disables retries. | |
#define | INIT_CMD_RETRY_US (250 * US_PER_MS) |
initialization command retry | |
#define | INIT_CMD0_RETRY_US (100UL) |
initialization command 0 retry | |
#define | R1_POLLING_RETRY_US (100 * US_PER_MS) |
initialization first response | |
#define | SD_DATA_TOKEN_RETRY_US (100 * US_PER_MS) |
data packet token read retry | |
#define | SD_WAIT_FOR_NOT_BUSY_US (250 * US_PER_MS) |
wait for SD card | |
#define | SD_BLOCK_READ_CMD_RETRY_US (100UL) |
only affects sending of cmd not whole transaction! | |
#define | SD_BLOCK_WRITE_CMD_RETRY_US (100UL) |
only affects sending of cmd not whole transaction! | |
enum | sd_init_fsm_state_t { SD_INIT_START , SD_INIT_SPI_POWER_SEQ , SD_INIT_SEND_CMD0 , SD_INIT_SEND_CMD8 , SD_INIT_CARD_UNKNOWN , SD_INIT_SEND_ACMD41_HCS , SD_INIT_SEND_ACMD41 , SD_INIT_SEND_CMD1 , SD_INIT_SEND_CMD58 , SD_INIT_SEND_CMD16 , SD_INIT_ENABLE_CRC , SD_INIT_READ_CID , SD_INIT_READ_CSD , SD_INIT_SET_MAX_SPI_SPEED , SD_INIT_FINISH } |
SD card driver internal states. More... | |
#define DATA_RESPONSE_ACCEPTED | ( | X | ) | (((X) & 0x0E) == (1<<2)) |
Definition at line 67 of file sdcard_spi_internal.h.
#define DATA_RESPONSE_CRC_ERR | ( | X | ) | (((X) & 0x0E) == 0x0A) |
Definition at line 68 of file sdcard_spi_internal.h.
#define DATA_RESPONSE_IS_VALID | ( | X | ) | (((X) & 0x11) == 0x01) |
Definition at line 66 of file sdcard_spi_internal.h.
#define DATA_RESPONSE_WRITE_ERR | ( | X | ) | (((X) & 0x0E) == 0x0C) |
Definition at line 69 of file sdcard_spi_internal.h.
#define INIT_CMD0_RETRY_US (100UL) |
initialization command 0 retry
Definition at line 139 of file sdcard_spi_internal.h.
#define INIT_CMD_RETRY_US (250 * US_PER_MS) |
initialization command retry
Definition at line 136 of file sdcard_spi_internal.h.
#define OCR_CCS (1L << 30) |
Definition at line 76 of file sdcard_spi_internal.h.
#define OCR_POWER_UP_STATUS (1L << 31) |
Definition at line 79 of file sdcard_spi_internal.h.
#define OCR_VOLTAGE_3_2_TO_3_3 (1L << 20) |
Definition at line 72 of file sdcard_spi_internal.h.
#define OCR_VOLTAGE_3_3_TO_3_4 (1L << 21) |
Definition at line 73 of file sdcard_spi_internal.h.
#define R1_ADDR_ERR | ( | X | ) | ((((X) &SD_R1_RESPONSE_ADDR_ERROR) != 0)) |
Definition at line 57 of file sdcard_spi_internal.h.
#define R1_CMD_CRC_ERR | ( | X | ) | ((((X) &SD_R1_RESPONSE_CMD_CRC_ERROR) != 0)) |
Definition at line 59 of file sdcard_spi_internal.h.
#define R1_ERASE_ERR | ( | X | ) | ((((X) &SD_R1_RESPONSE_ERASE_SEQ_ERROR) != 0)) |
Definition at line 58 of file sdcard_spi_internal.h.
#define R1_ERROR | ( | X | ) |
Definition at line 62 of file sdcard_spi_internal.h.
#define R1_IDLE_BIT_SET | ( | X | ) | (((X) &SD_R1_RESPONSE_IN_IDLE_STATE) != 0) |
Definition at line 61 of file sdcard_spi_internal.h.
#define R1_ILL_CMD_ERR | ( | X | ) | ((((X) &SD_R1_RESPONSE_ILLEGAL_CMD_ERROR) != 0)) |
Definition at line 60 of file sdcard_spi_internal.h.
#define R1_PARAM_ERR | ( | X | ) | ((((X) &SD_R1_RESPONSE_PARAM_ERROR) != 0)) |
Definition at line 56 of file sdcard_spi_internal.h.
#define R1_POLLING_RETRY_US (100 * US_PER_MS) |
initialization first response
Definition at line 142 of file sdcard_spi_internal.h.
#define R1_VALID | ( | X | ) | (((X) >> 7) == 0) |
Definition at line 55 of file sdcard_spi_internal.h.
#define SD_ACMD_41_ARG_HC 0x40000000 |
Definition at line 109 of file sdcard_spi_internal.h.
#define SD_BLOCK_READ_CMD_RETRY_US (100UL) |
only affects sending of cmd not whole transaction!
Definition at line 151 of file sdcard_spi_internal.h.
#define SD_BLOCK_WRITE_CMD_RETRY_US (100UL) |
only affects sending of cmd not whole transaction!
Definition at line 154 of file sdcard_spi_internal.h.
#define SD_BLOCKS_FOR_REG_READ 1 |
Definition at line 120 of file sdcard_spi_internal.h.
#define SD_CARD_DUMMY_BYTE (0xFF) |
Dummy Byte.
Definition at line 187 of file sdcard_spi_internal.h.
#define SD_CARD_PREINIT_CLOCK_PERIOD_US 10 /* used to generate 100 kHz clock in init phase*/ |
Definition at line 42 of file sdcard_spi_internal.h.
#define SD_CARD_SPI_MODE SPI_MODE_0 |
SPI mode used for SD card.
Definition at line 167 of file sdcard_spi_internal.h.
#define SD_CARD_SPI_SPEED_POSTINIT SPI_CLK_10MHZ |
after init procedure is finished the driver auto sets the card to this speed
Definition at line 181 of file sdcard_spi_internal.h.
#define SD_CARD_SPI_SPEED_PREINIT SPI_CLK_400KHZ |
this speed setting is only used while the init procedure is performed
Definition at line 174 of file sdcard_spi_internal.h.
#define SD_CARD_WAIT_AFTER_POWER_UP_US 1000 |
Definition at line 43 of file sdcard_spi_internal.h.
#define SD_CMD_0 0 /* Resets the SD Memory Card */ |
Definition at line 88 of file sdcard_spi_internal.h.
#define SD_CMD_1 1 /* Sends host capacity support info and starts the cards init process */ |
Definition at line 89 of file sdcard_spi_internal.h.
#define SD_CMD_10 10 /* Asks the selected card to send its card identification (CID) */ |
Definition at line 92 of file sdcard_spi_internal.h.
#define SD_CMD_12 12 /* Forces the card to stop transmission in Multiple Block Read Operation */ |
Definition at line 93 of file sdcard_spi_internal.h.
#define SD_CMD_13 13 /* Sent as ACMD13 asks the card to send it's SD status */ |
Definition at line 94 of file sdcard_spi_internal.h.
#define SD_CMD_16 16 /* In case of SDSC Card, block length is set by this command */ |
Definition at line 96 of file sdcard_spi_internal.h.
#define SD_CMD_17 17 /* Reads a block of the size selected by the SET_BLOCKLEN command */ |
Definition at line 97 of file sdcard_spi_internal.h.
#define SD_CMD_18 |
Definition at line 98 of file sdcard_spi_internal.h.
#define SD_CMD_24 24 /* Writes a block of the size selected by the SET_BLOCKLEN command */ |
Definition at line 99 of file sdcard_spi_internal.h.
#define SD_CMD_25 25 /* Continuously writes blocks of data until 'Stop Tran'token is sent */ |
Definition at line 100 of file sdcard_spi_internal.h.
#define SD_CMD_41 41 /* Reserved (used for ACMD41) */ |
Definition at line 101 of file sdcard_spi_internal.h.
#define SD_CMD_55 |
Definition at line 102 of file sdcard_spi_internal.h.
#define SD_CMD_58 58 /* Reads the OCR register of a card */ |
Definition at line 103 of file sdcard_spi_internal.h.
#define SD_CMD_59 59 /* Turns the CRC option on or off. Argument: 1:on; 0:off */ |
Definition at line 104 of file sdcard_spi_internal.h.
#define SD_CMD_59_ARG_DIS 0x00000000 |
Definition at line 111 of file sdcard_spi_internal.h.
#define SD_CMD_59_ARG_EN 0x00000001 |
Definition at line 110 of file sdcard_spi_internal.h.
#define SD_CMD_8 8 /* Sends SD Card interface condition incl. host supply voltage info */ |
Definition at line 90 of file sdcard_spi_internal.h.
#define SD_CMD_8_CHECK_PATTERN 0xB5 |
Definition at line 107 of file sdcard_spi_internal.h.
#define SD_CMD_8_VHS_2_7_V_TO_3_6_V 0x01 |
Definition at line 106 of file sdcard_spi_internal.h.
#define SD_CMD_9 9 /* Asks the selected card to send its card-specific data (CSD) */ |
Definition at line 91 of file sdcard_spi_internal.h.
#define SD_CMD_NO_ARG 0x00000000 |
Definition at line 108 of file sdcard_spi_internal.h.
#define SD_CMD_PREFIX_MASK (1<<6) |
Definition at line 86 of file sdcard_spi_internal.h.
#define SD_CSD_V1 0 |
Definition at line 122 of file sdcard_spi_internal.h.
#define SD_CSD_V2 1 |
Definition at line 123 of file sdcard_spi_internal.h.
#define SD_CSD_V2_C_SIZE_BLOCK_MULT 1024 |
memory capacity in bytes = (C_SIZE+1) * SD_CSD_V2_C_SIZE_BLOCK_MULT * BLOCK_LEN
Definition at line 161 of file sdcard_spi_internal.h.
#define SD_CSD_VUNSUPPORTED -1 |
Definition at line 124 of file sdcard_spi_internal.h.
#define SD_DATA_TOKEN_CMD_17_18_24 0xFE |
Definition at line 114 of file sdcard_spi_internal.h.
#define SD_DATA_TOKEN_CMD_25 0xFC |
Definition at line 115 of file sdcard_spi_internal.h.
#define SD_DATA_TOKEN_CMD_25_STOP 0xFD |
Definition at line 116 of file sdcard_spi_internal.h.
#define SD_DATA_TOKEN_RETRY_US (100 * US_PER_MS) |
data packet token read retry
Definition at line 145 of file sdcard_spi_internal.h.
#define SD_GET_CSD_STRUCTURE | ( | CSD_RAW_DATA | ) | ((CSD_RAW_DATA)[0] >> 6) |
Definition at line 121 of file sdcard_spi_internal.h.
#define SD_INVALID_R1_RESPONSE (1<<7) |
Definition at line 53 of file sdcard_spi_internal.h.
#define SD_POWERSEQUENCE_CLOCK_COUNT 74 |
Definition at line 40 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_ADDR_ERROR (1<<5) |
Definition at line 47 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_CMD_CRC_ERROR (1<<3) |
Definition at line 49 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_ERASE_RESET (1<<1) |
Definition at line 51 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_ERASE_SEQ_ERROR (1<<4) |
Definition at line 48 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_ILLEGAL_CMD_ERROR (1<<2) |
Definition at line 50 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_IN_IDLE_STATE (0x01) |
Definition at line 52 of file sdcard_spi_internal.h.
#define SD_R1_RESPONSE_PARAM_ERROR (1<<6) |
Definition at line 46 of file sdcard_spi_internal.h.
#define SD_SIZE_OF_CID_AND_CSD_REG 16 |
Definition at line 118 of file sdcard_spi_internal.h.
#define SD_SIZE_OF_SD_STATUS 64 |
Definition at line 119 of file sdcard_spi_internal.h.
#define SD_WAIT_FOR_NOT_BUSY_US (250 * US_PER_MS) |
wait for SD card
Definition at line 148 of file sdcard_spi_internal.h.
#define SDCARD_SPI_IEC_KIBI (1024L) |
1 kiB in Bytes
Definition at line 192 of file sdcard_spi_internal.h.
#define SDCARD_SPI_SI_KILO (1000L) |
1 kB in Bytes
Definition at line 197 of file sdcard_spi_internal.h.
#define SYSTEM_VOLTAGE (OCR_VOLTAGE_3_2_TO_3_3 | OCR_VOLTAGE_3_2_TO_3_3) |
Definition at line 83 of file sdcard_spi_internal.h.
enum sd_init_fsm_state_t |
SD card driver internal states.
Definition at line 203 of file sdcard_spi_internal.h.
uint32_t sdcard_spi_get_au_size | ( | sdcard_spi_t * | card | ) |
Gets the allocation unit size of the card.
[in] | card | Initialized sd-card struct |
uint32_t sdcard_spi_get_sector_count | ( | sdcard_spi_t * | card | ) |
Gets the sector count of the card.
[in] | card | Initialized sd-card struct |
sd_rw_response_t sdcard_spi_read_sds | ( | sdcard_spi_t * | card, |
sd_status_t * | sd_status | ||
) |
Gets the SD status of the card.
[in] | card | Initialized sd-card struct |
[out] | sd_status | memory location where status struct is stored |
uint8_t sdcard_spi_send_acmd | ( | sdcard_spi_t * | card, |
uint8_t | sd_cmd_idx, | ||
uint32_t | argument, | ||
uint32_t | retry_us | ||
) |
Sends an acmd to the sd card.
ACMD<n> consists of sending CMD55 + CMD<n>
[in] | card | Initialized sd-card struct |
[in] | sd_cmd_idx | A supported sd-card command index for SPI-mode like defined in "7.3.1.3 Detailed Command Description" of sd spec. (for ACMDX this parameter is simply the integer value X). |
[in] | argument | The argument for the given cmd. As described by "7.3.1.1 Command Format". This argument is transmitted byte wise with most significant byte first. |
[in] | retry_us | Specifies microsecond timeout for retries in case of command errors. Use 0 to try exactly once. |
uint8_t sdcard_spi_send_cmd | ( | sdcard_spi_t * | card, |
uint8_t | sd_cmd_idx, | ||
uint32_t | argument, | ||
uint32_t | retry_us | ||
) |
Sends a cmd to the sd card.
[in] | card | Initialized sd-card struct |
[in] | sd_cmd_idx | A supported sd-card command index for SPI-mode like defined in "7.3.1.3 Detailed Command Description" of sd spec. (for CMDX this parameter is simply the integer value X). |
[in] | argument | The argument for the given cmd. As described by "7.3.1.1 Command Format". This argument is transmitted byte wise with most significant byte first. |
[in] | retry_us | Specifies microsecond timeout for retries in case of command errors. Use 0 to try exactly once. |