spi_slave.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _DRIVER_SPI_SLAVE_H_
  7. #define _DRIVER_SPI_SLAVE_H_
  8. #include "esp_err.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/semphr.h"
  11. #include "driver/spi_common.h"
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. #define SPI_SLAVE_TXBIT_LSBFIRST (1<<0) ///< Transmit command/address/data LSB first instead of the default MSB first
  17. #define SPI_SLAVE_RXBIT_LSBFIRST (1<<1) ///< Receive data LSB first instead of the default MSB first
  18. #define SPI_SLAVE_BIT_LSBFIRST (SPI_SLAVE_TXBIT_LSBFIRST|SPI_SLAVE_RXBIT_LSBFIRST) ///< Transmit and receive LSB first
  19. #define SPI_SLAVE_NO_RETURN_RESULT (1<<2) ///< Don't return the descriptor to the host on completion (use `post_trans_cb` to notify instead)
  20. /** @cond */
  21. typedef struct spi_slave_transaction_t spi_slave_transaction_t;
  22. /** @endcond */
  23. typedef void(*slave_transaction_cb_t)(spi_slave_transaction_t *trans);
  24. /**
  25. * @brief This is a configuration for a SPI host acting as a slave device.
  26. */
  27. typedef struct {
  28. int spics_io_num; ///< CS GPIO pin for this device
  29. uint32_t flags; ///< Bitwise OR of SPI_SLAVE_* flags
  30. int queue_size; ///< Transaction queue size. This sets how many transactions can be 'in the air' (queued using spi_slave_queue_trans but not yet finished using spi_slave_get_trans_result) at the same time
  31. uint8_t mode; /**< SPI mode, representing a pair of (CPOL, CPHA) configuration:
  32. - 0: (0, 0)
  33. - 1: (0, 1)
  34. - 2: (1, 0)
  35. - 3: (1, 1)
  36. */
  37. slave_transaction_cb_t post_setup_cb; /**< Callback called after the SPI registers are loaded with new data.
  38. *
  39. * This callback is called within interrupt
  40. * context should be in IRAM for best
  41. * performance, see "Transferring Speed"
  42. * section in the SPI Master documentation for
  43. * full details. If not, the callback may crash
  44. * during flash operation when the driver is
  45. * initialized with ESP_INTR_FLAG_IRAM.
  46. */
  47. slave_transaction_cb_t post_trans_cb; /**< Callback called after a transaction is done.
  48. *
  49. * This callback is called within interrupt
  50. * context should be in IRAM for best
  51. * performance, see "Transferring Speed"
  52. * section in the SPI Master documentation for
  53. * full details. If not, the callback may crash
  54. * during flash operation when the driver is
  55. * initialized with ESP_INTR_FLAG_IRAM.
  56. */
  57. } spi_slave_interface_config_t;
  58. #define SPI_SLAVE_TRANS_DMA_BUFFER_ALIGN_AUTO (1<<0) ///< Automatically re-malloc dma buffer if user buffer doesn't meet hardware alignment or dma_capable, this process may loss some memory and performance
  59. /**
  60. * This structure describes one SPI transaction
  61. */
  62. struct spi_slave_transaction_t {
  63. uint32_t flags; ///< Bitwise OR of SPI_SLAVE_TRANS_* flags
  64. size_t length; ///< Total data length, in bits
  65. size_t trans_len; ///< Transaction data length, in bits
  66. const void *tx_buffer; ///< Pointer to transmit buffer, or NULL for no MOSI phase
  67. void *rx_buffer; /**< Pointer to receive buffer, or NULL for no MISO phase.
  68. * When the DMA is enabled, must start at WORD boundary (``rx_buffer%4==0``),
  69. * and has length of a multiple of 4 bytes.
  70. */
  71. void *user; ///< User-defined variable. Can be used to store eg transaction ID.
  72. };
  73. /**
  74. * @brief Initialize a SPI bus as a slave interface
  75. *
  76. * @warning SPI0/1 is not supported
  77. *
  78. * @param host SPI peripheral to use as a SPI slave interface
  79. * @param bus_config Pointer to a spi_bus_config_t struct specifying how the host should be initialized
  80. * @param slave_config Pointer to a spi_slave_interface_config_t struct specifying the details for the slave interface
  81. * @param dma_chan - Selecting a DMA channel for an SPI bus allows transactions on the bus with size only limited by the amount of internal memory.
  82. * - Selecting SPI_DMA_DISABLED limits the size of transactions.
  83. * - Set to SPI_DMA_DISABLED if only the SPI flash uses this bus.
  84. * - Set to SPI_DMA_CH_AUTO to let the driver to allocate the DMA channel.
  85. *
  86. * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in
  87. * DMA-capable memory.
  88. *
  89. * @warning The ISR of SPI is always executed on the core which calls this
  90. * function. Never starve the ISR on this core or the SPI transactions will not
  91. * be handled.
  92. *
  93. * @return
  94. * - ESP_ERR_INVALID_ARG if configuration is invalid
  95. * - ESP_ERR_INVALID_STATE if host already is in use
  96. * - ESP_ERR_NOT_FOUND if there is no available DMA channel
  97. * - ESP_ERR_NO_MEM if out of memory
  98. * - ESP_OK on success
  99. */
  100. esp_err_t spi_slave_initialize(spi_host_device_t host, const spi_bus_config_t *bus_config, const spi_slave_interface_config_t *slave_config, spi_dma_chan_t dma_chan);
  101. /**
  102. * @brief Free a SPI bus claimed as a SPI slave interface
  103. *
  104. * @param host SPI peripheral to free
  105. * @return
  106. * - ESP_ERR_INVALID_ARG if parameter is invalid
  107. * - ESP_ERR_INVALID_STATE if not all devices on the bus are freed
  108. * - ESP_OK on success
  109. */
  110. esp_err_t spi_slave_free(spi_host_device_t host);
  111. /**
  112. * @brief Queue a SPI transaction for execution
  113. *
  114. * Queues a SPI transaction to be executed by this slave device. (The transaction queue size was specified when the slave
  115. * device was initialised via spi_slave_initialize.) This function may block if the queue is full (depending on the
  116. * ticks_to_wait parameter). No SPI operation is directly initiated by this function, the next queued transaction
  117. * will happen when the master initiates a SPI transaction by pulling down CS and sending out clock signals.
  118. *
  119. * This function hands over ownership of the buffers in ``trans_desc`` to the SPI slave driver; the application is
  120. * not to access this memory until ``spi_slave_queue_trans`` is called to hand ownership back to the application.
  121. *
  122. * @param host SPI peripheral that is acting as a slave
  123. * @param trans_desc Description of transaction to execute. Not const because we may want to write status back
  124. * into the transaction description.
  125. * @param ticks_to_wait Ticks to wait until there's room in the queue; use portMAX_DELAY to
  126. * never time out.
  127. * @return
  128. * - ESP_ERR_INVALID_ARG if parameter is invalid
  129. * - ESP_ERR_NO_MEM if set flag `SPI_SLAVE_TRANS_DMA_BUFFER_ALIGN_AUTO` but there is no free memory
  130. * - ESP_ERR_INVALID_STATE if sync data between Cache and memory failed
  131. * - ESP_OK on success
  132. */
  133. esp_err_t spi_slave_queue_trans(spi_host_device_t host, const spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait);
  134. /**
  135. * @brief Get the result of a SPI transaction queued earlier
  136. *
  137. * This routine will wait until a transaction to the given device (queued earlier with
  138. * spi_slave_queue_trans) has succesfully completed. It will then return the description of the
  139. * completed transaction so software can inspect the result and e.g. free the memory or
  140. * re-use the buffers.
  141. *
  142. * It is mandatory to eventually use this function for any transaction queued by ``spi_slave_queue_trans``.
  143. *
  144. * @param host SPI peripheral to that is acting as a slave
  145. * @param[out] trans_desc Pointer to variable able to contain a pointer to the description of the
  146. * transaction that is executed
  147. * @param ticks_to_wait Ticks to wait until there's a returned item; use portMAX_DELAY to never time
  148. * out.
  149. * @return
  150. * - ESP_ERR_INVALID_ARG if parameter is invalid
  151. * - ESP_ERR_NOT_SUPPORTED if flag `SPI_SLAVE_NO_RETURN_RESULT` is set
  152. * - ESP_OK on success
  153. */
  154. esp_err_t spi_slave_get_trans_result(spi_host_device_t host, spi_slave_transaction_t **trans_desc, TickType_t ticks_to_wait);
  155. /**
  156. * @brief Do a SPI transaction
  157. *
  158. * Essentially does the same as spi_slave_queue_trans followed by spi_slave_get_trans_result. Do
  159. * not use this when there is still a transaction queued that hasn't been finalized
  160. * using spi_slave_get_trans_result.
  161. *
  162. * @param host SPI peripheral to that is acting as a slave
  163. * @param trans_desc Pointer to variable able to contain a pointer to the description of the
  164. * transaction that is executed. Not const because we may want to write status back
  165. * into the transaction description.
  166. * @param ticks_to_wait Ticks to wait until there's a returned item; use portMAX_DELAY to never time
  167. * out.
  168. * @return
  169. * - ESP_ERR_INVALID_ARG if parameter is invalid
  170. * - ESP_OK on success
  171. */
  172. esp_err_t spi_slave_transmit(spi_host_device_t host, spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait);
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif