drv_usart.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. while ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
  125. uart->UartHandle.Instance->TDR = c;
  126. return 1;
  127. }
  128. static int drv_getc(struct rt_serial_device *serial)
  129. {
  130. int ch;
  131. struct drv_uart *uart;
  132. RT_ASSERT(serial != RT_NULL);
  133. uart = (struct drv_uart *)serial->parent.user_data;
  134. ch = -1;
  135. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  136. ch = uart->UartHandle.Instance->RDR & 0xff;
  137. return ch;
  138. }
  139. static const struct rt_uart_ops drv_uart_ops =
  140. {
  141. drv_configure,
  142. drv_control,
  143. drv_putc,
  144. drv_getc,
  145. };
  146. #if defined(BSP_USING_UART1)
  147. /* UART1 device driver structure */
  148. static struct drv_uart uart1;
  149. struct rt_serial_device serial1;
  150. void USART1_IRQHandler(void)
  151. {
  152. struct drv_uart *uart;
  153. uart = &uart1;
  154. /* enter interrupt */
  155. rt_interrupt_enter();
  156. /* UART in mode Receiver -------------------------------------------------*/
  157. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  158. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  159. {
  160. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  161. /* Clear RXNE interrupt flag */
  162. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  163. }
  164. /* leave interrupt */
  165. rt_interrupt_leave();
  166. }
  167. #endif /* BSP_USING_UART1 */
  168. #if defined(BSP_USING_UART2)
  169. /* UART2 device driver structure */
  170. static struct drv_uart uart2;
  171. struct rt_serial_device serial2;
  172. void USART2_IRQHandler(void)
  173. {
  174. struct drv_uart *uart;
  175. uart = &uart2;
  176. /* enter interrupt */
  177. rt_interrupt_enter();
  178. /* UART in mode Receiver -------------------------------------------------*/
  179. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  180. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  181. {
  182. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  183. /* Clear RXNE interrupt flag */
  184. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  185. }
  186. /* leave interrupt */
  187. rt_interrupt_leave();
  188. }
  189. #endif /* BSP_USING_UART2 */
  190. #if defined(BSP_USING_UART3)
  191. /* UART3 device driver structure */
  192. static struct drv_uart uart3;
  193. struct rt_serial_device serial3;
  194. void USART3_IRQHandler(void)
  195. {
  196. struct drv_uart *uart;
  197. uart = &uart3;
  198. /* enter interrupt */
  199. rt_interrupt_enter();
  200. /* UART in mode Receiver -------------------------------------------------*/
  201. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  202. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  203. {
  204. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  205. /* Clear RXNE interrupt flag */
  206. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  207. }
  208. /* leave interrupt */
  209. rt_interrupt_leave();
  210. }
  211. #endif /* BSP_USING_UART3 */
  212. #if defined(BSP_USING_UART4)
  213. /* UART4 device driver structure */
  214. static struct drv_uart uart4;
  215. struct rt_serial_device serial4;
  216. void UART4_IRQHandler(void)
  217. {
  218. struct drv_uart *uart;
  219. uart = &uart4;
  220. /* enter interrupt */
  221. rt_interrupt_enter();
  222. /* UART in mode Receiver -------------------------------------------------*/
  223. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  224. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  225. {
  226. rt_hw_serial_isr(&serial4, RT_SERIAL_EVENT_RX_IND);
  227. /* Clear RXNE interrupt flag */
  228. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  229. }
  230. /* leave interrupt */
  231. rt_interrupt_leave();
  232. }
  233. #endif /* BSP_USING_UART4 */
  234. #if defined(BSP_USING_UART5)
  235. /* UART5 device driver structure */
  236. static struct drv_uart uart5;
  237. struct rt_serial_device serial5;
  238. void UART5_IRQHandler(void)
  239. {
  240. struct drv_uart *uart;
  241. uart = &uart5;
  242. /* enter interrupt */
  243. rt_interrupt_enter();
  244. /* UART in mode Receiver -------------------------------------------------*/
  245. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  246. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  247. {
  248. rt_hw_serial_isr(&serial5, RT_SERIAL_EVENT_RX_IND);
  249. /* Clear RXNE interrupt flag */
  250. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  251. }
  252. /* leave interrupt */
  253. rt_interrupt_leave();
  254. }
  255. #endif /* BSP_USING_UART5 */
  256. #if defined(BSP_USING_LPUART1)
  257. /* UART6 device driver structure */
  258. static struct drv_uart lpuart1;
  259. struct rt_serial_device lpserial1;
  260. void LPUART1_IRQHandler(void)
  261. {
  262. struct drv_uart *uart;
  263. uart = &lpuart1;
  264. /* enter interrupt */
  265. rt_interrupt_enter();
  266. /* UART in mode Receiver -------------------------------------------------*/
  267. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  268. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  269. {
  270. rt_hw_serial_isr(&lpserial1, RT_SERIAL_EVENT_RX_IND);
  271. /* Clear RXNE interrupt flag */
  272. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  273. }
  274. /* leave interrupt */
  275. rt_interrupt_leave();
  276. }
  277. #endif /* BSP_USING_LPUART1 */
  278. /**
  279. * @brief UART MSP Initialization
  280. * This function configures the hardware resources used in this example:
  281. * - Peripheral's clock enable
  282. * - Peripheral's GPIO Configuration
  283. * - NVIC configuration for UART interrupt request enable
  284. * @param huart: UART handle pointer
  285. * @retval None
  286. */
  287. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  288. {
  289. GPIO_InitTypeDef GPIO_InitStruct;
  290. if (uartHandle->Instance == LPUART1)
  291. {
  292. /* LPUART1 clock enable */
  293. __HAL_RCC_LPUART1_CLK_ENABLE();
  294. /* GPIOC clock enable */
  295. __HAL_RCC_GPIOC_CLK_ENABLE();
  296. /**LPUART1 GPIO Configuration
  297. PC0 ------> LPUART1_RX
  298. PC1 ------> LPUART1_TX
  299. */
  300. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  301. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  302. GPIO_InitStruct.Pull = GPIO_NOPULL;
  303. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  304. GPIO_InitStruct.Alternate = GPIO_AF8_LPUART1;
  305. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  306. }
  307. else if (uartHandle->Instance == UART4)
  308. {
  309. /* UART4 clock enable */
  310. __HAL_RCC_UART4_CLK_ENABLE();
  311. /* GPIOA clock enable */
  312. __HAL_RCC_GPIOA_CLK_ENABLE();
  313. /**UART4 GPIO Configuration
  314. PA0 ------> UART4_TX
  315. PA1 ------> UART4_RX
  316. */
  317. GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  318. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  319. GPIO_InitStruct.Pull = GPIO_NOPULL;
  320. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  321. GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
  322. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  323. }
  324. else if (uartHandle->Instance == UART5)
  325. {
  326. /* UART5 clock enable */
  327. __HAL_RCC_UART5_CLK_ENABLE();
  328. /* GPIOD clock enable */
  329. __HAL_RCC_GPIOD_CLK_ENABLE();
  330. /* GPIOC clock enable */
  331. __HAL_RCC_GPIOC_CLK_ENABLE();
  332. /**UART5 GPIO Configuration
  333. PC12 ------> UART5_TX
  334. PD2 ------> UART5_RX
  335. */
  336. GPIO_InitStruct.Pin = GPIO_PIN_12;
  337. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  338. GPIO_InitStruct.Pull = GPIO_NOPULL;
  339. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  340. GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
  341. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  342. GPIO_InitStruct.Pin = GPIO_PIN_2;
  343. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  344. GPIO_InitStruct.Pull = GPIO_NOPULL;
  345. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  346. GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
  347. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  348. }
  349. else if (uartHandle->Instance == USART1)
  350. {
  351. /* USART1 clock enable */
  352. __HAL_RCC_USART1_CLK_ENABLE();
  353. /* GPIOA clock enable */
  354. __HAL_RCC_GPIOA_CLK_ENABLE();
  355. /**USART1 GPIO Configuration
  356. PA9 ------> USART1_TX
  357. PA10 ------> USART1_RX
  358. */
  359. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  360. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  361. GPIO_InitStruct.Pull = GPIO_NOPULL;
  362. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  363. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  364. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  365. }
  366. else if (uartHandle->Instance == USART2)
  367. {
  368. /* USART2 clock enable */
  369. __HAL_RCC_USART2_CLK_ENABLE();
  370. /* GPIOA clock enable */
  371. __HAL_RCC_GPIOA_CLK_ENABLE();
  372. /**USART2 GPIO Configuration
  373. PA2 ------> USART2_TX
  374. PA3 ------> USART2_RX
  375. */
  376. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  377. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  378. GPIO_InitStruct.Pull = GPIO_NOPULL;
  379. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  380. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  381. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  382. }
  383. else if (uartHandle->Instance == USART3)
  384. {
  385. /* USART3 clock enable */
  386. __HAL_RCC_USART3_CLK_ENABLE();
  387. /* GPIOC clock enable */
  388. __HAL_RCC_GPIOC_CLK_ENABLE();
  389. /**USART3 GPIO Configuration
  390. PC4 ------> USART3_TX
  391. PC5 ------> USART3_RX
  392. */
  393. GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
  394. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  395. GPIO_InitStruct.Pull = GPIO_NOPULL;
  396. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  397. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  398. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  399. }
  400. }
  401. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  402. {
  403. if (uartHandle->Instance == LPUART1)
  404. {
  405. /* Peripheral clock disable */
  406. __HAL_RCC_LPUART1_CLK_DISABLE();
  407. /**LPUART1 GPIO Configuration
  408. PC0 ------> LPUART1_RX
  409. PC1 ------> LPUART1_TX
  410. */
  411. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0 | GPIO_PIN_1);
  412. }
  413. else if (uartHandle->Instance == UART4)
  414. {
  415. /* Peripheral clock disable */
  416. __HAL_RCC_UART4_CLK_DISABLE();
  417. /**UART4 GPIO Configuration
  418. PA0 ------> UART4_TX
  419. PA1 ------> UART4_RX
  420. */
  421. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0 | GPIO_PIN_1);
  422. }
  423. else if (uartHandle->Instance == UART5)
  424. {
  425. /* Peripheral clock disable */
  426. __HAL_RCC_UART5_CLK_DISABLE();
  427. /**UART5 GPIO Configuration
  428. PC12 ------> UART5_TX
  429. PD2 ------> UART5_RX
  430. */
  431. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);
  432. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
  433. }
  434. else if (uartHandle->Instance == USART1)
  435. {
  436. /* Peripheral clock disable */
  437. __HAL_RCC_USART1_CLK_DISABLE();
  438. /**USART1 GPIO Configuration
  439. PA9 ------> USART1_TX
  440. PA10 ------> USART1_RX
  441. */
  442. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  443. }
  444. else if (uartHandle->Instance == USART2)
  445. {
  446. /* Peripheral clock disable */
  447. __HAL_RCC_USART2_CLK_DISABLE();
  448. /**USART2 GPIO Configuration
  449. PA2 ------> USART2_TX
  450. PA3 ------> USART2_RX
  451. */
  452. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
  453. }
  454. else if (uartHandle->Instance == USART3)
  455. {
  456. /* Peripheral clock disable */
  457. __HAL_RCC_USART3_CLK_DISABLE();
  458. /**USART3 GPIO Configuration
  459. PC4 ------> USART3_TX
  460. PC5 ------> USART3_RX
  461. */
  462. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4 | GPIO_PIN_5);
  463. }
  464. }
  465. int bsp_hw_usart_init(void)
  466. {
  467. struct drv_uart *uart;
  468. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  469. #ifdef BSP_USING_UART1
  470. uart = &uart1;
  471. uart->UartHandle.Instance = USART1;
  472. uart->irq = USART1_IRQn;
  473. serial1.ops = &drv_uart_ops;
  474. serial1.config = config;
  475. /* register UART1 device */
  476. rt_hw_serial_register(&serial1, "uart1",
  477. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  478. uart);
  479. #endif /* BSP_USING_UART1 */
  480. #ifdef BSP_USING_UART2
  481. uart = &uart2;
  482. uart->UartHandle.Instance = USART2;
  483. uart->irq = USART2_IRQn;
  484. serial2.ops = &drv_uart_ops;
  485. serial2.config = config;
  486. /* register UART2 device */
  487. rt_hw_serial_register(&serial2, "uart2",
  488. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  489. uart);
  490. #endif /* BSP_USING_UART2 */
  491. #ifdef BSP_USING_UART3
  492. uart = &uart3;
  493. uart->UartHandle.Instance = USART3;
  494. uart->irq = USART3_IRQn;
  495. serial3.ops = &drv_uart_ops;
  496. serial3.config = config;
  497. /* register UART3 device */
  498. rt_hw_serial_register(&serial3, "uart3",
  499. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  500. uart);
  501. #endif /* BSP_USING_UART3 */
  502. #ifdef BSP_USING_UART4
  503. uart = &uart4;
  504. uart->UartHandle.Instance = UART4;
  505. uart->irq = UART4_IRQn;
  506. serial4.ops = &drv_uart_ops;
  507. serial4.config = config;
  508. /* register UART4 device */
  509. rt_hw_serial_register(&serial4, "uart4",
  510. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  511. uart);
  512. #endif /* BSP_USING_UART4 */
  513. #ifdef BSP_USING_UART5
  514. uart = &uart5;
  515. uart->UartHandle.Instance = UART5;
  516. uart->irq = UART5_IRQn;
  517. serial5.ops = &drv_uart_ops;
  518. serial5.config = config;
  519. /* register UART5 device */
  520. rt_hw_serial_register(&serial5, "uart5",
  521. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  522. uart);
  523. #endif /* BSP_USING_UART5 */
  524. #ifdef BSP_USING_LPUART1
  525. uart = &lpuart1;
  526. uart->UartHandle.Instance = LPUART1;
  527. uart->irq = LPUART1_IRQn;
  528. lpserial1.ops = &drv_uart_ops;
  529. lpserial1.config = config;
  530. /* register LPUART1 device */
  531. rt_hw_serial_register(&lpserial1, "lpuart1",
  532. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  533. uart);
  534. #endif /* BSP_USING_LPUART1 */
  535. return 0;
  536. }
  537. INIT_BOARD_EXPORT(bsp_hw_usart_init);