SDMMC Host Driver

Overview

ESP32-S3’s SDMMC host peripheral has two slots. Each slot can be used independently to connect to an SD card, SDIO device or eMMC chip.

Both slots (SDMMC_HOST_SLOT_0, SDMMC_HOST_SLOT_1) support 1-, 4- and 8-line SD interface. The slots are connected to ESP32-S3 GPIOs using GPIO matrix. This means that any GPIO may be used for each of the SD card signals.

Supported Speed Modes

SDMMC Host driver supports the following speed modes:

  • Default Speed (20 MHz), 1/4-line (with SD cards), and 1/4/8-line (with 3.3 V eMMC)

  • High Speed (40 MHz), 1/4-line (with SD cards), and 1/4/8-line (with 3.3 V eMMC)

  • High Speed DDR (40 MHz), 4-line (with 3.3 V eMMC)

Speed modes not supported at present:

  • High Speed DDR mode, 8-line eMMC

  • UHS-I 1.8 V modes, 4-line SD cards

Using the SDMMC Host Driver

Of all the functions listed below, only the following ones will be used directly by most applications:

Other functions, such as the ones given below, will be called by the SD/MMC protocol layer via function pointers in the sdmmc_host_t structure:

Configuring Bus Width and Frequency

With the default initializers for sdmmc_host_t and sdmmc_slot_config_t (SDMMC_HOST_DEFAULT and SDMMC_SLOT_CONFIG_DEFAULT), SDMMC Host driver will attempt to use the widest bus supported by the card (4 lines for SD, 8 lines for eMMC) and the frequency of 20 MHz.

In the designs where communication at 40 MHz frequency can be achieved, it is possible to increase the bus frequency by changing the max_freq_khz field of sdmmc_host_t:

sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;

To configure the bus width, set the width field of sdmmc_slot_config_t. For example, to set 1-line mode:

sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
slot.width = 1;

Configuring GPIOs

ESP32-S3 SDMMC Host can be configured to use arbitrary GPIOs for each of the signals. Configuration is performed by setting members of sdmmc_slot_config_t structure. For example, to use GPIOs 1-6 for CLK, CMD, D0 - D3 signals, respectively:

sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
slot.clk = GPIO_NUM_1;
slot.cmd = GPIO_NUM_2;
slot.d0 = GPIO_NUM_3;
slot.d1 = GPIO_NUM_4;
slot.d2 = GPIO_NUM_5;
slot.d3 = GPIO_NUM_6;

It is also possible to configure Card Detect and Write Protect pins. Similar to other signals, set cd and wp members of the same structure:

slot.cd = GPIO_NUM_7;
slot.wp = GPIO_NUM_8;

SDMMC_SLOT_CONFIG_DEFAULT sets both to GPIO_NUM_NC, meaning that by default the signals are not used.

Once sdmmc_slot_config_t structure is initialized this way, you can use it when calling sdmmc_host_init_slot() or one of the higher level functions, such as esp_vfs_fat_sdmmc_mount().

DDR Mode for eMMC chips

By default, DDR mode will be used if:

DDR mode places higher requirements for signal integrity. To disable DDR mode while keeping SDMMC_FREQ_HIGHSPEED frequency, clear SDMMC_HOST_FLAG_DDR bit in flags field of sdmmc_host_t:

sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
host.flags &= ~SDMMC_HOST_FLAG_DDR;

See also

See SD/SDIO/MMC Driver for the higher level driver which implements the protocol layer.

See SD SPI Host Driver for a similar driver which uses the SPI controller and is limited to SD protocol’s SPI mode.

See SD Pull-up Requirements for pullup support and compatibilities of modules and development kits.

API Reference

Functions

esp_err_t sdmmc_host_init(void)

Initialize SDMMC host peripheral.

备注

This function is not thread safe

返回

  • ESP_OK on success

  • ESP_ERR_INVALID_STATE if sdmmc_host_init was already called

  • ESP_ERR_NO_MEM if memory can not be allocated

esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t *slot_config)

Initialize given slot of SDMMC peripheral.

On the ESP32, SDMMC peripheral has two slots:

  • Slot 0: 8-bit wide, maps to HS1_* signals in PIN MUX

  • Slot 1: 4-bit wide, maps to HS2_* signals in PIN MUX

Card detect and write protect signals can be routed to arbitrary GPIOs using GPIO matrix.

备注

This function is not thread safe

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • slot_config – additional configuration for the slot

返回

  • ESP_OK on success

  • ESP_ERR_INVALID_STATE if host has not been initialized using sdmmc_host_init

esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)

Select bus width to be used for data transfer.

SD/MMC card must be initialized prior to this command, and a command to set bus width has to be sent to the card (e.g. SD_APP_SET_BUS_WIDTH)

备注

This function is not thread safe

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • width – bus width (1, 4, or 8 for slot 0; 1 or 4 for slot 1)

返回

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if slot number or width is not valid

size_t sdmmc_host_get_slot_width(int slot)

Get bus width configured in sdmmc_host_init_slot to be used for data transfer.

参数

slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

返回

configured bus width of the specified slot.

esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz)

Set card clock frequency.

Currently only integer fractions of 40MHz clock can be used. For High Speed cards, 40MHz can be used. For Default Speed cards, 20MHz can be used.

备注

This function is not thread safe

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • freq_khz – card clock frequency, in kHz

返回

  • ESP_OK on success

  • other error codes may be returned in the future

esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled)

Enable or disable DDR mode of SD interface.

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • ddr_enabled – enable or disable DDR mode

返回

  • ESP_OK on success

  • ESP_ERR_NOT_SUPPORTED if DDR mode is not supported on this slot

esp_err_t sdmmc_host_set_cclk_always_on(int slot, bool cclk_always_on)

Enable or disable always-on card clock When cclk_always_on is false, the host controller is allowed to shut down the card clock between the commands. When cclk_always_on is true, the clock is generated even if no command is in progress.

参数
  • slot – slot number

  • cclk_always_on – enable or disable always-on clock

返回

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if the slot number is invalid

esp_err_t sdmmc_host_do_transaction(int slot, sdmmc_command_t *cmdinfo)

Send command to the card and get response.

This function returns when command is sent and response is received, or data is transferred, or timeout occurs.

Attention

Data buffer passed in cmdinfo->data must be in DMA capable memory

备注

This function is not thread safe w.r.t. init/deinit functions, and bus width/clock speed configuration functions. Multiple tasks can call sdmmc_host_do_transaction as long as other sdmmc_host_* functions are not called.

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • cmdinfo – pointer to structure describing command and data to transfer

返回

  • ESP_OK on success

  • ESP_ERR_TIMEOUT if response or data transfer has timed out

  • ESP_ERR_INVALID_CRC if response or data transfer CRC check has failed

  • ESP_ERR_INVALID_RESPONSE if the card has sent an invalid response

  • ESP_ERR_INVALID_SIZE if the size of data transfer is not valid in SD protocol

  • ESP_ERR_INVALID_ARG if the data buffer is not in DMA capable memory

esp_err_t sdmmc_host_io_int_enable(int slot)

Enable IO interrupts.

This function configures the host to accept SDIO interrupts.

参数

slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

返回

returns ESP_OK, other errors possible in the future

esp_err_t sdmmc_host_io_int_wait(int slot, TickType_t timeout_ticks)

Block until an SDIO interrupt is received, or timeout occurs.

参数
  • slot – slot number (SDMMC_HOST_SLOT_0 or SDMMC_HOST_SLOT_1)

  • timeout_ticks – number of RTOS ticks to wait for the interrupt

返回

  • ESP_OK on success (interrupt received)

  • ESP_ERR_TIMEOUT if the interrupt did not occur within timeout_ticks

esp_err_t sdmmc_host_deinit(void)

Disable SDMMC host and release allocated resources.

备注

This function is not thread safe

返回

  • ESP_OK on success

  • ESP_ERR_INVALID_STATE if sdmmc_host_init function has not been called

esp_err_t sdmmc_host_pullup_en(int slot, int width)

Enable the pull-ups of sd pins.

This function is deprecated. Please set SDMMC_SLOT_FLAG_INTERNAL_PULLUP flag in sdmmc_slot_config_t::flags instead.

备注

You should always place actual pullups on the lines instead of using this function. Internal pullup resistance are high and not sufficient, may cause instability in products. This is for debug or examples only.

参数
  • slot – Slot to use, normally set it to 1.

  • width – Bit width of your configuration, 1 or 4.

返回

  • ESP_OK: if success

  • ESP_ERR_INVALID_ARG: if configured width larger than maximum the slot can support

Structures

struct sdmmc_slot_config_t

Extra configuration for SDMMC peripheral slot

Public Members

gpio_num_t clk

GPIO number of CLK signal.

gpio_num_t cmd

GPIO number of CMD signal.

gpio_num_t d0

GPIO number of D0 signal.

gpio_num_t d1

GPIO number of D1 signal.

gpio_num_t d2

GPIO number of D2 signal.

gpio_num_t d3

GPIO number of D3 signal.

gpio_num_t d4

GPIO number of D4 signal. Ignored in 1- or 4- line mode.

gpio_num_t d5

GPIO number of D5 signal. Ignored in 1- or 4- line mode.

gpio_num_t d6

GPIO number of D6 signal. Ignored in 1- or 4- line mode.

gpio_num_t d7

GPIO number of D7 signal. Ignored in 1- or 4- line mode.

gpio_num_t gpio_cd

GPIO number of card detect signal.

gpio_num_t cd

GPIO number of card detect signal; shorter name.

gpio_num_t gpio_wp

GPIO number of write protect signal.

gpio_num_t wp

GPIO number of write protect signal; shorter name.

uint8_t width

Bus width used by the slot (might be less than the max width supported)

uint32_t flags

Features used by this slot.

Macros

SDMMC_HOST_SLOT_0

SDMMC slot 0.

SDMMC_HOST_SLOT_1

SDMMC slot 1.

SDMMC_HOST_DEFAULT()

Default sdmmc_host_t structure initializer for SDMMC peripheral.

Uses SDMMC peripheral, with 4-bit mode enabled, and max frequency set to 20MHz

SDMMC_SLOT_FLAG_INTERNAL_PULLUP

Enable internal pullups on enabled pins. The internal pullups are insufficient however, please make sure external pullups are connected on the bus. This is for debug / example purpose only.

SDMMC_SLOT_NO_CD

indicates that card detect line is not used

SDMMC_SLOT_NO_WP

indicates that write protect line is not used

SDMMC_SLOT_WIDTH_DEFAULT

use the maximum possible width for the slot

SDMMC_SLOT_CONFIG_DEFAULT()

Macro defining default configuration of SDMMC host slot