drv_uart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-31 ZYH first version
  9. * 2018-12-10 Zohar_Lee format file
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #include <SWM320_port.h>
  15. #include <SWM320_uart.h>
  16. struct swm320_uart
  17. {
  18. UART_TypeDef *uart;
  19. IRQn_Type irq;
  20. };
  21. static rt_err_t swm320_uart_configure(struct rt_serial_device *serial,
  22. struct serial_configure *cfg)
  23. {
  24. struct swm320_uart *uart;
  25. UART_InitStructure UART_initStruct;
  26. RT_ASSERT(serial != RT_NULL);
  27. RT_ASSERT(cfg != RT_NULL);
  28. uart = (struct swm320_uart *)serial->parent.user_data;
  29. NVIC_DisableIRQ(uart->irq);
  30. UART_initStruct.Baudrate = cfg->baud_rate;
  31. UART_initStruct.RXThreshold = 1;
  32. UART_initStruct.RXThresholdIEn = 1;
  33. UART_initStruct.TXThresholdIEn = 0;
  34. UART_initStruct.TimeoutTime = 10;
  35. UART_initStruct.TimeoutIEn = 0;
  36. switch (cfg->data_bits)
  37. {
  38. case DATA_BITS_9:
  39. UART_initStruct.DataBits = UART_DATA_9BIT;
  40. break;
  41. default:
  42. UART_initStruct.DataBits = UART_DATA_8BIT;
  43. break;
  44. }
  45. switch (cfg->stop_bits)
  46. {
  47. case STOP_BITS_2:
  48. UART_initStruct.StopBits = UART_STOP_2BIT;
  49. break;
  50. default:
  51. UART_initStruct.StopBits = UART_STOP_1BIT;
  52. break;
  53. }
  54. switch (cfg->parity)
  55. {
  56. case PARITY_ODD:
  57. UART_initStruct.Parity = UART_PARITY_ODD;
  58. break;
  59. case PARITY_EVEN:
  60. UART_initStruct.Parity = UART_PARITY_EVEN;
  61. break;
  62. default:
  63. UART_initStruct.Parity = UART_PARITY_NONE;
  64. break;
  65. }
  66. UART_Init(uart->uart, &UART_initStruct);
  67. UART_Open(uart->uart);
  68. return RT_EOK;
  69. }
  70. static rt_err_t swm320_uart_control(struct rt_serial_device *serial,
  71. int cmd, void *arg)
  72. {
  73. struct swm320_uart *uart;
  74. RT_ASSERT(serial != RT_NULL);
  75. uart = (struct swm320_uart *)serial->parent.user_data;
  76. switch (cmd)
  77. {
  78. case RT_DEVICE_CTRL_CLR_INT:
  79. /* disable rx irq */
  80. NVIC_DisableIRQ(uart->irq);
  81. break;
  82. case RT_DEVICE_CTRL_SET_INT:
  83. /* enable rx irq */
  84. NVIC_EnableIRQ(uart->irq);
  85. break;
  86. }
  87. return RT_EOK;
  88. }
  89. static int swm320_uart_putc(struct rt_serial_device *serial, char c)
  90. {
  91. struct swm320_uart *uart;
  92. RT_ASSERT(serial != RT_NULL);
  93. uart = (struct swm320_uart *)serial->parent.user_data;
  94. while (UART_IsTXBusy(uart->uart));
  95. uart->uart->DATA = c;
  96. return 1;
  97. }
  98. static int swm320_uart_getc(struct rt_serial_device *serial)
  99. {
  100. int ch;
  101. struct swm320_uart *uart;
  102. RT_ASSERT(serial != RT_NULL);
  103. uart = (struct swm320_uart *)serial->parent.user_data;
  104. ch = -1;
  105. if (UART_IsRXFIFOEmpty(uart->uart) == 0)
  106. {
  107. UART_ReadByte(uart->uart, (uint32_t *)&ch);
  108. }
  109. return ch;
  110. }
  111. static const struct rt_uart_ops swm320_uart_ops =
  112. {
  113. swm320_uart_configure,
  114. swm320_uart_control,
  115. swm320_uart_putc,
  116. swm320_uart_getc,
  117. };
  118. #if defined(BSP_USING_UART0)
  119. /* UART0 device driver structure */
  120. static struct swm320_uart uart0;
  121. static struct rt_serial_device serial0;
  122. void UART0_Handler(void)
  123. {
  124. /* enter interrupt */
  125. rt_interrupt_enter();
  126. /* UART in mode Receiver */
  127. if (UART_INTRXThresholdStat(uart0.uart) || UART_INTTimeoutStat(uart0.uart))
  128. {
  129. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  130. }
  131. /* leave interrupt */
  132. rt_interrupt_leave();
  133. }
  134. #endif /* BSP_USING_UART0 */
  135. #if defined(BSP_USING_UART1)
  136. /* UART1 device driver structure */
  137. static struct swm320_uart uart1;
  138. static struct rt_serial_device serial1;
  139. void UART1_Handler(void)
  140. {
  141. /* enter interrupt */
  142. rt_interrupt_enter();
  143. /* UART in mode Receiver */
  144. if (UART_INTRXThresholdStat(uart1.uart) || UART_INTTimeoutStat(uart1.uart))
  145. {
  146. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  147. }
  148. /* leave interrupt */
  149. rt_interrupt_leave();
  150. }
  151. #endif /* BSP_USING_UART1 */
  152. #if defined(BSP_USING_UART2)
  153. /* UART2 device driver structure */
  154. static struct swm320_uart uart2;
  155. static struct rt_serial_device serial2;
  156. void UART2_Handler(void)
  157. {
  158. /* enter interrupt */
  159. rt_interrupt_enter();
  160. /* UART in mode Receiver */
  161. if (UART_INTRXThresholdStat(uart2.uart) || UART_INTTimeoutStat(uart2.uart))
  162. {
  163. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  164. }
  165. /* leave interrupt */
  166. rt_interrupt_leave();
  167. }
  168. #endif /* BSP_USING_UART2 */
  169. #if defined(BSP_USING_UART3)
  170. /* UART3 device driver structure */
  171. static struct swm320_uart uart3;
  172. static struct rt_serial_device serial3;
  173. void UART3_Handler(void)
  174. {
  175. /* enter interrupt */
  176. rt_interrupt_enter();
  177. /* UART in mode Receiver */
  178. if (UART_INTRXThresholdStat(uart3.uart) || UART_INTTimeoutStat(uart3.uart))
  179. {
  180. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  181. }
  182. /* leave interrupt */
  183. rt_interrupt_leave();
  184. }
  185. #endif /* BSP_USING_UART3 */
  186. int rt_hw_uart_init(void)
  187. {
  188. struct swm320_uart *uart;
  189. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  190. #ifdef BSP_USING_UART0
  191. PORT_Init(PORTA, PIN2, FUNMUX0_UART0_RXD, 1);
  192. PORT_Init(PORTA, PIN3, FUNMUX1_UART0_TXD, 0);
  193. uart = &uart0;
  194. uart->uart = UART0;
  195. uart->irq = UART0_IRQn;
  196. serial0.ops = &swm320_uart_ops;
  197. serial0.config = config;
  198. /* register UART0 device */
  199. rt_hw_serial_register(&serial0, "uart0",
  200. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  201. uart);
  202. #endif /* BSP_USING_UART0 */
  203. #ifdef BSP_USING_UART1
  204. PORT_Init(PORTC, PIN2, FUNMUX0_UART1_RXD, 1);
  205. PORT_Init(PORTC, PIN3, FUNMUX1_UART1_TXD, 0);
  206. uart = &uart1;
  207. uart->uart = UART1;
  208. uart->irq = UART1_IRQn;
  209. serial1.ops = &swm320_uart_ops;
  210. serial1.config = config;
  211. /* register UART1 device */
  212. rt_hw_serial_register(&serial1, "uart1",
  213. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  214. uart);
  215. #endif /* BSP_USING_UART1 */
  216. #ifdef BSP_USING_UART2
  217. PORT_Init(PORTC, PIN4, FUNMUX0_UART2_RXD, 1);
  218. PORT_Init(PORTC, PIN5, FUNMUX1_UART2_TXD, 0);
  219. uart = &uart2;
  220. uart->uart = UART2;
  221. uart->irq = UART2_IRQn;
  222. serial2.ops = &swm320_uart_ops;
  223. serial2.config = config;
  224. /* register UART2 device */
  225. rt_hw_serial_register(&serial2, "uart2",
  226. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  227. uart);
  228. #endif /* BSP_USING_UART2 */
  229. #ifdef BSP_USING_UART3
  230. PORT_Init(PORTC, PIN6, FUNMUX0_UART3_RXD, 1);
  231. PORT_Init(PORTC, PIN7, FUNMUX1_UART3_TXD, 0);
  232. uart = &uart3;
  233. uart->uart = UART3;
  234. uart->irq = UART3_IRQn;
  235. serial3.ops = &swm320_uart_ops;
  236. serial3.config = config;
  237. /* register UART3 device */
  238. rt_hw_serial_register(&serial3, "uart3",
  239. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  240. uart);
  241. #endif /* BSP_USING_UART3 */
  242. return 0;
  243. }
  244. INIT_BOARD_EXPORT(rt_hw_uart_init);