usart.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. while((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
  162. uart->UartHandle.Instance->DR = c;
  163. return 1;
  164. }
  165. static int stm32_getc(struct rt_serial_device *serial)
  166. {
  167. int ch;
  168. struct stm32_uart *uart;
  169. RT_ASSERT(serial != RT_NULL);
  170. uart = (struct stm32_uart *)serial->parent.user_data;
  171. ch = -1;
  172. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  173. ch = uart->UartHandle.Instance->DR & 0xff;
  174. return ch;
  175. }
  176. /**
  177. * Uart common interrupt process. This need add to uart ISR.
  178. *
  179. * @param serial serial device
  180. */
  181. static void uart_isr(struct rt_serial_device *serial) {
  182. struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
  183. RT_ASSERT(uart != RT_NULL);
  184. /* UART in mode Receiver -------------------------------------------------*/
  185. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  186. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  187. {
  188. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  189. /* Clear RXNE interrupt flag */
  190. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  191. }
  192. }
  193. /**
  194. * @brief UART MSP Initialization
  195. * This function configures the hardware resources used in this example:
  196. * - Peripheral's clock enable
  197. * - Peripheral's GPIO Configuration
  198. * - NVIC configuration for UART interrupt request enable
  199. * @param huart: UART handle pointer
  200. * @retval None
  201. */
  202. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  203. {
  204. GPIO_InitTypeDef GPIO_InitStruct;
  205. #if defined(RT_USING_UART1)
  206. if (huart->Instance == USART1)
  207. {
  208. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  209. /* Enable GPIO TX/RX clock */
  210. USART1_TX_GPIO_CLK_ENABLE();
  211. USART1_RX_GPIO_CLK_ENABLE();
  212. /* Enable USARTx clock */
  213. __HAL_RCC_USART1_CLK_ENABLE();
  214. /*##-2- Configure peripheral GPIO ##########################################*/
  215. /* UART TX GPIO pin configuration */
  216. GPIO_InitStruct.Pin = USART1_TX_PIN;
  217. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  218. GPIO_InitStruct.Pull = GPIO_PULLUP;
  219. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  220. GPIO_InitStruct.Alternate = USART1_TX_AF;
  221. HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
  222. /* UART RX GPIO pin configuration */
  223. GPIO_InitStruct.Pin = USART1_RX_PIN;
  224. GPIO_InitStruct.Alternate = USART1_RX_AF;
  225. HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
  226. HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
  227. HAL_NVIC_EnableIRQ(USART1_IRQn);
  228. }
  229. #endif
  230. #if defined(RT_USING_UART2)
  231. if (huart->Instance == USART2)
  232. {
  233. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  234. /* Enable GPIO TX/RX clock */
  235. USART2_TX_GPIO_CLK_ENABLE();
  236. USART2_RX_GPIO_CLK_ENABLE();
  237. /* Enable USARTx clock */
  238. __HAL_RCC_USART2_CLK_ENABLE();
  239. /*##-2- Configure peripheral GPIO ##########################################*/
  240. /* UART TX GPIO pin configuration */
  241. GPIO_InitStruct.Pin = USART2_TX_PIN;
  242. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  243. GPIO_InitStruct.Pull = GPIO_PULLUP;
  244. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  245. GPIO_InitStruct.Alternate = USART2_TX_AF;
  246. HAL_GPIO_Init(USART2_TX_GPIO_PORT, &GPIO_InitStruct);
  247. /* UART RX GPIO pin configuration */
  248. GPIO_InitStruct.Pin = USART2_RX_PIN;
  249. GPIO_InitStruct.Alternate = USART2_RX_AF;
  250. HAL_GPIO_Init(USART2_RX_GPIO_PORT, &GPIO_InitStruct);
  251. HAL_NVIC_SetPriority(USART2_IRQn, 0, 1);
  252. HAL_NVIC_EnableIRQ(USART2_IRQn);
  253. }
  254. #endif
  255. #if defined(RT_USING_UART3)
  256. if (huart->Instance == USART3)
  257. {
  258. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  259. /* Enable GPIO TX/RX clock */
  260. USART3_TX_GPIO_CLK_ENABLE();
  261. USART3_RX_GPIO_CLK_ENABLE();
  262. /* Enable USARTx clock */
  263. __HAL_RCC_USART3_CLK_ENABLE();
  264. /*##-2- Configure peripheral GPIO ##########################################*/
  265. /* UART TX GPIO pin configuration */
  266. GPIO_InitStruct.Pin = USART3_TX_PIN;
  267. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  268. GPIO_InitStruct.Pull = GPIO_PULLUP;
  269. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  270. GPIO_InitStruct.Alternate = USART3_TX_AF;
  271. HAL_GPIO_Init(USART3_TX_GPIO_PORT, &GPIO_InitStruct);
  272. /* UART RX GPIO pin configuration */
  273. GPIO_InitStruct.Pin = USART3_RX_PIN;
  274. GPIO_InitStruct.Alternate = USART3_RX_AF;
  275. HAL_GPIO_Init(USART3_RX_GPIO_PORT, &GPIO_InitStruct);
  276. HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
  277. HAL_NVIC_EnableIRQ(USART3_IRQn);
  278. }
  279. #endif
  280. #if defined(RT_USING_UART4)
  281. if (huart->Instance == USART4)
  282. {
  283. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  284. /* Enable GPIO TX/RX clock */
  285. USART4_TX_GPIO_CLK_ENABLE();
  286. USART4_RX_GPIO_CLK_ENABLE();
  287. /* Enable USARTx clock */
  288. __HAL_RCC_USART4_CLK_ENABLE();
  289. /*##-2- Configure peripheral GPIO ##########################################*/
  290. /* UART TX GPIO pin configuration */
  291. GPIO_InitStruct.Pin = USART4_TX_PIN;
  292. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  293. GPIO_InitStruct.Pull = GPIO_PULLUP;
  294. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  295. GPIO_InitStruct.Alternate = USART4_TX_AF;
  296. HAL_GPIO_Init(USART4_TX_GPIO_PORT, &GPIO_InitStruct);
  297. /* UART RX GPIO pin configuration */
  298. GPIO_InitStruct.Pin = USART4_RX_PIN;
  299. GPIO_InitStruct.Alternate = USART4_RX_AF;
  300. HAL_GPIO_Init(USART4_RX_GPIO_PORT, &GPIO_InitStruct);
  301. HAL_NVIC_SetPriority(USART4_IRQn, 0, 1);
  302. HAL_NVIC_EnableIRQ(USART4_IRQn);
  303. }
  304. #endif
  305. #if defined(RT_USING_UART5)
  306. if (huart->Instance == USART5)
  307. {
  308. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  309. /* Enable GPIO TX/RX clock */
  310. USART5_TX_GPIO_CLK_ENABLE();
  311. USART5_RX_GPIO_CLK_ENABLE();
  312. /* Enable USARTx clock */
  313. __HAL_RCC_USART5_CLK_ENABLE();
  314. /*##-2- Configure peripheral GPIO ##########################################*/
  315. /* UART TX GPIO pin configuration */
  316. GPIO_InitStruct.Pin = USART5_TX_PIN;
  317. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  318. GPIO_InitStruct.Pull = GPIO_PULLUP;
  319. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  320. GPIO_InitStruct.Alternate = USART5_TX_AF;
  321. HAL_GPIO_Init(USART5_TX_GPIO_PORT, &GPIO_InitStruct);
  322. /* UART RX GPIO pin configuration */
  323. GPIO_InitStruct.Pin = USART5_RX_PIN;
  324. GPIO_InitStruct.Alternate = USART5_RX_AF;
  325. HAL_GPIO_Init(USART5_RX_GPIO_PORT, &GPIO_InitStruct);
  326. HAL_NVIC_SetPriority(USART5_IRQn, 0, 1);
  327. HAL_NVIC_EnableIRQ(USART5_IRQn);
  328. }
  329. #endif
  330. }
  331. /**
  332. * @brief UART MSP De-Initialization
  333. * This function frees the hardware resources used in this example:
  334. * - Disable the Peripheral's clock
  335. * - Revert GPIO and NVIC configuration to their default state
  336. * @param huart: UART handle pointer
  337. * @retval None
  338. */
  339. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  340. {
  341. #if defined(RT_USING_UART1)
  342. if (huart->Instance == USART1)
  343. {
  344. /*##-1- Reset peripherals ##################################################*/
  345. __HAL_RCC_USART1_FORCE_RESET();
  346. __HAL_RCC_USART1_RELEASE_RESET();
  347. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  348. /* Configure UART Tx as alternate function */
  349. HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_PIN);
  350. /* Configure UART Rx as alternate function */
  351. HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_PIN);
  352. HAL_NVIC_DisableIRQ(USART1_IRQn);
  353. }
  354. #endif
  355. #if defined(RT_USING_UART2)
  356. if (huart->Instance == USART2)
  357. {
  358. /*##-1- Reset peripherals ##################################################*/
  359. __HAL_RCC_USART2_FORCE_RESET();
  360. __HAL_RCC_USART2_RELEASE_RESET();
  361. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  362. /* Configure UART Tx as alternate function */
  363. HAL_GPIO_DeInit(USART2_TX_GPIO_PORT, USART2_TX_PIN);
  364. /* Configure UART Rx as alternate function */
  365. HAL_GPIO_DeInit(USART2_RX_GPIO_PORT, USART2_RX_PIN);
  366. HAL_NVIC_DisableIRQ(USART2_IRQn);
  367. }
  368. #endif
  369. #if defined(RT_USING_UART3)
  370. if (huart->Instance == USART3)
  371. {
  372. /*##-1- Reset peripherals ##################################################*/
  373. __HAL_RCC_USART3_FORCE_RESET();
  374. __HAL_RCC_USART3_RELEASE_RESET();
  375. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  376. /* Configure UART Tx as alternate function */
  377. HAL_GPIO_DeInit(USART3_TX_GPIO_PORT, USART3_TX_PIN);
  378. /* Configure UART Rx as alternate function */
  379. HAL_GPIO_DeInit(USART3_RX_GPIO_PORT, USART3_RX_PIN);
  380. HAL_NVIC_DisableIRQ(USART3_IRQn);
  381. }
  382. #endif
  383. #if defined(RT_USING_UART4)
  384. if (huart->Instance == USART4)
  385. {
  386. /*##-1- Reset peripherals ##################################################*/
  387. __HAL_RCC_USART4_FORCE_RESET();
  388. __HAL_RCC_USART4_RELEASE_RESET();
  389. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  390. /* Configure UART Tx as alternate function */
  391. HAL_GPIO_DeInit(USART4_TX_GPIO_PORT, USART4_TX_PIN);
  392. /* Configure UART Rx as alternate function */
  393. HAL_GPIO_DeInit(USART4_RX_GPIO_PORT, USART4_RX_PIN);
  394. HAL_NVIC_DisableIRQ(USART4_IRQn);
  395. }
  396. #endif
  397. #if defined(RT_USING_UART5)
  398. if (huart->Instance == USART5)
  399. {
  400. /*##-1- Reset peripherals ##################################################*/
  401. __HAL_RCC_USART5_FORCE_RESET();
  402. __HAL_RCC_USART5_RELEASE_RESET();
  403. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  404. /* Configure UART Tx as alternate function */
  405. HAL_GPIO_DeInit(USART5_TX_GPIO_PORT, USART5_TX_PIN);
  406. /* Configure UART Rx as alternate function */
  407. HAL_GPIO_DeInit(USART5_RX_GPIO_PORT, USART5_RX_PIN);
  408. HAL_NVIC_DisableIRQ(USART5_IRQn);
  409. }
  410. #endif
  411. }
  412. static const struct rt_uart_ops stm32_uart_ops =
  413. {
  414. stm32_configure,
  415. stm32_control,
  416. stm32_putc,
  417. stm32_getc,
  418. };
  419. #if defined(RT_USING_UART1)
  420. /* UART1 device driver structure */
  421. struct stm32_uart uart1 =
  422. {
  423. {USART1},
  424. USART1_IRQn,
  425. };
  426. struct rt_serial_device serial1;
  427. void USART1_IRQHandler(void)
  428. {
  429. /* enter interrupt */
  430. rt_interrupt_enter();
  431. uart_isr(&serial1);
  432. /* leave interrupt */
  433. rt_interrupt_leave();
  434. }
  435. #endif /* RT_USING_UART1 */
  436. #if defined(RT_USING_UART2)
  437. /* UART2 device driver structure */
  438. struct stm32_uart uart2 =
  439. {
  440. {USART2},
  441. USART2_IRQn,
  442. };
  443. struct rt_serial_device serial2;
  444. void USART2_IRQHandler(void)
  445. {
  446. /* enter interrupt */
  447. rt_interrupt_enter();
  448. uart_isr(&serial2);
  449. /* leave interrupt */
  450. rt_interrupt_leave();
  451. }
  452. #endif /* RT_USING_UART2 */
  453. #if defined(RT_USING_UART3)
  454. /* UART3 device driver structure */
  455. struct stm32_uart uart3 =
  456. {
  457. {USART3},
  458. USART3_IRQn,
  459. };
  460. struct rt_serial_device serial3;
  461. void USART3_IRQHandler(void)
  462. {
  463. /* enter interrupt */
  464. rt_interrupt_enter();
  465. uart_isr(&serial3);
  466. /* leave interrupt */
  467. rt_interrupt_leave();
  468. }
  469. #endif /* RT_USING_UART3 */
  470. #if defined(RT_USING_UART4)
  471. /* UART4 device driver structure */
  472. struct stm32_uart uart4 =
  473. {
  474. {UART4},
  475. UART4_IRQn,
  476. };
  477. struct rt_serial_device serial4;
  478. void UART4_IRQHandler(void)
  479. {
  480. /* enter interrupt */
  481. rt_interrupt_enter();
  482. uart_isr(&serial4);
  483. /* leave interrupt */
  484. rt_interrupt_leave();
  485. }
  486. #endif /* RT_USING_UART4 */
  487. #if defined(RT_USING_UART5)
  488. /* UART5 device driver structure */
  489. struct stm32_uart uart5 =
  490. {
  491. {UART5},
  492. UART5_IRQn,
  493. };
  494. struct rt_serial_device serial5;
  495. void UART5_IRQHandler(void)
  496. {
  497. /* enter interrupt */
  498. rt_interrupt_enter();
  499. uart_isr(&serial5);
  500. /* leave interrupt */
  501. rt_interrupt_leave();
  502. }
  503. #endif /* RT_USING_UART5 */
  504. int stm32_hw_usart_init(void)
  505. {
  506. struct stm32_uart *uart;
  507. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  508. #ifdef RT_USING_UART1
  509. uart = &uart1;
  510. uart->UartHandle.Instance = USART1;
  511. serial1.ops = &stm32_uart_ops;
  512. serial1.config = config;
  513. /* register UART1 device */
  514. rt_hw_serial_register(&serial1,
  515. "uart1",
  516. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  517. uart);
  518. #endif /* RT_USING_UART1 */
  519. #ifdef RT_USING_UART2
  520. uart = &uart2;
  521. uart->UartHandle.Instance = USART2;
  522. serial2.ops = &stm32_uart_ops;
  523. serial2.config = config;
  524. /* register UART1 device */
  525. rt_hw_serial_register(&serial2,
  526. "uart2",
  527. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  528. uart);
  529. #endif /* RT_USING_UART2 */
  530. #ifdef RT_USING_UART3
  531. uart = &uart3;
  532. uart->UartHandle.Instance = USART3;
  533. serial3.ops = &stm32_uart_ops;
  534. serial3.config = config;
  535. /* register UART3 device */
  536. rt_hw_serial_register(&serial3,
  537. "uart3",
  538. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  539. uart);
  540. #endif /* RT_USING_UART3 */
  541. #ifdef RT_USING_UART4
  542. uart = &uart4;
  543. uart->UartHandle.Instance = USART4;
  544. serial4.ops = &stm32_uart_ops;
  545. serial4.config = config;
  546. /* register UART4 device */
  547. rt_hw_serial_register(&serial4,
  548. "uart4",
  549. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  550. uart);
  551. #endif /* RT_USING_UART4 */
  552. #ifdef RT_USING_UART5
  553. uart = &uart5;
  554. uart->UartHandle.Instance = USART5;
  555. serial5.ops = &stm32_uart_ops;
  556. serial5.config = config;
  557. /* register UART5 device */
  558. rt_hw_serial_register(&serial5,
  559. "uart5",
  560. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  561. uart);
  562. #endif /* RT_USING_UART5 */
  563. return 0;
  564. }
  565. INIT_BOARD_EXPORT(stm32_hw_usart_init);