esp_slave_protocol.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. Communication with ESP SDIO Slave
  2. ====================================
  3. .. _esp_slave_init:
  4. ESP SDIO slave initialization
  5. ------------------------------
  6. The host should initialize the ESP32 SDIO slave according to the standard
  7. SDIO initialization process (Sector 3.1.2 of `SDIO Simplified
  8. Specification <https://www.sdcard.org/downloads/pls/>`_). In this specification
  9. and below, the SDIO slave is also called an (SD)IO card. All the
  10. initialization CMD52 and CMD53 are sent to Func 0 (CIA region). Here is an
  11. brief example on how to do this:
  12. 1. SDIO reset
  13. CMD52 (Write 0x6=0x8)
  14. 2. SD reset
  15. CMD0
  16. 3. Check whether IO card (optional)
  17. CMD8
  18. 4. Send SDIO op cond and wait for card ready
  19. CMD5 arg = 0x00000000
  20. CMD5 arg = 0x00ff8000 (according to the response above, poll until ready)
  21. **Example:**
  22. Arg of R4 after first CMD5 (arg=0x00000000) is 0xXXFFFF00.
  23. Keep sending CMD5 with arg=0x00FFFF00 until the R4 shows card ready (arg bit 31=1).
  24. 5. Set address
  25. CMD3
  26. 6. Select card
  27. CMD7 (arg address according to CMD3 response)
  28. **Example:**
  29. Arg of R6 after CMD3 is 0x0001xxxx.
  30. Arg of CMD7 should be 0x00010000.
  31. 7. Select 4-bit mode (optional)
  32. CMD52 (Write 0x07=0x02)
  33. 8. Enable func1
  34. CMD52 (Write 0x02=0x02)
  35. 9. Enable SDIO interrupt (required if interrupt line (DAT1) is used)
  36. CMD52 (Write 0x04=0x03)
  37. 10. Set Func0 blocksize (optional, default value is 512 (0x200))
  38. CMD52/53 (Read 0x10~0x11)
  39. CMD52/53 (Write 0x10=0x00)
  40. CMD52/53 (Write 0x11=0x02)
  41. CMD52/53 (Read 0x10~0x11, read to check the final value)
  42. 11. Set Func1 blocksize (optional, default value is 512 (0x200))
  43. CMD52/53 (Read 0x110~0x111)
  44. CMD52/53 (Write 0x110=0x00)
  45. CMD52/53 (Write 0x111=0x02)
  46. CMD52/53 (Read 0x110~0x111, read to check the final value)
  47. .. _esp_slave_protocol_layer:
  48. ESP SDIO slave protocol
  49. -----------------------
  50. The protocol is based on Function 1 access by CMD52 and CMD53, offering 3 services: (1) sending and receiving FIFO, (2) 52 8-bit R/W
  51. register shared by host and slave, (3) 8 general purpose interrupt sources from host to slave and 8 in the opposite direction.
  52. There is a component `esp_serial_slave_link` implementing the logic of this protocol for
  53. ESP32 master to communicate with the ESP32 slave. See :doc:`/api-reference/protocols/esp_serial_slave_link`.
  54. The host should access the registers below as described to communicate with slave.
  55. Slave register table
  56. ^^^^^^^^^^^^^^^^^^^^
  57. 32-bit
  58. #######
  59. - 0x044 (TOKEN_RDATA): in which bit 27-16 holds the receiving buffer number.
  60. - 0x058 (INT_ST): holds the interrupt source bits from slave to host.
  61. - 0x060 (PKT_LEN): holds the accumulated length (by byte) to be sent from slave to host.
  62. - 0x0D4 (INT_CLR): write 1 to clear interrupt bits corresponding to INT_ST.
  63. - 0x0DC (INT_ENA): mask bits for interrupts from slave to host.
  64. 8-bit
  65. #####
  66. Shared general purpose registers:
  67. - 0x06C-0x077: R/W registers 0-11 shared by slave and host.
  68. - 0x07A-0x07B: R/W registers 14-15 shared by slave and host.
  69. - 0x07E-0x07F: R/W registers 18-19 shared by slave and host.
  70. - 0x088-0x08B: R/W registers 24-27 shared by slave and host.
  71. - 0x09C-0x0BB: R/W registers 32-63 shared by slave and host.
  72. Interrupt Registers:
  73. - 0x08D (SLAVE_INT): bits for host to interrupt slave. auto clear.
  74. FIFO (sending and receiving)
  75. ############################
  76. 0x090 - 0x1F7FF are reserved for FIFOs.
  77. The address of CMD53 is related to the length requested to read from/write to
  78. the slave in a single transfer:
  79. *requested length = 0x1F800-address*
  80. The slave will respond with the length according to the length field in
  81. CMD53, with the data longer than *requested length* filled with 0 (sending)
  82. or discard (receiving).
  83. .. note:: This includes both the block and the byte mode of CMD53.
  84. The function number should be set to 1, OP Code should be set to 1 (for CMD53).
  85. It is allowed to use CMD53 mode combination of block+byte to get higher
  86. effeciency when accessing the FIFO by arbitrary length. E.g. The block
  87. size is set to 512 by default, you can write/get 1031 bytes of data
  88. to/from the FIFO by:
  89. 1. Send CMD53 in block mode, block count=2 (1024 bytes) to address
  90. 0x1F3F9=0x1F800-**1031**.
  91. 2. Then send CMD53 in byte mode, byte count=8 (or 7 if your controller
  92. supports that) to address 0x1F7F9=0x1F800-**7**.
  93. Interrupts
  94. ^^^^^^^^^^
  95. For the host interrupts, the slave raise the interrupt by pulling DAT1 line down at a proper time (level sensitive).
  96. The host detect this and read the INT_ST register to see the source. Then the host can clear it by writing the INT_CLR
  97. register and do something with the interrupt. The host can also mask unneeded sources by clearing the bits in INT_ENA
  98. register corresponding to the sources. If all the sources are cleared (or masked), the DAT1 line goes inactive.
  99. ``sdio_slave_hostint_t`` (:doc:`sdio_slave`) shows the bit definition corresponding to host interrupt sources.
  100. For the slave interrupts, the host send transfers to write the SLAVE_INT register. Once a bit is written from 0 to 1,
  101. the slave hardware and driver will detect it and inform the app.
  102. Receiving FIFO
  103. ^^^^^^^^^^^^^^
  104. To write the receiving FIFO in the slave, host should work in the following steps:
  105. 1. Read the TOKEN1 field (bits 27-16) of TOKEN_RDATA (0x044) register. The buffer number remaining is TOKEN1 minus
  106. the number of buffers used by host.
  107. 2. Make sure the buffer number is sufficient (*recv_buffer_size* * *buffer_num* is greater than data to write, *recv_buffer_size*
  108. is pre-defined between the host and the slave before the communication starts). Or go back to step 1 until the buffer
  109. is enough.
  110. 3. Write to the FIFO address with CMD53. Note that the *requested length* should not be larger than calculated in step 2,
  111. and the FIFO address is related to *rquested length*.
  112. 4. Calculate used buffers, note that non-full buffer at the tail should be seen as one that is used.
  113. Sending FIFO
  114. ^^^^^^^^^^^^
  115. To read the sending FIFO in the slave, host should work in the following steps:
  116. 1. Wait for the interrupt line to be active (optional, low by default).
  117. 2. Read (poll) the interrupt bits in INT_ST register to see whether new packets exists.
  118. 3. If new packets are ready, reads the PKT_LEN reg. The data length to read from slave is PKT_LEN minuses the length
  119. that has been read from the host. If the PKT_LEN is not larger than used, wait and poll until the slave is ready and
  120. update the PKT_LEN.
  121. 4. Read from the FIFO with CMD53. Note that the *requested length* should not be larger than calculated in step3, and
  122. the FIFO address is related to *requested length*.
  123. 5. Recored read length.