drv_pdma.c 30 KB

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