Loading...
Searching...
No Matches

SDIO/SD/MMC device API using a low-level peripheral driver. More...

Detailed Description

SDIO/SD/MMC device API using a low-level peripheral driver.

Author
Gunar Schorcht gunar.nosp@m.@sch.nosp@m.orcht.nosp@m..net

Definition in file sdmmc.h.

#include <errno.h>
#include "assert.h"
#include "byteorder.h"
#include "macros/units.h"
#include "periph_conf.h"
#include "xfa.h"
+ Include dependency graph for sdmmc.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

union  sdmmc_card_status_t
 SD/MMC Card status as structure. More...
 
struct  sdmmc_sd_status_t
 SD Status (SD Memory Card only) More...
 
struct  sdmmc_cid_sd_t
 CID register structure (SD Memory Cards) More...
 
struct  sdmmc_cid_mmc_t
 CID register structure (MMC) More...
 
union  sdmmc_cid_t
 CID register structure (SD Memory and MMC Cards) More...
 
struct  sdmmc_csd_v1_t
 CSD register structure Version 1.0. More...
 
struct  sdmmc_csd_v2_t
 CSD register structure Version 2.0 and Version 3.0. More...
 
struct  sdmmc_csd_mmc_t
 CSD register structure for MMC. More...
 
union  sdmmc_csd_t
 CSD register (SD Memory Card and MMC) More...
 
struct  sdmmc_ext_csd_t
 Extended CSD (EXT_CSD) register structure (MMC only) More...
 
struct  sdmmc_scr_t
 SCR register structure (SD Memory Card only) More...
 
struct  sdmmc_xfer_desc_t
 Transfer descriptor. More...
 
struct  sdmmc_driver_t
 Low-level SDIO/SD/MMC peripheral driver. More...
 
struct  sdmmc_dev
 SDIO/SD/MMC device descriptor. More...
 

Macros

#define SDMMC_CPU_DMA_REQUIREMENTS
 CPU-specific requirements for SDIO/SD/MMC buffers.
 
#define sdmmc_buf_t   SDMMC_CPU_DMA_REQUIREMENTS uint8_t
 Instantiation type for SDIO/SD/MMC buffers.
 
#define SDMMC_SDHC_BLOCK_SIZE   (512)
 Size of a single data block on SDHC/SDXC Cards in bytes.
 
#define SDMMC_CARD_TYPE_SD
 Mask for any type of SD Memory card.
 
#define SDMMC_NUMOF   XFA_LEN(sdmmc_dev_t *, sdmmc_devs)
 Number of SDIO/SD/MMC devices defined.
 

Typedefs

typedef struct sdmmc_dev sdmmc_dev_t
 sdmmc_dev_t forward declaration
 
typedef void(* sdmmc_event_cb_t) (sdmmc_dev_t *dev, sdmmc_event_t event)
 Event callback function type.
 

Enumerations

enum  sdmmc_card_type_t {
  SDMMC_CARD_TYPE_UNKNOWN = 0x00 , SDMMC_CARD_TYPE_SDSC_V1 = 0x01 , SDMMC_CARD_TYPE_SDSC_V2_V3 = 0x02 , SDMMC_CARD_TYPE_SDHC_SDXC = 0x04 ,
  SDMMC_CARD_TYPE_SDIO = 0x40 , SDMMC_CARD_TYPE_MMC = 0x80
}
 SDIO/SD/MMC Card types. More...
 
enum  sdmmc_bus_width_t { SDMMC_BUS_WIDTH_1BIT = 1 , SDMMC_BUS_WIDTH_4BIT = 4 , SDMMC_BUS_WIDTH_8BIT = 8 }
 SDIO/SD/MMC Card data bus widths. More...
 
enum  sdmmc_clock_rate_t {
  SDMMC_CLK_400K = KHZ(400) , SDMMC_CLK_20M = MHZ(20) , SDMMC_CLK_25M = MHZ(25) , SDMMC_CLK_26M = MHZ(26) ,
  SDMMC_CLK_50M = MHZ(50) , SDMMC_CLK_52M = MHZ(52)
}
 SDIO/SD/MMC Card clock rate types. More...
 
