drv_scuart.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. * 2022-3-16 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if defined(BSP_USING_SCUART)
  14. #include "NuMicro.h"
  15. #include <rtdevice.h>
  16. #include <rthw.h>
  17. #include <drv_sys.h>
  18. /* Private definition
  19. * ---------------------------------------------------------------*/
  20. #define LOG_TAG "drv.scuart"
  21. #define DBG_ENABLE
  22. #define DBG_SECTION_NAME "drv.scuart"
  23. #define DBG_LEVEL DBG_ERROR
  24. #define DBG_COLOR
  25. #include <rtdbg.h>
  26. enum
  27. {
  28. SCUART_START = -1,
  29. #if defined(BSP_USING_SCUART0)
  30. SCUART0_IDX,
  31. #endif
  32. #if defined(BSP_USING_SCUART1)
  33. SCUART1_IDX,
  34. #endif
  35. #if defined(BSP_USING_SCUART2)
  36. SCUART2_IDX,
  37. #endif
  38. SCUART_CNT
  39. };
  40. /* Private typedef
  41. * --------------------------------------------------------------*/
  42. struct nu_scuart
  43. {
  44. rt_serial_t dev;
  45. char *name;
  46. SC_T *base;
  47. IRQn_Type irqn;
  48. uint32_t rstidx;
  49. uint32_t modid;
  50. };
  51. typedef struct nu_scuart *nu_scuart_t;
  52. /* Private functions
  53. * ------------------------------------------------------------*/
  54. static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
  55. struct serial_configure *cfg);
  56. static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
  57. void *arg);
  58. static int nu_scuart_send(struct rt_serial_device *serial, char c);
  59. static int nu_scuart_receive(struct rt_serial_device *serial);
  60. static void nu_scuart_isr(nu_scuart_t psNuSCUart);
  61. static const struct rt_uart_ops nu_scuart_ops =
  62. {
  63. .configure = nu_scuart_configure,
  64. .control = nu_scuart_control,
  65. .putc = nu_scuart_send,
  66. .getc = nu_scuart_receive,
  67. .dma_transmit = RT_NULL /* not support DMA mode */
  68. };
  69. static const struct serial_configure nu_scuart_default_config =
  70. RT_SERIAL_CONFIG_DEFAULT;
  71. static struct nu_scuart nu_scuart_arr[] =
  72. {
  73. #if defined(BSP_USING_SCUART0)
  74. {
  75. .name = "scuart0", .base = SC0, .irqn = SC0_IRQn, .rstidx = SC0_RST, .modid = SC0_MODULE,
  76. },
  77. #endif
  78. #if defined(BSP_USING_SCUART1)
  79. {
  80. .name = "scuart1", .base = SC1, .irqn = SC1_IRQn, .rstidx = SC1_RST, .modid = SC1_MODULE,
  81. },
  82. #endif
  83. #if defined(BSP_USING_SCUART2)
  84. {
  85. .name = "scuart2", .base = SC2, .irqn = SC2_IRQn, .rstidx = SC2_RST, .modid = SC2_MODULE,
  86. },
  87. #endif
  88. }; /* scuart nu_scuart */
  89. #if defined(BSP_USING_SCUART0)
  90. void SC0_IRQHandler(void)
  91. {
  92. rt_interrupt_enter();
  93. nu_scuart_isr((void *)&nu_scuart_arr[SCUART0_IDX]);
  94. rt_interrupt_leave();
  95. }
  96. #endif
  97. #if defined(BSP_USING_SCUART1)
  98. void SC1_IRQHandler(void)
  99. {
  100. rt_interrupt_enter();
  101. nu_scuart_isr((void *)&nu_scuart_arr[SCUART1_IDX]);
  102. rt_interrupt_leave();
  103. }
  104. #endif
  105. #if defined(BSP_USING_SCUART2)
  106. void SC2_IRQHandler(void)
  107. {
  108. rt_interrupt_enter();
  109. nu_scuart_isr((void *)&nu_scuart_arr[SCUART2_IDX]);
  110. rt_interrupt_leave();
  111. }
  112. #endif
  113. /**
  114. * All SCUART interrupt service routine
  115. */
  116. static void nu_scuart_isr(nu_scuart_t psNuSCUart)
  117. {
  118. /* Handle RX event */
  119. if (SCUART_GET_INT_FLAG(psNuSCUart->base, SC_INTSTS_RXTOIF_Msk) ||
  120. SCUART_GET_INT_FLAG(psNuSCUart->base, SC_INTSTS_RDAIF_Msk))
  121. {
  122. rt_hw_serial_isr(&psNuSCUart->dev, RT_SERIAL_EVENT_RX_IND);
  123. // RDA is the only interrupt enabled in this driver, this status bit
  124. // automatically cleared after Rx FIFO empty. So no need to clear interrupt
  125. // status here.
  126. SCUART_CLR_INT_FLAG(psNuSCUart->base, SC_INTSTS_RXTOIF_Msk);
  127. }
  128. }
  129. /**
  130. * Configure scuart port
  131. */
  132. static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
  133. struct serial_configure *cfg)
  134. {
  135. rt_err_t ret = RT_EOK;
  136. uint32_t scuart_word_len = 0;
  137. uint32_t scuart_stop_bit = 0;
  138. uint32_t scuart_parity = 0;
  139. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  140. RT_ASSERT(psNuSCUart != RT_NULL);
  141. /* Check baud rate */ RT_ASSERT(cfg->baud_rate != 0);
  142. RT_ASSERT(cfg->baud_rate != 0);
  143. /* Check word len */
  144. switch (cfg->data_bits)
  145. {
  146. case DATA_BITS_5:
  147. scuart_word_len = SCUART_CHAR_LEN_5;
  148. break;
  149. case DATA_BITS_6:
  150. scuart_word_len = SCUART_CHAR_LEN_6;
  151. break;
  152. case DATA_BITS_7:
  153. scuart_word_len = SCUART_CHAR_LEN_7;
  154. break;
  155. case DATA_BITS_8:
  156. scuart_word_len = SCUART_CHAR_LEN_8;
  157. break;
  158. default:
  159. LOG_E("Unsupported data length\n");
  160. ret = -RT_EINVAL;
  161. goto exit_nu_scuart_configure;
  162. }
  163. /* Check stop bit */
  164. switch (cfg->stop_bits)
  165. {
  166. case STOP_BITS_1:
  167. scuart_stop_bit = SCUART_STOP_BIT_1;
  168. break;
  169. case STOP_BITS_2:
  170. scuart_stop_bit = SCUART_STOP_BIT_2;
  171. break;
  172. default:
  173. LOG_E("Unsupported stop bit\n");
  174. ret = -RT_EINVAL;
  175. goto exit_nu_scuart_configure;
  176. }
  177. /* Check parity */
  178. switch (cfg->parity)
  179. {
  180. case PARITY_NONE:
  181. scuart_parity = SCUART_PARITY_NONE;
  182. break;
  183. case PARITY_ODD:
  184. scuart_parity = SCUART_PARITY_ODD;
  185. break;
  186. case PARITY_EVEN:
  187. scuart_parity = SCUART_PARITY_EVEN;
  188. break;
  189. default:
  190. LOG_E("Unsupported parity\n");
  191. ret = -RT_EINVAL;
  192. goto exit_nu_scuart_configure;
  193. }
  194. SYS_ResetModule(psNuSCUart->rstidx);
  195. /* Open SCUART and set SCUART baud rate */
  196. SCUART_Open(psNuSCUart->base, cfg->baud_rate);
  197. /* Set line configuration. */
  198. SCUART_SetLineConfig(psNuSCUart->base, 0, scuart_word_len, scuart_parity,
  199. scuart_stop_bit);
  200. /* Enable interrupt. */
  201. NVIC_EnableIRQ(psNuSCUart->irqn);
  202. exit_nu_scuart_configure:
  203. if (ret != RT_EOK)
  204. SCUART_Close(psNuSCUart->base);
  205. return -(ret);
  206. }
  207. /**
  208. * SCUART interrupt control
  209. */
  210. static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
  211. void *arg)
  212. {
  213. rt_err_t result = RT_EOK;
  214. rt_uint32_t flag;
  215. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  216. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  217. RT_ASSERT(psNuSCUart != RT_NULL);
  218. switch (cmd)
  219. {
  220. case RT_DEVICE_CTRL_CLR_INT:
  221. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Disable INT-RX */
  222. {
  223. flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
  224. SCUART_DISABLE_INT(psNuSCUart->base, flag);
  225. }
  226. break;
  227. case RT_DEVICE_CTRL_SET_INT:
  228. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Enable INT-RX */
  229. {
  230. flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
  231. SCUART_ENABLE_INT(psNuSCUart->base, flag);
  232. }
  233. break;
  234. case RT_DEVICE_CTRL_CLOSE:
  235. /* Disable interrupt. */
  236. NVIC_DisableIRQ(psNuSCUart->irqn);
  237. /* Close SCUART port */
  238. SCUART_Close(psNuSCUart->base);
  239. break;
  240. default:
  241. result = -RT_EINVAL;
  242. break;
  243. }
  244. return result;
  245. }
  246. /**
  247. * SCUART put char
  248. */
  249. static int nu_scuart_send(struct rt_serial_device *serial, char c)
  250. {
  251. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  252. RT_ASSERT(psNuSCUart != RT_NULL);
  253. /* Waiting if TX-FIFO is full. */
  254. while (SCUART_IS_TX_FULL(psNuSCUart->base)) ;
  255. /* Put char into TX-FIFO */
  256. SCUART_WRITE(psNuSCUart->base, c);
  257. return 1;
  258. }
  259. /**
  260. * SCUART get char
  261. */
  262. static int nu_scuart_receive(struct rt_serial_device *serial)
  263. {
  264. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  265. RT_ASSERT(psNuSCUart != RT_NULL);
  266. /* Return failure if RX-FIFO is empty. */
  267. if (SCUART_GET_RX_EMPTY(psNuSCUart->base))
  268. {
  269. return -1;
  270. }
  271. /* Get char from RX-FIFO */
  272. return SCUART_READ(psNuSCUart->base);
  273. }
  274. /**
  275. * Hardware SCUART Initialization
  276. */
  277. static int rt_hw_scuart_init(void)
  278. {
  279. int i;
  280. rt_uint32_t flag;
  281. rt_err_t ret = RT_EOK;
  282. for (i = (SCUART_START + 1); i < SCUART_CNT; i++)
  283. {
  284. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  285. nu_scuart_arr[i].dev.ops = &nu_scuart_ops;
  286. nu_scuart_arr[i].dev.config = nu_scuart_default_config;
  287. CLK_EnableModuleClock(nu_scuart_arr[i].modid);
  288. SYS_ResetModule(nu_scuart_arr[i].rstidx);
  289. ret = rt_hw_serial_register(&nu_scuart_arr[i].dev, nu_scuart_arr[i].name, flag, NULL);
  290. RT_ASSERT(ret == RT_EOK);
  291. }
  292. return (int)ret;
  293. }
  294. INIT_DEVICE_EXPORT(rt_hw_scuart_init);
  295. #endif //#if defined(BSP_USING_SCUART)