spi_slave.rst 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 full-duplex SPI slave. It can make
  13. use of DMA to send/receive transactions of arbitrary length.
  14. Terminology
  15. ^^^^^^^^^^^
  16. The spi_slave driver uses the following terms:
  17. * Host: The SPI peripheral inside the ESP32 initiating the SPI transmissions. One of HSPI or VSPI.
  18. * Bus: The SPI bus, common to all SPI devices connected to a master. In general the bus consists of the
  19. miso, mosi, sclk and optionally quadwp and quadhd signals. The SPI slaves are connected to these
  20. signals in parallel. Each SPI slave is also connected to one CS signal.
  21. - miso - Also known as q, this is the output of the serial stream from the ESP32 to the SPI master
  22. - mosi - Also known as d, this is the output of the serial stream from the SPI master to the ESP32
  23. - sclk - Clock signal. Each data bit is clocked out or in on the positive or negative edge of this signal
  24. - cs - Chip Select. An active Chip Select delineates a single transaction to/from a slave.
  25. * Transaction: One instance of CS going active, data transfer from and to a master happening, and
  26. CS going inactive again. Transactions are atomic, as in they will never be interrupted by another
  27. transaction.
  28. SPI transactions
  29. ^^^^^^^^^^^^^^^^
  30. A full-duplex SPI transaction starts with the master pulling CS low. After this happens, the master
  31. starts sending out clock pulses on the CLK line: every clock pulse causes a data bit to be shifted from
  32. the master to the slave on the MOSI line and vice versa on the MISO line. At the end of the transaction,
  33. the master makes CS high again.
  34. Using the spi_slave driver
  35. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  36. - Initialize a SPI peripheral as a slave by calling ``spi_slave_initialize``. Make sure to set the
  37. correct IO pins in the ``bus_config`` struct. Take care to set signals that are not needed to -1.
  38. A DMA channel (either 1 or 2) must be given if transactions will be larger than 32 bytes, if not
  39. the dma_chan parameter may be 0.
  40. - To set up a transaction, fill one or more spi_transaction_t structure with any transaction
  41. parameters you need. Either queue all transactions by calling ``spi_slave_queue_trans``, later
  42. quering the result using ``spi_slave_get_trans_result``, or handle all requests synchroneously
  43. by feeding them into ``spi_slave_transmit``. The latter two functions will block until the
  44. master has initiated and finished a transaction, causing the queued data to be sent and received.
  45. - Optional: to unload the SPI slave driver, call ``spi_slave_free``.
  46. Transaction data and master/slave length mismatches
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. Normally, data to be transferred to or from a device will be read from or written to a chunk of memory
  49. indicated by the ``rx_buffer`` and ``tx_buffer`` members of the transaction structure. The SPI driver
  50. may decide to use DMA for transfers, so these buffers should be allocated in DMA-capable memory using
  51. ``pvPortMallocCaps(size, MALLOC_CAP_DMA)``.
  52. The amount of data written to the buffers is limited by the ``length`` member of the transaction structure:
  53. the driver will never read/write more data than indicated there. The ``length`` cannot define the actual
  54. length of the SPI transaction; this is determined by the master as it drives the clock and CS lines. The actual length
  55. transferred can be read from the ``trans_len`` member of the ``spi_slave_transaction_t`` structure after transaction.
  56. In case the length of the transmission is larger than the buffer length, only the start of the transmission
  57. will be sent and received, and the ``trans_len`` is set to ``length`` instead of the actual length. It's recommended to
  58. set ``length`` longer than the maximum length expected if the ``trans_len`` is required. In case the transmission
  59. length is shorter than the buffer length, only data up to the length of the buffer will be exchanged.
  60. Warning: Due to a design peculiarity in the ESP32, if the amount of bytes sent by the master or the length
  61. of the transmission queues in the slave driver, in bytes, is not both larger than eight and dividable by
  62. four, the SPI hardware can fail to write the last one to seven bytes to the receive buffer.
  63. Application Example
  64. -------------------
  65. Slave/master communication: :example:`peripherals/spi_slave`.
  66. API Reference
  67. -------------
  68. .. include:: /_build/inc/spi_slave.inc