esp_slave.h 9.1 KB

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