drv_scuart.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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-12-1 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. SCUART_CNT
  36. };
  37. /* Private typedef
  38. * --------------------------------------------------------------*/
  39. struct nu_scuart
  40. {
  41. rt_serial_t dev;
  42. char *name;
  43. uint32_t idx;
  44. IRQn_Type irqn;
  45. E_SYS_IPRST rstidx;
  46. E_SYS_IPCLK clkidx;
  47. };
  48. typedef struct nu_scuart *nu_scuart_t;
  49. /* Private functions
  50. * ------------------------------------------------------------*/
  51. static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
  52. struct serial_configure *cfg);
  53. static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
  54. void *arg);
  55. static int nu_scuart_send(struct rt_serial_device *serial, char c);
  56. static int nu_scuart_receive(struct rt_serial_device *serial);
  57. static void nu_scuart_isr(int vector, void *param);
  58. static const struct rt_uart_ops nu_scuart_ops =
  59. {
  60. .configure = nu_scuart_configure,
  61. .control = nu_scuart_control,
  62. .putc = nu_scuart_send,
  63. .getc = nu_scuart_receive,
  64. .dma_transmit = RT_NULL /* not support DMA mode */
  65. };
  66. static const struct serial_configure nu_scuart_default_config =
  67. RT_SERIAL_CONFIG_DEFAULT;
  68. static struct nu_scuart nu_scuart_arr[] =
  69. {
  70. #if defined(BSP_USING_SCUART0)
  71. {
  72. .name = "scuart0",
  73. .idx = 0,
  74. .irqn = IRQ_SMC0,
  75. .rstidx = SMC0RST,
  76. .clkidx = SMC0CKEN,
  77. },
  78. #endif
  79. #if defined(BSP_USING_SCUART1)
  80. {
  81. .name = "scuart1",
  82. .idx = 1,
  83. .irqn = IRQ_SMC1,
  84. .rstidx = SMC1RST,
  85. .clkidx = SMC1CKEN,
  86. },
  87. #endif
  88. }; /* scuart nu_scuart */
  89. /**
  90. * All SCUART interrupt service routine
  91. */
  92. static void nu_scuart_isr(int vector, void *param)
  93. {
  94. nu_scuart_t psNuSCUart = (nu_scuart_t)param;
  95. /* Handle RX event */
  96. if (SCUART_GET_INT_FLAG(psNuSCUart->idx, SC_INTSTS_RBTOIF_Msk) ||
  97. SCUART_GET_INT_FLAG(psNuSCUart->idx, SC_INTSTS_RDAIF_Msk))
  98. {
  99. rt_hw_serial_isr(&psNuSCUart->dev, RT_SERIAL_EVENT_RX_IND);
  100. // RDA is the only interrupt enabled in this driver, this status bit
  101. // automatically cleared after Rx FIFO empty. So no need to clear interrupt
  102. // status here.
  103. SCUART_CLR_INT_FLAG(psNuSCUart->idx, SC_INTSTS_RBTOIF_Msk);
  104. }
  105. }
  106. /**
  107. * Configure scuart port
  108. */
  109. static rt_err_t nu_scuart_configure(struct rt_serial_device *serial,
  110. struct serial_configure *cfg)
  111. {
  112. rt_err_t ret = RT_EOK;
  113. uint32_t scuart_word_len = 0;
  114. uint32_t scuart_stop_bit = 0;
  115. uint32_t scuart_parity = 0;
  116. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  117. RT_ASSERT(psNuSCUart != RT_NULL);
  118. /* Check baud rate */ RT_ASSERT(cfg->baud_rate != 0);
  119. RT_ASSERT(cfg->baud_rate != 0);
  120. /* Check word len */
  121. switch (cfg->data_bits)
  122. {
  123. case DATA_BITS_5:
  124. scuart_word_len = SCUART_CHAR_LEN_5;
  125. break;
  126. case DATA_BITS_6:
  127. scuart_word_len = SCUART_CHAR_LEN_6;
  128. break;
  129. case DATA_BITS_7:
  130. scuart_word_len = SCUART_CHAR_LEN_7;
  131. break;
  132. case DATA_BITS_8:
  133. scuart_word_len = SCUART_CHAR_LEN_8;
  134. break;
  135. default:
  136. LOG_E("Unsupported data length");
  137. ret = RT_EINVAL;
  138. goto exit_nu_scuart_configure;
  139. }
  140. /* Check stop bit */
  141. switch (cfg->stop_bits)
  142. {
  143. case STOP_BITS_1:
  144. scuart_stop_bit = SCUART_STOP_BIT_1;
  145. break;
  146. case STOP_BITS_2:
  147. scuart_stop_bit = SCUART_STOP_BIT_2;
  148. break;
  149. default:
  150. LOG_E("Unsupported stop bit");
  151. ret = RT_EINVAL;
  152. goto exit_nu_scuart_configure;
  153. }
  154. /* Check parity */
  155. switch (cfg->parity)
  156. {
  157. case PARITY_NONE:
  158. scuart_parity = SCUART_PARITY_NONE;
  159. break;
  160. case PARITY_ODD:
  161. scuart_parity = SCUART_PARITY_ODD;
  162. break;
  163. case PARITY_EVEN:
  164. scuart_parity = SCUART_PARITY_EVEN;
  165. break;
  166. default:
  167. LOG_E("Unsupported parity");
  168. ret = RT_EINVAL;
  169. goto exit_nu_scuart_configure;
  170. }
  171. nu_sys_ip_reset(psNuSCUart->rstidx);
  172. /* Open SCUART and set SCUART baud rate */
  173. SCUART_Open(psNuSCUart->idx, cfg->baud_rate);
  174. /* Set line configuration. */
  175. SCUART_SetLineConfig(psNuSCUart->idx, 0, scuart_word_len, scuart_parity,
  176. scuart_stop_bit);
  177. /* Enable interrupt. */
  178. rt_hw_interrupt_umask(psNuSCUart->irqn);
  179. exit_nu_scuart_configure:
  180. if (ret != RT_EOK)
  181. SCUART_Close(psNuSCUart->idx);
  182. return -(ret);
  183. }
  184. /**
  185. * SCUART interrupt control
  186. */
  187. static rt_err_t nu_scuart_control(struct rt_serial_device *serial, int cmd,
  188. void *arg)
  189. {
  190. rt_err_t result = RT_EOK;
  191. rt_uint32_t flag;
  192. rt_ubase_t ctrl_arg = (rt_ubase_t)arg;
  193. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  194. RT_ASSERT(psNuSCUart != RT_NULL);
  195. switch (cmd)
  196. {
  197. case RT_DEVICE_CTRL_CLR_INT:
  198. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Disable INT-RX */
  199. {
  200. flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
  201. SCUART_DISABLE_INT(psNuSCUart->idx, flag);
  202. }
  203. break;
  204. case RT_DEVICE_CTRL_SET_INT:
  205. if (ctrl_arg == RT_DEVICE_FLAG_INT_RX) /* Enable INT-RX */
  206. {
  207. flag = SC_INTEN_RDAIEN_Msk | SC_INTEN_RXTOIEN_Msk;
  208. SCUART_ENABLE_INT(psNuSCUart->idx, flag);
  209. }
  210. break;
  211. case RT_DEVICE_CTRL_CLOSE:
  212. /* Disable interrupt. */
  213. rt_hw_interrupt_mask(psNuSCUart->irqn);
  214. /* Close SCUART port */
  215. SCUART_Close(psNuSCUart->idx);
  216. break;
  217. default:
  218. result = -RT_EINVAL;
  219. break;
  220. }
  221. return result;
  222. }
  223. /**
  224. * SCUART put char
  225. */
  226. static int nu_scuart_send(struct rt_serial_device *serial, char c)
  227. {
  228. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  229. RT_ASSERT(psNuSCUart != RT_NULL);
  230. /* Waiting if TX-FIFO is full. */
  231. while (SCUART_IS_TX_FULL(psNuSCUart->idx)) ;
  232. /* Put char into TX-FIFO */
  233. SCUART_WRITE(psNuSCUart->idx, c);
  234. return 1;
  235. }
  236. /**
  237. * SCUART get char
  238. */
  239. static int nu_scuart_receive(struct rt_serial_device *serial)
  240. {
  241. nu_scuart_t psNuSCUart = (nu_scuart_t)serial;
  242. RT_ASSERT(psNuSCUart != RT_NULL);
  243. /* Return failure if RX-FIFO is empty. */
  244. if (SCUART_GET_RX_EMPTY(psNuSCUart->idx))
  245. {
  246. return -1;
  247. }
  248. /* Get char from RX-FIFO */
  249. return SCUART_READ(psNuSCUart->idx);
  250. }
  251. /**
  252. * Hardware SCUART Initialization
  253. */
  254. static int rt_hw_scuart_init(void)
  255. {
  256. int i;
  257. rt_uint32_t flag;
  258. rt_err_t ret = RT_EOK;
  259. for (i = (SCUART_START + 1); i < SCUART_CNT; i++)
  260. {
  261. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  262. nu_scuart_arr[i].dev.ops = &nu_scuart_ops;
  263. nu_scuart_arr[i].dev.config = nu_scuart_default_config;
  264. rt_hw_interrupt_install(nu_scuart_arr[i].irqn, nu_scuart_isr, &nu_scuart_arr[i], nu_scuart_arr[i].name);
  265. nu_sys_ipclk_enable(nu_scuart_arr[i].clkidx);
  266. ret = rt_hw_serial_register(&nu_scuart_arr[i].dev, nu_scuart_arr[i].name, flag, NULL);
  267. RT_ASSERT(ret == RT_EOK);
  268. }
  269. return (int)ret;
  270. }
  271. INIT_DEVICE_EXPORT(rt_hw_scuart_init);
  272. #endif //#if defined(BSP_USING_SCUART)