enum  sdmmc_xfer_type_t { SDMMC_MULTIBYTE , SDMMC_BLOCK , SDMMC_STREAM }
 Data transfer types. More...
 
enum  sdmmc_auto_cmd_t { SDMMC_AUTO_CMD_NONE = 0x00 , SDMMC_AUTO_CMD12 = 0x01 , SDMMC_AUTO_CMD23 = 0x02 , SDMMC_AUTO_CMD_BOTH = 0x03 }
 Auto Command features supported by the SDIO/SD/MMC peripheral. More...
 
enum  sdmmc_event_t { SDMMC_EVENT_CARD_INSERTED , SDMMC_EVENT_CARD_REMOVED }
 Events generated by SDIO/SD/MMC high level API. More...
 

Functions

static sdmmc_dev_tsdmmc_get_dev (unsigned num)
 Retrieve SDIO/SD/MMC device descriptor reference from device index.
 
static void sdmmc_init (sdmmc_dev_t *dev)
 Basic initialization of the given SDIO/SD/MMC device.
 
int sdmmc_send_cmd (sdmmc_dev_t *dev, sdmmc_cmd_t cmd_idx, uint32_t arg, sdmmc_resp_t resp_type, uint32_t *resp)
 Send command to SDIO/SD/MMC Card and optionally wait for response.
 
int sdmmc_send_acmd (sdmmc_dev_t *dev, sdmmc_cmd_t cmd_idx, uint32_t arg, sdmmc_resp_t resp_type, uint32_t *resp)
 Send application specific command optionally wait for response.
 
int sdmmc_card_init (sdmmc_dev_t *dev)
 Card Initialization and Identification.
 
int sdmmc_xfer (sdmmc_dev_t *dev, sdmmc_cmd_t cmd_idx, uint32_t arg, uint16_t block_size, uint16_t block_num, const void *data_wr, void *data_rd, uint16_t *done)
 Perform a data transfer with the selected card or embedded device.
 
int sdmmc_read_blocks (sdmmc_dev_t *dev, uint32_t block_addr, uint16_t block_size, uint16_t block_num, void *data, uint16_t *done)
 Read a number of blocks.
 
int sdmmc_write_blocks (sdmmc_dev_t *dev, uint32_t block_addr, uint16_t block_size, uint16_t block_num, const void *data, uint16_t *done)
 Write a number of blocks.
 
int sdmmc_erase_blocks (sdmmc_dev_t *dev, uint32_t block_addr, uint16_t block_num)
 Erase a number of blocks.
 
int sdmmc_read_sds (sdmmc_dev_t *dev, sdmmc_sd_status_t *sds)
 Read SD Status Register.
 
uint64_t sdmmc_get_capacity (sdmmc_dev_t *dev)
 Get Capacity of SD/MMC Card.
 

Variables

sdmmc_dev_t sdmmc_devs []
 SDIO/SD/MMC device descriptor references as read-only XFA.
 

Command and Response related definitions

#define SDMMC_CMD(n)   (n)
 Command index.
 
#define SDMMC_ACMD_PREFIX   (1 << 7)
 Application specific command index prefix (Bit is 7 used)
 
#define SDMMC_ACMD(n)   (SDMMC_ACMD_PREFIX | SDMMC_CMD(n))
 Application specific command index.
 
#define SDMMC_CMD_NO_ARG   (0x00000000UL)
 Command argument if no argument is required.
 
#define SDMMC_CMD_ARG_RCA(n)   ((uint32_t)n << 16)
 Command argument if RCA is used in addressed commands.
 
#define SDMMC_RESP_CRC   (1UL << 4)
 Mask to check whether the response type uses CRC7.
 
#define SDMMC_RESP_BUSY   (1UL << 5)
 Mask to check whether the response includes busy status from card.
 
#define SDMMC_RESP_IDX   (0xf)
 Mask of response index.
 
