spi_slave.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2010-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _DRIVER_SPI_SLAVE_H_
  14. #define _DRIVER_SPI_SLAVE_H_
  15. #include "esp_err.h"
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/semphr.h"
  18. #include "driver/spi_common.h"
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. #define SPI_SLAVE_TXBIT_LSBFIRST (1<<0) ///< Transmit command/address/data LSB first instead of the default MSB first
  24. #define SPI_SLAVE_RXBIT_LSBFIRST (1<<1) ///< Receive data LSB first instead of the default MSB first
  25. #define SPI_SLAVE_BIT_LSBFIRST (SPI_TXBIT_LSBFIRST|SPI_RXBIT_LSBFIRST); ///< Transmit and receive LSB first
  26. typedef struct spi_slave_transaction_t spi_slave_transaction_t;
  27. typedef void(*slave_transaction_cb_t)(spi_slave_transaction_t *trans);
  28. /**
  29. * @brief This is a configuration for a SPI host acting as a slave device.
  30. */
  31. typedef struct {
  32. int spics_io_num; ///< CS GPIO pin for this device
  33. uint32_t flags; ///< Bitwise OR of SPI_SLAVE_* flags
  34. 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
  35. uint8_t mode; ///< SPI mode (0-3)
  36. slave_transaction_cb_t post_setup_cb; ///< Callback called after the SPI registers are loaded with new data
  37. slave_transaction_cb_t post_trans_cb; ///< Callback called after a transaction is done
  38. } spi_slave_interface_config_t;
  39. /**
  40. * This structure describes one SPI transaction
  41. */
  42. struct spi_slave_transaction_t {
  43. size_t length; ///< Total data length, in bits
  44. size_t trans_len; ///< Transaction data length, in bits
  45. const void *tx_buffer; ///< Pointer to transmit buffer, or NULL for no MOSI phase
  46. void *rx_buffer; ///< Pointer to receive buffer, or NULL for no MISO phase
  47. void *user; ///< User-defined variable. Can be used to store eg transaction ID.
  48. };
  49. /**
  50. * @brief Initialize a SPI bus as a slave interface
  51. *
  52. * @warning For now, only supports HSPI and VSPI.
  53. *
  54. * @param host SPI peripheral to use as a SPI slave interface
  55. * @param bus_config Pointer to a spi_bus_config_t struct specifying how the host should be initialized
  56. * @param slave_config Pointer to a spi_slave_interface_config_t struct specifying the details for the slave interface
  57. * @param dma_chan Either 1 or 2. A SPI bus used by this driver must have a DMA channel associated with
  58. * it. The SPI hardware has two DMA channels to share. This parameter indicates which
  59. * one to use.
  60. *
  61. * @warning If a DMA channel is selected, any transmit and receive buffer used should be allocated in
  62. * DMA-capable memory.
  63. *
  64. * @return
  65. * - ESP_ERR_INVALID_ARG if configuration is invalid
  66. * - ESP_ERR_INVALID_STATE if host already is in use
  67. * - ESP_ERR_NO_MEM if out of memory
  68. * - ESP_OK on success
  69. */
  70. 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, int dma_chan);
  71. /**
  72. * @brief Free a SPI bus claimed as a SPI slave interface
  73. *
  74. * @param host SPI peripheral to free
  75. * @return
  76. * - ESP_ERR_INVALID_ARG if parameter is invalid
  77. * - ESP_ERR_INVALID_STATE if not all devices on the bus are freed
  78. * - ESP_OK on success
  79. */
  80. esp_err_t spi_slave_free(spi_host_device_t host);
  81. /**
  82. * @brief Queue a SPI transaction for execution
  83. *
  84. * Queues a SPI transaction to be executed by this slave device. (The transaction queue size was specified when the slave
  85. * device was initialised via spi_slave_initialize.) This function may block if the queue is full (depending on the
  86. * ticks_to_wait parameter). No SPI operation is directly initiated by this function, the next queued transaction
  87. * will happen when the master initiates a SPI transaction by pulling down CS and sending out clock signals.
  88. *
  89. * This function hands over ownership of the buffers in ``trans_desc`` to the SPI slave driver; the application is
  90. * not to access this memory until ``spi_slave_queue_trans`` is called to hand ownership back to the application.
  91. *
  92. * @param host SPI peripheral that is acting as a slave
  93. * @param trans_desc Description of transaction to execute. Not const because we may want to write status back
  94. * into the transaction description.
  95. * @param ticks_to_wait Ticks to wait until there's room in the queue; use portMAX_DELAY to
  96. * never time out.
  97. * @return
  98. * - ESP_ERR_INVALID_ARG if parameter is invalid
  99. * - ESP_OK on success
  100. */
  101. esp_err_t spi_slave_queue_trans(spi_host_device_t host, const spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait);
  102. /**
  103. * @brief Get the result of a SPI transaction queued earlier
  104. *
  105. * This routine will wait until a transaction to the given device (queued earlier with
  106. * spi_slave_queue_trans) has succesfully completed. It will then return the description of the
  107. * completed transaction so software can inspect the result and e.g. free the memory or
  108. * re-use the buffers.
  109. *
  110. * It is mandatory to eventually use this function for any transaction queued by ``spi_slave_queue_trans``.
  111. *
  112. * @param host SPI peripheral to that is acting as a slave
  113. * @param[out] trans_desc Pointer to variable able to contain a pointer to the description of the
  114. * transaction that is executed
  115. * @param ticks_to_wait Ticks to wait until there's a returned item; use portMAX_DELAY to never time
  116. * out.
  117. * @return
  118. * - ESP_ERR_INVALID_ARG if parameter is invalid
  119. * - ESP_OK on success
  120. */
  121. esp_err_t spi_slave_get_trans_result(spi_host_device_t host, spi_slave_transaction_t **trans_desc, TickType_t ticks_to_wait);
  122. /**
  123. * @brief Do a SPI transaction
  124. *
  125. * Essentially does the same as spi_slave_queue_trans followed by spi_slave_get_trans_result. Do
  126. * not use this when there is still a transaction queued that hasn't been finalized
  127. * using spi_slave_get_trans_result.
  128. *
  129. * @param host SPI peripheral to that is acting as a slave
  130. * @param trans_desc Pointer to variable able to contain a pointer to the description of the
  131. * transaction that is executed. Not const because we may want to write status back
  132. * into the transaction description.
  133. * @param ticks_to_wait Ticks to wait until there's a returned item; use portMAX_DELAY to never time
  134. * out.
  135. * @return
  136. * - ESP_ERR_INVALID_ARG if parameter is invalid
  137. * - ESP_OK on success
  138. */
  139. esp_err_t spi_slave_transmit(spi_host_device_t host, spi_slave_transaction_t *trans_desc, TickType_t ticks_to_wait);
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif