spi_slave.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. SPI Slave driver
  2. =================
  3. Overview
  4. --------
  5. The ESP32 has four SPI peripheral devices, called SPI0, SPI1, HSPI and VSPI. SPI0 is entirely dedicated to
  6. the flash cache the ESP32 uses to map the SPI flash device it is connected to into memory. SPI1 is
  7. connected to the same hardware lines as SPI0 and is used to write to the flash chip. HSPI and VSPI
  8. are free to use, and with the spi_slave driver, these can be used as a SPI slave, driven from a
  9. connected SPI master.
  10. The spi_slave driver
  11. ^^^^^^^^^^^^^^^^^^^^^
  12. The spi_slave driver allows using the HSPI and/or VSPI peripheral as a
  13. full-duplex SPI slave. It can send/receive transactions within 64 bytes, or
  14. make use of DMA to send/receive transactions longer than that. However, there
  15. are some `known issues <spi_dma_known_issues>`_ when the DMA is enabled.
  16. Terminology
  17. ^^^^^^^^^^^
  18. The spi_slave driver uses the following terms:
  19. * Host: The SPI peripheral inside the ESP32 initiating the SPI transmissions. One of HSPI or VSPI.
  20. * Bus: The SPI bus, common to all SPI devices connected to a master. In general the bus consists of the
  21. miso, mosi, sclk and optionally quadwp and quadhd signals. The SPI slaves are connected to these
  22. signals in parallel. Each SPI slave is also connected to one CS signal.
  23. - miso - Also known as q, this is the output of the serial stream from the ESP32 to the SPI master
  24. - mosi - Also known as d, this is the output of the serial stream from the SPI master to the ESP32
  25. - sclk - Clock signal. Each data bit is clocked out or in on the positive or negative edge of this signal
  26. - cs - Chip Select. An active Chip Select delineates a single transaction to/from a slave.
  27. * Transaction: One instance of CS going active, data transfer from and to a master happening, and
  28. CS going inactive again. Transactions are atomic, as in they will never be interrupted by another
  29. transaction.
  30. SPI transactions
  31. ^^^^^^^^^^^^^^^^
  32. A full-duplex SPI transaction starts with the master pulling CS low. After this happens, the master
  33. starts sending out clock pulses on the CLK line: every clock pulse causes a data bit to be shifted from
  34. the master to the slave on the MOSI line and vice versa on the MISO line. At the end of the transaction,
  35. the master makes CS high again.
  36. .. note:: The SPI slave peripheral relies on the control of software very
  37. much. The master shouldn't start a transaction when the slave hasn't prepared
  38. for it. Using one more GPIO as the handshake signal to sync is a good idea.
  39. For more details, see :ref:`transaction_interval`.
  40. GPIO matrix and IOMUX
  41. ^^^^^^^^^^^^^^^^^^^^^
  42. Most peripheral signals in ESP32 can connect directly to a specific GPIO,
  43. which is called its IOMUX pin. When a peripheral signal is routed to a pin
  44. other than its IOMUX pin, ESP32 uses the less direct GPIO matrix to make this
  45. connection.
  46. If the driver is configured with all SPI signals set to their specific IOMUX
  47. pins (or left unconnected), it will bypass the GPIO matrix. If any SPI signal
  48. is configured to a pin other than its IOMUx pin, the driver will
  49. automatically route all the signals via the GPIO Matrix. The GPIO matrix
  50. samples all signals at 80MHz and sends them between the GPIO and the
  51. peripheral.
  52. When the GPIO matrix is used, setup time of MISO is more easily violated,
  53. since the output delay of MISO signal is increased.
  54. .. note:: More details about influence of output delay on the maximum clock
  55. frequency, see :ref:`timing_considerations` below.
  56. IOMUX pins for SPI controllers are as below:
  57. +----------+------+------+
  58. | Pin Name | HSPI | VSPI |
  59. + +------+------+
  60. | | GPIO Number |
  61. +==========+======+======+
  62. | CS0* | 15 | 5 |
  63. +----------+------+------+
  64. | SCLK | 14 | 18 |
  65. +----------+------+------+
  66. | MISO | 12 | 19 |
  67. +----------+------+------+
  68. | MOSI | 13 | 23 |
  69. +----------+------+------+
  70. | QUADWP | 2 | 22 |
  71. +----------+------+------+
  72. | QUADHD | 4 | 21 |
  73. +----------+------+------+
  74. note * Only the first device attaching to the bus can use CS0 pin.
  75. Using the spi_slave driver
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. - Initialize a SPI peripheral as a slave by calling ``spi_slave_initialize``. Make sure to set the
  78. correct IO pins in the ``bus_config`` struct. Take care to set signals that are not needed to -1.
  79. A DMA channel (either 1 or 2) must be given if transactions will be larger than 32 bytes, if not
  80. the dma_chan parameter may be 0.
  81. - To set up a transaction, fill one or more spi_transaction_t structure with any transaction
  82. parameters you need. Either queue all transactions by calling ``spi_slave_queue_trans``, later
  83. quering the result using ``spi_slave_get_trans_result``, or handle all requests synchroneously
  84. by feeding them into ``spi_slave_transmit``. The latter two functions will block until the
  85. master has initiated and finished a transaction, causing the queued data to be sent and received.
  86. - Optional: to unload the SPI slave driver, call ``spi_slave_free``.
  87. Transaction data and master/slave length mismatches
  88. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  89. Normally, data to be transferred to or from a device will be read from or written to a chunk of memory
  90. indicated by the ``rx_buffer`` and ``tx_buffer`` members of the transaction structure. The SPI driver
  91. may decide to use DMA for transfers, so these buffers should be allocated in DMA-capable memory using
  92. ``pvPortMallocCaps(size, MALLOC_CAP_DMA)``.
  93. The amount of data written to the buffers is limited by the ``length`` member of the transaction structure:
  94. the driver will never read/write more data than indicated there. The ``length`` cannot define the actual
  95. length of the SPI transaction; this is determined by the master as it drives the clock and CS lines. The actual length
  96. transferred can be read from the ``trans_len`` member of the ``spi_slave_transaction_t`` structure after transaction.
  97. In case the length of the transmission is larger than the buffer length, only the start of the transmission
  98. will be sent and received, and the ``trans_len`` is set to ``length`` instead of the actual length. It's recommended to
  99. set ``length`` longer than the maximum length expected if the ``trans_len`` is required. In case the transmission
  100. length is shorter than the buffer length, only data up to the length of the buffer will be exchanged.
  101. Warning: Due to a design peculiarity in the ESP32, if the amount of bytes sent by the master or the length
  102. of the transmission queues in the slave driver, in bytes, is not both larger than eight and dividable by
  103. four, the SPI hardware can fail to write the last one to seven bytes to the receive buffer.
  104. Speed and Timing considerations
  105. -------------------------------
  106. .. _transaction_interval:
  107. Transaction interval
  108. ^^^^^^^^^^^^^^^^^^^^
  109. The SPI slave is designed as s general purpose device controlled by the CPU.
  110. Different from dedicated devices, CPU-based SPI slave doesn't have too much
  111. pre-defined registers. All transactions should be triggered by the CPU, which
  112. means the response speed would not be real-time, and there'll always be
  113. noticeable intervals between transfers.
  114. During the transaction intervals, the device is not prepared for
  115. transactions, the response is not meaningful at all. It is suggested to use
  116. :cpp:func:`spi_slave_queue_trans` with :cpp:func:`spi_slave_get_trans_result`
  117. to shorten the interval to half the case when using
  118. :cpp:func:`spi_slave_transmit`.
  119. The master should always wait for the slave to be ready to start new
  120. transactions. Suggested way is to use a gpio by the slave to indicate whether
  121. it's ready. The example is in :example:`peripherals/spi_slave`.
  122. SCLK frequency requirement
  123. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  124. The spi slave is designed to work under 10MHz or lower. The clock and data
  125. cannot be recognized or received correctly if the clock is too fast or
  126. doesn't have a 50% duty cycle.
  127. Moreover, there are more requirements if the data meets the timing
  128. requirement:
  129. - Read (MOSI):
  130. Given that the MOSI is valid right at the launch edge, the slave can
  131. read data correctly. Luckily, it's usually the case for most masters.
  132. - Write (MISO):
  133. To meet the requirement that MISO is stable before the next latch edge of
  134. SPI clock, the output delay of MISO signal should be shorter than half a
  135. clock. The output delay and frequency limitation (given that the clock is
  136. balanced) of different cases are as below :
  137. +-------------+---------------------------+------------------------+
  138. | | Output delay of MISO (ns) | Freq. limit (MHZ) |
  139. +=============+===========================+========================+
  140. | IOMUX | 43.75 | <11.4 |
  141. +-------------+---------------------------+------------------------+
  142. | GPIO matrix | 68.75 | <7.2 |
  143. +-------------+---------------------------+------------------------+
  144. Note:
  145. 1. Random error will happen if the frequency exactly equals the
  146. limitation
  147. 2. The clock uncertainty between master and slave (12.5ns) is
  148. included.
  149. 3. The output delay is measured under ideal case (free of load). When
  150. the loading of MISO pin is too heavy, the output delay will be longer,
  151. and the maximum allowed frequency will be lower.
  152. There is an exceptions: The frequency is allowed to be higher if the
  153. master has more toleration for the MISO setup time, e.g. latch data at
  154. the next edge than expected, or configurable latching time.
  155. .. _spi_dma_known_issues:
  156. Restrictions and Known issues
  157. -------------------------------
  158. 1. If the DMA is enabled, the rx buffer should be WORD aligned, i.e. Start
  159. from the boundary of 32-bit and have length of multiples of 4 bytes. Or the
  160. DMA may write incorrectly or out of the boundary.The driver will check for
  161. this.
  162. Also, master should write lengths which are a multiple of 4 bytes. Data
  163. longer than that will be discarded.
  164. 2. Furthurmore, the DMA requires a spi mode 1/3 timing. When using spi mode
  165. 0/2, the MISO signal has to output half a clock earlier to meet the timing.
  166. The new timing is as below:
  167. .. image:: /../_static/spi_slave_miso_dma.png
  168. The hold time after the latch edge is 68.75ns (when GPIO matrix is
  169. bypassed), no longer half a SPI clock. The master should sample immediately
  170. at the latch edge, or communicate in mode 1/3. Or just initial the spi
  171. slave without DMA.
  172. Application Example
  173. -------------------
  174. Slave/master communication: :example:`peripherals/spi_slave`.
  175. API Reference
  176. -------------
  177. .. include:: /_build/inc/spi_slave.inc