enum  sdmmc_cmd_t {
  SDMMC_CMD0 = SDMMC_CMD(0) , SDMMC_CMD1 = SDMMC_CMD(1) , SDMMC_CMD2 = SDMMC_CMD(2) , SDMMC_CMD3 = SDMMC_CMD(3) ,
  SDMMC_CMD4 = SDMMC_CMD(4) , SDMMC_CMD5 = SDMMC_CMD(5) , SDMMC_CMD6 = SDMMC_CMD(6) , SDMMC_CMD7 = SDMMC_CMD(7) ,
  SDMMC_CMD8 = SDMMC_CMD(8) , SDMMC_CMD9 = SDMMC_CMD(9) , SDMMC_CMD10 = SDMMC_CMD(10) , SDMMC_CMD12 = SDMMC_CMD(12) ,
  SDMMC_CMD13 = SDMMC_CMD(13) , SDMMC_CMD16 = SDMMC_CMD(16) , SDMMC_CMD17 = SDMMC_CMD(17) , SDMMC_CMD18 = SDMMC_CMD(18) ,
  SDMMC_CMD23 = SDMMC_CMD(23) , SDMMC_CMD24 = SDMMC_CMD(24) , SDMMC_CMD25 = SDMMC_CMD(25) , SDMMC_CMD32 = SDMMC_CMD(32) ,
  SDMMC_CMD33 = SDMMC_CMD(33) , SDMMC_CMD38 = SDMMC_CMD(38) , SDMMC_CMD52 = SDMMC_CMD(52) , SDMMC_CMD53 = SDMMC_CMD(53) ,
  SDMMC_CMD55 = SDMMC_CMD(55) , SDMMC_CMD58 = SDMMC_CMD(58) , SDMMC_CMD59 = SDMMC_CMD(59) , SDMMC_ACMD6 = SDMMC_ACMD(6) ,
  SDMMC_ACMD13 = SDMMC_ACMD(13) , SDMMC_ACMD23 = SDMMC_ACMD(23) , SDMMC_ACMD41 = SDMMC_ACMD(41) , SDMMC_ACMD51 = SDMMC_ACMD(51)
}
 SDIO/SD/MMC Commands. More...
 
enum  sdmmc_resp_t {
  SDMMC_NO_R = 0 , SDMMC_R1 = 1 | SDMMC_RESP_CRC , SDMMC_R1B , SDMMC_R2 = 2 | SDMMC_RESP_CRC ,
  SDMMC_R3 = 3 , SDMMC_R4 = 4 , SDMMC_R5 = 5 | SDMMC_RESP_CRC , SDMMC_R6 = 6 | SDMMC_RESP_CRC ,
  SDMMC_R7 = 7 | SDMMC_RESP_CRC
}
 SDIO/SD/MMC Response types. More...
 

Definitions used for CMD8 - SEND_IF_COND (SD only)

See also
Physical Layer Simplified Specification Version 9.00, Section 4.3.13 [sdcard.org]
#define SDMMC_CMD8_CHECK_PATTERN   (0xaa)
 Check pattern, 0xAA recommended.
 
#define SDMMC_CMD8_VHS_27_36V   (0b0001 << 8)
 Voltage Supplied by host 2.7-3.6V.
 
#define SDMMC_CMD8_PCIE_AVAIL   (1 << 12)
 PCIe Availability (not yet used)
 
#define SDMMC_CMD8_PCIE_12V   (1 << 13)
 PCIe 1.2V Support (not yet used)
 
#define SDMMC_CMD8_CHECK   (SDMMC_CMD8_VHS_27_36V | SDMMC_CMD8_CHECK_PATTERN)
 Command argument used in CMD8.
 

OCR Register Definition

See also
Physical Layer Simplified Specification Version 9.00, Section 5.1, Table 5-1 [sdcard.org]
#define SDMMC_OCR_18V   (1UL << 7)
 Low Voltage Range.
 
#define SDMMC_OCR_27_28V   (1UL << 15)
 2.7V to 2.8V Range
 
#define SDMMC_OCR_28_29V   (1UL << 16)
 2.8V to 2.9V Range
 
#define SDMMC_OCR_29_30V   (1UL << 17)
 2.9V to 3.0V Range
 
