drv_uart_ch32f20x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-21 charlown first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include "ch32f20x_usart.h"
  15. #include "ch32f20x_misc.h"
  16. #ifdef BSP_USING_UART
  17. #ifndef ITEM_NUM
  18. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  19. #endif
  20. struct usart_device
  21. {
  22. struct rt_serial_device parent;
  23. char *name;
  24. USART_TypeDef *periph;
  25. IRQn_Type irqn;
  26. };
  27. #ifdef BSP_USING_UART1
  28. static struct usart_device usart_device1 =
  29. {
  30. .name = "uart1",
  31. .periph = USART1,
  32. .irqn = USART1_IRQn,
  33. };
  34. #endif
  35. #ifdef BSP_USING_UART2
  36. static struct usart_device usart_device2 =
  37. {
  38. .name = "uart2",
  39. .periph = USART2,
  40. .irqn = USART2_IRQn,
  41. };
  42. #endif
  43. #ifdef BSP_USING_UART3
  44. static struct usart_device usart_device3 =
  45. {
  46. .name = "uart3",
  47. .periph = USART3,
  48. .irqn = USART3_IRQn,
  49. };
  50. #endif
  51. #ifdef BSP_USING_UART4
  52. static struct usart_device usart_device4 =
  53. {
  54. .name = "uart4",
  55. .periph = UART4,
  56. .irqn = UART4_IRQn,
  57. };
  58. #endif
  59. #ifdef BSP_USING_UART5
  60. static struct usart_device usart_device5 =
  61. {
  62. .name = "uart5",
  63. .periph = UART5,
  64. .irqn = UART5_IRQn,
  65. };
  66. #endif
  67. #ifdef BSP_USING_UART6
  68. static struct usart_device usart_device6 =
  69. {
  70. .name = "uart6",
  71. .periph = UART6,
  72. .irqn = UART6_IRQn,
  73. };
  74. #endif
  75. #ifdef BSP_USING_UART7
  76. static struct usart_device usart_device7 =
  77. {
  78. .name = "uart7",
  79. .periph = UART7,
  80. .irqn = UART7_IRQn,
  81. };
  82. #endif
  83. #ifdef BSP_USING_UART8
  84. static struct usart_device usart_device8 =
  85. {
  86. .name = "uart8",
  87. .periph = UART8,
  88. .irqn = UART8_IRQn,
  89. };
  90. #endif
  91. static rt_err_t ch32f2_usart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  92. {
  93. struct usart_device *usart_dev;
  94. USART_InitTypeDef USART_InitStructure;
  95. RT_ASSERT(serial != RT_NULL);
  96. RT_ASSERT(cfg != RT_NULL);
  97. usart_dev = (struct usart_device *)serial;
  98. RT_ASSERT(usart_dev != RT_NULL);
  99. ch32f2_usart_clock_and_io_init(usart_dev->periph);
  100. USART_StructInit(&USART_InitStructure);
  101. USART_InitStructure.USART_BaudRate = cfg->baud_rate;
  102. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  103. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  104. switch (cfg->data_bits)
  105. {
  106. case DATA_BITS_8:
  107. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  108. break;
  109. case DATA_BITS_9:
  110. USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  111. break;
  112. default:
  113. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  114. break;
  115. }
  116. switch (cfg->stop_bits)
  117. {
  118. case STOP_BITS_1:
  119. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  120. break;
  121. case STOP_BITS_2:
  122. USART_InitStructure.USART_StopBits = USART_StopBits_2;
  123. break;
  124. default:
  125. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  126. break;
  127. }
  128. switch (cfg->parity)
  129. {
  130. case PARITY_NONE:
  131. USART_InitStructure.USART_Parity = USART_Parity_No;
  132. break;
  133. case PARITY_ODD:
  134. USART_InitStructure.USART_Parity = USART_Parity_Odd;
  135. break;
  136. case PARITY_EVEN:
  137. USART_InitStructure.USART_Parity = USART_Parity_Even;
  138. break;
  139. default:
  140. USART_InitStructure.USART_Parity = USART_Parity_No;
  141. break;
  142. }
  143. USART_Init(usart_dev->periph, &USART_InitStructure);
  144. USART_Cmd(usart_dev->periph, ENABLE);
  145. return RT_EOK;
  146. }
  147. static rt_err_t ch32f2_usart_control(struct rt_serial_device *serial, int cmd, void *arg)
  148. {
  149. struct usart_device *usart_dev;
  150. NVIC_InitTypeDef NVIC_InitStruct;
  151. RT_ASSERT(serial != RT_NULL);
  152. usart_dev = (struct usart_device *)serial;
  153. RT_ASSERT(usart_dev != RT_NULL);
  154. NVIC_InitStruct.NVIC_IRQChannel = usart_dev->irqn;
  155. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
  156. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  157. switch (cmd)
  158. {
  159. case RT_DEVICE_CTRL_CLR_INT:
  160. NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE;
  161. NVIC_Init(&NVIC_InitStruct);
  162. USART_ITConfig(usart_dev->periph, USART_IT_RXNE, DISABLE);
  163. break;
  164. case RT_DEVICE_CTRL_SET_INT:
  165. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  166. NVIC_Init(&NVIC_InitStruct);
  167. USART_ITConfig(usart_dev->periph, USART_IT_RXNE, ENABLE);
  168. break;
  169. }
  170. return RT_EOK;
  171. }
  172. static int ch32f2_usart_putc(struct rt_serial_device *serial, char ch)
  173. {
  174. struct usart_device *usart_dev;
  175. RT_ASSERT(serial != RT_NULL);
  176. usart_dev = (struct usart_device *)serial;
  177. RT_ASSERT(usart_dev != RT_NULL);
  178. USART_SendData(usart_dev->periph, (uint8_t)ch);
  179. while (USART_GetFlagStatus(usart_dev->periph, USART_FLAG_TXE) == RESET)
  180. ;
  181. return 1;
  182. }
  183. static int ch32f2_usart_getc(struct rt_serial_device *serial)
  184. {
  185. struct usart_device *usart_dev;
  186. int ch;
  187. RT_ASSERT(serial != RT_NULL);
  188. usart_dev = (struct usart_device *)serial;
  189. RT_ASSERT(usart_dev != RT_NULL);
  190. ch = -1;
  191. if (RESET != USART_GetFlagStatus(usart_dev->periph, USART_FLAG_RXNE))
  192. {
  193. ch = USART_ReceiveData(usart_dev->periph) & 0xff;
  194. }
  195. return ch;
  196. }
  197. static const struct rt_uart_ops usart_ops = {
  198. .configure = ch32f2_usart_configure,
  199. .control = ch32f2_usart_control,
  200. .putc = ch32f2_usart_putc,
  201. .getc = ch32f2_usart_getc,
  202. .dma_transmit = RT_NULL};
  203. static void usart_isr(struct usart_device *usart_dev)
  204. {
  205. RT_ASSERT(usart_dev != RT_NULL);
  206. if ((USART_GetITStatus(usart_dev->periph, USART_IT_RXNE) != RESET) && (RESET != USART_GetFlagStatus(usart_dev->periph, USART_FLAG_RXNE)))
  207. {
  208. rt_hw_serial_isr(&usart_dev->parent, RT_SERIAL_EVENT_RX_IND);
  209. USART_ClearITPendingBit(usart_dev->periph, USART_IT_RXNE);
  210. USART_ClearFlag(usart_dev->periph, USART_FLAG_RXNE);
  211. }
  212. else
  213. {
  214. if (USART_GetFlagStatus(usart_dev->periph, USART_FLAG_CTS) != RESET)
  215. {
  216. USART_ClearFlag(usart_dev->periph, USART_FLAG_CTS);
  217. }
  218. if (USART_GetFlagStatus(usart_dev->periph, USART_FLAG_LBD) != RESET)
  219. {
  220. USART_ClearFlag(usart_dev->periph, USART_FLAG_LBD);
  221. }
  222. if (USART_GetFlagStatus(usart_dev->periph, USART_FLAG_TC) != RESET)
  223. {
  224. USART_ClearFlag(usart_dev->periph, USART_FLAG_TC);
  225. }
  226. }
  227. }
  228. #ifdef BSP_USING_UART1
  229. void USART1_IRQHandler(void)
  230. {
  231. rt_interrupt_enter();
  232. usart_isr(&usart_device1);
  233. rt_interrupt_leave();
  234. }
  235. #endif
  236. #ifdef BSP_USING_UART2
  237. void USART2_IRQHandler(void)
  238. {
  239. rt_interrupt_enter();
  240. usart_isr(&usart_device2);
  241. rt_interrupt_leave();
  242. }
  243. #endif
  244. #ifdef BSP_USING_UART3
  245. void USART3_IRQHandler(void)
  246. {
  247. rt_interrupt_enter();
  248. usart_isr(&usart_device3);
  249. rt_interrupt_leave();
  250. }
  251. #endif
  252. #ifdef BSP_USING_UART4
  253. void USART4_IRQHandler(void)
  254. {
  255. rt_interrupt_enter();
  256. usart_isr(&usart_device4);
  257. rt_interrupt_leave();
  258. }
  259. #endif
  260. #ifdef BSP_USING_UART5
  261. void USART5_IRQHandler(void)
  262. {
  263. rt_interrupt_enter();
  264. usart_isr(&usart_device5);
  265. rt_interrupt_leave();
  266. }
  267. #endif
  268. #ifdef BSP_USING_UART6
  269. void USART6_IRQHandler(void)
  270. {
  271. rt_interrupt_enter();
  272. usart_isr(&usart_device6);
  273. rt_interrupt_leave();
  274. }
  275. #endif
  276. #ifdef BSP_USING_UART7
  277. void USART7_IRQHandler(void)
  278. {
  279. rt_interrupt_enter();
  280. usart_isr(&usart_device7);
  281. rt_interrupt_leave();
  282. }
  283. #endif
  284. #ifdef BSP_USING_UART8
  285. void USART8_IRQHandler(void)
  286. {
  287. rt_interrupt_enter();
  288. usart_isr(&usart_device8);
  289. rt_interrupt_leave();
  290. }
  291. #endif
  292. int rt_hw_uart_init(void)
  293. {
  294. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  295. #ifdef BSP_USING_UART1
  296. usart_device1.parent.ops = &usart_ops;
  297. usart_device1.parent.config = config;
  298. rt_hw_serial_register(&usart_device1.parent, usart_device1.name,
  299. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  300. RT_NULL);
  301. #endif
  302. #ifdef BSP_USING_UART2
  303. usart_device2.parent.ops = &usart_ops;
  304. usart_device2.parent.config = config;
  305. rt_hw_serial_register(&usart_device2.parent, usart_device2.name,
  306. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  307. RT_NULL);
  308. #endif
  309. #ifdef BSP_USING_UART3
  310. usart_device3.parent.ops = &usart_ops;
  311. usart_device3.parent.config = config;
  312. rt_hw_serial_register(&usart_device3.parent, usart_device3.name,
  313. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  314. RT_NULL);
  315. #endif
  316. #ifdef BSP_USING_UART4
  317. usart_device4.parent.ops = &usart_ops;
  318. usart_device4.parent.config = config;
  319. rt_hw_serial_register(&usart_device4.parent, usart_device4.name,
  320. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  321. RT_NULL);
  322. #endif
  323. #ifdef BSP_USING_UART5
  324. usart_device5.parent.ops = &usart_ops;
  325. usart_device5.parent.config = config;
  326. rt_hw_serial_register(&usart_device5.parent, usart_device5.name,
  327. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  328. RT_NULL);
  329. #endif
  330. #ifdef BSP_USING_UART6
  331. usart_device6.parent.ops = &usart_ops;
  332. usart_device6.parent.config = config;
  333. rt_hw_serial_register(&usart_device6.parent, usart_device6.name,
  334. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  335. RT_NULL);
  336. #endif
  337. #ifdef BSP_USING_UART7
  338. usart_device7.parent.ops = &usart_ops;
  339. usart_device7.parent.config = config;
  340. rt_hw_serial_register(&usart_device7.parent, usart_device7.name,
  341. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  342. RT_NULL);
  343. #endif
  344. #ifdef BSP_USING_UART8
  345. usart_device8.parent.ops = &usart_ops;
  346. usart_device8.parent.config = config;
  347. rt_hw_serial_register(&usart_device8.parent, usart_device8.name,
  348. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX,
  349. RT_NULL);
  350. #endif
  351. return RT_EOK;
  352. }
  353. #endif /* BSP_USING_UART */