usart.c 24 KB

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