1 // Copyright 2020 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 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "freertos/FreeRTOS.h"
16 #include "soc/periph_defs.h"
17 #include "soc/soc_memory_layout.h"
18 #include "soc/soc_caps.h"
19 #include "hal/gdma_ll.h"
20 #include "hal/gdma_hal.h"
21 #include "driver/periph_ctrl.h"
22 #include "esp_log.h"
23 #include "esp_attr.h"
24 #include "esp_err.h"
25 #include "esp_async_memcpy_impl.h"
26 
async_memcpy_impl_rx_eof_callback(gdma_channel_handle_t dma_chan,gdma_event_data_t * event_data,void * user_data)27 IRAM_ATTR static bool async_memcpy_impl_rx_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
28 {
29     async_memcpy_impl_t *mcp_impl = (async_memcpy_impl_t *)user_data;
30     mcp_impl->rx_eof_addr = event_data->rx_eof_desc_addr;
31 
32     async_memcpy_isr_on_rx_done_event(mcp_impl);
33     return mcp_impl->isr_need_yield;
34 }
35 
async_memcpy_impl_init(async_memcpy_impl_t * impl)36 esp_err_t async_memcpy_impl_init(async_memcpy_impl_t *impl)
37 {
38     esp_err_t ret = ESP_OK;
39     // create TX channel and reserve sibling channel for future use
40     gdma_channel_alloc_config_t tx_alloc_config = {
41         .flags.reserve_sibling = 1,
42         .direction = GDMA_CHANNEL_DIRECTION_TX,
43     };
44     ret = gdma_new_channel(&tx_alloc_config, &impl->tx_channel);
45     if (ret != ESP_OK) {
46         goto err;
47     }
48 
49     // create RX channel and specify it should be reside in the same pair as TX
50     gdma_channel_alloc_config_t rx_alloc_config = {
51         .direction = GDMA_CHANNEL_DIRECTION_RX,
52         .sibling_chan = impl->tx_channel,
53     };
54     ret = gdma_new_channel(&rx_alloc_config, &impl->rx_channel);
55     if (ret != ESP_OK) {
56         goto err;
57     }
58 
59     gdma_connect(impl->rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_M2M, 0));
60     gdma_connect(impl->tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_M2M, 0));
61 
62     gdma_strategy_config_t strategy_config = {
63         .auto_update_desc = true,
64         .owner_check = true
65     };
66 
67     gdma_apply_strategy(impl->tx_channel, &strategy_config);
68     gdma_apply_strategy(impl->rx_channel, &strategy_config);
69 
70     gdma_rx_event_callbacks_t cbs = {
71         .on_recv_eof = async_memcpy_impl_rx_eof_callback
72     };
73     ret = gdma_register_rx_event_callbacks(impl->rx_channel, &cbs, impl);
74 
75 err:
76     return ret;
77 }
78 
async_memcpy_impl_deinit(async_memcpy_impl_t * impl)79 esp_err_t async_memcpy_impl_deinit(async_memcpy_impl_t *impl)
80 {
81     gdma_disconnect(impl->rx_channel);
82     gdma_disconnect(impl->tx_channel);
83     gdma_del_channel(impl->rx_channel);
84     gdma_del_channel(impl->tx_channel);
85     return ESP_OK;
86 }
87 
async_memcpy_impl_start(async_memcpy_impl_t * impl,intptr_t outlink_base,intptr_t inlink_base)88 esp_err_t async_memcpy_impl_start(async_memcpy_impl_t *impl, intptr_t outlink_base, intptr_t inlink_base)
89 {
90     gdma_start(impl->rx_channel, inlink_base);
91     gdma_start(impl->tx_channel, outlink_base);
92     return ESP_OK;
93 }
94 
async_memcpy_impl_stop(async_memcpy_impl_t * impl)95 esp_err_t async_memcpy_impl_stop(async_memcpy_impl_t *impl)
96 {
97     gdma_stop(impl->rx_channel);
98     gdma_stop(impl->tx_channel);
99     return ESP_OK;
100 }
101 
async_memcpy_impl_restart(async_memcpy_impl_t * impl)102 esp_err_t async_memcpy_impl_restart(async_memcpy_impl_t *impl)
103 {
104     gdma_append(impl->rx_channel);
105     gdma_append(impl->tx_channel);
106     return ESP_OK;
107 }
108 
async_memcpy_impl_is_buffer_address_valid(async_memcpy_impl_t * impl,void * src,void * dst)109 bool async_memcpy_impl_is_buffer_address_valid(async_memcpy_impl_t *impl, void *src, void *dst)
110 {
111     return true;
112 }
113