drv_usart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-01-05 Bernard the first version
  13. * 2010-03-29 Bernard remove interrupt Tx and DMA Rx mode
  14. * 2012-02-08 aozima update for F4.
  15. * 2012-07-28 aozima update for ART board.
  16. * 2016-05-28 armink add DMA Rx mode
  17. */
  18. #include <gd32f4xx.h>
  19. #include <drv_usart.h>
  20. #include <board.h>
  21. #include <rtdevice.h>
  22. #ifdef RT_USING_UART1
  23. #define USART1_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  24. #define USART1_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  25. /* Definition for USART1 Pins */
  26. #define USART1_TX_PIN GPIO_PIN_9
  27. #define USART1_TX_GPIO_PORT GPIOA
  28. #define USART1_TX_AF GPIO_AF7_USART1
  29. #define USART1_RX_PIN GPIO_PIN_10
  30. #define USART1_RX_GPIO_PORT GPIOA
  31. #define USART1_RX_AF GPIO_AF7_USART1
  32. #endif
  33. #ifdef RT_USING_UART2
  34. #define USART2_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  35. #define USART2_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  36. /* Definition for USART2 Pins */
  37. #define USART2_TX_PIN GPIO_PIN_2
  38. #define USART2_TX_GPIO_PORT GPIOA
  39. #define USART2_TX_AF GPIO_AF7_USART2
  40. #define USART2_RX_PIN GPIO_PIN_3
  41. #define USART2_RX_GPIO_PORT GPIOA
  42. #define USART2_RX_AF GPIO_AF7_USART2
  43. #endif
  44. #ifdef RT_USING_UART3
  45. #define USART3_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  46. #define USART3_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
  47. /* Definition for USART3 Pins */
  48. #define USART3_TX_PIN GPIO_PIN_10
  49. #define USART3_TX_GPIO_PORT GPIOB
  50. #define USART3_TX_AF GPIO_AF7_USART3
  51. #define USART3_RX_PIN GPIO_PIN_11
  52. #define USART3_RX_GPIO_PORT GPIOB
  53. #define USART3_RX_AF GPIO_AF7_USART3
  54. #endif
  55. #ifdef RT_USING_UART4
  56. #define USART4_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  57. #define USART4_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  58. /* Definition for USART4 Pins */
  59. #define USART4_TX_PIN GPIO_PIN_10
  60. #define USART4_TX_GPIO_PORT GPIOC
  61. #define USART4_TX_AF GPIO_AF7_USART4
  62. #define USART4_RX_PIN GPIO_PIN_11
  63. #define USART4_RX_GPIO_PORT GPIOC
  64. #define USART4_RX_AF GPIO_AF7_USART4
  65. #endif
  66. #ifdef RT_USING_UART5
  67. #define USART5_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
  68. #define USART5_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
  69. /* Definition for USART5 Pins */
  70. #define USART5_TX_PIN GPIO_PIN_12
  71. #define USART5_TX_GPIO_PORT GPIOC
  72. #define USART5_TX_AF GPIO_AF7_USART5
  73. #define USART5_RX_PIN GPIO_PIN_2
  74. #define USART5_RX_GPIO_PORT GPIOD
  75. #define USART5_RX_AF GPIO_AF7_USART5
  76. #endif
  77. /* GD32 uart driver */
  78. struct gd32_uart
  79. {
  80. UART_HandleTypeDef UartHandle;
  81. IRQn_Type irq;
  82. };
  83. static rt_err_t gd32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  84. {
  85. struct gd32_uart *uart;
  86. RT_ASSERT(serial != RT_NULL);
  87. RT_ASSERT(cfg != RT_NULL);
  88. uart = (struct gd32_uart *)serial->parent.user_data;
  89. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  90. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  91. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  92. uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  93. switch (cfg->data_bits)
  94. {
  95. case DATA_BITS_8:
  96. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  97. break;
  98. case DATA_BITS_9:
  99. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  100. break;
  101. default:
  102. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  103. break;
  104. }
  105. switch (cfg->stop_bits)
  106. {
  107. case STOP_BITS_1:
  108. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  109. break;
  110. case STOP_BITS_2:
  111. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  112. break;
  113. default:
  114. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  115. break;
  116. }
  117. switch (cfg->parity)
  118. {
  119. case PARITY_NONE:
  120. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  121. break;
  122. case PARITY_ODD:
  123. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  124. break;
  125. case PARITY_EVEN:
  126. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  127. break;
  128. default:
  129. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  130. break;
  131. }
  132. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  133. {
  134. return RT_ERROR;
  135. }
  136. return RT_EOK;
  137. }
  138. static rt_err_t gd32_control(struct rt_serial_device *serial, int cmd, void *arg)
  139. {
  140. struct gd32_uart *uart;
  141. RT_ASSERT(serial != RT_NULL);
  142. uart = (struct gd32_uart *)serial->parent.user_data;
  143. switch (cmd)
  144. {
  145. case RT_DEVICE_CTRL_CLR_INT:
  146. /* disable rx irq */
  147. NVIC_DisableIRQ(uart->irq);
  148. /* disable interrupt */
  149. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  150. break;
  151. case RT_DEVICE_CTRL_SET_INT:
  152. /* enable rx irq */
  153. NVIC_EnableIRQ(uart->irq);
  154. /* enable interrupt */
  155. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  156. break;
  157. }
  158. return RT_EOK;
  159. }
  160. static int gd32_putc(struct rt_serial_device *serial, char c)
  161. {
  162. struct gd32_uart *uart;
  163. RT_ASSERT(serial != RT_NULL);
  164. uart = (struct gd32_uart *)serial->parent.user_data;
  165. while((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
  166. uart->UartHandle.Instance->DR = c;
  167. return 1;
  168. }
  169. static int gd32_getc(struct rt_serial_device *serial)
  170. {
  171. int ch;
  172. struct gd32_uart *uart;
  173. RT_ASSERT(serial != RT_NULL);
  174. uart = (struct gd32_uart *)serial->parent.user_data;
  175. ch = -1;
  176. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  177. ch = uart->UartHandle.Instance->DR & 0xff;
  178. return ch;
  179. }
  180. /**
  181. * Uart common interrupt process. This need add to uart ISR.
  182. *
  183. * @param serial serial device
  184. */
  185. static void uart_isr(struct rt_serial_device *serial) {
  186. struct gd32_uart *uart = (struct gd32_uart *) serial->parent.user_data;
  187. RT_ASSERT(uart != RT_NULL);
  188. /* UART in mode Receiver -------------------------------------------------*/
  189. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  190. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  191. {
  192. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  193. /* Clear RXNE interrupt flag */
  194. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  195. }
  196. }
  197. /**
  198. * @brief UART MSP Initialization
  199. * This function configures the hardware resources used in this example:
  200. * - Peripheral's clock enable
  201. * - Peripheral's GPIO Configuration
  202. * - NVIC configuration for UART interrupt request enable
  203. * @param huart: UART handle pointer
  204. * @retval None
  205. */
  206. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  207. {
  208. GPIO_InitTypeDef GPIO_InitStruct;
  209. #if defined(RT_USING_UART1)
  210. if (huart->Instance == USART1)
  211. {
  212. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  213. /* Enable GPIO TX/RX clock */
  214. USART1_TX_GPIO_CLK_ENABLE();
  215. USART1_RX_GPIO_CLK_ENABLE();
  216. /* Enable USARTx clock */
  217. __HAL_RCC_USART1_CLK_ENABLE();
  218. /*##-2- Configure peripheral GPIO ##########################################*/
  219. /* UART TX GPIO pin configuration */
  220. GPIO_InitStruct.Pin = USART1_TX_PIN;
  221. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  222. GPIO_InitStruct.Pull = GPIO_PULLUP;
  223. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  224. GPIO_InitStruct.Alternate = USART1_TX_AF;
  225. HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
  226. /* UART RX GPIO pin configuration */
  227. GPIO_InitStruct.Pin = USART1_RX_PIN;
  228. GPIO_InitStruct.Alternate = USART1_RX_AF;
  229. HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
  230. HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
  231. HAL_NVIC_EnableIRQ(USART1_IRQn);
  232. }
  233. #endif
  234. #if defined(RT_USING_UART2)
  235. if (huart->Instance == USART2)
  236. {
  237. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  238. /* Enable GPIO TX/RX clock */
  239. USART2_TX_GPIO_CLK_ENABLE();
  240. USART2_RX_GPIO_CLK_ENABLE();
  241. /* Enable USARTx clock */
  242. __HAL_RCC_USART2_CLK_ENABLE();
  243. /*##-2- Configure peripheral GPIO ##########################################*/
  244. /* UART TX GPIO pin configuration */
  245. GPIO_InitStruct.Pin = USART2_TX_PIN;
  246. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  247. GPIO_InitStruct.Pull = GPIO_PULLUP;
  248. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  249. GPIO_InitStruct.Alternate = USART2_TX_AF;
  250. HAL_GPIO_Init(USART2_TX_GPIO_PORT, &GPIO_InitStruct);
  251. /* UART RX GPIO pin configuration */
  252. GPIO_InitStruct.Pin = USART2_RX_PIN;
  253. GPIO_InitStruct.Alternate = USART2_RX_AF;
  254. HAL_GPIO_Init(USART2_RX_GPIO_PORT, &GPIO_InitStruct);
  255. HAL_NVIC_SetPriority(USART2_IRQn, 0, 1);
  256. HAL_NVIC_EnableIRQ(USART2_IRQn);
  257. }
  258. #endif
  259. #if defined(RT_USING_UART3)
  260. if (huart->Instance == USART3)
  261. {
  262. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  263. /* Enable GPIO TX/RX clock */
  264. USART3_TX_GPIO_CLK_ENABLE();
  265. USART3_RX_GPIO_CLK_ENABLE();
  266. /* Enable USARTx clock */
  267. __HAL_RCC_USART3_CLK_ENABLE();
  268. /*##-2- Configure peripheral GPIO ##########################################*/
  269. /* UART TX GPIO pin configuration */
  270. GPIO_InitStruct.Pin = USART3_TX_PIN;
  271. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  272. GPIO_InitStruct.Pull = GPIO_PULLUP;
  273. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  274. GPIO_InitStruct.Alternate = USART3_TX_AF;
  275. HAL_GPIO_Init(USART3_TX_GPIO_PORT, &GPIO_InitStruct);
  276. /* UART RX GPIO pin configuration */
  277. GPIO_InitStruct.Pin = USART3_RX_PIN;
  278. GPIO_InitStruct.Alternate = USART3_RX_AF;
  279. HAL_GPIO_Init(USART3_RX_GPIO_PORT, &GPIO_InitStruct);
  280. HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
  281. HAL_NVIC_EnableIRQ(USART3_IRQn);
  282. }
  283. #endif
  284. #if defined(RT_USING_UART4)
  285. if (huart->Instance == USART4)
  286. {
  287. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  288. /* Enable GPIO TX/RX clock */
  289. USART4_TX_GPIO_CLK_ENABLE();
  290. USART4_RX_GPIO_CLK_ENABLE();
  291. /* Enable USARTx clock */
  292. __HAL_RCC_USART4_CLK_ENABLE();
  293. /*##-2- Configure peripheral GPIO ##########################################*/
  294. /* UART TX GPIO pin configuration */
  295. GPIO_InitStruct.Pin = USART4_TX_PIN;
  296. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  297. GPIO_InitStruct.Pull = GPIO_PULLUP;
  298. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  299. GPIO_InitStruct.Alternate = USART4_TX_AF;
  300. HAL_GPIO_Init(USART4_TX_GPIO_PORT, &GPIO_InitStruct);
  301. /* UART RX GPIO pin configuration */
  302. GPIO_InitStruct.Pin = USART4_RX_PIN;
  303. GPIO_InitStruct.Alternate = USART4_RX_AF;
  304. HAL_GPIO_Init(USART4_RX_GPIO_PORT, &GPIO_InitStruct);
  305. HAL_NVIC_SetPriority(USART4_IRQn, 0, 1);
  306. HAL_NVIC_EnableIRQ(USART4_IRQn);
  307. }
  308. #endif
  309. #if defined(RT_USING_UART5)
  310. if (huart->Instance == USART5)
  311. {
  312. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  313. /* Enable GPIO TX/RX clock */
  314. USART5_TX_GPIO_CLK_ENABLE();
  315. USART5_RX_GPIO_CLK_ENABLE();
  316. /* Enable USARTx clock */
  317. __HAL_RCC_USART5_CLK_ENABLE();
  318. /*##-2- Configure peripheral GPIO ##########################################*/
  319. /* UART TX GPIO pin configuration */
  320. GPIO_InitStruct.Pin = USART5_TX_PIN;
  321. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  322. GPIO_InitStruct.Pull = GPIO_PULLUP;
  323. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  324. GPIO_InitStruct.Alternate = USART5_TX_AF;
  325. HAL_GPIO_Init(USART5_TX_GPIO_PORT, &GPIO_InitStruct);
  326. /* UART RX GPIO pin configuration */
  327. GPIO_InitStruct.Pin = USART5_RX_PIN;
  328. GPIO_InitStruct.Alternate = USART5_RX_AF;
  329. HAL_GPIO_Init(USART5_RX_GPIO_PORT, &GPIO_InitStruct);
  330. HAL_NVIC_SetPriority(USART5_IRQn, 0, 1);
  331. HAL_NVIC_EnableIRQ(USART5_IRQn);
  332. }
  333. #endif
  334. }
  335. /**
  336. * @brief UART MSP De-Initialization
  337. * This function frees the hardware resources used in this example:
  338. * - Disable the Peripheral's clock
  339. * - Revert GPIO and NVIC configuration to their default state
  340. * @param huart: UART handle pointer
  341. * @retval None
  342. */
  343. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  344. {
  345. #if defined(RT_USING_UART1)
  346. if (huart->Instance == USART1)
  347. {
  348. /*##-1- Reset peripherals ##################################################*/
  349. __HAL_RCC_USART1_FORCE_RESET();
  350. __HAL_RCC_USART1_RELEASE_RESET();
  351. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  352. /* Configure UART Tx as alternate function */
  353. HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_PIN);
  354. /* Configure UART Rx as alternate function */
  355. HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_PIN);
  356. HAL_NVIC_DisableIRQ(USART1_IRQn);
  357. }
  358. #endif
  359. #if defined(RT_USING_UART2)
  360. if (huart->Instance == USART2)
  361. {
  362. /*##-1- Reset peripherals ##################################################*/
  363. __HAL_RCC_USART2_FORCE_RESET();
  364. __HAL_RCC_USART2_RELEASE_RESET();
  365. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  366. /* Configure UART Tx as alternate function */
  367. HAL_GPIO_DeInit(USART2_TX_GPIO_PORT, USART2_TX_PIN);
  368. /* Configure UART Rx as alternate function */
  369. HAL_GPIO_DeInit(USART2_RX_GPIO_PORT, USART2_RX_PIN);
  370. HAL_NVIC_DisableIRQ(USART2_IRQn);
  371. }
  372. #endif
  373. #if defined(RT_USING_UART3)
  374. if (huart->Instance == USART3)
  375. {
  376. /*##-1- Reset peripherals ##################################################*/
  377. __HAL_RCC_USART3_FORCE_RESET();
  378. __HAL_RCC_USART3_RELEASE_RESET();
  379. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  380. /* Configure UART Tx as alternate function */
  381. HAL_GPIO_DeInit(USART3_TX_GPIO_PORT, USART3_TX_PIN);
  382. /* Configure UART Rx as alternate function */
  383. HAL_GPIO_DeInit(USART3_RX_GPIO_PORT, USART3_RX_PIN);
  384. HAL_NVIC_DisableIRQ(USART3_IRQn);
  385. }
  386. #endif
  387. #if defined(RT_USING_UART4)
  388. if (huart->Instance == USART4)
  389. {
  390. /*##-1- Reset peripherals ##################################################*/
  391. __HAL_RCC_USART4_FORCE_RESET();
  392. __HAL_RCC_USART4_RELEASE_RESET();
  393. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  394. /* Configure UART Tx as alternate function */
  395. HAL_GPIO_DeInit(USART4_TX_GPIO_PORT, USART4_TX_PIN);
  396. /* Configure UART Rx as alternate function */
  397. HAL_GPIO_DeInit(USART4_RX_GPIO_PORT, USART4_RX_PIN);
  398. HAL_NVIC_DisableIRQ(USART4_IRQn);
  399. }
  400. #endif
  401. #if defined(RT_USING_UART5)
  402. if (huart->Instance == USART5)
  403. {
  404. /*##-1- Reset peripherals ##################################################*/
  405. __HAL_RCC_USART5_FORCE_RESET();
  406. __HAL_RCC_USART5_RELEASE_RESET();
  407. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  408. /* Configure UART Tx as alternate function */
  409. HAL_GPIO_DeInit(USART5_TX_GPIO_PORT, USART5_TX_PIN);
  410. /* Configure UART Rx as alternate function */
  411. HAL_GPIO_DeInit(USART5_RX_GPIO_PORT, USART5_RX_PIN);
  412. HAL_NVIC_DisableIRQ(USART5_IRQn);
  413. }
  414. #endif
  415. }
  416. static const struct rt_uart_ops gd32_uart_ops =
  417. {
  418. gd32_configure,
  419. gd32_control,
  420. gd32_putc,
  421. gd32_getc,
  422. };
  423. #if defined(RT_USING_UART1)
  424. /* UART1 device driver structure */
  425. struct gd32_uart uart1 =
  426. {
  427. {USART1},
  428. USART1_IRQn,
  429. };
  430. struct rt_serial_device serial1;
  431. void USART1_IRQHandler(void)
  432. {
  433. /* enter interrupt */
  434. rt_interrupt_enter();
  435. uart_isr(&serial1);
  436. /* leave interrupt */
  437. rt_interrupt_leave();
  438. }
  439. #endif /* RT_USING_UART1 */
  440. #if defined(RT_USING_UART2)
  441. /* UART2 device driver structure */
  442. struct gd32_uart uart2 =
  443. {
  444. {USART2},
  445. USART2_IRQn,
  446. };
  447. struct rt_serial_device serial2;
  448. void USART2_IRQHandler(void)
  449. {
  450. /* enter interrupt */
  451. rt_interrupt_enter();
  452. uart_isr(&serial2);
  453. /* leave interrupt */
  454. rt_interrupt_leave();
  455. }
  456. #endif /* RT_USING_UART2 */
  457. #if defined(RT_USING_UART3)
  458. /* UART3 device driver structure */
  459. struct gd32_uart uart3 =
  460. {
  461. {USART3},
  462. USART3_IRQn,
  463. };
  464. struct rt_serial_device serial3;
  465. void USART3_IRQHandler(void)
  466. {
  467. /* enter interrupt */
  468. rt_interrupt_enter();
  469. uart_isr(&serial3);
  470. /* leave interrupt */
  471. rt_interrupt_leave();
  472. }
  473. #endif /* RT_USING_UART3 */
  474. #if defined(RT_USING_UART4)
  475. /* UART4 device driver structure */
  476. struct gd32_uart uart4 =
  477. {
  478. {UART4},
  479. UART4_IRQn,
  480. };
  481. struct rt_serial_device serial4;
  482. void UART4_IRQHandler(void)
  483. {
  484. /* enter interrupt */
  485. rt_interrupt_enter();
  486. uart_isr(&serial4);
  487. /* leave interrupt */
  488. rt_interrupt_leave();
  489. }
  490. #endif /* RT_USING_UART4 */
  491. #if defined(RT_USING_UART5)
  492. /* UART5 device driver structure */
  493. struct gd32_uart uart5 =
  494. {
  495. {UART5},
  496. UART5_IRQn,
  497. };
  498. struct rt_serial_device serial5;
  499. void UART5_IRQHandler(void)
  500. {
  501. /* enter interrupt */
  502. rt_interrupt_enter();
  503. uart_isr(&serial5);
  504. /* leave interrupt */
  505. rt_interrupt_leave();
  506. }
  507. #endif /* RT_USING_UART5 */
  508. int gd32_hw_usart_init(void)
  509. {
  510. struct gd32_uart *uart;
  511. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  512. #ifdef RT_USING_UART1
  513. uart = &uart1;
  514. uart->UartHandle.Instance = USART1;
  515. serial1.ops = &gd32_uart_ops;
  516. serial1.config = config;
  517. /* register UART1 device */
  518. rt_hw_serial_register(&serial1,
  519. "uart1",
  520. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  521. uart);
  522. #endif /* RT_USING_UART1 */
  523. #ifdef RT_USING_UART2
  524. uart = &uart2;
  525. uart->UartHandle.Instance = USART2;
  526. serial2.ops = &gd32_uart_ops;
  527. serial2.config = config;
  528. /* register UART1 device */
  529. rt_hw_serial_register(&serial2,
  530. "uart2",
  531. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  532. uart);
  533. #endif /* RT_USING_UART2 */
  534. #ifdef RT_USING_UART3
  535. uart = &uart3;
  536. uart->UartHandle.Instance = USART3;
  537. serial3.ops = &gd32_uart_ops;
  538. serial3.config = config;
  539. /* register UART3 device */
  540. rt_hw_serial_register(&serial3,
  541. "uart3",
  542. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  543. uart);
  544. #endif /* RT_USING_UART3 */
  545. #ifdef RT_USING_UART4
  546. uart = &uart4;
  547. uart->UartHandle.Instance = USART4;
  548. serial4.ops = &gd32_uart_ops;
  549. serial4.config = config;
  550. /* register UART4 device */
  551. rt_hw_serial_register(&serial4,
  552. "uart4",
  553. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  554. uart);
  555. #endif /* RT_USING_UART4 */
  556. #ifdef RT_USING_UART5
  557. uart = &uart5;
  558. uart->UartHandle.Instance = USART5;
  559. serial5.ops = &gd32_uart_ops;
  560. serial5.config = config;
  561. /* register UART5 device */
  562. rt_hw_serial_register(&serial5,
  563. "uart5",
  564. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  565. uart);
  566. #endif /* RT_USING_UART5 */
  567. return 0;
  568. }
  569. INIT_BOARD_EXPORT(gd32_hw_usart_init);