esp_async_memcpy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "hal/dma_types.h"
  10. #include "esp_check.h"
  11. #include "esp_heap_caps.h"
  12. #include "esp_log.h"
  13. #include "esp_async_memcpy.h"
  14. #include "esp_async_memcpy_impl.h"
  15. static const char *TAG = "async_memcpy";
  16. #define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
  17. /**
  18. * @brief Type of async mcp stream
  19. * mcp stream inherits DMA descriptor, besides that, it has a callback function member
  20. */
  21. typedef struct {
  22. dma_descriptor_t desc;
  23. async_memcpy_isr_cb_t cb;
  24. void *cb_args;
  25. } async_memcpy_stream_t;
  26. /**
  27. * @brief Type of async mcp driver context
  28. */
  29. typedef struct async_memcpy_context_t {
  30. async_memcpy_impl_t mcp_impl; // implementation layer
  31. portMUX_TYPE spinlock; // spinlock, prevent operating descriptors concurrently
  32. intr_handle_t intr_hdl; // interrupt handle
  33. uint32_t flags; // extra driver flags
  34. dma_descriptor_t *tx_desc; // pointer to the next free TX descriptor
  35. dma_descriptor_t *rx_desc; // pointer to the next free RX descriptor
  36. dma_descriptor_t *next_rx_desc_to_check; // pointer to the next RX descriptor to recycle
  37. uint32_t max_stream_num; // maximum number of streams
  38. size_t max_dma_buffer_size; // maximum DMA buffer size
  39. async_memcpy_stream_t *out_streams; // pointer to the first TX stream
  40. async_memcpy_stream_t *in_streams; // pointer to the first RX stream
  41. async_memcpy_stream_t streams_pool[0]; // stream pool (TX + RX), the size is configured during driver installation
  42. } async_memcpy_context_t;
  43. esp_err_t esp_async_memcpy_install(const async_memcpy_config_t *config, async_memcpy_t *asmcp)
  44. {
  45. esp_err_t ret = ESP_OK;
  46. async_memcpy_context_t *mcp_hdl = NULL;
  47. ESP_GOTO_ON_FALSE(config, ESP_ERR_INVALID_ARG, err, TAG, "configuration can't be null");
  48. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "can't assign mcp handle to null");
  49. // context memory size + stream pool size
  50. size_t total_malloc_size = sizeof(async_memcpy_context_t) + sizeof(async_memcpy_stream_t) * config->backlog * 2;
  51. // to work when cache is disabled, the driver handle should located in SRAM
  52. mcp_hdl = heap_caps_calloc(1, total_malloc_size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  53. ESP_GOTO_ON_FALSE(mcp_hdl, ESP_ERR_NO_MEM, err, TAG, "allocate context memory failed");
  54. mcp_hdl->flags = config->flags;
  55. mcp_hdl->out_streams = mcp_hdl->streams_pool;
  56. mcp_hdl->in_streams = mcp_hdl->streams_pool + config->backlog;
  57. mcp_hdl->max_stream_num = config->backlog;
  58. // circle TX/RX descriptors
  59. for (size_t i = 0; i < mcp_hdl->max_stream_num; i++) {
  60. mcp_hdl->out_streams[i].desc.dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_CPU;
  61. mcp_hdl->out_streams[i].desc.next = &mcp_hdl->out_streams[i + 1].desc;
  62. mcp_hdl->in_streams[i].desc.dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_CPU;
  63. mcp_hdl->in_streams[i].desc.next = &mcp_hdl->in_streams[i + 1].desc;
  64. }
  65. mcp_hdl->out_streams[mcp_hdl->max_stream_num - 1].desc.next = &mcp_hdl->out_streams[0].desc;
  66. mcp_hdl->in_streams[mcp_hdl->max_stream_num - 1].desc.next = &mcp_hdl->in_streams[0].desc;
  67. mcp_hdl->tx_desc = &mcp_hdl->out_streams[0].desc;
  68. mcp_hdl->rx_desc = &mcp_hdl->in_streams[0].desc;
  69. mcp_hdl->next_rx_desc_to_check = &mcp_hdl->in_streams[0].desc;
  70. mcp_hdl->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  71. mcp_hdl->mcp_impl.sram_trans_align = config->sram_trans_align;
  72. mcp_hdl->mcp_impl.psram_trans_align = config->psram_trans_align;
  73. size_t trans_align = MAX(config->sram_trans_align, config->psram_trans_align);
  74. mcp_hdl->max_dma_buffer_size = trans_align ? ALIGN_DOWN(DMA_DESCRIPTOR_BUFFER_MAX_SIZE, trans_align) : DMA_DESCRIPTOR_BUFFER_MAX_SIZE;
  75. // initialize implementation layer
  76. ret = async_memcpy_impl_init(&mcp_hdl->mcp_impl);
  77. ESP_GOTO_ON_ERROR(ret, err, TAG, "DMA M2M init failed");
  78. *asmcp = mcp_hdl;
  79. async_memcpy_impl_start(&mcp_hdl->mcp_impl, (intptr_t)&mcp_hdl->out_streams[0].desc, (intptr_t)&mcp_hdl->in_streams[0].desc);
  80. return ESP_OK;
  81. err:
  82. if (mcp_hdl) {
  83. free(mcp_hdl);
  84. }
  85. if (asmcp) {
  86. *asmcp = NULL;
  87. }
  88. return ret;
  89. }
  90. esp_err_t esp_async_memcpy_uninstall(async_memcpy_t asmcp)
  91. {
  92. esp_err_t ret = ESP_OK;
  93. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "mcp handle can't be null");
  94. async_memcpy_impl_stop(&asmcp->mcp_impl);
  95. async_memcpy_impl_deinit(&asmcp->mcp_impl);
  96. free(asmcp);
  97. err:
  98. return ret;
  99. }
  100. static int async_memcpy_prepare_receive(async_memcpy_t asmcp, void *buffer, size_t size, dma_descriptor_t **start_desc, dma_descriptor_t **end_desc)
  101. {
  102. uint32_t prepared_length = 0;
  103. uint8_t *buf = (uint8_t *)buffer;
  104. dma_descriptor_t *desc = asmcp->rx_desc; // descriptor iterator
  105. dma_descriptor_t *start = desc;
  106. dma_descriptor_t *end = desc;
  107. while (size > asmcp->max_dma_buffer_size) {
  108. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  109. desc->dw0.suc_eof = 0;
  110. desc->dw0.size = asmcp->max_dma_buffer_size;
  111. desc->buffer = &buf[prepared_length];
  112. desc = desc->next; // move to next descriptor
  113. prepared_length += asmcp->max_dma_buffer_size;
  114. size -= asmcp->max_dma_buffer_size;
  115. } else {
  116. // out of RX descriptors
  117. goto _exit;
  118. }
  119. }
  120. if (size) {
  121. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  122. end = desc; // the last descriptor used
  123. desc->dw0.suc_eof = 0;
  124. desc->dw0.size = size;
  125. desc->buffer = &buf[prepared_length];
  126. desc = desc->next; // move to next descriptor
  127. prepared_length += size;
  128. } else {
  129. // out of RX descriptors
  130. goto _exit;
  131. }
  132. }
  133. _exit:
  134. *start_desc = start;
  135. *end_desc = end;
  136. return prepared_length;
  137. }
  138. static int async_memcpy_prepare_transmit(async_memcpy_t asmcp, void *buffer, size_t len, dma_descriptor_t **start_desc, dma_descriptor_t **end_desc)
  139. {
  140. uint32_t prepared_length = 0;
  141. uint8_t *buf = (uint8_t *)buffer;
  142. dma_descriptor_t *desc = asmcp->tx_desc; // descriptor iterator
  143. dma_descriptor_t *start = desc;
  144. dma_descriptor_t *end = desc;
  145. while (len > asmcp->max_dma_buffer_size) {
  146. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  147. desc->dw0.suc_eof = 0; // not the end of the transaction
  148. desc->dw0.size = asmcp->max_dma_buffer_size;
  149. desc->dw0.length = asmcp->max_dma_buffer_size;
  150. desc->buffer = &buf[prepared_length];
  151. desc = desc->next; // move to next descriptor
  152. prepared_length += asmcp->max_dma_buffer_size;
  153. len -= asmcp->max_dma_buffer_size;
  154. } else {
  155. // out of TX descriptors
  156. goto _exit;
  157. }
  158. }
  159. if (len) {
  160. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  161. end = desc; // the last descriptor used
  162. desc->dw0.suc_eof = 1; // end of the transaction
  163. desc->dw0.size = len;
  164. desc->dw0.length = len;
  165. desc->buffer = &buf[prepared_length];
  166. desc = desc->next; // move to next descriptor
  167. prepared_length += len;
  168. } else {
  169. // out of TX descriptors
  170. goto _exit;
  171. }
  172. }
  173. *start_desc = start;
  174. *end_desc = end;
  175. _exit:
  176. return prepared_length;
  177. }
  178. static bool async_memcpy_get_next_rx_descriptor(async_memcpy_t asmcp, dma_descriptor_t *eof_desc, dma_descriptor_t **next_desc)
  179. {
  180. dma_descriptor_t *next = asmcp->next_rx_desc_to_check;
  181. // additional check, to avoid potential interrupt got triggered by mistake
  182. if (next->dw0.owner == DMA_DESCRIPTOR_BUFFER_OWNER_CPU) {
  183. asmcp->next_rx_desc_to_check = asmcp->next_rx_desc_to_check->next;
  184. *next_desc = next;
  185. // return if we need to continue
  186. return eof_desc == next ? false : true;
  187. }
  188. *next_desc = NULL;
  189. return false;
  190. }
  191. esp_err_t esp_async_memcpy(async_memcpy_t asmcp, void *dst, void *src, size_t n, async_memcpy_isr_cb_t cb_isr, void *cb_args)
  192. {
  193. esp_err_t ret = ESP_OK;
  194. dma_descriptor_t *rx_start_desc = NULL;
  195. dma_descriptor_t *rx_end_desc = NULL;
  196. dma_descriptor_t *tx_start_desc = NULL;
  197. dma_descriptor_t *tx_end_desc = NULL;
  198. size_t rx_prepared_size = 0;
  199. size_t tx_prepared_size = 0;
  200. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "mcp handle can't be null");
  201. ESP_GOTO_ON_FALSE(async_memcpy_impl_is_buffer_address_valid(&asmcp->mcp_impl, src, dst), ESP_ERR_INVALID_ARG, err, TAG, "buffer address not valid: %p -> %p", src, dst);
  202. ESP_GOTO_ON_FALSE(n <= asmcp->max_dma_buffer_size * asmcp->max_stream_num, ESP_ERR_INVALID_ARG, err, TAG, "buffer size too large");
  203. if (asmcp->mcp_impl.sram_trans_align) {
  204. ESP_GOTO_ON_FALSE(((n & (asmcp->mcp_impl.sram_trans_align - 1)) == 0), ESP_ERR_INVALID_ARG, err, TAG, "copy size should align to %d bytes", asmcp->mcp_impl.sram_trans_align);
  205. }
  206. if (asmcp->mcp_impl.psram_trans_align) {
  207. ESP_GOTO_ON_FALSE(((n & (asmcp->mcp_impl.psram_trans_align - 1)) == 0), ESP_ERR_INVALID_ARG, err, TAG, "copy size should align to %d bytes", asmcp->mcp_impl.psram_trans_align);
  208. }
  209. // Prepare TX and RX descriptor
  210. portENTER_CRITICAL_SAFE(&asmcp->spinlock);
  211. rx_prepared_size = async_memcpy_prepare_receive(asmcp, dst, n, &rx_start_desc, &rx_end_desc);
  212. tx_prepared_size = async_memcpy_prepare_transmit(asmcp, src, n, &tx_start_desc, &tx_end_desc);
  213. if (rx_start_desc && tx_start_desc && (rx_prepared_size == n) && (tx_prepared_size == n)) {
  214. // register user callback to the last descriptor
  215. async_memcpy_stream_t *mcp_stream = __containerof(rx_end_desc, async_memcpy_stream_t, desc);
  216. mcp_stream->cb = cb_isr;
  217. mcp_stream->cb_args = cb_args;
  218. // restart RX firstly
  219. dma_descriptor_t *desc = rx_start_desc;
  220. while (desc != rx_end_desc) {
  221. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  222. desc = desc->next;
  223. }
  224. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  225. asmcp->rx_desc = desc->next;
  226. // restart TX secondly
  227. desc = tx_start_desc;
  228. while (desc != tx_end_desc) {
  229. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  230. desc = desc->next;
  231. }
  232. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  233. asmcp->tx_desc = desc->next;
  234. async_memcpy_impl_restart(&asmcp->mcp_impl);
  235. }
  236. portEXIT_CRITICAL_SAFE(&asmcp->spinlock);
  237. // It's unlikely that we have space for rx descriptor but no space for tx descriptor
  238. // Both tx and rx descriptor should move in the same pace
  239. ESP_GOTO_ON_FALSE(rx_prepared_size == n, ESP_FAIL, err, TAG, "out of rx descriptor");
  240. ESP_GOTO_ON_FALSE(tx_prepared_size == n, ESP_FAIL, err, TAG, "out of tx descriptor");
  241. err:
  242. return ret;
  243. }
  244. IRAM_ATTR void async_memcpy_isr_on_rx_done_event(async_memcpy_impl_t *impl)
  245. {
  246. bool to_continue = false;
  247. async_memcpy_stream_t *in_stream = NULL;
  248. dma_descriptor_t *next_desc = NULL;
  249. async_memcpy_context_t *asmcp = __containerof(impl, async_memcpy_context_t, mcp_impl);
  250. // get the RX eof descriptor address
  251. dma_descriptor_t *eof = (dma_descriptor_t *)impl->rx_eof_addr;
  252. // traversal all unchecked descriptors
  253. do {
  254. portENTER_CRITICAL_ISR(&asmcp->spinlock);
  255. // There is an assumption that the usage of rx descriptors are in the same pace as tx descriptors (this is determined by M2M DMA working mechanism)
  256. // And once the rx descriptor is recycled, the corresponding tx desc is guaranteed to be returned by DMA
  257. to_continue = async_memcpy_get_next_rx_descriptor(asmcp, eof, &next_desc);
  258. portEXIT_CRITICAL_ISR(&asmcp->spinlock);
  259. if (next_desc) {
  260. in_stream = __containerof(next_desc, async_memcpy_stream_t, desc);
  261. // invoke user registered callback if available
  262. if (in_stream->cb) {
  263. async_memcpy_event_t e = {0};
  264. if (in_stream->cb(asmcp, &e, in_stream->cb_args)) {
  265. impl->isr_need_yield = true;
  266. }
  267. in_stream->cb = NULL;
  268. in_stream->cb_args = NULL;
  269. }
  270. }
  271. } while (to_continue);
  272. }