SD/SDIO/MMC 驱动程序¶
概述¶
SD/SDIO/MMC 驱动是一种基于 SDMMC 和 SD SPI 主机驱动的协议级驱动程序,目前已支持 SD 存储器、SDIO 卡和 eMMC 芯片。
SDMMC 主机驱动和 SD SPI 主机驱动(driver/include/driver/sdmmc_host.h)为以下功能提供 API:
发送命令至从设备
接收和发送数据
处理总线错误
初始化函数及配置函数:
如需初始化和配置 SDMMC 主机,请参阅 SDMMC 主机 API
如需初始化和配置 SD SPI 主机,请参阅 SD SPI 主机 API
本文档中所述的 SDMMC 协议层仅处理 SD 协议相关事项,例如卡初始化和数据传输命令。
协议层通过 sdmmc_host_t
结构体和主机协同工作,该结构体包含指向主机各类函数的指针。
应用示例¶
ESP-IDF storage/sd_card 目录下提供了 SDMMC 驱动与 FatFs 库组合使用的示例,演示了先初始化卡,然后使用 POSIX 和 C 库 API 向卡读写数据。请参考示例目录下 README.md 文件,查看更多详细信息。
协议层 API¶
协议层具备 sdmmc_host_t
结构体,此结构体描述了 SD/MMC 主机驱动,列出了其功能,并提供指向驱动程序函数的指针。协议层将卡信息储存于 sdmmc_card_t
结构体中。向 SD/MMC 主机发送命令时,协议层调用时需要一个 sdmmc_command_t
结构体来描述命令、参数、预期返回值和需传输的数据(如有)。
用于 SD 存储卡的 API¶
初始化主机,请调用主机驱动函数,例如
sdmmc_host_init()
和sdmmc_host_init_slot()
;初始化卡,请调用
sdmmc_card_init()
,并将参数host
(即主机驱动信息)和参数card
(指向sdmmc_card_t
结构体的指针)传递给此函数。函数运行结束后,将会向sdmmc_card_t
结构体填充该卡的信息;读取或写入卡的扇区,请分别调用
sdmmc_read_sectors()
和sdmmc_write_sectors()
,并将参数card
(指向卡信息结构的指针)传递给函数;如果不再使用该卡,请调用主机驱动函数,例如
sdmmc_host_deinit()
,以禁用主机外设,并释放驱动程序分配的资源。
用于 eMMC 芯片的 API¶
从协议层的角度而言,eMMC 存储芯片与 SD 存储卡相同。尽管 eMMC 是芯片,不具备卡的外形,但由于协议相似 (sdmmc_card_t, sdmmc_card_init),用于 SD 卡的一些概念同样适用于 eMMC 芯片。注意,eMMC 芯片不可通过 SPI 使用,因此它与 SD API 主机驱动不兼容。
如需初始化 eMMC 内存并执行读/写操作,请参照上一章节 SD 卡操作步骤。
用于 SDIO 卡的 API¶
SDIO 卡初始化和检测过程与 SD 存储卡相同,唯一的区别是 SDIO 模式下数据传输命令不同。
在卡初始化和卡检测(通过运行 sdmmc_card_init()
)期间,驱动仅配置 SDIO 卡如下寄存器:
I/O 中止 (0x06) 寄存器:在该寄存器中设置 RES 位可重置卡的 I/O 部分;
总线接口控制 (0x07) 寄存器:如果主机和插槽配置中启用 4 线模式,则驱动程序会尝试在该寄存器中设置总线宽度字段。如果字段设置成功,则从机支持 4 线模式,主机也切换至 4 线模式;
高速(0x13)寄存器:如果主机配置中启用高速模式,则会在该寄存器中设置 SHS 位。
注意,驱动程序不会在 (1) I/O 使能寄存器和 Int 使能寄存器,及 (2) I/O 块大小中,设置任何位。应用程序可通过调用 sdmmc_io_write_byte()
来设置相关位。
如需设置卡配置或传输数据,请根据您的具体情况选择下表中的函数:
操作 |
读函数 |
写函数 |
---|---|---|
使用 IO_RW_DIRECT (CMD52) 读写单个字节。 |
||
使用 IO_RW_EXTENDED (CMD53) 的字节模式读写多个字节。 |
||
块模式下,使用 IO_RW_EXTENDED (CMD53) 读写数据块。 |
使用 sdmmc_io_enable_int()
函数,应用程序可启用 SDIO 中断。
在单线模式下使用 SDIO 时,还需要连接 D1 线来启用 SDIO 中断。
如果您需要应用程序保持等待直至发生 SDIO 中断,请使用 sdmmc_io_wait_int()
函数。
复合卡(存储 + SDIO)¶
该驱动程序不支持 SDIO 复合卡,复合卡会被视为 SDIO 卡。
线程安全¶
多数应用程序仅需在一个任务中使用协议层。因此,协议层在 sdmmc_card_t
结构体或在访问 SDMMC 或 SD SPI 主机驱动程序时不使用任何类型的锁。这种锁通常在较高层级实现,例如文件系统驱动程序。
API 参考¶
Header File¶
Functions¶
-
esp_err_t
sdmmc_card_init
(const sdmmc_host_t *host, sdmmc_card_t *out_card)¶ Probe and initialize SD/MMC card using given host
- Note
Only SD cards (SDSC and SDHC/SDXC) are supported now. Support for MMC/eMMC cards will be added later.
- Return
ESP_OK on success
One of the error codes from SDMMC host controller
- Parameters
host
: pointer to structure defining host controllerout_card
: pointer to structure which will receive information about the card when the function completes
-
void
sdmmc_card_print_info
(FILE *stream, const sdmmc_card_t *card)¶ Print information about the card to a stream.
- Parameters
stream
: stream obtained using fopen or fdopencard
: card information structure initialized using sdmmc_card_init
-
esp_err_t
sdmmc_write_sectors
(sdmmc_card_t *card, const void *src, size_t start_sector, size_t sector_count)¶ Write given number of sectors to SD/MMC card
- Return
ESP_OK on success
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initsrc
: pointer to data buffer to read data from; data size must be equal to sector_count * card->csd.sector_sizestart_sector
: sector where to start writingsector_count
: number of sectors to write
-
esp_err_t
sdmmc_read_sectors
(sdmmc_card_t *card, void *dst, size_t start_sector, size_t sector_count)¶ Write given number of sectors to SD/MMC card
- Return
ESP_OK on success
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initdst
: pointer to data buffer to write into; buffer size must be at least sector_count * card->csd.sector_sizestart_sector
: sector where to start readingsector_count
: number of sectors to read
-
esp_err_t
sdmmc_io_read_byte
(sdmmc_card_t *card, uint32_t function, uint32_t reg, uint8_t *out_byte)¶ Read one byte from an SDIO card using IO_RW_DIRECT (CMD52)
- Return
ESP_OK on success
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberreg
: byte address within IO function[out] out_byte
: output, receives the value read from the card
-
esp_err_t
sdmmc_io_write_byte
(sdmmc_card_t *card, uint32_t function, uint32_t reg, uint8_t in_byte, uint8_t *out_byte)¶ Write one byte to an SDIO card using IO_RW_DIRECT (CMD52)
- Return
ESP_OK on success
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberreg
: byte address within IO functionin_byte
: value to be written[out] out_byte
: if not NULL, receives new byte value read from the card (read-after-write).
-
esp_err_t
sdmmc_io_read_bytes
(sdmmc_card_t *card, uint32_t function, uint32_t addr, void *dst, size_t size)¶ Read multiple bytes from an SDIO card using IO_RW_EXTENDED (CMD53)
This function performs read operation using CMD53 in byte mode. For block mode, see sdmmc_io_read_blocks.
- Return
ESP_OK on success
ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberaddr
: byte address within IO function where reading startsdst
: buffer which receives the data read from cardsize
: number of bytes to read
-
esp_err_t
sdmmc_io_write_bytes
(sdmmc_card_t *card, uint32_t function, uint32_t addr, const void *src, size_t size)¶ Write multiple bytes to an SDIO card using IO_RW_EXTENDED (CMD53)
This function performs write operation using CMD53 in byte mode. For block mode, see sdmmc_io_write_blocks.
- Return
ESP_OK on success
ESP_ERR_INVALID_SIZE if size exceeds 512 bytes
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberaddr
: byte address within IO function where writing startssrc
: data to be writtensize
: number of bytes to write
-
esp_err_t
sdmmc_io_read_blocks
(sdmmc_card_t *card, uint32_t function, uint32_t addr, void *dst, size_t size)¶ Read blocks of data from an SDIO card using IO_RW_EXTENDED (CMD53)
This function performs read operation using CMD53 in block mode. For byte mode, see sdmmc_io_read_bytes.
- Return
ESP_OK on success
ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberaddr
: byte address within IO function where writing startsdst
: buffer which receives the data read from cardsize
: number of bytes to read, must be divisible by the card block size.
-
esp_err_t
sdmmc_io_write_blocks
(sdmmc_card_t *card, uint32_t function, uint32_t addr, const void *src, size_t size)¶ Write blocks of data to an SDIO card using IO_RW_EXTENDED (CMD53)
This function performs write operation using CMD53 in block mode. For byte mode, see sdmmc_io_write_bytes.
- Return
ESP_OK on success
ESP_ERR_INVALID_SIZE if size is not divisible by 512 bytes
One of the error codes from SDMMC host controller
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initfunction
: IO function numberaddr
: byte address within IO function where writing startssrc
: data to be writtensize
: number of bytes to read, must be divisible by the card block size.
-
esp_err_t
sdmmc_io_enable_int
(sdmmc_card_t *card)¶ Enable SDIO interrupt in the SDMMC host
- Return
ESP_OK on success
ESP_ERR_NOT_SUPPORTED if the host controller does not support IO interrupts
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_init
-
esp_err_t
sdmmc_io_wait_int
(sdmmc_card_t *card, TickType_t timeout_ticks)¶ Block until an SDIO interrupt is received
Slave uses D1 line to signal interrupt condition to the host. This function can be used to wait for the interrupt.
- Return
ESP_OK if the interrupt is received
ESP_ERR_NOT_SUPPORTED if the host controller does not support IO interrupts
ESP_ERR_TIMEOUT if the interrupt does not happen in timeout_ticks
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_inittimeout_ticks
: time to wait for the interrupt, in RTOS ticks
-
esp_err_t
sdmmc_io_get_cis_data
(sdmmc_card_t *card, uint8_t *out_buffer, size_t buffer_size, size_t *inout_cis_size)¶ Get the data of CIS region of a SDIO card.
You may provide a buffer not sufficient to store all the CIS data. In this case, this functions store as much data into your buffer as possible. Also, this function will try to get and return the size required for you.
- Return
ESP_OK: on success
ESP_ERR_INVALID_RESPONSE: if the card does not (correctly) support CIS.
ESP_ERR_INVALID_SIZE: CIS_CODE_END found, but buffer_size is less than required size, which is stored in the inout_cis_size then.
ESP_ERR_NOT_FOUND: if the CIS_CODE_END not found. Increase input value of inout_cis_size or set it to 0, if you still want to search for the end; output value of inout_cis_size is invalid in this case.
and other error code return from sdmmc_io_read_bytes
- Parameters
card
: pointer to card information structure previously initialized using sdmmc_card_initout_buffer
: Output buffer of the CIS databuffer_size
: Size of the buffer.inout_cis_size
: Mandatory, pointer to a size, input and output.input: Limitation of maximum searching range, should be 0 or larger than buffer_size. The function searches for CIS_CODE_END until this range. Set to 0 to search infinitely.
output: The size required to store all the CIS data, if CIS_CODE_END is found.
-
esp_err_t
sdmmc_io_print_cis_info
(uint8_t *buffer, size_t buffer_size, FILE *fp)¶ Parse and print the CIS information of a SDIO card.
- Note
Not all the CIS codes and all kinds of tuples are supported. If you see some unresolved code, you can add the parsing of these code in sdmmc_io.c and contribute to the IDF through the Github repository.
using sdmmc_card_init
- Return
ESP_OK: on success
ESP_ERR_NOT_SUPPORTED: if the value from the card is not supported to be parsed.
ESP_ERR_INVALID_SIZE: if the CIS size fields are not correct.
- Parameters
buffer
: Buffer to parsebuffer_size
: Size of the buffer.fp
: File pointer to print to, set to NULL to print to stdout.
Header File¶
Structures¶
-
struct
sdmmc_csd_t
¶ Decoded values from SD card Card Specific Data register
-
struct
sdmmc_cid_t
¶ Decoded values from SD card Card IDentification register
-
struct
sdmmc_scr_t
¶ Decoded values from SD Configuration Register
-
struct
sdmmc_ext_csd_t
¶ Decoded values of Extended Card Specific Data
Public Members
-
uint8_t
power_class
¶ Power class used by the card
-
uint8_t
-
struct
sdmmc_switch_func_rsp_t
¶ SD SWITCH_FUNC response buffer
Public Members
-
uint32_t
data
[512 / 8 / sizeof(uint32_t)]¶ response data
-
uint32_t
-
struct
sdmmc_command_t
¶ SD/MMC command information
Public Members
-
uint32_t
opcode
¶ SD or MMC command index
-
uint32_t
arg
¶ SD/MMC command argument
-
sdmmc_response_t
response
¶ response buffer
-
void *
data
¶ buffer to send or read into
-
size_t
datalen
¶ length of data buffer
-
size_t
blklen
¶ block length
-
int
flags
¶ see below
-
int
timeout_ms
¶ response timeout, in milliseconds
-
uint32_t
-
struct
sdmmc_host_t
¶ SD/MMC Host description
This structure defines properties of SD/MMC host and functions of SD/MMC host which can be used by upper layers.
Public Members
-
uint32_t
flags
¶ flags defining host properties
-
int
slot
¶ slot number, to be passed to host functions
-
int
max_freq_khz
¶ max frequency supported by the host
-
float
io_voltage
¶ I/O voltage used by the controller (voltage switching is not supported)
-
size_t (*
get_bus_width
)(int slot)¶ host function to get bus width
-
esp_err_t (*
do_transaction
)(int slot, sdmmc_command_t *cmdinfo)¶ host function to do a transaction
-
esp_err_t (*
io_int_wait
)(int slot, TickType_t timeout_ticks)¶ Host function to wait for SDIO interrupt line to be active
-
int
command_timeout_ms
¶ timeout, in milliseconds, of a single command. Set to 0 to use the default value.
-
uint32_t
-
struct
sdmmc_card_t
¶ SD/MMC card information structure
Public Members
-
sdmmc_host_t
host
¶ Host with which the card is associated
-
uint32_t
ocr
¶ OCR (Operation Conditions Register) value
-
sdmmc_cid_t
cid
¶ decoded CID (Card IDentification) register value
-
sdmmc_response_t
raw_cid
¶ raw CID of MMC card to be decoded after the CSD is fetched in the data transfer mode
-
sdmmc_csd_t
csd
¶ decoded CSD (Card-Specific Data) register value
-
sdmmc_scr_t
scr
¶ decoded SCR (SD card Configuration Register) value
-
sdmmc_ext_csd_t
ext_csd
¶ decoded EXT_CSD (Extended Card Specific Data) register value
-
uint16_t
rca
¶ RCA (Relative Card Address)
-
uint16_t
max_freq_khz
¶ Maximum frequency, in kHz, supported by the card
-
uint32_t
is_mem
: 1¶ Bit indicates if the card is a memory card
-
uint32_t
is_sdio
: 1¶ Bit indicates if the card is an IO card
-
uint32_t
is_mmc
: 1¶ Bit indicates if the card is MMC
-
uint32_t
num_io_functions
: 3¶ If is_sdio is 1, contains the number of IO functions on the card
-
uint32_t
log_bus_width
: 2¶ log2(bus width supported by card)
-
uint32_t
is_ddr
: 1¶ Card supports DDR mode
-
uint32_t
reserved
: 23¶ Reserved for future expansion
-
sdmmc_host_t
Macros¶
-
SDMMC_HOST_FLAG_1BIT
¶ host supports 1-line SD and MMC protocol
-
SDMMC_HOST_FLAG_4BIT
¶ host supports 4-line SD and MMC protocol
-
SDMMC_HOST_FLAG_8BIT
¶ host supports 8-line MMC protocol
-
SDMMC_HOST_FLAG_SPI
¶ host supports SPI protocol
-
SDMMC_HOST_FLAG_DDR
¶ host supports DDR mode for SD/MMC
-
SDMMC_FREQ_DEFAULT
¶ SD/MMC Default speed (limited by clock divider)
-
SDMMC_FREQ_HIGHSPEED
¶ SD High speed (limited by clock divider)
-
SDMMC_FREQ_PROBING
¶ SD/MMC probing speed
-
SDMMC_FREQ_52M
¶ MMC 52MHz speed
-
SDMMC_FREQ_26M
¶ MMC 26MHz speed