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