drv_pdma.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-11-11 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_PDMA)
  14. #include <rtdevice.h>
  15. #include <rtthread.h>
  16. #include <drv_pdma.h>
  17. #include <nu_bitutil.h>
  18. #include "drv_sys.h"
  19. /* Private define ---------------------------------------------------------------*/
  20. // RT_DEV_NAME_PREFIX pdma
  21. #ifndef NU_PDMA_MEMFUN_ACTOR_MAX
  22. #define NU_PDMA_MEMFUN_ACTOR_MAX (4)
  23. #endif
  24. #define NU_PDMA_SG_TBL_MAXSIZE (NU_PDMA_SG_LIMITED_DISTANCE/sizeof(DSCT_T))
  25. #define NU_PDMA_CH_MAX (2*PDMA_CH_MAX) /* Specify maximum channels of PDMA */
  26. #define NU_PDMA_CH_Pos (0) /* Specify first channel number of PDMA */
  27. #define NU_PDMA_CH_Msk (((1 << NU_PDMA_CH_MAX) - 1) << NU_PDMA_CH_Pos)
  28. #define NU_PDMA_CH_HALF_Msk (((1 << PDMA_CH_MAX) - 1) << NU_PDMA_CH_Pos)
  29. #define NU_PDMA_GET_BASE(ch) (PDMA_T *)((((ch)/PDMA_CH_MAX)>0)?PDMA1_BA:PDMA0_BA)
  30. #define NU_PDMA_GET_MOD_CHIDX(ch) ((ch)%PDMA_CH_MAX)
  31. /* Private typedef --------------------------------------------------------------*/
  32. struct nu_pdma_periph_ctl
  33. {
  34. uint32_t m_u32Peripheral;
  35. nu_pdma_memctrl_t m_eMemCtl;
  36. };
  37. typedef struct nu_pdma_periph_ctl nu_pdma_periph_ctl_t;
  38. struct nu_pdma_chn
  39. {
  40. struct nu_pdma_chn_cb m_sCB_Event;
  41. struct nu_pdma_chn_cb m_sCB_Trigger;
  42. struct nu_pdma_chn_cb m_sCB_Disable;
  43. nu_pdma_desc_t *m_ppsSgtbl;
  44. uint32_t m_u32WantedSGTblNum;
  45. uint32_t m_u32EventFilter;
  46. uint32_t m_u32IdleTimeout_us;
  47. nu_pdma_periph_ctl_t m_spPeripCtl;
  48. };
  49. typedef struct nu_pdma_chn nu_pdma_chn_t;
  50. struct nu_pdma_memfun_actor
  51. {
  52. int m_i32ChannID;
  53. uint32_t m_u32Result;
  54. rt_sem_t m_psSemMemFun;
  55. } ;
  56. typedef struct nu_pdma_memfun_actor *nu_pdma_memfun_actor_t;
  57. /* Private functions ------------------------------------------------------------*/
  58. static int nu_pdma_peripheral_set(uint32_t u32PeriphType);
  59. static void nu_pdma_init(void);
  60. static void nu_pdma_channel_enable(int i32ChannID);
  61. static void nu_pdma_channel_disable(int i32ChannID);
  62. static void nu_pdma_channel_reset(int i32ChannID);
  63. static rt_err_t nu_pdma_timeout_set(int i32ChannID, int i32Timeout_us);
  64. static void nu_pdma_periph_ctrl_fill(int i32ChannID, int i32CtlPoolIdx);
  65. static rt_size_t nu_pdma_memfun(void *dest, void *src, uint32_t u32DataWidth, unsigned int u32TransferCnt, nu_pdma_memctrl_t eMemCtl);
  66. static void nu_pdma_memfun_cb(void *pvUserData, uint32_t u32Events);
  67. static void nu_pdma_memfun_actor_init(void);
  68. static int nu_pdma_memfun_employ(void);
  69. static int nu_pdma_non_transfer_count_get(int32_t i32ChannID);
  70. /* Public functions -------------------------------------------------------------*/
  71. /* Private variables ------------------------------------------------------------*/
  72. static volatile int nu_pdma_inited = 0;
  73. static volatile uint32_t nu_pdma_chn_mask = 0;
  74. static nu_pdma_chn_t nu_pdma_chn_arr[NU_PDMA_CH_MAX];
  75. static volatile uint32_t nu_pdma_memfun_actor_mask = 0;
  76. static volatile uint32_t nu_pdma_memfun_actor_maxnum = 0;
  77. static rt_sem_t nu_pdma_memfun_actor_pool_sem = RT_NULL;
  78. static rt_mutex_t nu_pdma_memfun_actor_pool_lock = RT_NULL;
  79. static void nu_pdma_isr(int vector, void *pvdata);
  80. static const nu_pdma_periph_ctl_t g_nu_pdma_peripheral_ctl_pool[ ] =
  81. {
  82. // M2M
  83. { PDMA_MEM, eMemCtl_SrcInc_DstInc },
  84. // M2P
  85. { PDMA_UART0_TX, eMemCtl_SrcInc_DstFix },
  86. { PDMA_UART1_TX, eMemCtl_SrcInc_DstFix },
  87. { PDMA_UART2_TX, eMemCtl_SrcInc_DstFix },
  88. { PDMA_UART3_TX, eMemCtl_SrcInc_DstFix },
  89. { PDMA_UART4_TX, eMemCtl_SrcInc_DstFix },
  90. { PDMA_UART5_TX, eMemCtl_SrcInc_DstFix },
  91. { PDMA_UART6_TX, eMemCtl_SrcInc_DstFix },
  92. { PDMA_UART7_TX, eMemCtl_SrcInc_DstFix },
  93. { PDMA_UART8_TX, eMemCtl_SrcInc_DstFix },
  94. { PDMA_UART9_TX, eMemCtl_SrcInc_DstFix },
  95. { PDMA_QSPI0_TX, eMemCtl_SrcInc_DstFix },
  96. { PDMA_SPI0_TX, eMemCtl_SrcInc_DstFix },
  97. { PDMA_SPI1_TX, eMemCtl_SrcInc_DstFix },
  98. { PDMA_I2C0_TX, eMemCtl_SrcInc_DstFix },
  99. { PDMA_I2C1_TX, eMemCtl_SrcInc_DstFix },
  100. { PDMA_I2C2_TX, eMemCtl_SrcInc_DstFix },
  101. { PDMA_I2C3_TX, eMemCtl_SrcInc_DstFix },
  102. // P2M
  103. { PDMA_UART0_RX, eMemCtl_SrcFix_DstInc },
  104. { PDMA_UART1_RX, eMemCtl_SrcFix_DstInc },
  105. { PDMA_UART2_RX, eMemCtl_SrcFix_DstInc },
  106. { PDMA_UART3_RX, eMemCtl_SrcFix_DstInc },
  107. { PDMA_UART4_RX, eMemCtl_SrcFix_DstInc },
  108. { PDMA_UART5_RX, eMemCtl_SrcFix_DstInc },
  109. { PDMA_UART6_RX, eMemCtl_SrcFix_DstInc },
  110. { PDMA_UART7_RX, eMemCtl_SrcFix_DstInc },
  111. { PDMA_UART8_RX, eMemCtl_SrcFix_DstInc },
  112. { PDMA_UART9_RX, eMemCtl_SrcFix_DstInc },
  113. { PDMA_QSPI0_RX, eMemCtl_SrcFix_DstInc },
  114. { PDMA_SPI0_RX, eMemCtl_SrcFix_DstInc },
  115. { PDMA_SPI1_RX, eMemCtl_SrcFix_DstInc },
  116. { PDMA_I2C0_RX, eMemCtl_SrcFix_DstInc },
  117. { PDMA_I2C1_RX, eMemCtl_SrcFix_DstInc },
  118. { PDMA_I2C2_RX, eMemCtl_SrcFix_DstInc },
  119. { PDMA_I2C3_RX, eMemCtl_SrcFix_DstInc },
  120. };
  121. #define NU_PERIPHERAL_SIZE ( sizeof(g_nu_pdma_peripheral_ctl_pool) / sizeof(g_nu_pdma_peripheral_ctl_pool[0]) )
  122. static struct nu_pdma_memfun_actor nu_pdma_memfun_actor_arr[NU_PDMA_MEMFUN_ACTOR_MAX];
  123. static int nu_pdma_peripheral_set(uint32_t u32PeriphType)
  124. {
  125. int idx = 0;
  126. while (idx < NU_PERIPHERAL_SIZE)
  127. {
  128. if (g_nu_pdma_peripheral_ctl_pool[idx].m_u32Peripheral == u32PeriphType)
  129. return idx;
  130. idx++;
  131. }
  132. // Not such peripheral
  133. return -1;
  134. }
  135. static void nu_pdma_periph_ctrl_fill(int i32ChannID, int i32CtlPoolIdx)
  136. {
  137. nu_pdma_chn_t *psPdmaChann = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos];
  138. psPdmaChann->m_spPeripCtl.m_u32Peripheral = g_nu_pdma_peripheral_ctl_pool[i32CtlPoolIdx].m_u32Peripheral;
  139. psPdmaChann->m_spPeripCtl.m_eMemCtl = g_nu_pdma_peripheral_ctl_pool[i32CtlPoolIdx].m_eMemCtl;
  140. }
  141. static void nu_pdma_init(void)
  142. {
  143. if (nu_pdma_inited)
  144. return;
  145. nu_pdma_chn_mask = ~(NU_PDMA_CH_Msk);
  146. rt_memset(nu_pdma_chn_arr, 0x00, NU_PDMA_CH_MAX * sizeof(nu_pdma_chn_t));
  147. nu_sys_ipclk_enable(PDMA0CKEN);
  148. nu_sys_ipclk_enable(PDMA1CKEN);
  149. nu_sys_ip_reset(PDMA0RST);
  150. nu_sys_ip_reset(PDMA1RST);
  151. /* Initialize PDMA0 setting */
  152. PDMA_Open(PDMA0, NU_PDMA_CH_HALF_Msk);
  153. PDMA_Close(PDMA0);
  154. /* Register PDMA0 ISR */
  155. rt_hw_interrupt_install(IRQ_PDMA0, nu_pdma_isr, (void *)PDMA0, "pdma0");
  156. rt_hw_interrupt_umask(IRQ_PDMA0);
  157. /* Initialize PDMA1 setting */
  158. PDMA_Open(PDMA1, NU_PDMA_CH_HALF_Msk);
  159. PDMA_Close(PDMA1);
  160. /* Register PDMA1 ISR */
  161. rt_hw_interrupt_install(IRQ_PDMA1, nu_pdma_isr, (void *)PDMA1, "pdma1");
  162. rt_hw_interrupt_umask(IRQ_PDMA1);
  163. /* Assign first SG table address to SRAM's start address */
  164. PDMA0->SCATBA = PDMA1->SCATBA = BOARD_SDRAM_START;
  165. nu_pdma_inited = 1;
  166. }
  167. static void nu_pdma_channel_enable(int i32ChannID)
  168. {
  169. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  170. PDMA_Open(PDMA, 1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  171. }
  172. static inline void nu_pdma_channel_disable(int i32ChannID)
  173. {
  174. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  175. PDMA->CHCTL &= ~(1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  176. }
  177. static inline void nu_pdma_channel_reset(int i32ChannID)
  178. {
  179. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  180. PDMA->CHRST = (1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  181. }
  182. void nu_pdma_channel_terminate(int i32ChannID)
  183. {
  184. PDMA_T *PDMA;
  185. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  186. goto exit_pdma_channel_terminate;
  187. PDMA = NU_PDMA_GET_BASE(i32ChannID);
  188. // Reset specified channel ID
  189. nu_pdma_channel_reset(i32ChannID);
  190. // Clean descriptor table control register.
  191. PDMA->DSCT[NU_PDMA_GET_MOD_CHIDX(i32ChannID)].CTL = 0UL;
  192. PDMA->CHCTL |= (1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  193. exit_pdma_channel_terminate:
  194. return;
  195. }
  196. static rt_err_t nu_pdma_timeout_set(int i32ChannID, int i32Timeout_us)
  197. {
  198. rt_err_t ret = RT_EINVAL;
  199. PDMA_T *PDMA = NULL;
  200. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  201. goto exit_nu_pdma_timeout_set;
  202. PDMA = NU_PDMA_GET_BASE(i32ChannID);
  203. nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_u32IdleTimeout_us = i32Timeout_us;
  204. if (i32Timeout_us && (NU_PDMA_GET_MOD_CHIDX(i32ChannID) < PDMA_CH_MAX)) // Limit
  205. {
  206. uint32_t u32HCLK = sysGetClock(SYS_HCLK) * 1000000;
  207. uint32_t u32ToClk_Max = u32HCLK / (1 << 8);
  208. uint32_t u32Divider = ((i32Timeout_us * u32ToClk_Max) / 1000000) / (1 << 16);
  209. uint32_t u32TOutCnt = ((i32Timeout_us * u32ToClk_Max) / 1000000) % (1 << 16);
  210. PDMA_DisableTimeout(PDMA, 1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  211. PDMA_EnableInt(PDMA, NU_PDMA_GET_MOD_CHIDX(i32ChannID), PDMA_INT_TIMEOUT); // Interrupt type
  212. if (u32Divider > 7)
  213. {
  214. u32Divider = 7;
  215. u32TOutCnt = (1 << 16);
  216. }
  217. PDMA->TOUTPSC |= (u32Divider << (PDMA_TOUTPSC_TOUTPSC1_Pos * NU_PDMA_GET_MOD_CHIDX(i32ChannID)));
  218. PDMA_SetTimeOut(PDMA, NU_PDMA_GET_MOD_CHIDX(i32ChannID), 1, u32TOutCnt);
  219. ret = RT_EOK;
  220. }
  221. else
  222. {
  223. PDMA_DisableInt(PDMA, NU_PDMA_GET_MOD_CHIDX(i32ChannID), PDMA_INT_TIMEOUT); // Interrupt type
  224. PDMA_DisableTimeout(PDMA, 1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  225. }
  226. exit_nu_pdma_timeout_set:
  227. return -(ret);
  228. }
  229. int nu_pdma_channel_allocate(int32_t i32PeripType)
  230. {
  231. int i, i32PeripCtlIdx;
  232. nu_pdma_init();
  233. if ((i32PeripCtlIdx = nu_pdma_peripheral_set(i32PeripType)) < 0)
  234. goto exit_nu_pdma_channel_allocate;
  235. /* Find the position of first '0' in nu_pdma_chn_mask. */
  236. i = nu_cto(nu_pdma_chn_mask);
  237. if (i != 32)
  238. {
  239. nu_pdma_chn_mask |= (1 << i);
  240. rt_memset(nu_pdma_chn_arr + i - NU_PDMA_CH_Pos, 0x00, sizeof(nu_pdma_chn_t));
  241. /* Set idx number of g_nu_pdma_peripheral_ctl_pool */
  242. nu_pdma_periph_ctrl_fill(i, i32PeripCtlIdx);
  243. /* Reset channel */
  244. nu_pdma_channel_reset(i);
  245. nu_pdma_channel_enable(i);
  246. return i;
  247. }
  248. exit_nu_pdma_channel_allocate:
  249. // No channel available
  250. return -(RT_ERROR);
  251. }
  252. rt_err_t nu_pdma_channel_free(int i32ChannID)
  253. {
  254. rt_err_t ret = RT_EINVAL;
  255. if (! nu_pdma_inited)
  256. goto exit_nu_pdma_channel_free;
  257. if (i32ChannID < NU_PDMA_CH_MAX && i32ChannID >= NU_PDMA_CH_Pos)
  258. {
  259. nu_pdma_chn_mask &= ~(1 << i32ChannID);
  260. nu_pdma_channel_disable(i32ChannID);
  261. ret = RT_EOK;
  262. }
  263. exit_nu_pdma_channel_free:
  264. return -(ret);
  265. }
  266. rt_err_t nu_pdma_filtering_set(int i32ChannID, uint32_t u32EventFilter)
  267. {
  268. rt_err_t ret = RT_EINVAL;
  269. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  270. goto exit_nu_pdma_filtering_set;
  271. nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_u32EventFilter = u32EventFilter;
  272. ret = RT_EOK;
  273. exit_nu_pdma_filtering_set:
  274. return -(ret) ;
  275. }
  276. uint32_t nu_pdma_filtering_get(int i32ChannID)
  277. {
  278. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  279. goto exit_nu_pdma_filtering_get;
  280. return nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_u32EventFilter;
  281. exit_nu_pdma_filtering_get:
  282. return 0;
  283. }
  284. rt_err_t nu_pdma_callback_register(int i32ChannID, nu_pdma_chn_cb_t psChnCb)
  285. {
  286. rt_err_t ret = RT_EINVAL;
  287. nu_pdma_chn_cb_t psChnCb_Current = RT_NULL;
  288. RT_ASSERT(psChnCb != RT_NULL);
  289. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  290. goto exit_nu_pdma_callback_register;
  291. switch (psChnCb->m_eCBType)
  292. {
  293. case eCBType_Event:
  294. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Event;
  295. break;
  296. case eCBType_Trigger:
  297. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Trigger;
  298. break;
  299. case eCBType_Disable:
  300. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Disable;
  301. break;
  302. default:
  303. goto exit_nu_pdma_callback_register;
  304. }
  305. psChnCb_Current->m_pfnCBHandler = psChnCb->m_pfnCBHandler;
  306. psChnCb_Current->m_pvUserData = psChnCb->m_pvUserData;
  307. ret = RT_EOK;
  308. exit_nu_pdma_callback_register:
  309. return -(ret) ;
  310. }
  311. nu_pdma_cb_handler_t nu_pdma_callback_hijack(int i32ChannID, nu_pdma_cbtype_t eCBType, nu_pdma_chn_cb_t psChnCb_Hijack)
  312. {
  313. nu_pdma_chn_cb_t psChnCb_Current = RT_NULL;
  314. struct nu_pdma_chn_cb sChnCB_Tmp;
  315. RT_ASSERT(psChnCb_Hijack != NULL);
  316. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  317. goto exit_nu_pdma_callback_hijack;
  318. switch (eCBType)
  319. {
  320. case eCBType_Event:
  321. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Event;
  322. break;
  323. case eCBType_Trigger:
  324. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Trigger;
  325. break;
  326. case eCBType_Disable:
  327. psChnCb_Current = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_sCB_Disable;
  328. break;
  329. default:
  330. goto exit_nu_pdma_callback_hijack;
  331. }
  332. /* Backup */
  333. sChnCB_Tmp.m_pfnCBHandler = psChnCb_Current->m_pfnCBHandler;
  334. sChnCB_Tmp.m_pvUserData = psChnCb_Current->m_pvUserData;
  335. /* Update */
  336. psChnCb_Current->m_pfnCBHandler = psChnCb_Hijack->m_pfnCBHandler;
  337. psChnCb_Current->m_pvUserData = psChnCb_Hijack->m_pvUserData;
  338. /* Restore */
  339. psChnCb_Hijack->m_pfnCBHandler = sChnCB_Tmp.m_pfnCBHandler;
  340. psChnCb_Hijack->m_pvUserData = sChnCB_Tmp.m_pvUserData;
  341. exit_nu_pdma_callback_hijack:
  342. return sChnCB_Tmp.m_pfnCBHandler;
  343. }
  344. static int nu_pdma_non_transfer_count_get(int32_t i32ChannID)
  345. {
  346. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  347. return ((PDMA->DSCT[NU_PDMA_GET_MOD_CHIDX(i32ChannID)].CTL & PDMA_DSCT_CTL_TXCNT_Msk) >> PDMA_DSCT_CTL_TXCNT_Pos) + 1;
  348. }
  349. int nu_pdma_transferred_byte_get(int32_t i32ChannID, int32_t i32TriggerByteLen)
  350. {
  351. int i32BitWidth = 0;
  352. int cur_txcnt = 0;
  353. PDMA_T *PDMA;
  354. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  355. goto exit_nu_pdma_transferred_byte_get;
  356. PDMA = NU_PDMA_GET_BASE(i32ChannID);
  357. i32BitWidth = PDMA->DSCT[NU_PDMA_GET_MOD_CHIDX(i32ChannID)].CTL & PDMA_DSCT_CTL_TXWIDTH_Msk;
  358. i32BitWidth = (i32BitWidth == PDMA_WIDTH_8) ? 1 : (i32BitWidth == PDMA_WIDTH_16) ? 2 : (i32BitWidth == PDMA_WIDTH_32) ? 4 : 0;
  359. cur_txcnt = nu_pdma_non_transfer_count_get(i32ChannID);
  360. return (i32TriggerByteLen - (cur_txcnt) * i32BitWidth);
  361. exit_nu_pdma_transferred_byte_get:
  362. return -1;
  363. }
  364. nu_pdma_memctrl_t nu_pdma_channel_memctrl_get(int i32ChannID)
  365. {
  366. nu_pdma_memctrl_t eMemCtrl = eMemCtl_Undefined;
  367. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  368. goto exit_nu_pdma_channel_memctrl_get;
  369. eMemCtrl = nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_spPeripCtl.m_eMemCtl;
  370. exit_nu_pdma_channel_memctrl_get:
  371. return eMemCtrl;
  372. }
  373. rt_err_t nu_pdma_channel_memctrl_set(int i32ChannID, nu_pdma_memctrl_t eMemCtrl)
  374. {
  375. rt_err_t ret = RT_EINVAL;
  376. nu_pdma_chn_t *psPdmaChann = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos];
  377. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  378. goto exit_nu_pdma_channel_memctrl_set;
  379. else if ((eMemCtrl < eMemCtl_SrcFix_DstFix) || (eMemCtrl > eMemCtl_SrcInc_DstInc))
  380. goto exit_nu_pdma_channel_memctrl_set;
  381. /* PDMA_MEM/SAR_FIX/BURST mode is not supported. */
  382. if ((psPdmaChann->m_spPeripCtl.m_u32Peripheral == PDMA_MEM) &&
  383. ((eMemCtrl == eMemCtl_SrcFix_DstInc) || (eMemCtrl == eMemCtl_SrcFix_DstFix)))
  384. goto exit_nu_pdma_channel_memctrl_set;
  385. nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_spPeripCtl.m_eMemCtl = eMemCtrl;
  386. ret = RT_EOK;
  387. exit_nu_pdma_channel_memctrl_set:
  388. return -(ret);
  389. }
  390. static void nu_pdma_channel_memctrl_fill(nu_pdma_memctrl_t eMemCtl, uint32_t *pu32SrcCtl, uint32_t *pu32DstCtl)
  391. {
  392. switch ((int)eMemCtl)
  393. {
  394. case eMemCtl_SrcFix_DstFix:
  395. *pu32SrcCtl = PDMA_SAR_FIX;
  396. *pu32DstCtl = PDMA_DAR_FIX;
  397. break;
  398. case eMemCtl_SrcFix_DstInc:
  399. *pu32SrcCtl = PDMA_SAR_FIX;
  400. *pu32DstCtl = PDMA_DAR_INC;
  401. break;
  402. case eMemCtl_SrcInc_DstFix:
  403. *pu32SrcCtl = PDMA_SAR_INC;
  404. *pu32DstCtl = PDMA_DAR_FIX;
  405. break;
  406. case eMemCtl_SrcInc_DstInc:
  407. *pu32SrcCtl = PDMA_SAR_INC;
  408. *pu32DstCtl = PDMA_DAR_INC;
  409. break;
  410. default:
  411. break;
  412. }
  413. }
  414. /* This is for Scatter-gather DMA. */
  415. rt_err_t nu_pdma_desc_setup(int i32ChannID, nu_pdma_desc_t dma_desc, uint32_t u32DataWidth, uint32_t u32AddrSrc,
  416. uint32_t u32AddrDst, int32_t i32TransferCnt, nu_pdma_desc_t next, uint32_t u32BeSilent)
  417. {
  418. nu_pdma_periph_ctl_t *psPeriphCtl = NULL;
  419. PDMA_T *PDMA = NULL;
  420. uint32_t u32SrcCtl = 0;
  421. uint32_t u32DstCtl = 0;
  422. rt_err_t ret = RT_EINVAL;
  423. if (!dma_desc)
  424. goto exit_nu_pdma_desc_setup;
  425. else if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  426. goto exit_nu_pdma_desc_setup;
  427. else if (!(u32DataWidth == 8 || u32DataWidth == 16 || u32DataWidth == 32))
  428. goto exit_nu_pdma_desc_setup;
  429. else if ((u32AddrSrc % (u32DataWidth / 8)) || (u32AddrDst % (u32DataWidth / 8)))
  430. goto exit_nu_pdma_desc_setup;
  431. else if (i32TransferCnt > NU_PDMA_MAX_TXCNT)
  432. goto exit_nu_pdma_desc_setup;
  433. PDMA = NU_PDMA_GET_BASE(i32ChannID);
  434. psPeriphCtl = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_spPeripCtl;
  435. nu_pdma_channel_memctrl_fill(psPeriphCtl->m_eMemCtl, &u32SrcCtl, &u32DstCtl);
  436. dma_desc->CTL = ((i32TransferCnt - 1) << PDMA_DSCT_CTL_TXCNT_Pos) |
  437. ((u32DataWidth == 8) ? PDMA_WIDTH_8 : (u32DataWidth == 16) ? PDMA_WIDTH_16 : PDMA_WIDTH_32) |
  438. u32SrcCtl |
  439. u32DstCtl |
  440. PDMA_OP_BASIC;
  441. dma_desc->SA = u32AddrSrc;
  442. dma_desc->DA = u32AddrDst;
  443. dma_desc->NEXT = 0; /* Terminating node by default. */
  444. if (psPeriphCtl->m_u32Peripheral == PDMA_MEM)
  445. {
  446. /* For M2M transfer */
  447. dma_desc->CTL |= (PDMA_REQ_BURST | PDMA_BURST_32);
  448. }
  449. else
  450. {
  451. /* For P2M and M2P transfer */
  452. dma_desc->CTL |= (PDMA_REQ_SINGLE);
  453. }
  454. if (next)
  455. {
  456. /* Link to Next and modify to scatter-gather DMA mode. */
  457. dma_desc->CTL = (dma_desc->CTL & ~PDMA_DSCT_CTL_OPMODE_Msk) | PDMA_OP_SCATTER;
  458. dma_desc->NEXT = (uint32_t)next - (PDMA->SCATBA);
  459. }
  460. /* Be silent */
  461. if (u32BeSilent)
  462. dma_desc->CTL |= PDMA_DSCT_CTL_TBINTDIS_Msk;
  463. ret = RT_EOK;
  464. exit_nu_pdma_desc_setup:
  465. return -(ret);
  466. }
  467. rt_err_t nu_pdma_sgtbls_allocate(nu_pdma_desc_t *ppsSgtbls, int num)
  468. {
  469. int i;
  470. nu_pdma_desc_t psSgTblHead;
  471. RT_ASSERT(ppsSgtbls != NULL);
  472. RT_ASSERT(num > 0);
  473. psSgTblHead = (nu_pdma_desc_t) rt_malloc_align(sizeof(DSCT_T) * num, 32);
  474. RT_ASSERT(psSgTblHead != RT_NULL);
  475. rt_memset((void *)psSgTblHead, 0, sizeof(DSCT_T) * num);
  476. for (i = 0; i < num; i++)
  477. ppsSgtbls[i] = &psSgTblHead[i];
  478. return RT_EOK;
  479. }
  480. void nu_pdma_sgtbls_free(nu_pdma_desc_t *ppsSgtbls, int num)
  481. {
  482. nu_pdma_desc_t psSgTblHead;
  483. RT_ASSERT(ppsSgtbls != NULL);
  484. psSgTblHead = *ppsSgtbls;
  485. RT_ASSERT(psSgTblHead != NULL);
  486. rt_free_align(psSgTblHead);
  487. }
  488. static void _nu_pdma_transfer(int i32ChannID, uint32_t u32Peripheral, nu_pdma_desc_t head, uint32_t u32IdleTimeout_us)
  489. {
  490. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  491. nu_pdma_chn_t *psPdmaChann = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos];
  492. PDMA_DisableTimeout(PDMA, 1 << NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  493. PDMA_EnableInt(PDMA, NU_PDMA_GET_MOD_CHIDX(i32ChannID), PDMA_INT_TRANS_DONE);
  494. nu_pdma_timeout_set(i32ChannID, u32IdleTimeout_us);
  495. /* Set scatter-gather mode and head */
  496. PDMA_SetTransferMode(PDMA,
  497. NU_PDMA_GET_MOD_CHIDX(i32ChannID),
  498. u32Peripheral,
  499. (head->NEXT != 0) ? 1 : 0,
  500. (uint32_t)head);
  501. #if defined(BSP_USING_MMU)
  502. /* Writeback data in dcache to memory before transferring. */
  503. {
  504. static uint32_t bNonCacheAlignedWarning = 1;
  505. nu_pdma_desc_t next = head;
  506. while (next != RT_NULL)
  507. {
  508. uint32_t u32TxCnt = ((next->CTL & PDMA_DSCT_CTL_TXCNT_Msk) >> PDMA_DSCT_CTL_TXCNT_Pos) + 1;
  509. uint32_t u32DataWidth = (1 << ((next->CTL & PDMA_DSCT_CTL_TXWIDTH_Msk) >> PDMA_DSCT_CTL_TXWIDTH_Pos));
  510. uint32_t u32SrcCtl = (next->CTL & PDMA_DSCT_CTL_SAINC_Msk);
  511. uint32_t u32DstCtl = (next->CTL & PDMA_DSCT_CTL_DAINC_Msk);
  512. uint32_t u32FlushLen = u32TxCnt * u32DataWidth;
  513. /* Flush Src buffer into memory. */
  514. if ((u32SrcCtl == PDMA_SAR_INC)) // for M2P, M2M
  515. mmu_clean_invalidated_dcache(next->SA, u32FlushLen);
  516. /* Flush Dst buffer into memory. */
  517. if ((u32DstCtl == PDMA_DAR_INC)) // for P2M, M2M
  518. mmu_clean_invalidated_dcache(next->DA, u32FlushLen);
  519. /* Flush descriptor into memory */
  520. if (!((rt_uint32_t)next & NONCACHEABLE))
  521. mmu_clean_invalidated_dcache((rt_uint32_t)next, sizeof(DSCT_T));
  522. if (bNonCacheAlignedWarning)
  523. {
  524. if ((u32FlushLen & (CACHE_LINE_SIZE - 1)) ||
  525. (next->SA & (CACHE_LINE_SIZE - 1)) ||
  526. (next->DA & (CACHE_LINE_SIZE - 1)) ||
  527. ((rt_uint32_t)next & (CACHE_LINE_SIZE - 1)))
  528. {
  529. /*
  530. Race-condition avoidance between DMA-transferring and DCache write-back:
  531. Source, destination, DMA descriptor address and length should be aligned at len(CACHE_LINE_SIZE)
  532. */
  533. bNonCacheAlignedWarning = 0;
  534. rt_kprintf("[PDMA-W]\n");
  535. }
  536. }
  537. next = (nu_pdma_desc_t)next->NEXT;
  538. if (next == head) break;
  539. }
  540. }
  541. #endif
  542. /* If peripheral is M2M, trigger it. */
  543. if (u32Peripheral == PDMA_MEM)
  544. {
  545. PDMA_Trigger(PDMA, NU_PDMA_GET_MOD_CHIDX(i32ChannID));
  546. }
  547. else if (psPdmaChann->m_sCB_Trigger.m_pfnCBHandler)
  548. {
  549. psPdmaChann->m_sCB_Trigger.m_pfnCBHandler(psPdmaChann->m_sCB_Trigger.m_pvUserData, psPdmaChann->m_sCB_Trigger.m_u32Reserved);
  550. }
  551. }
  552. static void _nu_pdma_free_sgtbls(nu_pdma_chn_t *psPdmaChann)
  553. {
  554. if (psPdmaChann->m_ppsSgtbl)
  555. {
  556. nu_pdma_sgtbls_free(psPdmaChann->m_ppsSgtbl, psPdmaChann->m_u32WantedSGTblNum);
  557. rt_free_align((void *)psPdmaChann->m_ppsSgtbl);
  558. psPdmaChann->m_ppsSgtbl = RT_NULL;
  559. psPdmaChann->m_u32WantedSGTblNum = 0;
  560. }
  561. }
  562. static rt_err_t _nu_pdma_transfer_chain(int i32ChannID, uint32_t u32DataWidth, uint32_t u32AddrSrc, uint32_t u32AddrDst, uint32_t u32TransferCnt, uint32_t u32IdleTimeout_us)
  563. {
  564. int i = 0;
  565. rt_err_t ret = RT_ERROR;
  566. nu_pdma_periph_ctl_t *psPeriphCtl = NULL;
  567. nu_pdma_chn_t *psPdmaChann = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos];
  568. nu_pdma_memctrl_t eMemCtl = nu_pdma_channel_memctrl_get(i32ChannID);
  569. rt_uint32_t u32Offset = 0;
  570. rt_uint32_t u32TxCnt = 0;
  571. psPeriphCtl = &psPdmaChann->m_spPeripCtl;
  572. if (psPdmaChann->m_u32WantedSGTblNum != (u32TransferCnt / NU_PDMA_MAX_TXCNT + 1))
  573. {
  574. if (psPdmaChann->m_u32WantedSGTblNum > 0)
  575. _nu_pdma_free_sgtbls(psPdmaChann);
  576. psPdmaChann->m_u32WantedSGTblNum = u32TransferCnt / NU_PDMA_MAX_TXCNT + 1;
  577. psPdmaChann->m_ppsSgtbl = (nu_pdma_desc_t *)rt_malloc_align(sizeof(nu_pdma_desc_t) * psPdmaChann->m_u32WantedSGTblNum, 4);
  578. if (!psPdmaChann->m_ppsSgtbl)
  579. goto exit__nu_pdma_transfer_chain;
  580. ret = nu_pdma_sgtbls_allocate(psPdmaChann->m_ppsSgtbl, psPdmaChann->m_u32WantedSGTblNum);
  581. if (ret != RT_EOK)
  582. goto exit__nu_pdma_transfer_chain;
  583. }
  584. for (i = 0; i < psPdmaChann->m_u32WantedSGTblNum; i++)
  585. {
  586. u32TxCnt = (u32TransferCnt > NU_PDMA_MAX_TXCNT) ? NU_PDMA_MAX_TXCNT : u32TransferCnt;
  587. ret = nu_pdma_desc_setup(i32ChannID,
  588. psPdmaChann->m_ppsSgtbl[i],
  589. u32DataWidth,
  590. (eMemCtl & 0x2ul) ? u32AddrSrc + u32Offset : u32AddrSrc, /* Src address is Inc or not. */
  591. (eMemCtl & 0x1ul) ? u32AddrDst + u32Offset : u32AddrDst, /* Dst address is Inc or not. */
  592. u32TxCnt,
  593. ((i + 1) == psPdmaChann->m_u32WantedSGTblNum) ? RT_NULL : psPdmaChann->m_ppsSgtbl[i + 1],
  594. ((i + 1) == psPdmaChann->m_u32WantedSGTblNum) ? 0 : 1); // Silent, w/o TD interrupt
  595. if (ret != RT_EOK)
  596. goto exit__nu_pdma_transfer_chain;
  597. u32TransferCnt -= u32TxCnt;
  598. u32Offset += (u32TxCnt * u32DataWidth / 8);
  599. }
  600. _nu_pdma_transfer(i32ChannID, psPeriphCtl->m_u32Peripheral, psPdmaChann->m_ppsSgtbl[0], u32IdleTimeout_us);
  601. ret = RT_EOK;
  602. return ret;
  603. exit__nu_pdma_transfer_chain:
  604. _nu_pdma_free_sgtbls(psPdmaChann);
  605. return -(ret);
  606. }
  607. rt_err_t nu_pdma_transfer(int i32ChannID, uint32_t u32DataWidth, uint32_t u32AddrSrc, uint32_t u32AddrDst, uint32_t u32TransferCnt, uint32_t u32IdleTimeout_us)
  608. {
  609. rt_err_t ret = RT_EINVAL;
  610. PDMA_T *PDMA = NU_PDMA_GET_BASE(i32ChannID);
  611. nu_pdma_desc_t head;
  612. nu_pdma_chn_t *psPdmaChann;
  613. nu_pdma_periph_ctl_t *psPeriphCtl = NULL;
  614. if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  615. goto exit_nu_pdma_transfer;
  616. else if (!u32TransferCnt)
  617. goto exit_nu_pdma_transfer;
  618. else if (u32TransferCnt > NU_PDMA_MAX_TXCNT)
  619. return _nu_pdma_transfer_chain(i32ChannID, u32DataWidth, u32AddrSrc, u32AddrDst, u32TransferCnt, u32IdleTimeout_us);
  620. psPdmaChann = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos];
  621. psPeriphCtl = &psPdmaChann->m_spPeripCtl;
  622. head = &PDMA->DSCT[NU_PDMA_GET_MOD_CHIDX(i32ChannID)];
  623. ret = nu_pdma_desc_setup(i32ChannID,
  624. head,
  625. u32DataWidth,
  626. u32AddrSrc,
  627. u32AddrDst,
  628. u32TransferCnt,
  629. RT_NULL,
  630. 0);
  631. if (ret != RT_EOK)
  632. goto exit_nu_pdma_transfer;
  633. _nu_pdma_transfer(i32ChannID, psPeriphCtl->m_u32Peripheral, head, u32IdleTimeout_us);
  634. ret = RT_EOK;
  635. exit_nu_pdma_transfer:
  636. return -(ret);
  637. }
  638. rt_err_t nu_pdma_sg_transfer(int i32ChannID, nu_pdma_desc_t head, uint32_t u32IdleTimeout_us)
  639. {
  640. rt_err_t ret = RT_EINVAL;
  641. nu_pdma_periph_ctl_t *psPeriphCtl = NULL;
  642. if (!head)
  643. goto exit_nu_pdma_sg_transfer;
  644. else if (!(nu_pdma_chn_mask & (1 << i32ChannID)))
  645. goto exit_nu_pdma_sg_transfer;
  646. psPeriphCtl = &nu_pdma_chn_arr[i32ChannID - NU_PDMA_CH_Pos].m_spPeripCtl;
  647. _nu_pdma_transfer(i32ChannID, psPeriphCtl->m_u32Peripheral, head, u32IdleTimeout_us);
  648. ret = RT_EOK;
  649. exit_nu_pdma_sg_transfer:
  650. return -(ret);
  651. }
  652. static void nu_pdma_isr(int vector, void *pvdata)
  653. {
  654. int i;
  655. PDMA_T *PDMA = (void *)pvdata;
  656. uint32_t intsts = PDMA_GET_INT_STATUS(PDMA);
  657. uint32_t abtsts = PDMA_GET_ABORT_STS(PDMA);
  658. uint32_t tdsts = PDMA_GET_TD_STS(PDMA);
  659. uint32_t unalignsts = PDMA_GET_ALIGN_STS(PDMA);
  660. uint32_t reqto = intsts & PDMA_INTSTS_REQTOFn_Msk;
  661. uint32_t reqto_ch = (reqto >> PDMA_INTSTS_REQTOF0_Pos);
  662. int allch_sts = (reqto_ch | tdsts | abtsts | unalignsts);
  663. // Abort
  664. if (intsts & PDMA_INTSTS_ABTIF_Msk)
  665. {
  666. // Clear all Abort flags
  667. PDMA_CLR_ABORT_FLAG(PDMA, abtsts);
  668. }
  669. // Transfer done
  670. if (intsts & PDMA_INTSTS_TDIF_Msk)
  671. {
  672. // Clear all transfer done flags
  673. PDMA_CLR_TD_FLAG(PDMA, tdsts);
  674. }
  675. // Unaligned
  676. if (intsts & PDMA_INTSTS_ALIGNF_Msk)
  677. {
  678. // Clear all Unaligned flags
  679. PDMA_CLR_ALIGN_FLAG(PDMA, unalignsts);
  680. }
  681. // Timeout
  682. if (reqto)
  683. {
  684. // Clear all Timeout flags
  685. PDMA->INTSTS = reqto;
  686. }
  687. // Find the position of first '1' in allch_sts.
  688. while ((i = nu_ctz(allch_sts)) != 32)
  689. {
  690. int j = i;
  691. int ch_mask = (1 << i);
  692. if (PDMA == PDMA1)
  693. {
  694. j += PDMA_CH_MAX;
  695. }
  696. if (nu_pdma_chn_mask & (1 << j))
  697. {
  698. int ch_event = 0;
  699. nu_pdma_chn_t *dma_chn = nu_pdma_chn_arr + j - NU_PDMA_CH_Pos;
  700. if (dma_chn->m_sCB_Event.m_pfnCBHandler)
  701. {
  702. if (abtsts & ch_mask)
  703. {
  704. ch_event |= NU_PDMA_EVENT_ABORT;
  705. }
  706. if (tdsts & ch_mask)
  707. {
  708. ch_event |= NU_PDMA_EVENT_TRANSFER_DONE;
  709. }
  710. if (unalignsts & ch_mask)
  711. {
  712. ch_event |= NU_PDMA_EVENT_ALIGNMENT;
  713. }
  714. if (reqto_ch & ch_mask)
  715. {
  716. PDMA_DisableTimeout(PDMA, ch_mask);
  717. ch_event |= NU_PDMA_EVENT_TIMEOUT;
  718. }
  719. if (dma_chn->m_sCB_Disable.m_pfnCBHandler)
  720. dma_chn->m_sCB_Disable.m_pfnCBHandler(dma_chn->m_sCB_Disable.m_pvUserData, dma_chn->m_sCB_Disable.m_u32Reserved);
  721. if (dma_chn->m_u32EventFilter & ch_event)
  722. dma_chn->m_sCB_Event.m_pfnCBHandler(dma_chn->m_sCB_Event.m_pvUserData, ch_event);
  723. if (reqto_ch & ch_mask)
  724. nu_pdma_timeout_set(j, nu_pdma_chn_arr[j - NU_PDMA_CH_Pos].m_u32IdleTimeout_us);
  725. }//if(dma_chn->handler)
  726. } //if (nu_pdma_chn_mask & ch_mask)
  727. // Clear the served bit.
  728. allch_sts &= ~ch_mask;
  729. } //while
  730. }
  731. static void nu_pdma_memfun_actor_init(void)
  732. {
  733. int i = 0 ;
  734. nu_pdma_init();
  735. for (i = 0; i < NU_PDMA_MEMFUN_ACTOR_MAX; i++)
  736. {
  737. rt_memset(&nu_pdma_memfun_actor_arr[i], 0, sizeof(struct nu_pdma_memfun_actor));
  738. if (-(RT_ERROR) != (nu_pdma_memfun_actor_arr[i].m_i32ChannID = nu_pdma_channel_allocate(PDMA_MEM)))
  739. {
  740. nu_pdma_memfun_actor_arr[i].m_psSemMemFun = rt_sem_create("memactor_sem", 0, RT_IPC_FLAG_FIFO);
  741. }
  742. else
  743. break;
  744. }
  745. if (i)
  746. {
  747. nu_pdma_memfun_actor_maxnum = i;
  748. nu_pdma_memfun_actor_mask = ~(((1 << i) - 1));
  749. nu_pdma_memfun_actor_pool_sem = rt_sem_create("mempool_sem", nu_pdma_memfun_actor_maxnum, RT_IPC_FLAG_FIFO);
  750. RT_ASSERT(nu_pdma_memfun_actor_pool_sem != RT_NULL);
  751. nu_pdma_memfun_actor_pool_lock = rt_mutex_create("mempool_lock", RT_IPC_FLAG_PRIO);
  752. RT_ASSERT(nu_pdma_memfun_actor_pool_lock != RT_NULL);
  753. }
  754. }
  755. static void nu_pdma_memfun_cb(void *pvUserData, uint32_t u32Events)
  756. {
  757. rt_err_t result = RT_EOK;
  758. nu_pdma_memfun_actor_t psMemFunActor = (nu_pdma_memfun_actor_t)pvUserData;
  759. psMemFunActor->m_u32Result = u32Events;
  760. result = rt_sem_release(psMemFunActor->m_psSemMemFun);
  761. RT_ASSERT(result == RT_EOK);
  762. }
  763. static int nu_pdma_memfun_employ(void)
  764. {
  765. int idx = -1 ;
  766. rt_err_t result = RT_EOK;
  767. /* Headhunter */
  768. if (nu_pdma_memfun_actor_pool_sem &&
  769. ((result = rt_sem_take(nu_pdma_memfun_actor_pool_sem, RT_WAITING_FOREVER)) == RT_EOK))
  770. {
  771. result = rt_mutex_take(nu_pdma_memfun_actor_pool_lock, RT_WAITING_FOREVER);
  772. RT_ASSERT(result == RT_EOK);
  773. /* Find the position of first '0' in nu_pdma_memfun_actor_mask. */
  774. idx = nu_cto(nu_pdma_memfun_actor_mask);
  775. if (idx != 32)
  776. {
  777. nu_pdma_memfun_actor_mask |= (1 << idx);
  778. }
  779. else
  780. {
  781. idx = -1;
  782. }
  783. result = rt_mutex_release(nu_pdma_memfun_actor_pool_lock);
  784. RT_ASSERT(result == RT_EOK);
  785. }
  786. return idx;
  787. }
  788. static rt_size_t nu_pdma_memfun(void *dest, void *src, uint32_t u32DataWidth, unsigned int u32TransferCnt, nu_pdma_memctrl_t eMemCtl)
  789. {
  790. nu_pdma_memfun_actor_t psMemFunActor = NULL;
  791. struct nu_pdma_chn_cb sChnCB;
  792. rt_err_t result = RT_ERROR;
  793. int idx;
  794. rt_size_t ret = 0;
  795. /* Employ actor */
  796. while ((idx = nu_pdma_memfun_employ()) < 0);
  797. psMemFunActor = &nu_pdma_memfun_actor_arr[idx];
  798. /* Set PDMA memory control to eMemCtl. */
  799. nu_pdma_channel_memctrl_set(psMemFunActor->m_i32ChannID, eMemCtl);
  800. /* Register ISR callback function */
  801. sChnCB.m_eCBType = eCBType_Event;
  802. sChnCB.m_pfnCBHandler = nu_pdma_memfun_cb;
  803. sChnCB.m_pvUserData = (void *)psMemFunActor;
  804. nu_pdma_filtering_set(psMemFunActor->m_i32ChannID, NU_PDMA_EVENT_ABORT | NU_PDMA_EVENT_TRANSFER_DONE);
  805. nu_pdma_callback_register(psMemFunActor->m_i32ChannID, &sChnCB);
  806. psMemFunActor->m_u32Result = 0;
  807. /* Trigger it */
  808. nu_pdma_transfer(psMemFunActor->m_i32ChannID,
  809. u32DataWidth,
  810. (uint32_t)src,
  811. (uint32_t)dest,
  812. u32TransferCnt,
  813. 0);
  814. /* Wait it done. */
  815. result = rt_sem_take(psMemFunActor->m_psSemMemFun, RT_WAITING_FOREVER);
  816. RT_ASSERT(result == RT_EOK);
  817. /* Give result if get NU_PDMA_EVENT_TRANSFER_DONE.*/
  818. if (psMemFunActor->m_u32Result & NU_PDMA_EVENT_TRANSFER_DONE)
  819. {
  820. ret += u32TransferCnt;
  821. }
  822. else
  823. {
  824. ret += (u32TransferCnt - nu_pdma_non_transfer_count_get(psMemFunActor->m_i32ChannID));
  825. }
  826. /* Terminate it if get ABORT event */
  827. if (psMemFunActor->m_u32Result & NU_PDMA_EVENT_ABORT)
  828. {
  829. nu_pdma_channel_terminate(psMemFunActor->m_i32ChannID);
  830. }
  831. result = rt_mutex_take(nu_pdma_memfun_actor_pool_lock, RT_WAITING_FOREVER);
  832. RT_ASSERT(result == RT_EOK);
  833. nu_pdma_memfun_actor_mask &= ~(1 << idx);
  834. result = rt_mutex_release(nu_pdma_memfun_actor_pool_lock);
  835. RT_ASSERT(result == RT_EOK);
  836. /* Fire actor */
  837. result = rt_sem_release(nu_pdma_memfun_actor_pool_sem);
  838. RT_ASSERT(result == RT_EOK);
  839. return ret;
  840. }
  841. rt_size_t nu_pdma_mempush(void *dest, void *src, uint32_t data_width, unsigned int transfer_count)
  842. {
  843. if (data_width == 8 || data_width == 16 || data_width == 32)
  844. return nu_pdma_memfun(dest, src, data_width, transfer_count, eMemCtl_SrcInc_DstFix);
  845. return 0;
  846. }
  847. void *nu_pdma_memcpy(void *dest, void *src, unsigned int count)
  848. {
  849. int i = 0;
  850. uint32_t u32Offset = 0;
  851. uint32_t u32Remaining = count;
  852. for (i = 4; (i > 0) && (u32Remaining > 0) ; i >>= 1)
  853. {
  854. uint32_t u32src = (uint32_t)src + u32Offset;
  855. uint32_t u32dest = (uint32_t)dest + u32Offset;
  856. if (((u32src % i) == (u32dest % i)) &&
  857. ((u32src % i) == 0) &&
  858. (RT_ALIGN_DOWN(u32Remaining, i) >= i))
  859. {
  860. uint32_t u32TXCnt = u32Remaining / i;
  861. if (u32TXCnt != nu_pdma_memfun((void *)u32dest, (void *)u32src, i * 8, u32TXCnt, eMemCtl_SrcInc_DstInc))
  862. goto exit_nu_pdma_memcpy;
  863. u32Offset += (u32TXCnt * i);
  864. u32Remaining -= (u32TXCnt * i);
  865. }
  866. }
  867. if (count == u32Offset)
  868. return dest;
  869. exit_nu_pdma_memcpy:
  870. return NULL;
  871. }
  872. /**
  873. * PDMA memfun actor initialization
  874. */
  875. int rt_hw_pdma_memfun_init(void)
  876. {
  877. nu_pdma_memfun_actor_init();
  878. return 0;
  879. }
  880. INIT_DEVICE_EXPORT(rt_hw_pdma_memfun_init);
  881. #endif // #if defined(BSP_USING_PDMA)