drv_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 2006-2022, 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. * 2020-07-10 lik format file
  11. */
  12. #include "drv_uart.h"
  13. #ifdef RT_USING_SERIAL
  14. #ifdef BSP_USING_UART
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.uart"
  17. #include <drv_log.h>
  18. #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && \
  19. !defined(BSP_USING_UART3)
  20. #error "Please define at least one BSP_USING_UARTx"
  21. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  22. #endif
  23. #ifdef BSP_USING_UART0
  24. #ifndef UART0_CFG
  25. #define UART0_CFG \
  26. { \
  27. .name = "uart0", \
  28. .UARTx = UART0, \
  29. .irq = UART0_IRQn, \
  30. .uart_initstruct.Baudrate = 115200, \
  31. .uart_initstruct.DataBits = UART_DATA_8BIT, \
  32. .uart_initstruct.Parity = UART_PARITY_NONE, \
  33. .uart_initstruct.StopBits = UART_STOP_1BIT, \
  34. .uart_initstruct.RXThreshold = 0, \
  35. .uart_initstruct.RXThresholdIEn = 1, \
  36. .uart_initstruct.TXThresholdIEn = 0, \
  37. .uart_initstruct.TimeoutTime = 10, \
  38. .uart_initstruct.TimeoutIEn = 1, \
  39. }
  40. #endif /* UART0_CFG */
  41. #endif /* BSP_USING_UART0 */
  42. #ifdef BSP_USING_UART1
  43. #ifndef UART1_CFG
  44. #define UART1_CFG \
  45. { \
  46. .name = "uart1", \
  47. .UARTx = UART1, \
  48. .irq = UART1_IRQn, \
  49. .uart_initstruct.Baudrate = 115200, \
  50. .uart_initstruct.DataBits = UART_DATA_8BIT, \
  51. .uart_initstruct.Parity = UART_PARITY_NONE, \
  52. .uart_initstruct.StopBits = UART_STOP_1BIT, \
  53. .uart_initstruct.RXThreshold = 0, \
  54. .uart_initstruct.RXThresholdIEn = 1, \
  55. .uart_initstruct.TXThresholdIEn = 0, \
  56. .uart_initstruct.TimeoutTime = 10, \
  57. .uart_initstruct.TimeoutIEn = 1, \
  58. }
  59. #endif /* UART1_CFG */
  60. #endif /* BSP_USING_UART1 */
  61. #ifdef BSP_USING_UART2
  62. #ifndef UART2_CFG
  63. #define UART2_CFG \
  64. { \
  65. .name = "uart2", \
  66. .UARTx = UART2, \
  67. .irq = UART2_IRQn, \
  68. .uart_initstruct.Baudrate = 115200, \
  69. .uart_initstruct.DataBits = UART_DATA_8BIT, \
  70. .uart_initstruct.Parity = UART_PARITY_NONE, \
  71. .uart_initstruct.StopBits = UART_STOP_1BIT, \
  72. .uart_initstruct.RXThreshold = 0, \
  73. .uart_initstruct.RXThresholdIEn = 1, \
  74. .uart_initstruct.TXThresholdIEn = 0, \
  75. .uart_initstruct.TimeoutTime = 10, \
  76. .uart_initstruct.TimeoutIEn = 1, \
  77. }
  78. #endif /* UART2_CFG */
  79. #endif /* BSP_USING_UART2 */
  80. #ifdef BSP_USING_UART3
  81. #ifndef UART3_CFG
  82. #define UART3_CFG \
  83. { \
  84. .name = "uart3", \
  85. .UARTx = UART3, \
  86. .irq = UART3_IRQn, \
  87. .uart_initstruct.Baudrate = 115200, \
  88. .uart_initstruct.DataBits = UART_DATA_8BIT, \
  89. .uart_initstruct.Parity = UART_PARITY_NONE, \
  90. .uart_initstruct.StopBits = UART_STOP_1BIT, \
  91. .uart_initstruct.RXThreshold = 0, \
  92. .uart_initstruct.RXThresholdIEn = 1, \
  93. .uart_initstruct.TXThresholdIEn = 0, \
  94. .uart_initstruct.TimeoutTime = 10, \
  95. .uart_initstruct.TimeoutIEn = 1, \
  96. }
  97. #endif /* UART3_CFG */
  98. #endif /* BSP_USING_UART3 */
  99. /* swm config class */
  100. struct swm_uart_cfg
  101. {
  102. const char *name;
  103. UART_TypeDef *UARTx;
  104. IRQn_Type irq;
  105. UART_InitStructure uart_initstruct;
  106. };
  107. /* swm uart dirver class */
  108. struct swm_uart
  109. {
  110. struct swm_uart_cfg *uart_cfg;
  111. struct rt_serial_device serial_device;
  112. };
  113. enum
  114. {
  115. #ifdef BSP_USING_UART0
  116. UART0_INDEX,
  117. #endif
  118. #ifdef BSP_USING_UART1
  119. UART1_INDEX,
  120. #endif
  121. #ifdef BSP_USING_UART2
  122. UART2_INDEX,
  123. #endif
  124. #ifdef BSP_USING_UART3
  125. UART3_INDEX,
  126. #endif
  127. };
  128. static struct swm_uart_cfg swm_uart_cfg[] =
  129. {
  130. #ifdef BSP_USING_UART0
  131. UART0_CFG,
  132. #endif
  133. #ifdef BSP_USING_UART1
  134. UART1_CFG,
  135. #endif
  136. #ifdef BSP_USING_UART2
  137. UART2_CFG,
  138. #endif
  139. #ifdef BSP_USING_UART3
  140. UART3_CFG,
  141. #endif
  142. };
  143. static struct swm_uart uart_obj[sizeof(swm_uart_cfg) / sizeof(swm_uart_cfg[0])] = {0};
  144. static rt_err_t swm_uart_configure(struct rt_serial_device *serial_device, struct serial_configure *configure)
  145. {
  146. struct swm_uart_cfg *uart_cfg;
  147. RT_ASSERT(serial_device != RT_NULL);
  148. RT_ASSERT(configure != RT_NULL);
  149. uart_cfg = serial_device->parent.user_data;
  150. uart_cfg->uart_initstruct.Baudrate = configure->baud_rate;
  151. switch (configure->data_bits)
  152. {
  153. case DATA_BITS_8:
  154. uart_cfg->uart_initstruct.DataBits = UART_DATA_8BIT;
  155. break;
  156. case DATA_BITS_9:
  157. uart_cfg->uart_initstruct.DataBits = UART_DATA_9BIT;
  158. break;
  159. default:
  160. uart_cfg->uart_initstruct.DataBits = UART_DATA_8BIT;
  161. break;
  162. }
  163. switch (configure->stop_bits)
  164. {
  165. case STOP_BITS_1:
  166. uart_cfg->uart_initstruct.StopBits = UART_STOP_1BIT;
  167. break;
  168. case STOP_BITS_2:
  169. uart_cfg->uart_initstruct.StopBits = UART_STOP_2BIT;
  170. break;
  171. default:
  172. uart_cfg->uart_initstruct.StopBits = UART_STOP_1BIT;
  173. break;
  174. }
  175. switch (configure->parity)
  176. {
  177. case PARITY_NONE:
  178. uart_cfg->uart_initstruct.Parity = UART_PARITY_NONE;
  179. break;
  180. case PARITY_ODD:
  181. uart_cfg->uart_initstruct.Parity = UART_PARITY_ODD;
  182. break;
  183. case PARITY_EVEN:
  184. uart_cfg->uart_initstruct.Parity = UART_PARITY_EVEN;
  185. break;
  186. default:
  187. uart_cfg->uart_initstruct.Parity = UART_PARITY_NONE;
  188. break;
  189. }
  190. UART_Init(uart_cfg->UARTx, &(uart_cfg->uart_initstruct));
  191. UART_Open(uart_cfg->UARTx);
  192. return RT_EOK;
  193. }
  194. static rt_err_t swm_uart_control(struct rt_serial_device *serial_device, int cmd, void *arg)
  195. {
  196. struct swm_uart_cfg *uart_cfg;
  197. RT_ASSERT(serial_device != RT_NULL);
  198. uart_cfg = serial_device->parent.user_data;
  199. switch (cmd)
  200. {
  201. case RT_DEVICE_CTRL_CLR_INT:
  202. /* disable rx irq */
  203. NVIC_DisableIRQ(uart_cfg->irq);
  204. break;
  205. case RT_DEVICE_CTRL_SET_INT:
  206. /* enable rx irq */
  207. NVIC_EnableIRQ(uart_cfg->irq);
  208. break;
  209. }
  210. return RT_EOK;
  211. }
  212. static int swm_uart_putc(struct rt_serial_device *serial_device, char c)
  213. {
  214. struct swm_uart_cfg *uart_cfg;
  215. RT_ASSERT(serial_device != RT_NULL);
  216. uart_cfg = serial_device->parent.user_data;
  217. while (UART_IsTXFIFOFull(uart_cfg->UARTx))
  218. ;
  219. UART_WriteByte(uart_cfg->UARTx, c);
  220. while (UART_IsTXBusy(uart_cfg->UARTx))
  221. ;
  222. return 1;
  223. }
  224. static int swm_uart_getc(struct rt_serial_device *serial_device)
  225. {
  226. int ch;
  227. struct swm_uart_cfg *uart_cfg;
  228. RT_ASSERT(serial_device != RT_NULL);
  229. uart_cfg = serial_device->parent.user_data;
  230. ch = -1;
  231. if (UART_IsRXFIFOEmpty(uart_cfg->UARTx) == 0)
  232. {
  233. UART_ReadByte(uart_cfg->UARTx, (uint32_t *)&ch);
  234. }
  235. return ch;
  236. }
  237. static const struct rt_uart_ops swm_uart_ops =
  238. {
  239. .configure = swm_uart_configure,
  240. .control = swm_uart_control,
  241. .putc = swm_uart_putc,
  242. .getc = swm_uart_getc,
  243. .dma_transmit = RT_NULL};
  244. /**
  245. * Uart common interrupt process. This need add to uart ISR.
  246. *
  247. * @param serial serial device
  248. */
  249. static void rt_hw_uart_isr(struct rt_serial_device *serial_device)
  250. {
  251. struct swm_uart_cfg *uart_cfg;
  252. RT_ASSERT(serial_device != RT_NULL);
  253. uart_cfg = serial_device->parent.user_data;
  254. /* UART in mode Receiver -------------------------------------------------*/
  255. if (UART_INTStat(uart_cfg->UARTx, UART_IT_RX_THR) || UART_INTStat(uart_cfg->UARTx, UART_IT_RX_TOUT))
  256. {
  257. rt_hw_serial_isr(serial_device, RT_SERIAL_EVENT_RX_IND);
  258. }
  259. }
  260. #if defined(BSP_USING_UART0)
  261. void UART0_Handler(void)
  262. {
  263. /* enter interrupt */
  264. rt_interrupt_enter();
  265. rt_hw_uart_isr(&(uart_obj[UART0_INDEX].serial_device));
  266. /* leave interrupt */
  267. rt_interrupt_leave();
  268. }
  269. #endif /* BSP_USING_UART0 */
  270. #if defined(BSP_USING_UART1)
  271. void UART1_Handler(void)
  272. {
  273. /* enter interrupt */
  274. rt_interrupt_enter();
  275. rt_hw_uart_isr(&(uart_obj[UART1_INDEX].serial_device));
  276. /* leave interrupt */
  277. rt_interrupt_leave();
  278. }
  279. #endif /* BSP_USING_UART1 */
  280. #if defined(BSP_USING_UART2)
  281. void UART2_Handler(void)
  282. {
  283. /* enter interrupt */
  284. rt_interrupt_enter();
  285. rt_hw_uart_isr(&(uart_obj[UART2_INDEX].serial_device));
  286. /* leave interrupt */
  287. rt_interrupt_leave();
  288. }
  289. #endif /* BSP_USING_UART2 */
  290. #if defined(BSP_USING_UART3)
  291. void UART3_Handler(void)
  292. {
  293. /* enter interrupt */
  294. rt_interrupt_enter();
  295. rt_hw_uart_isr(&(uart_obj[UART3_INDEX].serial_device));
  296. /* leave interrupt */
  297. rt_interrupt_leave();
  298. }
  299. #endif /* BSP_USING_UART3 */
  300. int swm_uart_init(void)
  301. {
  302. struct serial_configure serial_cfg = RT_SERIAL_CONFIG_DEFAULT;
  303. int i = 0;
  304. rt_err_t result = RT_EOK;
  305. #ifdef BSP_USING_UART0
  306. PORT_Init(PORTA, PIN2, FUNMUX0_UART0_RXD, 1);
  307. PORT_Init(PORTA, PIN3, FUNMUX1_UART0_TXD, 0);
  308. #endif
  309. #ifdef BSP_USING_UART1
  310. PORT_Init(PORTC, PIN2, FUNMUX0_UART1_RXD, 1);
  311. PORT_Init(PORTC, PIN3, FUNMUX1_UART1_TXD, 0);
  312. #endif
  313. #ifdef BSP_USING_UART2
  314. PORT_Init(PORTC, PIN4, FUNMUX0_UART2_RXD, 1);
  315. PORT_Init(PORTC, PIN5, FUNMUX1_UART2_TXD, 0);
  316. #endif
  317. #ifdef BSP_USING_UART3
  318. PORT_Init(PORTC, PIN6, FUNMUX0_UART3_RXD, 1);
  319. PORT_Init(PORTC, PIN7, FUNMUX1_UART3_TXD, 0);
  320. #endif
  321. for (i = 0; i < sizeof(swm_uart_cfg) / sizeof(swm_uart_cfg[0]); i++)
  322. {
  323. uart_obj[i].uart_cfg = &swm_uart_cfg[i];
  324. uart_obj[i].serial_device.ops = &swm_uart_ops;
  325. uart_obj[i].serial_device.config = serial_cfg;
  326. /* register UART device */
  327. result = rt_hw_serial_register(&uart_obj[i].serial_device, uart_obj[i].uart_cfg->name,
  328. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart_obj[i].uart_cfg);
  329. RT_ASSERT(result == RT_EOK);
  330. }
  331. return result;
  332. }
  333. #endif /* BSP_USING_UART */
  334. #endif /* RT_USING_SERIAL */