drv_pdma.c 32 KB

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