sdio_slave.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. SDIO Card Slave Driver
  2. ======================
  3. .. only:: esp32c3
  4. .. warning::
  5. This document is not updated for ESP32-C3 yet.
  6. Overview
  7. --------
  8. The ESP32 SDIO Card peripherals (Host, Slave) shares two sets of pins as below table.
  9. The first set is usually occupied by SPI0 bus which is responsible for the SPI flash holding the code to run.
  10. This means SDIO slave driver can only runs on the second set of pins while SDIO host is not using it.
  11. The SDIO slave can run under 3 modes: SPI, 1-bit SD and 4-bit SD modes, which
  12. is detected automatically by the hardware. According to the SDIO
  13. specification, CMD and DAT0-3 lines should be pulled up no matter in 1-bit,
  14. 4-bit or SPI mode.
  15. Connections
  16. ^^^^^^^^^^^
  17. +----------+---------------+-------+-------+
  18. | Pin Name | Corresponding | Slot1 | Slot2 |
  19. + + pins in SPI +-------+-------+
  20. | | mode | GPIO Number |
  21. +==========+===============+=======+=======+
  22. | CLK | SCLK | 6 | 14 |
  23. +----------+---------------+-------+-------+
  24. | CMD | MOSI | 11 | 15 |
  25. +----------+---------------+-------+-------+
  26. | DAT0 | MISO | 7 | 2 |
  27. +----------+---------------+-------+-------+
  28. | DAT1 | Interrupt | 8 | 4 |
  29. +----------+---------------+-------+-------+
  30. | DAT2 | N.C. (pullup) | 9 | 12 |
  31. +----------+---------------+-------+-------+
  32. | DAT3 | #CS | 10 | 13 |
  33. +----------+---------------+-------+-------+
  34. - 1-bit SD mode: Connect CLK, CMD, DAT0, DAT1 pins and the ground.
  35. - 4-bit SD mode: Connect all pins and the ground.
  36. - SPI mode: Connect SCLK, MOSI, MISO, Interrupt, #CS pins and the ground.
  37. .. note:: Please check if CMD and DATA lines D0-D3 of the card are properly
  38. pulled up by 10 KOhm resistors. This should be ensured even in 1-bit mode
  39. or SPI mode. Most official modules don't offer these pullups internally.
  40. If you are using official development boards, check
  41. :ref:`compatibility_overview_espressif_hw_sdio` to see whether your
  42. development boards have such pullups.
  43. .. note:: Most official modules have conflicts on strapping pins with the
  44. SDIO slave function. If you are using a ESP32 module with 3.3 V flash
  45. inside, you have to burn the EFUSE when you are developing on the module
  46. for the first time. See :ref:`compatibility_overview_espressif_hw_sdio` to
  47. see how to make your modules compatible with the SDIO.
  48. Here is a list for modules/kits with 3.3 V flash:
  49. - Modules: ESP32-PICO-D4, ESP32-WROOM-32 series (including ESP32-SOLO-1),
  50. ESP32-WROVER-B and ESP32-WROVER-IB
  51. - Kits: ESP32-PICO-KIT, ESP32-DevKitC (till v4), ESP32-WROVER-KIT
  52. (v4.1 (also known as ESP32-WROVER-KIT-VB), v2, v1 (also known as DevKitJ
  53. v1))
  54. You can tell the version of your ESP23-WROVER-KIT version from the module
  55. on it: v4.1 are with ESP32-WROVER-B modules, v3 are with ESP32-WROVER
  56. modules, while v2 and v1 are with ESP32-WROOM-32 modules.
  57. Refer to :doc:`sd_pullup_requirements` for more technical details of the pullups.
  58. .. toctree::
  59. :hidden:
  60. sd_pullup_requirements
  61. The host initialize the slave into SD mode by first sending CMD0 with DAT3
  62. pin high, or in SPI mode by sending CMD0 with CS pin (the same pin as DAT3)
  63. low.
  64. After the initialization, the host can enable the 4-bit SD mode by writing
  65. CCCR register 0x07 by CMD52. All the bus detection process are handled by the
  66. slave peripheral.
  67. The host has to communicate with the slave by an ESP-slave-specific protocol.
  68. The slave driver offers 3 services over Function 1 access by CMD52 and CMD53:
  69. (1) a sending FIFO and a receiving FIFO, (2) 52 8-bit R/W registers shared by
  70. host and slave, (3) 16 interrupt sources (8 from host to slave, and 8 from
  71. slave to host).
  72. Terminology
  73. ^^^^^^^^^^^
  74. The SDIO slave driver uses the following terms:
  75. - Transfer: a transfer is always started by a command token from the host, and may contain a reply and several data
  76. blocks. ESP32 slave software is based on transfers.
  77. - Sending: slave to host transfers.
  78. - Receiving: host to slave transfers.
  79. .. note:: Register names in *{IDF_TARGET_NAME} Technical Reference Manual* > *SDIO Slave Controller*
  80. [`PDF <{IDF_TARGET_TRM_EN_URL}#sdioslave>`__] are oriented from the point of view of the host, i.e. 'rx'
  81. registers refer to sending, while 'tx' registers refer to receiving. We're not using `tx` or `rx` in the driver to
  82. avoid ambiguities.
  83. - FIFO: specific address in Function 1 that can be access by CMD53 to read/write large amount of data. The address is
  84. related to the length requested to read from/write to the slave in a single transfer:
  85. *requested length* = 0x1F800-address.
  86. - Ownership: When the driver takes ownership of a buffer, it means the driver can randomly read/write the buffer
  87. (usually via DMA). The application should not read/write the buffer until the ownership is returned to the
  88. application. If the application reads from a buffer owned by a receiving driver, the data read can be random; if
  89. the application writes to a buffer owned by a sending driver, the data sent may be corrupted.
  90. - Requested length: The length requested in one transfer determined by the FIFO address.
  91. - Transfer length: The length requested in one transfer determined by the CMD53 byte/block count field.
  92. .. note:: Requested length is different from the transfer length. ESP32 slave DMA base on the *requested length* rather
  93. than the *transfer length*. The *transfer length* should be no shorter than the *requested length*, and the rest
  94. part will be filled with 0 (sending) or discard (receiving).
  95. - Receiving buffer size: The buffer size is pre-defined between the host and the slave before communication starts.
  96. Slave application has to set the buffer size during initialization by the ``recv_buffer_size`` member of
  97. ``sdio_slave_config_t``.
  98. - Interrupts: the esp32 slave support interrupts in two directions: from host to slave (called slave interrupts below)
  99. and from slave to host (called host interrupts below). See more in :ref:`interrupts`.
  100. - Registers: specific address in Function 1 access by CMD52 or CMD53.
  101. Communication with ESP SDIO Slave
  102. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  103. The host should initialize the ESP32 SDIO slave according to the standard
  104. SDIO initialization process (Sector 3.1.2 of `SDIO Simplified
  105. Specification <https://www.sdcard.org/downloads/pls/>`_), which is described
  106. briefly in :ref:`esp_slave_init`.
  107. Furthermore, there's an ESP32-specific upper-level communication protocol upon the CMD52/CMD53 to
  108. Func 1. Please refer to :ref:`esp_slave_protocol_layer`. There is also a component
  109. :doc:`ESP Serial Slave Link </api-reference/protocols/esp_serial_slave_link>`
  110. for ESP32 master to communicate with ESP32 SDIO slave, see example :example:`peripherals/sdio`
  111. when programming your host.
  112. .. _interrupts:
  113. Interrupts
  114. ^^^^^^^^^^
  115. There are interrupts from host to slave, and from slave to host to help communicating conveniently.
  116. Slave Interrupts
  117. """"""""""""""""
  118. The host can interrupt the slave by writing any one bit in the register 0x08D. Once any bit of the register is
  119. set, an interrupt is raised and the SDIO slave driver calls the callback function defined in the ``slave_intr_cb`` member
  120. in the ``sdio_slave_config_t`` structure.
  121. .. note:: The callback function is called in the ISR, do not use any delay, loop or spinlock in the callback.
  122. There's another set of functions can be used. You can call ``sdio_slave_wait_int`` to wait for an interrupt within a
  123. certain time, or call ``sdio_slave_clear_int`` to clear interrupts from host. The callback function can work with the
  124. wait functions perfectly.
  125. Host Interrupts
  126. """""""""""""""
  127. The slave can interrupt the host by an interrupt line (at certain time) which is level sensitive. When the host see the
  128. interrupt line pulled down, it may read the slave interrupt status register, to see the interrupt source. Host can clear
  129. interrupt bits, or choose to disable a interrupt source. The interrupt line will hold active until all the sources are
  130. cleared or disabled.
  131. There are several dedicated interrupt sources as well as general purpose sources. see ``sdio_slave_hostint_t`` for
  132. more information.
  133. Shared Registers
  134. ^^^^^^^^^^^^^^^^
  135. There are 52 8-bit R/W shared registers to share information between host and slave. The slave can write or read the
  136. registers at any time by ``sdio_slave_read_reg`` and ``sdio_slave_write_reg``. The host can access (R/W) the register by CMD52 or CMD53.
  137. Receiving FIFO
  138. ^^^^^^^^^^^^^^
  139. When the host is going to send the slave some packets, it has to check whether the slave is ready to receive by reading
  140. the buffer number of slave.
  141. To allow the host sending data to the slave, the application has to load buffers to the slave driver by the following steps:
  142. 1. Register the buffer by calling ``sdio_slave_recv_register_buf``, and get the handle of the registered buffer. The driver
  143. will allocate memory for the linked-list descriptor needed to link the buffer onto the hardware. The size of these buffers should equal to the Receiving buffer size.
  144. 2. Load buffers onto the driver by passing the buffer handle to ``sdio_slave_recv_load_buf``.
  145. 3. Get the received data by calling ``sdio_slave_recv`` or ``sdio_slave_recv_packet``. If non-blocking call is needed, set ``wait=0``.
  146. The difference between two APIs is that, ``sdio_slave_recv_packet`` gives more information about packet, which can consist of several buffers. When ``ESP_ERR_NOT_FINISHED`` is returned by this API, you should call this API iteratively until the return value is ``ESP_OK``. All the continuous buffers returned with ``ESP_ERR_NOT_FINISHED``, together with the last buffer returned with ``ESP_OK``, belong to one packet from the host. Call ``sdio_slave_recv_get_buf`` to get the address of the received data, and the actual length received in each buffer. The packet length is the sum of received length of all the buffers in the packet.
  147. If the host never send data longer than the Receiving buffer size, or you don't care about the packet boundary (e.g. the data is only a byte stream), you can call the simpler version ``sdio_slave_recv`` instead.
  148. 4. Pass the handle of processed buffer back to the driver by ``sdio_recv_load_buf`` again.
  149. .. note:: To avoid overhead from copying data, the driver itself doesn't have any buffer inside, the application is
  150. responsible to offer new buffers in time. The DMA will automatically store received data to the buffer.
  151. Sending FIFO
  152. ^^^^^^^^^^^^
  153. Each time the slave has data to send, it raises an interrupt and the host will request for the packet length. There are
  154. two sending modes:
  155. - Stream Mode: when a buffer is loaded to the driver, the buffer length will be counted into the packet length requested
  156. by host in the incoming communications. Regardless previous packets are sent or not. This means the host can get data
  157. of several buffers in one transfer.
  158. - Packet Mode: the packet length is updated packet by packet, and only when previous packet is sent. This means that the
  159. host can only get data of one buffer in one transfer.
  160. .. note:: To avoid overhead from copying data, the driver itself doesn't have any buffer inside. Namely, the DMA takes
  161. data directly from the buffer provided by the application. The application should not touch the buffer until the
  162. sending is finished.
  163. The sending mode can be set in the ``sending_mode`` member of ``sdio_slave_config_t``, and the buffer numbers can be
  164. set in the ``send_queue_size``. All the buffers are restricted to be no larger than 4092 bytes. Though in the stream
  165. mode several buffers can be sent in one transfer, each buffer is still counted as one in the queue.
  166. The application can call ``sdio_slave_transmit`` to send packets. In this case the function returns when the transfer
  167. is successfully done, so the queue is not fully used. When higher effeciency is required, the application can use the
  168. following functions instead:
  169. 1. Pass buffer information (address, length, as well as an ``arg`` indicating the buffer) to ``sdio_slave_send_queue``.
  170. If non-blocking call is needed, set ``wait=0``. If the ``wait`` is not ``portMAX_DELAY`` (wait until success),
  171. application has to check the result to know whether the data is put in to the queue or discard.
  172. 2. Call ``sdio_slave_send_get_finished`` to get and deal with a finished transfer. A buffer should be keep unmodified
  173. until returned from ``sdio_slave_send_get_finished``. This means the buffer is actually sent to the host, rather
  174. than just staying in the queue.
  175. There are several ways to use the ``arg`` in the queue parameter:
  176. 1. Directly point ``arg`` to a dynamic-allocated buffer, and use the ``arg`` to free it when transfer finished.
  177. 2. Wrap transfer informations in a transfer structure, and point ``arg`` to the structure. You can use the
  178. structure to do more things like::
  179. typedef struct {
  180. uint8_t* buffer;
  181. size_t size;
  182. int id;
  183. }sdio_transfer_t;
  184. //and send as:
  185. sdio_transfer_t trans = {
  186. .buffer = ADDRESS_TO_SEND,
  187. .size = 8,
  188. .id = 3, //the 3rd transfer so far
  189. };
  190. sdio_slave_send_queue(trans.buffer, trans.size, &trans, portMAX_DELAY);
  191. //... maybe more transfers are sent here
  192. //and deal with finished transfer as:
  193. sdio_transfer_t* arg = NULL;
  194. sdio_slave_send_get_finished((void**)&arg, portMAX_DELAY);
  195. ESP_LOGI("tag", "(%d) successfully send %d bytes of %p", arg->id, arg->size, arg->buffer);
  196. some_post_callback(arg); //do more things
  197. 3. Working with the receiving part of this driver, point ``arg`` to the receive buffer handle of this buffer. So
  198. that we can directly use the buffer to receive data when it's sent::
  199. uint8_t buffer[256]={1,2,3,4,5,6,7,8};
  200. sdio_slave_buf_handle_t handle = sdio_slave_recv_register_buf(buffer);
  201. sdio_slave_send_queue(buffer, 8, handle, portMAX_DELAY);
  202. //... maybe more transfers are sent here
  203. //and load finished buffer to receive as
  204. sdio_slave_buf_handle_t handle = NULL;
  205. sdio_slave_send_get_finished((void**)&handle, portMAX_DELAY);
  206. sdio_slave_recv_load_buf(handle);
  207. More about this, see :example:`peripherals/sdio`.
  208. Application Example
  209. -------------------
  210. Slave/master communication: :example:`peripherals/sdio`.
  211. API Reference
  212. -------------
  213. .. include-build-file:: inc/sdio_slave_types.inc
  214. .. include-build-file:: inc/sdio_slave.inc