drv_scuart.c 9.1 KB

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