1The Async memcpy API 2==================== 3 4Overview 5-------- 6 7{IDF_TARGET_NAME} has a DMA engine which can help to offload internal memory copy operations from the CPU in a asynchronous way. 8 9The async memcpy API wraps all DMA configurations and operations, the signature of :cpp:func:`esp_async_memcpy` is almost the same to the standard libc one. 10 11Thanks to the benefit of the DMA, we don't have to wait for each memory copy to be done before we issue another memcpy request. By the way, it's still possible to know when memcpy is finished by listening in the memcpy callback function. 12 13.. only:: esp32s2 14 15 .. note:: 16 Memory copy from/to external PSRAM is not supported on ESP32-S2, :cpp:func:`esp_async_memcpy` will abort returning an error if buffer address is not in SRAM. 17 18Configure and Install driver 19---------------------------- 20 21:cpp:func:`esp_async_memcpy_install` is used to install the driver with user's configuration. Please note that async memcpy has to be called with the handle returned from :cpp:func:`esp_async_memcpy_install`. 22 23Driver configuration is described in :cpp:type:`async_memcpy_config_t`: 24:cpp:member:`backlog`: This is used to configured the maximum number of DMA operation that can be working at the background at the same time. 25:cpp:member:`sram_trans_align`: Declare SRAM alignment for both data address and copy size, set to zero if the data has no restriction in alignment. If set to a quadruple value (i.e. 4X), the driver will enable the burst mode internally, which is helpful for some performance related application. 26:cpp:member:`psram_trans_align`: Declare PSRAM alignment for both data address and copy size. User has to give it a valid value (only 16, 32, 64 are supported) if the destination of memcpy is located in PSRAM. The default alignment (i.e. 16) will be applied if it's set to zero. Internally, the driver configures the size of block used by DMA to access PSRAM, according to the alignment. 27:cpp:member:`flags`: This is used to enable some special driver features. 28 29:c:macro:`ASYNC_MEMCPY_DEFAULT_CONFIG` provides a default configuration, which specifies the backlog to 8. 30 31.. highlight:: c 32 33:: 34 35 async_memcpy_config_t config = ASYNC_MEMCPY_DEFAULT_CONFIG(); 36 // update the maximum data stream supported by underlying DMA engine 37 config.backlog = 16; 38 async_memcpy_t driver = NULL; 39 ESP_ERROR_CHECK(esp_async_memcpy_install(&config, &driver)); // install driver, return driver handle 40 41Send memory copy request 42------------------------ 43 44:cpp:func:`esp_async_memcpy` is the API to send memory copy request to DMA engine. It must be called after driver is installed successfully. This API is thread safe, so it can be called from different tasks. 45 46Different from the libc version of `memcpy`, user should also pass a callback to :cpp:func:`esp_async_memcpy`, if it's necessary to be notified when the memory copy is done. The callback is executed in the ISR context, make sure you won't violate the the restriction applied to ISR handler. 47 48Besides that, the callback function should reside in IRAM space by applying `IRAM_ATTR` attribute. The prototype of the callback function is :cpp:type:`async_memcpy_isr_cb_t`, please note that, the callback function should return true if it wakes up a high priority task by some API like :cpp:func:`xSemaphoreGiveFromISR`. 49 50.. highlight:: c 51 52:: 53 54 Semphr_Handle_t semphr; //already initialized in somewhere 55 56 // Callback implementation, running in ISR context 57 static IRAM_ATTR bool my_async_memcpy_cb(async_memcpy_t mcp_hdl, async_memcpy_event_t *event, void *cb_args) 58 { 59 SemaphoreHandle_t sem = (SemaphoreHandle_t)cb_args; 60 BaseType_t high_task_wakeup = pdFALSE; 61 SemphrGiveInISR(semphr, &high_task_wakeup); // high_task_wakeup set to pdTRUE if some high priority task unblocked 62 return high_task_wakeup == pdTRUE; 63 } 64 65 // Called from user's context 66 ESP_ERROR_CHECK(esp_async_memcpy(driver_handle, to, from, copy_len, my_async_memcpy_cb, my_semaphore)); 67 //Do something else here 68 SemphrTake(my_semaphore, ...); //wait until the buffer copy is done 69 70Uninstall driver (optional) 71--------------------------- 72 73:cpp:func:`esp_async_memcpy_uninstall` is used to uninstall asynchronous memcpy driver. It's not necessary to uninstall the driver after each memcpy operation. If you know your application won't use this driver anymore, then this API can recycle the memory for you. 74 75API Reference 76------------- 77 78.. include-build-file:: inc/esp_async_memcpy.inc 79