#define SDMMC_OCR_30_31V   (1UL << 18)
 3.0V to 3.1V Range
 
#define SDMMC_OCR_31_32V   (1UL << 19)
 3.1V to 3.2V Range
 
#define SDMMC_OCR_32_33V   (1UL << 20)
 3.2V to 3.3V Range
 
#define SDMMC_OCR_33_34V   (1UL << 21)
 3.3V to 3.4V Range
 
#define SDMMC_OCR_34_35V   (1UL << 22)
 3.4V to 3.5V Range
 
#define SDMMC_OCR_35_36V   (1UL << 23)
 3.5V to 3.6V Range
 
#define SDMMC_OCR_S18A   (1UL << 24)
 Switching to 1.8V Accepted.
 
#define SDMMC_OCR_OVER_2TB   (1UL << 27)
 Over 2TB support status (CO2T)
 
#define SDMMC_OCR_UHS_II   (1UL << 29)
 UHS-II Card Status.
 
#define SDMMC_OCR_CCS   (1UL << 30)
 Card Capacity Status (CCS)
 
#define SDMMC_OCR_POWER_UP   (1UL << 31)
 Card power up status bit (busy)
 
#define SDMMC_OCR_ALL_VOLTAGES
 Voltage profile for the range 2.7-3.6V as defined for MMC.
 

SDIO/SD/MMC Card status as returned in responses R1 to CMD13 with b[15]=0

See also
Physical Layer Simplified Specification Version 9.00, Section 4.10.1, Table 4-42 [sdcard.org]
SDIO Simplified Specification Version 3.00, 4.10.8, Table 4-7, [sdcard.org]
JEDEC Standard No. JESD84-B42, MultiMediaCard (MMC) Electrical Standard, High Capacity (MMCA, 4.2), Section 7.11, Table 25 [jedec.org]
#define SDMMC_CARD_STATUS_OUT_OF_RANGE   (1UL << 31)
 SD/SDIO and MMC.
 
#define SDMMC_CARD_STATUS_ADDRESS_ERROR   (1UL << 30)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_BLOCK_LEN_ERROR   (1UL << 29)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_ERASE_SEQ_ERROR   (1UL << 28)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_ERASE_PARAM   (1UL << 27)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_WP_VIOLATION   (1UL << 26)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_CARD_IS_LOCKED   (1UL << 25)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_LOCK_UNLOCK_FAILED   (1UL << 24)
 SD/SDIO and MMC.
 
#define SDMMC_CARD_STATUS_COM_CRC_ERROR   (1UL << 23)
 SD/SDIO and MMC.
 
#define SDMMC_CARD_STATUS_ILLEGAL_COMMAND   (1UL << 22)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_CARD_ECC_FAILED   (1UL << 21)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_CC_ERROR   (1UL << 20)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_ERROR   (1UL << 19)
 SD/SDIO and MMC.
 
#define SDMMC_CARD_STATUS_UNDERRUN   (1UL << 18)
 MMC only.
 
#define SDMMC_CARD_STATUS_OVERRUN   (1UL << 17)
 MMC only.
 
#define SDMMC_CARD_STATUS_CSD_OVERWRITE   (1UL << 16)
 SD (CSD), MMC (CSD and CID)
 
#define SDMMC_CARD_STATUS_WP_ERASE_SKIP   (1UL << 15)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_CARD_ECC_DISABLED   (1UL << 14)
 SD only.
 
#define SDMMC_CARD_STATUS_ERASE_RESET   (1UL << 13)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_READY_FOR_DATA   (1UL << 8)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_SWITCH_ERROR   (1UL << 7)
 MMC only.
 
#define SDMMC_CARD_STATUS_FX_EVENT   (1UL << 6)
 SD only.
 
#define SDMMC_CARD_STATUS_APP_CMD   (1UL << 5)
 SD and MMC.
 
#define SDMMC_CARD_STATUS_AKE_SEQ_ERROR   (1UL << 3)
 SD only.
 
#define SDMMC_CARD_STATUS_ERRORS
 SD/MMC Card status mask for error flags.
 
