sdcard_spi_internal.h File Reference

Interface for issuing commands on sd-cards via SPI. More...

Detailed Description

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/

Author
Michel Rottleuthner miche.nosp@m.l.ro.nosp@m.ttleu.nosp@m.thne.nosp@m.r@haw.nosp@m.-ham.nosp@m.burg..nosp@m.de

Definition in file sdcard_spi_internal.h.

#include "periph/spi.h"
#include "periph/gpio.h"
#include "stdbool.h"
#include "sdcard_spi.h"
+ Include dependency graph for sdcard_spi_internal.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. More...
 
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. More...
 
uint32_t sdcard_spi_get_sector_count (sdcard_spi_t *card)
 Gets the sector count of the card. More...
 
uint32_t sdcard_spi_get_au_size (sdcard_spi_t *card)
 Gets the allocation unit size of the card. More...
 
sd_rw_response_t sdcard_spi_read_sds (sdcard_spi_t *card, sd_status_t *sd_status)
 Gets the SD status of the card. More...
 
#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.
 

Macro Definition Documentation

◆ R1_ERROR

#define R1_ERROR (   X)
Value:
(R1_PARAM_ERR(X) || R1_ADDR_ERR(X) || R1_ERASE_ERR(X) || \
R1_CMD_CRC_ERR(X) || R1_ILL_CMD_ERR(X))

Definition at line 61 of file sdcard_spi_internal.h.

◆ SD_CMD_18

#define SD_CMD_18
Value:
18 /* Continuously transfers data blocks from card to host
until interrupted by a STOP_TRANSMISSION command */

Definition at line 98 of file sdcard_spi_internal.h.

◆ SD_CMD_55

#define SD_CMD_55
Value:
55 /* Defines to the card that the next command is an application specific
command rather than a standard command */

Definition at line 102 of file sdcard_spi_internal.h.

Function Documentation

◆ sdcard_spi_get_au_size()

uint32_t sdcard_spi_get_au_size ( sdcard_spi_t card)

Gets the allocation unit size of the card.

Parameters
[in]cardInitialized sd-card struct
Returns
size of AU in bytes

◆ sdcard_spi_get_sector_count()

uint32_t sdcard_spi_get_sector_count ( sdcard_spi_t card)

Gets the sector count of the card.

Parameters
[in]cardInitialized sd-card struct
Returns
number of available sectors

◆ sdcard_spi_read_sds()

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
[in]cardInitialized sd-card struct
[out]sd_statusmemory location where status struct is stored
Returns
sd_status_t struct that contains all SD status information

◆ sdcard_spi_send_acmd()

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>

Parameters
[in]cardInitialized sd-card struct
[in]sd_cmd_idxA 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]argumentThe 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_usSpecifies microsecond timeout for retries in case of command errors. Use 0 to try exactly once.
Returns
R1 response of the command if no (low-level) communication error occurred
SD_INVALID_R1_RESPONSE if either waiting for the card to enter not-busy-state timed out or spi communication failed

◆ sdcard_spi_send_cmd()

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.

Parameters
[in]cardInitialized sd-card struct
[in]sd_cmd_idxA 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]argumentThe 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_usSpecifies microsecond timeout for retries in case of command errors. Use 0 to try exactly once.
Returns
R1 response of the command if no (low-level) communication error occurred
SD_INVALID_R1_RESPONSE if either waiting for the card to enter not-busy-state timed out or spi communication failed