sdcard_spi_internal.h

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/pdf/part1_500.pdf.

SD_POWERSEQUENCE_CLOCK_COUNT
1
74
SD_CARD_PREINIT_CLOCK_PERIOD_US
1
10 /* used to generate 100 kHz clock in init phase*/
SD_CARD_WAIT_AFTER_POWER_UP_US
1
1000
SD_R1_RESPONSE_PARAM_ERROR
1
(1<<6)
SD_R1_RESPONSE_ADDR_ERROR
1
(1<<5)
SD_R1_RESPONSE_ERASE_SEQ_ERROR
1
(1<<4)
SD_R1_RESPONSE_CMD_CRC_ERROR
1
(1<<3)
SD_R1_RESPONSE_ILLEGAL_CMD_ERROR
1
(1<<2)
SD_R1_RESPONSE_ERASE_RESET
1
(1<<1)
SD_R1_RESPONSE_IN_IDLE_STATE
1
(0x01)
SD_INVALID_R1_RESPONSE
1
(1<<7)
R1_VALID( X)
1
(((X) >> 7) == 0)
R1_PARAM_ERR( X)
1
((((X) &SD_R1_RESPONSE_PARAM_ERROR) != 0))
R1_ADDR_ERR( X)
1
((((X) &SD_R1_RESPONSE_ADDR_ERROR) != 0))
R1_ERASE_ERR( X)
1
((((X) &SD_R1_RESPONSE_ERASE_SEQ_ERROR) != 0))
R1_CMD_CRC_ERR( X)
1
((((X) &SD_R1_RESPONSE_CMD_CRC_ERROR) != 0))
R1_ILL_CMD_ERR( X)
1
((((X) &SD_R1_RESPONSE_ILLEGAL_CMD_ERROR) != 0))
R1_IDLE_BIT_SET( X)
1
(((X) &SD_R1_RESPONSE_IN_IDLE_STATE) != 0)
R1_ERROR( X)
1
2
(R1_PARAM_ERR(X) || R1_ADDR_ERR(X) || R1_ERASE_ERR(X) || \
                     R1_CMD_CRC_ERR(X) || R1_ILL_CMD_ERR(X))
DATA_RESPONSE_IS_VALID( X)
1
(((X) & 0x11) == 0x01)
DATA_RESPONSE_ACCEPTED( X)
1
(((X) & 0x0E) == (1<<2))
DATA_RESPONSE_CRC_ERR( X)
1
(((X) & 0x0E) == 0x0A)
DATA_RESPONSE_WRITE_ERR( X)
1
(((X) & 0x0E) == 0x0C)
OCR_VOLTAGE_3_2_TO_3_3
1
(1L << 20)
OCR_VOLTAGE_3_3_TO_3_4
1
(1L << 21)
OCR_CCS
1
(1L << 30)
OCR_POWER_UP_STATUS
1
(1L << 31)
SYSTEM_VOLTAGE
1
(OCR_VOLTAGE_3_2_TO_3_3 | OCR_VOLTAGE_3_2_TO_3_3)
SD_CMD_PREFIX_MASK
1
(1<<6)
SD_CMD_0
1
0   /* Resets the SD Memory Card */
SD_CMD_1
1
1   /* Sends host capacity support info and starts the cards init process */
SD_CMD_8
1
8   /* Sends SD Card interface condition incl. host supply voltage info */
SD_CMD_9
1
9   /* Asks the selected card to send its card-specific data (CSD) */
SD_CMD_10
1
10 /* Asks the selected card to send its card identification (CID) */
SD_CMD_12
1
12 /* Forces the card to stop transmission in Multiple Block Read Operation */
SD_CMD_13
1
13 /* Sent as ACMD13 asks the card to send it's SD status */
SD_CMD_16
1
16 /* In case of SDSC Card, block length is set by this command */
SD_CMD_17
1
17 /* Reads a block of the size selected by the SET_BLOCKLEN command */
SD_CMD_18
1
2
18 /* Continuously transfers data blocks from card to host
                        until interrupted by a STOP_TRANSMISSION command */
SD_CMD_24
1
24 /* Writes a block of the size selected by the SET_BLOCKLEN command */
SD_CMD_25
1
25 /* Continuously writes blocks of data until 'Stop Tran'token is sent */
SD_CMD_41
1
41 /* Reserved (used for ACMD41) */
SD_CMD_55
1
2
55 /* Defines to the card that the next commmand is an application specific
                        command rather than a standard command */
