gdma.c 25 KB

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