drv_usart.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. *
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Change Logs:
  6. * Date Author Notes
  7. * 2021-9-1 DongBowen first version
  8. */
  9. #include "drv_usart.h"
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #ifdef RT_USING_SERIAL
  14. #ifdef BSP_USING_UART
  15. #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && \
  16. !defined(BSP_USING_UART3)
  17. #error "Please define at least one BSP_USING_UARTx"
  18. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  19. #endif
  20. enum
  21. {
  22. #ifdef BSP_USING_UART0
  23. UART0_INDEX,
  24. #endif
  25. #ifdef BSP_USING_UART1
  26. UART1_INDEX,
  27. #endif
  28. #ifdef BSP_USING_UART2
  29. UART2_INDEX,
  30. #endif
  31. #ifdef BSP_USING_UART3
  32. UART3_INDEX,
  33. #endif
  34. };
  35. static struct hc_uart_cfg uart_cfg[] =
  36. {
  37. #ifdef BSP_USING_UART0
  38. UART0_CFG,
  39. #endif
  40. #ifdef BSP_USING_UART1
  41. UART1_CFG,
  42. #endif
  43. #ifdef BSP_USING_UART2
  44. UART2_CFG,
  45. #endif
  46. #ifdef BSP_USING_UART3
  47. UART3_CFG,
  48. #endif
  49. };
  50. static struct hc_uart uart_drv[sizeof(uart_cfg) / sizeof(uart_cfg[0])] = {0};
  51. static rt_err_t _uart_init(struct rt_serial_device *serial_device, struct serial_configure *configure)
  52. {
  53. stc_gpio_cfg_t stcGpioCfg;
  54. stc_uart_cfg_t stcCfg;
  55. struct hc_uart_cfg *cfg;
  56. RT_ASSERT(serial_device != RT_NULL);
  57. RT_ASSERT(configure != RT_NULL);
  58. cfg = serial_device->parent.user_data;
  59. /* configure rx and tx gpio */
  60. rt_memset(&stcGpioCfg, 0, sizeof(stcGpioCfg));
  61. Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
  62. stcGpioCfg.enDir = GpioDirOut;
  63. Gpio_Init(cfg->tx_port, cfg->tx_pin, &stcGpioCfg);
  64. Gpio_SetAfMode(cfg->tx_port, cfg->tx_pin, cfg->tx_af);
  65. stcGpioCfg.enDir = GpioDirIn;
  66. Gpio_Init(cfg->rx_port, cfg->rx_pin, &stcGpioCfg);
  67. Gpio_SetAfMode(cfg->rx_port, cfg->rx_pin, cfg->rx_af);
  68. /* configure uart */
  69. rt_memset(&stcCfg, 0, sizeof(stcCfg));
  70. Sysctrl_SetPeripheralGate(cfg->uart_periph, TRUE);
  71. stcCfg.stcBaud.u32Baud = configure->baud_rate;
  72. stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;
  73. stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq();
  74. switch (configure->data_bits)
  75. {
  76. case DATA_BITS_8:
  77. break;
  78. default:
  79. break;
  80. }
  81. switch (configure->stop_bits)
  82. {
  83. case STOP_BITS_1:
  84. stcCfg.enStopBit = UartMsk1bit;
  85. break;
  86. case STOP_BITS_2:
  87. stcCfg.enStopBit = UartMsk2bit;
  88. break;
  89. default:
  90. stcCfg.enStopBit = UartMsk1bit;
  91. break;
  92. }
  93. switch (configure->parity)
  94. {
  95. case PARITY_NONE:
  96. stcCfg.enMmdorCk = UartMskDataOrAddr;
  97. stcCfg.enRunMode = UartMskMode1;
  98. break;
  99. case PARITY_ODD:
  100. stcCfg.enMmdorCk = UartMskOdd;
  101. stcCfg.enRunMode = UartMskMode3;
  102. break;
  103. case PARITY_EVEN:
  104. stcCfg.enMmdorCk = UartMskEven;
  105. stcCfg.enRunMode = UartMskMode3;
  106. break;
  107. default:
  108. stcCfg.enMmdorCk = UartMskDataOrAddr;
  109. stcCfg.enRunMode = UartMskMode1;
  110. break;
  111. }
  112. Uart_Init(cfg->uart, &stcCfg);
  113. Uart_ClrStatus(cfg->uart, UartRC);
  114. Uart_ClrStatus(cfg->uart, UartTC);
  115. rt_hw_us_delay(2);
  116. return RT_EOK;
  117. }
  118. static rt_err_t _uart_control(struct rt_serial_device *serial_device, int cmd, void *arg)
  119. {
  120. struct hc_uart_cfg *cfg;
  121. RT_ASSERT(serial_device != RT_NULL);
  122. cfg = serial_device->parent.user_data;
  123. switch (cmd)
  124. {
  125. case RT_DEVICE_CTRL_CLR_INT:
  126. /* disable rx irq */
  127. Uart_DisableIrq(cfg->uart, UartRxIrq);
  128. EnableNvic(cfg->irqn, IrqLevel3, FALSE);
  129. break;
  130. case RT_DEVICE_CTRL_SET_INT:
  131. /* enable rx irq */
  132. Uart_EnableIrq(cfg->uart, UartRxIrq);
  133. EnableNvic(cfg->irqn, IrqLevel3, TRUE);
  134. break;
  135. }
  136. return RT_EOK;
  137. }
  138. static int _uart_putc(struct rt_serial_device *serial_device, char c)
  139. {
  140. struct hc_uart_cfg *cfg;
  141. RT_ASSERT(serial_device != RT_NULL);
  142. cfg = serial_device->parent.user_data;
  143. Uart_SendDataPoll(cfg->uart, (uint8_t)c);
  144. return 1;
  145. }
  146. static int _uart_getc(struct rt_serial_device *serial_device)
  147. {
  148. int ch;
  149. struct hc_uart_cfg *cfg;
  150. RT_ASSERT(serial_device != RT_NULL);
  151. cfg = serial_device->parent.user_data;
  152. ch = -1;
  153. if (Uart_GetStatus(cfg->uart, UartRC))
  154. {
  155. Uart_ClrStatus(cfg->uart, UartRC);
  156. ch = Uart_ReceiveData(cfg->uart);
  157. }
  158. return ch;
  159. }
  160. static const struct rt_uart_ops _uart_ops =
  161. {
  162. .configure = _uart_init,
  163. .control = _uart_control,
  164. .putc = _uart_putc,
  165. .getc = _uart_getc,
  166. .dma_transmit = RT_NULL
  167. };
  168. /**
  169. * Uart common interrupt process. This need add to uart ISR.
  170. *
  171. * @param serial serial device
  172. */
  173. static void rt_hw_uart_isr(struct rt_serial_device *serial_device)
  174. {
  175. struct hc_uart_cfg *cfg;
  176. uint32_t status;
  177. RT_ASSERT(serial_device != RT_NULL);
  178. cfg = serial_device->parent.user_data;
  179. status = cfg->uart->ISR;
  180. /* UART in mode Receiver -------------------------------------------------*/
  181. if (status & (1 << UartFE))
  182. {
  183. Uart_ClrStatus(cfg->uart, UartFE);
  184. }
  185. if (status & (1 << UartPE))
  186. {
  187. Uart_ClrStatus(cfg->uart, UartPE);
  188. }
  189. if (status & (1 << UartRC))
  190. {
  191. rt_hw_serial_isr(serial_device, RT_SERIAL_EVENT_RX_IND);
  192. }
  193. }
  194. #if defined(BSP_USING_UART0)
  195. void Uart0_IRQHandler(void)
  196. {
  197. /* enter interrupt */
  198. rt_interrupt_enter();
  199. rt_hw_uart_isr(&(uart_drv[UART0_INDEX].serial_device));
  200. /* leave interrupt */
  201. rt_interrupt_leave();
  202. }
  203. #endif /* BSP_USING_UART0 */
  204. #if defined(BSP_USING_UART1)
  205. void Uart1_IRQHandler(void)
  206. {
  207. /* enter interrupt */
  208. rt_interrupt_enter();
  209. rt_hw_uart_isr(&(uart_drv[UART1_INDEX].serial_device));
  210. /* leave interrupt */
  211. rt_interrupt_leave();
  212. }
  213. #endif /* BSP_USING_UART1 */
  214. #if defined(BSP_USING_UART2)
  215. void Uart2_IRQHandler(void)
  216. {
  217. /* enter interrupt */
  218. rt_interrupt_enter();
  219. rt_hw_uart_isr(&(uart_drv[UART2_INDEX].serial_device));
  220. /* leave interrupt */
  221. rt_interrupt_leave();
  222. }
  223. #endif /* BSP_USING_UART2 */
  224. #if defined(BSP_USING_UART3)
  225. void Uart3_IRQHandler(void)
  226. {
  227. /* enter interrupt */
  228. rt_interrupt_enter();
  229. rt_hw_uart_isr(&(uart_drv[UART3_INDEX].serial_device));
  230. /* leave interrupt */
  231. rt_interrupt_leave();
  232. }
  233. #endif /* BSP_USING_UART3 */
  234. int rt_hw_uart_init(void)
  235. {
  236. struct serial_configure cfg = RT_SERIAL_CONFIG_DEFAULT;
  237. int i = 0;
  238. rt_err_t result = RT_EOK;
  239. for (i = 0; i < sizeof(uart_cfg) / sizeof(uart_cfg[0]); i++)
  240. {
  241. uart_drv[i].cfg = &uart_cfg[i];
  242. uart_drv[i].serial_device.ops = &_uart_ops;
  243. uart_drv[i].serial_device.config = cfg;
  244. /* register UART device */
  245. result = rt_hw_serial_register(&uart_drv[i].serial_device, uart_drv[i].cfg->name,
  246. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart_drv[i].cfg);
  247. RT_ASSERT(result == RT_EOK);
  248. }
  249. return result;
  250. }
  251. INIT_BOARD_EXPORT(rt_hw_uart_init);
  252. #endif /* BSP_USING_UART */
  253. #endif /* RT_USING_SERIAL */