drv_uart.c 10 KB

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