STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_hal_uart.c 00004 * @author MCD Application Team 00005 * @brief UART HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART). 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral Control functions 00011 * 00012 * 00013 ****************************************************************************** 00014 * @attention 00015 * 00016 * Copyright (c) 2017 STMicroelectronics. 00017 * All rights reserved. 00018 * 00019 * This software is licensed under terms that can be found in the LICENSE file 00020 * in the root directory of this software component. 00021 * If no LICENSE file comes with this software, it is provided AS-IS. 00022 * 00023 ****************************************************************************** 00024 @verbatim 00025 =============================================================================== 00026 ##### How to use this driver ##### 00027 =============================================================================== 00028 [..] 00029 The UART HAL driver can be used as follows: 00030 00031 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart). 00032 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API: 00033 (++) Enable the USARTx interface clock. 00034 (++) UART pins configuration: 00035 (+++) Enable the clock for the UART GPIOs. 00036 (+++) Configure these UART pins as alternate function pull-up. 00037 (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT() 00038 and HAL_UART_Receive_IT() APIs): 00039 (+++) Configure the USARTx interrupt priority. 00040 (+++) Enable the NVIC USART IRQ handle. 00041 (++) UART interrupts handling: 00042 -@@- The specific UART interrupts (Transmission complete interrupt, 00043 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts) 00044 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() 00045 inside the transmit and receive processes. 00046 (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA() 00047 and HAL_UART_Receive_DMA() APIs): 00048 (+++) Declare a DMA handle structure for the Tx/Rx channel. 00049 (+++) Enable the DMAx interface clock. 00050 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters. 00051 (+++) Configure the DMA Tx/Rx channel. 00052 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle. 00053 (+++) Configure the priority and enable the NVIC for the transfer complete 00054 interrupt on the DMA Tx/Rx channel. 00055 00056 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware 00057 flow control and Mode (Receiver/Transmitter) in the huart handle Init structure. 00058 00059 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...) 00060 in the huart handle AdvancedInit structure. 00061 00062 (#) For the UART asynchronous mode, initialize the UART registers by calling 00063 the HAL_UART_Init() API. 00064 00065 (#) For the UART Half duplex mode, initialize the UART registers by calling 00066 the HAL_HalfDuplex_Init() API. 00067 00068 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers 00069 by calling the HAL_LIN_Init() API. 00070 00071 (#) For the UART Multiprocessor mode, initialize the UART registers 00072 by calling the HAL_MultiProcessor_Init() API. 00073 00074 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers 00075 by calling the HAL_RS485Ex_Init() API. 00076 00077 [..] 00078 (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(), 00079 also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by 00080 calling the customized HAL_UART_MspInit() API. 00081 00082 ##### Callback registration ##### 00083 ================================== 00084 00085 [..] 00086 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1 00087 allows the user to configure dynamically the driver callbacks. 00088 00089 [..] 00090 Use Function HAL_UART_RegisterCallback() to register a user callback. 00091 Function HAL_UART_RegisterCallback() allows to register following callbacks: 00092 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00093 (+) TxCpltCallback : Tx Complete Callback. 00094 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00095 (+) RxCpltCallback : Rx Complete Callback. 00096 (+) ErrorCallback : Error Callback. 00097 (+) AbortCpltCallback : Abort Complete Callback. 00098 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. 00099 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. 00100 (+) WakeupCallback : Wakeup Callback. 00101 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00102 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00103 (+) MspInitCallback : UART MspInit. 00104 (+) MspDeInitCallback : UART MspDeInit. 00105 This function takes as parameters the HAL peripheral handle, the Callback ID 00106 and a pointer to the user callback function. 00107 00108 [..] 00109 Use function HAL_UART_UnRegisterCallback() to reset a callback to the default 00110 weak (surcharged) function. 00111 HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00112 and the Callback ID. 00113 This function allows to reset following callbacks: 00114 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00115 (+) TxCpltCallback : Tx Complete Callback. 00116 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00117 (+) RxCpltCallback : Rx Complete Callback. 00118 (+) ErrorCallback : Error Callback. 00119 (+) AbortCpltCallback : Abort Complete Callback. 00120 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback. 00121 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback. 00122 (+) WakeupCallback : Wakeup Callback. 00123 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00124 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00125 (+) MspInitCallback : UART MspInit. 00126 (+) MspDeInitCallback : UART MspDeInit. 00127 00128 [..] 00129 For specific callback RxEventCallback, use dedicated registration/reset functions: 00130 respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback(). 00131 00132 [..] 00133 By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET 00134 all callbacks are set to the corresponding weak (surcharged) functions: 00135 examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback(). 00136 Exception done for MspInit and MspDeInit functions that are respectively 00137 reset to the legacy weak (surcharged) functions in the HAL_UART_Init() 00138 and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand). 00139 If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit() 00140 keep and use the user MspInit/MspDeInit callbacks (registered beforehand). 00141 00142 [..] 00143 Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only. 00144 Exception done MspInit/MspDeInit that can be registered/unregistered 00145 in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user) 00146 MspInit/DeInit callbacks can be used during the Init/DeInit. 00147 In that case first register the MspInit/MspDeInit user callbacks 00148 using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit() 00149 or HAL_UART_Init() function. 00150 00151 [..] 00152 When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or 00153 not defined, the callback registration feature is not available 00154 and weak (surcharged) callbacks are used. 00155 00156 00157 @endverbatim 00158 ****************************************************************************** 00159 */ 00160 00161 /* Includes ------------------------------------------------------------------*/ 00162 #include "stm32h7xx_hal.h" 00163 00164 /** @addtogroup STM32H7xx_HAL_Driver 00165 * @{ 00166 */ 00167 00168 /** @defgroup UART UART 00169 * @brief HAL UART module driver 00170 * @{ 00171 */ 00172 00173 #ifdef HAL_UART_MODULE_ENABLED 00174 00175 /* Private typedef -----------------------------------------------------------*/ 00176 /* Private define ------------------------------------------------------------*/ 00177 /** @defgroup UART_Private_Constants UART Private Constants 00178 * @{ 00179 */ 00180 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | \ 00181 USART_CR1_OVER8 | USART_CR1_FIFOEN)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */ 00182 00183 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT | USART_CR3_TXFTCFG | \ 00184 USART_CR3_RXFTCFG)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */ 00185 00186 #define LPUART_BRR_MIN 0x00000300U /* LPUART BRR minimum authorized value */ 00187 #define LPUART_BRR_MAX 0x000FFFFFU /* LPUART BRR maximum authorized value */ 00188 00189 #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */ 00190 #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */ 00191 /** 00192 * @} 00193 */ 00194 00195 /* Private macros ------------------------------------------------------------*/ 00196 /* Private function prototypes -----------------------------------------------*/ 00197 /** @addtogroup UART_Private_Functions 00198 * @{ 00199 */ 00200 static void UART_EndTxTransfer(UART_HandleTypeDef *huart); 00201 static void UART_EndRxTransfer(UART_HandleTypeDef *huart); 00202 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00203 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00204 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00205 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00206 static void UART_DMAError(DMA_HandleTypeDef *hdma); 00207 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma); 00208 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); 00209 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma); 00210 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00211 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00212 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart); 00213 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart); 00214 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart); 00215 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart); 00216 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart); 00217 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart); 00218 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart); 00219 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart); 00220 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart); 00221 /** 00222 * @} 00223 */ 00224 00225 /* Private variables ---------------------------------------------------------*/ 00226 /** @addtogroup UART_Private_variables 00227 * @{ 00228 */ 00229 const uint16_t UARTPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U}; 00230 /** 00231 * @} 00232 */ 00233 00234 /* Exported Constants --------------------------------------------------------*/ 00235 /* Exported functions --------------------------------------------------------*/ 00236 00237 /** @defgroup UART_Exported_Functions UART Exported Functions 00238 * @{ 00239 */ 00240 00241 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions 00242 * @brief Initialization and Configuration functions 00243 * 00244 @verbatim 00245 =============================================================================== 00246 ##### Initialization and Configuration functions ##### 00247 =============================================================================== 00248 [..] 00249 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy 00250 in asynchronous mode. 00251 (+) For the asynchronous mode the parameters below can be configured: 00252 (++) Baud Rate 00253 (++) Word Length 00254 (++) Stop Bit 00255 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00256 in the data register is transmitted but is changed by the parity bit. 00257 (++) Hardware flow control 00258 (++) Receiver/transmitter modes 00259 (++) Over Sampling Method 00260 (++) One-Bit Sampling Method 00261 (+) For the asynchronous mode, the following advanced features can be configured as well: 00262 (++) TX and/or RX pin level inversion 00263 (++) data logical level inversion 00264 (++) RX and TX pins swap 00265 (++) RX overrun detection disabling 00266 (++) DMA disabling on RX error 00267 (++) MSB first on communication line 00268 (++) auto Baud rate detection 00269 [..] 00270 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API 00271 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode 00272 and UART multiprocessor mode configuration procedures (details for the procedures 00273 are available in reference manual). 00274 00275 @endverbatim 00276 00277 Depending on the frame length defined by the M1 and M0 bits (7-bit, 00278 8-bit or 9-bit), the possible UART formats are listed in the 00279 following table. 00280 00281 Table 1. UART frame format. 00282 +-----------------------------------------------------------------------+ 00283 | M1 bit | M0 bit | PCE bit | UART frame | 00284 |---------|---------|-----------|---------------------------------------| 00285 | 0 | 0 | 0 | | SB | 8 bit data | STB | | 00286 |---------|---------|-----------|---------------------------------------| 00287 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | | 00288 |---------|---------|-----------|---------------------------------------| 00289 | 0 | 1 | 0 | | SB | 9 bit data | STB | | 00290 |---------|---------|-----------|---------------------------------------| 00291 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | | 00292 |---------|---------|-----------|---------------------------------------| 00293 | 1 | 0 | 0 | | SB | 7 bit data | STB | | 00294 |---------|---------|-----------|---------------------------------------| 00295 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | | 00296 +-----------------------------------------------------------------------+ 00297 00298 * @{ 00299 */ 00300 00301 /** 00302 * @brief Initialize the UART mode according to the specified 00303 * parameters in the UART_InitTypeDef and initialize the associated handle. 00304 * @param huart UART handle. 00305 * @retval HAL status 00306 */ 00307 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) 00308 { 00309 /* Check the UART handle allocation */ 00310 if (huart == NULL) 00311 { 00312 return HAL_ERROR; 00313 } 00314 00315 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) 00316 { 00317 /* Check the parameters */ 00318 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); 00319 } 00320 else 00321 { 00322 /* Check the parameters */ 00323 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance))); 00324 } 00325 00326 if (huart->gState == HAL_UART_STATE_RESET) 00327 { 00328 /* Allocate lock resource and initialize it */ 00329 huart->Lock = HAL_UNLOCKED; 00330 00331 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00332 UART_InitCallbacksToDefault(huart); 00333 00334 if (huart->MspInitCallback == NULL) 00335 { 00336 huart->MspInitCallback = HAL_UART_MspInit; 00337 } 00338 00339 /* Init the low level hardware */ 00340 huart->MspInitCallback(huart); 00341 #else 00342 /* Init the low level hardware : GPIO, CLOCK */ 00343 HAL_UART_MspInit(huart); 00344 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00345 } 00346 00347 huart->gState = HAL_UART_STATE_BUSY; 00348 00349 __HAL_UART_DISABLE(huart); 00350 00351 /* Set the UART Communication parameters */ 00352 if (UART_SetConfig(huart) == HAL_ERROR) 00353 { 00354 return HAL_ERROR; 00355 } 00356 00357 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 00358 { 00359 UART_AdvFeatureConfig(huart); 00360 } 00361 00362 /* In asynchronous mode, the following bits must be kept cleared: 00363 - LINEN and CLKEN bits in the USART_CR2 register, 00364 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ 00365 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00366 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00367 00368 __HAL_UART_ENABLE(huart); 00369 00370 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 00371 return (UART_CheckIdleState(huart)); 00372 } 00373 00374 /** 00375 * @brief Initialize the half-duplex mode according to the specified 00376 * parameters in the UART_InitTypeDef and creates the associated handle. 00377 * @param huart UART handle. 00378 * @retval HAL status 00379 */ 00380 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) 00381 { 00382 /* Check the UART handle allocation */ 00383 if (huart == NULL) 00384 { 00385 return HAL_ERROR; 00386 } 00387 00388 /* Check UART instance */ 00389 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance)); 00390 00391 if (huart->gState == HAL_UART_STATE_RESET) 00392 { 00393 /* Allocate lock resource and initialize it */ 00394 huart->Lock = HAL_UNLOCKED; 00395 00396 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00397 UART_InitCallbacksToDefault(huart); 00398 00399 if (huart->MspInitCallback == NULL) 00400 { 00401 huart->MspInitCallback = HAL_UART_MspInit; 00402 } 00403 00404 /* Init the low level hardware */ 00405 huart->MspInitCallback(huart); 00406 #else 00407 /* Init the low level hardware : GPIO, CLOCK */ 00408 HAL_UART_MspInit(huart); 00409 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00410 } 00411 00412 huart->gState = HAL_UART_STATE_BUSY; 00413 00414 __HAL_UART_DISABLE(huart); 00415 00416 /* Set the UART Communication parameters */ 00417 if (UART_SetConfig(huart) == HAL_ERROR) 00418 { 00419 return HAL_ERROR; 00420 } 00421 00422 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 00423 { 00424 UART_AdvFeatureConfig(huart); 00425 } 00426 00427 /* In half-duplex mode, the following bits must be kept cleared: 00428 - LINEN and CLKEN bits in the USART_CR2 register, 00429 - SCEN and IREN bits in the USART_CR3 register.*/ 00430 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00431 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN)); 00432 00433 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ 00434 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL); 00435 00436 __HAL_UART_ENABLE(huart); 00437 00438 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 00439 return (UART_CheckIdleState(huart)); 00440 } 00441 00442 00443 /** 00444 * @brief Initialize the LIN mode according to the specified 00445 * parameters in the UART_InitTypeDef and creates the associated handle. 00446 * @param huart UART handle. 00447 * @param BreakDetectLength Specifies the LIN break detection length. 00448 * This parameter can be one of the following values: 00449 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection 00450 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection 00451 * @retval HAL status 00452 */ 00453 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength) 00454 { 00455 /* Check the UART handle allocation */ 00456 if (huart == NULL) 00457 { 00458 return HAL_ERROR; 00459 } 00460 00461 /* Check the LIN UART instance */ 00462 assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); 00463 /* Check the Break detection length parameter */ 00464 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength)); 00465 00466 /* LIN mode limited to 16-bit oversampling only */ 00467 if (huart->Init.OverSampling == UART_OVERSAMPLING_8) 00468 { 00469 return HAL_ERROR; 00470 } 00471 /* LIN mode limited to 8-bit data length */ 00472 if (huart->Init.WordLength != UART_WORDLENGTH_8B) 00473 { 00474 return HAL_ERROR; 00475 } 00476 00477 if (huart->gState == HAL_UART_STATE_RESET) 00478 { 00479 /* Allocate lock resource and initialize it */ 00480 huart->Lock = HAL_UNLOCKED; 00481 00482 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00483 UART_InitCallbacksToDefault(huart); 00484 00485 if (huart->MspInitCallback == NULL) 00486 { 00487 huart->MspInitCallback = HAL_UART_MspInit; 00488 } 00489 00490 /* Init the low level hardware */ 00491 huart->MspInitCallback(huart); 00492 #else 00493 /* Init the low level hardware : GPIO, CLOCK */ 00494 HAL_UART_MspInit(huart); 00495 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00496 } 00497 00498 huart->gState = HAL_UART_STATE_BUSY; 00499 00500 __HAL_UART_DISABLE(huart); 00501 00502 /* Set the UART Communication parameters */ 00503 if (UART_SetConfig(huart) == HAL_ERROR) 00504 { 00505 return HAL_ERROR; 00506 } 00507 00508 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 00509 { 00510 UART_AdvFeatureConfig(huart); 00511 } 00512 00513 /* In LIN mode, the following bits must be kept cleared: 00514 - LINEN and CLKEN bits in the USART_CR2 register, 00515 - SCEN and IREN bits in the USART_CR3 register.*/ 00516 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN); 00517 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN)); 00518 00519 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */ 00520 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN); 00521 00522 /* Set the USART LIN Break detection length. */ 00523 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength); 00524 00525 __HAL_UART_ENABLE(huart); 00526 00527 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 00528 return (UART_CheckIdleState(huart)); 00529 } 00530 00531 00532 /** 00533 * @brief Initialize the multiprocessor mode according to the specified 00534 * parameters in the UART_InitTypeDef and initialize the associated handle. 00535 * @param huart UART handle. 00536 * @param Address UART node address (4-, 6-, 7- or 8-bit long). 00537 * @param WakeUpMethod Specifies the UART wakeup method. 00538 * This parameter can be one of the following values: 00539 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection 00540 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark 00541 * @note If the user resorts to idle line detection wake up, the Address parameter 00542 * is useless and ignored by the initialization function. 00543 * @note If the user resorts to address mark wake up, the address length detection 00544 * is configured by default to 4 bits only. For the UART to be able to 00545 * manage 6-, 7- or 8-bit long addresses detection, the API 00546 * HAL_MultiProcessorEx_AddressLength_Set() must be called after 00547 * HAL_MultiProcessor_Init(). 00548 * @retval HAL status 00549 */ 00550 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod) 00551 { 00552 /* Check the UART handle allocation */ 00553 if (huart == NULL) 00554 { 00555 return HAL_ERROR; 00556 } 00557 00558 /* Check the wake up method parameter */ 00559 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod)); 00560 00561 if (huart->gState == HAL_UART_STATE_RESET) 00562 { 00563 /* Allocate lock resource and initialize it */ 00564 huart->Lock = HAL_UNLOCKED; 00565 00566 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00567 UART_InitCallbacksToDefault(huart); 00568 00569 if (huart->MspInitCallback == NULL) 00570 { 00571 huart->MspInitCallback = HAL_UART_MspInit; 00572 } 00573 00574 /* Init the low level hardware */ 00575 huart->MspInitCallback(huart); 00576 #else 00577 /* Init the low level hardware : GPIO, CLOCK */ 00578 HAL_UART_MspInit(huart); 00579 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00580 } 00581 00582 huart->gState = HAL_UART_STATE_BUSY; 00583 00584 __HAL_UART_DISABLE(huart); 00585 00586 /* Set the UART Communication parameters */ 00587 if (UART_SetConfig(huart) == HAL_ERROR) 00588 { 00589 return HAL_ERROR; 00590 } 00591 00592 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 00593 { 00594 UART_AdvFeatureConfig(huart); 00595 } 00596 00597 /* In multiprocessor mode, the following bits must be kept cleared: 00598 - LINEN and CLKEN bits in the USART_CR2 register, 00599 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */ 00600 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00601 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00602 00603 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK) 00604 { 00605 /* If address mark wake up method is chosen, set the USART address node */ 00606 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS)); 00607 } 00608 00609 /* Set the wake up method by setting the WAKE bit in the CR1 register */ 00610 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod); 00611 00612 __HAL_UART_ENABLE(huart); 00613 00614 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 00615 return (UART_CheckIdleState(huart)); 00616 } 00617 00618 00619 /** 00620 * @brief DeInitialize the UART peripheral. 00621 * @param huart UART handle. 00622 * @retval HAL status 00623 */ 00624 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) 00625 { 00626 /* Check the UART handle allocation */ 00627 if (huart == NULL) 00628 { 00629 return HAL_ERROR; 00630 } 00631 00632 /* Check the parameters */ 00633 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance))); 00634 00635 huart->gState = HAL_UART_STATE_BUSY; 00636 00637 __HAL_UART_DISABLE(huart); 00638 00639 huart->Instance->CR1 = 0x0U; 00640 huart->Instance->CR2 = 0x0U; 00641 huart->Instance->CR3 = 0x0U; 00642 00643 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00644 if (huart->MspDeInitCallback == NULL) 00645 { 00646 huart->MspDeInitCallback = HAL_UART_MspDeInit; 00647 } 00648 /* DeInit the low level hardware */ 00649 huart->MspDeInitCallback(huart); 00650 #else 00651 /* DeInit the low level hardware */ 00652 HAL_UART_MspDeInit(huart); 00653 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00654 00655 huart->ErrorCode = HAL_UART_ERROR_NONE; 00656 huart->gState = HAL_UART_STATE_RESET; 00657 huart->RxState = HAL_UART_STATE_RESET; 00658 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 00659 00660 __HAL_UNLOCK(huart); 00661 00662 return HAL_OK; 00663 } 00664 00665 /** 00666 * @brief Initialize the UART MSP. 00667 * @param huart UART handle. 00668 * @retval None 00669 */ 00670 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) 00671 { 00672 /* Prevent unused argument(s) compilation warning */ 00673 UNUSED(huart); 00674 00675 /* NOTE : This function should not be modified, when the callback is needed, 00676 the HAL_UART_MspInit can be implemented in the user file 00677 */ 00678 } 00679 00680 /** 00681 * @brief DeInitialize the UART MSP. 00682 * @param huart UART handle. 00683 * @retval None 00684 */ 00685 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) 00686 { 00687 /* Prevent unused argument(s) compilation warning */ 00688 UNUSED(huart); 00689 00690 /* NOTE : This function should not be modified, when the callback is needed, 00691 the HAL_UART_MspDeInit can be implemented in the user file 00692 */ 00693 } 00694 00695 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00696 /** 00697 * @brief Register a User UART Callback 00698 * To be used instead of the weak predefined callback 00699 * @param huart uart handle 00700 * @param CallbackID ID of the callback to be registered 00701 * This parameter can be one of the following values: 00702 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00703 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00704 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00705 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00706 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID 00707 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00708 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00709 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00710 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID 00711 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00712 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00713 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID 00714 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID 00715 * @param pCallback pointer to the Callback function 00716 * @retval HAL status 00717 */ 00718 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID, 00719 pUART_CallbackTypeDef pCallback) 00720 { 00721 HAL_StatusTypeDef status = HAL_OK; 00722 00723 if (pCallback == NULL) 00724 { 00725 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00726 00727 return HAL_ERROR; 00728 } 00729 00730 __HAL_LOCK(huart); 00731 00732 if (huart->gState == HAL_UART_STATE_READY) 00733 { 00734 switch (CallbackID) 00735 { 00736 case HAL_UART_TX_HALFCOMPLETE_CB_ID : 00737 huart->TxHalfCpltCallback = pCallback; 00738 break; 00739 00740 case HAL_UART_TX_COMPLETE_CB_ID : 00741 huart->TxCpltCallback = pCallback; 00742 break; 00743 00744 case HAL_UART_RX_HALFCOMPLETE_CB_ID : 00745 huart->RxHalfCpltCallback = pCallback; 00746 break; 00747 00748 case HAL_UART_RX_COMPLETE_CB_ID : 00749 huart->RxCpltCallback = pCallback; 00750 break; 00751 00752 case HAL_UART_ERROR_CB_ID : 00753 huart->ErrorCallback = pCallback; 00754 break; 00755 00756 case HAL_UART_ABORT_COMPLETE_CB_ID : 00757 huart->AbortCpltCallback = pCallback; 00758 break; 00759 00760 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID : 00761 huart->AbortTransmitCpltCallback = pCallback; 00762 break; 00763 00764 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID : 00765 huart->AbortReceiveCpltCallback = pCallback; 00766 break; 00767 00768 case HAL_UART_WAKEUP_CB_ID : 00769 huart->WakeupCallback = pCallback; 00770 break; 00771 00772 case HAL_UART_RX_FIFO_FULL_CB_ID : 00773 huart->RxFifoFullCallback = pCallback; 00774 break; 00775 00776 case HAL_UART_TX_FIFO_EMPTY_CB_ID : 00777 huart->TxFifoEmptyCallback = pCallback; 00778 break; 00779 00780 case HAL_UART_MSPINIT_CB_ID : 00781 huart->MspInitCallback = pCallback; 00782 break; 00783 00784 case HAL_UART_MSPDEINIT_CB_ID : 00785 huart->MspDeInitCallback = pCallback; 00786 break; 00787 00788 default : 00789 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00790 00791 status = HAL_ERROR; 00792 break; 00793 } 00794 } 00795 else if (huart->gState == HAL_UART_STATE_RESET) 00796 { 00797 switch (CallbackID) 00798 { 00799 case HAL_UART_MSPINIT_CB_ID : 00800 huart->MspInitCallback = pCallback; 00801 break; 00802 00803 case HAL_UART_MSPDEINIT_CB_ID : 00804 huart->MspDeInitCallback = pCallback; 00805 break; 00806 00807 default : 00808 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00809 00810 status = HAL_ERROR; 00811 break; 00812 } 00813 } 00814 else 00815 { 00816 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00817 00818 status = HAL_ERROR; 00819 } 00820 00821 __HAL_UNLOCK(huart); 00822 00823 return status; 00824 } 00825 00826 /** 00827 * @brief Unregister an UART Callback 00828 * UART callaback is redirected to the weak predefined callback 00829 * @param huart uart handle 00830 * @param CallbackID ID of the callback to be unregistered 00831 * This parameter can be one of the following values: 00832 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00833 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00834 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00835 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00836 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID 00837 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00838 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00839 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00840 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID 00841 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00842 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00843 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID 00844 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID 00845 * @retval HAL status 00846 */ 00847 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID) 00848 { 00849 HAL_StatusTypeDef status = HAL_OK; 00850 00851 __HAL_LOCK(huart); 00852 00853 if (HAL_UART_STATE_READY == huart->gState) 00854 { 00855 switch (CallbackID) 00856 { 00857 case HAL_UART_TX_HALFCOMPLETE_CB_ID : 00858 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 00859 break; 00860 00861 case HAL_UART_TX_COMPLETE_CB_ID : 00862 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00863 break; 00864 00865 case HAL_UART_RX_HALFCOMPLETE_CB_ID : 00866 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 00867 break; 00868 00869 case HAL_UART_RX_COMPLETE_CB_ID : 00870 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00871 break; 00872 00873 case HAL_UART_ERROR_CB_ID : 00874 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */ 00875 break; 00876 00877 case HAL_UART_ABORT_COMPLETE_CB_ID : 00878 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00879 break; 00880 00881 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID : 00882 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak 00883 AbortTransmitCpltCallback */ 00884 break; 00885 00886 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID : 00887 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak 00888 AbortReceiveCpltCallback */ 00889 break; 00890 00891 case HAL_UART_WAKEUP_CB_ID : 00892 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */ 00893 break; 00894 00895 case HAL_UART_RX_FIFO_FULL_CB_ID : 00896 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */ 00897 break; 00898 00899 case HAL_UART_TX_FIFO_EMPTY_CB_ID : 00900 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */ 00901 break; 00902 00903 case HAL_UART_MSPINIT_CB_ID : 00904 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */ 00905 break; 00906 00907 case HAL_UART_MSPDEINIT_CB_ID : 00908 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */ 00909 break; 00910 00911 default : 00912 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00913 00914 status = HAL_ERROR; 00915 break; 00916 } 00917 } 00918 else if (HAL_UART_STATE_RESET == huart->gState) 00919 { 00920 switch (CallbackID) 00921 { 00922 case HAL_UART_MSPINIT_CB_ID : 00923 huart->MspInitCallback = HAL_UART_MspInit; 00924 break; 00925 00926 case HAL_UART_MSPDEINIT_CB_ID : 00927 huart->MspDeInitCallback = HAL_UART_MspDeInit; 00928 break; 00929 00930 default : 00931 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00932 00933 status = HAL_ERROR; 00934 break; 00935 } 00936 } 00937 else 00938 { 00939 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00940 00941 status = HAL_ERROR; 00942 } 00943 00944 __HAL_UNLOCK(huart); 00945 00946 return status; 00947 } 00948 00949 /** 00950 * @brief Register a User UART Rx Event Callback 00951 * To be used instead of the weak predefined callback 00952 * @param huart Uart handle 00953 * @param pCallback Pointer to the Rx Event Callback function 00954 * @retval HAL status 00955 */ 00956 HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback) 00957 { 00958 HAL_StatusTypeDef status = HAL_OK; 00959 00960 if (pCallback == NULL) 00961 { 00962 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00963 00964 return HAL_ERROR; 00965 } 00966 00967 /* Process locked */ 00968 __HAL_LOCK(huart); 00969 00970 if (huart->gState == HAL_UART_STATE_READY) 00971 { 00972 huart->RxEventCallback = pCallback; 00973 } 00974 else 00975 { 00976 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00977 00978 status = HAL_ERROR; 00979 } 00980 00981 /* Release Lock */ 00982 __HAL_UNLOCK(huart); 00983 00984 return status; 00985 } 00986 00987 /** 00988 * @brief UnRegister the UART Rx Event Callback 00989 * UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback 00990 * @param huart Uart handle 00991 * @retval HAL status 00992 */ 00993 HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart) 00994 { 00995 HAL_StatusTypeDef status = HAL_OK; 00996 00997 /* Process locked */ 00998 __HAL_LOCK(huart); 00999 01000 if (huart->gState == HAL_UART_STATE_READY) 01001 { 01002 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback */ 01003 } 01004 else 01005 { 01006 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 01007 01008 status = HAL_ERROR; 01009 } 01010 01011 /* Release Lock */ 01012 __HAL_UNLOCK(huart); 01013 return status; 01014 } 01015 01016 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 01017 01018 /** 01019 * @} 01020 */ 01021 01022 /** @defgroup UART_Exported_Functions_Group2 IO operation functions 01023 * @brief UART Transmit/Receive functions 01024 * 01025 @verbatim 01026 =============================================================================== 01027 ##### IO operation functions ##### 01028 =============================================================================== 01029 This subsection provides a set of functions allowing to manage the UART asynchronous 01030 and Half duplex data transfers. 01031 01032 (#) There are two mode of transfer: 01033 (+) Blocking mode: The communication is performed in polling mode. 01034 The HAL status of all data processing is returned by the same function 01035 after finishing transfer. 01036 (+) Non-Blocking mode: The communication is performed using Interrupts 01037 or DMA, These API's return the HAL status. 01038 The end of the data processing will be indicated through the 01039 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when 01040 using DMA mode. 01041 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks 01042 will be executed respectively at the end of the transmit or Receive process 01043 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected 01044 01045 (#) Blocking mode API's are : 01046 (+) HAL_UART_Transmit() 01047 (+) HAL_UART_Receive() 01048 01049 (#) Non-Blocking mode API's with Interrupt are : 01050 (+) HAL_UART_Transmit_IT() 01051 (+) HAL_UART_Receive_IT() 01052 (+) HAL_UART_IRQHandler() 01053 01054 (#) Non-Blocking mode API's with DMA are : 01055 (+) HAL_UART_Transmit_DMA() 01056 (+) HAL_UART_Receive_DMA() 01057 (+) HAL_UART_DMAPause() 01058 (+) HAL_UART_DMAResume() 01059 (+) HAL_UART_DMAStop() 01060 01061 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode: 01062 (+) HAL_UART_TxHalfCpltCallback() 01063 (+) HAL_UART_TxCpltCallback() 01064 (+) HAL_UART_RxHalfCpltCallback() 01065 (+) HAL_UART_RxCpltCallback() 01066 (+) HAL_UART_ErrorCallback() 01067 01068 (#) Non-Blocking mode transfers could be aborted using Abort API's : 01069 (+) HAL_UART_Abort() 01070 (+) HAL_UART_AbortTransmit() 01071 (+) HAL_UART_AbortReceive() 01072 (+) HAL_UART_Abort_IT() 01073 (+) HAL_UART_AbortTransmit_IT() 01074 (+) HAL_UART_AbortReceive_IT() 01075 01076 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided: 01077 (+) HAL_UART_AbortCpltCallback() 01078 (+) HAL_UART_AbortTransmitCpltCallback() 01079 (+) HAL_UART_AbortReceiveCpltCallback() 01080 01081 (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced 01082 reception services: 01083 (+) HAL_UARTEx_RxEventCallback() 01084 01085 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. 01086 Errors are handled as follows : 01087 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is 01088 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error 01089 in Interrupt mode reception . 01090 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user 01091 to identify error type, and HAL_UART_ErrorCallback() user callback is executed. 01092 Transfer is kept ongoing on UART side. 01093 If user wants to abort it, Abort services should be called by user. 01094 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted. 01095 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode. 01096 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() 01097 user callback is executed. 01098 01099 -@- In the Half duplex communication, it is forbidden to run the transmit 01100 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful. 01101 01102 @endverbatim 01103 * @{ 01104 */ 01105 01106 /** 01107 * @brief Send an amount of data in blocking mode. 01108 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01109 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01110 * of u16 provided through pData. 01111 * @note When FIFO mode is enabled, writing a data in the TDR register adds one 01112 * data to the TXFIFO. Write operations to the TDR register are performed 01113 * when TXFNF flag is set. From hardware perspective, TXFNF flag and 01114 * TXE are mapped on the same bit-field. 01115 * @param huart UART handle. 01116 * @param pData Pointer to data buffer (u8 or u16 data elements). 01117 * @param Size Amount of data elements (u8 or u16) to be sent. 01118 * @param Timeout Timeout duration. 01119 * @retval HAL status 01120 */ 01121 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout) 01122 { 01123 const uint8_t *pdata8bits; 01124 const uint16_t *pdata16bits; 01125 uint32_t tickstart; 01126 01127 /* Check that a Tx process is not already ongoing */ 01128 if (huart->gState == HAL_UART_STATE_READY) 01129 { 01130 if ((pData == NULL) || (Size == 0U)) 01131 { 01132 return HAL_ERROR; 01133 } 01134 01135 __HAL_LOCK(huart); 01136 01137 huart->ErrorCode = HAL_UART_ERROR_NONE; 01138 huart->gState = HAL_UART_STATE_BUSY_TX; 01139 01140 /* Init tickstart for timeout management */ 01141 tickstart = HAL_GetTick(); 01142 01143 huart->TxXferSize = Size; 01144 huart->TxXferCount = Size; 01145 01146 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */ 01147 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01148 { 01149 pdata8bits = NULL; 01150 pdata16bits = (const uint16_t *) pData; 01151 } 01152 else 01153 { 01154 pdata8bits = pData; 01155 pdata16bits = NULL; 01156 } 01157 01158 __HAL_UNLOCK(huart); 01159 01160 while (huart->TxXferCount > 0U) 01161 { 01162 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 01163 { 01164 return HAL_TIMEOUT; 01165 } 01166 if (pdata8bits == NULL) 01167 { 01168 huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU); 01169 pdata16bits++; 01170 } 01171 else 01172 { 01173 huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU); 01174 pdata8bits++; 01175 } 01176 huart->TxXferCount--; 01177 } 01178 01179 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) 01180 { 01181 return HAL_TIMEOUT; 01182 } 01183 01184 /* At end of Tx process, restore huart->gState to Ready */ 01185 huart->gState = HAL_UART_STATE_READY; 01186 01187 return HAL_OK; 01188 } 01189 else 01190 { 01191 return HAL_BUSY; 01192 } 01193 } 01194 01195 /** 01196 * @brief Receive an amount of data in blocking mode. 01197 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01198 * the received data is handled as a set of u16. In this case, Size must indicate the number 01199 * of u16 available through pData. 01200 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO 01201 * is not empty. Read operations from the RDR register are performed when 01202 * RXFNE flag is set. From hardware perspective, RXFNE flag and 01203 * RXNE are mapped on the same bit-field. 01204 * @param huart UART handle. 01205 * @param pData Pointer to data buffer (u8 or u16 data elements). 01206 * @param Size Amount of data elements (u8 or u16) to be received. 01207 * @param Timeout Timeout duration. 01208 * @retval HAL status 01209 */ 01210 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 01211 { 01212 uint8_t *pdata8bits; 01213 uint16_t *pdata16bits; 01214 uint16_t uhMask; 01215 uint32_t tickstart; 01216 01217 /* Check that a Rx process is not already ongoing */ 01218 if (huart->RxState == HAL_UART_STATE_READY) 01219 { 01220 if ((pData == NULL) || (Size == 0U)) 01221 { 01222 return HAL_ERROR; 01223 } 01224 01225 __HAL_LOCK(huart); 01226 01227 huart->ErrorCode = HAL_UART_ERROR_NONE; 01228 huart->RxState = HAL_UART_STATE_BUSY_RX; 01229 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01230 01231 /* Init tickstart for timeout management */ 01232 tickstart = HAL_GetTick(); 01233 01234 huart->RxXferSize = Size; 01235 huart->RxXferCount = Size; 01236 01237 /* Computation of UART mask to apply to RDR register */ 01238 UART_MASK_COMPUTATION(huart); 01239 uhMask = huart->Mask; 01240 01241 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */ 01242 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01243 { 01244 pdata8bits = NULL; 01245 pdata16bits = (uint16_t *) pData; 01246 } 01247 else 01248 { 01249 pdata8bits = pData; 01250 pdata16bits = NULL; 01251 } 01252 01253 __HAL_UNLOCK(huart); 01254 01255 /* as long as data have to be received */ 01256 while (huart->RxXferCount > 0U) 01257 { 01258 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 01259 { 01260 return HAL_TIMEOUT; 01261 } 01262 if (pdata8bits == NULL) 01263 { 01264 *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask); 01265 pdata16bits++; 01266 } 01267 else 01268 { 01269 *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask); 01270 pdata8bits++; 01271 } 01272 huart->RxXferCount--; 01273 } 01274 01275 /* At end of Rx process, restore huart->RxState to Ready */ 01276 huart->RxState = HAL_UART_STATE_READY; 01277 01278 return HAL_OK; 01279 } 01280 else 01281 { 01282 return HAL_BUSY; 01283 } 01284 } 01285 01286 /** 01287 * @brief Send an amount of data in interrupt mode. 01288 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01289 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01290 * of u16 provided through pData. 01291 * @param huart UART handle. 01292 * @param pData Pointer to data buffer (u8 or u16 data elements). 01293 * @param Size Amount of data elements (u8 or u16) to be sent. 01294 * @retval HAL status 01295 */ 01296 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size) 01297 { 01298 /* Check that a Tx process is not already ongoing */ 01299 if (huart->gState == HAL_UART_STATE_READY) 01300 { 01301 if ((pData == NULL) || (Size == 0U)) 01302 { 01303 return HAL_ERROR; 01304 } 01305 01306 __HAL_LOCK(huart); 01307 01308 huart->pTxBuffPtr = pData; 01309 huart->TxXferSize = Size; 01310 huart->TxXferCount = Size; 01311 huart->TxISR = NULL; 01312 01313 huart->ErrorCode = HAL_UART_ERROR_NONE; 01314 huart->gState = HAL_UART_STATE_BUSY_TX; 01315 01316 /* Configure Tx interrupt processing */ 01317 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 01318 { 01319 /* Set the Tx ISR function pointer according to the data word length */ 01320 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01321 { 01322 huart->TxISR = UART_TxISR_16BIT_FIFOEN; 01323 } 01324 else 01325 { 01326 huart->TxISR = UART_TxISR_8BIT_FIFOEN; 01327 } 01328 01329 __HAL_UNLOCK(huart); 01330 01331 /* Enable the TX FIFO threshold interrupt */ 01332 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE); 01333 } 01334 else 01335 { 01336 /* Set the Tx ISR function pointer according to the data word length */ 01337 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01338 { 01339 huart->TxISR = UART_TxISR_16BIT; 01340 } 01341 else 01342 { 01343 huart->TxISR = UART_TxISR_8BIT; 01344 } 01345 01346 __HAL_UNLOCK(huart); 01347 01348 /* Enable the Transmit Data Register Empty interrupt */ 01349 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 01350 } 01351 01352 return HAL_OK; 01353 } 01354 else 01355 { 01356 return HAL_BUSY; 01357 } 01358 } 01359 01360 /** 01361 * @brief Receive an amount of data in interrupt mode. 01362 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01363 * the received data is handled as a set of u16. In this case, Size must indicate the number 01364 * of u16 available through pData. 01365 * @param huart UART handle. 01366 * @param pData Pointer to data buffer (u8 or u16 data elements). 01367 * @param Size Amount of data elements (u8 or u16) to be received. 01368 * @retval HAL status 01369 */ 01370 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01371 { 01372 /* Check that a Rx process is not already ongoing */ 01373 if (huart->RxState == HAL_UART_STATE_READY) 01374 { 01375 if ((pData == NULL) || (Size == 0U)) 01376 { 01377 return HAL_ERROR; 01378 } 01379 01380 __HAL_LOCK(huart); 01381 01382 /* Set Reception type to Standard reception */ 01383 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01384 01385 if (!(IS_LPUART_INSTANCE(huart->Instance))) 01386 { 01387 /* Check that USART RTOEN bit is set */ 01388 if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U) 01389 { 01390 /* Enable the UART Receiver Timeout Interrupt */ 01391 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE); 01392 } 01393 } 01394 01395 return (UART_Start_Receive_IT(huart, pData, Size)); 01396 } 01397 else 01398 { 01399 return HAL_BUSY; 01400 } 01401 } 01402 01403 /** 01404 * @brief Send an amount of data in DMA mode. 01405 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01406 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01407 * of u16 provided through pData. 01408 * @param huart UART handle. 01409 * @param pData Pointer to data buffer (u8 or u16 data elements). 01410 * @param Size Amount of data elements (u8 or u16) to be sent. 01411 * @retval HAL status 01412 */ 01413 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size) 01414 { 01415 /* Check that a Tx process is not already ongoing */ 01416 if (huart->gState == HAL_UART_STATE_READY) 01417 { 01418 if ((pData == NULL) || (Size == 0U)) 01419 { 01420 return HAL_ERROR; 01421 } 01422 01423 __HAL_LOCK(huart); 01424 01425 huart->pTxBuffPtr = pData; 01426 huart->TxXferSize = Size; 01427 huart->TxXferCount = Size; 01428 01429 huart->ErrorCode = HAL_UART_ERROR_NONE; 01430 huart->gState = HAL_UART_STATE_BUSY_TX; 01431 01432 if (huart->hdmatx != NULL) 01433 { 01434 /* Set the UART DMA transfer complete callback */ 01435 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; 01436 01437 /* Set the UART DMA Half transfer complete callback */ 01438 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; 01439 01440 /* Set the DMA error callback */ 01441 huart->hdmatx->XferErrorCallback = UART_DMAError; 01442 01443 /* Set the DMA abort callback */ 01444 huart->hdmatx->XferAbortCallback = NULL; 01445 01446 /* Enable the UART transmit DMA channel */ 01447 if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK) 01448 { 01449 /* Set error code to DMA */ 01450 huart->ErrorCode = HAL_UART_ERROR_DMA; 01451 01452 __HAL_UNLOCK(huart); 01453 01454 /* Restore huart->gState to ready */ 01455 huart->gState = HAL_UART_STATE_READY; 01456 01457 return HAL_ERROR; 01458 } 01459 } 01460 /* Clear the TC flag in the ICR register */ 01461 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF); 01462 01463 __HAL_UNLOCK(huart); 01464 01465 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01466 in the UART CR3 register */ 01467 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01468 01469 return HAL_OK; 01470 } 01471 else 01472 { 01473 return HAL_BUSY; 01474 } 01475 } 01476 01477 /** 01478 * @brief Receive an amount of data in DMA mode. 01479 * @note When the UART parity is enabled (PCE = 1), the received data contain 01480 * the parity bit (MSB position). 01481 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01482 * the received data is handled as a set of u16. In this case, Size must indicate the number 01483 * of u16 available through pData. 01484 * @param huart UART handle. 01485 * @param pData Pointer to data buffer (u8 or u16 data elements). 01486 * @param Size Amount of data elements (u8 or u16) to be received. 01487 * @retval HAL status 01488 */ 01489 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01490 { 01491 /* Check that a Rx process is not already ongoing */ 01492 if (huart->RxState == HAL_UART_STATE_READY) 01493 { 01494 if ((pData == NULL) || (Size == 0U)) 01495 { 01496 return HAL_ERROR; 01497 } 01498 01499 __HAL_LOCK(huart); 01500 01501 /* Set Reception type to Standard reception */ 01502 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01503 01504 if (!(IS_LPUART_INSTANCE(huart->Instance))) 01505 { 01506 /* Check that USART RTOEN bit is set */ 01507 if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U) 01508 { 01509 /* Enable the UART Receiver Timeout Interrupt */ 01510 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE); 01511 } 01512 } 01513 01514 return (UART_Start_Receive_DMA(huart, pData, Size)); 01515 } 01516 else 01517 { 01518 return HAL_BUSY; 01519 } 01520 } 01521 01522 /** 01523 * @brief Pause the DMA Transfer. 01524 * @param huart UART handle. 01525 * @retval HAL status 01526 */ 01527 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) 01528 { 01529 const HAL_UART_StateTypeDef gstate = huart->gState; 01530 const HAL_UART_StateTypeDef rxstate = huart->RxState; 01531 01532 __HAL_LOCK(huart); 01533 01534 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) && 01535 (gstate == HAL_UART_STATE_BUSY_TX)) 01536 { 01537 /* Disable the UART DMA Tx request */ 01538 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01539 } 01540 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) && 01541 (rxstate == HAL_UART_STATE_BUSY_RX)) 01542 { 01543 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01544 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01545 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01546 01547 /* Disable the UART DMA Rx request */ 01548 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01549 } 01550 01551 __HAL_UNLOCK(huart); 01552 01553 return HAL_OK; 01554 } 01555 01556 /** 01557 * @brief Resume the DMA Transfer. 01558 * @param huart UART handle. 01559 * @retval HAL status 01560 */ 01561 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) 01562 { 01563 __HAL_LOCK(huart); 01564 01565 if (huart->gState == HAL_UART_STATE_BUSY_TX) 01566 { 01567 /* Enable the UART DMA Tx request */ 01568 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01569 } 01570 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 01571 { 01572 /* Clear the Overrun flag before resuming the Rx transfer */ 01573 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); 01574 01575 /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01576 if (huart->Init.Parity != UART_PARITY_NONE) 01577 { 01578 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01579 } 01580 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 01581 01582 /* Enable the UART DMA Rx request */ 01583 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01584 } 01585 01586 __HAL_UNLOCK(huart); 01587 01588 return HAL_OK; 01589 } 01590 01591 /** 01592 * @brief Stop the DMA Transfer. 01593 * @param huart UART handle. 01594 * @retval HAL status 01595 */ 01596 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) 01597 { 01598 /* The Lock is not implemented on this API to allow the user application 01599 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() / 01600 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback: 01601 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete 01602 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of 01603 the stream and the corresponding call back is executed. */ 01604 01605 const HAL_UART_StateTypeDef gstate = huart->gState; 01606 const HAL_UART_StateTypeDef rxstate = huart->RxState; 01607 01608 /* Stop UART DMA Tx request if ongoing */ 01609 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) && 01610 (gstate == HAL_UART_STATE_BUSY_TX)) 01611 { 01612 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01613 01614 /* Abort the UART DMA Tx channel */ 01615 if (huart->hdmatx != NULL) 01616 { 01617 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK) 01618 { 01619 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01620 { 01621 /* Set error code to DMA */ 01622 huart->ErrorCode = HAL_UART_ERROR_DMA; 01623 01624 return HAL_TIMEOUT; 01625 } 01626 } 01627 } 01628 01629 UART_EndTxTransfer(huart); 01630 } 01631 01632 /* Stop UART DMA Rx request if ongoing */ 01633 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) && 01634 (rxstate == HAL_UART_STATE_BUSY_RX)) 01635 { 01636 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01637 01638 /* Abort the UART DMA Rx channel */ 01639 if (huart->hdmarx != NULL) 01640 { 01641 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK) 01642 { 01643 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01644 { 01645 /* Set error code to DMA */ 01646 huart->ErrorCode = HAL_UART_ERROR_DMA; 01647 01648 return HAL_TIMEOUT; 01649 } 01650 } 01651 } 01652 01653 UART_EndRxTransfer(huart); 01654 } 01655 01656 return HAL_OK; 01657 } 01658 01659 /** 01660 * @brief Abort ongoing transfers (blocking mode). 01661 * @param huart UART handle. 01662 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01663 * This procedure performs following operations : 01664 * - Disable UART Interrupts (Tx and Rx) 01665 * - Disable the DMA transfer in the peripheral register (if enabled) 01666 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01667 * - Set handle State to READY 01668 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01669 * @retval HAL status 01670 */ 01671 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart) 01672 { 01673 /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */ 01674 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | 01675 USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE)); 01676 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE); 01677 01678 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */ 01679 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 01680 { 01681 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE)); 01682 } 01683 01684 /* Abort the UART DMA Tx channel if enabled */ 01685 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01686 { 01687 /* Disable the UART DMA Tx request if enabled */ 01688 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01689 01690 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */ 01691 if (huart->hdmatx != NULL) 01692 { 01693 /* Set the UART DMA Abort callback to Null. 01694 No call back execution at end of DMA abort procedure */ 01695 huart->hdmatx->XferAbortCallback = NULL; 01696 01697 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK) 01698 { 01699 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01700 { 01701 /* Set error code to DMA */ 01702 huart->ErrorCode = HAL_UART_ERROR_DMA; 01703 01704 return HAL_TIMEOUT; 01705 } 01706 } 01707 } 01708 } 01709 01710 /* Abort the UART DMA Rx channel if enabled */ 01711 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01712 { 01713 /* Disable the UART DMA Rx request if enabled */ 01714 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01715 01716 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */ 01717 if (huart->hdmarx != NULL) 01718 { 01719 /* Set the UART DMA Abort callback to Null. 01720 No call back execution at end of DMA abort procedure */ 01721 huart->hdmarx->XferAbortCallback = NULL; 01722 01723 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK) 01724 { 01725 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01726 { 01727 /* Set error code to DMA */ 01728 huart->ErrorCode = HAL_UART_ERROR_DMA; 01729 01730 return HAL_TIMEOUT; 01731 } 01732 } 01733 } 01734 } 01735 01736 /* Reset Tx and Rx transfer counters */ 01737 huart->TxXferCount = 0U; 01738 huart->RxXferCount = 0U; 01739 01740 /* Clear the Error flags in the ICR register */ 01741 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 01742 01743 /* Flush the whole TX FIFO (if needed) */ 01744 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 01745 { 01746 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 01747 } 01748 01749 /* Discard the received data */ 01750 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 01751 01752 /* Restore huart->gState and huart->RxState to Ready */ 01753 huart->gState = HAL_UART_STATE_READY; 01754 huart->RxState = HAL_UART_STATE_READY; 01755 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01756 01757 huart->ErrorCode = HAL_UART_ERROR_NONE; 01758 01759 return HAL_OK; 01760 } 01761 01762 /** 01763 * @brief Abort ongoing Transmit transfer (blocking mode). 01764 * @param huart UART handle. 01765 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. 01766 * This procedure performs following operations : 01767 * - Disable UART Interrupts (Tx) 01768 * - Disable the DMA transfer in the peripheral register (if enabled) 01769 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01770 * - Set handle State to READY 01771 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01772 * @retval HAL status 01773 */ 01774 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart) 01775 { 01776 /* Disable TCIE, TXEIE and TXFTIE interrupts */ 01777 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE)); 01778 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE); 01779 01780 /* Abort the UART DMA Tx channel if enabled */ 01781 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01782 { 01783 /* Disable the UART DMA Tx request if enabled */ 01784 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01785 01786 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */ 01787 if (huart->hdmatx != NULL) 01788 { 01789 /* Set the UART DMA Abort callback to Null. 01790 No call back execution at end of DMA abort procedure */ 01791 huart->hdmatx->XferAbortCallback = NULL; 01792 01793 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK) 01794 { 01795 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01796 { 01797 /* Set error code to DMA */ 01798 huart->ErrorCode = HAL_UART_ERROR_DMA; 01799 01800 return HAL_TIMEOUT; 01801 } 01802 } 01803 } 01804 } 01805 01806 /* Reset Tx transfer counter */ 01807 huart->TxXferCount = 0U; 01808 01809 /* Flush the whole TX FIFO (if needed) */ 01810 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 01811 { 01812 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 01813 } 01814 01815 /* Restore huart->gState to Ready */ 01816 huart->gState = HAL_UART_STATE_READY; 01817 01818 return HAL_OK; 01819 } 01820 01821 /** 01822 * @brief Abort ongoing Receive transfer (blocking mode). 01823 * @param huart UART handle. 01824 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. 01825 * This procedure performs following operations : 01826 * - Disable UART Interrupts (Rx) 01827 * - Disable the DMA transfer in the peripheral register (if enabled) 01828 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01829 * - Set handle State to READY 01830 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01831 * @retval HAL status 01832 */ 01833 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart) 01834 { 01835 /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */ 01836 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE)); 01837 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE); 01838 01839 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */ 01840 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 01841 { 01842 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE)); 01843 } 01844 01845 /* Abort the UART DMA Rx channel if enabled */ 01846 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01847 { 01848 /* Disable the UART DMA Rx request if enabled */ 01849 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01850 01851 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */ 01852 if (huart->hdmarx != NULL) 01853 { 01854 /* Set the UART DMA Abort callback to Null. 01855 No call back execution at end of DMA abort procedure */ 01856 huart->hdmarx->XferAbortCallback = NULL; 01857 01858 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK) 01859 { 01860 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01861 { 01862 /* Set error code to DMA */ 01863 huart->ErrorCode = HAL_UART_ERROR_DMA; 01864 01865 return HAL_TIMEOUT; 01866 } 01867 } 01868 } 01869 } 01870 01871 /* Reset Rx transfer counter */ 01872 huart->RxXferCount = 0U; 01873 01874 /* Clear the Error flags in the ICR register */ 01875 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 01876 01877 /* Discard the received data */ 01878 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 01879 01880 /* Restore huart->RxState to Ready */ 01881 huart->RxState = HAL_UART_STATE_READY; 01882 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01883 01884 return HAL_OK; 01885 } 01886 01887 /** 01888 * @brief Abort ongoing transfers (Interrupt mode). 01889 * @param huart UART handle. 01890 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01891 * This procedure performs following operations : 01892 * - Disable UART Interrupts (Tx and Rx) 01893 * - Disable the DMA transfer in the peripheral register (if enabled) 01894 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01895 * - Set handle State to READY 01896 * - At abort completion, call user abort complete callback 01897 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01898 * considered as completed only when user abort complete callback is executed (not when exiting function). 01899 * @retval HAL status 01900 */ 01901 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart) 01902 { 01903 uint32_t abortcplt = 1U; 01904 01905 /* Disable interrupts */ 01906 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE | 01907 USART_CR1_TXEIE_TXFNFIE)); 01908 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 01909 01910 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */ 01911 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 01912 { 01913 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE)); 01914 } 01915 01916 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised 01917 before any call to DMA Abort functions */ 01918 /* DMA Tx Handle is valid */ 01919 if (huart->hdmatx != NULL) 01920 { 01921 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled. 01922 Otherwise, set it to NULL */ 01923 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01924 { 01925 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback; 01926 } 01927 else 01928 { 01929 huart->hdmatx->XferAbortCallback = NULL; 01930 } 01931 } 01932 /* DMA Rx Handle is valid */ 01933 if (huart->hdmarx != NULL) 01934 { 01935 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled. 01936 Otherwise, set it to NULL */ 01937 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01938 { 01939 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback; 01940 } 01941 else 01942 { 01943 huart->hdmarx->XferAbortCallback = NULL; 01944 } 01945 } 01946 01947 /* Abort the UART DMA Tx channel if enabled */ 01948 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01949 { 01950 /* Disable DMA Tx at UART level */ 01951 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01952 01953 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */ 01954 if (huart->hdmatx != NULL) 01955 { 01956 /* UART Tx DMA Abort callback has already been initialised : 01957 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01958 01959 /* Abort DMA TX */ 01960 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK) 01961 { 01962 huart->hdmatx->XferAbortCallback = NULL; 01963 } 01964 else 01965 { 01966 abortcplt = 0U; 01967 } 01968 } 01969 } 01970 01971 /* Abort the UART DMA Rx channel if enabled */ 01972 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01973 { 01974 /* Disable the UART DMA Rx request if enabled */ 01975 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01976 01977 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */ 01978 if (huart->hdmarx != NULL) 01979 { 01980 /* UART Rx DMA Abort callback has already been initialised : 01981 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01982 01983 /* Abort DMA RX */ 01984 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 01985 { 01986 huart->hdmarx->XferAbortCallback = NULL; 01987 abortcplt = 1U; 01988 } 01989 else 01990 { 01991 abortcplt = 0U; 01992 } 01993 } 01994 } 01995 01996 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ 01997 if (abortcplt == 1U) 01998 { 01999 /* Reset Tx and Rx transfer counters */ 02000 huart->TxXferCount = 0U; 02001 huart->RxXferCount = 0U; 02002 02003 /* Clear ISR function pointers */ 02004 huart->RxISR = NULL; 02005 huart->TxISR = NULL; 02006 02007 /* Reset errorCode */ 02008 huart->ErrorCode = HAL_UART_ERROR_NONE; 02009 02010 /* Clear the Error flags in the ICR register */ 02011 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 02012 02013 /* Flush the whole TX FIFO (if needed) */ 02014 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 02015 { 02016 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 02017 } 02018 02019 /* Discard the received data */ 02020 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 02021 02022 /* Restore huart->gState and huart->RxState to Ready */ 02023 huart->gState = HAL_UART_STATE_READY; 02024 huart->RxState = HAL_UART_STATE_READY; 02025 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02026 02027 /* As no DMA to be aborted, call directly user Abort complete callback */ 02028 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02029 /* Call registered Abort complete callback */ 02030 huart->AbortCpltCallback(huart); 02031 #else 02032 /* Call legacy weak Abort complete callback */ 02033 HAL_UART_AbortCpltCallback(huart); 02034 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02035 } 02036 02037 return HAL_OK; 02038 } 02039 02040 /** 02041 * @brief Abort ongoing Transmit transfer (Interrupt mode). 02042 * @param huart UART handle. 02043 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode. 02044 * This procedure performs following operations : 02045 * - Disable UART Interrupts (Tx) 02046 * - Disable the DMA transfer in the peripheral register (if enabled) 02047 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 02048 * - Set handle State to READY 02049 * - At abort completion, call user abort complete callback 02050 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 02051 * considered as completed only when user abort complete callback is executed (not when exiting function). 02052 * @retval HAL status 02053 */ 02054 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart) 02055 { 02056 /* Disable interrupts */ 02057 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE)); 02058 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE); 02059 02060 /* Abort the UART DMA Tx channel if enabled */ 02061 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 02062 { 02063 /* Disable the UART DMA Tx request if enabled */ 02064 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 02065 02066 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */ 02067 if (huart->hdmatx != NULL) 02068 { 02069 /* Set the UART DMA Abort callback : 02070 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 02071 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback; 02072 02073 /* Abort DMA TX */ 02074 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK) 02075 { 02076 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */ 02077 huart->hdmatx->XferAbortCallback(huart->hdmatx); 02078 } 02079 } 02080 else 02081 { 02082 /* Reset Tx transfer counter */ 02083 huart->TxXferCount = 0U; 02084 02085 /* Clear TxISR function pointers */ 02086 huart->TxISR = NULL; 02087 02088 /* Restore huart->gState to Ready */ 02089 huart->gState = HAL_UART_STATE_READY; 02090 02091 /* As no DMA to be aborted, call directly user Abort complete callback */ 02092 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02093 /* Call registered Abort Transmit Complete Callback */ 02094 huart->AbortTransmitCpltCallback(huart); 02095 #else 02096 /* Call legacy weak Abort Transmit Complete Callback */ 02097 HAL_UART_AbortTransmitCpltCallback(huart); 02098 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02099 } 02100 } 02101 else 02102 { 02103 /* Reset Tx transfer counter */ 02104 huart->TxXferCount = 0U; 02105 02106 /* Clear TxISR function pointers */ 02107 huart->TxISR = NULL; 02108 02109 /* Flush the whole TX FIFO (if needed) */ 02110 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 02111 { 02112 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 02113 } 02114 02115 /* Restore huart->gState to Ready */ 02116 huart->gState = HAL_UART_STATE_READY; 02117 02118 /* As no DMA to be aborted, call directly user Abort complete callback */ 02119 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02120 /* Call registered Abort Transmit Complete Callback */ 02121 huart->AbortTransmitCpltCallback(huart); 02122 #else 02123 /* Call legacy weak Abort Transmit Complete Callback */ 02124 HAL_UART_AbortTransmitCpltCallback(huart); 02125 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02126 } 02127 02128 return HAL_OK; 02129 } 02130 02131 /** 02132 * @brief Abort ongoing Receive transfer (Interrupt mode). 02133 * @param huart UART handle. 02134 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode. 02135 * This procedure performs following operations : 02136 * - Disable UART Interrupts (Rx) 02137 * - Disable the DMA transfer in the peripheral register (if enabled) 02138 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 02139 * - Set handle State to READY 02140 * - At abort completion, call user abort complete callback 02141 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 02142 * considered as completed only when user abort complete callback is executed (not when exiting function). 02143 * @retval HAL status 02144 */ 02145 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart) 02146 { 02147 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 02148 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE)); 02149 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 02150 02151 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */ 02152 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 02153 { 02154 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE)); 02155 } 02156 02157 /* Abort the UART DMA Rx channel if enabled */ 02158 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 02159 { 02160 /* Disable the UART DMA Rx request if enabled */ 02161 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02162 02163 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */ 02164 if (huart->hdmarx != NULL) 02165 { 02166 /* Set the UART DMA Abort callback : 02167 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 02168 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback; 02169 02170 /* Abort DMA RX */ 02171 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 02172 { 02173 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */ 02174 huart->hdmarx->XferAbortCallback(huart->hdmarx); 02175 } 02176 } 02177 else 02178 { 02179 /* Reset Rx transfer counter */ 02180 huart->RxXferCount = 0U; 02181 02182 /* Clear RxISR function pointer */ 02183 huart->pRxBuffPtr = NULL; 02184 02185 /* Clear the Error flags in the ICR register */ 02186 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 02187 02188 /* Discard the received data */ 02189 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 02190 02191 /* Restore huart->RxState to Ready */ 02192 huart->RxState = HAL_UART_STATE_READY; 02193 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02194 02195 /* As no DMA to be aborted, call directly user Abort complete callback */ 02196 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02197 /* Call registered Abort Receive Complete Callback */ 02198 huart->AbortReceiveCpltCallback(huart); 02199 #else 02200 /* Call legacy weak Abort Receive Complete Callback */ 02201 HAL_UART_AbortReceiveCpltCallback(huart); 02202 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02203 } 02204 } 02205 else 02206 { 02207 /* Reset Rx transfer counter */ 02208 huart->RxXferCount = 0U; 02209 02210 /* Clear RxISR function pointer */ 02211 huart->pRxBuffPtr = NULL; 02212 02213 /* Clear the Error flags in the ICR register */ 02214 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 02215 02216 /* Restore huart->RxState to Ready */ 02217 huart->RxState = HAL_UART_STATE_READY; 02218 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02219 02220 /* As no DMA to be aborted, call directly user Abort complete callback */ 02221 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02222 /* Call registered Abort Receive Complete Callback */ 02223 huart->AbortReceiveCpltCallback(huart); 02224 #else 02225 /* Call legacy weak Abort Receive Complete Callback */ 02226 HAL_UART_AbortReceiveCpltCallback(huart); 02227 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02228 } 02229 02230 return HAL_OK; 02231 } 02232 02233 /** 02234 * @brief Handle UART interrupt request. 02235 * @param huart UART handle. 02236 * @retval None 02237 */ 02238 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) 02239 { 02240 uint32_t isrflags = READ_REG(huart->Instance->ISR); 02241 uint32_t cr1its = READ_REG(huart->Instance->CR1); 02242 uint32_t cr3its = READ_REG(huart->Instance->CR3); 02243 02244 uint32_t errorflags; 02245 uint32_t errorcode; 02246 02247 /* If no error occurs */ 02248 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF)); 02249 if (errorflags == 0U) 02250 { 02251 /* UART in mode Receiver ---------------------------------------------------*/ 02252 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 02253 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02254 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 02255 { 02256 if (huart->RxISR != NULL) 02257 { 02258 huart->RxISR(huart); 02259 } 02260 return; 02261 } 02262 } 02263 02264 /* If some errors occur */ 02265 if ((errorflags != 0U) 02266 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U) 02267 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U)))) 02268 { 02269 /* UART parity error interrupt occurred -------------------------------------*/ 02270 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 02271 { 02272 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); 02273 02274 huart->ErrorCode |= HAL_UART_ERROR_PE; 02275 } 02276 02277 /* UART frame error interrupt occurred --------------------------------------*/ 02278 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02279 { 02280 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); 02281 02282 huart->ErrorCode |= HAL_UART_ERROR_FE; 02283 } 02284 02285 /* UART noise error interrupt occurred --------------------------------------*/ 02286 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02287 { 02288 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); 02289 02290 huart->ErrorCode |= HAL_UART_ERROR_NE; 02291 } 02292 02293 /* UART Over-Run interrupt occurred -----------------------------------------*/ 02294 if (((isrflags & USART_ISR_ORE) != 0U) 02295 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) || 02296 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U))) 02297 { 02298 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF); 02299 02300 huart->ErrorCode |= HAL_UART_ERROR_ORE; 02301 } 02302 02303 /* UART Receiver Timeout interrupt occurred ---------------------------------*/ 02304 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U)) 02305 { 02306 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF); 02307 02308 huart->ErrorCode |= HAL_UART_ERROR_RTO; 02309 } 02310 02311 /* Call UART Error Call back function if need be ----------------------------*/ 02312 if (huart->ErrorCode != HAL_UART_ERROR_NONE) 02313 { 02314 /* UART in mode Receiver --------------------------------------------------*/ 02315 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 02316 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02317 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 02318 { 02319 if (huart->RxISR != NULL) 02320 { 02321 huart->RxISR(huart); 02322 } 02323 } 02324 02325 /* If Error is to be considered as blocking : 02326 - Receiver Timeout error in Reception 02327 - Overrun error in Reception 02328 - any error occurs in DMA mode reception 02329 */ 02330 errorcode = huart->ErrorCode; 02331 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) || 02332 ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U)) 02333 { 02334 /* Blocking error : transfer is aborted 02335 Set the UART state ready to be able to start again the process, 02336 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ 02337 UART_EndRxTransfer(huart); 02338 02339 /* Abort the UART DMA Rx channel if enabled */ 02340 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 02341 { 02342 /* Disable the UART DMA Rx request if enabled */ 02343 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02344 02345 /* Abort the UART DMA Rx channel */ 02346 if (huart->hdmarx != NULL) 02347 { 02348 /* Set the UART DMA Abort callback : 02349 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */ 02350 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError; 02351 02352 /* Abort DMA RX */ 02353 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 02354 { 02355 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */ 02356 huart->hdmarx->XferAbortCallback(huart->hdmarx); 02357 } 02358 } 02359 else 02360 { 02361 /* Call user error callback */ 02362 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02363 /*Call registered error callback*/ 02364 huart->ErrorCallback(huart); 02365 #else 02366 /*Call legacy weak error callback*/ 02367 HAL_UART_ErrorCallback(huart); 02368 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02369 02370 } 02371 } 02372 else 02373 { 02374 /* Call user error callback */ 02375 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02376 /*Call registered error callback*/ 02377 huart->ErrorCallback(huart); 02378 #else 02379 /*Call legacy weak error callback*/ 02380 HAL_UART_ErrorCallback(huart); 02381 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02382 } 02383 } 02384 else 02385 { 02386 /* Non Blocking error : transfer could go on. 02387 Error is notified to user through user error callback */ 02388 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02389 /*Call registered error callback*/ 02390 huart->ErrorCallback(huart); 02391 #else 02392 /*Call legacy weak error callback*/ 02393 HAL_UART_ErrorCallback(huart); 02394 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02395 huart->ErrorCode = HAL_UART_ERROR_NONE; 02396 } 02397 } 02398 return; 02399 02400 } /* End if some error occurs */ 02401 02402 /* Check current reception Mode : 02403 If Reception till IDLE event has been selected : */ 02404 if ((huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 02405 && ((isrflags & USART_ISR_IDLE) != 0U) 02406 && ((cr1its & USART_ISR_IDLE) != 0U)) 02407 { 02408 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF); 02409 02410 /* Check if DMA mode is enabled in UART */ 02411 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 02412 { 02413 /* DMA mode enabled */ 02414 /* Check received length : If all expected data are received, do nothing, 02415 (DMA cplt callback will be called). 02416 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */ 02417 uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx); 02418 if ((nb_remaining_rx_data > 0U) 02419 && (nb_remaining_rx_data < huart->RxXferSize)) 02420 { 02421 /* Reception is not complete */ 02422 huart->RxXferCount = nb_remaining_rx_data; 02423 02424 /* In Normal mode, end DMA xfer and HAL UART Rx process*/ 02425 if (huart->hdmarx->Init.Mode != DMA_CIRCULAR) 02426 { 02427 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 02428 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 02429 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02430 02431 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit 02432 in the UART CR3 register */ 02433 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02434 02435 /* At end of Rx process, restore huart->RxState to Ready */ 02436 huart->RxState = HAL_UART_STATE_READY; 02437 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02438 02439 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 02440 02441 /* Last bytes received, so no need as the abort is immediate */ 02442 (void)HAL_DMA_Abort(huart->hdmarx); 02443 } 02444 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02445 /*Call registered Rx Event callback*/ 02446 huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount)); 02447 #else 02448 /*Call legacy weak Rx Event callback*/ 02449 HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount)); 02450 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 02451 } 02452 return; 02453 } 02454 else 02455 { 02456 /* DMA mode not enabled */ 02457 /* Check received length : If all expected data are received, do nothing. 02458 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */ 02459 uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount; 02460 if ((huart->RxXferCount > 0U) 02461 && (nb_rx_data > 0U)) 02462 { 02463 /* Disable the UART Parity Error Interrupt and RXNE interrupts */ 02464 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 02465 02466 /* Disable the UART Error Interrupt:(Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */ 02467 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 02468 02469 /* Rx process is completed, restore huart->RxState to Ready */ 02470 huart->RxState = HAL_UART_STATE_READY; 02471 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02472 02473 /* Clear RxISR function pointer */ 02474 huart->RxISR = NULL; 02475 02476 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 02477 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02478 /*Call registered Rx complete callback*/ 02479 huart->RxEventCallback(huart, nb_rx_data); 02480 #else 02481 /*Call legacy weak Rx Event callback*/ 02482 HAL_UARTEx_RxEventCallback(huart, nb_rx_data); 02483 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 02484 } 02485 return; 02486 } 02487 } 02488 02489 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/ 02490 if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U)) 02491 { 02492 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF); 02493 02494 /* UART Rx state is not reset as a reception process might be ongoing. 02495 If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */ 02496 02497 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02498 /* Call registered Wakeup Callback */ 02499 huart->WakeupCallback(huart); 02500 #else 02501 /* Call legacy weak Wakeup Callback */ 02502 HAL_UARTEx_WakeupCallback(huart); 02503 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02504 return; 02505 } 02506 02507 /* UART in mode Transmitter ------------------------------------------------*/ 02508 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) 02509 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U) 02510 || ((cr3its & USART_CR3_TXFTIE) != 0U))) 02511 { 02512 if (huart->TxISR != NULL) 02513 { 02514 huart->TxISR(huart); 02515 } 02516 return; 02517 } 02518 02519 /* UART in mode Transmitter (transmission end) -----------------------------*/ 02520 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U)) 02521 { 02522 UART_EndTransmit_IT(huart); 02523 return; 02524 } 02525 02526 /* UART TX Fifo Empty occurred ----------------------------------------------*/ 02527 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U)) 02528 { 02529 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02530 /* Call registered Tx Fifo Empty Callback */ 02531 huart->TxFifoEmptyCallback(huart); 02532 #else 02533 /* Call legacy weak Tx Fifo Empty Callback */ 02534 HAL_UARTEx_TxFifoEmptyCallback(huart); 02535 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02536 return; 02537 } 02538 02539 /* UART RX Fifo Full occurred ----------------------------------------------*/ 02540 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U)) 02541 { 02542 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02543 /* Call registered Rx Fifo Full Callback */ 02544 huart->RxFifoFullCallback(huart); 02545 #else 02546 /* Call legacy weak Rx Fifo Full Callback */ 02547 HAL_UARTEx_RxFifoFullCallback(huart); 02548 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02549 return; 02550 } 02551 } 02552 02553 /** 02554 * @brief Tx Transfer completed callback. 02555 * @param huart UART handle. 02556 * @retval None 02557 */ 02558 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 02559 { 02560 /* Prevent unused argument(s) compilation warning */ 02561 UNUSED(huart); 02562 02563 /* NOTE : This function should not be modified, when the callback is needed, 02564 the HAL_UART_TxCpltCallback can be implemented in the user file. 02565 */ 02566 } 02567 02568 /** 02569 * @brief Tx Half Transfer completed callback. 02570 * @param huart UART handle. 02571 * @retval None 02572 */ 02573 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) 02574 { 02575 /* Prevent unused argument(s) compilation warning */ 02576 UNUSED(huart); 02577 02578 /* NOTE: This function should not be modified, when the callback is needed, 02579 the HAL_UART_TxHalfCpltCallback can be implemented in the user file. 02580 */ 02581 } 02582 02583 /** 02584 * @brief Rx Transfer completed callback. 02585 * @param huart UART handle. 02586 * @retval None 02587 */ 02588 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 02589 { 02590 /* Prevent unused argument(s) compilation warning */ 02591 UNUSED(huart); 02592 02593 /* NOTE : This function should not be modified, when the callback is needed, 02594 the HAL_UART_RxCpltCallback can be implemented in the user file. 02595 */ 02596 } 02597 02598 /** 02599 * @brief Rx Half Transfer completed callback. 02600 * @param huart UART handle. 02601 * @retval None 02602 */ 02603 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) 02604 { 02605 /* Prevent unused argument(s) compilation warning */ 02606 UNUSED(huart); 02607 02608 /* NOTE: This function should not be modified, when the callback is needed, 02609 the HAL_UART_RxHalfCpltCallback can be implemented in the user file. 02610 */ 02611 } 02612 02613 /** 02614 * @brief UART error callback. 02615 * @param huart UART handle. 02616 * @retval None 02617 */ 02618 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 02619 { 02620 /* Prevent unused argument(s) compilation warning */ 02621 UNUSED(huart); 02622 02623 /* NOTE : This function should not be modified, when the callback is needed, 02624 the HAL_UART_ErrorCallback can be implemented in the user file. 02625 */ 02626 } 02627 02628 /** 02629 * @brief UART Abort Complete callback. 02630 * @param huart UART handle. 02631 * @retval None 02632 */ 02633 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart) 02634 { 02635 /* Prevent unused argument(s) compilation warning */ 02636 UNUSED(huart); 02637 02638 /* NOTE : This function should not be modified, when the callback is needed, 02639 the HAL_UART_AbortCpltCallback can be implemented in the user file. 02640 */ 02641 } 02642 02643 /** 02644 * @brief UART Abort Complete callback. 02645 * @param huart UART handle. 02646 * @retval None 02647 */ 02648 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart) 02649 { 02650 /* Prevent unused argument(s) compilation warning */ 02651 UNUSED(huart); 02652 02653 /* NOTE : This function should not be modified, when the callback is needed, 02654 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file. 02655 */ 02656 } 02657 02658 /** 02659 * @brief UART Abort Receive Complete callback. 02660 * @param huart UART handle. 02661 * @retval None 02662 */ 02663 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart) 02664 { 02665 /* Prevent unused argument(s) compilation warning */ 02666 UNUSED(huart); 02667 02668 /* NOTE : This function should not be modified, when the callback is needed, 02669 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file. 02670 */ 02671 } 02672 02673 /** 02674 * @brief Reception Event Callback (Rx event notification called after use of advanced reception service). 02675 * @param huart UART handle 02676 * @param Size Number of data available in application reception buffer (indicates a position in 02677 * reception buffer until which, data are available) 02678 * @retval None 02679 */ 02680 __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) 02681 { 02682 /* Prevent unused argument(s) compilation warning */ 02683 UNUSED(huart); 02684 UNUSED(Size); 02685 02686 /* NOTE : This function should not be modified, when the callback is needed, 02687 the HAL_UARTEx_RxEventCallback can be implemented in the user file. 02688 */ 02689 } 02690 02691 /** 02692 * @} 02693 */ 02694 02695 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions 02696 * @brief UART control functions 02697 * 02698 @verbatim 02699 =============================================================================== 02700 ##### Peripheral Control functions ##### 02701 =============================================================================== 02702 [..] 02703 This subsection provides a set of functions allowing to control the UART. 02704 (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly 02705 (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature 02706 (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature 02707 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode 02708 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode 02709 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode 02710 (+) UART_SetConfig() API configures the UART peripheral 02711 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features 02712 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization 02713 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter 02714 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver 02715 (+) HAL_LIN_SendBreak() API transmits the break characters 02716 @endverbatim 02717 * @{ 02718 */ 02719 02720 /** 02721 * @brief Update on the fly the receiver timeout value in RTOR register. 02722 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02723 * the configuration information for the specified UART module. 02724 * @param TimeoutValue receiver timeout value in number of baud blocks. The timeout 02725 * value must be less or equal to 0x0FFFFFFFF. 02726 * @retval None 02727 */ 02728 void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue) 02729 { 02730 if (!(IS_LPUART_INSTANCE(huart->Instance))) 02731 { 02732 assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue)); 02733 MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue); 02734 } 02735 } 02736 02737 /** 02738 * @brief Enable the UART receiver timeout feature. 02739 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02740 * the configuration information for the specified UART module. 02741 * @retval HAL status 02742 */ 02743 HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart) 02744 { 02745 if (!(IS_LPUART_INSTANCE(huart->Instance))) 02746 { 02747 if (huart->gState == HAL_UART_STATE_READY) 02748 { 02749 /* Process Locked */ 02750 __HAL_LOCK(huart); 02751 02752 huart->gState = HAL_UART_STATE_BUSY; 02753 02754 /* Set the USART RTOEN bit */ 02755 SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN); 02756 02757 huart->gState = HAL_UART_STATE_READY; 02758 02759 /* Process Unlocked */ 02760 __HAL_UNLOCK(huart); 02761 02762 return HAL_OK; 02763 } 02764 else 02765 { 02766 return HAL_BUSY; 02767 } 02768 } 02769 else 02770 { 02771 return HAL_ERROR; 02772 } 02773 } 02774 02775 /** 02776 * @brief Disable the UART receiver timeout feature. 02777 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02778 * the configuration information for the specified UART module. 02779 * @retval HAL status 02780 */ 02781 HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart) 02782 { 02783 if (!(IS_LPUART_INSTANCE(huart->Instance))) 02784 { 02785 if (huart->gState == HAL_UART_STATE_READY) 02786 { 02787 /* Process Locked */ 02788 __HAL_LOCK(huart); 02789 02790 huart->gState = HAL_UART_STATE_BUSY; 02791 02792 /* Clear the USART RTOEN bit */ 02793 CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN); 02794 02795 huart->gState = HAL_UART_STATE_READY; 02796 02797 /* Process Unlocked */ 02798 __HAL_UNLOCK(huart); 02799 02800 return HAL_OK; 02801 } 02802 else 02803 { 02804 return HAL_BUSY; 02805 } 02806 } 02807 else 02808 { 02809 return HAL_ERROR; 02810 } 02811 } 02812 02813 /** 02814 * @brief Enable UART in mute mode (does not mean UART enters mute mode; 02815 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called). 02816 * @param huart UART handle. 02817 * @retval HAL status 02818 */ 02819 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart) 02820 { 02821 __HAL_LOCK(huart); 02822 02823 huart->gState = HAL_UART_STATE_BUSY; 02824 02825 /* Enable USART mute mode by setting the MME bit in the CR1 register */ 02826 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_MME); 02827 02828 huart->gState = HAL_UART_STATE_READY; 02829 02830 return (UART_CheckIdleState(huart)); 02831 } 02832 02833 /** 02834 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode 02835 * as it may not have been in mute mode at this very moment). 02836 * @param huart UART handle. 02837 * @retval HAL status 02838 */ 02839 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart) 02840 { 02841 __HAL_LOCK(huart); 02842 02843 huart->gState = HAL_UART_STATE_BUSY; 02844 02845 /* Disable USART mute mode by clearing the MME bit in the CR1 register */ 02846 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME); 02847 02848 huart->gState = HAL_UART_STATE_READY; 02849 02850 return (UART_CheckIdleState(huart)); 02851 } 02852 02853 /** 02854 * @brief Enter UART mute mode (means UART actually enters mute mode). 02855 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called. 02856 * @param huart UART handle. 02857 * @retval None 02858 */ 02859 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) 02860 { 02861 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST); 02862 } 02863 02864 /** 02865 * @brief Enable the UART transmitter and disable the UART receiver. 02866 * @param huart UART handle. 02867 * @retval HAL status 02868 */ 02869 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) 02870 { 02871 __HAL_LOCK(huart); 02872 huart->gState = HAL_UART_STATE_BUSY; 02873 02874 /* Clear TE and RE bits */ 02875 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); 02876 02877 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */ 02878 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TE); 02879 02880 huart->gState = HAL_UART_STATE_READY; 02881 02882 __HAL_UNLOCK(huart); 02883 02884 return HAL_OK; 02885 } 02886 02887 /** 02888 * @brief Enable the UART receiver and disable the UART transmitter. 02889 * @param huart UART handle. 02890 * @retval HAL status. 02891 */ 02892 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) 02893 { 02894 __HAL_LOCK(huart); 02895 huart->gState = HAL_UART_STATE_BUSY; 02896 02897 /* Clear TE and RE bits */ 02898 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE)); 02899 02900 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */ 02901 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RE); 02902 02903 huart->gState = HAL_UART_STATE_READY; 02904 02905 __HAL_UNLOCK(huart); 02906 02907 return HAL_OK; 02908 } 02909 02910 02911 /** 02912 * @brief Transmit break characters. 02913 * @param huart UART handle. 02914 * @retval HAL status 02915 */ 02916 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) 02917 { 02918 /* Check the parameters */ 02919 assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); 02920 02921 __HAL_LOCK(huart); 02922 02923 huart->gState = HAL_UART_STATE_BUSY; 02924 02925 /* Send break characters */ 02926 __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST); 02927 02928 huart->gState = HAL_UART_STATE_READY; 02929 02930 __HAL_UNLOCK(huart); 02931 02932 return HAL_OK; 02933 } 02934 02935 /** 02936 * @} 02937 */ 02938 02939 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions 02940 * @brief UART Peripheral State functions 02941 * 02942 @verbatim 02943 ============================================================================== 02944 ##### Peripheral State and Error functions ##### 02945 ============================================================================== 02946 [..] 02947 This subsection provides functions allowing to : 02948 (+) Return the UART handle state. 02949 (+) Return the UART handle error code 02950 02951 @endverbatim 02952 * @{ 02953 */ 02954 02955 /** 02956 * @brief Return the UART handle state. 02957 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02958 * the configuration information for the specified UART. 02959 * @retval HAL state 02960 */ 02961 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) 02962 { 02963 uint32_t temp1; 02964 uint32_t temp2; 02965 temp1 = huart->gState; 02966 temp2 = huart->RxState; 02967 02968 return (HAL_UART_StateTypeDef)(temp1 | temp2); 02969 } 02970 02971 /** 02972 * @brief Return the UART handle error code. 02973 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02974 * the configuration information for the specified UART. 02975 * @retval UART Error Code 02976 */ 02977 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart) 02978 { 02979 return huart->ErrorCode; 02980 } 02981 /** 02982 * @} 02983 */ 02984 02985 /** 02986 * @} 02987 */ 02988 02989 /** @defgroup UART_Private_Functions UART Private Functions 02990 * @{ 02991 */ 02992 02993 /** 02994 * @brief Initialize the callbacks to their default values. 02995 * @param huart UART handle. 02996 * @retval none 02997 */ 02998 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02999 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart) 03000 { 03001 /* Init the UART Callback settings */ 03002 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 03003 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 03004 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 03005 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 03006 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */ 03007 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 03008 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */ 03009 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */ 03010 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */ 03011 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */ 03012 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */ 03013 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak RxEventCallback */ 03014 03015 } 03016 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03017 03018 /** 03019 * @brief Configure the UART peripheral. 03020 * @param huart UART handle. 03021 * @retval HAL status 03022 */ 03023 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart) 03024 { 03025 uint32_t tmpreg; 03026 uint16_t brrtemp; 03027 UART_ClockSourceTypeDef clocksource; 03028 uint32_t usartdiv; 03029 HAL_StatusTypeDef ret = HAL_OK; 03030 uint32_t lpuart_ker_ck_pres; 03031 PLL2_ClocksTypeDef pll2_clocks; 03032 PLL3_ClocksTypeDef pll3_clocks; 03033 uint32_t pclk; 03034 03035 /* Check the parameters */ 03036 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); 03037 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 03038 if (UART_INSTANCE_LOWPOWER(huart)) 03039 { 03040 assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits)); 03041 } 03042 else 03043 { 03044 assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); 03045 assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling)); 03046 } 03047 03048 assert_param(IS_UART_PARITY(huart->Init.Parity)); 03049 assert_param(IS_UART_MODE(huart->Init.Mode)); 03050 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); 03051 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 03052 assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler)); 03053 03054 /*-------------------------- USART CR1 Configuration -----------------------*/ 03055 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure 03056 * the UART Word Length, Parity, Mode and oversampling: 03057 * set the M bits according to huart->Init.WordLength value 03058 * set PCE and PS bits according to huart->Init.Parity value 03059 * set TE and RE bits according to huart->Init.Mode value 03060 * set OVER8 bit according to huart->Init.OverSampling value */ 03061 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ; 03062 MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg); 03063 03064 /*-------------------------- USART CR2 Configuration -----------------------*/ 03065 /* Configure the UART Stop Bits: Set STOP[13:12] bits according 03066 * to huart->Init.StopBits value */ 03067 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits); 03068 03069 /*-------------------------- USART CR3 Configuration -----------------------*/ 03070 /* Configure 03071 * - UART HardWare Flow Control: set CTSE and RTSE bits according 03072 * to huart->Init.HwFlowCtl value 03073 * - one-bit sampling method versus three samples' majority rule according 03074 * to huart->Init.OneBitSampling (not applicable to LPUART) */ 03075 tmpreg = (uint32_t)huart->Init.HwFlowCtl; 03076 03077 if (!(UART_INSTANCE_LOWPOWER(huart))) 03078 { 03079 tmpreg |= huart->Init.OneBitSampling; 03080 } 03081 MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg); 03082 03083 /*-------------------------- USART PRESC Configuration -----------------------*/ 03084 /* Configure 03085 * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */ 03086 MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler); 03087 03088 /*-------------------------- USART BRR Configuration -----------------------*/ 03089 UART_GETCLOCKSOURCE(huart, clocksource); 03090 03091 /* Check LPUART instance */ 03092 if (UART_INSTANCE_LOWPOWER(huart)) 03093 { 03094 /* Retrieve frequency clock */ 03095 switch (clocksource) 03096 { 03097 case UART_CLOCKSOURCE_D3PCLK1: 03098 pclk = HAL_RCCEx_GetD3PCLK1Freq(); 03099 break; 03100 case UART_CLOCKSOURCE_PLL2: 03101 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks); 03102 pclk = pll2_clocks.PLL2_Q_Frequency; 03103 break; 03104 case UART_CLOCKSOURCE_PLL3: 03105 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks); 03106 pclk = pll3_clocks.PLL3_Q_Frequency; 03107 break; 03108 case UART_CLOCKSOURCE_HSI: 03109 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U) 03110 { 03111 pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)); 03112 } 03113 else 03114 { 03115 pclk = (uint32_t) HSI_VALUE; 03116 } 03117 break; 03118 case UART_CLOCKSOURCE_CSI: 03119 pclk = (uint32_t) CSI_VALUE; 03120 break; 03121 case UART_CLOCKSOURCE_LSE: 03122 pclk = (uint32_t) LSE_VALUE; 03123 break; 03124 default: 03125 pclk = 0U; 03126 ret = HAL_ERROR; 03127 break; 03128 } 03129 03130 /* If proper clock source reported */ 03131 if (pclk != 0U) 03132 { 03133 /* Compute clock after Prescaler */ 03134 lpuart_ker_ck_pres = (pclk / UARTPrescTable[huart->Init.ClockPrescaler]); 03135 03136 /* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */ 03137 if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) || 03138 (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate))) 03139 { 03140 ret = HAL_ERROR; 03141 } 03142 else 03143 { 03144 /* Check computed UsartDiv value is in allocated range 03145 (it is forbidden to write values lower than 0x300 in the LPUART_BRR register) */ 03146 usartdiv = (uint32_t)(UART_DIV_LPUART(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler)); 03147 if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX)) 03148 { 03149 huart->Instance->BRR = usartdiv; 03150 } 03151 else 03152 { 03153 ret = HAL_ERROR; 03154 } 03155 } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) || 03156 (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */ 03157 } /* if (pclk != 0) */ 03158 } 03159 /* Check UART Over Sampling to set Baud Rate Register */ 03160 else if (huart->Init.OverSampling == UART_OVERSAMPLING_8) 03161 { 03162 switch (clocksource) 03163 { 03164 case UART_CLOCKSOURCE_D2PCLK1: 03165 pclk = HAL_RCC_GetPCLK1Freq(); 03166 break; 03167 case UART_CLOCKSOURCE_D2PCLK2: 03168 pclk = HAL_RCC_GetPCLK2Freq(); 03169 break; 03170 case UART_CLOCKSOURCE_PLL2: 03171 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks); 03172 pclk = pll2_clocks.PLL2_Q_Frequency; 03173 break; 03174 case UART_CLOCKSOURCE_PLL3: 03175 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks); 03176 pclk = pll3_clocks.PLL3_Q_Frequency; 03177 break; 03178 case UART_CLOCKSOURCE_HSI: 03179 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U) 03180 { 03181 pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)); 03182 } 03183 else 03184 { 03185 pclk = (uint32_t) HSI_VALUE; 03186 } 03187 break; 03188 case UART_CLOCKSOURCE_CSI: 03189 pclk = (uint32_t) CSI_VALUE; 03190 break; 03191 case UART_CLOCKSOURCE_LSE: 03192 pclk = (uint32_t) LSE_VALUE; 03193 break; 03194 default: 03195 pclk = 0U; 03196 ret = HAL_ERROR; 03197 break; 03198 } 03199 03200 /* USARTDIV must be greater than or equal to 0d16 */ 03201 if (pclk != 0U) 03202 { 03203 usartdiv = (uint32_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler)); 03204 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX)) 03205 { 03206 brrtemp = (uint16_t)(usartdiv & 0xFFF0U); 03207 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U); 03208 huart->Instance->BRR = brrtemp; 03209 } 03210 else 03211 { 03212 ret = HAL_ERROR; 03213 } 03214 } 03215 } 03216 else 03217 { 03218 switch (clocksource) 03219 { 03220 case UART_CLOCKSOURCE_D2PCLK1: 03221 pclk = HAL_RCC_GetPCLK1Freq(); 03222 break; 03223 case UART_CLOCKSOURCE_D2PCLK2: 03224 pclk = HAL_RCC_GetPCLK2Freq(); 03225 break; 03226 case UART_CLOCKSOURCE_PLL2: 03227 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks); 03228 pclk = pll2_clocks.PLL2_Q_Frequency; 03229 break; 03230 case UART_CLOCKSOURCE_PLL3: 03231 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks); 03232 pclk = pll3_clocks.PLL3_Q_Frequency; 03233 break; 03234 case UART_CLOCKSOURCE_HSI: 03235 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U) 03236 { 03237 pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)); 03238 } 03239 else 03240 { 03241 pclk = (uint32_t) HSI_VALUE; 03242 } 03243 break; 03244 case UART_CLOCKSOURCE_CSI: 03245 pclk = (uint32_t) CSI_VALUE; 03246 break; 03247 case UART_CLOCKSOURCE_LSE: 03248 pclk = (uint32_t) LSE_VALUE; 03249 break; 03250 default: 03251 pclk = 0U; 03252 ret = HAL_ERROR; 03253 break; 03254 } 03255 03256 if (pclk != 0U) 03257 { 03258 /* USARTDIV must be greater than or equal to 0d16 */ 03259 usartdiv = (uint32_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler)); 03260 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX)) 03261 { 03262 huart->Instance->BRR = (uint16_t)usartdiv; 03263 } 03264 else 03265 { 03266 ret = HAL_ERROR; 03267 } 03268 } 03269 } 03270 03271 /* Initialize the number of data to process during RX/TX ISR execution */ 03272 huart->NbTxDataToProcess = 1; 03273 huart->NbRxDataToProcess = 1; 03274 03275 /* Clear ISR function pointers */ 03276 huart->RxISR = NULL; 03277 huart->TxISR = NULL; 03278 03279 return ret; 03280 } 03281 03282 /** 03283 * @brief Configure the UART peripheral advanced features. 03284 * @param huart UART handle. 03285 * @retval None 03286 */ 03287 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart) 03288 { 03289 /* Check whether the set of advanced features to configure is properly set */ 03290 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit)); 03291 03292 /* if required, configure TX pin active level inversion */ 03293 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT)) 03294 { 03295 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert)); 03296 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert); 03297 } 03298 03299 /* if required, configure RX pin active level inversion */ 03300 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT)) 03301 { 03302 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert)); 03303 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert); 03304 } 03305 03306 /* if required, configure data inversion */ 03307 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT)) 03308 { 03309 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert)); 03310 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert); 03311 } 03312 03313 /* if required, configure RX/TX pins swap */ 03314 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT)) 03315 { 03316 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap)); 03317 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap); 03318 } 03319 03320 /* if required, configure RX overrun detection disabling */ 03321 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT)) 03322 { 03323 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable)); 03324 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable); 03325 } 03326 03327 /* if required, configure DMA disabling on reception error */ 03328 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT)) 03329 { 03330 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError)); 03331 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError); 03332 } 03333 03334 /* if required, configure auto Baud rate detection scheme */ 03335 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT)) 03336 { 03337 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance)); 03338 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable)); 03339 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable); 03340 /* set auto Baudrate detection parameters if detection is enabled */ 03341 if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE) 03342 { 03343 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode)); 03344 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode); 03345 } 03346 } 03347 03348 /* if required, configure MSB first on communication line */ 03349 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT)) 03350 { 03351 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst)); 03352 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst); 03353 } 03354 } 03355 03356 /** 03357 * @brief Check the UART Idle State. 03358 * @param huart UART handle. 03359 * @retval HAL status 03360 */ 03361 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart) 03362 { 03363 uint32_t tickstart; 03364 03365 /* Initialize the UART ErrorCode */ 03366 huart->ErrorCode = HAL_UART_ERROR_NONE; 03367 03368 /* Init tickstart for timeout management */ 03369 tickstart = HAL_GetTick(); 03370 03371 /* Check if the Transmitter is enabled */ 03372 if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE) 03373 { 03374 /* Wait until TEACK flag is set */ 03375 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK) 03376 { 03377 /* Timeout occurred */ 03378 return HAL_TIMEOUT; 03379 } 03380 } 03381 03382 /* Check if the Receiver is enabled */ 03383 if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE) 03384 { 03385 /* Wait until REACK flag is set */ 03386 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK) 03387 { 03388 /* Timeout occurred */ 03389 return HAL_TIMEOUT; 03390 } 03391 } 03392 03393 /* Initialize the UART State */ 03394 huart->gState = HAL_UART_STATE_READY; 03395 huart->RxState = HAL_UART_STATE_READY; 03396 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03397 03398 __HAL_UNLOCK(huart); 03399 03400 return HAL_OK; 03401 } 03402 03403 /** 03404 * @brief This function handles UART Communication Timeout. It waits 03405 * until a flag is no longer in the specified status. 03406 * @param huart UART handle. 03407 * @param Flag Specifies the UART flag to check 03408 * @param Status The actual Flag status (SET or RESET) 03409 * @param Tickstart Tick start value 03410 * @param Timeout Timeout duration 03411 * @retval HAL status 03412 */ 03413 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, 03414 uint32_t Tickstart, uint32_t Timeout) 03415 { 03416 /* Wait until flag is set */ 03417 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) 03418 { 03419 /* Check for the Timeout */ 03420 if (Timeout != HAL_MAX_DELAY) 03421 { 03422 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 03423 { 03424 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) 03425 interrupts for the interrupt process */ 03426 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | 03427 USART_CR1_TXEIE_TXFNFIE)); 03428 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03429 03430 huart->gState = HAL_UART_STATE_READY; 03431 huart->RxState = HAL_UART_STATE_READY; 03432 03433 __HAL_UNLOCK(huart); 03434 03435 return HAL_TIMEOUT; 03436 } 03437 03438 if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U) 03439 { 03440 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET) 03441 { 03442 /* Clear Receiver Timeout flag*/ 03443 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF); 03444 03445 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) 03446 interrupts for the interrupt process */ 03447 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | 03448 USART_CR1_TXEIE_TXFNFIE)); 03449 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03450 03451 huart->gState = HAL_UART_STATE_READY; 03452 huart->RxState = HAL_UART_STATE_READY; 03453 huart->ErrorCode = HAL_UART_ERROR_RTO; 03454 03455 /* Process Unlocked */ 03456 __HAL_UNLOCK(huart); 03457 03458 return HAL_TIMEOUT; 03459 } 03460 } 03461 } 03462 } 03463 return HAL_OK; 03464 } 03465 03466 /** 03467 * @brief Start Receive operation in interrupt mode. 03468 * @note This function could be called by all HAL UART API providing reception in Interrupt mode. 03469 * @note When calling this function, parameters validity is considered as already checked, 03470 * i.e. Rx State, buffer address, ... 03471 * UART Handle is assumed as Locked. 03472 * @param huart UART handle. 03473 * @param pData Pointer to data buffer (u8 or u16 data elements). 03474 * @param Size Amount of data elements (u8 or u16) to be received. 03475 * @retval HAL status 03476 */ 03477 HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 03478 { 03479 huart->pRxBuffPtr = pData; 03480 huart->RxXferSize = Size; 03481 huart->RxXferCount = Size; 03482 huart->RxISR = NULL; 03483 03484 /* Computation of UART mask to apply to RDR register */ 03485 UART_MASK_COMPUTATION(huart); 03486 03487 huart->ErrorCode = HAL_UART_ERROR_NONE; 03488 huart->RxState = HAL_UART_STATE_BUSY_RX; 03489 03490 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 03491 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 03492 03493 /* Configure Rx interrupt processing */ 03494 if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess)) 03495 { 03496 /* Set the Rx ISR function pointer according to the data word length */ 03497 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 03498 { 03499 huart->RxISR = UART_RxISR_16BIT_FIFOEN; 03500 } 03501 else 03502 { 03503 huart->RxISR = UART_RxISR_8BIT_FIFOEN; 03504 } 03505 03506 __HAL_UNLOCK(huart); 03507 03508 /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */ 03509 if (huart->Init.Parity != UART_PARITY_NONE) 03510 { 03511 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 03512 } 03513 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE); 03514 } 03515 else 03516 { 03517 /* Set the Rx ISR function pointer according to the data word length */ 03518 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 03519 { 03520 huart->RxISR = UART_RxISR_16BIT; 03521 } 03522 else 03523 { 03524 huart->RxISR = UART_RxISR_8BIT; 03525 } 03526 03527 __HAL_UNLOCK(huart); 03528 03529 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */ 03530 if (huart->Init.Parity != UART_PARITY_NONE) 03531 { 03532 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE); 03533 } 03534 else 03535 { 03536 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03537 } 03538 } 03539 return HAL_OK; 03540 } 03541 03542 /** 03543 * @brief Start Receive operation in DMA mode. 03544 * @note This function could be called by all HAL UART API providing reception in DMA mode. 03545 * @note When calling this function, parameters validity is considered as already checked, 03546 * i.e. Rx State, buffer address, ... 03547 * UART Handle is assumed as Locked. 03548 * @param huart UART handle. 03549 * @param pData Pointer to data buffer (u8 or u16 data elements). 03550 * @param Size Amount of data elements (u8 or u16) to be received. 03551 * @retval HAL status 03552 */ 03553 HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 03554 { 03555 huart->pRxBuffPtr = pData; 03556 huart->RxXferSize = Size; 03557 03558 huart->ErrorCode = HAL_UART_ERROR_NONE; 03559 huart->RxState = HAL_UART_STATE_BUSY_RX; 03560 03561 if (huart->hdmarx != NULL) 03562 { 03563 /* Set the UART DMA transfer complete callback */ 03564 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; 03565 03566 /* Set the UART DMA Half transfer complete callback */ 03567 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; 03568 03569 /* Set the DMA error callback */ 03570 huart->hdmarx->XferErrorCallback = UART_DMAError; 03571 03572 /* Set the DMA abort callback */ 03573 huart->hdmarx->XferAbortCallback = NULL; 03574 03575 /* Enable the DMA channel */ 03576 if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK) 03577 { 03578 /* Set error code to DMA */ 03579 huart->ErrorCode = HAL_UART_ERROR_DMA; 03580 03581 __HAL_UNLOCK(huart); 03582 03583 /* Restore huart->RxState to ready */ 03584 huart->RxState = HAL_UART_STATE_READY; 03585 03586 return HAL_ERROR; 03587 } 03588 } 03589 __HAL_UNLOCK(huart); 03590 03591 /* Enable the UART Parity Error Interrupt */ 03592 if (huart->Init.Parity != UART_PARITY_NONE) 03593 { 03594 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 03595 } 03596 03597 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 03598 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 03599 03600 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 03601 in the UART CR3 register */ 03602 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 03603 03604 return HAL_OK; 03605 } 03606 03607 03608 /** 03609 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion). 03610 * @param huart UART handle. 03611 * @retval None 03612 */ 03613 static void UART_EndTxTransfer(UART_HandleTypeDef *huart) 03614 { 03615 /* Disable TXEIE, TCIE, TXFT interrupts */ 03616 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE)); 03617 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE)); 03618 03619 /* At end of Tx process, restore huart->gState to Ready */ 03620 huart->gState = HAL_UART_STATE_READY; 03621 } 03622 03623 03624 /** 03625 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). 03626 * @param huart UART handle. 03627 * @retval None 03628 */ 03629 static void UART_EndRxTransfer(UART_HandleTypeDef *huart) 03630 { 03631 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 03632 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 03633 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 03634 03635 /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */ 03636 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03637 { 03638 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 03639 } 03640 03641 /* At end of Rx process, restore huart->RxState to Ready */ 03642 huart->RxState = HAL_UART_STATE_READY; 03643 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03644 03645 /* Reset RxIsr function pointer */ 03646 huart->RxISR = NULL; 03647 } 03648 03649 03650 /** 03651 * @brief DMA UART transmit process complete callback. 03652 * @param hdma DMA handle. 03653 * @retval None 03654 */ 03655 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma) 03656 { 03657 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03658 03659 /* DMA Normal mode */ 03660 if (hdma->Init.Mode != DMA_CIRCULAR) 03661 { 03662 huart->TxXferCount = 0U; 03663 03664 /* Disable the DMA transfer for transmit request by resetting the DMAT bit 03665 in the UART CR3 register */ 03666 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 03667 03668 /* Enable the UART Transmit Complete Interrupt */ 03669 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 03670 } 03671 /* DMA Circular mode */ 03672 else 03673 { 03674 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03675 /*Call registered Tx complete callback*/ 03676 huart->TxCpltCallback(huart); 03677 #else 03678 /*Call legacy weak Tx complete callback*/ 03679 HAL_UART_TxCpltCallback(huart); 03680 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03681 } 03682 } 03683 03684 /** 03685 * @brief DMA UART transmit process half complete callback. 03686 * @param hdma DMA handle. 03687 * @retval None 03688 */ 03689 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 03690 { 03691 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03692 03693 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03694 /*Call registered Tx Half complete callback*/ 03695 huart->TxHalfCpltCallback(huart); 03696 #else 03697 /*Call legacy weak Tx Half complete callback*/ 03698 HAL_UART_TxHalfCpltCallback(huart); 03699 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03700 } 03701 03702 /** 03703 * @brief DMA UART receive process complete callback. 03704 * @param hdma DMA handle. 03705 * @retval None 03706 */ 03707 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 03708 { 03709 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03710 03711 /* DMA Normal mode */ 03712 if (hdma->Init.Mode != DMA_CIRCULAR) 03713 { 03714 huart->RxXferCount = 0U; 03715 03716 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 03717 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 03718 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03719 03720 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit 03721 in the UART CR3 register */ 03722 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 03723 03724 /* At end of Rx process, restore huart->RxState to Ready */ 03725 huart->RxState = HAL_UART_STATE_READY; 03726 03727 /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */ 03728 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03729 { 03730 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 03731 } 03732 } 03733 03734 /* Check current reception Mode : 03735 If Reception till IDLE event has been selected : use Rx Event callback */ 03736 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03737 { 03738 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03739 /*Call registered Rx Event callback*/ 03740 huart->RxEventCallback(huart, huart->RxXferSize); 03741 #else 03742 /*Call legacy weak Rx Event callback*/ 03743 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 03744 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03745 } 03746 else 03747 { 03748 /* In other cases : use Rx Complete callback */ 03749 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03750 /*Call registered Rx complete callback*/ 03751 huart->RxCpltCallback(huart); 03752 #else 03753 /*Call legacy weak Rx complete callback*/ 03754 HAL_UART_RxCpltCallback(huart); 03755 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03756 } 03757 } 03758 03759 /** 03760 * @brief DMA UART receive process half complete callback. 03761 * @param hdma DMA handle. 03762 * @retval None 03763 */ 03764 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 03765 { 03766 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03767 03768 /* Check current reception Mode : 03769 If Reception till IDLE event has been selected : use Rx Event callback */ 03770 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03771 { 03772 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03773 /*Call registered Rx Event callback*/ 03774 huart->RxEventCallback(huart, huart->RxXferSize / 2U); 03775 #else 03776 /*Call legacy weak Rx Event callback*/ 03777 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U); 03778 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03779 } 03780 else 03781 { 03782 /* In other cases : use Rx Half Complete callback */ 03783 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03784 /*Call registered Rx Half complete callback*/ 03785 huart->RxHalfCpltCallback(huart); 03786 #else 03787 /*Call legacy weak Rx Half complete callback*/ 03788 HAL_UART_RxHalfCpltCallback(huart); 03789 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03790 } 03791 } 03792 03793 /** 03794 * @brief DMA UART communication error callback. 03795 * @param hdma DMA handle. 03796 * @retval None 03797 */ 03798 static void UART_DMAError(DMA_HandleTypeDef *hdma) 03799 { 03800 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03801 03802 const HAL_UART_StateTypeDef gstate = huart->gState; 03803 const HAL_UART_StateTypeDef rxstate = huart->RxState; 03804 03805 /* Stop UART DMA Tx request if ongoing */ 03806 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) && 03807 (gstate == HAL_UART_STATE_BUSY_TX)) 03808 { 03809 huart->TxXferCount = 0U; 03810 UART_EndTxTransfer(huart); 03811 } 03812 03813 /* Stop UART DMA Rx request if ongoing */ 03814 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) && 03815 (rxstate == HAL_UART_STATE_BUSY_RX)) 03816 { 03817 huart->RxXferCount = 0U; 03818 UART_EndRxTransfer(huart); 03819 } 03820 03821 huart->ErrorCode |= HAL_UART_ERROR_DMA; 03822 03823 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03824 /*Call registered error callback*/ 03825 huart->ErrorCallback(huart); 03826 #else 03827 /*Call legacy weak error callback*/ 03828 HAL_UART_ErrorCallback(huart); 03829 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03830 } 03831 03832 /** 03833 * @brief DMA UART communication abort callback, when initiated by HAL services on Error 03834 * (To be called at end of DMA Abort procedure following error occurrence). 03835 * @param hdma DMA handle. 03836 * @retval None 03837 */ 03838 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma) 03839 { 03840 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03841 huart->RxXferCount = 0U; 03842 huart->TxXferCount = 0U; 03843 03844 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03845 /*Call registered error callback*/ 03846 huart->ErrorCallback(huart); 03847 #else 03848 /*Call legacy weak error callback*/ 03849 HAL_UART_ErrorCallback(huart); 03850 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03851 } 03852 03853 /** 03854 * @brief DMA UART Tx communication abort callback, when initiated by user 03855 * (To be called at end of DMA Tx Abort procedure following user abort request). 03856 * @note When this callback is executed, User Abort complete call back is called only if no 03857 * Abort still ongoing for Rx DMA Handle. 03858 * @param hdma DMA handle. 03859 * @retval None 03860 */ 03861 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) 03862 { 03863 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03864 03865 huart->hdmatx->XferAbortCallback = NULL; 03866 03867 /* Check if an Abort process is still ongoing */ 03868 if (huart->hdmarx != NULL) 03869 { 03870 if (huart->hdmarx->XferAbortCallback != NULL) 03871 { 03872 return; 03873 } 03874 } 03875 03876 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 03877 huart->TxXferCount = 0U; 03878 huart->RxXferCount = 0U; 03879 03880 /* Reset errorCode */ 03881 huart->ErrorCode = HAL_UART_ERROR_NONE; 03882 03883 /* Clear the Error flags in the ICR register */ 03884 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 03885 03886 /* Flush the whole TX FIFO (if needed) */ 03887 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 03888 { 03889 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 03890 } 03891 03892 /* Restore huart->gState and huart->RxState to Ready */ 03893 huart->gState = HAL_UART_STATE_READY; 03894 huart->RxState = HAL_UART_STATE_READY; 03895 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03896 03897 /* Call user Abort complete callback */ 03898 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03899 /* Call registered Abort complete callback */ 03900 huart->AbortCpltCallback(huart); 03901 #else 03902 /* Call legacy weak Abort complete callback */ 03903 HAL_UART_AbortCpltCallback(huart); 03904 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03905 } 03906 03907 03908 /** 03909 * @brief DMA UART Rx communication abort callback, when initiated by user 03910 * (To be called at end of DMA Rx Abort procedure following user abort request). 03911 * @note When this callback is executed, User Abort complete call back is called only if no 03912 * Abort still ongoing for Tx DMA Handle. 03913 * @param hdma DMA handle. 03914 * @retval None 03915 */ 03916 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) 03917 { 03918 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03919 03920 huart->hdmarx->XferAbortCallback = NULL; 03921 03922 /* Check if an Abort process is still ongoing */ 03923 if (huart->hdmatx != NULL) 03924 { 03925 if (huart->hdmatx->XferAbortCallback != NULL) 03926 { 03927 return; 03928 } 03929 } 03930 03931 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 03932 huart->TxXferCount = 0U; 03933 huart->RxXferCount = 0U; 03934 03935 /* Reset errorCode */ 03936 huart->ErrorCode = HAL_UART_ERROR_NONE; 03937 03938 /* Clear the Error flags in the ICR register */ 03939 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 03940 03941 /* Discard the received data */ 03942 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 03943 03944 /* Restore huart->gState and huart->RxState to Ready */ 03945 huart->gState = HAL_UART_STATE_READY; 03946 huart->RxState = HAL_UART_STATE_READY; 03947 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03948 03949 /* Call user Abort complete callback */ 03950 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03951 /* Call registered Abort complete callback */ 03952 huart->AbortCpltCallback(huart); 03953 #else 03954 /* Call legacy weak Abort complete callback */ 03955 HAL_UART_AbortCpltCallback(huart); 03956 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03957 } 03958 03959 03960 /** 03961 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to 03962 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer) 03963 * (This callback is executed at end of DMA Tx Abort procedure following user abort request, 03964 * and leads to user Tx Abort Complete callback execution). 03965 * @param hdma DMA handle. 03966 * @retval None 03967 */ 03968 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 03969 { 03970 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent); 03971 03972 huart->TxXferCount = 0U; 03973 03974 /* Flush the whole TX FIFO (if needed) */ 03975 if (huart->FifoMode == UART_FIFOMODE_ENABLE) 03976 { 03977 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST); 03978 } 03979 03980 /* Restore huart->gState to Ready */ 03981 huart->gState = HAL_UART_STATE_READY; 03982 03983 /* Call user Abort complete callback */ 03984 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03985 /* Call registered Abort Transmit Complete Callback */ 03986 huart->AbortTransmitCpltCallback(huart); 03987 #else 03988 /* Call legacy weak Abort Transmit Complete Callback */ 03989 HAL_UART_AbortTransmitCpltCallback(huart); 03990 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03991 } 03992 03993 /** 03994 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to 03995 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer) 03996 * (This callback is executed at end of DMA Rx Abort procedure following user abort request, 03997 * and leads to user Rx Abort Complete callback execution). 03998 * @param hdma DMA handle. 03999 * @retval None 04000 */ 04001 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 04002 { 04003 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 04004 04005 huart->RxXferCount = 0U; 04006 04007 /* Clear the Error flags in the ICR register */ 04008 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF); 04009 04010 /* Discard the received data */ 04011 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 04012 04013 /* Restore huart->RxState to Ready */ 04014 huart->RxState = HAL_UART_STATE_READY; 04015 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 04016 04017 /* Call user Abort complete callback */ 04018 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04019 /* Call registered Abort Receive Complete Callback */ 04020 huart->AbortReceiveCpltCallback(huart); 04021 #else 04022 /* Call legacy weak Abort Receive Complete Callback */ 04023 HAL_UART_AbortReceiveCpltCallback(huart); 04024 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04025 } 04026 04027 /** 04028 * @brief TX interrupt handler for 7 or 8 bits data word length . 04029 * @note Function is called under interruption only, once 04030 * interruptions have been enabled by HAL_UART_Transmit_IT(). 04031 * @param huart UART handle. 04032 * @retval None 04033 */ 04034 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart) 04035 { 04036 /* Check that a Tx process is ongoing */ 04037 if (huart->gState == HAL_UART_STATE_BUSY_TX) 04038 { 04039 if (huart->TxXferCount == 0U) 04040 { 04041 /* Disable the UART Transmit Data Register Empty Interrupt */ 04042 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 04043 04044 /* Enable the UART Transmit Complete Interrupt */ 04045 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 04046 } 04047 else 04048 { 04049 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF); 04050 huart->pTxBuffPtr++; 04051 huart->TxXferCount--; 04052 } 04053 } 04054 } 04055 04056 /** 04057 * @brief TX interrupt handler for 9 bits data word length. 04058 * @note Function is called under interruption only, once 04059 * interruptions have been enabled by HAL_UART_Transmit_IT(). 04060 * @param huart UART handle. 04061 * @retval None 04062 */ 04063 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart) 04064 { 04065 const uint16_t *tmp; 04066 04067 /* Check that a Tx process is ongoing */ 04068 if (huart->gState == HAL_UART_STATE_BUSY_TX) 04069 { 04070 if (huart->TxXferCount == 0U) 04071 { 04072 /* Disable the UART Transmit Data Register Empty Interrupt */ 04073 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 04074 04075 /* Enable the UART Transmit Complete Interrupt */ 04076 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 04077 } 04078 else 04079 { 04080 tmp = (const uint16_t *) huart->pTxBuffPtr; 04081 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL); 04082 huart->pTxBuffPtr += 2U; 04083 huart->TxXferCount--; 04084 } 04085 } 04086 } 04087 04088 /** 04089 * @brief TX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled. 04090 * @note Function is called under interruption only, once 04091 * interruptions have been enabled by HAL_UART_Transmit_IT(). 04092 * @param huart UART handle. 04093 * @retval None 04094 */ 04095 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart) 04096 { 04097 uint16_t nb_tx_data; 04098 04099 /* Check that a Tx process is ongoing */ 04100 if (huart->gState == HAL_UART_STATE_BUSY_TX) 04101 { 04102 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--) 04103 { 04104 if (huart->TxXferCount == 0U) 04105 { 04106 /* Disable the TX FIFO threshold interrupt */ 04107 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE); 04108 04109 /* Enable the UART Transmit Complete Interrupt */ 04110 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 04111 04112 break; /* force exit loop */ 04113 } 04114 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U) 04115 { 04116 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF); 04117 huart->pTxBuffPtr++; 04118 huart->TxXferCount--; 04119 } 04120 else 04121 { 04122 /* Nothing to do */ 04123 } 04124 } 04125 } 04126 } 04127 04128 /** 04129 * @brief TX interrupt handler for 9 bits data word length and FIFO mode is enabled. 04130 * @note Function is called under interruption only, once 04131 * interruptions have been enabled by HAL_UART_Transmit_IT(). 04132 * @param huart UART handle. 04133 * @retval None 04134 */ 04135 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart) 04136 { 04137 const uint16_t *tmp; 04138 uint16_t nb_tx_data; 04139 04140 /* Check that a Tx process is ongoing */ 04141 if (huart->gState == HAL_UART_STATE_BUSY_TX) 04142 { 04143 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--) 04144 { 04145 if (huart->TxXferCount == 0U) 04146 { 04147 /* Disable the TX FIFO threshold interrupt */ 04148 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE); 04149 04150 /* Enable the UART Transmit Complete Interrupt */ 04151 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 04152 04153 break; /* force exit loop */ 04154 } 04155 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U) 04156 { 04157 tmp = (const uint16_t *) huart->pTxBuffPtr; 04158 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL); 04159 huart->pTxBuffPtr += 2U; 04160 huart->TxXferCount--; 04161 } 04162 else 04163 { 04164 /* Nothing to do */ 04165 } 04166 } 04167 } 04168 } 04169 04170 /** 04171 * @brief Wrap up transmission in non-blocking mode. 04172 * @param huart pointer to a UART_HandleTypeDef structure that contains 04173 * the configuration information for the specified UART module. 04174 * @retval None 04175 */ 04176 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart) 04177 { 04178 /* Disable the UART Transmit Complete Interrupt */ 04179 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE); 04180 04181 /* Tx process is ended, restore huart->gState to Ready */ 04182 huart->gState = HAL_UART_STATE_READY; 04183 04184 /* Cleat TxISR function pointer */ 04185 huart->TxISR = NULL; 04186 04187 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04188 /*Call registered Tx complete callback*/ 04189 huart->TxCpltCallback(huart); 04190 #else 04191 /*Call legacy weak Tx complete callback*/ 04192 HAL_UART_TxCpltCallback(huart); 04193 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04194 } 04195 04196 /** 04197 * @brief RX interrupt handler for 7 or 8 bits data word length . 04198 * @param huart UART handle. 04199 * @retval None 04200 */ 04201 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart) 04202 { 04203 uint16_t uhMask = huart->Mask; 04204 uint16_t uhdata; 04205 04206 /* Check that a Rx process is ongoing */ 04207 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 04208 { 04209 uhdata = (uint16_t) READ_REG(huart->Instance->RDR); 04210 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask); 04211 huart->pRxBuffPtr++; 04212 huart->RxXferCount--; 04213 04214 if (huart->RxXferCount == 0U) 04215 { 04216 /* Disable the UART Parity Error Interrupt and RXNE interrupts */ 04217 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 04218 04219 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 04220 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 04221 04222 /* Rx process is completed, restore huart->RxState to Ready */ 04223 huart->RxState = HAL_UART_STATE_READY; 04224 04225 /* Clear RxISR function pointer */ 04226 huart->RxISR = NULL; 04227 04228 /* Check current reception Mode : 04229 If Reception till IDLE event has been selected : */ 04230 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 04231 { 04232 /* Set reception type to Standard */ 04233 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 04234 04235 /* Disable IDLE interrupt */ 04236 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 04237 04238 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET) 04239 { 04240 /* Clear IDLE Flag */ 04241 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF); 04242 } 04243 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04244 /*Call registered Rx Event callback*/ 04245 huart->RxEventCallback(huart, huart->RxXferSize); 04246 #else 04247 /*Call legacy weak Rx Event callback*/ 04248 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 04249 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 04250 } 04251 else 04252 { 04253 /* Standard reception API called */ 04254 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04255 /*Call registered Rx complete callback*/ 04256 huart->RxCpltCallback(huart); 04257 #else 04258 /*Call legacy weak Rx complete callback*/ 04259 HAL_UART_RxCpltCallback(huart); 04260 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04261 } 04262 } 04263 } 04264 else 04265 { 04266 /* Clear RXNE interrupt flag */ 04267 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 04268 } 04269 } 04270 04271 /** 04272 * @brief RX interrupt handler for 9 bits data word length . 04273 * @note Function is called under interruption only, once 04274 * interruptions have been enabled by HAL_UART_Receive_IT() 04275 * @param huart UART handle. 04276 * @retval None 04277 */ 04278 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart) 04279 { 04280 uint16_t *tmp; 04281 uint16_t uhMask = huart->Mask; 04282 uint16_t uhdata; 04283 04284 /* Check that a Rx process is ongoing */ 04285 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 04286 { 04287 uhdata = (uint16_t) READ_REG(huart->Instance->RDR); 04288 tmp = (uint16_t *) huart->pRxBuffPtr ; 04289 *tmp = (uint16_t)(uhdata & uhMask); 04290 huart->pRxBuffPtr += 2U; 04291 huart->RxXferCount--; 04292 04293 if (huart->RxXferCount == 0U) 04294 { 04295 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/ 04296 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 04297 04298 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 04299 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 04300 04301 /* Rx process is completed, restore huart->RxState to Ready */ 04302 huart->RxState = HAL_UART_STATE_READY; 04303 04304 /* Clear RxISR function pointer */ 04305 huart->RxISR = NULL; 04306 04307 /* Check current reception Mode : 04308 If Reception till IDLE event has been selected : */ 04309 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 04310 { 04311 /* Set reception type to Standard */ 04312 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 04313 04314 /* Disable IDLE interrupt */ 04315 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 04316 04317 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET) 04318 { 04319 /* Clear IDLE Flag */ 04320 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF); 04321 } 04322 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04323 /*Call registered Rx Event callback*/ 04324 huart->RxEventCallback(huart, huart->RxXferSize); 04325 #else 04326 /*Call legacy weak Rx Event callback*/ 04327 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 04328 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 04329 } 04330 else 04331 { 04332 /* Standard reception API called */ 04333 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04334 /*Call registered Rx complete callback*/ 04335 huart->RxCpltCallback(huart); 04336 #else 04337 /*Call legacy weak Rx complete callback*/ 04338 HAL_UART_RxCpltCallback(huart); 04339 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04340 } 04341 } 04342 } 04343 else 04344 { 04345 /* Clear RXNE interrupt flag */ 04346 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 04347 } 04348 } 04349 04350 /** 04351 * @brief RX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled. 04352 * @note Function is called under interruption only, once 04353 * interruptions have been enabled by HAL_UART_Receive_IT() 04354 * @param huart UART handle. 04355 * @retval None 04356 */ 04357 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart) 04358 { 04359 uint16_t uhMask = huart->Mask; 04360 uint16_t uhdata; 04361 uint16_t nb_rx_data; 04362 uint16_t rxdatacount; 04363 uint32_t isrflags = READ_REG(huart->Instance->ISR); 04364 uint32_t cr1its = READ_REG(huart->Instance->CR1); 04365 uint32_t cr3its = READ_REG(huart->Instance->CR3); 04366 04367 /* Check that a Rx process is ongoing */ 04368 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 04369 { 04370 nb_rx_data = huart->NbRxDataToProcess; 04371 while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U)) 04372 { 04373 uhdata = (uint16_t) READ_REG(huart->Instance->RDR); 04374 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask); 04375 huart->pRxBuffPtr++; 04376 huart->RxXferCount--; 04377 isrflags = READ_REG(huart->Instance->ISR); 04378 04379 /* If some non blocking errors occurred */ 04380 if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U) 04381 { 04382 /* UART parity error interrupt occurred -------------------------------------*/ 04383 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 04384 { 04385 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); 04386 04387 huart->ErrorCode |= HAL_UART_ERROR_PE; 04388 } 04389 04390 /* UART frame error interrupt occurred --------------------------------------*/ 04391 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 04392 { 04393 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); 04394 04395 huart->ErrorCode |= HAL_UART_ERROR_FE; 04396 } 04397 04398 /* UART noise error interrupt occurred --------------------------------------*/ 04399 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 04400 { 04401 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); 04402 04403 huart->ErrorCode |= HAL_UART_ERROR_NE; 04404 } 04405 04406 /* Call UART Error Call back function if need be ----------------------------*/ 04407 if (huart->ErrorCode != HAL_UART_ERROR_NONE) 04408 { 04409 /* Non Blocking error : transfer could go on. 04410 Error is notified to user through user error callback */ 04411 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04412 /*Call registered error callback*/ 04413 huart->ErrorCallback(huart); 04414 #else 04415 /*Call legacy weak error callback*/ 04416 HAL_UART_ErrorCallback(huart); 04417 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04418 huart->ErrorCode = HAL_UART_ERROR_NONE; 04419 } 04420 } 04421 04422 if (huart->RxXferCount == 0U) 04423 { 04424 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/ 04425 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 04426 04427 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) 04428 and RX FIFO Threshold interrupt */ 04429 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 04430 04431 /* Rx process is completed, restore huart->RxState to Ready */ 04432 huart->RxState = HAL_UART_STATE_READY; 04433 04434 /* Clear RxISR function pointer */ 04435 huart->RxISR = NULL; 04436 04437 /* Check current reception Mode : 04438 If Reception till IDLE event has been selected : */ 04439 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 04440 { 04441 /* Set reception type to Standard */ 04442 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 04443 04444 /* Disable IDLE interrupt */ 04445 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 04446 04447 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET) 04448 { 04449 /* Clear IDLE Flag */ 04450 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF); 04451 } 04452 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04453 /*Call registered Rx Event callback*/ 04454 huart->RxEventCallback(huart, huart->RxXferSize); 04455 #else 04456 /*Call legacy weak Rx Event callback*/ 04457 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 04458 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 04459 } 04460 else 04461 { 04462 /* Standard reception API called */ 04463 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04464 /*Call registered Rx complete callback*/ 04465 huart->RxCpltCallback(huart); 04466 #else 04467 /*Call legacy weak Rx complete callback*/ 04468 HAL_UART_RxCpltCallback(huart); 04469 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04470 } 04471 } 04472 } 04473 04474 /* When remaining number of bytes to receive is less than the RX FIFO 04475 threshold, next incoming frames are processed as if FIFO mode was 04476 disabled (i.e. one interrupt per received frame). 04477 */ 04478 rxdatacount = huart->RxXferCount; 04479 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess)) 04480 { 04481 /* Disable the UART RXFT interrupt*/ 04482 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE); 04483 04484 /* Update the RxISR function pointer */ 04485 huart->RxISR = UART_RxISR_8BIT; 04486 04487 /* Enable the UART Data Register Not Empty interrupt */ 04488 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 04489 } 04490 } 04491 else 04492 { 04493 /* Clear RXNE interrupt flag */ 04494 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 04495 } 04496 } 04497 04498 /** 04499 * @brief RX interrupt handler for 9 bits data word length and FIFO mode is enabled. 04500 * @note Function is called under interruption only, once 04501 * interruptions have been enabled by HAL_UART_Receive_IT() 04502 * @param huart UART handle. 04503 * @retval None 04504 */ 04505 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart) 04506 { 04507 uint16_t *tmp; 04508 uint16_t uhMask = huart->Mask; 04509 uint16_t uhdata; 04510 uint16_t nb_rx_data; 04511 uint16_t rxdatacount; 04512 uint32_t isrflags = READ_REG(huart->Instance->ISR); 04513 uint32_t cr1its = READ_REG(huart->Instance->CR1); 04514 uint32_t cr3its = READ_REG(huart->Instance->CR3); 04515 04516 /* Check that a Rx process is ongoing */ 04517 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 04518 { 04519 nb_rx_data = huart->NbRxDataToProcess; 04520 while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U)) 04521 { 04522 uhdata = (uint16_t) READ_REG(huart->Instance->RDR); 04523 tmp = (uint16_t *) huart->pRxBuffPtr ; 04524 *tmp = (uint16_t)(uhdata & uhMask); 04525 huart->pRxBuffPtr += 2U; 04526 huart->RxXferCount--; 04527 isrflags = READ_REG(huart->Instance->ISR); 04528 04529 /* If some non blocking errors occurred */ 04530 if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U) 04531 { 04532 /* UART parity error interrupt occurred -------------------------------------*/ 04533 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 04534 { 04535 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF); 04536 04537 huart->ErrorCode |= HAL_UART_ERROR_PE; 04538 } 04539 04540 /* UART frame error interrupt occurred --------------------------------------*/ 04541 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 04542 { 04543 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF); 04544 04545 huart->ErrorCode |= HAL_UART_ERROR_FE; 04546 } 04547 04548 /* UART noise error interrupt occurred --------------------------------------*/ 04549 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 04550 { 04551 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF); 04552 04553 huart->ErrorCode |= HAL_UART_ERROR_NE; 04554 } 04555 04556 /* Call UART Error Call back function if need be ----------------------------*/ 04557 if (huart->ErrorCode != HAL_UART_ERROR_NONE) 04558 { 04559 /* Non Blocking error : transfer could go on. 04560 Error is notified to user through user error callback */ 04561 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04562 /*Call registered error callback*/ 04563 huart->ErrorCallback(huart); 04564 #else 04565 /*Call legacy weak error callback*/ 04566 HAL_UART_ErrorCallback(huart); 04567 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04568 huart->ErrorCode = HAL_UART_ERROR_NONE; 04569 } 04570 } 04571 04572 if (huart->RxXferCount == 0U) 04573 { 04574 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/ 04575 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 04576 04577 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) 04578 and RX FIFO Threshold interrupt */ 04579 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 04580 04581 /* Rx process is completed, restore huart->RxState to Ready */ 04582 huart->RxState = HAL_UART_STATE_READY; 04583 04584 /* Clear RxISR function pointer */ 04585 huart->RxISR = NULL; 04586 04587 /* Check current reception Mode : 04588 If Reception till IDLE event has been selected : */ 04589 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 04590 { 04591 /* Set reception type to Standard */ 04592 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 04593 04594 /* Disable IDLE interrupt */ 04595 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 04596 04597 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET) 04598 { 04599 /* Clear IDLE Flag */ 04600 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF); 04601 } 04602 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04603 /*Call registered Rx Event callback*/ 04604 huart->RxEventCallback(huart, huart->RxXferSize); 04605 #else 04606 /*Call legacy weak Rx Event callback*/ 04607 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 04608 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 04609 } 04610 else 04611 { 04612 /* Standard reception API called */ 04613 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 04614 /*Call registered Rx complete callback*/ 04615 huart->RxCpltCallback(huart); 04616 #else 04617 /*Call legacy weak Rx complete callback*/ 04618 HAL_UART_RxCpltCallback(huart); 04619 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 04620 } 04621 } 04622 } 04623 04624 /* When remaining number of bytes to receive is less than the RX FIFO 04625 threshold, next incoming frames are processed as if FIFO mode was 04626 disabled (i.e. one interrupt per received frame). 04627 */ 04628 rxdatacount = huart->RxXferCount; 04629 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess)) 04630 { 04631 /* Disable the UART RXFT interrupt*/ 04632 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE); 04633 04634 /* Update the RxISR function pointer */ 04635 huart->RxISR = UART_RxISR_16BIT; 04636 04637 /* Enable the UART Data Register Not Empty interrupt */ 04638 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 04639 } 04640 } 04641 else 04642 { 04643 /* Clear RXNE interrupt flag */ 04644 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST); 04645 } 04646 } 04647 04648 /** 04649 * @} 04650 */ 04651 04652 #endif /* HAL_UART_MODULE_ENABLED */ 04653 /** 04654 * @} 04655 */ 04656 04657 /** 04658 * @} 04659 */ 04660