gdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. // #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  15. #include <stdlib.h>
  16. #include <sys/cdefs.h>
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "soc/soc_caps.h"
  20. #include "soc/periph_defs.h"
  21. #include "esp_intr_alloc.h"
  22. #include "esp_log.h"
  23. #include "driver/periph_ctrl.h"
  24. #include "esp_private/gdma.h"
  25. #include "hal/gdma_hal.h"
  26. #include "hal/gdma_ll.h"
  27. #include "soc/gdma_periph.h"
  28. static const char *TAG = "gdma";
  29. #define DMA_CHECK(a, msg, tag, ret, ...) \
  30. do { \
  31. if (unlikely(!(a))) { \
  32. ESP_LOGE(TAG, "%s(%d): " msg, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
  33. ret_code = ret; \
  34. goto tag; \
  35. } \
  36. } while (0)
  37. #define GDMA_INVALID_PERIPH_TRIG (0x3F)
  38. #define SEARCH_REQUEST_RX_CHANNEL (1 << 0)
  39. #define SEARCH_REQUEST_TX_CHANNEL (1 << 1)
  40. typedef struct gdma_platform_t gdma_platform_t;
  41. typedef struct gdma_group_t gdma_group_t;
  42. typedef struct gdma_pair_t gdma_pair_t;
  43. typedef struct gdma_channel_t gdma_channel_t;
  44. typedef struct gdma_tx_channel_t gdma_tx_channel_t;
  45. typedef struct gdma_rx_channel_t gdma_rx_channel_t;
  46. struct gdma_platform_t {
  47. portMUX_TYPE spinlock; // platform level spinlock
  48. gdma_group_t *groups[SOC_GDMA_GROUPS]; // array of GDMA group instances
  49. };
  50. struct gdma_group_t {
  51. int group_id; // Group ID, index from 0
  52. gdma_hal_context_t hal; // HAL instance is at group level
  53. portMUX_TYPE spinlock; // group level spinlock
  54. int ref_count; // reference count
  55. gdma_pair_t *pairs[SOC_GDMA_PAIRS_PER_GROUP]; // handles of GDMA pairs
  56. };
  57. struct gdma_pair_t {
  58. gdma_group_t *group; // which group the pair belongs to
  59. int pair_id; // Pair ID, index from 0
  60. gdma_tx_channel_t *tx_chan; // pointer of tx channel in the pair
  61. gdma_rx_channel_t *rx_chan; // pointer of rx channel in the pair
  62. int occupy_code; // each bit indicates which channel has been occupied (an occupied channel will be skipped during channel search)
  63. intr_handle_t intr; // Interrupt is at pair level
  64. portMUX_TYPE spinlock; // pair level spinlock
  65. int ref_count; // reference count
  66. };
  67. struct gdma_channel_t {
  68. gdma_pair_t *pair; // which pair the channel belongs to
  69. gdma_channel_direction_t direction; // channel direction
  70. int periph_id; // Peripheral instance ID, indicates which peripheral is connected to this GDMA channel
  71. esp_err_t (*del)(gdma_channel_t *channel); // channel deletion function, it's polymorphic, see `gdma_del_tx_channel` or `gdma_del_rx_channel`
  72. };
  73. struct gdma_tx_channel_t {
  74. gdma_channel_t base; // GDMA channel, base class
  75. void *user_data; // user registered DMA event data
  76. gdma_event_callback_t on_trans_eof; // TX EOF callback
  77. };
  78. struct gdma_rx_channel_t {
  79. gdma_channel_t base; // GDMA channel, base class
  80. void *user_data; // user registered DMA event data
  81. gdma_event_callback_t on_recv_eof; // RX EOF callback
  82. };
  83. static gdma_group_t *gdma_acquire_group_handle(int group_id);
  84. static void gdma_release_group_handle(gdma_group_t *group);
  85. static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id);
  86. static void gdma_release_pair_handle(gdma_pair_t *pair);
  87. static void gdma_uninstall_pair(gdma_pair_t *pair);
  88. static void gdma_uninstall_group(gdma_group_t *group);
  89. static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel);
  90. static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel);
  91. static esp_err_t gdma_install_interrupt(gdma_pair_t *pair);
  92. // gdma driver platform
  93. static gdma_platform_t s_platform = {
  94. .spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED,
  95. .groups = {} // groups will be lazy installed
  96. };
  97. esp_err_t gdma_new_channel(const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
  98. {
  99. esp_err_t ret_code = ESP_OK;
  100. gdma_tx_channel_t *alloc_tx_channel = NULL;
  101. gdma_rx_channel_t *alloc_rx_channel = NULL;
  102. int search_code = 0;
  103. gdma_pair_t *pair = NULL;
  104. gdma_group_t *group = NULL;
  105. DMA_CHECK(config && ret_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  106. if (config->flags.reserve_sibling) {
  107. search_code = SEARCH_REQUEST_RX_CHANNEL | SEARCH_REQUEST_TX_CHANNEL; // search for a pair of channels
  108. }
  109. if (config->direction == GDMA_CHANNEL_DIRECTION_TX) {
  110. search_code |= SEARCH_REQUEST_TX_CHANNEL; // search TX only
  111. alloc_tx_channel = calloc(1, sizeof(gdma_tx_channel_t));
  112. DMA_CHECK(alloc_tx_channel, "no mem for gdma tx channel", err, ESP_ERR_NO_MEM);
  113. } else if (config->direction == GDMA_CHANNEL_DIRECTION_RX) {
  114. search_code |= SEARCH_REQUEST_RX_CHANNEL; // search RX only
  115. alloc_rx_channel = calloc(1, sizeof(gdma_rx_channel_t));
  116. DMA_CHECK(alloc_rx_channel, "no mem for gdma rx channel", err, ESP_ERR_NO_MEM);
  117. }
  118. if (config->sibling_chan) {
  119. pair = config->sibling_chan->pair;
  120. DMA_CHECK(pair, "invalid sibling channel", err, ESP_ERR_INVALID_ARG);
  121. DMA_CHECK(config->sibling_chan->direction != config->direction,
  122. "sibling channel should have a different direction", err, ESP_ERR_INVALID_ARG);
  123. group = pair->group;
  124. portENTER_CRITICAL(&pair->spinlock);
  125. pair->ref_count++; // channel obtains a reference to pair
  126. portEXIT_CRITICAL(&pair->spinlock);
  127. goto search_done; // skip the search path below if user has specify a sibling channel
  128. }
  129. for (int i = 0; i < SOC_GDMA_GROUPS && search_code; i++) { // loop to search group
  130. group = gdma_acquire_group_handle(i);
  131. for (int j = 0; j < SOC_GDMA_PAIRS_PER_GROUP && search_code && group; j++) { // loop to search pair
  132. pair = gdma_acquire_pair_handle(group, j);
  133. if (pair) {
  134. portENTER_CRITICAL(&pair->spinlock);
  135. if (!(search_code & pair->occupy_code)) { // pair has suitable position for acquired channel(s)
  136. pair->occupy_code |= search_code;
  137. pair->ref_count++; // channel obtains a reference to pair
  138. search_code = 0; // exit search loop
  139. }
  140. portEXIT_CRITICAL(&pair->spinlock);
  141. }
  142. gdma_release_pair_handle(pair);
  143. } // loop used to search pair
  144. gdma_release_group_handle(group);
  145. } // loop used to search group
  146. DMA_CHECK(search_code == 0, "no free gdma channel, search code=%d", err, ESP_ERR_NOT_FOUND, search_code);
  147. search_done:
  148. // register TX channel
  149. if (alloc_tx_channel) {
  150. pair->tx_chan = alloc_tx_channel;
  151. alloc_tx_channel->base.pair = pair;
  152. alloc_tx_channel->base.direction = GDMA_CHANNEL_DIRECTION_TX;
  153. alloc_tx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
  154. alloc_tx_channel->base.del = gdma_del_tx_channel; // set channel deletion function
  155. *ret_chan = &alloc_tx_channel->base; // return the installed channel
  156. }
  157. // register RX channel
  158. if (alloc_rx_channel) {
  159. pair->rx_chan = alloc_rx_channel;
  160. alloc_rx_channel->base.pair = pair;
  161. alloc_rx_channel->base.direction = GDMA_CHANNEL_DIRECTION_RX;
  162. alloc_rx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
  163. alloc_rx_channel->base.del = gdma_del_rx_channel; // set channel deletion function
  164. *ret_chan = &alloc_rx_channel->base; // return the installed channel
  165. }
  166. ESP_LOGD(TAG, "new %s channel (%d,%d) at %p", (config->direction == GDMA_CHANNEL_DIRECTION_TX) ? "tx" : "rx",
  167. group->group_id, pair->pair_id, *ret_chan);
  168. return ESP_OK;
  169. err:
  170. if (alloc_tx_channel) {
  171. free(alloc_tx_channel);
  172. }
  173. if (alloc_rx_channel) {
  174. free(alloc_rx_channel);
  175. }
  176. return ret_code;
  177. }
  178. esp_err_t gdma_del_channel(gdma_channel_handle_t dma_chan)
  179. {
  180. esp_err_t ret_code = ESP_OK;
  181. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  182. ret_code = dma_chan->del(dma_chan); // call `gdma_del_tx_channel` or `gdma_del_rx_channel`
  183. err:
  184. return ret_code;
  185. }
  186. esp_err_t gdma_get_channel_id(gdma_channel_handle_t dma_chan, int *channel_id)
  187. {
  188. esp_err_t ret_code = ESP_OK;
  189. gdma_pair_t *pair = NULL;
  190. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  191. pair = dma_chan->pair;
  192. *channel_id = pair->pair_id;
  193. err:
  194. return ret_code;
  195. }
  196. esp_err_t gdma_connect(gdma_channel_handle_t dma_chan, gdma_trigger_t trig_periph)
  197. {
  198. esp_err_t ret_code = ESP_OK;
  199. gdma_pair_t *pair = NULL;
  200. gdma_group_t *group = NULL;
  201. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  202. DMA_CHECK(dma_chan->periph_id == GDMA_INVALID_PERIPH_TRIG, "channel is using by peripheral: %d", err, ESP_ERR_INVALID_STATE, dma_chan->periph_id);
  203. pair = dma_chan->pair;
  204. group = pair->group;
  205. dma_chan->periph_id = trig_periph.instance_id;
  206. // enable/disable m2m mode
  207. gdma_ll_enable_m2m_mode(group->hal.dev, pair->pair_id, trig_periph.periph == GDMA_TRIG_PERIPH_M2M);
  208. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
  209. gdma_ll_tx_reset_channel(group->hal.dev, pair->pair_id); // reset channel
  210. if (trig_periph.periph != GDMA_TRIG_PERIPH_M2M) {
  211. gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, trig_periph.instance_id);
  212. }
  213. } else {
  214. gdma_ll_rx_reset_channel(group->hal.dev, pair->pair_id); // reset channel
  215. if (trig_periph.periph != GDMA_TRIG_PERIPH_M2M) {
  216. gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, trig_periph.instance_id);
  217. }
  218. }
  219. err:
  220. return ret_code;
  221. }
  222. esp_err_t gdma_disconnect(gdma_channel_handle_t dma_chan)
  223. {
  224. esp_err_t ret_code = ESP_OK;
  225. gdma_pair_t *pair = NULL;
  226. gdma_group_t *group = NULL;
  227. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  228. DMA_CHECK(dma_chan->periph_id != GDMA_INVALID_PERIPH_TRIG, "no peripheral is connected to the channel", err, ESP_ERR_INVALID_STATE);
  229. pair = dma_chan->pair;
  230. group = pair->group;
  231. dma_chan->periph_id = GDMA_INVALID_PERIPH_TRIG;
  232. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
  233. gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, GDMA_INVALID_PERIPH_TRIG);
  234. } else {
  235. gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, GDMA_INVALID_PERIPH_TRIG);
  236. }
  237. err:
  238. return ret_code;
  239. }
  240. esp_err_t gdma_apply_strategy(gdma_channel_handle_t dma_chan, const gdma_strategy_config_t *config)
  241. {
  242. esp_err_t ret_code = ESP_OK;
  243. gdma_pair_t *pair = NULL;
  244. gdma_group_t *group = NULL;
  245. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  246. pair = dma_chan->pair;
  247. group = pair->group;
  248. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
  249. gdma_ll_tx_enable_owner_check(group->hal.dev, pair->pair_id, config->owner_check);
  250. gdma_ll_tx_enable_auto_write_back(group->hal.dev, pair->pair_id, config->auto_update_desc);
  251. } else {
  252. gdma_ll_rx_enable_owner_check(group->hal.dev, pair->pair_id, config->owner_check);
  253. }
  254. err:
  255. return ret_code;
  256. }
  257. esp_err_t gdma_register_tx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_tx_event_callbacks_t *cbs, void *user_data)
  258. {
  259. esp_err_t ret_code = ESP_OK;
  260. gdma_pair_t *pair = NULL;
  261. gdma_group_t *group = NULL;
  262. DMA_CHECK(dma_chan && dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX, "invalid argument", err, ESP_ERR_INVALID_ARG);
  263. pair = dma_chan->pair;
  264. group = pair->group;
  265. gdma_tx_channel_t *tx_chan = __containerof(dma_chan, gdma_tx_channel_t, base);
  266. // lazy install interrupt service
  267. DMA_CHECK(gdma_install_interrupt(pair) == ESP_OK, "install interrupt service failed", err, ESP_FAIL);
  268. // enable/disable GDMA interrupt events for TX channel
  269. portENTER_CRITICAL(&pair->spinlock);
  270. gdma_ll_enable_interrupt(group->hal.dev, pair->pair_id, GDMA_LL_EVENT_TX_EOF, cbs->on_trans_eof != NULL);
  271. portEXIT_CRITICAL(&pair->spinlock);
  272. tx_chan->on_trans_eof = cbs->on_trans_eof;
  273. tx_chan->user_data = user_data;
  274. err:
  275. return ret_code;
  276. }
  277. esp_err_t gdma_register_rx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_rx_event_callbacks_t *cbs, void *user_data)
  278. {
  279. esp_err_t ret_code = ESP_OK;
  280. gdma_pair_t *pair = NULL;
  281. gdma_group_t *group = NULL;
  282. DMA_CHECK(dma_chan && dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX, "invalid argument", err, ESP_ERR_INVALID_ARG);
  283. pair = dma_chan->pair;
  284. group = pair->group;
  285. gdma_rx_channel_t *rx_chan = __containerof(dma_chan, gdma_rx_channel_t, base);
  286. // lazy install interrupt service
  287. DMA_CHECK(gdma_install_interrupt(pair) == ESP_OK, "install interrupt service failed", err, ESP_FAIL);
  288. // enable/disable GDMA interrupt events for RX channel
  289. portENTER_CRITICAL(&pair->spinlock);
  290. gdma_ll_enable_interrupt(group->hal.dev, pair->pair_id, GDMA_LL_EVENT_RX_SUC_EOF, cbs->on_recv_eof != NULL);
  291. portEXIT_CRITICAL(&pair->spinlock);
  292. rx_chan->on_recv_eof = cbs->on_recv_eof;
  293. rx_chan->user_data = user_data;
  294. err:
  295. return ret_code;
  296. }
  297. esp_err_t gdma_start(gdma_channel_handle_t dma_chan, intptr_t desc_base_addr)
  298. {
  299. esp_err_t ret_code = ESP_OK;
  300. gdma_pair_t *pair = NULL;
  301. gdma_group_t *group = NULL;
  302. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  303. pair = dma_chan->pair;
  304. group = pair->group;
  305. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
  306. gdma_ll_rx_set_desc_addr(group->hal.dev, pair->pair_id, desc_base_addr);
  307. gdma_ll_rx_start(group->hal.dev, pair->pair_id);
  308. } else {
  309. gdma_ll_tx_set_desc_addr(group->hal.dev, pair->pair_id, desc_base_addr);
  310. gdma_ll_tx_start(group->hal.dev, pair->pair_id);
  311. }
  312. err:
  313. return ret_code;
  314. }
  315. esp_err_t gdma_stop(gdma_channel_handle_t dma_chan)
  316. {
  317. esp_err_t ret_code = ESP_OK;
  318. gdma_pair_t *pair = NULL;
  319. gdma_group_t *group = NULL;
  320. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  321. pair = dma_chan->pair;
  322. group = pair->group;
  323. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
  324. gdma_ll_rx_stop(group->hal.dev, pair->pair_id);
  325. } else {
  326. gdma_ll_tx_stop(group->hal.dev, pair->pair_id);
  327. }
  328. err:
  329. return ret_code;
  330. }
  331. esp_err_t gdma_append(gdma_channel_handle_t dma_chan)
  332. {
  333. esp_err_t ret_code = ESP_OK;
  334. gdma_pair_t *pair = NULL;
  335. gdma_group_t *group = NULL;
  336. DMA_CHECK(dma_chan, "invalid argument", err, ESP_ERR_INVALID_ARG);
  337. pair = dma_chan->pair;
  338. group = pair->group;
  339. if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
  340. gdma_ll_rx_restart(group->hal.dev, pair->pair_id);
  341. } else {
  342. gdma_ll_tx_restart(group->hal.dev, pair->pair_id);
  343. }
  344. err:
  345. return ret_code;
  346. }
  347. static inline bool gdma_is_group_busy(gdma_group_t *group)
  348. {
  349. return group->ref_count;
  350. }
  351. static void gdma_uninstall_group(gdma_group_t *group)
  352. {
  353. int group_id = group->group_id;
  354. bool do_deinitialize = false;
  355. if (s_platform.groups[group_id] && !gdma_is_group_busy(group)) {
  356. portENTER_CRITICAL(&s_platform.spinlock);
  357. if (s_platform.groups[group_id] && !gdma_is_group_busy(group)) {
  358. do_deinitialize = true;
  359. s_platform.groups[group_id] = NULL; // deregister from platfrom
  360. gdma_ll_enable_clock(group->hal.dev, false);
  361. periph_module_disable(gdma_periph_signals.groups[group_id].module);
  362. }
  363. portEXIT_CRITICAL(&s_platform.spinlock);
  364. }
  365. if (do_deinitialize) {
  366. free(group);
  367. ESP_LOGD(TAG, "del group %d", group_id);
  368. }
  369. }
  370. static gdma_group_t *gdma_acquire_group_handle(int group_id)
  371. {
  372. gdma_group_t *group = NULL;
  373. bool new_group = false;
  374. portENTER_CRITICAL(&s_platform.spinlock);
  375. if (!s_platform.groups[group_id]) {
  376. // lazy install group
  377. group = calloc(1, sizeof(gdma_group_t));
  378. if (group) {
  379. new_group = true;
  380. s_platform.groups[group_id] = group; // register to platform
  381. group->group_id = group_id;
  382. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  383. periph_module_enable(gdma_periph_signals.groups[group_id].module); // enable APB to access GDMA registers
  384. gdma_hal_init(&group->hal, group_id); // initialize HAL context
  385. gdma_ll_enable_clock(group->hal.dev, true); // enable gdma clock
  386. }
  387. } else {
  388. group = s_platform.groups[group_id];
  389. }
  390. if (group) {
  391. // someone acquired the group handle means we have a new object that refer to this group
  392. group->ref_count++;
  393. }
  394. portEXIT_CRITICAL(&s_platform.spinlock);
  395. if (new_group) {
  396. ESP_LOGD(TAG, "new group (%d) at %p", group->group_id, group);
  397. }
  398. return group;
  399. }
  400. static void gdma_release_group_handle(gdma_group_t *group)
  401. {
  402. if (group) {
  403. portENTER_CRITICAL(&group->spinlock);
  404. group->ref_count--;
  405. portEXIT_CRITICAL(&group->spinlock);
  406. gdma_uninstall_group(group);
  407. }
  408. }
  409. static inline bool gdma_is_pair_busy(gdma_pair_t *pair)
  410. {
  411. return pair->ref_count;
  412. }
  413. static void gdma_uninstall_pair(gdma_pair_t *pair)
  414. {
  415. gdma_group_t *group = pair->group;
  416. int pair_id = pair->pair_id;
  417. bool do_deinitialize = false;
  418. if (group->pairs[pair_id] && !gdma_is_pair_busy(pair)) {
  419. portENTER_CRITICAL(&group->spinlock);
  420. if (group->pairs[pair_id] && !gdma_is_pair_busy(pair)) {
  421. do_deinitialize = true;
  422. group->pairs[pair_id] = NULL; // deregister from pair
  423. group->ref_count--; // decrease reference count, because this pair won't refer to the group
  424. }
  425. portEXIT_CRITICAL(&group->spinlock);
  426. }
  427. if (do_deinitialize) {
  428. if (pair->intr) {
  429. esp_intr_free(pair->intr); // free interrupt resource
  430. ESP_LOGD(TAG, "uninstall interrupt service for pair (%d,%d)", group->group_id, pair_id);
  431. }
  432. free(pair);
  433. ESP_LOGD(TAG, "del pair (%d,%d)", group->group_id, pair_id);
  434. gdma_uninstall_group(group);
  435. }
  436. }
  437. static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id)
  438. {
  439. gdma_pair_t *pair = NULL;
  440. bool new_pair = false;
  441. portENTER_CRITICAL(&group->spinlock);
  442. if (!group->pairs[pair_id]) {
  443. // lazy install pair
  444. pair = calloc(1, sizeof(gdma_pair_t));
  445. if (pair) {
  446. new_pair = true;
  447. group->pairs[pair_id] = pair; // register to group
  448. group->ref_count++; // pair obtains a reference to group
  449. pair->group = group;
  450. pair->pair_id = pair_id;
  451. pair->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  452. }
  453. } else {
  454. pair = group->pairs[pair_id];
  455. }
  456. if (pair) {
  457. // someone acquired the pair handle means we have a new object that refer to this pair
  458. pair->ref_count++;
  459. }
  460. portEXIT_CRITICAL(&group->spinlock);
  461. if (new_pair) {
  462. ESP_LOGD(TAG, "new pair (%d,%d) at %p", group->group_id, pair->pair_id, pair);
  463. }
  464. return pair;
  465. }
  466. static void gdma_release_pair_handle(gdma_pair_t *pair)
  467. {
  468. if (pair) {
  469. portENTER_CRITICAL(&pair->spinlock);
  470. pair->ref_count--;
  471. portEXIT_CRITICAL(&pair->spinlock);
  472. gdma_uninstall_pair(pair);
  473. }
  474. }
  475. static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel)
  476. {
  477. gdma_pair_t *pair = dma_channel->pair;
  478. gdma_tx_channel_t *tx_chan = __containerof(dma_channel, gdma_tx_channel_t, base);
  479. portENTER_CRITICAL(&pair->spinlock);
  480. pair->tx_chan = NULL;
  481. pair->ref_count--; // decrease reference count, because this channel won't refer to the pair
  482. pair->occupy_code &= ~SEARCH_REQUEST_TX_CHANNEL;
  483. portEXIT_CRITICAL(&pair->spinlock);
  484. ESP_LOGD(TAG, "del tx channel (%d,%d)", pair->group->group_id, pair->pair_id);
  485. free(tx_chan);
  486. gdma_uninstall_pair(pair);
  487. return ESP_OK;
  488. }
  489. static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel)
  490. {
  491. gdma_pair_t *pair = dma_channel->pair;
  492. gdma_rx_channel_t *rx_chan = __containerof(dma_channel, gdma_rx_channel_t, base);
  493. portENTER_CRITICAL(&pair->spinlock);
  494. pair->rx_chan = NULL;
  495. pair->ref_count--; // decrease reference count, because this channel won't refer to the pair
  496. pair->occupy_code &= ~SEARCH_REQUEST_RX_CHANNEL;
  497. portEXIT_CRITICAL(&pair->spinlock);
  498. ESP_LOGD(TAG, "del rx channel (%d,%d)", pair->group->group_id, pair->pair_id);
  499. free(rx_chan);
  500. gdma_uninstall_pair(pair);
  501. return ESP_OK;
  502. }
  503. static void IRAM_ATTR gdma_default_isr(void *args)
  504. {
  505. gdma_pair_t *pair = (gdma_pair_t *)args;
  506. gdma_group_t *group = pair->group;
  507. gdma_rx_channel_t *rx_chan = pair->rx_chan;
  508. gdma_tx_channel_t *tx_chan = pair->tx_chan;
  509. bool need_yield = false;
  510. // clear pending interrupt event
  511. uint32_t intr_status = gdma_ll_get_interrupt_status(group->hal.dev, pair->pair_id);
  512. gdma_ll_clear_interrupt_status(group->hal.dev, pair->pair_id, intr_status);
  513. if (intr_status & GDMA_LL_EVENT_RX_SUC_EOF) {
  514. if (rx_chan && rx_chan->on_recv_eof) {
  515. uint32_t eof_addr = gdma_ll_rx_get_success_eof_desc_addr(group->hal.dev, pair->pair_id);
  516. gdma_event_data_t edata = {
  517. .rx_eof_desc_addr = eof_addr
  518. };
  519. if (rx_chan->on_recv_eof(&rx_chan->base, &edata, rx_chan->user_data)) {
  520. need_yield = true;
  521. }
  522. }
  523. }
  524. if (intr_status & GDMA_LL_EVENT_TX_EOF) {
  525. if (tx_chan && tx_chan->on_trans_eof) {
  526. uint32_t eof_addr = gdma_ll_tx_get_eof_desc_addr(group->hal.dev, pair->pair_id);
  527. gdma_event_data_t edata = {
  528. .tx_eof_desc_addr = eof_addr
  529. };
  530. if (tx_chan->on_trans_eof(&tx_chan->base, &edata, tx_chan->user_data)) {
  531. need_yield = true;
  532. }
  533. }
  534. }
  535. if (need_yield) {
  536. portYIELD_FROM_ISR();
  537. }
  538. }
  539. static esp_err_t gdma_install_interrupt(gdma_pair_t *pair)
  540. {
  541. esp_err_t ret_code = ESP_OK;
  542. gdma_group_t *group = pair->group;
  543. int isr_flags = 0;
  544. bool do_install_isr = false;
  545. if (!pair->intr) {
  546. portENTER_CRITICAL(&pair->spinlock);
  547. if (!pair->intr) {
  548. do_install_isr = true;
  549. ret_code = esp_intr_alloc(gdma_periph_signals.groups[group->group_id].pairs[pair->pair_id].irq_id, isr_flags, gdma_default_isr, pair, &pair->intr);
  550. gdma_ll_enable_interrupt(group->hal.dev, pair->pair_id, UINT32_MAX, false); // disable all interupt events
  551. gdma_ll_clear_interrupt_status(group->hal.dev, pair->pair_id, UINT32_MAX); // clear all pending events
  552. }
  553. portEXIT_CRITICAL(&pair->spinlock);
  554. }
  555. if (do_install_isr) {
  556. DMA_CHECK(ret_code == ESP_OK, "alloc interrupt failed", err, ret_code);
  557. ESP_LOGD(TAG, "install interrupt service for pair (%d,%d)", group->group_id, pair->pair_id);
  558. }
  559. err:
  560. return ret_code;
  561. }