usart.c 20 KB

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