SD_CMD_58
1
58 /* Reads the OCR register of a card */
SD_CMD_59
1
59 /* Turns the CRC option on or off. Argument: 1:on; 0:off */
SD_CMD_8_VHS_2_7_V_TO_3_6_V
1
0x01
SD_CMD_8_CHECK_PATTERN
1
0xB5
SD_CMD_NO_ARG
1
0x00000000
SD_ACMD_41_ARG_HC
1
0x40000000
SD_CMD_59_ARG_EN
1
0x00000001
SD_CMD_59_ARG_DIS
1
0x00000000
SD_DATA_TOKEN_CMD_17_18_24
1
0xFE
SD_DATA_TOKEN_CMD_25
1
0xFC
SD_DATA_TOKEN_CMD_25_STOP
1
0xFD
SD_SIZE_OF_CID_AND_CSD_REG
1
16
SD_SIZE_OF_SD_STATUS
1
64
SD_BLOCKS_FOR_REG_READ
1
1
SD_GET_CSD_STRUCTURE( CSD_RAW_DATA)
1
((CSD_RAW_DATA)[0] >> 6)
SD_CSD_V1
1
0
SD_CSD_V2
1
1
SD_CSD_VUNSUPPORTED
1
-1
R1_POLLING_RETRY_CNT
1
1000000
SD_DATA_TOKEN_RETRY_CNT
1
1000000
INIT_CMD_RETRY_CNT
1
1000000
INIT_CMD0_RETRY_CNT
1
3
SD_WAIT_FOR_NOT_BUSY_CNT
1
1000000 /* use -1 for full blocking till the card isn't busy */
SD_BLOCK_READ_CMD_RETRIES
1
10     /* only affects sending of cmd not whole transaction! */
SD_BLOCK_WRITE_CMD_RETRIES
1
10    /* only affects sending of cmd not whole transaction! */
SD_CSD_V2_C_SIZE_BLOCK_MULT
1
1024
SD_CARD_SPI_MODE
1
SPI_MODE_0
SD_CARD_SPI_SPEED_PREINIT
1
SPI_CLK_400KHZ
SD_CARD_SPI_SPEED_POSTINIT
1
SPI_CLK_10MHZ
SD_CARD_DUMMY_BYTE
1
0xFF
SDCARD_SPI_IEC_KIBI
1
(1024L)
SDCARD_SPI_SI_KILO
1
(1000L)
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
char sdcard_spi_send_cmd(sdcard_spi_t * card, char sd_cmd_idx, uint32_t argument, int32_t max_retry)

Sends a cmd to the sd card.

Parameters

card:Initialized sd-card struct
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).
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.
max_retry:Specifies how often the command should be retried if an error occures. Use 0 to try only once, -1 to try forever, or n to retry n times.

Return values

  • R1 response of the command if no (low-level) communication error occured
  • SD_INVALID_R1_RESPONSE if either waiting for the card to enter not-busy-state timed out or spi communication failed
char sdcard_spi_send_acmd(sdcard_spi_t * card, char sd_cmd_idx, uint32_t argument, int32_t max_retry)

Sends an acmd to the sd card.

ACMD<n> consists of sending CMD55 + CMD<n>

Parameters

card:Initialized sd-card struct
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).
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.
max_retry:Specifies how often the command should be retried if an error occures. Use 0 to try only once, -1 to try forever, or n to retry n times.

Return values

  • R1 response of the command if no (low-level) communication error occured
  • SD_INVALID_R1_RESPONSE if either waiting for the card to enter not-busy-state timed out or spi communication failed
uint32_t sdcard_spi_get_sector_count(sdcard_spi_t * card)

Gets the sector count of the card.

Parameters

card:Initialized sd-card struct

Return values

  • number of available sectors
uint32_t sdcard_spi_get_au_size(sdcard_spi_t * card)

Gets the allocation unit size of the card.

Parameters

card:Initialized sd-card struct

Return values

  • size of AU in bytes
sdcard_spi.h::sd_rw_response_t sdcard_spi_read_sds(sdcard_spi_t * card, sd_status_t * sd_status)

Gets the SD status of the card.

Parameters

card:Initialized sd-card struct
sd_status:memory location where status struct is stored

Return values

  • sd_status_t struct that contains all SD status information