drv_usart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * File : drv_usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-01-05 Bernard the first version
  23. * 2015-08-01 xiaonong the first version for stm32f7xx
  24. * 2016-01-15 ArdaFu the first version for stm32f4xx with STM32 HAL
  25. * 2016-01-15 zyh the first version for stm32f401rc with STM32 HAL
  26. */
  27. #include "drv_usart.h"
  28. #include "board.h"
  29. #include <rtdevice.h>
  30. #include <rthw.h>
  31. #include <rtthread.h>
  32. /* STM32 uart driver */
  33. struct drv_uart
  34. {
  35. UART_HandleTypeDef UartHandle;
  36. IRQn_Type irq;
  37. };
  38. static rt_err_t drv_configure(struct rt_serial_device *serial,
  39. struct serial_configure *cfg)
  40. {
  41. struct drv_uart *uart;
  42. RT_ASSERT(serial != RT_NULL);
  43. RT_ASSERT(cfg != RT_NULL);
  44. uart = (struct drv_uart *)serial->parent.user_data;
  45. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  46. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  47. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  48. uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  49. uart->UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  50. switch (cfg->data_bits)
  51. {
  52. case DATA_BITS_8:
  53. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  54. break;
  55. case DATA_BITS_9:
  56. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  57. break;
  58. default:
  59. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  60. break;
  61. }
  62. switch (cfg->stop_bits)
  63. {
  64. case STOP_BITS_1:
  65. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  66. break;
  67. case STOP_BITS_2:
  68. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  69. break;
  70. default:
  71. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  72. break;
  73. }
  74. switch (cfg->parity)
  75. {
  76. case PARITY_NONE:
  77. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  78. break;
  79. case PARITY_ODD:
  80. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  81. break;
  82. case PARITY_EVEN:
  83. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  84. break;
  85. default:
  86. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  87. break;
  88. }
  89. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  90. {
  91. return RT_ERROR;
  92. }
  93. return RT_EOK;
  94. }
  95. static rt_err_t drv_control(struct rt_serial_device *serial,
  96. int cmd, void *arg)
  97. {
  98. struct drv_uart *uart;
  99. RT_ASSERT(serial != RT_NULL);
  100. uart = (struct drv_uart *)serial->parent.user_data;
  101. switch (cmd)
  102. {
  103. case RT_DEVICE_CTRL_CLR_INT:
  104. /* disable rx irq */
  105. NVIC_DisableIRQ(uart->irq);
  106. /* disable interrupt */
  107. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  108. break;
  109. case RT_DEVICE_CTRL_SET_INT:
  110. /* enable rx irq */
  111. HAL_NVIC_SetPriority(uart->irq, 5, 0);
  112. NVIC_EnableIRQ(uart->irq);
  113. /* enable interrupt */
  114. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  115. break;
  116. }
  117. return RT_EOK;
  118. }
  119. static int drv_putc(struct rt_serial_device *serial, char c)
  120. {
  121. struct drv_uart *uart;
  122. RT_ASSERT(serial != RT_NULL);
  123. uart = (struct drv_uart *)serial->parent.user_data;
  124. __HAL_UART_CLEAR_FLAG(&(uart->UartHandle), UART_FLAG_TC);
  125. uart->UartHandle.Instance->TDR = c;
  126. while (__HAL_UART_GET_FLAG(&(uart->UartHandle), UART_FLAG_TC) == RESET);
  127. return 1;
  128. }
  129. static int drv_getc(struct rt_serial_device *serial)
  130. {
  131. int ch;
  132. struct drv_uart *uart;
  133. RT_ASSERT(serial != RT_NULL);
  134. uart = (struct drv_uart *)serial->parent.user_data;
  135. ch = -1;
  136. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  137. ch = uart->UartHandle.Instance->RDR & 0xff;
  138. return ch;
  139. }
  140. static const struct rt_uart_ops drv_uart_ops =
  141. {
  142. drv_configure,
  143. drv_control,
  144. drv_putc,
  145. drv_getc,
  146. };
  147. #if defined(BSP_USING_UART1)
  148. /* UART1 device driver structure */
  149. static struct drv_uart uart1;
  150. struct rt_serial_device serial1;
  151. void USART1_IRQHandler(void)
  152. {
  153. struct drv_uart *uart;
  154. uart = &uart1;
  155. /* enter interrupt */
  156. rt_interrupt_enter();
  157. /* UART in mode Receiver -------------------------------------------------*/
  158. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  159. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  160. {
  161. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  162. /* Clear RXNE interrupt flag */
  163. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  164. }
  165. /* leave interrupt */
  166. rt_interrupt_leave();
  167. }
  168. #endif /* BSP_USING_UART1 */
  169. #if defined(BSP_USING_UART2)
  170. /* UART2 device driver structure */
  171. static struct drv_uart uart2;
  172. struct rt_serial_device serial2;
  173. void USART2_IRQHandler(void)
  174. {
  175. struct drv_uart *uart;
  176. uart = &uart2;
  177. /* enter interrupt */
  178. rt_interrupt_enter();
  179. /* UART in mode Receiver -------------------------------------------------*/
  180. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  181. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  182. {
  183. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  184. /* Clear RXNE interrupt flag */
  185. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  186. }
  187. /* leave interrupt */
  188. rt_interrupt_leave();
  189. }
  190. #endif /* BSP_USING_UART2 */
  191. #if defined(BSP_USING_UART3)
  192. /* UART3 device driver structure */
  193. static struct drv_uart uart3;
  194. struct rt_serial_device serial3;
  195. void USART3_IRQHandler(void)
  196. {
  197. struct drv_uart *uart;
  198. uart = &uart3;
  199. /* enter interrupt */
  200. rt_interrupt_enter();
  201. /* UART in mode Receiver -------------------------------------------------*/
  202. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  203. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  204. {
  205. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  206. /* Clear RXNE interrupt flag */
  207. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  208. }
  209. /* leave interrupt */
  210. rt_interrupt_leave();
  211. }
  212. #endif /* BSP_USING_UART3 */
  213. #if defined(BSP_USING_UART4)
  214. /* UART4 device driver structure */
  215. static struct drv_uart uart4;
  216. struct rt_serial_device serial4;
  217. void UART4_IRQHandler(void)
  218. {
  219. struct drv_uart *uart;
  220. uart = &uart4;
  221. /* enter interrupt */
  222. rt_interrupt_enter();
  223. /* UART in mode Receiver -------------------------------------------------*/
  224. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  225. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  226. {
  227. rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND);
  228. /* Clear RXNE interrupt flag */
  229. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  230. }
  231. /* leave interrupt */
  232. rt_interrupt_leave();
  233. }
  234. #endif /* BSP_USING_UART4 */
  235. #if defined(BSP_USING_UART5)
  236. /* UART5 device driver structure */
  237. static struct drv_uart uart5;
  238. struct rt_serial_device serial5;
  239. void UART5_IRQHandler(void)
  240. {
  241. struct drv_uart *uart;
  242. uart = &uart5;
  243. /* enter interrupt */
  244. rt_interrupt_enter();
  245. /* UART in mode Receiver -------------------------------------------------*/
  246. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  247. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  248. {
  249. rt_hw_serial_isr(&serial5, RT_SERIAL_EVENT_RX_IND);
  250. /* Clear RXNE interrupt flag */
  251. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  252. }
  253. /* leave interrupt */
  254. rt_interrupt_leave();
  255. }
  256. #endif /* BSP_USING_UART5 */
  257. #if defined(BSP_USING_LPUART1)
  258. /* UART6 device driver structure */
  259. static struct drv_uart lpuart1;
  260. struct rt_serial_device lpserial1;
  261. void LPUART1_IRQHandler(void)
  262. {
  263. struct drv_uart *uart;
  264. uart = &lpuart1;
  265. /* enter interrupt */
  266. rt_interrupt_enter();
  267. /* UART in mode Receiver -------------------------------------------------*/
  268. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  269. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  270. {
  271. rt_hw_serial_isr(&lpserial1, RT_SERIAL_EVENT_RX_IND);
  272. /* Clear RXNE interrupt flag */
  273. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  274. }
  275. /* leave interrupt */
  276. rt_interrupt_leave();
  277. }
  278. #endif /* BSP_USING_LPUART1 */
  279. /**
  280. * @brief UART MSP Initialization
  281. * This function configures the hardware resources used in this example:
  282. * - Peripheral's clock enable
  283. * - Peripheral's GPIO Configuration
  284. * - NVIC configuration for UART interrupt request enable
  285. * @param huart: UART handle pointer
  286. * @retval None
  287. */
  288. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  289. {
  290. GPIO_InitTypeDef GPIO_InitStruct;
  291. if (uartHandle->Instance == LPUART1)
  292. {
  293. /* LPUART1 clock enable */
  294. __HAL_RCC_LPUART1_CLK_ENABLE();
  295. /* GPIOC clock enable */
  296. __HAL_RCC_GPIOC_CLK_ENABLE();
  297. /**LPUART1 GPIO Configuration
  298. PC0 ------> LPUART1_RX
  299. PC1 ------> LPUART1_TX
  300. */
  301. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  302. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  303. GPIO_InitStruct.Pull = GPIO_NOPULL;
  304. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  305. GPIO_InitStruct.Alternate = GPIO_AF8_LPUART1;
  306. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  307. }
  308. else if (uartHandle->Instance == UART4)
  309. {
  310. /* UART4 clock enable */
  311. __HAL_RCC_UART4_CLK_ENABLE();
  312. /* GPIOA clock enable */
  313. __HAL_RCC_GPIOA_CLK_ENABLE();
  314. /**UART4 GPIO Configuration
  315. PA0 ------> UART4_TX
  316. PA1 ------> UART4_RX
  317. */
  318. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  319. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  320. GPIO_InitStruct.Pull = GPIO_NOPULL;
  321. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  322. GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  323. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  324. }
  325. else if (uartHandle->Instance == UART5)
  326. {
  327. /* UART5 clock enable */
  328. __HAL_RCC_UART5_CLK_ENABLE();
  329. /* GPIOD clock enable */
  330. __HAL_RCC_GPIOD_CLK_ENABLE();
  331. /* GPIOC clock enable */
  332. __HAL_RCC_GPIOC_CLK_ENABLE();
  333. /**UART5 GPIO Configuration
  334. PC12 ------> UART5_TX
  335. PD2 ------> UART5_RX
  336. */
  337. GPIO_InitStruct.Pin = GPIO_PIN_12;
  338. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  339. GPIO_InitStruct.Pull = GPIO_NOPULL;
  340. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  341. GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
  342. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  343. GPIO_InitStruct.Pin = GPIO_PIN_2;
  344. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  345. GPIO_InitStruct.Pull = GPIO_NOPULL;
  346. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  347. GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
  348. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  349. }
  350. else if (uartHandle->Instance == USART1)
  351. {
  352. /* USART1 clock enable */
  353. __HAL_RCC_USART1_CLK_ENABLE();
  354. /* GPIOA clock enable */
  355. __HAL_RCC_GPIOA_CLK_ENABLE();
  356. /**USART1 GPIO Configuration
  357. PA9 ------> USART1_TX
  358. PA10 ------> USART1_RX
  359. */
  360. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  361. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  362. GPIO_InitStruct.Pull = GPIO_NOPULL;
  363. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  364. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  365. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  366. }
  367. else if (uartHandle->Instance == USART2)
  368. {
  369. /* USART2 clock enable */
  370. __HAL_RCC_USART2_CLK_ENABLE();
  371. /* GPIOA clock enable */
  372. __HAL_RCC_GPIOA_CLK_ENABLE();
  373. /**USART2 GPIO Configuration
  374. PA2 ------> USART2_TX
  375. PA3 ------> USART2_RX
  376. */
  377. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  378. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  379. GPIO_InitStruct.Pull = GPIO_NOPULL;
  380. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  381. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  382. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  383. }
  384. else if (uartHandle->Instance == USART3)
  385. {
  386. /* USART3 clock enable */
  387. __HAL_RCC_USART3_CLK_ENABLE();
  388. /* GPIOC clock enable */
  389. __HAL_RCC_GPIOC_CLK_ENABLE();
  390. /**USART3 GPIO Configuration
  391. PC4 ------> USART3_TX
  392. PC5 ------> USART3_RX
  393. */
  394. GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
  395. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  396. GPIO_InitStruct.Pull = GPIO_NOPULL;
  397. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  398. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  399. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  400. }
  401. }
  402. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  403. {
  404. if (uartHandle->Instance == LPUART1)
  405. {
  406. /* Peripheral clock disable */
  407. __HAL_RCC_LPUART1_CLK_DISABLE();
  408. /**LPUART1 GPIO Configuration
  409. PC0 ------> LPUART1_RX
  410. PC1 ------> LPUART1_TX
  411. */
  412. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_1);
  413. }
  414. else if (uartHandle->Instance == UART4)
  415. {
  416. /* Peripheral clock disable */
  417. __HAL_RCC_UART4_CLK_DISABLE();
  418. /**UART4 GPIO Configuration
  419. PA0 ------> UART4_TX
  420. PA1 ------> UART4_RX
  421. */
  422. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0 | GPIO_PIN_1);
  423. }
  424. else if (uartHandle->Instance == UART5)
  425. {
  426. /* Peripheral clock disable */
  427. __HAL_RCC_UART5_CLK_DISABLE();
  428. /**UART5 GPIO Configuration
  429. PC12 ------> UART5_TX
  430. PD2 ------> UART5_RX
  431. */
  432. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);
  433. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
  434. }
  435. else if (uartHandle->Instance == USART1)
  436. {
  437. /* Peripheral clock disable */
  438. __HAL_RCC_USART1_CLK_DISABLE();
  439. /**USART1 GPIO Configuration
  440. PA9 ------> USART1_TX
  441. PA10 ------> USART1_RX
  442. */
  443. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  444. }
  445. else if (uartHandle->Instance == USART2)
  446. {
  447. /* Peripheral clock disable */
  448. __HAL_RCC_USART2_CLK_DISABLE();
  449. /**USART2 GPIO Configuration
  450. PA2 ------> USART2_TX
  451. PA3 ------> USART2_RX
  452. */
  453. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
  454. }
  455. else if (uartHandle->Instance == USART3)
  456. {
  457. /* Peripheral clock disable */
  458. __HAL_RCC_USART3_CLK_DISABLE();
  459. /**USART3 GPIO Configuration
  460. PC4 ------> USART3_TX
  461. PC5 ------> USART3_RX
  462. */
  463. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4 | GPIO_PIN_5);
  464. }
  465. }
  466. int bsp_hw_usart_init(void)
  467. {
  468. struct drv_uart *uart;
  469. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  470. #ifdef BSP_USING_UART1
  471. uart = &uart1;
  472. uart->UartHandle.Instance = USART1;
  473. uart->irq = USART1_IRQn;
  474. serial1.ops = &drv_uart_ops;
  475. serial1.config = config;
  476. /* register UART1 device */
  477. rt_hw_serial_register(&serial1, "uart1",
  478. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  479. uart);
  480. #endif /* BSP_USING_UART1 */
  481. #ifdef BSP_USING_UART2
  482. uart = &uart2;
  483. uart->UartHandle.Instance = USART2;
  484. uart->irq = USART2_IRQn;
  485. serial2.ops = &drv_uart_ops;
  486. serial2.config = config;
  487. /* register UART2 device */
  488. rt_hw_serial_register(&serial2, "uart2",
  489. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  490. uart);
  491. #endif /* BSP_USING_UART2 */
  492. #ifdef BSP_USING_UART3
  493. uart = &uart3;
  494. uart->UartHandle.Instance = USART3;
  495. uart->irq = USART3_IRQn;
  496. serial3.ops = &drv_uart_ops;
  497. serial3.config = config;
  498. /* register UART3 device */
  499. rt_hw_serial_register(&serial3, "uart3",
  500. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  501. uart);
  502. #endif /* BSP_USING_UART3 */
  503. #ifdef BSP_USING_UART4
  504. uart = &uart4;
  505. uart->UartHandle.Instance = UART4;
  506. uart->irq = UART4_IRQn;
  507. serial4.ops = &drv_uart_ops;
  508. serial4.config = config;
  509. /* register UART4 device */
  510. rt_hw_serial_register(&serial4, "uart4",
  511. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  512. uart);
  513. #endif /* BSP_USING_UART4 */
  514. #ifdef BSP_USING_UART5
  515. uart = &uart5;
  516. uart->UartHandle.Instance = UART5;
  517. uart->irq = UART5_IRQn;
  518. serial5.ops = &drv_uart_ops;
  519. serial5.config = config;
  520. /* register UART5 device */
  521. rt_hw_serial_register(&serial5, "uart5",
  522. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  523. uart);
  524. #endif /* BSP_USING_UART5 */
  525. #ifdef BSP_USING_LPUART1
  526. uart = &lpuart1;
  527. uart->UartHandle.Instance = LPUART1;
  528. uart->irq = LPUART1_IRQn;
  529. lpserial1.ops = &drv_uart_ops;
  530. lpserial1.config = config;
  531. /* register LPUART1 device */
  532. rt_hw_serial_register(&lpserial1, "lpuart1",
  533. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  534. uart);
  535. #endif /* BSP_USING_LPUART1 */
  536. return 0;
  537. }
  538. INIT_BOARD_EXPORT(bsp_hw_usart_init);