drv_uart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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_UART)
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #include "NuMicro.h"
  17. #include <drv_uart.h>
  18. #if defined(RT_SERIAL_USING_DMA)
  19. #include <drv_pdma.h>
  20. #endif
  21. /* Private define ---------------------------------------------------------------*/
  22. enum
  23. {
  24. UART_START = -1,
  25. #if defined(BSP_USING_UART0)
  26. UART0_IDX,
  27. #endif
  28. #if defined(BSP_USING_UART1)
  29. UART1_IDX,
  30. #endif
  31. #if defined(BSP_USING_UART2)
  32. UART2_IDX,
  33. #endif
  34. #if defined(BSP_USING_UART3)
  35. UART3_IDX,
  36. #endif
  37. #if defined(BSP_USING_UART4)
  38. UART4_IDX,
  39. #endif
  40. #if defined(BSP_USING_UART5)
  41. UART5_IDX,
  42. #endif
  43. #if defined(BSP_USING_UART6)
  44. UART6_IDX,
  45. #endif
  46. #if defined(BSP_USING_UART7)
  47. UART7_IDX,
  48. #endif
  49. UART_CNT
  50. };
  51. /* Private typedef --------------------------------------------------------------*/
  52. struct nu_uart
  53. {
  54. rt_serial_t dev;
  55. char *name;
  56. UART_T *uart_base;
  57. uint32_t uart_rst;
  58. IRQn_Type uart_irq_n;
  59. #if defined(RT_SERIAL_USING_DMA)
  60. uint32_t dma_flag;
  61. int16_t pdma_perp_tx;
  62. int8_t pdma_chanid_tx;
  63. int16_t pdma_perp_rx;
  64. int8_t pdma_chanid_rx;
  65. int32_t rx_write_offset;
  66. int32_t rxdma_trigger_len;
  67. #endif
  68. };
  69. typedef struct nu_uart *nu_uart_t;
  70. /* Private functions ------------------------------------------------------------*/
  71. static rt_err_t nu_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  72. static rt_err_t nu_uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  73. static int nu_uart_send(struct rt_serial_device *serial, char c);
  74. static int nu_uart_receive(struct rt_serial_device *serial);
  75. static void nu_uart_isr(nu_uart_t serial);
  76. #if defined(RT_SERIAL_USING_DMA)
  77. static rt_size_t nu_uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  78. static void nu_pdma_uart_rx_cb(void *pvOwner, uint32_t u32Events);
  79. static void nu_pdma_uart_tx_cb(void *pvOwner, uint32_t u32Events);
  80. #endif
  81. /* Public functions ------------------------------------------------------------*/
  82. /* Private variables ------------------------------------------------------------*/
  83. static const struct rt_uart_ops nu_uart_ops =
  84. {
  85. .configure = nu_uart_configure,
  86. .control = nu_uart_control,
  87. .putc = nu_uart_send,
  88. .getc = nu_uart_receive,
  89. #if defined(RT_SERIAL_USING_DMA)
  90. .dma_transmit = nu_uart_dma_transmit
  91. #else
  92. .dma_transmit = RT_NULL
  93. #endif
  94. };
  95. static const struct serial_configure nu_uart_default_config =
  96. RT_SERIAL_CONFIG_DEFAULT;
  97. static struct nu_uart nu_uart_arr [] =
  98. {
  99. #if defined(BSP_USING_UART0)
  100. {
  101. .name = "uart0",
  102. .uart_base = UART0,
  103. .uart_rst = UART0_RST,
  104. .uart_irq_n = UART0_IRQn,
  105. #if defined(RT_SERIAL_USING_DMA)
  106. #if defined(BSP_USING_UART0_TX_DMA)
  107. .pdma_perp_tx = PDMA_UART0_TX,
  108. #else
  109. .pdma_perp_tx = NU_PDMA_UNUSED,
  110. #endif
  111. #if defined(BSP_USING_UART0_RX_DMA)
  112. .pdma_perp_rx = PDMA_UART0_RX,
  113. .rx_write_offset = 0,
  114. #else
  115. .pdma_perp_rx = NU_PDMA_UNUSED,
  116. #endif
  117. #endif
  118. },
  119. #endif
  120. #if defined(BSP_USING_UART1)
  121. {
  122. .name = "uart1",
  123. .uart_base = UART1,
  124. .uart_rst = UART1_RST,
  125. .uart_irq_n = UART1_IRQn,
  126. #if defined(RT_SERIAL_USING_DMA)
  127. #if defined(BSP_USING_UART1_TX_DMA)
  128. .pdma_perp_tx = PDMA_UART1_TX,
  129. #else
  130. .pdma_perp_tx = NU_PDMA_UNUSED,
  131. #endif
  132. #if defined(BSP_USING_UART1_RX_DMA)
  133. .pdma_perp_rx = PDMA_UART1_RX,
  134. .rx_write_offset = 0,
  135. #else
  136. .pdma_perp_rx = NU_PDMA_UNUSED,
  137. #endif
  138. #endif
  139. },
  140. #endif
  141. #if defined(BSP_USING_UART2)
  142. {
  143. .name = "uart2",
  144. .uart_base = UART2,
  145. .uart_rst = UART2_RST,
  146. .uart_irq_n = UART2_IRQn,
  147. #if defined(RT_SERIAL_USING_DMA)
  148. #if defined(BSP_USING_UART2_TX_DMA)
  149. .pdma_perp_tx = PDMA_UART2_TX,
  150. #else
  151. .pdma_perp_tx = NU_PDMA_UNUSED,
  152. #endif
  153. #if defined(BSP_USING_UART2_RX_DMA)
  154. .pdma_perp_rx = PDMA_UART2_RX,
  155. .rx_write_offset = 0,
  156. #else
  157. .pdma_perp_rx = NU_PDMA_UNUSED,
  158. #endif
  159. #endif
  160. },
  161. #endif
  162. #if defined(BSP_USING_UART3)
  163. {
  164. .name = "uart3",
  165. .uart_base = UART3,
  166. .uart_rst = UART3_RST,
  167. .uart_irq_n = UART3_IRQn,
  168. #if defined(RT_SERIAL_USING_DMA)
  169. #if defined(BSP_USING_UART3_TX_DMA)
  170. .pdma_perp_tx = PDMA_UART3_TX,
  171. #else
  172. .pdma_perp_tx = NU_PDMA_UNUSED,
  173. #endif
  174. #if defined(BSP_USING_UART3_RX_DMA)
  175. .pdma_perp_rx = PDMA_UART3_RX,
  176. .rx_write_offset = 0,
  177. #else
  178. .pdma_perp_rx = NU_PDMA_UNUSED,
  179. #endif
  180. #endif
  181. },
  182. #endif
  183. #if defined(BSP_USING_UART4)
  184. {
  185. .name = "uart4",
  186. .uart_base = UART4,
  187. .uart_rst = UART4_RST,
  188. .uart_irq_n = UART4_IRQn,
  189. #if defined(RT_SERIAL_USING_DMA)
  190. #if defined(BSP_USING_UART4_TX_DMA)
  191. .pdma_perp_tx = PDMA_UART4_TX,
  192. #else
  193. .pdma_perp_tx = NU_PDMA_UNUSED,
  194. #endif
  195. #if defined(BSP_USING_UART4_RX_DMA)
  196. .pdma_perp_rx = PDMA_UART4_RX,
  197. .rx_write_offset = 0,
  198. #else
  199. .pdma_perp_rx = NU_PDMA_UNUSED,
  200. #endif
  201. #endif
  202. },
  203. #endif
  204. #if defined(BSP_USING_UART5)
  205. {
  206. .name = "uart5",
  207. .uart_base = UART5,
  208. .uart_rst = UART5_RST,
  209. .uart_irq_n = UART5_IRQn,
  210. #if defined(RT_SERIAL_USING_DMA)
  211. #if defined(BSP_USING_UART5_TX_DMA)
  212. .pdma_perp_tx = PDMA_UART5_TX,
  213. #else
  214. .pdma_perp_tx = NU_PDMA_UNUSED,
  215. #endif
  216. #if defined(BSP_USING_UART5_RX_DMA)
  217. .pdma_perp_rx = PDMA_UART5_RX,
  218. .rx_write_offset = 0,
  219. #else
  220. .pdma_perp_rx = NU_PDMA_UNUSED,
  221. #endif
  222. #endif
  223. },
  224. #endif
  225. #if defined(BSP_USING_UART6)
  226. {
  227. .name = "uart6",
  228. .uart_base = UART6,
  229. .uart_rst = UART6_RST,
  230. .uart_irq_n = UART6_IRQn,
  231. #if defined(RT_SERIAL_USING_DMA)
  232. #if defined(BSP_USING_UART6_TX_DMA)
  233. .pdma_perp_tx = PDMA_UART6_TX,
  234. #else
  235. .pdma_perp_tx = NU_PDMA_UNUSED,
  236. #endif
  237. #if defined(BSP_USING_UART6_RX_DMA)
  238. .pdma_perp_rx = PDMA_UART6_RX,
  239. .rx_write_offset = 0,
  240. #else
  241. .pdma_perp_rx = NU_PDMA_UNUSED,
  242. #endif
  243. #endif
  244. },
  245. #endif
  246. #if defined(BSP_USING_UART7)
  247. {
  248. .name = "uart7",
  249. .uart_base = UART7,
  250. .uart_rst = UART7_RST,
  251. .uart_irq_n = UART7_IRQn,
  252. #if defined(RT_SERIAL_USING_DMA)
  253. #if defined(BSP_USING_UART7_TX_DMA)
  254. .pdma_perp_tx = PDMA_UART7_TX,
  255. #else
  256. .pdma_perp_tx = NU_PDMA_UNUSED,
  257. #endif
  258. #if defined(BSP_USING_UART7_RX_DMA)
  259. .pdma_perp_rx = PDMA_UART7_RX,
  260. .rx_write_offset = 0,
  261. #else
  262. .pdma_perp_rx = NU_PDMA_UNUSED,
  263. #endif
  264. #endif
  265. },
  266. #endif
  267. {0}
  268. }; /* uart nu_uart */
  269. /* Interrupt Handle Function ----------------------------------------------------*/
  270. #if defined(BSP_USING_UART0)
  271. /* UART0 interrupt entry */
  272. void UART0_IRQHandler(void)
  273. {
  274. /* enter interrupt */
  275. rt_interrupt_enter();
  276. nu_uart_isr(&nu_uart_arr[UART0_IDX]);
  277. /* leave interrupt */
  278. rt_interrupt_leave();
  279. }
  280. #endif
  281. #if defined(BSP_USING_UART1)
  282. /* UART1 interrupt entry */
  283. void UART1_IRQHandler(void)
  284. {
  285. /* enter interrupt */
  286. rt_interrupt_enter();
  287. nu_uart_isr(&nu_uart_arr[UART1_IDX]);
  288. /* leave interrupt */
  289. rt_interrupt_leave();
  290. }
  291. #endif
  292. #if defined(BSP_USING_UART2)
  293. /* UART2 interrupt entry */
  294. void UART2_IRQHandler(void)
  295. {
  296. /* enter interrupt */
  297. rt_interrupt_enter();
  298. nu_uart_isr(&nu_uart_arr[UART2_IDX]);
  299. /* leave interrupt */
  300. rt_interrupt_leave();
  301. }
  302. #endif
  303. #if defined(BSP_USING_UART3)
  304. /* UART3 interrupt service routine */
  305. void UART3_IRQHandler(void)
  306. {
  307. /* enter interrupt */
  308. rt_interrupt_enter();
  309. nu_uart_isr(&nu_uart_arr[UART3_IDX]);
  310. /* leave interrupt */
  311. rt_interrupt_leave();
  312. }
  313. #endif
  314. #if defined(BSP_USING_UART4)
  315. /* UART4 interrupt entry */
  316. void UART4_IRQHandler(void)
  317. {
  318. /* enter interrupt */
  319. rt_interrupt_enter();
  320. nu_uart_isr(&nu_uart_arr[UART4_IDX]);
  321. /* leave interrupt */
  322. rt_interrupt_leave();
  323. }
  324. #endif
  325. #if defined(BSP_USING_UART5)
  326. /* UART5 interrupt entry */
  327. void UART5_IRQHandler(void)
  328. {
  329. /* enter interrupt */
  330. rt_interrupt_enter();
  331. nu_uart_isr(&nu_uart_arr[UART5_IDX]);
  332. /* leave interrupt */
  333. rt_interrupt_leave();
  334. }
  335. #endif
  336. #if defined(BSP_USING_UART6)
  337. /* UART6 interrupt entry */
  338. void UART6_IRQHandler(void)
  339. {
  340. /* enter interrupt */
  341. rt_interrupt_enter();
  342. nu_uart_isr(&nu_uart_arr[UART6_IDX]);
  343. /* leave interrupt */
  344. rt_interrupt_leave();
  345. }
  346. #endif
  347. #if defined(BSP_USING_UART7)
  348. /* UART7 interrupt entry */
  349. void UART7_IRQHandler(void)
  350. {
  351. /* enter interrupt */
  352. rt_interrupt_enter();
  353. nu_uart_isr(&nu_uart_arr[UART7_IDX]);
  354. /* leave interrupt */
  355. rt_interrupt_leave();
  356. }
  357. #endif
  358. /**
  359. * All UART interrupt service routine
  360. */
  361. static void nu_uart_isr(nu_uart_t serial)
  362. {
  363. /* Get base address of uart register */
  364. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  365. /* Get interrupt event */
  366. uint32_t u32IntSts = uart_base->INTSTS;
  367. uint32_t u32FIFOSts = uart_base->FIFOSTS;
  368. #if defined(RT_SERIAL_USING_DMA)
  369. if (u32IntSts & UART_INTSTS_HWRLSIF_Msk)
  370. {
  371. /* Drain RX FIFO to remove remain FEF frames in FIFO. */
  372. uart_base->FIFO |= UART_FIFO_RXRST_Msk;
  373. uart_base->FIFOSTS |= (UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk);
  374. return;
  375. }
  376. #endif
  377. /* Handle RX event */
  378. if (u32IntSts & (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk))
  379. {
  380. rt_hw_serial_isr(&serial->dev, RT_SERIAL_EVENT_RX_IND);
  381. }
  382. uart_base->INTSTS = u32IntSts;
  383. uart_base->FIFOSTS = u32FIFOSts;
  384. }
  385. /**
  386. * Configure uart port
  387. */
  388. static rt_err_t nu_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  389. {
  390. rt_err_t ret = RT_EOK;
  391. uint32_t uart_word_len = 0;
  392. uint32_t uart_stop_bit = 0;
  393. uint32_t uart_parity = 0;
  394. /* Get base address of uart register */
  395. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  396. /* Check baudrate */
  397. RT_ASSERT(cfg->baud_rate != 0);
  398. /* Check word len */
  399. switch (cfg->data_bits)
  400. {
  401. case DATA_BITS_5:
  402. uart_word_len = UART_WORD_LEN_5;
  403. break;
  404. case DATA_BITS_6:
  405. uart_word_len = UART_WORD_LEN_6;
  406. break;
  407. case DATA_BITS_7:
  408. uart_word_len = UART_WORD_LEN_7;
  409. break;
  410. case DATA_BITS_8:
  411. uart_word_len = UART_WORD_LEN_8;
  412. break;
  413. default:
  414. rt_kprintf("Unsupported data length");
  415. ret = RT_EINVAL;
  416. goto exit_nu_uart_configure;
  417. }
  418. /* Check stop bit */
  419. switch (cfg->stop_bits)
  420. {
  421. case STOP_BITS_1:
  422. uart_stop_bit = UART_STOP_BIT_1;
  423. break;
  424. case STOP_BITS_2:
  425. uart_stop_bit = UART_STOP_BIT_2;
  426. break;
  427. default:
  428. rt_kprintf("Unsupported stop bit");
  429. ret = RT_EINVAL;
  430. goto exit_nu_uart_configure;
  431. }
  432. /* Check parity */
  433. switch (cfg->parity)
  434. {
  435. case PARITY_NONE:
  436. uart_parity = UART_PARITY_NONE;
  437. break;
  438. case PARITY_ODD:
  439. uart_parity = UART_PARITY_ODD;
  440. break;
  441. case PARITY_EVEN:
  442. uart_parity = UART_PARITY_EVEN;
  443. break;
  444. default:
  445. rt_kprintf("Unsupported parity");
  446. ret = RT_EINVAL;
  447. goto exit_nu_uart_configure;
  448. }
  449. /* Reset this module */
  450. SYS_ResetModule(((nu_uart_t)serial)->uart_rst);
  451. /* Open Uart and set UART Baudrate */
  452. UART_Open(uart_base, cfg->baud_rate);
  453. /* Set line configuration. */
  454. UART_SetLineConfig(uart_base, 0, uart_word_len, uart_parity, uart_stop_bit);
  455. /* Enable NVIC interrupt. */
  456. NVIC_EnableIRQ(((nu_uart_t)serial)->uart_irq_n);
  457. exit_nu_uart_configure:
  458. if (ret != RT_EOK)
  459. UART_Close(uart_base);
  460. return -(ret);
  461. }
  462. #if defined(RT_SERIAL_USING_DMA)
  463. static rt_err_t nu_pdma_uart_rx_config(struct rt_serial_device *serial, uint8_t *pu8Buf, int32_t i32TriggerLen)
  464. {
  465. rt_err_t result = RT_EOK;
  466. /* Get base address of uart register */
  467. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  468. result = nu_pdma_callback_register(((nu_uart_t)serial)->pdma_chanid_rx,
  469. nu_pdma_uart_rx_cb,
  470. (void *)serial,
  471. NU_PDMA_EVENT_TRANSFER_DONE | NU_PDMA_EVENT_TIMEOUT);
  472. if (result != RT_EOK)
  473. {
  474. goto exit_nu_pdma_uart_rx_config;
  475. }
  476. result = nu_pdma_transfer(((nu_uart_t)serial)->pdma_chanid_rx,
  477. 8,
  478. (uint32_t)uart_base,
  479. (uint32_t)pu8Buf,
  480. i32TriggerLen,
  481. 1000); //Idle-timeout, 1ms
  482. if (result != RT_EOK)
  483. {
  484. goto exit_nu_pdma_uart_rx_config;
  485. }
  486. /* Enable Receive Line interrupt & Start DMA RX transfer. */
  487. UART_ENABLE_INT(uart_base, UART_INTEN_RLSIEN_Msk);
  488. UART_PDMA_ENABLE(uart_base, UART_INTEN_RXPDMAEN_Msk);
  489. exit_nu_pdma_uart_rx_config:
  490. return result;
  491. }
  492. static void nu_pdma_uart_rx_cb(void *pvOwner, uint32_t u32Events)
  493. {
  494. rt_size_t recv_len = 0;
  495. rt_size_t transferred_rxbyte = 0;
  496. struct rt_serial_device *serial = (struct rt_serial_device *)pvOwner;
  497. nu_uart_t puart = (nu_uart_t)serial;
  498. RT_ASSERT(serial != RT_NULL);
  499. /* Get base address of uart register */
  500. UART_T *uart_base = puart->uart_base;
  501. transferred_rxbyte = nu_pdma_transferred_byte_get(puart->pdma_chanid_rx, puart->rxdma_trigger_len);
  502. if (u32Events & (NU_PDMA_EVENT_TRANSFER_DONE | NU_PDMA_EVENT_TIMEOUT))
  503. {
  504. if (u32Events & NU_PDMA_EVENT_TRANSFER_DONE)
  505. {
  506. if (serial->config.bufsz != 0)
  507. {
  508. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  509. nu_pdma_uart_rx_config(serial, &rx_fifo->buffer[0], puart->rxdma_trigger_len); // Config & trigger next
  510. }
  511. else
  512. {
  513. UART_DISABLE_INT(uart_base, UART_INTEN_RLSIEN_Msk);
  514. UART_PDMA_DISABLE(uart_base, UART_INTEN_RXPDMAEN_Msk);
  515. }
  516. transferred_rxbyte = puart->rxdma_trigger_len;
  517. }
  518. else if ((u32Events & NU_PDMA_EVENT_TIMEOUT) && !UART_GET_RX_EMPTY(uart_base))
  519. {
  520. return;
  521. }
  522. recv_len = transferred_rxbyte - puart->rx_write_offset;
  523. puart->rx_write_offset = transferred_rxbyte % puart->rxdma_trigger_len;
  524. }
  525. if ((serial->config.bufsz == 0) && (u32Events & NU_PDMA_EVENT_TRANSFER_DONE))
  526. {
  527. recv_len = puart->rxdma_trigger_len;
  528. }
  529. if (recv_len)
  530. {
  531. rt_hw_serial_isr(&puart->dev, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
  532. }
  533. }
  534. static rt_err_t nu_pdma_uart_tx_config(struct rt_serial_device *serial)
  535. {
  536. rt_err_t result = RT_EOK;
  537. RT_ASSERT(serial != RT_NULL);
  538. result = nu_pdma_callback_register(((nu_uart_t)serial)->pdma_chanid_tx,
  539. nu_pdma_uart_tx_cb,
  540. (void *)serial,
  541. NU_PDMA_EVENT_TRANSFER_DONE);
  542. return result;
  543. }
  544. static void nu_pdma_uart_tx_cb(void *pvOwner, uint32_t u32Events)
  545. {
  546. nu_uart_t puart = (nu_uart_t)pvOwner;
  547. RT_ASSERT(puart != RT_NULL);
  548. UART_PDMA_DISABLE(puart->uart_base, UART_INTEN_TXPDMAEN_Msk);// Stop DMA TX transfer
  549. if (u32Events & NU_PDMA_EVENT_TRANSFER_DONE)
  550. {
  551. rt_hw_serial_isr(&puart->dev, RT_SERIAL_EVENT_TX_DMADONE);
  552. }
  553. }
  554. /**
  555. * Uart DMA transfer
  556. */
  557. static rt_size_t nu_uart_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  558. {
  559. rt_err_t result = RT_EOK;
  560. RT_ASSERT(serial != RT_NULL);
  561. RT_ASSERT(buf != RT_NULL);
  562. /* Get base address of uart register */
  563. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  564. if (direction == RT_SERIAL_DMA_TX)
  565. {
  566. result = nu_pdma_transfer(((nu_uart_t)serial)->pdma_chanid_tx,
  567. 8,
  568. (uint32_t)buf,
  569. (uint32_t)uart_base,
  570. size,
  571. 0); // wait-forever
  572. UART_PDMA_ENABLE(uart_base, UART_INTEN_TXPDMAEN_Msk); // Start DMA TX transfer
  573. }
  574. else if (direction == RT_SERIAL_DMA_RX)
  575. {
  576. UART_DISABLE_INT(uart_base, UART_INTEN_RLSIEN_Msk);
  577. UART_PDMA_DISABLE(uart_base, UART_INTEN_RXPDMAEN_Msk);
  578. // If config.bufsz = 0, serial will trigger once.
  579. ((nu_uart_t)serial)->rxdma_trigger_len = size;
  580. ((nu_uart_t)serial)->rx_write_offset = 0;
  581. result = nu_pdma_uart_rx_config(serial, buf, size);
  582. }
  583. else
  584. {
  585. result = RT_ERROR;
  586. }
  587. return result;
  588. }
  589. static int nu_hw_uart_dma_allocate(nu_uart_t pusrt)
  590. {
  591. RT_ASSERT(pusrt != RT_NULL);
  592. /* Allocate UART_TX nu_dma channel */
  593. if (pusrt->pdma_perp_tx != NU_PDMA_UNUSED)
  594. {
  595. pusrt->pdma_chanid_tx = nu_pdma_channel_allocate(pusrt->pdma_perp_tx);
  596. if (pusrt->pdma_chanid_tx >= 0)
  597. {
  598. pusrt->dma_flag |= RT_DEVICE_FLAG_DMA_TX;
  599. }
  600. }
  601. /* Allocate UART_RX nu_dma channel */
  602. if (pusrt->pdma_perp_rx != NU_PDMA_UNUSED)
  603. {
  604. pusrt->pdma_chanid_rx = nu_pdma_channel_allocate(pusrt->pdma_perp_rx);
  605. if (pusrt->pdma_chanid_rx >= 0)
  606. {
  607. pusrt->dma_flag |= RT_DEVICE_FLAG_DMA_RX;
  608. }
  609. }
  610. return RT_EOK;
  611. }
  612. #endif
  613. /**
  614. * Uart interrupt control
  615. */
  616. static rt_err_t nu_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  617. {
  618. rt_err_t result = RT_EOK;
  619. rt_uint32_t flag;
  620. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  621. RT_ASSERT(serial != RT_NULL);
  622. /* Get base address of uart register */
  623. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  624. switch (cmd)
  625. {
  626. case RT_DEVICE_CTRL_CLR_INT:
  627. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Disable INT-RX */
  628. {
  629. flag = UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk | UART_INTEN_TOCNTEN_Msk;
  630. UART_DISABLE_INT(uart_base, flag);
  631. }
  632. else if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX) /* Disable DMA-RX */
  633. {
  634. /* Disable Receive Line interrupt & Stop DMA RX transfer. */
  635. #if defined(RT_SERIAL_USING_DMA)
  636. nu_pdma_channel_terminate(((nu_uart_t)serial)->pdma_chanid_rx);
  637. UART_DISABLE_INT(uart_base, UART_INTEN_RLSIEN_Msk);
  638. UART_PDMA_DISABLE(uart_base, UART_INTEN_RXPDMAEN_Msk);
  639. #endif
  640. }
  641. break;
  642. case RT_DEVICE_CTRL_SET_INT:
  643. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Enable INT-RX */
  644. {
  645. flag = UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk | UART_INTEN_TOCNTEN_Msk;
  646. UART_ENABLE_INT(uart_base, flag);
  647. }
  648. break;
  649. #if defined(RT_SERIAL_USING_DMA)
  650. case RT_DEVICE_CTRL_CONFIG:
  651. if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX) /* Configure and trigger DMA-RX */
  652. {
  653. struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  654. ((nu_uart_t)serial)->rxdma_trigger_len = serial->config.bufsz;
  655. ((nu_uart_t)serial)->rx_write_offset = 0;
  656. result = nu_pdma_uart_rx_config(serial, &rx_fifo->buffer[0], ((nu_uart_t)serial)->rxdma_trigger_len); // Config & trigger
  657. }
  658. else if (ctrl_arg == RT_DEVICE_FLAG_DMA_TX) /* Configure DMA-TX */
  659. {
  660. result = nu_pdma_uart_tx_config(serial);
  661. }
  662. break;
  663. #endif
  664. case RT_DEVICE_CTRL_CLOSE:
  665. /* Disable NVIC interrupt. */
  666. NVIC_DisableIRQ(((nu_uart_t)serial)->uart_irq_n);
  667. #if defined(RT_SERIAL_USING_DMA)
  668. nu_pdma_channel_terminate(((nu_uart_t)serial)->pdma_chanid_tx);
  669. nu_pdma_channel_terminate(((nu_uart_t)serial)->pdma_chanid_rx);
  670. #endif
  671. /* Reset this module */
  672. SYS_ResetModule(((nu_uart_t)serial)->uart_rst);
  673. /* Close UART port */
  674. UART_Close(uart_base);
  675. break;
  676. default:
  677. result = -RT_EINVAL;
  678. break;
  679. }
  680. return result;
  681. }
  682. /**
  683. * Uart put char
  684. */
  685. static int nu_uart_send(struct rt_serial_device *serial, char c)
  686. {
  687. RT_ASSERT(serial != RT_NULL);
  688. /* Get base address of uart register */
  689. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  690. /* Waiting if TX-FIFO is full. */
  691. while (UART_IS_TX_FULL(uart_base));
  692. /* Put char into TX-FIFO */
  693. UART_WRITE(uart_base, c);
  694. return 1;
  695. }
  696. /**
  697. * Uart get char
  698. */
  699. static int nu_uart_receive(struct rt_serial_device *serial)
  700. {
  701. RT_ASSERT(serial != RT_NULL);
  702. /* Get base address of uart register */
  703. UART_T *uart_base = ((nu_uart_t)serial)->uart_base;
  704. /* Return failure if RX-FIFO is empty. */
  705. if (UART_GET_RX_EMPTY(uart_base))
  706. {
  707. return -1;
  708. }
  709. /* Get char from RX-FIFO */
  710. return UART_READ(uart_base);
  711. }
  712. /**
  713. * Hardware UART Initialization
  714. */
  715. rt_err_t rt_hw_uart_init(void)
  716. {
  717. int i;
  718. rt_uint32_t flag;
  719. rt_err_t ret = RT_EOK;
  720. for (i = (UART_START + 1); i < UART_CNT; i++)
  721. {
  722. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  723. nu_uart_arr[i].dev.ops = &nu_uart_ops;
  724. nu_uart_arr[i].dev.config = nu_uart_default_config;
  725. #if defined(RT_SERIAL_USING_DMA)
  726. nu_uart_arr[i].dma_flag = 0;
  727. nu_hw_uart_dma_allocate(&nu_uart_arr[i]);
  728. flag |= nu_uart_arr[i].dma_flag;
  729. #endif
  730. ret = rt_hw_serial_register(&nu_uart_arr[i].dev, nu_uart_arr[i].name, flag, NULL);
  731. RT_ASSERT(ret == RT_EOK);
  732. }
  733. return ret;
  734. }
  735. #endif //#if defined(BSP_USING_UART)