esp_slave.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2015-2018 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. #include "sdmmc_cmd.h"
  14. #include "driver/sdmmc_defs.h"
  15. #include "soc/sdio_slave_periph.h"
  16. /*
  17. * NOTE: This component is for example purpose only. Assertion fails if any of
  18. * the preconditions (connections, grounding, slave data preparation, etc.) is
  19. * not met.
  20. * Please do check and handle the return value in your real product.
  21. */
  22. #define ESP_ERR_NOT_FINISHED 0x201
  23. /** Context used by the ``esp_slave`` component.
  24. */
  25. typedef struct {
  26. sdmmc_card_t* card; ///< Initialized sdmmc_cmd card
  27. uint16_t buffer_size;
  28. ///< All data that do not fully fill a buffer is still counted as one buffer. E.g. 10 bytes data costs 2 buffers if the size is 8 bytes per buffer.
  29. ///< Buffer size of the slave pre-defined between host and slave before communication.
  30. uint16_t block_size;
  31. ///< If this is too large, it takes time to send stuff bits; while if too small, intervals between blocks cost much.
  32. ///< Should be set according to length of data, and larger than ``TRANS_LEN_MAX/511``.
  33. ///< Block size of the SDIO function 1. After the initialization this will hold the value the slave really do. Valid value is 1-2048.
  34. size_t tx_sent_buffers; ///< Counter hold the amount of buffers already sent to ESP32 slave. Should be set to 0 when initialization.
  35. size_t rx_got_bytes; ///< Counter hold the amount of bytes already received from ESP32 slave. Should be set to 0 when initialization.
  36. } esp_slave_context_t;
  37. /** Initialize ``esp_slave_context_t`` by this macro.
  38. */
  39. #define ESP_SLAVE_DEFAULT_CONTEXT(card) (esp_slave_context_t){\
  40. .card = card, \
  41. .block_size = 0x200, \
  42. .buffer_size = 128, \
  43. .tx_sent_buffers = 0, \
  44. .rx_got_bytes = 0, \
  45. }
  46. /** SDIO Initialize process of a ESP32 slave device.
  47. *
  48. * @param context Context of the ``esp_slave`` component. Send to other functions later.
  49. *
  50. * @return
  51. * - ESP_OK if success
  52. * - One of the error codes from SDMMC host controller
  53. */
  54. esp_err_t esp_slave_init_io(esp_slave_context_t *context);
  55. /** Wait for interrupt of a ESP32 slave device.
  56. *
  57. * @param context Context of the ``esp_slave`` component.
  58. *
  59. * @return
  60. * - ESP_OK if success
  61. * - One of the error codes from SDMMC host controller
  62. */
  63. esp_err_t esp_slave_wait_for_ioready(esp_slave_context_t *context);
  64. /** Get buffer num for the host to send data to the slave. The buffers are size of ``buffer_size``.
  65. *
  66. * @param context Context of the component.
  67. * @param tx_num Output of buffer num that host can send data to ESP32 slave.
  68. *
  69. * @return
  70. * - ESP_OK Success
  71. * - One of the error codes from SDMMC host controller
  72. */
  73. esp_err_t esp_slave_get_tx_buffer_num(esp_slave_context_t *context, uint32_t* tx_num);
  74. /** Get amount of data the ESP32 slave preparing to send to host.
  75. *
  76. * @param context Context of the component.
  77. * @param rx_size Output of data size to read from slave.
  78. *
  79. * @return
  80. * - ESP_OK Success
  81. * - One of the error codes from SDMMC host controller
  82. */
  83. esp_err_t esp_slave_get_rx_data_size(esp_slave_context_t *context, uint32_t* rx_size);
  84. /** Reset the counters of this component. Usually you don't need to do this unless you know the slave is reset.
  85. *
  86. * @param context Context of the component.
  87. */
  88. inline static void esp_slave_reset_cnt(esp_slave_context_t *context)
  89. {
  90. context->rx_got_bytes = 0;
  91. context->tx_sent_buffers = 0;
  92. }
  93. /** Send a packet to the ESP32 slave. The slave receive the packet into buffers whose size is ``buffer_size`` in the context.
  94. *
  95. * @param context Context of the component.
  96. * @param start Start address of the packet to send
  97. * @param length Length of data to send, if the packet is over-size, the it will be divided into blocks and hold into different buffers automatically.
  98. * @param wait_ms Time to wait before timeout, in ms.
  99. *
  100. * @return
  101. * - ESP_OK Success
  102. * - ESP_ERR_TIMEOUT No buffer to use, or error ftrom SDMMC host controller
  103. * - One of the error codes from SDMMC host controller
  104. */
  105. esp_err_t esp_slave_send_packet(esp_slave_context_t *context, const void* start, size_t length, uint32_t wait_ms);
  106. /** Get a packet from ESP32 slave.
  107. *
  108. * @param context Context of the component.
  109. * @param[out] out_data Data output address
  110. * @param size The size of the output buffer, if the buffer is smaller than the size of data to receive from slave, the driver returns ``ESP_ERR_NOT_FINISHED``
  111. * @param[out] out_length Output of length the data actually received from slave.
  112. * @param wait_ms Time to wait before timeout, in ms.
  113. *
  114. * @return
  115. * - ESP_OK Success, all the data are read from the slave.
  116. * - ESP_ERR_NOT_FINISHED Read success, while there're data remaining.
  117. * - One of the error codes from SDMMC host controller
  118. */
  119. esp_err_t esp_slave_get_packet(esp_slave_context_t *context, void* out_data, size_t size, size_t *out_length, uint32_t wait_ms);
  120. /** wait for an interrupt of the slave
  121. *
  122. * @param context Context of the component.
  123. * @param wait Ticks to wait.
  124. *
  125. * @return
  126. * - ESP_ERR_NOT_SUPPORTED Currently our driver doesnot support SDIO with SPI interface.
  127. * - ESP_OK If interrupt triggered.
  128. * - ESP_ERR_TIMEOUT No interrupts before timeout.
  129. */
  130. inline static esp_err_t esp_slave_wait_int(esp_slave_context_t *context, TickType_t wait)
  131. {
  132. return sdmmc_io_wait_int(context->card, wait);
  133. }
  134. /** Clear interrupt bits of ESP32 slave. All the bits set in the mask will be cleared, while other bits will stay the same.
  135. *
  136. * @param context Context of the component.
  137. * @param intr_mask Mask of interrupt bits to clear.
  138. *
  139. * @return
  140. * - ESP_OK Success
  141. * - One of the error codes from SDMMC host controller
  142. */
  143. esp_err_t esp_slave_clear_intr(esp_slave_context_t *context, uint32_t intr_mask);
  144. /** Get interrupt bits of ESP32 slave.
  145. *
  146. * @param context Context of the component.
  147. * @param intr_raw Output of the raw interrupt bits. Set to NULL if only masked bits are read.
  148. * @param intr_st Output of the masked interrupt bits. set to NULL if only raw bits are read.
  149. *
  150. * @return
  151. * - ESP_OK Success
  152. * - ESP_INVALID_ARG if both ``intr_raw`` and ``intr_st`` are NULL.
  153. * - One of the error codes from SDMMC host controller
  154. */
  155. esp_err_t esp_slave_get_intr(esp_slave_context_t *context, uint32_t *intr_raw, uint32_t *intr_st);
  156. /** Set interrupt enable bits of ESP32 slave. The slave only sends interrupt on the line when there is a bit both the raw status and the enable are set.
  157. *
  158. * @param context Context of the component.
  159. * @param ena_mask Mask of the interrupt bits to enable.
  160. *
  161. * @return
  162. * - ESP_OK Success
  163. * - One of the error codes from SDMMC host controller
  164. */
  165. esp_err_t esp_slave_set_intr_ena(esp_slave_context_t *context, uint32_t ena_mask);
  166. /** Get interrupt enable bits of ESP32 slave.
  167. *
  168. * @param context Context of the component.
  169. * @param ena_mask_o Output of interrupt bit enable mask.
  170. *
  171. * @return
  172. * - ESP_OK Success
  173. * - One of the error codes from SDMMC host controller
  174. */
  175. esp_err_t esp_slave_get_intr_ena(esp_slave_context_t *context, uint32_t *ena_mask_o);
  176. /** Write general purpose R/W registers (8-bit) of ESP32 slave.
  177. *
  178. * @param context Context of the component.
  179. * @param addr Address of register to write. Valid address: 0-27, 32-63 (28-31 reserved).
  180. * @param value Value to write to the register.
  181. *
  182. * @return
  183. * - ESP_OK Success
  184. * - ESP_ERR_INVALID_ARG Address not valid.
  185. * - One of the error codes from SDMMC host controller
  186. */
  187. esp_err_t esp_slave_write_reg(esp_slave_context_t *context, uint8_t addr, uint8_t value, uint8_t* value_o);
  188. /** Read general purpose R/W registers (8-bit) of ESP32 slave.
  189. *
  190. * @param context Context of the component.
  191. * @param add Address of register to read. Valid address: 0-27, 32-63 (28-31 reserved, return interrupt bits on read).
  192. * @param value Output value read from the register.
  193. *
  194. * @return
  195. * - ESP_OK Success
  196. * - ESP_ERR_INVALID_ARG Address not valid.
  197. * - One of the error codes from SDMMC host controller
  198. */
  199. esp_err_t esp_slave_read_reg(esp_slave_context_t *context, uint8_t add, uint8_t *value_o);
  200. /** Send interrupts to slave. Each bit of the interrupt will be triggered.
  201. *
  202. * @param context Context of the component.
  203. * @param intr_mask Mask of interrupt bits to send to slave.
  204. *
  205. * @return
  206. * - ESP_OK Success
  207. * - One of the error codes from SDMMC host controller
  208. */
  209. esp_err_t esp_slave_send_slave_intr(esp_slave_context_t *context, uint8_t intr_mask);