STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_hal_usart.c 00004 * @author MCD Application Team 00005 * @brief USART HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter 00008 * Peripheral (USART). 00009 * + Initialization and de-initialization functions 00010 * + IO operation functions 00011 * + Peripheral Control functions 00012 * + Peripheral State and Error functions 00013 * 00014 ****************************************************************************** 00015 * @attention 00016 * 00017 * Copyright (c) 2017 STMicroelectronics. 00018 * All rights reserved. 00019 * 00020 * This software is licensed under terms that can be found in the LICENSE file 00021 * in the root directory of this software component. 00022 * If no LICENSE file comes with this software, it is provided AS-IS. 00023 * 00024 ****************************************************************************** 00025 @verbatim 00026 =============================================================================== 00027 ##### How to use this driver ##### 00028 =============================================================================== 00029 [..] 00030 The USART HAL driver can be used as follows: 00031 00032 (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart). 00033 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API: 00034 (++) Enable the USARTx interface clock. 00035 (++) USART pins configuration: 00036 (+++) Enable the clock for the USART GPIOs. 00037 (+++) Configure these USART pins as alternate function pull-up. 00038 (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(), 00039 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs): 00040 (+++) Configure the USARTx interrupt priority. 00041 (+++) Enable the NVIC USART IRQ handle. 00042 (++) USART interrupts handling: 00043 -@@- The specific USART interrupts (Transmission complete interrupt, 00044 RXNE interrupt and Error Interrupts) will be managed using the macros 00045 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process. 00046 (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA() 00047 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_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 USART DMA Tx/Rx handle. 00053 (+++) Configure the priority and enable the NVIC for the transfer 00054 complete interrupt on the DMA Tx/Rx channel. 00055 00056 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode 00057 (Receiver/Transmitter) in the husart handle Init structure. 00058 00059 (#) Initialize the USART registers by calling the HAL_USART_Init() API: 00060 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc) 00061 by calling the customized HAL_USART_MspInit(&husart) API. 00062 00063 [..] 00064 (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's 00065 HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and 00066 HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef. 00067 00068 ##### Callback registration ##### 00069 ================================== 00070 00071 [..] 00072 The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1 00073 allows the user to configure dynamically the driver callbacks. 00074 00075 [..] 00076 Use Function HAL_USART_RegisterCallback() to register a user callback. 00077 Function HAL_USART_RegisterCallback() allows to register following callbacks: 00078 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00079 (+) TxCpltCallback : Tx Complete Callback. 00080 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00081 (+) RxCpltCallback : Rx Complete Callback. 00082 (+) TxRxCpltCallback : Tx Rx Complete Callback. 00083 (+) ErrorCallback : Error Callback. 00084 (+) AbortCpltCallback : Abort Complete Callback. 00085 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00086 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00087 (+) MspInitCallback : USART MspInit. 00088 (+) MspDeInitCallback : USART MspDeInit. 00089 This function takes as parameters the HAL peripheral handle, the Callback ID 00090 and a pointer to the user callback function. 00091 00092 [..] 00093 Use function HAL_USART_UnRegisterCallback() to reset a callback to the default 00094 weak (surcharged) function. 00095 HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle, 00096 and the Callback ID. 00097 This function allows to reset following callbacks: 00098 (+) TxHalfCpltCallback : Tx Half Complete Callback. 00099 (+) TxCpltCallback : Tx Complete Callback. 00100 (+) RxHalfCpltCallback : Rx Half Complete Callback. 00101 (+) RxCpltCallback : Rx Complete Callback. 00102 (+) TxRxCpltCallback : Tx Rx Complete Callback. 00103 (+) ErrorCallback : Error Callback. 00104 (+) AbortCpltCallback : Abort Complete Callback. 00105 (+) RxFifoFullCallback : Rx Fifo Full Callback. 00106 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback. 00107 (+) MspInitCallback : USART MspInit. 00108 (+) MspDeInitCallback : USART MspDeInit. 00109 00110 [..] 00111 By default, after the HAL_USART_Init() and when the state is HAL_USART_STATE_RESET 00112 all callbacks are set to the corresponding weak (surcharged) functions: 00113 examples HAL_USART_TxCpltCallback(), HAL_USART_RxHalfCpltCallback(). 00114 Exception done for MspInit and MspDeInit functions that are respectively 00115 reset to the legacy weak (surcharged) functions in the HAL_USART_Init() 00116 and HAL_USART_DeInit() only when these callbacks are null (not registered beforehand). 00117 If not, MspInit or MspDeInit are not null, the HAL_USART_Init() and HAL_USART_DeInit() 00118 keep and use the user MspInit/MspDeInit callbacks (registered beforehand). 00119 00120 [..] 00121 Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only. 00122 Exception done MspInit/MspDeInit that can be registered/unregistered 00123 in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user) 00124 MspInit/DeInit callbacks can be used during the Init/DeInit. 00125 In that case first register the MspInit/MspDeInit user callbacks 00126 using HAL_USART_RegisterCallback() before calling HAL_USART_DeInit() 00127 or HAL_USART_Init() function. 00128 00129 [..] 00130 When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or 00131 not defined, the callback registration feature is not available 00132 and weak (surcharged) callbacks are used. 00133 00134 00135 @endverbatim 00136 ****************************************************************************** 00137 */ 00138 00139 /* Includes ------------------------------------------------------------------*/ 00140 #include "stm32h7xx_hal.h" 00141 00142 /** @addtogroup STM32H7xx_HAL_Driver 00143 * @{ 00144 */ 00145 00146 /** @defgroup USART USART 00147 * @brief HAL USART Synchronous module driver 00148 * @{ 00149 */ 00150 00151 #ifdef HAL_USART_MODULE_ENABLED 00152 00153 /* Private typedef -----------------------------------------------------------*/ 00154 /* Private define ------------------------------------------------------------*/ 00155 /** @defgroup USART_Private_Constants USART Private Constants 00156 * @{ 00157 */ 00158 #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */ 00159 #define USART_TEACK_REACK_TIMEOUT 1000U /*!< USART TX or RX enable acknowledge time-out value */ 00160 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \ 00161 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 | \ 00162 USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by USART_SetConfig API */ 00163 00164 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \ 00165 USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \ 00166 USART_CR2_DIS_NSS)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */ 00167 00168 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */ 00169 00170 #define USART_BRR_MIN 0x10U /* USART BRR minimum authorized value */ 00171 #define USART_BRR_MAX 0xFFFFU /* USART BRR maximum authorized value */ 00172 /** 00173 * @} 00174 */ 00175 00176 /* Private macros ------------------------------------------------------------*/ 00177 /* Private variables ---------------------------------------------------------*/ 00178 /* Private function prototypes -----------------------------------------------*/ 00179 /** @addtogroup USART_Private_Functions 00180 * @{ 00181 */ 00182 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 00183 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart); 00184 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 00185 static void USART_EndTransfer(USART_HandleTypeDef *husart); 00186 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00187 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00188 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00189 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00190 static void USART_DMAError(DMA_HandleTypeDef *hdma); 00191 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma); 00192 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); 00193 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma); 00194 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, 00195 uint32_t Tickstart, uint32_t Timeout); 00196 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart); 00197 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart); 00198 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart); 00199 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart); 00200 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart); 00201 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart); 00202 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart); 00203 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart); 00204 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart); 00205 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart); 00206 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart); 00207 00208 00209 /** 00210 * @} 00211 */ 00212 00213 /* Exported functions --------------------------------------------------------*/ 00214 00215 /** @defgroup USART_Exported_Functions USART Exported Functions 00216 * @{ 00217 */ 00218 00219 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions 00220 * @brief Initialization and Configuration functions 00221 * 00222 @verbatim 00223 =============================================================================== 00224 ##### Initialization and Configuration functions ##### 00225 =============================================================================== 00226 [..] 00227 This subsection provides a set of functions allowing to initialize the USART 00228 in asynchronous and in synchronous modes. 00229 (+) For the asynchronous mode only these parameters can be configured: 00230 (++) Baud Rate 00231 (++) Word Length 00232 (++) Stop Bit 00233 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00234 in the data register is transmitted but is changed by the parity bit. 00235 (++) USART polarity 00236 (++) USART phase 00237 (++) USART LastBit 00238 (++) Receiver/transmitter modes 00239 00240 [..] 00241 The HAL_USART_Init() function follows the USART synchronous configuration 00242 procedure (details for the procedure are available in reference manual). 00243 00244 @endverbatim 00245 00246 Depending on the frame length defined by the M1 and M0 bits (7-bit, 00247 8-bit or 9-bit), the possible USART formats are listed in the 00248 following table. 00249 00250 Table 1. USART frame format. 00251 +-----------------------------------------------------------------------+ 00252 | M1 bit | M0 bit | PCE bit | USART frame | 00253 |---------|---------|-----------|---------------------------------------| 00254 | 0 | 0 | 0 | | SB | 8 bit data | STB | | 00255 |---------|---------|-----------|---------------------------------------| 00256 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | | 00257 |---------|---------|-----------|---------------------------------------| 00258 | 0 | 1 | 0 | | SB | 9 bit data | STB | | 00259 |---------|---------|-----------|---------------------------------------| 00260 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | | 00261 |---------|---------|-----------|---------------------------------------| 00262 | 1 | 0 | 0 | | SB | 7 bit data | STB | | 00263 |---------|---------|-----------|---------------------------------------| 00264 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | | 00265 +-----------------------------------------------------------------------+ 00266 00267 * @{ 00268 */ 00269 00270 /** 00271 * @brief Initialize the USART mode according to the specified 00272 * parameters in the USART_InitTypeDef and initialize the associated handle. 00273 * @param husart USART handle. 00274 * @retval HAL status 00275 */ 00276 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart) 00277 { 00278 /* Check the USART handle allocation */ 00279 if (husart == NULL) 00280 { 00281 return HAL_ERROR; 00282 } 00283 00284 /* Check the parameters */ 00285 assert_param(IS_USART_INSTANCE(husart->Instance)); 00286 00287 if (husart->State == HAL_USART_STATE_RESET) 00288 { 00289 /* Allocate lock resource and initialize it */ 00290 husart->Lock = HAL_UNLOCKED; 00291 00292 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 00293 USART_InitCallbacksToDefault(husart); 00294 00295 if (husart->MspInitCallback == NULL) 00296 { 00297 husart->MspInitCallback = HAL_USART_MspInit; 00298 } 00299 00300 /* Init the low level hardware */ 00301 husart->MspInitCallback(husart); 00302 #else 00303 /* Init the low level hardware : GPIO, CLOCK */ 00304 HAL_USART_MspInit(husart); 00305 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 00306 } 00307 00308 husart->State = HAL_USART_STATE_BUSY; 00309 00310 /* Disable the Peripheral */ 00311 __HAL_USART_DISABLE(husart); 00312 00313 /* Set the Usart Communication parameters */ 00314 if (USART_SetConfig(husart) == HAL_ERROR) 00315 { 00316 return HAL_ERROR; 00317 } 00318 00319 /* In Synchronous mode, the following bits must be kept cleared: 00320 - LINEN bit in the USART_CR2 register 00321 - HDSEL, SCEN and IREN bits in the USART_CR3 register. 00322 */ 00323 husart->Instance->CR2 &= ~USART_CR2_LINEN; 00324 husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN); 00325 00326 /* Enable the Peripheral */ 00327 __HAL_USART_ENABLE(husart); 00328 00329 /* TEACK and/or REACK to check before moving husart->State to Ready */ 00330 return (USART_CheckIdleState(husart)); 00331 } 00332 00333 /** 00334 * @brief DeInitialize the USART peripheral. 00335 * @param husart USART handle. 00336 * @retval HAL status 00337 */ 00338 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart) 00339 { 00340 /* Check the USART handle allocation */ 00341 if (husart == NULL) 00342 { 00343 return HAL_ERROR; 00344 } 00345 00346 /* Check the parameters */ 00347 assert_param(IS_USART_INSTANCE(husart->Instance)); 00348 00349 husart->State = HAL_USART_STATE_BUSY; 00350 00351 husart->Instance->CR1 = 0x0U; 00352 husart->Instance->CR2 = 0x0U; 00353 husart->Instance->CR3 = 0x0U; 00354 00355 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 00356 if (husart->MspDeInitCallback == NULL) 00357 { 00358 husart->MspDeInitCallback = HAL_USART_MspDeInit; 00359 } 00360 /* DeInit the low level hardware */ 00361 husart->MspDeInitCallback(husart); 00362 #else 00363 /* DeInit the low level hardware */ 00364 HAL_USART_MspDeInit(husart); 00365 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 00366 00367 husart->ErrorCode = HAL_USART_ERROR_NONE; 00368 husart->State = HAL_USART_STATE_RESET; 00369 00370 /* Process Unlock */ 00371 __HAL_UNLOCK(husart); 00372 00373 return HAL_OK; 00374 } 00375 00376 /** 00377 * @brief Initialize the USART MSP. 00378 * @param husart USART handle. 00379 * @retval None 00380 */ 00381 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart) 00382 { 00383 /* Prevent unused argument(s) compilation warning */ 00384 UNUSED(husart); 00385 00386 /* NOTE : This function should not be modified, when the callback is needed, 00387 the HAL_USART_MspInit can be implemented in the user file 00388 */ 00389 } 00390 00391 /** 00392 * @brief DeInitialize the USART MSP. 00393 * @param husart USART handle. 00394 * @retval None 00395 */ 00396 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart) 00397 { 00398 /* Prevent unused argument(s) compilation warning */ 00399 UNUSED(husart); 00400 00401 /* NOTE : This function should not be modified, when the callback is needed, 00402 the HAL_USART_MspDeInit can be implemented in the user file 00403 */ 00404 } 00405 00406 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 00407 /** 00408 * @brief Register a User USART Callback 00409 * To be used instead of the weak predefined callback 00410 * @param husart usart handle 00411 * @param CallbackID ID of the callback to be registered 00412 * This parameter can be one of the following values: 00413 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00414 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00415 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00416 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00417 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID 00418 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID 00419 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00420 * @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00421 * @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00422 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID 00423 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID 00424 * @param pCallback pointer to the Callback function 00425 * @retval HAL status 00426 + */ 00427 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, 00428 pUSART_CallbackTypeDef pCallback) 00429 { 00430 HAL_StatusTypeDef status = HAL_OK; 00431 00432 if (pCallback == NULL) 00433 { 00434 /* Update the error code */ 00435 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00436 00437 return HAL_ERROR; 00438 } 00439 /* Process locked */ 00440 __HAL_LOCK(husart); 00441 00442 if (husart->State == HAL_USART_STATE_READY) 00443 { 00444 switch (CallbackID) 00445 { 00446 case HAL_USART_TX_HALFCOMPLETE_CB_ID : 00447 husart->TxHalfCpltCallback = pCallback; 00448 break; 00449 00450 case HAL_USART_TX_COMPLETE_CB_ID : 00451 husart->TxCpltCallback = pCallback; 00452 break; 00453 00454 case HAL_USART_RX_HALFCOMPLETE_CB_ID : 00455 husart->RxHalfCpltCallback = pCallback; 00456 break; 00457 00458 case HAL_USART_RX_COMPLETE_CB_ID : 00459 husart->RxCpltCallback = pCallback; 00460 break; 00461 00462 case HAL_USART_TX_RX_COMPLETE_CB_ID : 00463 husart->TxRxCpltCallback = pCallback; 00464 break; 00465 00466 case HAL_USART_ERROR_CB_ID : 00467 husart->ErrorCallback = pCallback; 00468 break; 00469 00470 case HAL_USART_ABORT_COMPLETE_CB_ID : 00471 husart->AbortCpltCallback = pCallback; 00472 break; 00473 00474 case HAL_USART_RX_FIFO_FULL_CB_ID : 00475 husart->RxFifoFullCallback = pCallback; 00476 break; 00477 00478 case HAL_USART_TX_FIFO_EMPTY_CB_ID : 00479 husart->TxFifoEmptyCallback = pCallback; 00480 break; 00481 00482 case HAL_USART_MSPINIT_CB_ID : 00483 husart->MspInitCallback = pCallback; 00484 break; 00485 00486 case HAL_USART_MSPDEINIT_CB_ID : 00487 husart->MspDeInitCallback = pCallback; 00488 break; 00489 00490 default : 00491 /* Update the error code */ 00492 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00493 00494 /* Return error status */ 00495 status = HAL_ERROR; 00496 break; 00497 } 00498 } 00499 else if (husart->State == HAL_USART_STATE_RESET) 00500 { 00501 switch (CallbackID) 00502 { 00503 case HAL_USART_MSPINIT_CB_ID : 00504 husart->MspInitCallback = pCallback; 00505 break; 00506 00507 case HAL_USART_MSPDEINIT_CB_ID : 00508 husart->MspDeInitCallback = pCallback; 00509 break; 00510 00511 default : 00512 /* Update the error code */ 00513 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00514 00515 /* Return error status */ 00516 status = HAL_ERROR; 00517 break; 00518 } 00519 } 00520 else 00521 { 00522 /* Update the error code */ 00523 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00524 00525 /* Return error status */ 00526 status = HAL_ERROR; 00527 } 00528 00529 /* Release Lock */ 00530 __HAL_UNLOCK(husart); 00531 00532 return status; 00533 } 00534 00535 /** 00536 * @brief Unregister an USART Callback 00537 * USART callaback is redirected to the weak predefined callback 00538 * @param husart usart handle 00539 * @param CallbackID ID of the callback to be unregistered 00540 * This parameter can be one of the following values: 00541 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00542 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00543 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00544 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00545 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID 00546 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID 00547 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00548 * @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID 00549 * @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID 00550 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID 00551 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID 00552 * @retval HAL status 00553 */ 00554 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID) 00555 { 00556 HAL_StatusTypeDef status = HAL_OK; 00557 00558 /* Process locked */ 00559 __HAL_LOCK(husart); 00560 00561 if (HAL_USART_STATE_READY == husart->State) 00562 { 00563 switch (CallbackID) 00564 { 00565 case HAL_USART_TX_HALFCOMPLETE_CB_ID : 00566 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 00567 break; 00568 00569 case HAL_USART_TX_COMPLETE_CB_ID : 00570 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00571 break; 00572 00573 case HAL_USART_RX_HALFCOMPLETE_CB_ID : 00574 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 00575 break; 00576 00577 case HAL_USART_RX_COMPLETE_CB_ID : 00578 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00579 break; 00580 00581 case HAL_USART_TX_RX_COMPLETE_CB_ID : 00582 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ 00583 break; 00584 00585 case HAL_USART_ERROR_CB_ID : 00586 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */ 00587 break; 00588 00589 case HAL_USART_ABORT_COMPLETE_CB_ID : 00590 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00591 break; 00592 00593 case HAL_USART_RX_FIFO_FULL_CB_ID : 00594 husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */ 00595 break; 00596 00597 case HAL_USART_TX_FIFO_EMPTY_CB_ID : 00598 husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */ 00599 break; 00600 00601 case HAL_USART_MSPINIT_CB_ID : 00602 husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */ 00603 break; 00604 00605 case HAL_USART_MSPDEINIT_CB_ID : 00606 husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */ 00607 break; 00608 00609 default : 00610 /* Update the error code */ 00611 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00612 00613 /* Return error status */ 00614 status = HAL_ERROR; 00615 break; 00616 } 00617 } 00618 else if (HAL_USART_STATE_RESET == husart->State) 00619 { 00620 switch (CallbackID) 00621 { 00622 case HAL_USART_MSPINIT_CB_ID : 00623 husart->MspInitCallback = HAL_USART_MspInit; 00624 break; 00625 00626 case HAL_USART_MSPDEINIT_CB_ID : 00627 husart->MspDeInitCallback = HAL_USART_MspDeInit; 00628 break; 00629 00630 default : 00631 /* Update the error code */ 00632 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00633 00634 /* Return error status */ 00635 status = HAL_ERROR; 00636 break; 00637 } 00638 } 00639 else 00640 { 00641 /* Update the error code */ 00642 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK; 00643 00644 /* Return error status */ 00645 status = HAL_ERROR; 00646 } 00647 00648 /* Release Lock */ 00649 __HAL_UNLOCK(husart); 00650 00651 return status; 00652 } 00653 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 00654 00655 00656 /** 00657 * @} 00658 */ 00659 00660 /** @defgroup USART_Exported_Functions_Group2 IO operation functions 00661 * @brief USART Transmit and Receive functions 00662 * 00663 @verbatim 00664 =============================================================================== 00665 ##### IO operation functions ##### 00666 =============================================================================== 00667 [..] This subsection provides a set of functions allowing to manage the USART synchronous 00668 data transfers. 00669 00670 [..] The USART supports master mode only: it cannot receive or send data related to an input 00671 clock (SCLK is always an output). 00672 00673 [..] 00674 00675 (#) There are two modes of transfer: 00676 (++) Blocking mode: The communication is performed in polling mode. 00677 The HAL status of all data processing is returned by the same function 00678 after finishing transfer. 00679 (++) No-Blocking mode: The communication is performed using Interrupts 00680 or DMA, These API's return the HAL status. 00681 The end of the data processing will be indicated through the 00682 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when 00683 using DMA mode. 00684 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks 00685 will be executed respectively at the end of the transmit or Receive process 00686 The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected 00687 00688 (#) Blocking mode API's are : 00689 (++) HAL_USART_Transmit() in simplex mode 00690 (++) HAL_USART_Receive() in full duplex receive only 00691 (++) HAL_USART_TransmitReceive() in full duplex mode 00692 00693 (#) Non-Blocking mode API's with Interrupt are : 00694 (++) HAL_USART_Transmit_IT() in simplex mode 00695 (++) HAL_USART_Receive_IT() in full duplex receive only 00696 (++) HAL_USART_TransmitReceive_IT() in full duplex mode 00697 (++) HAL_USART_IRQHandler() 00698 00699 (#) No-Blocking mode API's with DMA are : 00700 (++) HAL_USART_Transmit_DMA() in simplex mode 00701 (++) HAL_USART_Receive_DMA() in full duplex receive only 00702 (++) HAL_USART_TransmitReceive_DMA() in full duplex mode 00703 (++) HAL_USART_DMAPause() 00704 (++) HAL_USART_DMAResume() 00705 (++) HAL_USART_DMAStop() 00706 00707 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode: 00708 (++) HAL_USART_TxCpltCallback() 00709 (++) HAL_USART_RxCpltCallback() 00710 (++) HAL_USART_TxHalfCpltCallback() 00711 (++) HAL_USART_RxHalfCpltCallback() 00712 (++) HAL_USART_ErrorCallback() 00713 (++) HAL_USART_TxRxCpltCallback() 00714 00715 (#) Non-Blocking mode transfers could be aborted using Abort API's : 00716 (++) HAL_USART_Abort() 00717 (++) HAL_USART_Abort_IT() 00718 00719 (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided: 00720 (++) HAL_USART_AbortCpltCallback() 00721 00722 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. 00723 Errors are handled as follows : 00724 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is 00725 to be evaluated by user : this concerns Frame Error, 00726 Parity Error or Noise Error in Interrupt mode reception . 00727 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify 00728 error type, and HAL_USART_ErrorCallback() user callback is executed. 00729 Transfer is kept ongoing on USART side. 00730 If user wants to abort it, Abort services should be called by user. 00731 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted. 00732 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode. 00733 Error code is set to allow user to identify error type, 00734 and HAL_USART_ErrorCallback() user callback is executed. 00735 00736 @endverbatim 00737 * @{ 00738 */ 00739 00740 /** 00741 * @brief Simplex send an amount of data in blocking mode. 00742 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 00743 * the sent data is handled as a set of u16. In this case, Size must indicate the number 00744 * of u16 provided through pTxData. 00745 * @param husart USART handle. 00746 * @param pTxData Pointer to data buffer (u8 or u16 data elements). 00747 * @param Size Amount of data elements (u8 or u16) to be sent. 00748 * @param Timeout Timeout duration. 00749 * @retval HAL status 00750 */ 00751 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size, uint32_t Timeout) 00752 { 00753 const uint8_t *ptxdata8bits; 00754 const uint16_t *ptxdata16bits; 00755 uint32_t tickstart; 00756 00757 if (husart->State == HAL_USART_STATE_READY) 00758 { 00759 if ((pTxData == NULL) || (Size == 0U)) 00760 { 00761 return HAL_ERROR; 00762 } 00763 00764 /* Process Locked */ 00765 __HAL_LOCK(husart); 00766 00767 husart->ErrorCode = HAL_USART_ERROR_NONE; 00768 husart->State = HAL_USART_STATE_BUSY_TX; 00769 00770 /* Init tickstart for timeout management */ 00771 tickstart = HAL_GetTick(); 00772 00773 husart->TxXferSize = Size; 00774 husart->TxXferCount = Size; 00775 00776 /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */ 00777 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 00778 { 00779 ptxdata8bits = NULL; 00780 ptxdata16bits = (const uint16_t *) pTxData; 00781 } 00782 else 00783 { 00784 ptxdata8bits = pTxData; 00785 ptxdata16bits = NULL; 00786 } 00787 00788 /* Check the remaining data to be sent */ 00789 while (husart->TxXferCount > 0U) 00790 { 00791 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 00792 { 00793 return HAL_TIMEOUT; 00794 } 00795 if (ptxdata8bits == NULL) 00796 { 00797 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & 0x01FFU); 00798 ptxdata16bits++; 00799 } 00800 else 00801 { 00802 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & 0xFFU); 00803 ptxdata8bits++; 00804 } 00805 00806 husart->TxXferCount--; 00807 } 00808 00809 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) 00810 { 00811 return HAL_TIMEOUT; 00812 } 00813 00814 /* Clear Transmission Complete Flag */ 00815 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF); 00816 00817 /* Clear overrun flag and discard the received data */ 00818 __HAL_USART_CLEAR_OREFLAG(husart); 00819 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 00820 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 00821 00822 /* At end of Tx process, restore husart->State to Ready */ 00823 husart->State = HAL_USART_STATE_READY; 00824 00825 /* Process Unlocked */ 00826 __HAL_UNLOCK(husart); 00827 00828 return HAL_OK; 00829 } 00830 else 00831 { 00832 return HAL_BUSY; 00833 } 00834 } 00835 00836 /** 00837 * @brief Receive an amount of data in blocking mode. 00838 * @note To receive synchronous data, dummy data are simultaneously transmitted. 00839 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 00840 * the received data is handled as a set of u16. In this case, Size must indicate the number 00841 * of u16 available through pRxData. 00842 * @param husart USART handle. 00843 * @param pRxData Pointer to data buffer (u8 or u16 data elements). 00844 * @param Size Amount of data elements (u8 or u16) to be received. 00845 * @param Timeout Timeout duration. 00846 * @retval HAL status 00847 */ 00848 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout) 00849 { 00850 uint8_t *prxdata8bits; 00851 uint16_t *prxdata16bits; 00852 uint16_t uhMask; 00853 uint32_t tickstart; 00854 00855 if (husart->State == HAL_USART_STATE_READY) 00856 { 00857 if ((pRxData == NULL) || (Size == 0U)) 00858 { 00859 return HAL_ERROR; 00860 } 00861 00862 /* Process Locked */ 00863 __HAL_LOCK(husart); 00864 00865 husart->ErrorCode = HAL_USART_ERROR_NONE; 00866 husart->State = HAL_USART_STATE_BUSY_RX; 00867 00868 /* Init tickstart for timeout management */ 00869 tickstart = HAL_GetTick(); 00870 00871 husart->RxXferSize = Size; 00872 husart->RxXferCount = Size; 00873 00874 /* Computation of USART mask to apply to RDR register */ 00875 USART_MASK_COMPUTATION(husart); 00876 uhMask = husart->Mask; 00877 00878 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */ 00879 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 00880 { 00881 prxdata8bits = NULL; 00882 prxdata16bits = (uint16_t *) pRxData; 00883 } 00884 else 00885 { 00886 prxdata8bits = pRxData; 00887 prxdata16bits = NULL; 00888 } 00889 00890 /* as long as data have to be received */ 00891 while (husart->RxXferCount > 0U) 00892 { 00893 if (husart->SlaveMode == USART_SLAVEMODE_DISABLE) 00894 { 00895 /* Wait until TXE flag is set to send dummy byte in order to generate the 00896 * clock for the slave to send data. 00897 * Whatever the frame length (7, 8 or 9-bit long), the same dummy value 00898 * can be written for all the cases. */ 00899 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 00900 { 00901 return HAL_TIMEOUT; 00902 } 00903 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF); 00904 } 00905 00906 /* Wait for RXNE Flag */ 00907 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 00908 { 00909 return HAL_TIMEOUT; 00910 } 00911 00912 if (prxdata8bits == NULL) 00913 { 00914 *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask); 00915 prxdata16bits++; 00916 } 00917 else 00918 { 00919 *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU)); 00920 prxdata8bits++; 00921 } 00922 00923 husart->RxXferCount--; 00924 00925 } 00926 00927 /* Clear SPI slave underrun flag and discard transmit data */ 00928 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE) 00929 { 00930 __HAL_USART_CLEAR_UDRFLAG(husart); 00931 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 00932 } 00933 00934 /* At end of Rx process, restore husart->State to Ready */ 00935 husart->State = HAL_USART_STATE_READY; 00936 00937 /* Process Unlocked */ 00938 __HAL_UNLOCK(husart); 00939 00940 return HAL_OK; 00941 } 00942 else 00943 { 00944 return HAL_BUSY; 00945 } 00946 } 00947 00948 /** 00949 * @brief Full-Duplex Send and Receive an amount of data in blocking mode. 00950 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 00951 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number 00952 * of u16 available through pTxData and through pRxData. 00953 * @param husart USART handle. 00954 * @param pTxData pointer to TX data buffer (u8 or u16 data elements). 00955 * @param pRxData pointer to RX data buffer (u8 or u16 data elements). 00956 * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received). 00957 * @param Timeout Timeout duration. 00958 * @retval HAL status 00959 */ 00960 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 00961 uint16_t Size, uint32_t Timeout) 00962 { 00963 uint8_t *prxdata8bits; 00964 uint16_t *prxdata16bits; 00965 const uint8_t *ptxdata8bits; 00966 const uint16_t *ptxdata16bits; 00967 uint16_t uhMask; 00968 uint16_t rxdatacount; 00969 uint32_t tickstart; 00970 00971 if (husart->State == HAL_USART_STATE_READY) 00972 { 00973 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) 00974 { 00975 return HAL_ERROR; 00976 } 00977 00978 /* Process Locked */ 00979 __HAL_LOCK(husart); 00980 00981 husart->ErrorCode = HAL_USART_ERROR_NONE; 00982 husart->State = HAL_USART_STATE_BUSY_RX; 00983 00984 /* Init tickstart for timeout management */ 00985 tickstart = HAL_GetTick(); 00986 00987 husart->RxXferSize = Size; 00988 husart->TxXferSize = Size; 00989 husart->TxXferCount = Size; 00990 husart->RxXferCount = Size; 00991 00992 /* Computation of USART mask to apply to RDR register */ 00993 USART_MASK_COMPUTATION(husart); 00994 uhMask = husart->Mask; 00995 00996 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */ 00997 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 00998 { 00999 prxdata8bits = NULL; 01000 ptxdata8bits = NULL; 01001 ptxdata16bits = (const uint16_t *) pTxData; 01002 prxdata16bits = (uint16_t *) pRxData; 01003 } 01004 else 01005 { 01006 prxdata8bits = pRxData; 01007 ptxdata8bits = pTxData; 01008 ptxdata16bits = NULL; 01009 prxdata16bits = NULL; 01010 } 01011 01012 if ((husart->TxXferCount == 0x01U) || (husart->SlaveMode == USART_SLAVEMODE_ENABLE)) 01013 { 01014 /* Wait until TXE flag is set to send data */ 01015 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 01016 { 01017 return HAL_TIMEOUT; 01018 } 01019 if (ptxdata8bits == NULL) 01020 { 01021 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask); 01022 ptxdata16bits++; 01023 } 01024 else 01025 { 01026 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU)); 01027 ptxdata8bits++; 01028 } 01029 01030 husart->TxXferCount--; 01031 } 01032 01033 /* Check the remain data to be sent */ 01034 /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */ 01035 rxdatacount = husart->RxXferCount; 01036 while ((husart->TxXferCount > 0U) || (rxdatacount > 0U)) 01037 { 01038 if (husart->TxXferCount > 0U) 01039 { 01040 /* Wait until TXE flag is set to send data */ 01041 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 01042 { 01043 return HAL_TIMEOUT; 01044 } 01045 if (ptxdata8bits == NULL) 01046 { 01047 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask); 01048 ptxdata16bits++; 01049 } 01050 else 01051 { 01052 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU)); 01053 ptxdata8bits++; 01054 } 01055 01056 husart->TxXferCount--; 01057 } 01058 01059 if (husart->RxXferCount > 0U) 01060 { 01061 /* Wait for RXNE Flag */ 01062 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 01063 { 01064 return HAL_TIMEOUT; 01065 } 01066 01067 if (prxdata8bits == NULL) 01068 { 01069 *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask); 01070 prxdata16bits++; 01071 } 01072 else 01073 { 01074 *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU)); 01075 prxdata8bits++; 01076 } 01077 01078 husart->RxXferCount--; 01079 } 01080 rxdatacount = husart->RxXferCount; 01081 } 01082 01083 /* At end of TxRx process, restore husart->State to Ready */ 01084 husart->State = HAL_USART_STATE_READY; 01085 01086 /* Process Unlocked */ 01087 __HAL_UNLOCK(husart); 01088 01089 return HAL_OK; 01090 } 01091 else 01092 { 01093 return HAL_BUSY; 01094 } 01095 } 01096 01097 /** 01098 * @brief Send an amount of data in interrupt mode. 01099 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01100 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01101 * of u16 provided through pTxData. 01102 * @param husart USART handle. 01103 * @param pTxData pointer to data buffer (u8 or u16 data elements). 01104 * @param Size amount of data elements (u8 or u16) to be sent. 01105 * @retval HAL status 01106 */ 01107 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size) 01108 { 01109 if (husart->State == HAL_USART_STATE_READY) 01110 { 01111 if ((pTxData == NULL) || (Size == 0U)) 01112 { 01113 return HAL_ERROR; 01114 } 01115 01116 /* Process Locked */ 01117 __HAL_LOCK(husart); 01118 01119 husart->pTxBuffPtr = pTxData; 01120 husart->TxXferSize = Size; 01121 husart->TxXferCount = Size; 01122 husart->TxISR = NULL; 01123 01124 husart->ErrorCode = HAL_USART_ERROR_NONE; 01125 husart->State = HAL_USART_STATE_BUSY_TX; 01126 01127 /* The USART Error Interrupts: (Frame error, noise error, overrun error) 01128 are not managed by the USART Transmit Process to avoid the overrun interrupt 01129 when the usart mode is configured for transmit and receive "USART_MODE_TX_RX" 01130 to benefit for the frame error and noise interrupts the usart mode should be 01131 configured only for transmit "USART_MODE_TX" */ 01132 01133 /* Configure Tx interrupt processing */ 01134 if (husart->FifoMode == USART_FIFOMODE_ENABLE) 01135 { 01136 /* Set the Tx ISR function pointer according to the data word length */ 01137 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01138 { 01139 husart->TxISR = USART_TxISR_16BIT_FIFOEN; 01140 } 01141 else 01142 { 01143 husart->TxISR = USART_TxISR_8BIT_FIFOEN; 01144 } 01145 01146 /* Process Unlocked */ 01147 __HAL_UNLOCK(husart); 01148 01149 /* Enable the TX FIFO threshold interrupt */ 01150 __HAL_USART_ENABLE_IT(husart, USART_IT_TXFT); 01151 } 01152 else 01153 { 01154 /* Set the Tx ISR function pointer according to the data word length */ 01155 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01156 { 01157 husart->TxISR = USART_TxISR_16BIT; 01158 } 01159 else 01160 { 01161 husart->TxISR = USART_TxISR_8BIT; 01162 } 01163 01164 /* Process Unlocked */ 01165 __HAL_UNLOCK(husart); 01166 01167 /* Enable the USART Transmit Data Register Empty Interrupt */ 01168 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE); 01169 } 01170 01171 return HAL_OK; 01172 } 01173 else 01174 { 01175 return HAL_BUSY; 01176 } 01177 } 01178 01179 /** 01180 * @brief Receive an amount of data in interrupt mode. 01181 * @note To receive synchronous data, dummy data are simultaneously transmitted. 01182 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01183 * the received data is handled as a set of u16. In this case, Size must indicate the number 01184 * of u16 available through pRxData. 01185 * @param husart USART handle. 01186 * @param pRxData pointer to data buffer (u8 or u16 data elements). 01187 * @param Size amount of data elements (u8 or u16) to be received. 01188 * @retval HAL status 01189 */ 01190 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) 01191 { 01192 uint16_t nb_dummy_data; 01193 01194 if (husart->State == HAL_USART_STATE_READY) 01195 { 01196 if ((pRxData == NULL) || (Size == 0U)) 01197 { 01198 return HAL_ERROR; 01199 } 01200 01201 /* Process Locked */ 01202 __HAL_LOCK(husart); 01203 01204 husart->pRxBuffPtr = pRxData; 01205 husart->RxXferSize = Size; 01206 husart->RxXferCount = Size; 01207 husart->RxISR = NULL; 01208 01209 USART_MASK_COMPUTATION(husart); 01210 01211 husart->ErrorCode = HAL_USART_ERROR_NONE; 01212 husart->State = HAL_USART_STATE_BUSY_RX; 01213 01214 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 01215 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01216 01217 /* Configure Rx interrupt processing */ 01218 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess)) 01219 { 01220 /* Set the Rx ISR function pointer according to the data word length */ 01221 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01222 { 01223 husart->RxISR = USART_RxISR_16BIT_FIFOEN; 01224 } 01225 else 01226 { 01227 husart->RxISR = USART_RxISR_8BIT_FIFOEN; 01228 } 01229 01230 /* Process Unlocked */ 01231 __HAL_UNLOCK(husart); 01232 01233 /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */ 01234 if (husart->Init.Parity != USART_PARITY_NONE) 01235 { 01236 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01237 } 01238 SET_BIT(husart->Instance->CR3, USART_CR3_RXFTIE); 01239 } 01240 else 01241 { 01242 /* Set the Rx ISR function pointer according to the data word length */ 01243 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01244 { 01245 husart->RxISR = USART_RxISR_16BIT; 01246 } 01247 else 01248 { 01249 husart->RxISR = USART_RxISR_8BIT; 01250 } 01251 01252 /* Process Unlocked */ 01253 __HAL_UNLOCK(husart); 01254 01255 /* Enable the USART Parity Error and Data Register not empty Interrupts */ 01256 if (husart->Init.Parity != USART_PARITY_NONE) 01257 { 01258 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE); 01259 } 01260 else 01261 { 01262 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 01263 } 01264 } 01265 01266 if (husart->SlaveMode == USART_SLAVEMODE_DISABLE) 01267 { 01268 /* Send dummy data in order to generate the clock for the Slave to send the next data. 01269 When FIFO mode is disabled only one data must be transferred. 01270 When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold. 01271 */ 01272 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess)) 01273 { 01274 for (nb_dummy_data = husart->NbRxDataToProcess ; nb_dummy_data > 0U ; nb_dummy_data--) 01275 { 01276 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 01277 } 01278 } 01279 else 01280 { 01281 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 01282 } 01283 } 01284 01285 return HAL_OK; 01286 } 01287 else 01288 { 01289 return HAL_BUSY; 01290 } 01291 } 01292 01293 /** 01294 * @brief Full-Duplex Send and Receive an amount of data in interrupt mode. 01295 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01296 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number 01297 * of u16 available through pTxData and through pRxData. 01298 * @param husart USART handle. 01299 * @param pTxData pointer to TX data buffer (u8 or u16 data elements). 01300 * @param pRxData pointer to RX data buffer (u8 or u16 data elements). 01301 * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received). 01302 * @retval HAL status 01303 */ 01304 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 01305 uint16_t Size) 01306 { 01307 01308 if (husart->State == HAL_USART_STATE_READY) 01309 { 01310 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) 01311 { 01312 return HAL_ERROR; 01313 } 01314 01315 /* Process Locked */ 01316 __HAL_LOCK(husart); 01317 01318 husart->pRxBuffPtr = pRxData; 01319 husart->RxXferSize = Size; 01320 husart->RxXferCount = Size; 01321 husart->pTxBuffPtr = pTxData; 01322 husart->TxXferSize = Size; 01323 husart->TxXferCount = Size; 01324 01325 /* Computation of USART mask to apply to RDR register */ 01326 USART_MASK_COMPUTATION(husart); 01327 01328 husart->ErrorCode = HAL_USART_ERROR_NONE; 01329 husart->State = HAL_USART_STATE_BUSY_TX_RX; 01330 01331 /* Configure TxRx interrupt processing */ 01332 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess)) 01333 { 01334 /* Set the Rx ISR function pointer according to the data word length */ 01335 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01336 { 01337 husart->TxISR = USART_TxISR_16BIT_FIFOEN; 01338 husart->RxISR = USART_RxISR_16BIT_FIFOEN; 01339 } 01340 else 01341 { 01342 husart->TxISR = USART_TxISR_8BIT_FIFOEN; 01343 husart->RxISR = USART_RxISR_8BIT_FIFOEN; 01344 } 01345 01346 /* Process Locked */ 01347 __HAL_UNLOCK(husart); 01348 01349 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 01350 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01351 01352 if (husart->Init.Parity != USART_PARITY_NONE) 01353 { 01354 /* Enable the USART Parity Error interrupt */ 01355 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01356 } 01357 01358 /* Enable the TX and RX FIFO Threshold interrupts */ 01359 SET_BIT(husart->Instance->CR3, (USART_CR3_TXFTIE | USART_CR3_RXFTIE)); 01360 } 01361 else 01362 { 01363 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE)) 01364 { 01365 husart->TxISR = USART_TxISR_16BIT; 01366 husart->RxISR = USART_RxISR_16BIT; 01367 } 01368 else 01369 { 01370 husart->TxISR = USART_TxISR_8BIT; 01371 husart->RxISR = USART_RxISR_8BIT; 01372 } 01373 01374 /* Process Locked */ 01375 __HAL_UNLOCK(husart); 01376 01377 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 01378 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01379 01380 /* Enable the USART Parity Error and USART Data Register not empty Interrupts */ 01381 if (husart->Init.Parity != USART_PARITY_NONE) 01382 { 01383 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE); 01384 } 01385 else 01386 { 01387 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 01388 } 01389 01390 /* Enable the USART Transmit Data Register Empty Interrupt */ 01391 SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE); 01392 } 01393 01394 return HAL_OK; 01395 } 01396 else 01397 { 01398 return HAL_BUSY; 01399 } 01400 } 01401 01402 /** 01403 * @brief Send an amount of data in DMA mode. 01404 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01405 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01406 * of u16 provided through pTxData. 01407 * @param husart USART handle. 01408 * @param pTxData pointer to data buffer (u8 or u16 data elements). 01409 * @param Size amount of data elements (u8 or u16) to be sent. 01410 * @retval HAL status 01411 */ 01412 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size) 01413 { 01414 HAL_StatusTypeDef status = HAL_OK; 01415 const uint32_t *tmp; 01416 01417 if (husart->State == HAL_USART_STATE_READY) 01418 { 01419 if ((pTxData == NULL) || (Size == 0U)) 01420 { 01421 return HAL_ERROR; 01422 } 01423 01424 /* Process Locked */ 01425 __HAL_LOCK(husart); 01426 01427 husart->pTxBuffPtr = pTxData; 01428 husart->TxXferSize = Size; 01429 husart->TxXferCount = Size; 01430 01431 husart->ErrorCode = HAL_USART_ERROR_NONE; 01432 husart->State = HAL_USART_STATE_BUSY_TX; 01433 01434 if (husart->hdmatx != NULL) 01435 { 01436 /* Set the USART DMA transfer complete callback */ 01437 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; 01438 01439 /* Set the USART DMA Half transfer complete callback */ 01440 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; 01441 01442 /* Set the DMA error callback */ 01443 husart->hdmatx->XferErrorCallback = USART_DMAError; 01444 01445 /* Enable the USART transmit DMA channel */ 01446 tmp = (const uint32_t *)&pTxData; 01447 status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size); 01448 } 01449 01450 if (status == HAL_OK) 01451 { 01452 /* Clear the TC flag in the ICR register */ 01453 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF); 01454 01455 /* Process Unlocked */ 01456 __HAL_UNLOCK(husart); 01457 01458 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01459 in the USART CR3 register */ 01460 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01461 01462 return HAL_OK; 01463 } 01464 else 01465 { 01466 /* Set error code to DMA */ 01467 husart->ErrorCode = HAL_USART_ERROR_DMA; 01468 01469 /* Process Unlocked */ 01470 __HAL_UNLOCK(husart); 01471 01472 /* Restore husart->State to ready */ 01473 husart->State = HAL_USART_STATE_READY; 01474 01475 return HAL_ERROR; 01476 } 01477 } 01478 else 01479 { 01480 return HAL_BUSY; 01481 } 01482 } 01483 01484 /** 01485 * @brief Receive an amount of data in DMA mode. 01486 * @note When the USART parity is enabled (PCE = 1), the received data contain 01487 * the parity bit (MSB position). 01488 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave. 01489 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01490 * the received data is handled as a set of u16. In this case, Size must indicate the number 01491 * of u16 available through pRxData. 01492 * @param husart USART handle. 01493 * @param pRxData pointer to data buffer (u8 or u16 data elements). 01494 * @param Size amount of data elements (u8 or u16) to be received. 01495 * @retval HAL status 01496 */ 01497 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size) 01498 { 01499 HAL_StatusTypeDef status = HAL_OK; 01500 uint32_t *tmp = (uint32_t *)&pRxData; 01501 01502 /* Check that a Rx process is not already ongoing */ 01503 if (husart->State == HAL_USART_STATE_READY) 01504 { 01505 if ((pRxData == NULL) || (Size == 0U)) 01506 { 01507 return HAL_ERROR; 01508 } 01509 01510 /* Process Locked */ 01511 __HAL_LOCK(husart); 01512 01513 husart->pRxBuffPtr = pRxData; 01514 husart->RxXferSize = Size; 01515 husart->pTxBuffPtr = pRxData; 01516 husart->TxXferSize = Size; 01517 01518 husart->ErrorCode = HAL_USART_ERROR_NONE; 01519 husart->State = HAL_USART_STATE_BUSY_RX; 01520 01521 if (husart->hdmarx != NULL) 01522 { 01523 /* Set the USART DMA Rx transfer complete callback */ 01524 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; 01525 01526 /* Set the USART DMA Half transfer complete callback */ 01527 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; 01528 01529 /* Set the USART DMA Rx transfer error callback */ 01530 husart->hdmarx->XferErrorCallback = USART_DMAError; 01531 01532 /* Enable the USART receive DMA channel */ 01533 status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size); 01534 } 01535 01536 if ((status == HAL_OK) && 01537 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 01538 { 01539 /* Enable the USART transmit DMA channel: the transmit channel is used in order 01540 to generate in the non-blocking mode the clock to the slave device, 01541 this mode isn't a simplex receive mode but a full-duplex receive mode */ 01542 01543 /* Set the USART DMA Tx Complete and Error callback to Null */ 01544 if (husart->hdmatx != NULL) 01545 { 01546 husart->hdmatx->XferErrorCallback = NULL; 01547 husart->hdmatx->XferHalfCpltCallback = NULL; 01548 husart->hdmatx->XferCpltCallback = NULL; 01549 status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size); 01550 } 01551 } 01552 01553 if (status == HAL_OK) 01554 { 01555 /* Process Unlocked */ 01556 __HAL_UNLOCK(husart); 01557 01558 if (husart->Init.Parity != USART_PARITY_NONE) 01559 { 01560 /* Enable the USART Parity Error Interrupt */ 01561 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01562 } 01563 01564 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 01565 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01566 01567 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 01568 in the USART CR3 register */ 01569 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01570 01571 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01572 in the USART CR3 register */ 01573 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01574 01575 return HAL_OK; 01576 } 01577 else 01578 { 01579 if (husart->hdmarx != NULL) 01580 { 01581 status = HAL_DMA_Abort(husart->hdmarx); 01582 } 01583 01584 /* No need to check on error code */ 01585 UNUSED(status); 01586 01587 /* Set error code to DMA */ 01588 husart->ErrorCode = HAL_USART_ERROR_DMA; 01589 01590 /* Process Unlocked */ 01591 __HAL_UNLOCK(husart); 01592 01593 /* Restore husart->State to ready */ 01594 husart->State = HAL_USART_STATE_READY; 01595 01596 return HAL_ERROR; 01597 } 01598 } 01599 else 01600 { 01601 return HAL_BUSY; 01602 } 01603 } 01604 01605 /** 01606 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode. 01607 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit. 01608 * @note When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01609 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number 01610 * of u16 available through pTxData and through pRxData. 01611 * @param husart USART handle. 01612 * @param pTxData pointer to TX data buffer (u8 or u16 data elements). 01613 * @param pRxData pointer to RX data buffer (u8 or u16 data elements). 01614 * @param Size amount of data elements (u8 or u16) to be received/sent. 01615 * @retval HAL status 01616 */ 01617 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 01618 uint16_t Size) 01619 { 01620 HAL_StatusTypeDef status; 01621 const uint32_t *tmp; 01622 01623 if (husart->State == HAL_USART_STATE_READY) 01624 { 01625 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U)) 01626 { 01627 return HAL_ERROR; 01628 } 01629 01630 /* Process Locked */ 01631 __HAL_LOCK(husart); 01632 01633 husart->pRxBuffPtr = pRxData; 01634 husart->RxXferSize = Size; 01635 husart->pTxBuffPtr = pTxData; 01636 husart->TxXferSize = Size; 01637 01638 husart->ErrorCode = HAL_USART_ERROR_NONE; 01639 husart->State = HAL_USART_STATE_BUSY_TX_RX; 01640 01641 if ((husart->hdmarx != NULL) && (husart->hdmatx != NULL)) 01642 { 01643 /* Set the USART DMA Rx transfer complete callback */ 01644 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt; 01645 01646 /* Set the USART DMA Half transfer complete callback */ 01647 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt; 01648 01649 /* Set the USART DMA Tx transfer complete callback */ 01650 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt; 01651 01652 /* Set the USART DMA Half transfer complete callback */ 01653 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt; 01654 01655 /* Set the USART DMA Tx transfer error callback */ 01656 husart->hdmatx->XferErrorCallback = USART_DMAError; 01657 01658 /* Set the USART DMA Rx transfer error callback */ 01659 husart->hdmarx->XferErrorCallback = USART_DMAError; 01660 01661 /* Enable the USART receive DMA channel */ 01662 tmp = (uint32_t *)&pRxData; 01663 status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(const uint32_t *)tmp, Size); 01664 01665 /* Enable the USART transmit DMA channel */ 01666 if (status == HAL_OK) 01667 { 01668 tmp = (const uint32_t *)&pTxData; 01669 status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size); 01670 } 01671 } 01672 else 01673 { 01674 status = HAL_ERROR; 01675 } 01676 01677 if (status == HAL_OK) 01678 { 01679 /* Process Unlocked */ 01680 __HAL_UNLOCK(husart); 01681 01682 if (husart->Init.Parity != USART_PARITY_NONE) 01683 { 01684 /* Enable the USART Parity Error Interrupt */ 01685 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01686 } 01687 01688 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 01689 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01690 01691 /* Clear the TC flag in the ICR register */ 01692 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF); 01693 01694 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 01695 in the USART CR3 register */ 01696 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01697 01698 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01699 in the USART CR3 register */ 01700 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01701 01702 return HAL_OK; 01703 } 01704 else 01705 { 01706 if (husart->hdmarx != NULL) 01707 { 01708 status = HAL_DMA_Abort(husart->hdmarx); 01709 } 01710 01711 /* No need to check on error code */ 01712 UNUSED(status); 01713 01714 /* Set error code to DMA */ 01715 husart->ErrorCode = HAL_USART_ERROR_DMA; 01716 01717 /* Process Unlocked */ 01718 __HAL_UNLOCK(husart); 01719 01720 /* Restore husart->State to ready */ 01721 husart->State = HAL_USART_STATE_READY; 01722 01723 return HAL_ERROR; 01724 } 01725 } 01726 else 01727 { 01728 return HAL_BUSY; 01729 } 01730 } 01731 01732 /** 01733 * @brief Pause the DMA Transfer. 01734 * @param husart USART handle. 01735 * @retval HAL status 01736 */ 01737 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart) 01738 { 01739 const HAL_USART_StateTypeDef state = husart->State; 01740 01741 /* Process Locked */ 01742 __HAL_LOCK(husart); 01743 01744 if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) && 01745 (state == HAL_USART_STATE_BUSY_TX)) 01746 { 01747 /* Disable the USART DMA Tx request */ 01748 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01749 } 01750 else if ((state == HAL_USART_STATE_BUSY_RX) || 01751 (state == HAL_USART_STATE_BUSY_TX_RX)) 01752 { 01753 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) 01754 { 01755 /* Disable the USART DMA Tx request */ 01756 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01757 } 01758 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) 01759 { 01760 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01761 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01762 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); 01763 01764 /* Disable the USART DMA Rx request */ 01765 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01766 } 01767 } 01768 else 01769 { 01770 /* Nothing to do */ 01771 } 01772 01773 /* Process Unlocked */ 01774 __HAL_UNLOCK(husart); 01775 01776 return HAL_OK; 01777 } 01778 01779 /** 01780 * @brief Resume the DMA Transfer. 01781 * @param husart USART handle. 01782 * @retval HAL status 01783 */ 01784 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart) 01785 { 01786 const HAL_USART_StateTypeDef state = husart->State; 01787 01788 /* Process Locked */ 01789 __HAL_LOCK(husart); 01790 01791 if (state == HAL_USART_STATE_BUSY_TX) 01792 { 01793 /* Enable the USART DMA Tx request */ 01794 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01795 } 01796 else if ((state == HAL_USART_STATE_BUSY_RX) || 01797 (state == HAL_USART_STATE_BUSY_TX_RX)) 01798 { 01799 /* Clear the Overrun flag before resuming the Rx transfer*/ 01800 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF); 01801 01802 /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01803 if (husart->Init.Parity != USART_PARITY_NONE) 01804 { 01805 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE); 01806 } 01807 SET_BIT(husart->Instance->CR3, USART_CR3_EIE); 01808 01809 /* Enable the USART DMA Rx request before the DMA Tx request */ 01810 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01811 01812 /* Enable the USART DMA Tx request */ 01813 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01814 } 01815 else 01816 { 01817 /* Nothing to do */ 01818 } 01819 01820 /* Process Unlocked */ 01821 __HAL_UNLOCK(husart); 01822 01823 return HAL_OK; 01824 } 01825 01826 /** 01827 * @brief Stop the DMA Transfer. 01828 * @param husart USART handle. 01829 * @retval HAL status 01830 */ 01831 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart) 01832 { 01833 /* The Lock is not implemented on this API to allow the user application 01834 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() / 01835 HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback: 01836 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete 01837 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of 01838 the stream and the corresponding call back is executed. */ 01839 01840 /* Disable the USART Tx/Rx DMA requests */ 01841 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01842 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01843 01844 /* Abort the USART DMA tx channel */ 01845 if (husart->hdmatx != NULL) 01846 { 01847 if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK) 01848 { 01849 if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01850 { 01851 /* Set error code to DMA */ 01852 husart->ErrorCode = HAL_USART_ERROR_DMA; 01853 01854 return HAL_TIMEOUT; 01855 } 01856 } 01857 } 01858 /* Abort the USART DMA rx channel */ 01859 if (husart->hdmarx != NULL) 01860 { 01861 if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK) 01862 { 01863 if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01864 { 01865 /* Set error code to DMA */ 01866 husart->ErrorCode = HAL_USART_ERROR_DMA; 01867 01868 return HAL_TIMEOUT; 01869 } 01870 } 01871 } 01872 01873 USART_EndTransfer(husart); 01874 husart->State = HAL_USART_STATE_READY; 01875 01876 return HAL_OK; 01877 } 01878 01879 /** 01880 * @brief Abort ongoing transfers (blocking mode). 01881 * @param husart USART handle. 01882 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01883 * This procedure performs following operations : 01884 * - Disable USART Interrupts (Tx and Rx) 01885 * - Disable the DMA transfer in the peripheral register (if enabled) 01886 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01887 * - Set handle State to READY 01888 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01889 * @retval HAL status 01890 */ 01891 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart) 01892 { 01893 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01894 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | 01895 USART_CR1_TCIE)); 01896 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 01897 01898 /* Abort the USART DMA Tx channel if enabled */ 01899 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) 01900 { 01901 /* Disable the USART DMA Tx request if enabled */ 01902 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 01903 01904 /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */ 01905 if (husart->hdmatx != NULL) 01906 { 01907 /* Set the USART DMA Abort callback to Null. 01908 No call back execution at end of DMA abort procedure */ 01909 husart->hdmatx->XferAbortCallback = NULL; 01910 01911 if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK) 01912 { 01913 if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT) 01914 { 01915 /* Set error code to DMA */ 01916 husart->ErrorCode = HAL_USART_ERROR_DMA; 01917 01918 return HAL_TIMEOUT; 01919 } 01920 } 01921 } 01922 } 01923 01924 /* Abort the USART DMA Rx channel if enabled */ 01925 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) 01926 { 01927 /* Disable the USART DMA Rx request if enabled */ 01928 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); 01929 01930 /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */ 01931 if (husart->hdmarx != NULL) 01932 { 01933 /* Set the USART DMA Abort callback to Null. 01934 No call back execution at end of DMA abort procedure */ 01935 husart->hdmarx->XferAbortCallback = NULL; 01936 01937 if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK) 01938 { 01939 if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT) 01940 { 01941 /* Set error code to DMA */ 01942 husart->ErrorCode = HAL_USART_ERROR_DMA; 01943 01944 return HAL_TIMEOUT; 01945 } 01946 } 01947 } 01948 } 01949 01950 /* Reset Tx and Rx transfer counters */ 01951 husart->TxXferCount = 0U; 01952 husart->RxXferCount = 0U; 01953 01954 /* Clear the Error flags in the ICR register */ 01955 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF); 01956 01957 /* Flush the whole TX FIFO (if needed) */ 01958 if (husart->FifoMode == USART_FIFOMODE_ENABLE) 01959 { 01960 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 01961 } 01962 01963 /* Discard the received data */ 01964 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 01965 01966 /* Restore husart->State to Ready */ 01967 husart->State = HAL_USART_STATE_READY; 01968 01969 /* Reset Handle ErrorCode to No Error */ 01970 husart->ErrorCode = HAL_USART_ERROR_NONE; 01971 01972 return HAL_OK; 01973 } 01974 01975 /** 01976 * @brief Abort ongoing transfers (Interrupt mode). 01977 * @param husart USART handle. 01978 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01979 * This procedure performs following operations : 01980 * - Disable USART Interrupts (Tx and Rx) 01981 * - Disable the DMA transfer in the peripheral register (if enabled) 01982 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01983 * - Set handle State to READY 01984 * - At abort completion, call user abort complete callback 01985 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01986 * considered as completed only when user abort complete callback is executed (not when exiting function). 01987 * @retval HAL status 01988 */ 01989 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart) 01990 { 01991 uint32_t abortcplt = 1U; 01992 01993 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01994 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | 01995 USART_CR1_TCIE)); 01996 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 01997 01998 /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised 01999 before any call to DMA Abort functions */ 02000 /* DMA Tx Handle is valid */ 02001 if (husart->hdmatx != NULL) 02002 { 02003 /* Set DMA Abort Complete callback if USART DMA Tx request if enabled. 02004 Otherwise, set it to NULL */ 02005 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) 02006 { 02007 husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback; 02008 } 02009 else 02010 { 02011 husart->hdmatx->XferAbortCallback = NULL; 02012 } 02013 } 02014 /* DMA Rx Handle is valid */ 02015 if (husart->hdmarx != NULL) 02016 { 02017 /* Set DMA Abort Complete callback if USART DMA Rx request if enabled. 02018 Otherwise, set it to NULL */ 02019 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) 02020 { 02021 husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback; 02022 } 02023 else 02024 { 02025 husart->hdmarx->XferAbortCallback = NULL; 02026 } 02027 } 02028 02029 /* Abort the USART DMA Tx channel if enabled */ 02030 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) 02031 { 02032 /* Disable DMA Tx at USART level */ 02033 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 02034 02035 /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */ 02036 if (husart->hdmatx != NULL) 02037 { 02038 /* USART Tx DMA Abort callback has already been initialised : 02039 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ 02040 02041 /* Abort DMA TX */ 02042 if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK) 02043 { 02044 husart->hdmatx->XferAbortCallback = NULL; 02045 } 02046 else 02047 { 02048 abortcplt = 0U; 02049 } 02050 } 02051 } 02052 02053 /* Abort the USART DMA Rx channel if enabled */ 02054 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) 02055 { 02056 /* Disable the USART DMA Rx request if enabled */ 02057 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); 02058 02059 /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */ 02060 if (husart->hdmarx != NULL) 02061 { 02062 /* USART Rx DMA Abort callback has already been initialised : 02063 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */ 02064 02065 /* Abort DMA RX */ 02066 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) 02067 { 02068 husart->hdmarx->XferAbortCallback = NULL; 02069 abortcplt = 1U; 02070 } 02071 else 02072 { 02073 abortcplt = 0U; 02074 } 02075 } 02076 } 02077 02078 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ 02079 if (abortcplt == 1U) 02080 { 02081 /* Reset Tx and Rx transfer counters */ 02082 husart->TxXferCount = 0U; 02083 husart->RxXferCount = 0U; 02084 02085 /* Reset errorCode */ 02086 husart->ErrorCode = HAL_USART_ERROR_NONE; 02087 02088 /* Clear the Error flags in the ICR register */ 02089 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF); 02090 02091 /* Flush the whole TX FIFO (if needed) */ 02092 if (husart->FifoMode == USART_FIFOMODE_ENABLE) 02093 { 02094 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 02095 } 02096 02097 /* Discard the received data */ 02098 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 02099 02100 /* Restore husart->State to Ready */ 02101 husart->State = HAL_USART_STATE_READY; 02102 02103 /* As no DMA to be aborted, call directly user Abort complete callback */ 02104 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02105 /* Call registered Abort Complete Callback */ 02106 husart->AbortCpltCallback(husart); 02107 #else 02108 /* Call legacy weak Abort Complete Callback */ 02109 HAL_USART_AbortCpltCallback(husart); 02110 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02111 } 02112 02113 return HAL_OK; 02114 } 02115 02116 /** 02117 * @brief Handle USART interrupt request. 02118 * @param husart USART handle. 02119 * @retval None 02120 */ 02121 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart) 02122 { 02123 uint32_t isrflags = READ_REG(husart->Instance->ISR); 02124 uint32_t cr1its = READ_REG(husart->Instance->CR1); 02125 uint32_t cr3its = READ_REG(husart->Instance->CR3); 02126 02127 uint32_t errorflags; 02128 uint32_t errorcode; 02129 02130 /* If no error occurs */ 02131 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF | 02132 USART_ISR_UDR)); 02133 if (errorflags == 0U) 02134 { 02135 /* USART in mode Receiver ---------------------------------------------------*/ 02136 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 02137 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02138 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 02139 { 02140 if (husart->RxISR != NULL) 02141 { 02142 husart->RxISR(husart); 02143 } 02144 return; 02145 } 02146 } 02147 02148 /* If some errors occur */ 02149 if ((errorflags != 0U) 02150 && (((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U) 02151 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))) 02152 { 02153 /* USART parity error interrupt occurred -------------------------------------*/ 02154 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U)) 02155 { 02156 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF); 02157 02158 husart->ErrorCode |= HAL_USART_ERROR_PE; 02159 } 02160 02161 /* USART frame error interrupt occurred --------------------------------------*/ 02162 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02163 { 02164 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF); 02165 02166 husart->ErrorCode |= HAL_USART_ERROR_FE; 02167 } 02168 02169 /* USART noise error interrupt occurred --------------------------------------*/ 02170 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02171 { 02172 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF); 02173 02174 husart->ErrorCode |= HAL_USART_ERROR_NE; 02175 } 02176 02177 /* USART Over-Run interrupt occurred -----------------------------------------*/ 02178 if (((isrflags & USART_ISR_ORE) != 0U) 02179 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) || 02180 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U))) 02181 { 02182 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF); 02183 02184 husart->ErrorCode |= HAL_USART_ERROR_ORE; 02185 } 02186 02187 /* USART Receiver Timeout interrupt occurred ---------------------------------*/ 02188 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U)) 02189 { 02190 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_RTOF); 02191 02192 husart->ErrorCode |= HAL_USART_ERROR_RTO; 02193 } 02194 02195 /* USART SPI slave underrun error interrupt occurred -------------------------*/ 02196 if (((isrflags & USART_ISR_UDR) != 0U) && ((cr3its & USART_CR3_EIE) != 0U)) 02197 { 02198 /* Ignore SPI slave underrun errors when reception is going on */ 02199 if (husart->State == HAL_USART_STATE_BUSY_RX) 02200 { 02201 __HAL_USART_CLEAR_UDRFLAG(husart); 02202 return; 02203 } 02204 else 02205 { 02206 __HAL_USART_CLEAR_UDRFLAG(husart); 02207 husart->ErrorCode |= HAL_USART_ERROR_UDR; 02208 } 02209 } 02210 02211 /* Call USART Error Call back function if need be --------------------------*/ 02212 if (husart->ErrorCode != HAL_USART_ERROR_NONE) 02213 { 02214 /* USART in mode Receiver ---------------------------------------------------*/ 02215 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U) 02216 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) 02217 || ((cr3its & USART_CR3_RXFTIE) != 0U))) 02218 { 02219 if (husart->RxISR != NULL) 02220 { 02221 husart->RxISR(husart); 02222 } 02223 } 02224 02225 /* If Overrun error occurs, or if any error occurs in DMA mode reception, 02226 consider error as blocking */ 02227 errorcode = husart->ErrorCode & HAL_USART_ERROR_ORE; 02228 if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) || 02229 (errorcode != 0U)) 02230 { 02231 /* Blocking error : transfer is aborted 02232 Set the USART state ready to be able to start again the process, 02233 Disable Interrupts, and disable DMA requests, if ongoing */ 02234 USART_EndTransfer(husart); 02235 02236 /* Abort the USART DMA Rx channel if enabled */ 02237 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) 02238 { 02239 /* Disable the USART DMA Rx request if enabled */ 02240 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR); 02241 02242 /* Abort the USART DMA Tx channel */ 02243 if (husart->hdmatx != NULL) 02244 { 02245 /* Set the USART Tx DMA Abort callback to NULL : no callback 02246 executed at end of DMA abort procedure */ 02247 husart->hdmatx->XferAbortCallback = NULL; 02248 02249 /* Abort DMA TX */ 02250 (void)HAL_DMA_Abort_IT(husart->hdmatx); 02251 } 02252 02253 /* Abort the USART DMA Rx channel */ 02254 if (husart->hdmarx != NULL) 02255 { 02256 /* Set the USART Rx DMA Abort callback : 02257 will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */ 02258 husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError; 02259 02260 /* Abort DMA RX */ 02261 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK) 02262 { 02263 /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */ 02264 husart->hdmarx->XferAbortCallback(husart->hdmarx); 02265 } 02266 } 02267 else 02268 { 02269 /* Call user error callback */ 02270 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02271 /* Call registered Error Callback */ 02272 husart->ErrorCallback(husart); 02273 #else 02274 /* Call legacy weak Error Callback */ 02275 HAL_USART_ErrorCallback(husart); 02276 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02277 } 02278 } 02279 else 02280 { 02281 /* Call user error callback */ 02282 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02283 /* Call registered Error Callback */ 02284 husart->ErrorCallback(husart); 02285 #else 02286 /* Call legacy weak Error Callback */ 02287 HAL_USART_ErrorCallback(husart); 02288 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02289 } 02290 } 02291 else 02292 { 02293 /* Non Blocking error : transfer could go on. 02294 Error is notified to user through user error callback */ 02295 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02296 /* Call registered Error Callback */ 02297 husart->ErrorCallback(husart); 02298 #else 02299 /* Call legacy weak Error Callback */ 02300 HAL_USART_ErrorCallback(husart); 02301 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02302 husart->ErrorCode = HAL_USART_ERROR_NONE; 02303 } 02304 } 02305 return; 02306 02307 } /* End if some error occurs */ 02308 02309 02310 /* USART in mode Transmitter ------------------------------------------------*/ 02311 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U) 02312 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U) 02313 || ((cr3its & USART_CR3_TXFTIE) != 0U))) 02314 { 02315 if (husart->TxISR != NULL) 02316 { 02317 husart->TxISR(husart); 02318 } 02319 return; 02320 } 02321 02322 /* USART in mode Transmitter (transmission end) -----------------------------*/ 02323 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U)) 02324 { 02325 USART_EndTransmit_IT(husart); 02326 return; 02327 } 02328 02329 /* USART TX Fifo Empty occurred ----------------------------------------------*/ 02330 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U)) 02331 { 02332 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02333 /* Call registered Tx Fifo Empty Callback */ 02334 husart->TxFifoEmptyCallback(husart); 02335 #else 02336 /* Call legacy weak Tx Fifo Empty Callback */ 02337 HAL_USARTEx_TxFifoEmptyCallback(husart); 02338 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02339 return; 02340 } 02341 02342 /* USART RX Fifo Full occurred ----------------------------------------------*/ 02343 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U)) 02344 { 02345 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02346 /* Call registered Rx Fifo Full Callback */ 02347 husart->RxFifoFullCallback(husart); 02348 #else 02349 /* Call legacy weak Rx Fifo Full Callback */ 02350 HAL_USARTEx_RxFifoFullCallback(husart); 02351 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02352 return; 02353 } 02354 } 02355 02356 /** 02357 * @brief Tx Transfer completed callback. 02358 * @param husart USART handle. 02359 * @retval None 02360 */ 02361 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart) 02362 { 02363 /* Prevent unused argument(s) compilation warning */ 02364 UNUSED(husart); 02365 02366 /* NOTE : This function should not be modified, when the callback is needed, 02367 the HAL_USART_TxCpltCallback can be implemented in the user file. 02368 */ 02369 } 02370 02371 /** 02372 * @brief Tx Half Transfer completed callback. 02373 * @param husart USART handle. 02374 * @retval None 02375 */ 02376 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart) 02377 { 02378 /* Prevent unused argument(s) compilation warning */ 02379 UNUSED(husart); 02380 02381 /* NOTE: This function should not be modified, when the callback is needed, 02382 the HAL_USART_TxHalfCpltCallback can be implemented in the user file. 02383 */ 02384 } 02385 02386 /** 02387 * @brief Rx Transfer completed callback. 02388 * @param husart USART handle. 02389 * @retval None 02390 */ 02391 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart) 02392 { 02393 /* Prevent unused argument(s) compilation warning */ 02394 UNUSED(husart); 02395 02396 /* NOTE: This function should not be modified, when the callback is needed, 02397 the HAL_USART_RxCpltCallback can be implemented in the user file. 02398 */ 02399 } 02400 02401 /** 02402 * @brief Rx Half Transfer completed callback. 02403 * @param husart USART handle. 02404 * @retval None 02405 */ 02406 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart) 02407 { 02408 /* Prevent unused argument(s) compilation warning */ 02409 UNUSED(husart); 02410 02411 /* NOTE : This function should not be modified, when the callback is needed, 02412 the HAL_USART_RxHalfCpltCallback can be implemented in the user file 02413 */ 02414 } 02415 02416 /** 02417 * @brief Tx/Rx Transfers completed callback for the non-blocking process. 02418 * @param husart USART handle. 02419 * @retval None 02420 */ 02421 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart) 02422 { 02423 /* Prevent unused argument(s) compilation warning */ 02424 UNUSED(husart); 02425 02426 /* NOTE : This function should not be modified, when the callback is needed, 02427 the HAL_USART_TxRxCpltCallback can be implemented in the user file 02428 */ 02429 } 02430 02431 /** 02432 * @brief USART error callback. 02433 * @param husart USART handle. 02434 * @retval None 02435 */ 02436 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart) 02437 { 02438 /* Prevent unused argument(s) compilation warning */ 02439 UNUSED(husart); 02440 02441 /* NOTE : This function should not be modified, when the callback is needed, 02442 the HAL_USART_ErrorCallback can be implemented in the user file. 02443 */ 02444 } 02445 02446 /** 02447 * @brief USART Abort Complete callback. 02448 * @param husart USART handle. 02449 * @retval None 02450 */ 02451 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart) 02452 { 02453 /* Prevent unused argument(s) compilation warning */ 02454 UNUSED(husart); 02455 02456 /* NOTE : This function should not be modified, when the callback is needed, 02457 the HAL_USART_AbortCpltCallback can be implemented in the user file. 02458 */ 02459 } 02460 02461 /** 02462 * @} 02463 */ 02464 02465 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions 02466 * @brief USART Peripheral State and Error functions 02467 * 02468 @verbatim 02469 ============================================================================== 02470 ##### Peripheral State and Error functions ##### 02471 ============================================================================== 02472 [..] 02473 This subsection provides functions allowing to : 02474 (+) Return the USART handle state 02475 (+) Return the USART handle error code 02476 02477 @endverbatim 02478 * @{ 02479 */ 02480 02481 02482 /** 02483 * @brief Return the USART handle state. 02484 * @param husart pointer to a USART_HandleTypeDef structure that contains 02485 * the configuration information for the specified USART. 02486 * @retval USART handle state 02487 */ 02488 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart) 02489 { 02490 return husart->State; 02491 } 02492 02493 /** 02494 * @brief Return the USART error code. 02495 * @param husart pointer to a USART_HandleTypeDef structure that contains 02496 * the configuration information for the specified USART. 02497 * @retval USART handle Error Code 02498 */ 02499 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart) 02500 { 02501 return husart->ErrorCode; 02502 } 02503 02504 /** 02505 * @} 02506 */ 02507 02508 /** 02509 * @} 02510 */ 02511 02512 /** @defgroup USART_Private_Functions USART Private Functions 02513 * @{ 02514 */ 02515 02516 /** 02517 * @brief Initialize the callbacks to their default values. 02518 * @param husart USART handle. 02519 * @retval none 02520 */ 02521 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02522 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart) 02523 { 02524 /* Init the USART Callback settings */ 02525 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 02526 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 02527 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 02528 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 02529 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */ 02530 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */ 02531 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 02532 husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */ 02533 husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */ 02534 } 02535 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02536 02537 /** 02538 * @brief End ongoing transfer on USART peripheral (following error detection or Transfer completion). 02539 * @param husart USART handle. 02540 * @retval None 02541 */ 02542 static void USART_EndTransfer(USART_HandleTypeDef *husart) 02543 { 02544 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */ 02545 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | 02546 USART_CR1_TCIE)); 02547 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE)); 02548 02549 /* At end of process, restore husart->State to Ready */ 02550 husart->State = HAL_USART_STATE_READY; 02551 } 02552 02553 /** 02554 * @brief DMA USART transmit process complete callback. 02555 * @param hdma DMA handle. 02556 * @retval None 02557 */ 02558 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma) 02559 { 02560 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02561 02562 /* DMA Normal mode */ 02563 if (hdma->Init.Mode != DMA_CIRCULAR) 02564 { 02565 husart->TxXferCount = 0U; 02566 02567 if (husart->State == HAL_USART_STATE_BUSY_TX) 02568 { 02569 /* Disable the DMA transfer for transmit request by resetting the DMAT bit 02570 in the USART CR3 register */ 02571 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 02572 02573 /* Enable the USART Transmit Complete Interrupt */ 02574 __HAL_USART_ENABLE_IT(husart, USART_IT_TC); 02575 } 02576 } 02577 /* DMA Circular mode */ 02578 else 02579 { 02580 if (husart->State == HAL_USART_STATE_BUSY_TX) 02581 { 02582 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02583 /* Call registered Tx Complete Callback */ 02584 husart->TxCpltCallback(husart); 02585 #else 02586 /* Call legacy weak Tx Complete Callback */ 02587 HAL_USART_TxCpltCallback(husart); 02588 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02589 } 02590 } 02591 } 02592 02593 /** 02594 * @brief DMA USART transmit process half complete callback. 02595 * @param hdma DMA handle. 02596 * @retval None 02597 */ 02598 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 02599 { 02600 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02601 02602 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02603 /* Call registered Tx Half Complete Callback */ 02604 husart->TxHalfCpltCallback(husart); 02605 #else 02606 /* Call legacy weak Tx Half Complete Callback */ 02607 HAL_USART_TxHalfCpltCallback(husart); 02608 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02609 } 02610 02611 /** 02612 * @brief DMA USART receive process complete callback. 02613 * @param hdma DMA handle. 02614 * @retval None 02615 */ 02616 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 02617 { 02618 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02619 02620 /* DMA Normal mode */ 02621 if (hdma->Init.Mode != DMA_CIRCULAR) 02622 { 02623 husart->RxXferCount = 0U; 02624 02625 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 02626 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); 02627 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); 02628 02629 /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit 02630 in USART CR3 register */ 02631 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR); 02632 /* similarly, disable the DMA TX transfer that was started to provide the 02633 clock to the slave device */ 02634 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT); 02635 02636 if (husart->State == HAL_USART_STATE_BUSY_RX) 02637 { 02638 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02639 /* Call registered Rx Complete Callback */ 02640 husart->RxCpltCallback(husart); 02641 #else 02642 /* Call legacy weak Rx Complete Callback */ 02643 HAL_USART_RxCpltCallback(husart); 02644 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02645 } 02646 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ 02647 else 02648 { 02649 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02650 /* Call registered Tx Rx Complete Callback */ 02651 husart->TxRxCpltCallback(husart); 02652 #else 02653 /* Call legacy weak Tx Rx Complete Callback */ 02654 HAL_USART_TxRxCpltCallback(husart); 02655 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02656 } 02657 husart->State = HAL_USART_STATE_READY; 02658 } 02659 /* DMA circular mode */ 02660 else 02661 { 02662 if (husart->State == HAL_USART_STATE_BUSY_RX) 02663 { 02664 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02665 /* Call registered Rx Complete Callback */ 02666 husart->RxCpltCallback(husart); 02667 #else 02668 /* Call legacy weak Rx Complete Callback */ 02669 HAL_USART_RxCpltCallback(husart); 02670 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02671 } 02672 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */ 02673 else 02674 { 02675 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02676 /* Call registered Tx Rx Complete Callback */ 02677 husart->TxRxCpltCallback(husart); 02678 #else 02679 /* Call legacy weak Tx Rx Complete Callback */ 02680 HAL_USART_TxRxCpltCallback(husart); 02681 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02682 } 02683 } 02684 } 02685 02686 /** 02687 * @brief DMA USART receive process half complete callback. 02688 * @param hdma DMA handle. 02689 * @retval None 02690 */ 02691 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 02692 { 02693 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02694 02695 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02696 /* Call registered Rx Half Complete Callback */ 02697 husart->RxHalfCpltCallback(husart); 02698 #else 02699 /* Call legacy weak Rx Half Complete Callback */ 02700 HAL_USART_RxHalfCpltCallback(husart); 02701 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02702 } 02703 02704 /** 02705 * @brief DMA USART communication error callback. 02706 * @param hdma DMA handle. 02707 * @retval None 02708 */ 02709 static void USART_DMAError(DMA_HandleTypeDef *hdma) 02710 { 02711 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02712 02713 husart->RxXferCount = 0U; 02714 husart->TxXferCount = 0U; 02715 USART_EndTransfer(husart); 02716 02717 husart->ErrorCode |= HAL_USART_ERROR_DMA; 02718 husart->State = HAL_USART_STATE_READY; 02719 02720 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02721 /* Call registered Error Callback */ 02722 husart->ErrorCallback(husart); 02723 #else 02724 /* Call legacy weak Error Callback */ 02725 HAL_USART_ErrorCallback(husart); 02726 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02727 } 02728 02729 /** 02730 * @brief DMA USART communication abort callback, when initiated by HAL services on Error 02731 * (To be called at end of DMA Abort procedure following error occurrence). 02732 * @param hdma DMA handle. 02733 * @retval None 02734 */ 02735 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma) 02736 { 02737 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02738 husart->RxXferCount = 0U; 02739 husart->TxXferCount = 0U; 02740 02741 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02742 /* Call registered Error Callback */ 02743 husart->ErrorCallback(husart); 02744 #else 02745 /* Call legacy weak Error Callback */ 02746 HAL_USART_ErrorCallback(husart); 02747 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02748 } 02749 02750 /** 02751 * @brief DMA USART Tx communication abort callback, when initiated by user 02752 * (To be called at end of DMA Tx Abort procedure following user abort request). 02753 * @note When this callback is executed, User Abort complete call back is called only if no 02754 * Abort still ongoing for Rx DMA Handle. 02755 * @param hdma DMA handle. 02756 * @retval None 02757 */ 02758 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) 02759 { 02760 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02761 02762 husart->hdmatx->XferAbortCallback = NULL; 02763 02764 /* Check if an Abort process is still ongoing */ 02765 if (husart->hdmarx != NULL) 02766 { 02767 if (husart->hdmarx->XferAbortCallback != NULL) 02768 { 02769 return; 02770 } 02771 } 02772 02773 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02774 husart->TxXferCount = 0U; 02775 husart->RxXferCount = 0U; 02776 02777 /* Reset errorCode */ 02778 husart->ErrorCode = HAL_USART_ERROR_NONE; 02779 02780 /* Clear the Error flags in the ICR register */ 02781 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF); 02782 02783 /* Restore husart->State to Ready */ 02784 husart->State = HAL_USART_STATE_READY; 02785 02786 /* Call user Abort complete callback */ 02787 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02788 /* Call registered Abort Complete Callback */ 02789 husart->AbortCpltCallback(husart); 02790 #else 02791 /* Call legacy weak Abort Complete Callback */ 02792 HAL_USART_AbortCpltCallback(husart); 02793 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02794 02795 } 02796 02797 02798 /** 02799 * @brief DMA USART Rx communication abort callback, when initiated by user 02800 * (To be called at end of DMA Rx Abort procedure following user abort request). 02801 * @note When this callback is executed, User Abort complete call back is called only if no 02802 * Abort still ongoing for Tx DMA Handle. 02803 * @param hdma DMA handle. 02804 * @retval None 02805 */ 02806 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) 02807 { 02808 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent); 02809 02810 husart->hdmarx->XferAbortCallback = NULL; 02811 02812 /* Check if an Abort process is still ongoing */ 02813 if (husart->hdmatx != NULL) 02814 { 02815 if (husart->hdmatx->XferAbortCallback != NULL) 02816 { 02817 return; 02818 } 02819 } 02820 02821 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02822 husart->TxXferCount = 0U; 02823 husart->RxXferCount = 0U; 02824 02825 /* Reset errorCode */ 02826 husart->ErrorCode = HAL_USART_ERROR_NONE; 02827 02828 /* Clear the Error flags in the ICR register */ 02829 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF); 02830 02831 /* Restore husart->State to Ready */ 02832 husart->State = HAL_USART_STATE_READY; 02833 02834 /* Call user Abort complete callback */ 02835 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 02836 /* Call registered Abort Complete Callback */ 02837 husart->AbortCpltCallback(husart); 02838 #else 02839 /* Call legacy weak Abort Complete Callback */ 02840 HAL_USART_AbortCpltCallback(husart); 02841 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 02842 } 02843 02844 02845 /** 02846 * @brief Handle USART Communication Timeout. It waits 02847 * until a flag is no longer in the specified status. 02848 * @param husart USART handle. 02849 * @param Flag Specifies the USART flag to check. 02850 * @param Status the actual Flag status (SET or RESET). 02851 * @param Tickstart Tick start value 02852 * @param Timeout timeout duration. 02853 * @retval HAL status 02854 */ 02855 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, 02856 uint32_t Tickstart, uint32_t Timeout) 02857 { 02858 /* Wait until flag is set */ 02859 while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status) 02860 { 02861 /* Check for the Timeout */ 02862 if (Timeout != HAL_MAX_DELAY) 02863 { 02864 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U)) 02865 { 02866 husart->State = HAL_USART_STATE_READY; 02867 02868 /* Process Unlocked */ 02869 __HAL_UNLOCK(husart); 02870 02871 return HAL_TIMEOUT; 02872 } 02873 } 02874 } 02875 return HAL_OK; 02876 } 02877 02878 /** 02879 * @brief Configure the USART peripheral. 02880 * @param husart USART handle. 02881 * @retval HAL status 02882 */ 02883 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart) 02884 { 02885 uint32_t tmpreg; 02886 USART_ClockSourceTypeDef clocksource; 02887 HAL_StatusTypeDef ret = HAL_OK; 02888 uint16_t brrtemp; 02889 uint32_t usartdiv = 0x00000000; 02890 PLL2_ClocksTypeDef pll2_clocks; 02891 PLL3_ClocksTypeDef pll3_clocks; 02892 uint32_t pclk; 02893 02894 /* Check the parameters */ 02895 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity)); 02896 assert_param(IS_USART_PHASE(husart->Init.CLKPhase)); 02897 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit)); 02898 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate)); 02899 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength)); 02900 assert_param(IS_USART_STOPBITS(husart->Init.StopBits)); 02901 assert_param(IS_USART_PARITY(husart->Init.Parity)); 02902 assert_param(IS_USART_MODE(husart->Init.Mode)); 02903 assert_param(IS_USART_PRESCALER(husart->Init.ClockPrescaler)); 02904 02905 /*-------------------------- USART CR1 Configuration -----------------------*/ 02906 /* Clear M, PCE, PS, TE and RE bits and configure 02907 * the USART Word Length, Parity and Mode: 02908 * set the M bits according to husart->Init.WordLength value 02909 * set PCE and PS bits according to husart->Init.Parity value 02910 * set TE and RE bits according to husart->Init.Mode value 02911 * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */ 02912 tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8; 02913 MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg); 02914 02915 /*---------------------------- USART CR2 Configuration ---------------------*/ 02916 /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits: 02917 * set CPOL bit according to husart->Init.CLKPolarity value 02918 * set CPHA bit according to husart->Init.CLKPhase value 02919 * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only) 02920 * set STOP[13:12] bits according to husart->Init.StopBits value */ 02921 tmpreg = (uint32_t)(USART_CLOCK_ENABLE); 02922 tmpreg |= (uint32_t)husart->Init.CLKLastBit; 02923 tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase); 02924 tmpreg |= (uint32_t)husart->Init.StopBits; 02925 MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg); 02926 02927 /*-------------------------- USART PRESC Configuration -----------------------*/ 02928 /* Configure 02929 * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */ 02930 MODIFY_REG(husart->Instance->PRESC, USART_PRESC_PRESCALER, husart->Init.ClockPrescaler); 02931 02932 /*-------------------------- USART BRR Configuration -----------------------*/ 02933 /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */ 02934 USART_GETCLOCKSOURCE(husart, clocksource); 02935 02936 switch (clocksource) 02937 { 02938 case USART_CLOCKSOURCE_D2PCLK1: 02939 pclk = HAL_RCC_GetPCLK1Freq(); 02940 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02941 break; 02942 case USART_CLOCKSOURCE_D2PCLK2: 02943 pclk = HAL_RCC_GetPCLK2Freq(); 02944 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02945 break; 02946 case USART_CLOCKSOURCE_PLL2: 02947 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks); 02948 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll2_clocks.PLL2_Q_Frequency, husart->Init.BaudRate, 02949 husart->Init.ClockPrescaler)); 02950 break; 02951 case USART_CLOCKSOURCE_PLL3: 02952 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks); 02953 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll3_clocks.PLL3_Q_Frequency, husart->Init.BaudRate, 02954 husart->Init.ClockPrescaler)); 02955 break; 02956 case USART_CLOCKSOURCE_HSI: 02957 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U) 02958 { 02959 usartdiv = (uint32_t)(USART_DIV_SAMPLING8((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)), husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02960 } 02961 else 02962 { 02963 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02964 } 02965 break; 02966 case USART_CLOCKSOURCE_CSI: 02967 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(CSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02968 break; 02969 case USART_CLOCKSOURCE_LSE: 02970 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler)); 02971 break; 02972 default: 02973 ret = HAL_ERROR; 02974 break; 02975 } 02976 02977 /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */ 02978 if ((usartdiv >= USART_BRR_MIN) && (usartdiv <= USART_BRR_MAX)) 02979 { 02980 brrtemp = (uint16_t)(usartdiv & 0xFFF0U); 02981 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U); 02982 husart->Instance->BRR = brrtemp; 02983 } 02984 else 02985 { 02986 ret = HAL_ERROR; 02987 } 02988 02989 /* Initialize the number of data to process during RX/TX ISR execution */ 02990 husart->NbTxDataToProcess = 1U; 02991 husart->NbRxDataToProcess = 1U; 02992 02993 /* Clear ISR function pointers */ 02994 husart->RxISR = NULL; 02995 husart->TxISR = NULL; 02996 02997 return ret; 02998 } 02999 03000 /** 03001 * @brief Check the USART Idle State. 03002 * @param husart USART handle. 03003 * @retval HAL status 03004 */ 03005 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart) 03006 { 03007 uint32_t tickstart; 03008 03009 /* Initialize the USART ErrorCode */ 03010 husart->ErrorCode = HAL_USART_ERROR_NONE; 03011 03012 /* Init tickstart for timeout management */ 03013 tickstart = HAL_GetTick(); 03014 03015 /* Check if the Transmitter is enabled */ 03016 if ((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE) 03017 { 03018 /* Wait until TEACK flag is set */ 03019 if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK) 03020 { 03021 /* Timeout occurred */ 03022 return HAL_TIMEOUT; 03023 } 03024 } 03025 /* Check if the Receiver is enabled */ 03026 if ((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE) 03027 { 03028 /* Wait until REACK flag is set */ 03029 if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK) 03030 { 03031 /* Timeout occurred */ 03032 return HAL_TIMEOUT; 03033 } 03034 } 03035 03036 /* Initialize the USART state*/ 03037 husart->State = HAL_USART_STATE_READY; 03038 03039 /* Process Unlocked */ 03040 __HAL_UNLOCK(husart); 03041 03042 return HAL_OK; 03043 } 03044 03045 /** 03046 * @brief Simplex send an amount of data in non-blocking mode. 03047 * @note Function called under interruption only, once 03048 * interruptions have been enabled by HAL_USART_Transmit_IT(). 03049 * @note The USART errors are not managed to avoid the overrun error. 03050 * @note ISR function executed when FIFO mode is disabled and when the 03051 * data word length is less than 9 bits long. 03052 * @param husart USART handle. 03053 * @retval None 03054 */ 03055 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart) 03056 { 03057 const HAL_USART_StateTypeDef state = husart->State; 03058 03059 /* Check that a Tx process is ongoing */ 03060 if ((state == HAL_USART_STATE_BUSY_TX) || 03061 (state == HAL_USART_STATE_BUSY_TX_RX)) 03062 { 03063 if (husart->TxXferCount == 0U) 03064 { 03065 /* Disable the USART Transmit data register empty interrupt */ 03066 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE); 03067 03068 /* Enable the USART Transmit Complete Interrupt */ 03069 __HAL_USART_ENABLE_IT(husart, USART_IT_TC); 03070 } 03071 else 03072 { 03073 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF); 03074 husart->pTxBuffPtr++; 03075 husart->TxXferCount--; 03076 } 03077 } 03078 } 03079 03080 /** 03081 * @brief Simplex send an amount of data in non-blocking mode. 03082 * @note Function called under interruption only, once 03083 * interruptions have been enabled by HAL_USART_Transmit_IT(). 03084 * @note The USART errors are not managed to avoid the overrun error. 03085 * @note ISR function executed when FIFO mode is disabled and when the 03086 * data word length is 9 bits long. 03087 * @param husart USART handle. 03088 * @retval None 03089 */ 03090 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart) 03091 { 03092 const HAL_USART_StateTypeDef state = husart->State; 03093 const uint16_t *tmp; 03094 03095 if ((state == HAL_USART_STATE_BUSY_TX) || 03096 (state == HAL_USART_STATE_BUSY_TX_RX)) 03097 { 03098 if (husart->TxXferCount == 0U) 03099 { 03100 /* Disable the USART Transmit data register empty interrupt */ 03101 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE); 03102 03103 /* Enable the USART Transmit Complete Interrupt */ 03104 __HAL_USART_ENABLE_IT(husart, USART_IT_TC); 03105 } 03106 else 03107 { 03108 tmp = (const uint16_t *) husart->pTxBuffPtr; 03109 husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU); 03110 husart->pTxBuffPtr += 2U; 03111 husart->TxXferCount--; 03112 } 03113 } 03114 } 03115 03116 /** 03117 * @brief Simplex send an amount of data in non-blocking mode. 03118 * @note Function called under interruption only, once 03119 * interruptions have been enabled by HAL_USART_Transmit_IT(). 03120 * @note The USART errors are not managed to avoid the overrun error. 03121 * @note ISR function executed when FIFO mode is enabled and when the 03122 * data word length is less than 9 bits long. 03123 * @param husart USART handle. 03124 * @retval None 03125 */ 03126 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart) 03127 { 03128 const HAL_USART_StateTypeDef state = husart->State; 03129 uint16_t nb_tx_data; 03130 03131 /* Check that a Tx process is ongoing */ 03132 if ((state == HAL_USART_STATE_BUSY_TX) || 03133 (state == HAL_USART_STATE_BUSY_TX_RX)) 03134 { 03135 for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--) 03136 { 03137 if (husart->TxXferCount == 0U) 03138 { 03139 /* Disable the TX FIFO threshold interrupt */ 03140 __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT); 03141 03142 /* Enable the USART Transmit Complete Interrupt */ 03143 __HAL_USART_ENABLE_IT(husart, USART_IT_TC); 03144 03145 break; /* force exit loop */ 03146 } 03147 else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET) 03148 { 03149 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF); 03150 husart->pTxBuffPtr++; 03151 husart->TxXferCount--; 03152 } 03153 else 03154 { 03155 /* Nothing to do */ 03156 } 03157 } 03158 } 03159 } 03160 03161 /** 03162 * @brief Simplex send an amount of data in non-blocking mode. 03163 * @note Function called under interruption only, once 03164 * interruptions have been enabled by HAL_USART_Transmit_IT(). 03165 * @note The USART errors are not managed to avoid the overrun error. 03166 * @note ISR function executed when FIFO mode is enabled and when the 03167 * data word length is 9 bits long. 03168 * @param husart USART handle. 03169 * @retval None 03170 */ 03171 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart) 03172 { 03173 const HAL_USART_StateTypeDef state = husart->State; 03174 const uint16_t *tmp; 03175 uint16_t nb_tx_data; 03176 03177 /* Check that a Tx process is ongoing */ 03178 if ((state == HAL_USART_STATE_BUSY_TX) || 03179 (state == HAL_USART_STATE_BUSY_TX_RX)) 03180 { 03181 for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--) 03182 { 03183 if (husart->TxXferCount == 0U) 03184 { 03185 /* Disable the TX FIFO threshold interrupt */ 03186 __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT); 03187 03188 /* Enable the USART Transmit Complete Interrupt */ 03189 __HAL_USART_ENABLE_IT(husart, USART_IT_TC); 03190 03191 break; /* force exit loop */ 03192 } 03193 else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET) 03194 { 03195 tmp = (const uint16_t *) husart->pTxBuffPtr; 03196 husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU); 03197 husart->pTxBuffPtr += 2U; 03198 husart->TxXferCount--; 03199 } 03200 else 03201 { 03202 /* Nothing to do */ 03203 } 03204 } 03205 } 03206 } 03207 03208 /** 03209 * @brief Wraps up transmission in non-blocking mode. 03210 * @param husart Pointer to a USART_HandleTypeDef structure that contains 03211 * the configuration information for the specified USART module. 03212 * @retval None 03213 */ 03214 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart) 03215 { 03216 /* Disable the USART Transmit Complete Interrupt */ 03217 __HAL_USART_DISABLE_IT(husart, USART_IT_TC); 03218 03219 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 03220 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR); 03221 03222 /* Clear TxISR function pointer */ 03223 husart->TxISR = NULL; 03224 03225 if (husart->State == HAL_USART_STATE_BUSY_TX) 03226 { 03227 /* Clear overrun flag and discard the received data */ 03228 __HAL_USART_CLEAR_OREFLAG(husart); 03229 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 03230 03231 /* Tx process is completed, restore husart->State to Ready */ 03232 husart->State = HAL_USART_STATE_READY; 03233 03234 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03235 /* Call registered Tx Complete Callback */ 03236 husart->TxCpltCallback(husart); 03237 #else 03238 /* Call legacy weak Tx Complete Callback */ 03239 HAL_USART_TxCpltCallback(husart); 03240 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03241 } 03242 else if (husart->RxXferCount == 0U) 03243 { 03244 /* TxRx process is completed, restore husart->State to Ready */ 03245 husart->State = HAL_USART_STATE_READY; 03246 03247 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03248 /* Call registered Tx Rx Complete Callback */ 03249 husart->TxRxCpltCallback(husart); 03250 #else 03251 /* Call legacy weak Tx Rx Complete Callback */ 03252 HAL_USART_TxRxCpltCallback(husart); 03253 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03254 } 03255 else 03256 { 03257 /* Nothing to do */ 03258 } 03259 } 03260 03261 03262 /** 03263 * @brief Simplex receive an amount of data in non-blocking mode. 03264 * @note Function called under interruption only, once 03265 * interruptions have been enabled by HAL_USART_Receive_IT(). 03266 * @note ISR function executed when FIFO mode is disabled and when the 03267 * data word length is less than 9 bits long. 03268 * @param husart USART handle 03269 * @retval None 03270 */ 03271 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart) 03272 { 03273 const HAL_USART_StateTypeDef state = husart->State; 03274 uint16_t txdatacount; 03275 uint16_t uhMask = husart->Mask; 03276 uint32_t txftie; 03277 03278 if ((state == HAL_USART_STATE_BUSY_RX) || 03279 (state == HAL_USART_STATE_BUSY_TX_RX)) 03280 { 03281 *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask); 03282 husart->pRxBuffPtr++; 03283 husart->RxXferCount--; 03284 03285 if (husart->RxXferCount == 0U) 03286 { 03287 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/ 03288 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 03289 03290 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 03291 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); 03292 03293 /* Clear RxISR function pointer */ 03294 husart->RxISR = NULL; 03295 03296 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */ 03297 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE); 03298 txdatacount = husart->TxXferCount; 03299 03300 if (state == HAL_USART_STATE_BUSY_RX) 03301 { 03302 /* Clear SPI slave underrun flag and discard transmit data */ 03303 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE) 03304 { 03305 __HAL_USART_CLEAR_UDRFLAG(husart); 03306 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 03307 } 03308 03309 /* Rx process is completed, restore husart->State to Ready */ 03310 husart->State = HAL_USART_STATE_READY; 03311 03312 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03313 /* Call registered Rx Complete Callback */ 03314 husart->RxCpltCallback(husart); 03315 #else 03316 /* Call legacy weak Rx Complete Callback */ 03317 HAL_USART_RxCpltCallback(husart); 03318 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03319 } 03320 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) && 03321 (txftie != USART_CR3_TXFTIE) && 03322 (txdatacount == 0U)) 03323 { 03324 /* TxRx process is completed, restore husart->State to Ready */ 03325 husart->State = HAL_USART_STATE_READY; 03326 03327 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03328 /* Call registered Tx Rx Complete Callback */ 03329 husart->TxRxCpltCallback(husart); 03330 #else 03331 /* Call legacy weak Tx Rx Complete Callback */ 03332 HAL_USART_TxRxCpltCallback(husart); 03333 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03334 } 03335 else 03336 { 03337 /* Nothing to do */ 03338 } 03339 } 03340 else if ((state == HAL_USART_STATE_BUSY_RX) && 03341 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03342 { 03343 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03344 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03345 } 03346 else 03347 { 03348 /* Nothing to do */ 03349 } 03350 } 03351 } 03352 03353 /** 03354 * @brief Simplex receive an amount of data in non-blocking mode. 03355 * @note Function called under interruption only, once 03356 * interruptions have been enabled by HAL_USART_Receive_IT(). 03357 * @note ISR function executed when FIFO mode is disabled and when the 03358 * data word length is 9 bits long. 03359 * @param husart USART handle 03360 * @retval None 03361 */ 03362 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart) 03363 { 03364 const HAL_USART_StateTypeDef state = husart->State; 03365 uint16_t txdatacount; 03366 uint16_t *tmp; 03367 uint16_t uhMask = husart->Mask; 03368 uint32_t txftie; 03369 03370 if ((state == HAL_USART_STATE_BUSY_RX) || 03371 (state == HAL_USART_STATE_BUSY_TX_RX)) 03372 { 03373 tmp = (uint16_t *) husart->pRxBuffPtr; 03374 *tmp = (uint16_t)(husart->Instance->RDR & uhMask); 03375 husart->pRxBuffPtr += 2U; 03376 husart->RxXferCount--; 03377 03378 if (husart->RxXferCount == 0U) 03379 { 03380 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/ 03381 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)); 03382 03383 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */ 03384 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE); 03385 03386 /* Clear RxISR function pointer */ 03387 husart->RxISR = NULL; 03388 03389 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */ 03390 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE); 03391 txdatacount = husart->TxXferCount; 03392 03393 if (state == HAL_USART_STATE_BUSY_RX) 03394 { 03395 /* Clear SPI slave underrun flag and discard transmit data */ 03396 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE) 03397 { 03398 __HAL_USART_CLEAR_UDRFLAG(husart); 03399 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 03400 } 03401 03402 /* Rx process is completed, restore husart->State to Ready */ 03403 husart->State = HAL_USART_STATE_READY; 03404 03405 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03406 /* Call registered Rx Complete Callback */ 03407 husart->RxCpltCallback(husart); 03408 #else 03409 /* Call legacy weak Rx Complete Callback */ 03410 HAL_USART_RxCpltCallback(husart); 03411 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03412 } 03413 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) && 03414 (txftie != USART_CR3_TXFTIE) && 03415 (txdatacount == 0U)) 03416 { 03417 /* TxRx process is completed, restore husart->State to Ready */ 03418 husart->State = HAL_USART_STATE_READY; 03419 03420 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03421 /* Call registered Tx Rx Complete Callback */ 03422 husart->TxRxCpltCallback(husart); 03423 #else 03424 /* Call legacy weak Tx Rx Complete Callback */ 03425 HAL_USART_TxRxCpltCallback(husart); 03426 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03427 } 03428 else 03429 { 03430 /* Nothing to do */ 03431 } 03432 } 03433 else if ((state == HAL_USART_STATE_BUSY_RX) && 03434 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03435 { 03436 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03437 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03438 } 03439 else 03440 { 03441 /* Nothing to do */ 03442 } 03443 } 03444 } 03445 03446 /** 03447 * @brief Simplex receive an amount of data in non-blocking mode. 03448 * @note Function called under interruption only, once 03449 * interruptions have been enabled by HAL_USART_Receive_IT(). 03450 * @note ISR function executed when FIFO mode is enabled and when the 03451 * data word length is less than 9 bits long. 03452 * @param husart USART handle 03453 * @retval None 03454 */ 03455 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart) 03456 { 03457 HAL_USART_StateTypeDef state = husart->State; 03458 uint16_t txdatacount; 03459 uint16_t rxdatacount; 03460 uint16_t uhMask = husart->Mask; 03461 uint16_t nb_rx_data; 03462 uint32_t txftie; 03463 03464 /* Check that a Rx process is ongoing */ 03465 if ((state == HAL_USART_STATE_BUSY_RX) || 03466 (state == HAL_USART_STATE_BUSY_TX_RX)) 03467 { 03468 for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--) 03469 { 03470 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET) 03471 { 03472 *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU)); 03473 husart->pRxBuffPtr++; 03474 husart->RxXferCount--; 03475 03476 if (husart->RxXferCount == 0U) 03477 { 03478 /* Disable the USART Parity Error Interrupt */ 03479 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); 03480 03481 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) 03482 and RX FIFO Threshold interrupt */ 03483 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 03484 03485 /* Clear RxISR function pointer */ 03486 husart->RxISR = NULL; 03487 03488 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */ 03489 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE); 03490 txdatacount = husart->TxXferCount; 03491 03492 if (state == HAL_USART_STATE_BUSY_RX) 03493 { 03494 /* Clear SPI slave underrun flag and discard transmit data */ 03495 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE) 03496 { 03497 __HAL_USART_CLEAR_UDRFLAG(husart); 03498 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 03499 } 03500 03501 /* Rx process is completed, restore husart->State to Ready */ 03502 husart->State = HAL_USART_STATE_READY; 03503 state = HAL_USART_STATE_READY; 03504 03505 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03506 /* Call registered Rx Complete Callback */ 03507 husart->RxCpltCallback(husart); 03508 #else 03509 /* Call legacy weak Rx Complete Callback */ 03510 HAL_USART_RxCpltCallback(husart); 03511 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03512 } 03513 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) && 03514 (txftie != USART_CR3_TXFTIE) && 03515 (txdatacount == 0U)) 03516 { 03517 /* TxRx process is completed, restore husart->State to Ready */ 03518 husart->State = HAL_USART_STATE_READY; 03519 state = HAL_USART_STATE_READY; 03520 03521 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03522 /* Call registered Tx Rx Complete Callback */ 03523 husart->TxRxCpltCallback(husart); 03524 #else 03525 /* Call legacy weak Tx Rx Complete Callback */ 03526 HAL_USART_TxRxCpltCallback(husart); 03527 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03528 } 03529 else 03530 { 03531 /* Nothing to do */ 03532 } 03533 } 03534 else if ((state == HAL_USART_STATE_BUSY_RX) && 03535 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03536 { 03537 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03538 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03539 } 03540 else 03541 { 03542 /* Nothing to do */ 03543 } 03544 } 03545 } 03546 03547 /* When remaining number of bytes to receive is less than the RX FIFO 03548 threshold, next incoming frames are processed as if FIFO mode was 03549 disabled (i.e. one interrupt per received frame). 03550 */ 03551 rxdatacount = husart->RxXferCount; 03552 if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess)) 03553 { 03554 /* Disable the USART RXFT interrupt*/ 03555 CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE); 03556 03557 /* Update the RxISR function pointer */ 03558 husart->RxISR = USART_RxISR_8BIT; 03559 03560 /* Enable the USART Data Register Not Empty interrupt */ 03561 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03562 03563 if ((husart->TxXferCount == 0U) && 03564 (state == HAL_USART_STATE_BUSY_TX_RX) && 03565 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03566 { 03567 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03568 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03569 } 03570 } 03571 } 03572 else 03573 { 03574 /* Clear RXNE interrupt flag */ 03575 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 03576 } 03577 } 03578 03579 /** 03580 * @brief Simplex receive an amount of data in non-blocking mode. 03581 * @note Function called under interruption only, once 03582 * interruptions have been enabled by HAL_USART_Receive_IT(). 03583 * @note ISR function executed when FIFO mode is enabled and when the 03584 * data word length is 9 bits long. 03585 * @param husart USART handle 03586 * @retval None 03587 */ 03588 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart) 03589 { 03590 HAL_USART_StateTypeDef state = husart->State; 03591 uint16_t txdatacount; 03592 uint16_t rxdatacount; 03593 uint16_t *tmp; 03594 uint16_t uhMask = husart->Mask; 03595 uint16_t nb_rx_data; 03596 uint32_t txftie; 03597 03598 /* Check that a Tx process is ongoing */ 03599 if ((state == HAL_USART_STATE_BUSY_RX) || 03600 (state == HAL_USART_STATE_BUSY_TX_RX)) 03601 { 03602 for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--) 03603 { 03604 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET) 03605 { 03606 tmp = (uint16_t *) husart->pRxBuffPtr; 03607 *tmp = (uint16_t)(husart->Instance->RDR & uhMask); 03608 husart->pRxBuffPtr += 2U; 03609 husart->RxXferCount--; 03610 03611 if (husart->RxXferCount == 0U) 03612 { 03613 /* Disable the USART Parity Error Interrupt */ 03614 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE); 03615 03616 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) 03617 and RX FIFO Threshold interrupt */ 03618 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE)); 03619 03620 /* Clear RxISR function pointer */ 03621 husart->RxISR = NULL; 03622 03623 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */ 03624 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE); 03625 txdatacount = husart->TxXferCount; 03626 03627 if (state == HAL_USART_STATE_BUSY_RX) 03628 { 03629 /* Clear SPI slave underrun flag and discard transmit data */ 03630 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE) 03631 { 03632 __HAL_USART_CLEAR_UDRFLAG(husart); 03633 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST); 03634 } 03635 03636 /* Rx process is completed, restore husart->State to Ready */ 03637 husart->State = HAL_USART_STATE_READY; 03638 state = HAL_USART_STATE_READY; 03639 03640 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03641 /* Call registered Rx Complete Callback */ 03642 husart->RxCpltCallback(husart); 03643 #else 03644 /* Call legacy weak Rx Complete Callback */ 03645 HAL_USART_RxCpltCallback(husart); 03646 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03647 } 03648 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) && 03649 (txftie != USART_CR3_TXFTIE) && 03650 (txdatacount == 0U)) 03651 { 03652 /* TxRx process is completed, restore husart->State to Ready */ 03653 husart->State = HAL_USART_STATE_READY; 03654 state = HAL_USART_STATE_READY; 03655 03656 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 03657 /* Call registered Tx Rx Complete Callback */ 03658 husart->TxRxCpltCallback(husart); 03659 #else 03660 /* Call legacy weak Tx Rx Complete Callback */ 03661 HAL_USART_TxRxCpltCallback(husart); 03662 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 03663 } 03664 else 03665 { 03666 /* Nothing to do */ 03667 } 03668 } 03669 else if ((state == HAL_USART_STATE_BUSY_RX) && 03670 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03671 { 03672 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03673 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03674 } 03675 else 03676 { 03677 /* Nothing to do */ 03678 } 03679 } 03680 } 03681 03682 /* When remaining number of bytes to receive is less than the RX FIFO 03683 threshold, next incoming frames are processed as if FIFO mode was 03684 disabled (i.e. one interrupt per received frame). 03685 */ 03686 rxdatacount = husart->RxXferCount; 03687 if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess)) 03688 { 03689 /* Disable the USART RXFT interrupt*/ 03690 CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE); 03691 03692 /* Update the RxISR function pointer */ 03693 husart->RxISR = USART_RxISR_16BIT; 03694 03695 /* Enable the USART Data Register Not Empty interrupt */ 03696 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE); 03697 03698 if ((husart->TxXferCount == 0U) && 03699 (state == HAL_USART_STATE_BUSY_TX_RX) && 03700 (husart->SlaveMode == USART_SLAVEMODE_DISABLE)) 03701 { 03702 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */ 03703 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF); 03704 } 03705 } 03706 } 03707 else 03708 { 03709 /* Clear RXNE interrupt flag */ 03710 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST); 03711 } 03712 } 03713 03714 /** 03715 * @} 03716 */ 03717 03718 #endif /* HAL_USART_MODULE_ENABLED */ 03719 /** 03720 * @} 03721 */ 03722 03723 /** 03724 * @} 03725 */ 03726