1
0

drv_pdma.c 30 KB

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