#define SDMMC_CARD_STATUS_CURRENT_STATE(n)   (((n) >> SDMMC_CARD_STATUS_CURRENT_STATE_Pos) & 0x0f)
 SD/MMC Card status mask for current state.
 
#define SDMMC_CARD_STATUS_CURRENT_STATE_Pos   (9)
 CURRENT_STATE position.
 
enum  {
  SDMMC_CARD_STATE_IDLE = 0 , SDMMC_CARD_STATE_READY = 1 , SDMMC_CARD_STATE_IDENT = 2 , SDMMC_CARD_STATE_STBY = 3 ,
  SDMMC_CARD_STATE_TRAN = 4 , SDMMC_CARD_STATE_DATA = 5 , SDMMC_CARD_STATE_RCV = 6 , SDMMC_CARD_STATE_PRG = 7 ,
  SDMMC_CARD_STATE_DIS = 8 , SDMMC_CARD_STATE_BTST = 9
}
 SD/MMC Card states. More...
 

SD Status register (SD Memory Cards)

#define SDMMC_SD_STATUS_SIZE   (64)
 SD status register size.
 

CID register (SD Memory and MMC Cards)

#define SDMMC_CID_REG_SIZE   (16)
 CID register size in bytes (CID is 128 bit)
 
#define SDMMC_CID_OID_SIZE_SD   (2)
 OID (OEM/Application ID) size in byte (SD)
 
#define SDMMC_CID_PNM_SIZE_SD   (5)
 PNM (Product name) size in byte (SD)
 
#define SDMMC_CID_PNM_SIZE_MMC   (6)
 PNM (Product name) size in byte (MMC)
 

CSD Register Definitions

See also
Physical Layer Simplified Specification Version 9.00, Section 5.3, [sdcard.org]
JEDEC Standard No. JESD84-B42, MultiMediaCard (MMC) Electrical Standard, High Capacity (MMCA, 4.2), Section 8.3, Table 35 [jedec.org]
#define SDMMC_CSD_REG_SIZE   (16)
 CSD register size in byte (CSD is 128 bit)
 
#define SDMMC_EXT_CSD_REG_SIZE   (512)
 Extended CSD (EXT_CSD) register size in byte (EXT_CSD is 512 byte)
 
enum  sdmmc_csd_version_t { SDMMC_CSD_V1 = 0 , SDMMC_CSD_V2 = 1 , SDMMC_CSD_V3 = 2 , SDMMC_CSD_Vx = 3 }
 CSD Register Versions (SD Memory Card and MMC) More...
 

SCR Register Definitions (SD Memory Card only)

See also
Physical Layer Simplified Specification Version 9.00, Section 5.6, Table 5-17 [sdcard.org]
#define SDMMC_SCR_REG_SIZE   (8)
 SCR register size in byte (SCR is 64 bit)
 
#define SDMMC_SCR_ACMD_53_54_SUPPORT   (0b10000)
 sdmmc_scr_t::CMD_SUPPORT Secure Receive/Send supported
 
#define SDMMC_SCR_ACMD_58_59_SUPPORT   (0b01000)
 sdmmc_scr_t::CMD_SUPPORT Extension Register Multi-Block
 
#define SDMMC_SCR_ACMD_48_49_SUPPORT   (0b00100)
 sdmmc_scr_t::CMD_SUPPORT Extension Register Single-Block
 
#define SDMMC_SCR_ACMD_23_SUPPORT   (0b00010)
 sdmmc_scr_t::CMD_SUPPORT Set Block Count
 
#define SDMMC_SCR_ACMD_20_SUPPORT   (0b00001)
 sdmmc_scr_t::CMD_SUPPORT Speed Class Control
 
#define SDMMC_SCR_SD_SPEC(scr)    (scr.SD_SPEC + scr.SD_SPEC3 + (scr.SD_SPECX ? scr.SD_SPECX + 1 : scr.SD_SPEC4))
 Get Physical Layer Specification Version value from SCR value.
 
#define SDMMC_DEV(x)   (sdmmc_get_dev(x))
 SD/MMC device access macro.