You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2797 lines
95 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_uart.c
  4. * @author MCD Application Team
  5. * @brief UART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. *
  13. @verbatim
  14. ===============================================================================
  15. ##### How to use this driver #####
  16. ===============================================================================
  17. [..]
  18. The UART HAL driver can be used as follows:
  19. (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  20. (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  21. (++) Enable the USARTx interface clock.
  22. (++) UART pins configuration:
  23. (+++) Enable the clock for the UART GPIOs.
  24. (+++) Configure these UART pins as alternate function pull-up.
  25. (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  26. and HAL_UART_Receive_IT() APIs):
  27. (+++) Configure the USARTx interrupt priority.
  28. (+++) Enable the NVIC USART IRQ handle.
  29. (++) UART interrupts handling:
  30. -@@- The specific UART interrupts (Transmission complete interrupt,
  31. RXNE interrupt and Error Interrupts) are managed using the macros
  32. __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit and receive processes.
  33. (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  34. and HAL_UART_Receive_DMA() APIs):
  35. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  36. (+++) Enable the DMAx interface clock.
  37. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  38. (+++) Configure the DMA Tx/Rx channel.
  39. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  40. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  41. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  42. flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
  43. (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
  44. in the huart handle AdvancedInit structure.
  45. (#) For the UART asynchronous mode, initialize the UART registers by calling
  46. the HAL_UART_Init() API.
  47. (#) For the UART Half duplex mode, initialize the UART registers by calling
  48. the HAL_HalfDuplex_Init() API.
  49. (#) For the UART Multiprocessor mode, initialize the UART registers
  50. by calling the HAL_MultiProcessor_Init() API.
  51. (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
  52. by calling the HAL_RS485Ex_Init() API.
  53. [..]
  54. (@) These APIs(HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_MultiProcessor_Init(),
  55. also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
  56. calling the customized HAL_UART_MspInit() API.
  57. [..]
  58. [..] Three operation modes are available within this driver :
  59. *** Polling mode IO operation ***
  60. =================================
  61. [..]
  62. (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
  63. (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
  64. *** Interrupt mode IO operation ***
  65. ===================================
  66. [..]
  67. (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  68. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  69. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  70. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  72. (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  73. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  74. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  75. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  76. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  77. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  78. add his own code by customization of function pointer HAL_UART_ErrorCallback
  79. *** DMA mode IO operation ***
  80. ==============================
  81. [..]
  82. (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  83. (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  84. add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  85. (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  86. add his own code by customization of function pointer HAL_UART_TxCpltCallback
  87. (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  88. (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  89. add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  90. (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  91. add his own code by customization of function pointer HAL_UART_RxCpltCallback
  92. (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  93. add his own code by customization of function pointer HAL_UART_ErrorCallback
  94. (+) Pause the DMA Transfer using HAL_UART_DMAPause()
  95. (+) Resume the DMA Transfer using HAL_UART_DMAResume()
  96. (+) Stop the DMA Transfer using HAL_UART_DMAStop()
  97. *** UART HAL driver macros list ***
  98. =============================================
  99. [..]
  100. Below the list of most used macros in UART HAL driver.
  101. (+) __HAL_UART_ENABLE: Enable the UART peripheral
  102. (+) __HAL_UART_DISABLE: Disable the UART peripheral
  103. (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
  104. (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
  105. (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
  106. (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
  107. [..]
  108. (@) You can refer to the UART HAL driver header file for more useful macros
  109. @endverbatim
  110. ******************************************************************************
  111. * @attention
  112. *
  113. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  114. *
  115. * Redistribution and use in source and binary forms, with or without modification,
  116. * are permitted provided that the following conditions are met:
  117. * 1. Redistributions of source code must retain the above copyright notice,
  118. * this list of conditions and the following disclaimer.
  119. * 2. Redistributions in binary form must reproduce the above copyright notice,
  120. * this list of conditions and the following disclaimer in the documentation
  121. * and/or other materials provided with the distribution.
  122. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  123. * may be used to endorse or promote products derived from this software
  124. * without specific prior written permission.
  125. *
  126. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  127. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  128. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  129. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  130. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  131. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  132. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  133. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  134. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  135. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  136. *
  137. ******************************************************************************
  138. */
  139. /* Includes ------------------------------------------------------------------*/
  140. #include "stm32f0xx_hal.h"
  141. /** @addtogroup STM32F0xx_HAL_Driver
  142. * @{
  143. */
  144. /** @defgroup UART UART
  145. * @brief HAL UART module driver
  146. * @{
  147. */
  148. #ifdef HAL_UART_MODULE_ENABLED
  149. /* Private typedef -----------------------------------------------------------*/
  150. /* Private define ------------------------------------------------------------*/
  151. /** @defgroup UART_Private_Constants UART Private Constants
  152. * @{
  153. */
  154. #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  155. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  156. /**
  157. * @}
  158. */
  159. /* Private macros ------------------------------------------------------------*/
  160. /* Private variables ---------------------------------------------------------*/
  161. /* Private function prototypes -----------------------------------------------*/
  162. /** @addtogroup UART_Private_Functions
  163. * @{
  164. */
  165. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  166. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  167. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  168. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  169. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  170. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  171. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  172. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  173. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  174. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  175. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  176. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  177. HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
  178. HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  179. HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
  180. /**
  181. * @}
  182. */
  183. /* Exported functions --------------------------------------------------------*/
  184. /** @defgroup UART_Exported_Functions UART Exported Functions
  185. * @{
  186. */
  187. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  188. * @brief Initialization and Configuration functions
  189. *
  190. @verbatim
  191. ===============================================================================
  192. ##### Initialization and Configuration functions #####
  193. ===============================================================================
  194. [..]
  195. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  196. in asynchronous mode.
  197. (+) For the asynchronous mode the parameters below can be configured:
  198. (++) Baud Rate
  199. (++) Word Length
  200. (++) Stop Bit
  201. (++) Parity
  202. (++) Hardware flow control
  203. (++) Receiver/transmitter modes
  204. (++) Over Sampling Method
  205. (++) One-Bit Sampling Method
  206. (+) For the asynchronous mode, the following advanced features can be configured as well:
  207. (++) TX and/or RX pin level inversion
  208. (++) data logical level inversion
  209. (++) RX and TX pins swap
  210. (++) RX overrun detection disabling
  211. (++) DMA disabling on RX error
  212. (++) MSB first on communication line
  213. (++) auto Baud rate detection
  214. [..]
  215. The HAL_UART_Init(), HAL_HalfDuplex_Init() and HAL_MultiProcessor_Init()
  216. API follow respectively the UART asynchronous, UART Half duplex and multiprocessor mode
  217. configuration procedures (details for the procedures are available in reference manual).
  218. @endverbatim
  219. * @{
  220. */
  221. /*
  222. Additional Table: If the parity is enabled, then the MSB bit of the data written
  223. in the data register is transmitted but is changed by the parity bit.
  224. According to device capability (support or not of 7-bit word length),
  225. frame length is either defined by the M bit (8-bits or 9-bits)
  226. or by the M1 and M0 bits (7-bit, 8-bit or 9-bit).
  227. Possible UART frame formats are as listed in the following table:
  228. Table 1. UART frame format.
  229. +-----------------------------------------------------------------------+
  230. | M bit | PCE bit | UART frame |
  231. |-------------------|-----------|---------------------------------------|
  232. | 0 | 0 | | SB | 8-bit data | STB | |
  233. |-------------------|-----------|---------------------------------------|
  234. | 0 | 1 | | SB | 7-bit data | PB | STB | |
  235. |-------------------|-----------|---------------------------------------|
  236. | 1 | 0 | | SB | 9-bit data | STB | |
  237. |-------------------|-----------|---------------------------------------|
  238. | 1 | 1 | | SB | 8-bit data | PB | STB | |
  239. +-----------------------------------------------------------------------+
  240. | M1 bit | M0 bit | PCE bit | UART frame |
  241. |---------|---------|-----------|---------------------------------------|
  242. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  243. |---------|---------|-----------|---------------------------------------|
  244. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  245. |---------|---------|-----------|---------------------------------------|
  246. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  247. |---------|---------|-----------|---------------------------------------|
  248. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  249. |---------|---------|-----------|---------------------------------------|
  250. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  251. |---------|---------|-----------|---------------------------------------|
  252. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  253. +-----------------------------------------------------------------------+
  254. */
  255. /**
  256. * @brief Initialize the UART mode according to the specified
  257. * parameters in the UART_InitTypeDef and initialize the associated handle.
  258. * @param huart UART handle.
  259. * @retval HAL status
  260. */
  261. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  262. {
  263. /* Check the UART handle allocation */
  264. if(huart == NULL)
  265. {
  266. return HAL_ERROR;
  267. }
  268. if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  269. {
  270. /* Check the parameters */
  271. assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  272. }
  273. else
  274. {
  275. /* Check the parameters */
  276. assert_param(IS_UART_INSTANCE(huart->Instance));
  277. }
  278. if(huart->gState == HAL_UART_STATE_RESET)
  279. {
  280. /* Allocate lock resource and initialize it */
  281. huart->Lock = HAL_UNLOCKED;
  282. /* Init the low level hardware : GPIO, CLOCK */
  283. HAL_UART_MspInit(huart);
  284. }
  285. huart->gState = HAL_UART_STATE_BUSY;
  286. /* Disable the Peripheral */
  287. __HAL_UART_DISABLE(huart);
  288. /* Set the UART Communication parameters */
  289. if (UART_SetConfig(huart) == HAL_ERROR)
  290. {
  291. return HAL_ERROR;
  292. }
  293. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  294. {
  295. UART_AdvFeatureConfig(huart);
  296. }
  297. /* In asynchronous mode, the following bits must be kept cleared:
  298. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  299. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  300. #if defined (USART_CR2_LINEN)
  301. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  302. #else
  303. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  304. #endif
  305. #if defined (USART_CR3_SCEN)
  306. #if defined (USART_CR3_IREN)
  307. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  308. #else
  309. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  310. #endif
  311. #else
  312. #if defined (USART_CR3_IREN)
  313. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  314. #else
  315. CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  316. #endif
  317. #endif
  318. /* Enable the Peripheral */
  319. __HAL_UART_ENABLE(huart);
  320. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  321. return (UART_CheckIdleState(huart));
  322. }
  323. /**
  324. * @brief Initialize the half-duplex mode according to the specified
  325. * parameters in the UART_InitTypeDef and creates the associated handle.
  326. * @param huart UART handle.
  327. * @retval HAL status
  328. */
  329. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  330. {
  331. /* Check the UART handle allocation */
  332. if(huart == NULL)
  333. {
  334. return HAL_ERROR;
  335. }
  336. /* Check UART instance */
  337. assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  338. if(huart->gState == HAL_UART_STATE_RESET)
  339. {
  340. /* Allocate lock resource and initialize it */
  341. huart->Lock = HAL_UNLOCKED;
  342. /* Init the low level hardware : GPIO, CLOCK */
  343. HAL_UART_MspInit(huart);
  344. }
  345. huart->gState = HAL_UART_STATE_BUSY;
  346. /* Disable the Peripheral */
  347. __HAL_UART_DISABLE(huart);
  348. /* Set the UART Communication parameters */
  349. if (UART_SetConfig(huart) == HAL_ERROR)
  350. {
  351. return HAL_ERROR;
  352. }
  353. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  354. {
  355. UART_AdvFeatureConfig(huart);
  356. }
  357. /* In half-duplex mode, the following bits must be kept cleared:
  358. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  359. - SCEN (if Smartcard is supported), and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  360. #if defined (USART_CR2_LINEN)
  361. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  362. #else
  363. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  364. #endif
  365. #if defined (USART_CR3_SCEN)
  366. #if defined (USART_CR3_IREN)
  367. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  368. #else
  369. CLEAR_BIT(huart->Instance->CR3, USART_CR3_SCEN);
  370. #endif
  371. #else
  372. #if defined (USART_CR3_IREN)
  373. CLEAR_BIT(huart->Instance->CR3, USART_CR3_IREN);
  374. #endif
  375. #endif
  376. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  377. SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  378. /* Enable the Peripheral */
  379. __HAL_UART_ENABLE(huart);
  380. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  381. return (UART_CheckIdleState(huart));
  382. }
  383. /**
  384. * @brief Initialize the multiprocessor mode according to the specified
  385. * parameters in the UART_InitTypeDef and initialize the associated handle.
  386. * @param huart UART handle.
  387. * @param Address UART node address (4-, 6-, 7- or 8-bit long).
  388. * @param WakeUpMethod specifies the UART wakeup method.
  389. * This parameter can be one of the following values:
  390. * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
  391. * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
  392. * @note If the user resorts to idle line detection wake up, the Address parameter
  393. * is useless and ignored by the initialization function.
  394. * @note If the user resorts to address mark wake up, the address length detection
  395. * is configured by default to 4 bits only. For the UART to be able to
  396. * manage 6-, 7- or 8-bit long addresses detection, the API
  397. * HAL_MultiProcessorEx_AddressLength_Set() must be called after
  398. * HAL_MultiProcessor_Init().
  399. * @retval HAL status
  400. */
  401. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  402. {
  403. /* Check the UART handle allocation */
  404. if(huart == NULL)
  405. {
  406. return HAL_ERROR;
  407. }
  408. /* Check the wake up method parameter */
  409. assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  410. if(huart->gState == HAL_UART_STATE_RESET)
  411. {
  412. /* Allocate lock resource and initialize it */
  413. huart->Lock = HAL_UNLOCKED;
  414. /* Init the low level hardware : GPIO, CLOCK */
  415. HAL_UART_MspInit(huart);
  416. }
  417. huart->gState = HAL_UART_STATE_BUSY;
  418. /* Disable the Peripheral */
  419. __HAL_UART_DISABLE(huart);
  420. /* Set the UART Communication parameters */
  421. if (UART_SetConfig(huart) == HAL_ERROR)
  422. {
  423. return HAL_ERROR;
  424. }
  425. if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  426. {
  427. UART_AdvFeatureConfig(huart);
  428. }
  429. /* In multiprocessor mode, the following bits must be kept cleared:
  430. - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  431. - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  432. #if defined (USART_CR2_LINEN)
  433. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  434. #else
  435. CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  436. #endif
  437. #if defined (USART_CR3_SCEN)
  438. #if defined (USART_CR3_IREN)
  439. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  440. #else
  441. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  442. #endif
  443. #else
  444. #if defined (USART_CR3_IREN)
  445. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  446. #else
  447. CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  448. #endif
  449. #endif
  450. if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  451. {
  452. /* If address mark wake up method is chosen, set the USART address node */
  453. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  454. }
  455. /* Set the wake up method by setting the WAKE bit in the CR1 register */
  456. MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  457. /* Enable the Peripheral */
  458. __HAL_UART_ENABLE(huart);
  459. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  460. return (UART_CheckIdleState(huart));
  461. }
  462. /**
  463. * @brief DeInitialize the UART peripheral.
  464. * @param huart UART handle.
  465. * @retval HAL status
  466. */
  467. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  468. {
  469. /* Check the UART handle allocation */
  470. if(huart == NULL)
  471. {
  472. return HAL_ERROR;
  473. }
  474. /* Check the parameters */
  475. assert_param(IS_UART_INSTANCE(huart->Instance));
  476. huart->gState = HAL_UART_STATE_BUSY;
  477. /* Disable the Peripheral */
  478. __HAL_UART_DISABLE(huart);
  479. huart->Instance->CR1 = 0x0U;
  480. huart->Instance->CR2 = 0x0U;
  481. huart->Instance->CR3 = 0x0U;
  482. /* DeInit the low level hardware */
  483. HAL_UART_MspDeInit(huart);
  484. huart->ErrorCode = HAL_UART_ERROR_NONE;
  485. huart->gState = HAL_UART_STATE_RESET;
  486. huart->RxState = HAL_UART_STATE_RESET;
  487. /* Process Unlock */
  488. __HAL_UNLOCK(huart);
  489. return HAL_OK;
  490. }
  491. /**
  492. * @brief Initialize the UART MSP.
  493. * @param huart UART handle.
  494. * @retval None
  495. */
  496. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  497. {
  498. /* Prevent unused argument(s) compilation warning */
  499. UNUSED(huart);
  500. /* NOTE : This function should not be modified, when the callback is needed,
  501. the HAL_UART_MspInit can be implemented in the user file
  502. */
  503. }
  504. /**
  505. * @brief DeInitialize the UART MSP.
  506. * @param huart UART handle.
  507. * @retval None
  508. */
  509. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  510. {
  511. /* Prevent unused argument(s) compilation warning */
  512. UNUSED(huart);
  513. /* NOTE : This function should not be modified, when the callback is needed,
  514. the HAL_UART_MspDeInit can be implemented in the user file
  515. */
  516. }
  517. /**
  518. * @}
  519. */
  520. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  521. * @brief UART Transmit/Receive functions
  522. *
  523. @verbatim
  524. ===============================================================================
  525. ##### IO operation functions #####
  526. ===============================================================================
  527. This subsection provides a set of functions allowing to manage the UART asynchronous
  528. and Half duplex data transfers.
  529. (#) There are two mode of transfer:
  530. (++) Blocking mode: The communication is performed in polling mode.
  531. The HAL status of all data processing is returned by the same function
  532. after finishing transfer.
  533. (++) Non-Blocking mode: The communication is performed using Interrupts
  534. or DMA, These API's return the HAL status.
  535. The end of the data processing will be indicated through the
  536. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  537. using DMA mode.
  538. The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  539. will be executed respectively at the end of the transmit or Receive process
  540. The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  541. (#) Blocking mode API's are :
  542. (++) HAL_UART_Transmit()
  543. (++) HAL_UART_Receive()
  544. (#) Non-Blocking mode API's with Interrupt are :
  545. (++) HAL_UART_Transmit_IT()
  546. (++) HAL_UART_Receive_IT()
  547. (++) HAL_UART_IRQHandler()
  548. (#) Non-Blocking mode API's with DMA are :
  549. (++) HAL_UART_Transmit_DMA()
  550. (++) HAL_UART_Receive_DMA()
  551. (++) HAL_UART_DMAPause()
  552. (++) HAL_UART_DMAResume()
  553. (++) HAL_UART_DMAStop()
  554. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  555. (++) HAL_UART_TxHalfCpltCallback()
  556. (++) HAL_UART_TxCpltCallback()
  557. (++) HAL_UART_RxHalfCpltCallback()
  558. (++) HAL_UART_RxCpltCallback()
  559. (++) HAL_UART_ErrorCallback()
  560. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  561. (++) HAL_UART_Abort()
  562. (++) HAL_UART_AbortTransmit()
  563. (++) HAL_UART_AbortReceive()
  564. (++) HAL_UART_Abort_IT()
  565. (++) HAL_UART_AbortTransmit_IT()
  566. (++) HAL_UART_AbortReceive_IT()
  567. (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  568. (++) HAL_UART_AbortCpltCallback()
  569. (++) HAL_UART_AbortTransmitCpltCallback()
  570. (++) HAL_UART_AbortReceiveCpltCallback()
  571. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  572. Errors are handled as follows :
  573. (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  574. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  575. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  576. and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
  577. If user wants to abort it, Abort services should be called by user.
  578. (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  579. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  580. Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
  581. -@- In the Half duplex communication, it is forbidden to run the transmit
  582. and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  583. @endverbatim
  584. * @{
  585. */
  586. /**
  587. * @brief Send an amount of data in blocking mode.
  588. * @param huart UART handle.
  589. * @param pData Pointer to data buffer.
  590. * @param Size Amount of data to be sent.
  591. * @param Timeout Timeout duration.
  592. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  593. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  594. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  595. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  596. * @retval HAL status
  597. */
  598. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  599. {
  600. uint16_t* tmp;
  601. uint32_t tickstart = 0U;
  602. /* Check that a Tx process is not already ongoing */
  603. if(huart->gState == HAL_UART_STATE_READY)
  604. {
  605. if((pData == NULL ) || (Size == 0U))
  606. {
  607. return HAL_ERROR;
  608. }
  609. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  610. should be aligned on a u16 frontier, as data to be filled into TDR will be
  611. handled through a u16 cast. */
  612. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  613. {
  614. if((((uint32_t)pData)&1U) != 0U)
  615. {
  616. return HAL_ERROR;
  617. }
  618. }
  619. /* Process Locked */
  620. __HAL_LOCK(huart);
  621. huart->ErrorCode = HAL_UART_ERROR_NONE;
  622. huart->gState = HAL_UART_STATE_BUSY_TX;
  623. /* Init tickstart for timeout managment*/
  624. tickstart = HAL_GetTick();
  625. huart->TxXferSize = Size;
  626. huart->TxXferCount = Size;
  627. while(huart->TxXferCount > 0)
  628. {
  629. huart->TxXferCount--;
  630. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  631. {
  632. return HAL_TIMEOUT;
  633. }
  634. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  635. {
  636. tmp = (uint16_t*) pData;
  637. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  638. pData += 2;
  639. }
  640. else
  641. {
  642. huart->Instance->TDR = (*pData++ & (uint8_t)0xFFU);
  643. }
  644. }
  645. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  646. {
  647. return HAL_TIMEOUT;
  648. }
  649. /* At end of Tx process, restore huart->gState to Ready */
  650. huart->gState = HAL_UART_STATE_READY;
  651. /* Process Unlocked */
  652. __HAL_UNLOCK(huart);
  653. return HAL_OK;
  654. }
  655. else
  656. {
  657. return HAL_BUSY;
  658. }
  659. }
  660. /**
  661. * @brief Receive an amount of data in blocking mode.
  662. * @param huart UART handle.
  663. * @param pData pointer to data buffer.
  664. * @param Size amount of data to be received.
  665. * @param Timeout Timeout duration.
  666. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  667. * address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  668. * (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  669. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  670. * @retval HAL status
  671. */
  672. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  673. {
  674. uint16_t* tmp;
  675. uint16_t uhMask;
  676. uint32_t tickstart = 0;
  677. /* Check that a Rx process is not already ongoing */
  678. if(huart->RxState == HAL_UART_STATE_READY)
  679. {
  680. if((pData == NULL ) || (Size == 0U))
  681. {
  682. return HAL_ERROR;
  683. }
  684. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  685. should be aligned on a u16 frontier, as data to be received from RDR will be
  686. handled through a u16 cast. */
  687. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  688. {
  689. if((((uint32_t)pData)&1U) != 0U)
  690. {
  691. return HAL_ERROR;
  692. }
  693. }
  694. /* Process Locked */
  695. __HAL_LOCK(huart);
  696. huart->ErrorCode = HAL_UART_ERROR_NONE;
  697. huart->RxState = HAL_UART_STATE_BUSY_RX;
  698. /* Init tickstart for timeout managment*/
  699. tickstart = HAL_GetTick();
  700. huart->RxXferSize = Size;
  701. huart->RxXferCount = Size;
  702. /* Computation of UART mask to apply to RDR register */
  703. UART_MASK_COMPUTATION(huart);
  704. uhMask = huart->Mask;
  705. /* as long as data have to be received */
  706. while(huart->RxXferCount > 0U)
  707. {
  708. huart->RxXferCount--;
  709. if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  710. {
  711. return HAL_TIMEOUT;
  712. }
  713. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  714. {
  715. tmp = (uint16_t*) pData ;
  716. *tmp = (uint16_t)(huart->Instance->RDR & uhMask);
  717. pData +=2U;
  718. }
  719. else
  720. {
  721. *pData++ = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  722. }
  723. }
  724. /* At end of Rx process, restore huart->RxState to Ready */
  725. huart->RxState = HAL_UART_STATE_READY;
  726. /* Process Unlocked */
  727. __HAL_UNLOCK(huart);
  728. return HAL_OK;
  729. }
  730. else
  731. {
  732. return HAL_BUSY;
  733. }
  734. }
  735. /**
  736. * @brief Send an amount of data in interrupt mode.
  737. * @param huart UART handle.
  738. * @param pData pointer to data buffer.
  739. * @param Size amount of data to be sent.
  740. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  741. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  742. * (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  743. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  744. * @retval HAL status
  745. */
  746. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  747. {
  748. /* Check that a Tx process is not already ongoing */
  749. if(huart->gState == HAL_UART_STATE_READY)
  750. {
  751. if((pData == NULL ) || (Size == 0U))
  752. {
  753. return HAL_ERROR;
  754. }
  755. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  756. should be aligned on a u16 frontier, as data to be filled into TDR will be
  757. handled through a u16 cast. */
  758. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  759. {
  760. if((((uint32_t)pData)&1U) != 0U)
  761. {
  762. return HAL_ERROR;
  763. }
  764. }
  765. /* Process Locked */
  766. __HAL_LOCK(huart);
  767. huart->pTxBuffPtr = pData;
  768. huart->TxXferSize = Size;
  769. huart->TxXferCount = Size;
  770. huart->ErrorCode = HAL_UART_ERROR_NONE;
  771. huart->gState = HAL_UART_STATE_BUSY_TX;
  772. /* Process Unlocked */
  773. __HAL_UNLOCK(huart);
  774. /* Enable the UART Transmit Data Register Empty Interrupt */
  775. SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  776. return HAL_OK;
  777. }
  778. else
  779. {
  780. return HAL_BUSY;
  781. }
  782. }
  783. /**
  784. * @brief Receive an amount of data in interrupt mode.
  785. * @param huart UART handle.
  786. * @param pData pointer to data buffer.
  787. * @param Size amount of data to be received.
  788. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  789. * address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  790. * (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  791. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  792. * @retval HAL status
  793. */
  794. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  795. {
  796. /* Check that a Rx process is not already ongoing */
  797. if(huart->RxState == HAL_UART_STATE_READY)
  798. {
  799. if((pData == NULL ) || (Size == 0U))
  800. {
  801. return HAL_ERROR;
  802. }
  803. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  804. should be aligned on a u16 frontier, as data to be received from RDR will be
  805. handled through a u16 cast. */
  806. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  807. {
  808. if((((uint32_t)pData)&1U) != 0U)
  809. {
  810. return HAL_ERROR;
  811. }
  812. }
  813. /* Process Locked */
  814. __HAL_LOCK(huart);
  815. huart->pRxBuffPtr = pData;
  816. huart->RxXferSize = Size;
  817. huart->RxXferCount = Size;
  818. /* Computation of UART mask to apply to RDR register */
  819. UART_MASK_COMPUTATION(huart);
  820. huart->ErrorCode = HAL_UART_ERROR_NONE;
  821. huart->RxState = HAL_UART_STATE_BUSY_RX;
  822. /* Process Unlocked */
  823. __HAL_UNLOCK(huart);
  824. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  825. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  826. /* Enable the UART Parity Error and Data Register not empty Interrupts */
  827. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  828. return HAL_OK;
  829. }
  830. else
  831. {
  832. return HAL_BUSY;
  833. }
  834. }
  835. /**
  836. * @brief Send an amount of data in DMA mode.
  837. * @param huart UART handle.
  838. * @param pData pointer to data buffer.
  839. * @param Size amount of data to be sent.
  840. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  841. * address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  842. * (as sent data will be handled by DMA from halfword frontier). Depending on compilation chain,
  843. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  844. * @retval HAL status
  845. */
  846. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  847. {
  848. /* Check that a Tx process is not already ongoing */
  849. if(huart->gState == HAL_UART_STATE_READY)
  850. {
  851. if((pData == NULL ) || (Size == 0U))
  852. {
  853. return HAL_ERROR;
  854. }
  855. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  856. should be aligned on a u16 frontier, as data copy into TDR will be
  857. handled by DMA from a u16 frontier. */
  858. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  859. {
  860. if((((uint32_t)pData)&1U) != 0U)
  861. {
  862. return HAL_ERROR;
  863. }
  864. }
  865. /* Process Locked */
  866. __HAL_LOCK(huart);
  867. huart->pTxBuffPtr = pData;
  868. huart->TxXferSize = Size;
  869. huart->TxXferCount = Size;
  870. huart->ErrorCode = HAL_UART_ERROR_NONE;
  871. huart->gState = HAL_UART_STATE_BUSY_TX;
  872. /* Set the UART DMA transfer complete callback */
  873. huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  874. /* Set the UART DMA Half transfer complete callback */
  875. huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  876. /* Set the DMA error callback */
  877. huart->hdmatx->XferErrorCallback = UART_DMAError;
  878. /* Set the DMA abort callback */
  879. huart->hdmatx->XferAbortCallback = NULL;
  880. /* Enable the UART transmit DMA channel */
  881. HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size);
  882. /* Clear the TC flag in the ICR register */
  883. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
  884. /* Process Unlocked */
  885. __HAL_UNLOCK(huart);
  886. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  887. in the UART CR3 register */
  888. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  889. return HAL_OK;
  890. }
  891. else
  892. {
  893. return HAL_BUSY;
  894. }
  895. }
  896. /**
  897. * @brief Receive an amount of data in DMA mode.
  898. * @param huart UART handle.
  899. * @param pData pointer to data buffer.
  900. * @param Size amount of data to be received.
  901. * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  902. * address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  903. * (as received data will be handled by DMA from halfword frontier). Depending on compilation chain,
  904. * use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  905. * @retval HAL status
  906. */
  907. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  908. {
  909. /* Check that a Rx process is not already ongoing */
  910. if(huart->RxState == HAL_UART_STATE_READY)
  911. {
  912. if((pData == NULL ) || (Size == 0U))
  913. {
  914. return HAL_ERROR;
  915. }
  916. /* In case of 9bits/No Parity transfer, pData buffer provided as input paramter
  917. should be aligned on a u16 frontier, as data copy from RDR will be
  918. handled by DMA from a u16 frontier. */
  919. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  920. {
  921. if((((uint32_t)pData)&1U) != 0U)
  922. {
  923. return HAL_ERROR;
  924. }
  925. }
  926. /* Process Locked */
  927. __HAL_LOCK(huart);
  928. huart->pRxBuffPtr = pData;
  929. huart->RxXferSize = Size;
  930. huart->ErrorCode = HAL_UART_ERROR_NONE;
  931. huart->RxState = HAL_UART_STATE_BUSY_RX;
  932. /* Set the UART DMA transfer complete callback */
  933. huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  934. /* Set the UART DMA Half transfer complete callback */
  935. huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  936. /* Set the DMA error callback */
  937. huart->hdmarx->XferErrorCallback = UART_DMAError;
  938. /* Set the DMA abort callback */
  939. huart->hdmarx->XferAbortCallback = NULL;
  940. /* Enable the DMA channel */
  941. HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size);
  942. /* Process Unlocked */
  943. __HAL_UNLOCK(huart);
  944. /* Enable the UART Parity Error Interrupt */
  945. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  946. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  947. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  948. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  949. in the UART CR3 register */
  950. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  951. return HAL_OK;
  952. }
  953. else
  954. {
  955. return HAL_BUSY;
  956. }
  957. }
  958. /**
  959. * @brief Pause the DMA Transfer.
  960. * @param huart UART handle.
  961. * @retval HAL status
  962. */
  963. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  964. {
  965. /* Process Locked */
  966. __HAL_LOCK(huart);
  967. if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
  968. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
  969. {
  970. /* Disable the UART DMA Tx request */
  971. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  972. }
  973. if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
  974. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  975. {
  976. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  977. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  978. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  979. /* Disable the UART DMA Rx request */
  980. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  981. }
  982. /* Process Unlocked */
  983. __HAL_UNLOCK(huart);
  984. return HAL_OK;
  985. }
  986. /**
  987. * @brief Resume the DMA Transfer.
  988. * @param huart UART handle.
  989. * @retval HAL status
  990. */
  991. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  992. {
  993. /* Process Locked */
  994. __HAL_LOCK(huart);
  995. if(huart->gState == HAL_UART_STATE_BUSY_TX)
  996. {
  997. /* Enable the UART DMA Tx request */
  998. SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  999. }
  1000. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  1001. {
  1002. /* Clear the Overrun flag before resuming the Rx transfer */
  1003. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1004. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1005. SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1006. SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1007. /* Enable the UART DMA Rx request */
  1008. SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1009. }
  1010. /* Process Unlocked */
  1011. __HAL_UNLOCK(huart);
  1012. return HAL_OK;
  1013. }
  1014. /**
  1015. * @brief Stop the DMA Transfer.
  1016. * @param huart UART handle.
  1017. * @retval HAL status
  1018. */
  1019. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  1020. {
  1021. /* The Lock is not implemented on this API to allow the user application
  1022. to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  1023. HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
  1024. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1025. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1026. the stream and the corresponding call back is executed. */
  1027. /* Stop UART DMA Tx request if ongoing */
  1028. if ((huart->gState == HAL_UART_STATE_BUSY_TX) &&
  1029. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)))
  1030. {
  1031. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1032. /* Abort the UART DMA Tx channel */
  1033. if(huart->hdmatx != NULL)
  1034. {
  1035. HAL_DMA_Abort(huart->hdmatx);
  1036. }
  1037. UART_EndTxTransfer(huart);
  1038. }
  1039. /* Stop UART DMA Rx request if ongoing */
  1040. if ((huart->RxState == HAL_UART_STATE_BUSY_RX) &&
  1041. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  1042. {
  1043. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1044. /* Abort the UART DMA Rx channel */
  1045. if(huart->hdmarx != NULL)
  1046. {
  1047. HAL_DMA_Abort(huart->hdmarx);
  1048. }
  1049. UART_EndRxTransfer(huart);
  1050. }
  1051. return HAL_OK;
  1052. }
  1053. /**
  1054. * @brief Abort ongoing transfers (blocking mode).
  1055. * @param huart UART handle.
  1056. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1057. * This procedure performs following operations :
  1058. * - Disable UART Interrupts (Tx and Rx)
  1059. * - Disable the DMA transfer in the peripheral register (if enabled)
  1060. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1061. * - Set handle State to READY
  1062. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1063. * @retval HAL status
  1064. */
  1065. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  1066. {
  1067. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1068. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1069. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1070. /* Disable the UART DMA Tx request if enabled */
  1071. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1072. {
  1073. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1074. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1075. if(huart->hdmatx != NULL)
  1076. {
  1077. /* Set the UART DMA Abort callback to Null.
  1078. No call back execution at end of DMA abort procedure */
  1079. huart->hdmatx->XferAbortCallback = NULL;
  1080. HAL_DMA_Abort(huart->hdmatx);
  1081. }
  1082. }
  1083. /* Disable the UART DMA Rx request if enabled */
  1084. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1085. {
  1086. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1087. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1088. if(huart->hdmarx != NULL)
  1089. {
  1090. /* Set the UART DMA Abort callback to Null.
  1091. No call back execution at end of DMA abort procedure */
  1092. huart->hdmarx->XferAbortCallback = NULL;
  1093. HAL_DMA_Abort(huart->hdmarx);
  1094. }
  1095. }
  1096. /* Reset Tx and Rx transfer counters */
  1097. huart->TxXferCount = 0U;
  1098. huart->RxXferCount = 0U;
  1099. /* Clear the Error flags in the ICR register */
  1100. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1101. /* Restore huart->gState and huart->RxState to Ready */
  1102. huart->gState = HAL_UART_STATE_READY;
  1103. huart->RxState = HAL_UART_STATE_READY;
  1104. /* Reset Handle ErrorCode to No Error */
  1105. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1106. return HAL_OK;
  1107. }
  1108. /**
  1109. * @brief Abort ongoing Transmit transfer (blocking mode).
  1110. * @param huart UART handle.
  1111. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1112. * This procedure performs following operations :
  1113. * - Disable UART Interrupts (Tx)
  1114. * - Disable the DMA transfer in the peripheral register (if enabled)
  1115. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1116. * - Set handle State to READY
  1117. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1118. * @retval HAL status
  1119. */
  1120. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
  1121. {
  1122. /* Disable TXEIE and TCIE interrupts */
  1123. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1124. /* Disable the UART DMA Tx request if enabled */
  1125. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1126. {
  1127. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1128. /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1129. if(huart->hdmatx != NULL)
  1130. {
  1131. /* Set the UART DMA Abort callback to Null.
  1132. No call back execution at end of DMA abort procedure */
  1133. huart->hdmatx->XferAbortCallback = NULL;
  1134. HAL_DMA_Abort(huart->hdmatx);
  1135. }
  1136. }
  1137. /* Reset Tx transfer counter */
  1138. huart->TxXferCount = 0U;
  1139. /* Restore huart->gState to Ready */
  1140. huart->gState = HAL_UART_STATE_READY;
  1141. return HAL_OK;
  1142. }
  1143. /**
  1144. * @brief Abort ongoing Receive transfer (blocking mode).
  1145. * @param huart UART handle.
  1146. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1147. * This procedure performs following operations :
  1148. * - Disable UART Interrupts (Rx)
  1149. * - Disable the DMA transfer in the peripheral register (if enabled)
  1150. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1151. * - Set handle State to READY
  1152. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1153. * @retval HAL status
  1154. */
  1155. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
  1156. {
  1157. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1158. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1159. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1160. /* Disable the UART DMA Rx request if enabled */
  1161. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1162. {
  1163. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1164. /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1165. if(huart->hdmarx != NULL)
  1166. {
  1167. /* Set the UART DMA Abort callback to Null.
  1168. No call back execution at end of DMA abort procedure */
  1169. huart->hdmarx->XferAbortCallback = NULL;
  1170. HAL_DMA_Abort(huart->hdmarx);
  1171. }
  1172. }
  1173. /* Reset Rx transfer counter */
  1174. huart->RxXferCount = 0U;
  1175. /* Clear the Error flags in the ICR register */
  1176. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1177. /* Restore huart->RxState to Ready */
  1178. huart->RxState = HAL_UART_STATE_READY;
  1179. return HAL_OK;
  1180. }
  1181. /**
  1182. * @brief Abort ongoing transfers (Interrupt mode).
  1183. * @param huart UART handle.
  1184. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1185. * This procedure performs following operations :
  1186. * - Disable UART Interrupts (Tx and Rx)
  1187. * - Disable the DMA transfer in the peripheral register (if enabled)
  1188. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1189. * - Set handle State to READY
  1190. * - At abort completion, call user abort complete callback
  1191. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1192. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
  1196. {
  1197. uint32_t abortcplt = 1U;
  1198. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1199. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1200. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1201. /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
  1202. before any call to DMA Abort functions */
  1203. /* DMA Tx Handle is valid */
  1204. if(huart->hdmatx != NULL)
  1205. {
  1206. /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  1207. Otherwise, set it to NULL */
  1208. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1209. {
  1210. huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
  1211. }
  1212. else
  1213. {
  1214. huart->hdmatx->XferAbortCallback = NULL;
  1215. }
  1216. }
  1217. /* DMA Rx Handle is valid */
  1218. if(huart->hdmarx != NULL)
  1219. {
  1220. /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  1221. Otherwise, set it to NULL */
  1222. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1223. {
  1224. huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
  1225. }
  1226. else
  1227. {
  1228. huart->hdmarx->XferAbortCallback = NULL;
  1229. }
  1230. }
  1231. /* Disable the UART DMA Tx request if enabled */
  1232. if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1233. {
  1234. /* Disable DMA Tx at UART level */
  1235. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1236. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1237. if(huart->hdmatx != NULL)
  1238. {
  1239. /* UART Tx DMA Abort callback has already been initialised :
  1240. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1241. /* Abort DMA TX */
  1242. if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1243. {
  1244. huart->hdmatx->XferAbortCallback = NULL;
  1245. }
  1246. else
  1247. {
  1248. abortcplt = 0U;
  1249. }
  1250. }
  1251. }
  1252. /* Disable the UART DMA Rx request if enabled */
  1253. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1254. {
  1255. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1256. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1257. if(huart->hdmarx != NULL)
  1258. {
  1259. /* UART Rx DMA Abort callback has already been initialised :
  1260. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1261. /* Abort DMA RX */
  1262. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1263. {
  1264. huart->hdmarx->XferAbortCallback = NULL;
  1265. abortcplt = 1U;
  1266. }
  1267. else
  1268. {
  1269. abortcplt = 0U;
  1270. }
  1271. }
  1272. }
  1273. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1274. if (abortcplt == 1U)
  1275. {
  1276. /* Reset Tx and Rx transfer counters */
  1277. huart->TxXferCount = 0U;
  1278. huart->RxXferCount = 0U;
  1279. /* Reset errorCode */
  1280. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1281. /* Clear the Error flags in the ICR register */
  1282. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1283. /* Restore huart->gState and huart->RxState to Ready */
  1284. huart->gState = HAL_UART_STATE_READY;
  1285. huart->RxState = HAL_UART_STATE_READY;
  1286. /* As no DMA to be aborted, call directly user Abort complete callback */
  1287. HAL_UART_AbortCpltCallback(huart);
  1288. }
  1289. return HAL_OK;
  1290. }
  1291. /**
  1292. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1293. * @param huart UART handle.
  1294. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1295. * This procedure performs following operations :
  1296. * - Disable UART Interrupts (Tx)
  1297. * - Disable the DMA transfer in the peripheral register (if enabled)
  1298. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1299. * - Set handle State to READY
  1300. * - At abort completion, call user abort complete callback
  1301. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1302. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1303. * @retval HAL status
  1304. */
  1305. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
  1306. {
  1307. /* Disable TXEIE and TCIE interrupts */
  1308. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1309. /* Disable the UART DMA Tx request if enabled */
  1310. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1311. {
  1312. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1313. /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1314. if(huart->hdmatx != NULL)
  1315. {
  1316. /* Set the UART DMA Abort callback :
  1317. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1318. huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
  1319. /* Abort DMA TX */
  1320. if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1321. {
  1322. /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
  1323. huart->hdmatx->XferAbortCallback(huart->hdmatx);
  1324. }
  1325. }
  1326. else
  1327. {
  1328. /* Reset Tx transfer counter */
  1329. huart->TxXferCount = 0U;
  1330. /* Restore huart->gState to Ready */
  1331. huart->gState = HAL_UART_STATE_READY;
  1332. /* As no DMA to be aborted, call directly user Abort complete callback */
  1333. HAL_UART_AbortTransmitCpltCallback(huart);
  1334. }
  1335. }
  1336. else
  1337. {
  1338. /* Reset Tx transfer counter */
  1339. huart->TxXferCount = 0U;
  1340. /* Restore huart->gState to Ready */
  1341. huart->gState = HAL_UART_STATE_READY;
  1342. /* As no DMA to be aborted, call directly user Abort complete callback */
  1343. HAL_UART_AbortTransmitCpltCallback(huart);
  1344. }
  1345. return HAL_OK;
  1346. }
  1347. /**
  1348. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1349. * @param huart UART handle.
  1350. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1351. * This procedure performs following operations :
  1352. * - Disable UART Interrupts (Rx)
  1353. * - Disable the DMA transfer in the peripheral register (if enabled)
  1354. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1355. * - Set handle State to READY
  1356. * - At abort completion, call user abort complete callback
  1357. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1358. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1359. * @retval HAL status
  1360. */
  1361. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
  1362. {
  1363. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1364. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1365. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1366. /* Disable the UART DMA Rx request if enabled */
  1367. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1368. {
  1369. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1370. /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1371. if(huart->hdmarx != NULL)
  1372. {
  1373. /* Set the UART DMA Abort callback :
  1374. will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1375. huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
  1376. /* Abort DMA RX */
  1377. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1378. {
  1379. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1380. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1381. }
  1382. }
  1383. else
  1384. {
  1385. /* Reset Rx transfer counter */
  1386. huart->RxXferCount = 0U;
  1387. /* Clear the Error flags in the ICR register */
  1388. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1389. /* Restore huart->RxState to Ready */
  1390. huart->RxState = HAL_UART_STATE_READY;
  1391. /* As no DMA to be aborted, call directly user Abort complete callback */
  1392. HAL_UART_AbortReceiveCpltCallback(huart);
  1393. }
  1394. }
  1395. else
  1396. {
  1397. /* Reset Rx transfer counter */
  1398. huart->RxXferCount = 0U;
  1399. /* Clear the Error flags in the ICR register */
  1400. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1401. /* Restore huart->RxState to Ready */
  1402. huart->RxState = HAL_UART_STATE_READY;
  1403. /* As no DMA to be aborted, call directly user Abort complete callback */
  1404. HAL_UART_AbortReceiveCpltCallback(huart);
  1405. }
  1406. return HAL_OK;
  1407. }
  1408. /**
  1409. * @brief Handle UART interrupt request.
  1410. * @param huart UART handle.
  1411. * @retval None
  1412. */
  1413. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  1414. {
  1415. uint32_t isrflags = READ_REG(huart->Instance->ISR);
  1416. uint32_t cr1its = READ_REG(huart->Instance->CR1);
  1417. uint32_t cr3its;
  1418. uint32_t errorflags;
  1419. /* If no error occurs */
  1420. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  1421. if (errorflags == RESET)
  1422. {
  1423. /* UART in mode Receiver ---------------------------------------------------*/
  1424. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1425. {
  1426. UART_Receive_IT(huart);
  1427. return;
  1428. }
  1429. }
  1430. /* If some errors occur */
  1431. cr3its = READ_REG(huart->Instance->CR3);
  1432. if( (errorflags != RESET)
  1433. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1434. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)) )
  1435. {
  1436. /* UART parity error interrupt occurred -------------------------------------*/
  1437. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1438. {
  1439. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_PEF);
  1440. huart->ErrorCode |= HAL_UART_ERROR_PE;
  1441. }
  1442. /* UART frame error interrupt occurred --------------------------------------*/
  1443. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1444. {
  1445. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_FEF);
  1446. huart->ErrorCode |= HAL_UART_ERROR_FE;
  1447. }
  1448. /* UART noise error interrupt occurred --------------------------------------*/
  1449. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1450. {
  1451. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_NEF);
  1452. huart->ErrorCode |= HAL_UART_ERROR_NE;
  1453. }
  1454. /* UART Over-Run interrupt occurred -----------------------------------------*/
  1455. if(((isrflags & USART_ISR_ORE) != RESET) &&
  1456. (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
  1457. {
  1458. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_OREF);
  1459. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1460. }
  1461. /* Call UART Error Call back function if need be --------------------------*/
  1462. if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  1463. {
  1464. /* UART in mode Receiver ---------------------------------------------------*/
  1465. if(((isrflags & USART_ISR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1466. {
  1467. UART_Receive_IT(huart);
  1468. }
  1469. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1470. consider error as blocking */
  1471. if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) ||
  1472. (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)))
  1473. {
  1474. /* Blocking error : transfer is aborted
  1475. Set the UART state ready to be able to start again the process,
  1476. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1477. UART_EndRxTransfer(huart);
  1478. /* Disable the UART DMA Rx request if enabled */
  1479. if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1480. {
  1481. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1482. /* Abort the UART DMA Rx channel */
  1483. if(huart->hdmarx != NULL)
  1484. {
  1485. /* Set the UART DMA Abort callback :
  1486. will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  1487. huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  1488. /* Abort DMA RX */
  1489. if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  1490. {
  1491. /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  1492. huart->hdmarx->XferAbortCallback(huart->hdmarx);
  1493. }
  1494. }
  1495. else
  1496. {
  1497. /* Call user error callback */
  1498. HAL_UART_ErrorCallback(huart);
  1499. }
  1500. }
  1501. else
  1502. {
  1503. /* Call user error callback */
  1504. HAL_UART_ErrorCallback(huart);
  1505. }
  1506. }
  1507. else
  1508. {
  1509. /* Non Blocking error : transfer could go on.
  1510. Error is notified to user through user error callback */
  1511. HAL_UART_ErrorCallback(huart);
  1512. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1513. }
  1514. }
  1515. return;
  1516. } /* End if some error occurs */
  1517. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  1518. /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
  1519. if(((isrflags & USART_ISR_WUF) != RESET) && ((cr3its & USART_CR3_WUFIE) != RESET))
  1520. {
  1521. __HAL_UART_CLEAR_IT(huart, UART_CLEAR_WUF);
  1522. /* Set the UART state ready to be able to start again the process */
  1523. huart->gState = HAL_UART_STATE_READY;
  1524. huart->RxState = HAL_UART_STATE_READY;
  1525. HAL_UARTEx_WakeupCallback(huart);
  1526. return;
  1527. }
  1528. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  1529. /* UART in mode Transmitter ------------------------------------------------*/
  1530. if(((isrflags & USART_ISR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
  1531. {
  1532. UART_Transmit_IT(huart);
  1533. return;
  1534. }
  1535. /* UART in mode Transmitter (transmission end) -----------------------------*/
  1536. if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1537. {
  1538. UART_EndTransmit_IT(huart);
  1539. return;
  1540. }
  1541. }
  1542. /**
  1543. * @brief Tx Transfer completed callback.
  1544. * @param huart UART handle.
  1545. * @retval None
  1546. */
  1547. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  1548. {
  1549. /* Prevent unused argument(s) compilation warning */
  1550. UNUSED(huart);
  1551. /* NOTE : This function should not be modified, when the callback is needed,
  1552. the HAL_UART_TxCpltCallback can be implemented in the user file.
  1553. */
  1554. }
  1555. /**
  1556. * @brief Tx Half Transfer completed callback.
  1557. * @param huart UART handle.
  1558. * @retval None
  1559. */
  1560. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  1561. {
  1562. /* Prevent unused argument(s) compilation warning */
  1563. UNUSED(huart);
  1564. /* NOTE: This function should not be modified, when the callback is needed,
  1565. the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
  1566. */
  1567. }
  1568. /**
  1569. * @brief Rx Transfer completed callback.
  1570. * @param huart UART handle.
  1571. * @retval None
  1572. */
  1573. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1574. {
  1575. /* Prevent unused argument(s) compilation warning */
  1576. UNUSED(huart);
  1577. /* NOTE : This function should not be modified, when the callback is needed,
  1578. the HAL_UART_RxCpltCallback can be implemented in the user file.
  1579. */
  1580. }
  1581. /**
  1582. * @brief Rx Half Transfer completed callback.
  1583. * @param huart UART handle.
  1584. * @retval None
  1585. */
  1586. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1587. {
  1588. /* Prevent unused argument(s) compilation warning */
  1589. UNUSED(huart);
  1590. /* NOTE: This function should not be modified, when the callback is needed,
  1591. the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
  1592. */
  1593. }
  1594. /**
  1595. * @brief UART error callback.
  1596. * @param huart UART handle.
  1597. * @retval None
  1598. */
  1599. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1600. {
  1601. /* Prevent unused argument(s) compilation warning */
  1602. UNUSED(huart);
  1603. /* NOTE : This function should not be modified, when the callback is needed,
  1604. the HAL_UART_ErrorCallback can be implemented in the user file.
  1605. */
  1606. }
  1607. /**
  1608. * @brief UART Abort Complete callback.
  1609. * @param huart UART handle.
  1610. * @retval None
  1611. */
  1612. __weak void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart)
  1613. {
  1614. /* Prevent unused argument(s) compilation warning */
  1615. UNUSED(huart);
  1616. /* NOTE : This function should not be modified, when the callback is needed,
  1617. the HAL_UART_AbortCpltCallback can be implemented in the user file.
  1618. */
  1619. }
  1620. /**
  1621. * @brief UART Abort Complete callback.
  1622. * @param huart UART handle.
  1623. * @retval None
  1624. */
  1625. __weak void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart)
  1626. {
  1627. /* Prevent unused argument(s) compilation warning */
  1628. UNUSED(huart);
  1629. /* NOTE : This function should not be modified, when the callback is needed,
  1630. the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
  1631. */
  1632. }
  1633. /**
  1634. * @brief UART Abort Receive Complete callback.
  1635. * @param huart UART handle.
  1636. * @retval None
  1637. */
  1638. __weak void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart)
  1639. {
  1640. /* Prevent unused argument(s) compilation warning */
  1641. UNUSED(huart);
  1642. /* NOTE : This function should not be modified, when the callback is needed,
  1643. the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
  1644. */
  1645. }
  1646. /**
  1647. * @}
  1648. */
  1649. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  1650. * @brief UART control functions
  1651. *
  1652. @verbatim
  1653. ===============================================================================
  1654. ##### Peripheral Control functions #####
  1655. ===============================================================================
  1656. [..]
  1657. This subsection provides a set of functions allowing to control the UART.
  1658. (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  1659. (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  1660. (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  1661. (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  1662. (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  1663. @endverbatim
  1664. * @{
  1665. */
  1666. /**
  1667. * @brief Enable UART in mute mode (does not mean UART enters mute mode;
  1668. * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
  1669. * @param huart UART handle.
  1670. * @retval HAL status
  1671. */
  1672. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  1673. {
  1674. /* Process Locked */
  1675. __HAL_LOCK(huart);
  1676. huart->gState = HAL_UART_STATE_BUSY;
  1677. /* Enable USART mute mode by setting the MME bit in the CR1 register */
  1678. SET_BIT(huart->Instance->CR1, USART_CR1_MME);
  1679. huart->gState = HAL_UART_STATE_READY;
  1680. return (UART_CheckIdleState(huart));
  1681. }
  1682. /**
  1683. * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
  1684. * as it may not have been in mute mode at this very moment).
  1685. * @param huart UART handle.
  1686. * @retval HAL status
  1687. */
  1688. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  1689. {
  1690. /* Process Locked */
  1691. __HAL_LOCK(huart);
  1692. huart->gState = HAL_UART_STATE_BUSY;
  1693. /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  1694. CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
  1695. huart->gState = HAL_UART_STATE_READY;
  1696. return (UART_CheckIdleState(huart));
  1697. }
  1698. /**
  1699. * @brief Enter UART mute mode (means UART actually enters mute mode).
  1700. * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  1701. * @param huart UART handle.
  1702. * @retval None
  1703. */
  1704. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1705. {
  1706. __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  1707. }
  1708. /**
  1709. * @brief Enable the UART transmitter and disable the UART receiver.
  1710. * @param huart UART handle.
  1711. * @retval HAL status
  1712. */
  1713. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1714. {
  1715. /* Process Locked */
  1716. __HAL_LOCK(huart);
  1717. huart->gState = HAL_UART_STATE_BUSY;
  1718. /* Clear TE and RE bits */
  1719. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1720. /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1721. SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  1722. huart->gState = HAL_UART_STATE_READY;
  1723. /* Process Unlocked */
  1724. __HAL_UNLOCK(huart);
  1725. return HAL_OK;
  1726. }
  1727. /**
  1728. * @brief Enable the UART receiver and disable the UART transmitter.
  1729. * @param huart UART handle.
  1730. * @retval HAL status.
  1731. */
  1732. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1733. {
  1734. /* Process Locked */
  1735. __HAL_LOCK(huart);
  1736. huart->gState = HAL_UART_STATE_BUSY;
  1737. /* Clear TE and RE bits */
  1738. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  1739. /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1740. SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  1741. huart->gState = HAL_UART_STATE_READY;
  1742. /* Process Unlocked */
  1743. __HAL_UNLOCK(huart);
  1744. return HAL_OK;
  1745. }
  1746. /**
  1747. * @}
  1748. */
  1749. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
  1750. * @brief UART Peripheral State functions
  1751. *
  1752. @verbatim
  1753. ==============================================================================
  1754. ##### Peripheral State and Error functions #####
  1755. ==============================================================================
  1756. [..]
  1757. This subsection provides functions allowing to :
  1758. (+) Return the UART handle state.
  1759. (+) Return the UART handle error code
  1760. @endverbatim
  1761. * @{
  1762. */
  1763. /**
  1764. * @brief Return the UART handle state.
  1765. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  1766. * the configuration information for the specified UART.
  1767. * @retval HAL state
  1768. */
  1769. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1770. {
  1771. uint32_t temp1= 0x00U, temp2 = 0x00U;
  1772. temp1 = huart->gState;
  1773. temp2 = huart->RxState;
  1774. return (HAL_UART_StateTypeDef)(temp1 | temp2);
  1775. }
  1776. /**
  1777. * @brief Return the UART handle error code.
  1778. * @param huart Pointer to a UART_HandleTypeDef structure that contains
  1779. * the configuration information for the specified UART.
  1780. * @retval UART Error Code
  1781. */
  1782. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1783. {
  1784. return huart->ErrorCode;
  1785. }
  1786. /**
  1787. * @}
  1788. */
  1789. /**
  1790. * @}
  1791. */
  1792. /** @defgroup UART_Private_Functions UART Private Functions
  1793. * @{
  1794. */
  1795. /**
  1796. * @brief Configure the UART peripheral.
  1797. * @param huart UART handle.
  1798. * @retval HAL status
  1799. */
  1800. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  1801. {
  1802. uint32_t tmpreg = 0x00000000U;
  1803. UART_ClockSourceTypeDef clocksource = UART_CLOCKSOURCE_UNDEFINED;
  1804. uint16_t brrtemp = 0x0000U;
  1805. uint16_t usartdiv = 0x0000U;
  1806. HAL_StatusTypeDef ret = HAL_OK;
  1807. /* Check the parameters */
  1808. assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  1809. assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  1810. assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1811. assert_param(IS_UART_PARITY(huart->Init.Parity));
  1812. assert_param(IS_UART_MODE(huart->Init.Mode));
  1813. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  1814. assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  1815. assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  1816. /*-------------------------- USART CR1 Configuration -----------------------*/
  1817. /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  1818. * the UART Word Length, Parity, Mode and oversampling:
  1819. * set the M bits according to huart->Init.WordLength value
  1820. * set PCE and PS bits according to huart->Init.Parity value
  1821. * set TE and RE bits according to huart->Init.Mode value
  1822. * set OVER8 bit according to huart->Init.OverSampling value */
  1823. tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  1824. MODIFY_REG(huart->Instance->CR1, UART_CR1_FIELDS, tmpreg);
  1825. /*-------------------------- USART CR2 Configuration -----------------------*/
  1826. /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1827. * to huart->Init.StopBits value */
  1828. MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1829. /*-------------------------- USART CR3 Configuration -----------------------*/
  1830. /* Configure
  1831. * - UART HardWare Flow Control: set CTSE and RTSE bits according
  1832. * to huart->Init.HwFlowCtl value
  1833. * - one-bit sampling method versus three samples' majority rule according
  1834. * to huart->Init.OneBitSampling */
  1835. tmpreg = (uint32_t)huart->Init.HwFlowCtl | huart->Init.OneBitSampling ;
  1836. MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT), tmpreg);
  1837. /*-------------------------- USART BRR Configuration -----------------------*/
  1838. UART_GETCLOCKSOURCE(huart, clocksource);
  1839. /* Check UART Over Sampling to set Baud Rate Register */
  1840. if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  1841. {
  1842. switch (clocksource)
  1843. {
  1844. case UART_CLOCKSOURCE_PCLK1:
  1845. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1846. break;
  1847. case UART_CLOCKSOURCE_HSI:
  1848. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  1849. break;
  1850. case UART_CLOCKSOURCE_SYSCLK:
  1851. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1852. break;
  1853. case UART_CLOCKSOURCE_LSE:
  1854. usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  1855. break;
  1856. case UART_CLOCKSOURCE_UNDEFINED:
  1857. default:
  1858. ret = HAL_ERROR;
  1859. break;
  1860. }
  1861. brrtemp = usartdiv & 0xFFF0U;
  1862. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  1863. huart->Instance->BRR = brrtemp;
  1864. }
  1865. else
  1866. {
  1867. switch (clocksource)
  1868. {
  1869. case UART_CLOCKSOURCE_PCLK1:
  1870. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
  1871. break;
  1872. case UART_CLOCKSOURCE_HSI:
  1873. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  1874. break;
  1875. case UART_CLOCKSOURCE_SYSCLK:
  1876. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
  1877. break;
  1878. case UART_CLOCKSOURCE_LSE:
  1879. huart->Instance->BRR = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  1880. break;
  1881. case UART_CLOCKSOURCE_UNDEFINED:
  1882. default:
  1883. ret = HAL_ERROR;
  1884. break;
  1885. }
  1886. }
  1887. return ret;
  1888. }
  1889. /**
  1890. * @brief Configure the UART peripheral advanced features.
  1891. * @param huart UART handle.
  1892. * @retval None
  1893. */
  1894. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  1895. {
  1896. /* Check whether the set of advanced features to configure is properly set */
  1897. assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  1898. /* if required, configure TX pin active level inversion */
  1899. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  1900. {
  1901. assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  1902. MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  1903. }
  1904. /* if required, configure RX pin active level inversion */
  1905. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  1906. {
  1907. assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  1908. MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  1909. }
  1910. /* if required, configure data inversion */
  1911. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  1912. {
  1913. assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  1914. MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  1915. }
  1916. /* if required, configure RX/TX pins swap */
  1917. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  1918. {
  1919. assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  1920. MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  1921. }
  1922. /* if required, configure RX overrun detection disabling */
  1923. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1924. {
  1925. assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  1926. MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  1927. }
  1928. /* if required, configure DMA disabling on reception error */
  1929. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  1930. {
  1931. assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  1932. MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  1933. }
  1934. /* if required, configure auto Baud rate detection scheme */
  1935. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  1936. {
  1937. assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  1938. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  1939. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  1940. /* set auto Baudrate detection parameters if detection is enabled */
  1941. if(huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  1942. {
  1943. assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  1944. MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  1945. }
  1946. }
  1947. /* if required, configure MSB first on communication line */
  1948. if(HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  1949. {
  1950. assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  1951. MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  1952. }
  1953. }
  1954. /**
  1955. * @brief Check the UART Idle State.
  1956. * @param huart UART handle.
  1957. * @retval HAL status
  1958. */
  1959. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  1960. {
  1961. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  1962. uint32_t tickstart = 0U;
  1963. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  1964. /* Initialize the UART ErrorCode */
  1965. huart->ErrorCode = HAL_UART_ERROR_NONE;
  1966. #if !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC)
  1967. /* Init tickstart for timeout managment*/
  1968. tickstart = HAL_GetTick();
  1969. /* TEACK and REACK bits in ISR are checked only when available (not available on all F0 devices).
  1970. Bits are defined for some specific devices, and are available only for UART instances supporting WakeUp from Stop Mode feature.
  1971. */
  1972. if (IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance))
  1973. {
  1974. /* Check if the Transmitter is enabled */
  1975. if((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1976. {
  1977. /* Wait until TEACK flag is set */
  1978. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  1979. {
  1980. /* Timeout occurred */
  1981. return HAL_TIMEOUT;
  1982. }
  1983. }
  1984. /* Check if the Receiver is enabled */
  1985. if((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1986. {
  1987. /* Wait until REACK flag is set */
  1988. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  1989. {
  1990. /* Timeout occurred */
  1991. return HAL_TIMEOUT;
  1992. }
  1993. }
  1994. }
  1995. #endif /* !defined(STM32F030x6) && !defined(STM32F030x8)&& !defined(STM32F070xB)&& !defined(STM32F070x6)&& !defined(STM32F030xC) */
  1996. /* Initialize the UART State */
  1997. huart->gState = HAL_UART_STATE_READY;
  1998. huart->RxState = HAL_UART_STATE_READY;
  1999. /* Process Unlocked */
  2000. __HAL_UNLOCK(huart);
  2001. return HAL_OK;
  2002. }
  2003. /**
  2004. * @brief Handle UART Communication Timeout.
  2005. * @param huart UART handle.
  2006. * @param Flag Specifies the UART flag to check
  2007. * @param Status Flag status (SET or RESET)
  2008. * @param Tickstart Tick start value
  2009. * @param Timeout Timeout duration
  2010. * @retval HAL status
  2011. */
  2012. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  2013. {
  2014. /* Wait until flag is set */
  2015. while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  2016. {
  2017. /* Check for the Timeout */
  2018. if(Timeout != HAL_MAX_DELAY)
  2019. {
  2020. if((Timeout == 0U) || ((HAL_GetTick()-Tickstart) > Timeout))
  2021. {
  2022. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  2023. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  2024. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2025. huart->gState = HAL_UART_STATE_READY;
  2026. huart->RxState = HAL_UART_STATE_READY;
  2027. /* Process Unlocked */
  2028. __HAL_UNLOCK(huart);
  2029. return HAL_TIMEOUT;
  2030. }
  2031. }
  2032. }
  2033. return HAL_OK;
  2034. }
  2035. /**
  2036. * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  2037. * @param huart UART handle.
  2038. * @retval None
  2039. */
  2040. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  2041. {
  2042. /* Disable TXEIE and TCIE interrupts */
  2043. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  2044. /* At end of Tx process, restore huart->gState to Ready */
  2045. huart->gState = HAL_UART_STATE_READY;
  2046. }
  2047. /**
  2048. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  2049. * @param huart UART handle.
  2050. * @retval None
  2051. */
  2052. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  2053. {
  2054. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2055. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2056. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2057. /* At end of Rx process, restore huart->RxState to Ready */
  2058. huart->RxState = HAL_UART_STATE_READY;
  2059. }
  2060. /**
  2061. * @brief DMA UART transmit process complete callback.
  2062. * @param hdma DMA handle.
  2063. * @retval None
  2064. */
  2065. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  2066. {
  2067. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2068. /* DMA Normal mode */
  2069. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  2070. {
  2071. huart->TxXferCount = 0;
  2072. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  2073. in the UART CR3 register */
  2074. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  2075. /* Enable the UART Transmit Complete Interrupt */
  2076. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  2077. }
  2078. /* DMA Circular mode */
  2079. else
  2080. {
  2081. HAL_UART_TxCpltCallback(huart);
  2082. }
  2083. }
  2084. /**
  2085. * @brief DMA UART transmit process half complete callback.
  2086. * @param hdma DMA handle.
  2087. * @retval None
  2088. */
  2089. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  2090. {
  2091. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2092. HAL_UART_TxHalfCpltCallback(huart);
  2093. }
  2094. /**
  2095. * @brief DMA UART receive process complete callback.
  2096. * @param hdma DMA handle.
  2097. * @retval None
  2098. */
  2099. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  2100. {
  2101. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2102. /* DMA Normal mode */
  2103. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  2104. {
  2105. huart->RxXferCount = 0U;
  2106. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  2107. CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  2108. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2109. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  2110. in the UART CR3 register */
  2111. CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2112. /* At end of Rx process, restore huart->RxState to Ready */
  2113. huart->RxState = HAL_UART_STATE_READY;
  2114. }
  2115. HAL_UART_RxCpltCallback(huart);
  2116. }
  2117. /**
  2118. * @brief DMA UART receive process half complete callback.
  2119. * @param hdma DMA handle.
  2120. * @retval None
  2121. */
  2122. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  2123. {
  2124. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2125. HAL_UART_RxHalfCpltCallback(huart);
  2126. }
  2127. /**
  2128. * @brief DMA UART communication error callback.
  2129. * @param hdma DMA handle.
  2130. * @retval None
  2131. */
  2132. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  2133. {
  2134. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2135. /* Stop UART DMA Tx request if ongoing */
  2136. if ( (huart->gState == HAL_UART_STATE_BUSY_TX)
  2137. &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) )
  2138. {
  2139. huart->TxXferCount = 0U;
  2140. UART_EndTxTransfer(huart);
  2141. }
  2142. /* Stop UART DMA Rx request if ongoing */
  2143. if ( (huart->RxState == HAL_UART_STATE_BUSY_RX)
  2144. &&(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) )
  2145. {
  2146. huart->RxXferCount = 0U;
  2147. UART_EndRxTransfer(huart);
  2148. }
  2149. huart->ErrorCode |= HAL_UART_ERROR_DMA;
  2150. HAL_UART_ErrorCallback(huart);
  2151. }
  2152. /**
  2153. * @brief DMA UART communication abort callback, when initiated by HAL services on Error
  2154. * (To be called at end of DMA Abort procedure following error occurrence).
  2155. * @param hdma DMA handle.
  2156. * @retval None
  2157. */
  2158. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  2159. {
  2160. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2161. huart->RxXferCount = 0U;
  2162. huart->TxXferCount = 0U;
  2163. HAL_UART_ErrorCallback(huart);
  2164. }
  2165. /**
  2166. * @brief DMA UART Tx communication abort callback, when initiated by user
  2167. * (To be called at end of DMA Tx Abort procedure following user abort request).
  2168. * @note When this callback is executed, User Abort complete call back is called only if no
  2169. * Abort still ongoing for Rx DMA Handle.
  2170. * @param hdma DMA handle.
  2171. * @retval None
  2172. */
  2173. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  2174. {
  2175. UART_HandleTypeDef* huart = (UART_HandleTypeDef* )(hdma->Parent);
  2176. huart->hdmatx->XferAbortCallback = NULL;
  2177. /* Check if an Abort process is still ongoing */
  2178. if(huart->hdmarx != NULL)
  2179. {
  2180. if(huart->hdmarx->XferAbortCallback != NULL)
  2181. {
  2182. return;
  2183. }
  2184. }
  2185. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2186. huart->TxXferCount = 0U;
  2187. huart->RxXferCount = 0U;
  2188. /* Reset errorCode */
  2189. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2190. /* Clear the Error flags in the ICR register */
  2191. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2192. /* Restore huart->gState and huart->RxState to Ready */
  2193. huart->gState = HAL_UART_STATE_READY;
  2194. huart->RxState = HAL_UART_STATE_READY;
  2195. /* Call user Abort complete callback */
  2196. HAL_UART_AbortCpltCallback(huart);
  2197. }
  2198. /**
  2199. * @brief DMA UART Rx communication abort callback, when initiated by user
  2200. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2201. * @note When this callback is executed, User Abort complete call back is called only if no
  2202. * Abort still ongoing for Tx DMA Handle.
  2203. * @param hdma DMA handle.
  2204. * @retval None
  2205. */
  2206. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2207. {
  2208. UART_HandleTypeDef* huart = (UART_HandleTypeDef* )(hdma->Parent);
  2209. huart->hdmarx->XferAbortCallback = NULL;
  2210. /* Check if an Abort process is still ongoing */
  2211. if(huart->hdmatx != NULL)
  2212. {
  2213. if(huart->hdmatx->XferAbortCallback != NULL)
  2214. {
  2215. return;
  2216. }
  2217. }
  2218. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2219. huart->TxXferCount = 0U;
  2220. huart->RxXferCount = 0U;
  2221. /* Reset errorCode */
  2222. huart->ErrorCode = HAL_UART_ERROR_NONE;
  2223. /* Clear the Error flags in the ICR register */
  2224. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2225. /* Restore huart->gState and huart->RxState to Ready */
  2226. huart->gState = HAL_UART_STATE_READY;
  2227. huart->RxState = HAL_UART_STATE_READY;
  2228. /* Call user Abort complete callback */
  2229. HAL_UART_AbortCpltCallback(huart);
  2230. }
  2231. /**
  2232. * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
  2233. * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
  2234. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2235. * and leads to user Tx Abort Complete callback execution).
  2236. * @param hdma DMA handle.
  2237. * @retval None
  2238. */
  2239. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2240. {
  2241. UART_HandleTypeDef* huart = (UART_HandleTypeDef*)(hdma->Parent);
  2242. huart->TxXferCount = 0U;
  2243. /* Restore huart->gState to Ready */
  2244. huart->gState = HAL_UART_STATE_READY;
  2245. /* Call user Abort complete callback */
  2246. HAL_UART_AbortTransmitCpltCallback(huart);
  2247. }
  2248. /**
  2249. * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
  2250. * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
  2251. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2252. * and leads to user Rx Abort Complete callback execution).
  2253. * @param hdma DMA handle.
  2254. * @retval None
  2255. */
  2256. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2257. {
  2258. UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  2259. huart->RxXferCount = 0U;
  2260. /* Clear the Error flags in the ICR register */
  2261. __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2262. /* Restore huart->RxState to Ready */
  2263. huart->RxState = HAL_UART_STATE_READY;
  2264. /* Call user Abort complete callback */
  2265. HAL_UART_AbortReceiveCpltCallback(huart);
  2266. }
  2267. /**
  2268. * @brief Send an amount of data in interrupt mode.
  2269. * @note Function is called under interruption only, once
  2270. * interruptions have been enabled by HAL_UART_Transmit_IT().
  2271. * @param huart UART handle.
  2272. * @retval HAL status
  2273. */
  2274. HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  2275. {
  2276. uint16_t* tmp;
  2277. /* Check that a Tx process is ongoing */
  2278. if (huart->gState == HAL_UART_STATE_BUSY_TX)
  2279. {
  2280. if(huart->TxXferCount == 0U)
  2281. {
  2282. /* Disable the UART Transmit Data Register Empty Interrupt */
  2283. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  2284. /* Enable the UART Transmit Complete Interrupt */
  2285. SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  2286. return HAL_OK;
  2287. }
  2288. else
  2289. {
  2290. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  2291. {
  2292. tmp = (uint16_t*) huart->pTxBuffPtr;
  2293. huart->Instance->TDR = (*tmp & (uint16_t)0x01FFU);
  2294. huart->pTxBuffPtr += 2U;
  2295. }
  2296. else
  2297. {
  2298. huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0xFFU);
  2299. }
  2300. huart->TxXferCount--;
  2301. return HAL_OK;
  2302. }
  2303. }
  2304. else
  2305. {
  2306. return HAL_BUSY;
  2307. }
  2308. }
  2309. /**
  2310. * @brief Wrap up transmission in non-blocking mode.
  2311. * @param huart pointer to a UART_HandleTypeDef structure that contains
  2312. * the configuration information for the specified UART module.
  2313. * @retval HAL status
  2314. */
  2315. HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  2316. {
  2317. /* Disable the UART Transmit Complete Interrupt */
  2318. CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  2319. /* Tx process is ended, restore huart->gState to Ready */
  2320. huart->gState = HAL_UART_STATE_READY;
  2321. HAL_UART_TxCpltCallback(huart);
  2322. return HAL_OK;
  2323. }
  2324. /**
  2325. * @brief Receive an amount of data in interrupt mode.
  2326. * @note Function is called under interruption only, once
  2327. * interruptions have been enabled by HAL_UART_Receive_IT()
  2328. * @param huart UART handle.
  2329. * @retval HAL status
  2330. */
  2331. HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  2332. {
  2333. uint16_t* tmp;
  2334. uint16_t uhMask = huart->Mask;
  2335. uint16_t uhdata;
  2336. /* Check that a Rx process is ongoing */
  2337. if(huart->RxState == HAL_UART_STATE_BUSY_RX)
  2338. {
  2339. uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  2340. if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  2341. {
  2342. tmp = (uint16_t*) huart->pRxBuffPtr ;
  2343. *tmp = (uint16_t)(uhdata & uhMask);
  2344. huart->pRxBuffPtr +=2U;
  2345. }
  2346. else
  2347. {
  2348. *huart->pRxBuffPtr++ = (uint8_t)(uhdata & (uint8_t)uhMask);
  2349. }
  2350. if(--huart->RxXferCount == 0U)
  2351. {
  2352. /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
  2353. CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2354. /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  2355. CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2356. /* Rx process is completed, restore huart->RxState to Ready */
  2357. huart->RxState = HAL_UART_STATE_READY;
  2358. HAL_UART_RxCpltCallback(huart);
  2359. return HAL_OK;
  2360. }
  2361. return HAL_OK;
  2362. }
  2363. else
  2364. {
  2365. /* Clear RXNE interrupt flag */
  2366. __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  2367. return HAL_BUSY;
  2368. }
  2369. }
  2370. /**
  2371. * @}
  2372. */
  2373. #endif /* HAL_UART_MODULE_ENABLED */
  2374. /**
  2375. * @}
  2376. */
  2377. /**
  2378. * @}
  2379. */
  2380. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/