drv_usart.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright (c) 2006-2023, 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. static rt_ssize_t ch32dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  325. {
  326. return -RT_EIO;
  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. #if defined (SOC_RISCV_SERIES_CH32V2)
  348. void USART1_IRQHandler(void) __attribute__((interrupt()));
  349. #else
  350. void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  351. #endif
  352. void USART1_IRQHandler(void)
  353. {
  354. GET_INT_SP();
  355. rt_interrupt_enter();
  356. uart_isr(&(uart_obj[UART1_INDEX].serial));
  357. rt_interrupt_leave();
  358. FREE_INT_SP();
  359. }
  360. #endif
  361. #ifdef BSP_USING_UART2
  362. #if defined (SOC_RISCV_SERIES_CH32V2)
  363. void USART2_IRQHandler(void) __attribute__((interrupt()));
  364. #else
  365. void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  366. #endif
  367. void USART2_IRQHandler(void)
  368. {
  369. GET_INT_SP();
  370. rt_interrupt_enter();
  371. uart_isr(&(uart_obj[UART2_INDEX].serial));
  372. rt_interrupt_leave();
  373. FREE_INT_SP();
  374. }
  375. #endif
  376. #ifdef BSP_USING_UART3
  377. #if defined (SOC_RISCV_SERIES_CH32V2)
  378. void USART3_IRQHandler(void) __attribute__((interrupt()));
  379. #else
  380. void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  381. #endif
  382. void USART3_IRQHandler(void)
  383. {
  384. GET_INT_SP();
  385. rt_interrupt_enter();
  386. uart_isr(&(uart_obj[UART3_INDEX].serial));
  387. rt_interrupt_leave();
  388. FREE_INT_SP();
  389. }
  390. #endif
  391. #ifdef BSP_USING_UART4
  392. #if defined (SOC_RISCV_SERIES_CH32V2)
  393. void USART4_IRQHandler(void) __attribute__((interrupt()));
  394. #else
  395. void USART4_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  396. #endif
  397. void UART4_IRQHandler(void)
  398. {
  399. GET_INT_SP();
  400. rt_interrupt_enter();
  401. uart_isr(&(uart_obj[UART4_INDEX].serial));
  402. rt_interrupt_leave();
  403. FREE_INT_SP();
  404. }
  405. #endif
  406. #ifdef BSP_USING_UART5
  407. #if defined (SOC_RISCV_SERIES_CH32V2)
  408. void USART5_IRQHandler(void) __attribute__((interrupt()));
  409. #else
  410. void USART5_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  411. #endif
  412. void UART5_IRQHandler(void)
  413. {
  414. GET_INT_SP();
  415. rt_interrupt_enter();
  416. uart_isr(&(uart_obj[UART5_INDEX].serial));
  417. rt_interrupt_leave();
  418. FREE_INT_SP();
  419. }
  420. #endif
  421. #ifdef BSP_USING_UART6
  422. #if defined (SOC_RISCV_SERIES_CH32V2)
  423. void USART6_IRQHandler(void) __attribute__((interrupt()));
  424. #else
  425. void USART6_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  426. #endif
  427. void UART6_IRQHandler(void)
  428. {
  429. GET_INT_SP();
  430. rt_interrupt_enter();
  431. uart_isr(&(uart_obj[UART6_INDEX].serial));
  432. rt_interrupt_leave();
  433. FREE_INT_SP();
  434. }
  435. #endif
  436. #ifdef BSP_USING_UART7
  437. #if defined (SOC_RISCV_SERIES_CH32V2)
  438. void USART7_IRQHandler(void) __attribute__((interrupt()));
  439. #else
  440. void USART7_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  441. #endif
  442. void UART7_IRQHandler(void)
  443. {
  444. GET_INT_SP();
  445. rt_interrupt_enter();
  446. uart_isr(&(uart_obj[UART7_INDEX].serial));
  447. rt_interrupt_leave();
  448. FREE_INT_SP();
  449. }
  450. #endif
  451. #ifdef BSP_USING_UART8
  452. #if defined (SOC_RISCV_SERIES_CH32V2)
  453. void USART8_IRQHandler(void) __attribute__((interrupt()));
  454. #else
  455. void USART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
  456. #endif
  457. void UART8_IRQHandler(void)
  458. {
  459. GET_INT_SP();
  460. rt_interrupt_enter();
  461. uart_isr(&(uart_obj[UART8_INDEX].serial));
  462. rt_interrupt_leave();
  463. FREE_INT_SP();
  464. }
  465. #endif
  466. int rt_hw_usart_init(void)
  467. {
  468. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct ch32_uart);
  469. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  470. rt_err_t result = 0;
  471. for (int i = 0; i < obj_num; i++)
  472. {
  473. /* init UART object */
  474. uart_obj[i].config = &uart_config[i];
  475. uart_obj[i].hw_config = &uart_hw_config[i];
  476. uart_obj[i].serial.ops = &ch32_uart_ops;
  477. uart_obj[i].serial.config = config;
  478. /* Hardware initialization is required, otherwise it
  479. will not be registered into the device framework */
  480. if(uart_obj[i].hw_config->gpio_periph_clock == 0)
  481. {
  482. LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
  483. continue;
  484. }
  485. if(uart_obj[i].hw_config->uart_periph_clock == 0)
  486. {
  487. LOG_E("You did not perform hardware initialization for %s", uart_obj[i].config->name);
  488. continue;
  489. }
  490. /* register UART device */
  491. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  492. RT_DEVICE_FLAG_RDWR
  493. | RT_DEVICE_FLAG_INT_RX
  494. , &uart_obj[i]);
  495. RT_ASSERT(result == RT_EOK);
  496. }
  497. return result;
  498. }
  499. #endif /* RT_USING_SERIAL */