spi_master.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. SPI Master 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. SPI1, HSPI and VSPI all have three chip select lines, allowing them to drive up to
  9. three SPI devices each as a master. The SPI peripherals also can be used in slave mode, driven from
  10. another SPI master.
  11. The spi_master driver
  12. ^^^^^^^^^^^^^^^^^^^^^
  13. The spi_master driver allows easy communicating with SPI slave devices, even in a multithreaded environment.
  14. It fully transparently handles DMA transfers to read and write data and automatically takes care of
  15. multiplexing between different SPI slaves on the same master
  16. Terminology
  17. ^^^^^^^^^^^
  18. The spi_master driver uses the following terms:
  19. * Host: The SPI peripheral inside the ESP32 initiating the SPI transmissions. One of SPI, HSPI or VSPI. (For
  20. now, only HSPI or VSPI are actually supported in the driver; it will support all 3 peripherals
  21. somewhere in the future.)
  22. * Bus: The SPI bus, common to all SPI devices connected to one host. In general the bus consists of the
  23. spid, spiq, spiclk and optionally spiwp and spihd signals. The SPI slaves are connected to these
  24. signals in parallel.
  25. * Device: A SPI slave. Each SPI slave has its own chip select (CS) line, which is made active when
  26. a transmission to/from the SPI slave occurs.
  27. * Transaction: One instance of CS going active, data transfer from and/or to a device 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 transaction on the SPI bus consists of five phases, any of which may be skipped:
  33. * The command phase. In this phase, a command (0-16 bit) is clocked out.
  34. * The address phase. In this phase, an address (0-64 bit) is clocked out.
  35. * The read phase. The slave sends data to the master.
  36. * The write phase. The master sends data to the slave.
  37. In full duplex, the read and write phases are combined, causing the SPI host to read and
  38. write data simultaneously.
  39. The command and address phase are optional in that not every SPI device will need to be sent a command
  40. and/or address. Tis is reflected in the device configuration: when the ``command_bits`` or ``data_bits``
  41. fields are set to zero, no command or address phase is done.
  42. Something similar is true for the read and write phase: not every transaction needs both data to be written
  43. as well as data to be read. When ``rx_buffer`` is NULL (and SPI_USE_RXDATA) is not set) the read phase
  44. is skipped. When ``tx_buffer`` is NULL (and SPI_USE_TXDATA) is not set) the write phase is skipped.
  45. Using the spi_master driver
  46. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  47. - Initialize a SPI bus by calling ``spi_bus_initialize``. Make sure to set the correct IO pins in
  48. the ``bus_config`` struct. Take care to set signals that are not needed to -1.
  49. - Tell the driver about a SPI slave device conencted to the bus by calling spi_bus_add_device.
  50. Make sure to configure any timing requirements the device has in the ``dev_config`` structure.
  51. You should now have a handle for the device, to be used when sending it a transaction.
  52. - To interact with the device, fill one or more spi_transaction_t structure with any transaction
  53. parameters you need. Either queue all transactions by calling ``spi_device_queue_trans``, later
  54. quering the result using ``spi_device_get_trans_result``, or handle all requests synchroneously
  55. by feeding them into ``spi_device_transmit``.
  56. - Optional: to unload the driver for a device, call ``spi_bus_remove_device`` with the device
  57. handle as an argument
  58. - Optional: to remove the driver for a bus, make sure no more drivers are attached and call
  59. ``spi_bus_free``.
  60. Transaction data
  61. ^^^^^^^^^^^^^^^^
  62. Normally, data to be transferred to or from a device will be read from or written to a chunk of memory
  63. indicated by the ``rx_buffer`` and ``tx_buffer`` members of the transaction structure. The SPI driver
  64. may decide to use DMA for transfers, so these buffers should be allocated in DMA-capable memory using
  65. ``pvPortMallocCaps(size, MALLOC_CAP_DMA)``.
  66. Sometimes, the amount of data is very small making it less than optimal allocating a separate buffer
  67. for it. If the data to be transferred is 32 bits or less, it can be stored in the transaction struct
  68. itself. For transmitted data, use the ``tx_data`` member for this and set the ``SPI_USE_TXDATA`` flag
  69. on the transmission. For received data, use ``rx_data`` and set ``SPI_USE_RXDATA``. In both cases, do
  70. not touch the ``tx_buffer`` or ``rx_buffer`` members, because they use the same memory locations
  71. as ``tx_data`` and ``rx_data``.
  72. API Reference
  73. -------------
  74. Header Files
  75. ^^^^^^^^^^^^
  76. * `drivers/include/drivers/spi_master.h <https://github.com/espressif/esp-idf/blob/master/components/drivers/include/drivers/spi_master.h>`_
  77. Macros
  78. ^^^^^^
  79. .. doxygendefine:: SPI_DEVICE_TXBIT_LSBFIRST
  80. .. doxygendefine:: SPI_DEVICE_RXBIT_LSBFIRST
  81. .. doxygendefine:: SPI_DEVICE_BIT_LSBFIRST
  82. .. doxygendefine:: SPI_DEVICE_3WIRE
  83. .. doxygendefine:: SPI_DEVICE_POSITIVE_CS
  84. .. doxygendefine:: SPI_DEVICE_HALFDUPLEX
  85. .. doxygendefine:: SPI_DEVICE_CLK_AS_CS
  86. .. doxygendefine:: SPI_MODE_DIO
  87. .. doxygendefine:: SPI_MODE_QIO
  88. .. doxygendefine:: SPI_MODE_DIOQIO_ADDR
  89. .. doxygendefine:: SPI_USE_RXDATA
  90. .. doxygendefine:: SPI_USE_TXDATA
  91. Type Definitions
  92. ^^^^^^^^^^^^^^^^
  93. .. doxygentypedef:: spi_device_handle_t
  94. Enumerations
  95. ^^^^^^^^^^^^
  96. .. doxygenenum:: spi_host_device_t
  97. Structures
  98. ^^^^^^^^^^
  99. .. doxygenstruct:: spi_transaction_t
  100. :members:
  101. .. doxygenstruct:: spi_bus_config_t
  102. :members:
  103. .. doxygenstruct:: spi_device_interface_config_t
  104. :members:
  105. Functions
  106. ---------
  107. .. doxygenfunction:: spi_bus_initialize
  108. .. doxygenfunction:: spi_bus_free
  109. .. doxygenfunction:: spi_bus_add_device
  110. .. doxygenfunction:: spi_bus_remove_device
  111. .. doxygenfunction:: spi_device_queue_trans
  112. .. doxygenfunction:: spi_device_get_trans_result
  113. .. doxygenfunction:: spi_device_transmit