drv_usart.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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-08-27 liYony the first version
  9. */
  10. #include "board.h"
  11. #include <rtdevice.h>
  12. #include <drv_usart.h>
  13. #ifdef RT_USING_SERIAL
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.uart"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
  18. !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
  19. #error "Please define at least one BSP_USING_UARTx"
  20. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  21. #endif
  22. enum
  23. {
  24. #ifdef BSP_USING_UART1
  25. UART1_INDEX,
  26. #endif
  27. #ifdef BSP_USING_UART2
  28. UART2_INDEX,
  29. #endif
  30. #ifdef BSP_USING_UART3
  31. UART3_INDEX,
  32. #endif
  33. #ifdef BSP_USING_UART4
  34. UART4_INDEX,
  35. #endif
  36. #ifdef BSP_USING_UART5
  37. UART5_INDEX,
  38. #endif
  39. #ifdef BSP_USING_UART6
  40. UART6_INDEX,
  41. #endif
  42. #ifdef BSP_USING_UART7
  43. UART7_INDEX,
  44. #endif
  45. #ifdef BSP_USING_UART8
  46. UART8_INDEX,
  47. #endif
  48. };
  49. /* If you want to use other serial ports, please follow UART1 to complete other
  50. serial ports. For clock configuration, */
  51. static struct ch32_uart_hw_config uart_hw_config[] =
  52. {
  53. #ifdef BSP_USING_UART1
  54. {
  55. /* clock configuration, please refer to ch32v30x_rcc.h */
  56. RCC_APB2Periph_USART1, RCC_APB2Periph_GPIOA,
  57. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  58. GPIOA, GPIO_Pin_9, /* Tx */GPIOA, GPIO_Pin_10, /* Rx */
  59. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  60. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  61. GPIO_Remap_NONE,
  62. },
  63. #endif
  64. #ifdef BSP_USING_UART2
  65. {
  66. /* clock configuration, please refer to ch32v30x_rcc.h */
  67. RCC_APB1Periph_USART2, RCC_APB2Periph_GPIOA,
  68. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  69. GPIOA, GPIO_Pin_2, /* Tx */GPIOA, GPIO_Pin_3, /* Rx */
  70. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  71. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  72. GPIO_Remap_NONE,
  73. },
  74. #endif
  75. #ifdef BSP_USING_UART3
  76. {
  77. /* clock configuration, please refer to ch32v30x_rcc.h */
  78. RCC_APB1Periph_USART3, RCC_APB2Periph_GPIOB,
  79. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  80. GPIOB, GPIO_Pin_10, /* Tx */GPIOB, GPIO_Pin_11, /* Rx */
  81. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  82. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  83. GPIO_Remap_NONE,
  84. },
  85. #endif
  86. #ifdef BSP_USING_UART4
  87. {
  88. /* clock configuration, please refer to ch32v30x_rcc.h */
  89. RCC_APB1Periph_UART4, RCC_APB2Periph_GPIOC,
  90. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  91. GPIOC, GPIO_Pin_10, /* Tx */GPIOC, GPIO_Pin_11, /* Rx */
  92. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  93. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  94. GPIO_Remap_NONE,
  95. },
  96. #endif
  97. #ifdef BSP_USING_UART5
  98. {
  99. /* clock configuration, please refer to ch32v30x_rcc.h */
  100. RCC_APB1Periph_UART5, RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,
  101. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  102. GPIOC, GPIO_Pin_12, /* Tx */GPIOD, GPIO_Pin_2, /* Rx */
  103. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  104. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  105. GPIO_Remap_NONE,
  106. },
  107. #endif
  108. #ifdef BSP_USING_UART6
  109. {
  110. /* clock configuration, please refer to ch32v30x_rcc.h */
  111. RCC_APB1Periph_UART6, RCC_APB2Periph_GPIOC,
  112. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  113. GPIOC, GPIO_Pin_0, /* Tx */GPIOC, GPIO_Pin_1, /* Rx */
  114. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  115. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  116. GPIO_Remap_NONE,
  117. },
  118. #endif
  119. #ifdef BSP_USING_UART7
  120. {
  121. /* clock configuration, please refer to ch32v30x_rcc.h */
  122. RCC_APB1Periph_UART7, RCC_APB2Periph_GPIOC,
  123. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  124. GPIOC, GPIO_Pin_2, /* Tx */GPIOC, GPIO_Pin_3, /* Rx */
  125. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  126. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  127. GPIO_Remap_NONE,
  128. },
  129. #endif
  130. #ifdef BSP_USING_UART8
  131. {
  132. /* clock configuration, please refer to ch32v30x_rcc.h */
  133. RCC_APB1Periph_UART8, RCC_APB2Periph_GPIOC,
  134. /* GPIO configuration : TX_Port,TX_Pin, RX_Port,RX_Pin */
  135. GPIOC, GPIO_Pin_4, /* Tx */GPIOC, GPIO_Pin_5, /* Rx */
  136. /* Whether to enable port remapping, you can refer to ch32v30x_gpio.h file,
  137. for example, USART1 needs to be turned on, you can use GPIO_Remap_USART1 */
  138. GPIO_Remap_NONE,
  139. },
  140. #endif
  141. };
  142. static struct ch32_uart_config uart_config[] =
  143. {
  144. #ifdef BSP_USING_UART1
  145. {
  146. "uart1",
  147. USART1,
  148. USART1_IRQn,
  149. },
  150. #endif
  151. #ifdef BSP_USING_UART2
  152. {
  153. "uart2",
  154. USART2,
  155. USART2_IRQn,
  156. },
  157. #endif
  158. #ifdef BSP_USING_UART3
  159. {
  160. "uart3",
  161. USART3,
  162. USART3_IRQn,
  163. },
  164. #endif
  165. #ifdef BSP_USING_UART4
  166. {
  167. "uart4",
  168. UART4,
  169. UART4_IRQn,
  170. },
  171. #endif
  172. #ifdef BSP_USING_UART5
  173. {
  174. "uart5",
  175. UART5,
  176. UART5_IRQn,
  177. },
  178. #endif
  179. #ifdef BSP_USING_UART6
  180. {
  181. "uart6",
  182. UART6,
  183. UART6_IRQn,
  184. },
  185. #endif
  186. #ifdef BSP_USING_UART7
  187. {
  188. "uart7",
  189. UART7,
  190. UART7_IRQn,
  191. },
  192. #endif
  193. #ifdef BSP_USING_UART8
  194. {
  195. "uart8",
  196. UART8,
  197. UART8_IRQn,
  198. },
  199. #endif
  200. };
  201. static struct ch32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  202. static rt_err_t ch32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  203. {
  204. struct ch32_uart *uart;
  205. GPIO_InitTypeDef GPIO_InitStructure={0};
  206. RT_ASSERT(serial != RT_NULL);
  207. RT_ASSERT(cfg != RT_NULL);
  208. uart = (struct ch32_uart *) serial->parent.user_data;
  209. uart->Init.USART_BaudRate = cfg->baud_rate;
  210. uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  211. uart->Init.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
  212. switch (cfg->data_bits)
  213. {
  214. case DATA_BITS_8:
  215. uart->Init.USART_WordLength = USART_WordLength_8b;
  216. break;
  217. case DATA_BITS_9:
  218. uart->Init.USART_WordLength = USART_WordLength_9b;
  219. break;
  220. default:
  221. uart->Init.USART_WordLength = USART_WordLength_8b;
  222. break;
  223. }
  224. switch (cfg->stop_bits)
  225. {
  226. case STOP_BITS_1:
  227. uart->Init.USART_StopBits = USART_StopBits_1;
  228. break;
  229. case STOP_BITS_2:
  230. uart->Init.USART_StopBits = USART_StopBits_2;
  231. break;
  232. default:
  233. uart->Init.USART_StopBits = USART_StopBits_1;
  234. break;
  235. }
  236. switch (cfg->parity)
  237. {
  238. case PARITY_NONE:
  239. uart->Init.USART_Parity = USART_Parity_No;
  240. break;
  241. case PARITY_ODD:
  242. uart->Init.USART_Parity = USART_Parity_Odd;
  243. break;
  244. case PARITY_EVEN:
  245. uart->Init.USART_Parity = USART_Parity_Even;
  246. break;
  247. default:
  248. uart->Init.USART_Parity = USART_Parity_No;
  249. break;
  250. }
  251. /* UART hardware configuration, including clock and GPIO, etc. */
  252. RCC_APB2PeriphClockCmd(uart->hw_config->gpio_periph_clock, ENABLE);
  253. if(uart->config->Instance == USART1)
  254. {
  255. RCC_APB2PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
  256. }
  257. else
  258. {
  259. RCC_APB1PeriphClockCmd(uart->hw_config->uart_periph_clock, ENABLE);
  260. }
  261. if(uart->hw_config->remap != GPIO_Remap_NONE)
  262. {
  263. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  264. GPIO_PinRemapConfig(uart->hw_config->remap, ENABLE);
  265. }
  266. GPIO_InitStructure.GPIO_Pin = uart->hw_config->tx_gpio_pin;
  267. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  268. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  269. GPIO_Init(uart->hw_config->tx_gpio_port, &GPIO_InitStructure);
  270. GPIO_InitStructure.GPIO_Pin = uart->hw_config->rx_gpio_pin;
  271. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  272. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  273. GPIO_Init(uart->hw_config->rx_gpio_port, &GPIO_InitStructure);
  274. USART_Init(uart->config->Instance,&uart->Init);
  275. USART_Cmd(uart->config->Instance, ENABLE);
  276. return RT_EOK;
  277. }
  278. static rt_err_t ch32_control(struct rt_serial_device *serial, int cmd, void *arg)
  279. {
  280. struct ch32_uart *uart;
  281. RT_ASSERT(serial != RT_NULL);
  282. uart = (struct ch32_uart *)serial->parent.user_data;
  283. switch (cmd)
  284. {
  285. /* disable interrupt */
  286. case RT_DEVICE_CTRL_CLR_INT:
  287. /* disable rx irq */
  288. NVIC_DisableIRQ(uart->config->irq_type);
  289. /* disable interrupt */
  290. USART_ITConfig(uart->config->Instance,USART_IT_RXNE,DISABLE);
  291. break;
  292. /* enable interrupt */
  293. case RT_DEVICE_CTRL_SET_INT:
  294. /* enable rx irq */
  295. NVIC_EnableIRQ(uart->config->irq_type);
  296. /* enable interrupt */
  297. USART_ITConfig(uart->config->Instance, USART_IT_RXNE,ENABLE);
  298. break;
  299. }
  300. return RT_EOK;
  301. }
  302. static int ch32_putc(struct rt_serial_device *serial, char c)
  303. {
  304. struct ch32_uart *uart;
  305. RT_ASSERT(serial != RT_NULL);
  306. uart = (struct ch32_uart *)serial->parent.user_data;
  307. while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) == RESET);
  308. uart->config->Instance->DATAR = c;
  309. return 1;
  310. }
  311. static int ch32_getc(struct rt_serial_device *serial)
  312. {
  313. int ch;
  314. struct ch32_uart *uart;
  315. RT_ASSERT(serial != RT_NULL);
  316. uart = (struct ch32_uart *)serial->parent.user_data;
  317. ch = -1;
  318. if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE) != RESET)
  319. {
  320. ch = uart->config->Instance->DATAR & 0xff;
  321. }
  322. return ch;
  323. }
  324. rt_size_t ch32dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  325. {
  326. return RT_EOK;
  327. }
  328. static void uart_isr(struct rt_serial_device *serial)
  329. {
  330. struct ch32_uart *uart = (struct ch32_uart *) serial->parent.user_data;
  331. RT_ASSERT(uart != RT_NULL);
  332. if (USART_GetITStatus(uart->config->Instance, USART_IT_RXNE) != RESET)
  333. {
  334. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  335. USART_ClearITPendingBit(uart->config->Instance, USART_IT_RXNE);
  336. }
  337. }
  338. static const struct rt_uart_ops ch32_uart_ops =
  339. {
  340. ch32_configure,
  341. ch32_control,
  342. ch32_putc,
  343. ch32_getc,
  344. ch32dma_transmit
  345. };
  346. #ifdef BSP_USING_UART1
  347. void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  348. void USART1_IRQHandler(void)
  349. {
  350. GET_INT_SP();
  351. rt_interrupt_enter();
  352. uart_isr(&(uart_obj[UART1_INDEX].serial));
  353. rt_interrupt_leave();
  354. FREE_INT_SP();
  355. }
  356. #endif
  357. #ifdef BSP_USING_UART2
  358. void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  359. void USART2_IRQHandler(void)
  360. {
  361. GET_INT_SP();
  362. rt_interrupt_enter();
  363. uart_isr(&(uart_obj[UART2_INDEX].serial));
  364. rt_interrupt_leave();
  365. FREE_INT_SP();
  366. }
  367. #endif
  368. #ifdef BSP_USING_UART3
  369. void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  370. void USART3_IRQHandler(void)
  371. {
  372. GET_INT_SP();
  373. rt_interrupt_enter();
  374. uart_isr(&(uart_obj[UART3_INDEX].serial));
  375. rt_interrupt_leave();
  376. FREE_INT_SP();
  377. }
  378. #endif
  379. #ifdef BSP_USING_UART4
  380. void UART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  381. void UART4_IRQHandler(void)
  382. {
  383. GET_INT_SP();
  384. rt_interrupt_enter();
  385. uart_isr(&(uart_obj[UART4_INDEX].serial));
  386. rt_interrupt_leave();
  387. FREE_INT_SP();
  388. }
  389. #endif
  390. #ifdef BSP_USING_UART5
  391. void UART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  392. void UART5_IRQHandler(void)
  393. {
  394. GET_INT_SP();
  395. rt_interrupt_enter();
  396. uart_isr(&(uart_obj[UART5_INDEX].serial));
  397. rt_interrupt_leave();
  398. FREE_INT_SP();
  399. }
  400. #endif
  401. #ifdef BSP_USING_UART6
  402. void UART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  403. void UART6_IRQHandler(void)
  404. {
  405. GET_INT_SP();
  406. rt_interrupt_enter();
  407. uart_isr(&(uart_obj[UART6_INDEX].serial));
  408. rt_interrupt_leave();
  409. FREE_INT_SP();
  410. }
  411. #endif
  412. #ifdef BSP_USING_UART7
  413. void UART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  414. void UART7_IRQHandler(void)
  415. {
  416. GET_INT_SP();
  417. rt_interrupt_enter();
  418. uart_isr(&(uart_obj[UART7_INDEX].serial));
  419. rt_interrupt_leave();
  420. FREE_INT_SP();
  421. }
  422. #endif
  423. #ifdef BSP_USING_UART8
  424. void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  425. void UART8_IRQHandler(void)
  426. {
  427. GET_INT_SP();
  428. rt_interrupt_enter();
  429. uart_isr(&(uart_obj[UART8_INDEX].serial));
  430. rt_interrupt_leave();
  431. FREE_INT_SP();
  432. }
  433. #endif
  434. int rt_hw_usart_init(void)
  435. {
  436. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ch32_uart);
  437. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  438. rt_err_t result = 0;
  439. for (int i = 0; i < obj_num; i++)
  440. {
  441. /* init UART object */
  442. uart_obj[i].config = &uart_config[i];
  443. uart_obj[i].hw_config = &uart_hw_config[i];
  444. uart_obj[i].serial.ops = &ch32_uart_ops;
  445. uart_obj[i].serial.config = config;
  446. /* Hardware initialization is required, otherwise it
  447. will not be registered into the device framework */
  448. if(uart_obj[i].hw_config->gpio_periph_clock == 0)
  449. {
  450. LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
  451. continue;
  452. }
  453. if(uart_obj[i].hw_config->uart_periph_clock == 0)
  454. {
  455. LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
  456. continue;
  457. }
  458. /* register UART device */
  459. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  460. RT_DEVICE_FLAG_RDWR
  461. | RT_DEVICE_FLAG_INT_RX
  462. , &uart_obj[i]);
  463. RT_ASSERT(result == RT_EOK);
  464. }
  465. return result;
  466. }
  467. #endif /* RT_USING_SERIAL */