sdmmc_host.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. SDMMC Host Driver
  2. =================
  3. :link_to_translation:`zh_CN:[中文]`
  4. Overview
  5. --------
  6. {IDF_TARGET_NAME}'s SDMMC host peripheral has two slots. Each slot can be used independently to connect to an SD card, SDIO device, or eMMC chip.
  7. .. only:: esp32
  8. - Slot 0 (:c:macro:`SDMMC_HOST_SLOT_0`) is an 8-bit slot. It uses ``HS1_*`` signals in the PIN MUX.
  9. - Slot 1 (:c:macro:`SDMMC_HOST_SLOT_1`) is a 4-bit slot. It uses ``HS2_*`` signals in the PIN MUX.
  10. The slots are connected to {IDF_TARGET_NAME} GPIOs using IO MUX. Pin mappings of these slots are given in the table below.
  11. .. list-table::
  12. :header-rows: 1
  13. :widths: 20 40 40
  14. :align: center
  15. * - Signal
  16. - Slot 0
  17. - Slot 1
  18. * - CMD
  19. - GPIO11
  20. - GPIO15
  21. * - CLK
  22. - GPIO6
  23. - GPIO14
  24. * - D0
  25. - GPIO7
  26. - GPIO2
  27. * - D1
  28. - GPIO8
  29. - GPIO4
  30. * - D2
  31. - GPIO9
  32. - GPIO12
  33. * - D3
  34. - GPIO10
  35. - GPIO13
  36. * - D4
  37. - GPIO16
  38. -
  39. * - D5
  40. - GPIO17
  41. -
  42. * - D6
  43. - GPIO5
  44. -
  45. * - D7
  46. - GPIO18
  47. -
  48. * - CD
  49. - any input via GPIO matrix
  50. - any input via GPIO matrix
  51. * - WP
  52. - any input via GPIO matrix
  53. - any input via GPIO matrix
  54. The Card Detect (CD) and Write Protect (WP) signals can be routed to arbitrary pins using the GPIO matrix. To reserve the pins, set the ``cd`` and ``wp`` members of the :cpp:class:`sdmmc_slot_config_t` structure before calling :cpp:func:`sdmmc_host_init_slot`. Please note that it is not advised to specify a CD pin when working with SDIO cards, because the CD signal in ESP32 can also trigger SDIO slave interrupt.
  55. .. warning::
  56. Pins used by Slot 0 (``HS1_*``) are also used to connect the SPI flash chip in ESP32-WROOM and ESP32-WROVER modules. These pins cannot be concurrently shared between an SD card and an SPI flash. If you need to use Slot 0, establish an alternative connection for the SPI flash using different pins and configure the necessary eFuses accordingly.
  57. .. only:: SOC_SDMMC_USE_GPIO_MATRIX
  58. Both slots :c:macro:`SDMMC_HOST_SLOT_0` and :c:macro:`SDMMC_HOST_SLOT_1` support 1-, 4- and 8-line SD interfaces. The slots are connected to {IDF_TARGET_NAME} GPIOs using the GPIO matrix. This means that any GPIO may be used for each of the SD card signals.
  59. Supported Speed Modes
  60. ---------------------
  61. SDMMC Host driver supports the following speed modes:
  62. - Default Speed (20 MHz): 1-line or 4-line with SD cards, and 1-line, 4-line, or 8-line with 3.3 V eMMC
  63. - High Speed (40 MHz): 1-line or 4-line with SD cards, and 1-line, 4-line, or 8-line with 3.3 V eMMC
  64. - High Speed DDR (40 MHz): 4-line with 3.3 V eMMC
  65. Speed modes not supported at present:
  66. - High Speed DDR mode: 8-line eMMC
  67. - UHS-I 1.8 V modes: 4-line SD cards
  68. Using the SDMMC Host Driver
  69. ---------------------------
  70. Of all the functions listed below, only the following ones will be used directly by most applications:
  71. - :cpp:func:`sdmmc_host_init`
  72. - :cpp:func:`sdmmc_host_init_slot`
  73. - :cpp:func:`sdmmc_host_deinit`
  74. Other functions, such as the ones given below, will be called by the SD/MMC protocol layer via function pointers in the :cpp:class:`sdmmc_host_t` structure:
  75. - :cpp:func:`sdmmc_host_set_bus_width`
  76. - :cpp:func:`sdmmc_host_set_card_clk`
  77. - :cpp:func:`sdmmc_host_do_transaction`
  78. Configuring Bus Width and Frequency
  79. -----------------------------------
  80. With the default initializers for :cpp:class:`sdmmc_host_t` and :cpp:class:`sdmmc_slot_config_t`, i.e., :c:macro:`SDMMC_HOST_DEFAULT` and :c:macro:`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.
  81. 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 :cpp:class:`sdmmc_host_t`:
  82. .. code-block::
  83. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  84. host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  85. If you need a specific frequency other than standard speeds, you are free to use any value from within an appropriate range of the SD interface given (SDMMC or SDSPI). However, the real clock frequency shall be calculated by the underlying driver and the value can be different from the one required.
  86. For the SDMMC, ``max_freq_khz`` works as the upper limit so the final frequency value shall be always lower or equal. For the SDSPI, the nearest fitting frequency is supplied and thus the value can be greater than/equal to/lower than ``max_freq_khz``.
  87. To configure the bus width, set the ``width`` field of :cpp:class:`sdmmc_slot_config_t`. For example, to set 1-line mode:
  88. .. code-block::
  89. sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
  90. slot.width = 1;
  91. .. only:: SOC_SDMMC_USE_GPIO_MATRIX
  92. Configuring GPIOs
  93. -----------------
  94. {IDF_TARGET_NAME} SDMMC Host can be configured to use arbitrary GPIOs for each of the signals. Configuration is performed by setting members of :cpp:class:`sdmmc_slot_config_t` structure.
  95. For example, to use GPIOs 1-6 for CLK, CMD, and D0-D3 signals respectively:
  96. .. code-block::
  97. sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
  98. slot.clk = GPIO_NUM_1;
  99. slot.cmd = GPIO_NUM_2;
  100. slot.d0 = GPIO_NUM_3;
  101. slot.d1 = GPIO_NUM_4;
  102. slot.d2 = GPIO_NUM_5;
  103. slot.d3 = GPIO_NUM_6;
  104. 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:
  105. .. code-block::
  106. slot.cd = GPIO_NUM_7;
  107. slot.wp = GPIO_NUM_8;
  108. ``SDMMC_SLOT_CONFIG_DEFAULT`` sets both to ``GPIO_NUM_NC``, meaning that by default the signals are not used.
  109. Once :cpp:class:`sdmmc_slot_config_t` structure is initialized this way, you can use it when calling :cpp:func:`sdmmc_host_init_slot` or one of the higher level functions (such as :cpp:func:`esp_vfs_fat_sdmmc_mount`).
  110. DDR Mode for eMMC Chips
  111. -----------------------
  112. By default, DDR mode will be used if:
  113. - SDMMC host frequency is set to :c:macro:`SDMMC_FREQ_HIGHSPEED` in :cpp:class:`sdmmc_host_t` structure, and
  114. - eMMC chip reports DDR mode support in its CSD register
  115. DDR mode places higher requirements for signal integrity. To disable DDR mode while keeping the :c:macro:`SDMMC_FREQ_HIGHSPEED` frequency, clear the :c:macro:`SDMMC_HOST_FLAG_DDR` bit in :cpp:member:`sdmmc_host_t::flags` field of the :cpp:class:`sdmmc_host_t`:
  116. .. code-block::
  117. sdmmc_host_t host = SDMMC_HOST_DEFAULT();
  118. host.max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  119. host.flags &= ~SDMMC_HOST_FLAG_DDR;
  120. See also
  121. --------
  122. - :doc:`../storage/sdmmc`: introduces the higher-level driver which implements the protocol layer.
  123. - :doc:`sdspi_host`: introduces a similar driver that uses the SPI controller and is limited to SD protocol's SPI mode.
  124. - :doc:`sd_pullup_requirements`: introduces pull-up support and compatibilities of modules and development kits.
  125. API Reference
  126. -------------
  127. .. include-build-file:: inc/sdmmc_host.inc