STM32F103xB HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_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 channel. 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 channel. 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 channel. 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 @ref HAL_UART_RegisterCallback() to register a user callback. 00076 Function @ref 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 @ref HAL_UART_UnRegisterCallback() to reset a callback to the default 00092 weak (surcharged) function. 00093 @ref 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 @ref HAL_UART_RegisterRxEventCallback() , @ref HAL_UART_UnRegisterRxEventCallback(). 00110 00111 [..] 00112 By default, after the @ref 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 @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback(). 00115 Exception done for MspInit and MspDeInit functions that are respectively 00116 reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init() 00117 and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand). 00118 If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref 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 @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit() 00128 or @ref 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 "stm32f1xx_hal.h" 00258 00259 /** @addtogroup STM32F1xx_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, uint32_t Tickstart, uint32_t Timeout); 00303 static void UART_SetConfig(UART_HandleTypeDef *huart); 00304 00305 /** 00306 * @} 00307 */ 00308 00309 /* Exported functions ---------------------------------------------------------*/ 00310 /** @defgroup UART_Exported_Functions UART Exported Functions 00311 * @{ 00312 */ 00313 00314 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions 00315 * @brief Initialization and Configuration functions 00316 * 00317 @verbatim 00318 =============================================================================== 00319 ##### Initialization and Configuration functions ##### 00320 =============================================================================== 00321 [..] 00322 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy 00323 in asynchronous mode. 00324 (+) For the asynchronous mode only these parameters can be configured: 00325 (++) Baud Rate 00326 (++) Word Length 00327 (++) Stop Bit 00328 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00329 in the data register is transmitted but is changed by the parity bit. 00330 Depending on the frame length defined by the M bit (8-bits or 9-bits), 00331 please refer to Reference manual for possible UART frame formats. 00332 (++) Hardware flow control 00333 (++) Receiver/transmitter modes 00334 (++) Over Sampling Method 00335 [..] 00336 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs 00337 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor configuration 00338 procedures (details for the procedures are available in reference manuals 00339 (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)). 00340 00341 @endverbatim 00342 * @{ 00343 */ 00344 00345 /** 00346 * @brief Initializes the UART mode according to the specified parameters in 00347 * the UART_InitTypeDef and create the associated handle. 00348 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00349 * the configuration information for the specified UART module. 00350 * @retval HAL status 00351 */ 00352 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) 00353 { 00354 /* Check the UART handle allocation */ 00355 if (huart == NULL) 00356 { 00357 return HAL_ERROR; 00358 } 00359 00360 /* Check the parameters */ 00361 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) 00362 { 00363 /* The hardware flow control is available only for USART1, USART2 and USART3 */ 00364 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); 00365 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); 00366 } 00367 else 00368 { 00369 assert_param(IS_UART_INSTANCE(huart->Instance)); 00370 } 00371 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00372 #if defined(USART_CR1_OVER8) 00373 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00374 #endif /* USART_CR1_OVER8 */ 00375 00376 if (huart->gState == HAL_UART_STATE_RESET) 00377 { 00378 /* Allocate lock resource and initialize it */ 00379 huart->Lock = HAL_UNLOCKED; 00380 00381 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00382 UART_InitCallbacksToDefault(huart); 00383 00384 if (huart->MspInitCallback == NULL) 00385 { 00386 huart->MspInitCallback = HAL_UART_MspInit; 00387 } 00388 00389 /* Init the low level hardware */ 00390 huart->MspInitCallback(huart); 00391 #else 00392 /* Init the low level hardware : GPIO, CLOCK */ 00393 HAL_UART_MspInit(huart); 00394 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00395 } 00396 00397 huart->gState = HAL_UART_STATE_BUSY; 00398 00399 /* Disable the peripheral */ 00400 __HAL_UART_DISABLE(huart); 00401 00402 /* Set the UART Communication parameters */ 00403 UART_SetConfig(huart); 00404 00405 /* In asynchronous mode, the following bits must be kept cleared: 00406 - LINEN and CLKEN bits in the USART_CR2 register, 00407 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ 00408 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00409 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00410 00411 /* Enable the peripheral */ 00412 __HAL_UART_ENABLE(huart); 00413 00414 /* Initialize the UART state */ 00415 huart->ErrorCode = HAL_UART_ERROR_NONE; 00416 huart->gState = HAL_UART_STATE_READY; 00417 huart->RxState = HAL_UART_STATE_READY; 00418 00419 return HAL_OK; 00420 } 00421 00422 /** 00423 * @brief Initializes the half-duplex mode according to the specified 00424 * parameters in the UART_InitTypeDef and create the associated handle. 00425 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00426 * the configuration information for the specified UART module. 00427 * @retval HAL status 00428 */ 00429 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) 00430 { 00431 /* Check the UART handle allocation */ 00432 if (huart == NULL) 00433 { 00434 return HAL_ERROR; 00435 } 00436 00437 /* Check the parameters */ 00438 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance)); 00439 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00440 #if defined(USART_CR1_OVER8) 00441 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00442 #endif /* USART_CR1_OVER8 */ 00443 00444 if (huart->gState == HAL_UART_STATE_RESET) 00445 { 00446 /* Allocate lock resource and initialize it */ 00447 huart->Lock = HAL_UNLOCKED; 00448 00449 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00450 UART_InitCallbacksToDefault(huart); 00451 00452 if (huart->MspInitCallback == NULL) 00453 { 00454 huart->MspInitCallback = HAL_UART_MspInit; 00455 } 00456 00457 /* Init the low level hardware */ 00458 huart->MspInitCallback(huart); 00459 #else 00460 /* Init the low level hardware : GPIO, CLOCK */ 00461 HAL_UART_MspInit(huart); 00462 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00463 } 00464 00465 huart->gState = HAL_UART_STATE_BUSY; 00466 00467 /* Disable the peripheral */ 00468 __HAL_UART_DISABLE(huart); 00469 00470 /* Set the UART Communication parameters */ 00471 UART_SetConfig(huart); 00472 00473 /* In half-duplex mode, the following bits must be kept cleared: 00474 - LINEN and CLKEN bits in the USART_CR2 register, 00475 - SCEN and IREN bits in the USART_CR3 register.*/ 00476 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00477 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN)); 00478 00479 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ 00480 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL); 00481 00482 /* Enable the peripheral */ 00483 __HAL_UART_ENABLE(huart); 00484 00485 /* Initialize the UART state*/ 00486 huart->ErrorCode = HAL_UART_ERROR_NONE; 00487 huart->gState = HAL_UART_STATE_READY; 00488 huart->RxState = HAL_UART_STATE_READY; 00489 00490 return HAL_OK; 00491 } 00492 00493 /** 00494 * @brief Initializes the LIN mode according to the specified 00495 * parameters in the UART_InitTypeDef and create the associated handle. 00496 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00497 * the configuration information for the specified UART module. 00498 * @param BreakDetectLength Specifies the LIN break detection length. 00499 * This parameter can be one of the following values: 00500 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection 00501 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection 00502 * @retval HAL status 00503 */ 00504 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength) 00505 { 00506 /* Check the UART handle allocation */ 00507 if (huart == NULL) 00508 { 00509 return HAL_ERROR; 00510 } 00511 00512 /* Check the LIN UART instance */ 00513 assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); 00514 00515 /* Check the Break detection length parameter */ 00516 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength)); 00517 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength)); 00518 #if defined(USART_CR1_OVER8) 00519 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling)); 00520 #endif /* USART_CR1_OVER8 */ 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 #if defined(USART_CR1_OVER8) 00603 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00604 #endif /* USART_CR1_OVER8 */ 00605 00606 if (huart->gState == HAL_UART_STATE_RESET) 00607 { 00608 /* Allocate lock resource and initialize it */ 00609 huart->Lock = HAL_UNLOCKED; 00610 00611 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00612 UART_InitCallbacksToDefault(huart); 00613 00614 if (huart->MspInitCallback == NULL) 00615 { 00616 huart->MspInitCallback = HAL_UART_MspInit; 00617 } 00618 00619 /* Init the low level hardware */ 00620 huart->MspInitCallback(huart); 00621 #else 00622 /* Init the low level hardware : GPIO, CLOCK */ 00623 HAL_UART_MspInit(huart); 00624 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00625 } 00626 00627 huart->gState = HAL_UART_STATE_BUSY; 00628 00629 /* Disable the peripheral */ 00630 __HAL_UART_DISABLE(huart); 00631 00632 /* Set the UART Communication parameters */ 00633 UART_SetConfig(huart); 00634 00635 /* In Multi-Processor mode, the following bits must be kept cleared: 00636 - LINEN and CLKEN bits in the USART_CR2 register, 00637 - SCEN, HDSEL and IREN bits in the USART_CR3 register */ 00638 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00639 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00640 00641 /* Set the USART address node */ 00642 CLEAR_BIT(huart->Instance->CR2, USART_CR2_ADD); 00643 SET_BIT(huart->Instance->CR2, Address); 00644 00645 /* Set the wake up method by setting the WAKE bit in the CR1 register */ 00646 CLEAR_BIT(huart->Instance->CR1, USART_CR1_WAKE); 00647 SET_BIT(huart->Instance->CR1, WakeUpMethod); 00648 00649 /* Enable the peripheral */ 00650 __HAL_UART_ENABLE(huart); 00651 00652 /* Initialize the UART state */ 00653 huart->ErrorCode = HAL_UART_ERROR_NONE; 00654 huart->gState = HAL_UART_STATE_READY; 00655 huart->RxState = HAL_UART_STATE_READY; 00656 00657 return HAL_OK; 00658 } 00659 00660 /** 00661 * @brief DeInitializes the UART peripheral. 00662 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00663 * the configuration information for the specified UART module. 00664 * @retval HAL status 00665 */ 00666 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) 00667 { 00668 /* Check the UART handle allocation */ 00669 if (huart == NULL) 00670 { 00671 return HAL_ERROR; 00672 } 00673 00674 /* Check the parameters */ 00675 assert_param(IS_UART_INSTANCE(huart->Instance)); 00676 00677 huart->gState = HAL_UART_STATE_BUSY; 00678 00679 /* Disable the Peripheral */ 00680 __HAL_UART_DISABLE(huart); 00681 00682 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00683 if (huart->MspDeInitCallback == NULL) 00684 { 00685 huart->MspDeInitCallback = HAL_UART_MspDeInit; 00686 } 00687 /* DeInit the low level hardware */ 00688 huart->MspDeInitCallback(huart); 00689 #else 00690 /* DeInit the low level hardware */ 00691 HAL_UART_MspDeInit(huart); 00692 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00693 00694 huart->ErrorCode = HAL_UART_ERROR_NONE; 00695 huart->gState = HAL_UART_STATE_RESET; 00696 huart->RxState = HAL_UART_STATE_RESET; 00697 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 00698 00699 /* Process Unlock */ 00700 __HAL_UNLOCK(huart); 00701 00702 return HAL_OK; 00703 } 00704 00705 /** 00706 * @brief UART MSP Init. 00707 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00708 * the configuration information for the specified UART module. 00709 * @retval None 00710 */ 00711 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) 00712 { 00713 /* Prevent unused argument(s) compilation warning */ 00714 UNUSED(huart); 00715 /* NOTE: This function should not be modified, when the callback is needed, 00716 the HAL_UART_MspInit could be implemented in the user file 00717 */ 00718 } 00719 00720 /** 00721 * @brief UART MSP DeInit. 00722 * @param huart Pointer to a UART_HandleTypeDef structure that contains 00723 * the configuration information for the specified UART module. 00724 * @retval None 00725 */ 00726 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) 00727 { 00728 /* Prevent unused argument(s) compilation warning */ 00729 UNUSED(huart); 00730 /* NOTE: This function should not be modified, when the callback is needed, 00731 the HAL_UART_MspDeInit could be implemented in the user file 00732 */ 00733 } 00734 00735 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00736 /** 00737 * @brief Register a User UART Callback 00738 * To be used instead of the weak predefined callback 00739 * @param huart uart handle 00740 * @param CallbackID ID of the callback to be registered 00741 * This parameter can be one of the following values: 00742 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00743 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00744 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00745 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00746 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID 00747 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00748 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00749 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00750 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID 00751 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID 00752 * @param pCallback pointer to the Callback function 00753 * @retval HAL status 00754 */ 00755 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID, pUART_CallbackTypeDef pCallback) 00756 { 00757 HAL_StatusTypeDef status = HAL_OK; 00758 00759 if (pCallback == NULL) 00760 { 00761 /* Update the error code */ 00762 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00763 00764 return HAL_ERROR; 00765 } 00766 /* Process locked */ 00767 __HAL_LOCK(huart); 00768 00769 if (huart->gState == HAL_UART_STATE_READY) 00770 { 00771 switch (CallbackID) 00772 { 00773 case HAL_UART_TX_HALFCOMPLETE_CB_ID : 00774 huart->TxHalfCpltCallback = pCallback; 00775 break; 00776 00777 case HAL_UART_TX_COMPLETE_CB_ID : 00778 huart->TxCpltCallback = pCallback; 00779 break; 00780 00781 case HAL_UART_RX_HALFCOMPLETE_CB_ID : 00782 huart->RxHalfCpltCallback = pCallback; 00783 break; 00784 00785 case HAL_UART_RX_COMPLETE_CB_ID : 00786 huart->RxCpltCallback = pCallback; 00787 break; 00788 00789 case HAL_UART_ERROR_CB_ID : 00790 huart->ErrorCallback = pCallback; 00791 break; 00792 00793 case HAL_UART_ABORT_COMPLETE_CB_ID : 00794 huart->AbortCpltCallback = pCallback; 00795 break; 00796 00797 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID : 00798 huart->AbortTransmitCpltCallback = pCallback; 00799 break; 00800 00801 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID : 00802 huart->AbortReceiveCpltCallback = pCallback; 00803 break; 00804 00805 case HAL_UART_MSPINIT_CB_ID : 00806 huart->MspInitCallback = pCallback; 00807 break; 00808 00809 case HAL_UART_MSPDEINIT_CB_ID : 00810 huart->MspDeInitCallback = pCallback; 00811 break; 00812 00813 default : 00814 /* Update the error code */ 00815 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00816 00817 /* Return error status */ 00818 status = HAL_ERROR; 00819 break; 00820 } 00821 } 00822 else if (huart->gState == HAL_UART_STATE_RESET) 00823 { 00824 switch (CallbackID) 00825 { 00826 case HAL_UART_MSPINIT_CB_ID : 00827 huart->MspInitCallback = pCallback; 00828 break; 00829 00830 case HAL_UART_MSPDEINIT_CB_ID : 00831 huart->MspDeInitCallback = pCallback; 00832 break; 00833 00834 default : 00835 /* Update the error code */ 00836 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00837 00838 /* Return error status */ 00839 status = HAL_ERROR; 00840 break; 00841 } 00842 } 00843 else 00844 { 00845 /* Update the error code */ 00846 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00847 00848 /* Return error status */ 00849 status = HAL_ERROR; 00850 } 00851 00852 /* Release Lock */ 00853 __HAL_UNLOCK(huart); 00854 00855 return status; 00856 } 00857 00858 /** 00859 * @brief Unregister an UART Callback 00860 * UART callaback is redirected to the weak predefined callback 00861 * @param huart uart handle 00862 * @param CallbackID ID of the callback to be unregistered 00863 * This parameter can be one of the following values: 00864 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID 00865 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID 00866 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID 00867 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID 00868 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID 00869 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID 00870 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID 00871 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID 00872 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID 00873 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID 00874 * @retval HAL status 00875 */ 00876 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID) 00877 { 00878 HAL_StatusTypeDef status = HAL_OK; 00879 00880 /* Process locked */ 00881 __HAL_LOCK(huart); 00882 00883 if (HAL_UART_STATE_READY == huart->gState) 00884 { 00885 switch (CallbackID) 00886 { 00887 case HAL_UART_TX_HALFCOMPLETE_CB_ID : 00888 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 00889 break; 00890 00891 case HAL_UART_TX_COMPLETE_CB_ID : 00892 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 00893 break; 00894 00895 case HAL_UART_RX_HALFCOMPLETE_CB_ID : 00896 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 00897 break; 00898 00899 case HAL_UART_RX_COMPLETE_CB_ID : 00900 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 00901 break; 00902 00903 case HAL_UART_ERROR_CB_ID : 00904 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */ 00905 break; 00906 00907 case HAL_UART_ABORT_COMPLETE_CB_ID : 00908 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 00909 break; 00910 00911 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID : 00912 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */ 00913 break; 00914 00915 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID : 00916 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */ 00917 break; 00918 00919 case HAL_UART_MSPINIT_CB_ID : 00920 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */ 00921 break; 00922 00923 case HAL_UART_MSPDEINIT_CB_ID : 00924 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */ 00925 break; 00926 00927 default : 00928 /* Update the error code */ 00929 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00930 00931 /* Return error status */ 00932 status = HAL_ERROR; 00933 break; 00934 } 00935 } 00936 else if (HAL_UART_STATE_RESET == huart->gState) 00937 { 00938 switch (CallbackID) 00939 { 00940 case HAL_UART_MSPINIT_CB_ID : 00941 huart->MspInitCallback = HAL_UART_MspInit; 00942 break; 00943 00944 case HAL_UART_MSPDEINIT_CB_ID : 00945 huart->MspDeInitCallback = HAL_UART_MspDeInit; 00946 break; 00947 00948 default : 00949 /* Update the error code */ 00950 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00951 00952 /* Return error status */ 00953 status = HAL_ERROR; 00954 break; 00955 } 00956 } 00957 else 00958 { 00959 /* Update the error code */ 00960 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00961 00962 /* Return error status */ 00963 status = HAL_ERROR; 00964 } 00965 00966 /* Release Lock */ 00967 __HAL_UNLOCK(huart); 00968 00969 return status; 00970 } 00971 00972 /** 00973 * @brief Register a User UART Rx Event Callback 00974 * To be used instead of the weak predefined callback 00975 * @param huart Uart handle 00976 * @param pCallback Pointer to the Rx Event Callback function 00977 * @retval HAL status 00978 */ 00979 HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback) 00980 { 00981 HAL_StatusTypeDef status = HAL_OK; 00982 00983 if (pCallback == NULL) 00984 { 00985 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 00986 00987 return HAL_ERROR; 00988 } 00989 00990 /* Process locked */ 00991 __HAL_LOCK(huart); 00992 00993 if (huart->gState == HAL_UART_STATE_READY) 00994 { 00995 huart->RxEventCallback = pCallback; 00996 } 00997 else 00998 { 00999 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 01000 01001 status = HAL_ERROR; 01002 } 01003 01004 /* Release Lock */ 01005 __HAL_UNLOCK(huart); 01006 01007 return status; 01008 } 01009 01010 /** 01011 * @brief UnRegister the UART Rx Event Callback 01012 * UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback 01013 * @param huart Uart handle 01014 * @retval HAL status 01015 */ 01016 HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart) 01017 { 01018 HAL_StatusTypeDef status = HAL_OK; 01019 01020 /* Process locked */ 01021 __HAL_LOCK(huart); 01022 01023 if (huart->gState == HAL_UART_STATE_READY) 01024 { 01025 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback */ 01026 } 01027 else 01028 { 01029 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK; 01030 01031 status = HAL_ERROR; 01032 } 01033 01034 /* Release Lock */ 01035 __HAL_UNLOCK(huart); 01036 return status; 01037 } 01038 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 01039 01040 /** 01041 * @} 01042 */ 01043 01044 /** @defgroup UART_Exported_Functions_Group2 IO operation functions 01045 * @brief UART Transmit and Receive functions 01046 * 01047 @verbatim 01048 =============================================================================== 01049 ##### IO operation functions ##### 01050 =============================================================================== 01051 This subsection provides a set of functions allowing to manage the UART asynchronous 01052 and Half duplex data transfers. 01053 01054 (#) There are two modes of transfer: 01055 (+) Blocking mode: The communication is performed in polling mode. 01056 The HAL status of all data processing is returned by the same function 01057 after finishing transfer. 01058 (+) Non-Blocking mode: The communication is performed using Interrupts 01059 or DMA, these API's return the HAL status. 01060 The end of the data processing will be indicated through the 01061 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when 01062 using DMA mode. 01063 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks 01064 will be executed respectively at the end of the transmit or receive process 01065 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected. 01066 01067 (#) Blocking mode API's are : 01068 (+) HAL_UART_Transmit() 01069 (+) HAL_UART_Receive() 01070 01071 (#) Non-Blocking mode API's with Interrupt are : 01072 (+) HAL_UART_Transmit_IT() 01073 (+) HAL_UART_Receive_IT() 01074 (+) HAL_UART_IRQHandler() 01075 01076 (#) Non-Blocking mode API's with DMA are : 01077 (+) HAL_UART_Transmit_DMA() 01078 (+) HAL_UART_Receive_DMA() 01079 (+) HAL_UART_DMAPause() 01080 (+) HAL_UART_DMAResume() 01081 (+) HAL_UART_DMAStop() 01082 01083 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode: 01084 (+) HAL_UART_TxHalfCpltCallback() 01085 (+) HAL_UART_TxCpltCallback() 01086 (+) HAL_UART_RxHalfCpltCallback() 01087 (+) HAL_UART_RxCpltCallback() 01088 (+) HAL_UART_ErrorCallback() 01089 01090 (#) Non-Blocking mode transfers could be aborted using Abort API's : 01091 (+) HAL_UART_Abort() 01092 (+) HAL_UART_AbortTransmit() 01093 (+) HAL_UART_AbortReceive() 01094 (+) HAL_UART_Abort_IT() 01095 (+) HAL_UART_AbortTransmit_IT() 01096 (+) HAL_UART_AbortReceive_IT() 01097 01098 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided: 01099 (+) HAL_UART_AbortCpltCallback() 01100 (+) HAL_UART_AbortTransmitCpltCallback() 01101 (+) HAL_UART_AbortReceiveCpltCallback() 01102 01103 (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced reception services: 01104 (+) HAL_UARTEx_RxEventCallback() 01105 01106 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories. 01107 Errors are handled as follows : 01108 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is 01109 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception . 01110 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type, 01111 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side. 01112 If user wants to abort it, Abort services should be called by user. 01113 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted. 01114 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode. 01115 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed. 01116 01117 -@- In the Half duplex communication, it is forbidden to run the transmit 01118 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful. 01119 01120 @endverbatim 01121 * @{ 01122 */ 01123 01124 /** 01125 * @brief Sends an amount of data in blocking mode. 01126 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01127 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01128 * of u16 provided through pData. 01129 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01130 * the configuration information for the specified UART module. 01131 * @param pData Pointer to data buffer (u8 or u16 data elements). 01132 * @param Size Amount of data elements (u8 or u16) to be sent 01133 * @param Timeout Timeout duration 01134 * @retval HAL status 01135 */ 01136 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 01137 { 01138 uint8_t *pdata8bits; 01139 uint16_t *pdata16bits; 01140 uint32_t tickstart = 0U; 01141 01142 /* Check that a Tx process is not already ongoing */ 01143 if (huart->gState == HAL_UART_STATE_READY) 01144 { 01145 if ((pData == NULL) || (Size == 0U)) 01146 { 01147 return HAL_ERROR; 01148 } 01149 01150 /* Process Locked */ 01151 __HAL_LOCK(huart); 01152 01153 huart->ErrorCode = HAL_UART_ERROR_NONE; 01154 huart->gState = HAL_UART_STATE_BUSY_TX; 01155 01156 /* Init tickstart for timeout management */ 01157 tickstart = HAL_GetTick(); 01158 01159 huart->TxXferSize = Size; 01160 huart->TxXferCount = Size; 01161 01162 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */ 01163 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01164 { 01165 pdata8bits = NULL; 01166 pdata16bits = (uint16_t *) pData; 01167 } 01168 else 01169 { 01170 pdata8bits = pData; 01171 pdata16bits = NULL; 01172 } 01173 01174 /* Process Unlocked */ 01175 __HAL_UNLOCK(huart); 01176 01177 while (huart->TxXferCount > 0U) 01178 { 01179 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 01180 { 01181 return HAL_TIMEOUT; 01182 } 01183 if (pdata8bits == NULL) 01184 { 01185 huart->Instance->DR = (uint16_t)(*pdata16bits & 0x01FFU); 01186 pdata16bits++; 01187 } 01188 else 01189 { 01190 huart->Instance->DR = (uint8_t)(*pdata8bits & 0xFFU); 01191 pdata8bits++; 01192 } 01193 huart->TxXferCount--; 01194 } 01195 01196 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) 01197 { 01198 return HAL_TIMEOUT; 01199 } 01200 01201 /* At end of Tx process, restore huart->gState to Ready */ 01202 huart->gState = HAL_UART_STATE_READY; 01203 01204 return HAL_OK; 01205 } 01206 else 01207 { 01208 return HAL_BUSY; 01209 } 01210 } 01211 01212 /** 01213 * @brief Receives an amount of data in blocking mode. 01214 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01215 * the received data is handled as a set of u16. In this case, Size must indicate the number 01216 * of u16 available through pData. 01217 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01218 * the configuration information for the specified UART module. 01219 * @param pData Pointer to data buffer (u8 or u16 data elements). 01220 * @param Size Amount of data elements (u8 or u16) to be received. 01221 * @param Timeout Timeout duration 01222 * @retval HAL status 01223 */ 01224 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 01225 { 01226 uint8_t *pdata8bits; 01227 uint16_t *pdata16bits; 01228 uint32_t tickstart = 0U; 01229 01230 /* Check that a Rx process is not already ongoing */ 01231 if (huart->RxState == HAL_UART_STATE_READY) 01232 { 01233 if ((pData == NULL) || (Size == 0U)) 01234 { 01235 return HAL_ERROR; 01236 } 01237 01238 /* Process Locked */ 01239 __HAL_LOCK(huart); 01240 01241 huart->ErrorCode = HAL_UART_ERROR_NONE; 01242 huart->RxState = HAL_UART_STATE_BUSY_RX; 01243 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01244 01245 /* Init tickstart for timeout management */ 01246 tickstart = HAL_GetTick(); 01247 01248 huart->RxXferSize = Size; 01249 huart->RxXferCount = Size; 01250 01251 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */ 01252 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 01253 { 01254 pdata8bits = NULL; 01255 pdata16bits = (uint16_t *) pData; 01256 } 01257 else 01258 { 01259 pdata8bits = pData; 01260 pdata16bits = NULL; 01261 } 01262 01263 /* Process Unlocked */ 01264 __HAL_UNLOCK(huart); 01265 01266 /* Check the remain data to be received */ 01267 while (huart->RxXferCount > 0U) 01268 { 01269 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 01270 { 01271 return HAL_TIMEOUT; 01272 } 01273 if (pdata8bits == NULL) 01274 { 01275 *pdata16bits = (uint16_t)(huart->Instance->DR & 0x01FF); 01276 pdata16bits++; 01277 } 01278 else 01279 { 01280 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) || ((huart->Init.WordLength == UART_WORDLENGTH_8B) && (huart->Init.Parity == UART_PARITY_NONE))) 01281 { 01282 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 01283 } 01284 else 01285 { 01286 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 01287 } 01288 pdata8bits++; 01289 } 01290 huart->RxXferCount--; 01291 } 01292 01293 /* At end of Rx process, restore huart->RxState to Ready */ 01294 huart->RxState = HAL_UART_STATE_READY; 01295 01296 return HAL_OK; 01297 } 01298 else 01299 { 01300 return HAL_BUSY; 01301 } 01302 } 01303 01304 /** 01305 * @brief Sends an amount of data in non blocking mode. 01306 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01307 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01308 * of u16 provided through pData. 01309 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01310 * the configuration information for the specified UART module. 01311 * @param pData Pointer to data buffer (u8 or u16 data elements). 01312 * @param Size Amount of data elements (u8 or u16) to be sent 01313 * @retval HAL status 01314 */ 01315 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01316 { 01317 /* Check that a Tx process is not already ongoing */ 01318 if (huart->gState == HAL_UART_STATE_READY) 01319 { 01320 if ((pData == NULL) || (Size == 0U)) 01321 { 01322 return HAL_ERROR; 01323 } 01324 01325 /* Process Locked */ 01326 __HAL_LOCK(huart); 01327 01328 huart->pTxBuffPtr = pData; 01329 huart->TxXferSize = Size; 01330 huart->TxXferCount = Size; 01331 01332 huart->ErrorCode = HAL_UART_ERROR_NONE; 01333 huart->gState = HAL_UART_STATE_BUSY_TX; 01334 01335 /* Process Unlocked */ 01336 __HAL_UNLOCK(huart); 01337 01338 /* Enable the UART Transmit data register empty Interrupt */ 01339 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); 01340 01341 return HAL_OK; 01342 } 01343 else 01344 { 01345 return HAL_BUSY; 01346 } 01347 } 01348 01349 /** 01350 * @brief Receives an amount of data in non blocking mode. 01351 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01352 * the received data is handled as a set of u16. In this case, Size must indicate the number 01353 * of u16 available through pData. 01354 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01355 * the configuration information for the specified UART module. 01356 * @param pData Pointer to data buffer (u8 or u16 data elements). 01357 * @param Size Amount of data elements (u8 or u16) to be received. 01358 * @retval HAL status 01359 */ 01360 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01361 { 01362 /* Check that a Rx process is not already ongoing */ 01363 if (huart->RxState == HAL_UART_STATE_READY) 01364 { 01365 if ((pData == NULL) || (Size == 0U)) 01366 { 01367 return HAL_ERROR; 01368 } 01369 01370 /* Process Locked */ 01371 __HAL_LOCK(huart); 01372 01373 /* Set Reception type to Standard reception */ 01374 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01375 01376 return(UART_Start_Receive_IT(huart, pData, Size)); 01377 } 01378 else 01379 { 01380 return HAL_BUSY; 01381 } 01382 } 01383 01384 /** 01385 * @brief Sends an amount of data in DMA mode. 01386 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01387 * the sent data is handled as a set of u16. In this case, Size must indicate the number 01388 * of u16 provided through pData. 01389 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01390 * the configuration information for the specified UART module. 01391 * @param pData Pointer to data buffer (u8 or u16 data elements). 01392 * @param Size Amount of data elements (u8 or u16) to be sent 01393 * @retval HAL status 01394 */ 01395 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01396 { 01397 uint32_t *tmp; 01398 01399 /* Check that a Tx process is not already ongoing */ 01400 if (huart->gState == HAL_UART_STATE_READY) 01401 { 01402 if ((pData == NULL) || (Size == 0U)) 01403 { 01404 return HAL_ERROR; 01405 } 01406 01407 /* Process Locked */ 01408 __HAL_LOCK(huart); 01409 01410 huart->pTxBuffPtr = pData; 01411 huart->TxXferSize = Size; 01412 huart->TxXferCount = Size; 01413 01414 huart->ErrorCode = HAL_UART_ERROR_NONE; 01415 huart->gState = HAL_UART_STATE_BUSY_TX; 01416 01417 /* Set the UART DMA transfer complete callback */ 01418 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; 01419 01420 /* Set the UART DMA Half transfer complete callback */ 01421 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; 01422 01423 /* Set the DMA error callback */ 01424 huart->hdmatx->XferErrorCallback = UART_DMAError; 01425 01426 /* Set the DMA abort callback */ 01427 huart->hdmatx->XferAbortCallback = NULL; 01428 01429 /* Enable the UART transmit DMA channel */ 01430 tmp = (uint32_t *)&pData; 01431 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t *)tmp, (uint32_t)&huart->Instance->DR, Size); 01432 01433 /* Clear the TC flag in the SR register by writing 0 to it */ 01434 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); 01435 01436 /* Process Unlocked */ 01437 __HAL_UNLOCK(huart); 01438 01439 /* Enable the DMA transfer for transmit request by setting the DMAT bit 01440 in the UART CR3 register */ 01441 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01442 01443 return HAL_OK; 01444 } 01445 else 01446 { 01447 return HAL_BUSY; 01448 } 01449 } 01450 01451 /** 01452 * @brief Receives an amount of data in DMA mode. 01453 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01), 01454 * the received data is handled as a set of u16. In this case, Size must indicate the number 01455 * of u16 available through pData. 01456 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01457 * the configuration information for the specified UART module. 01458 * @param pData Pointer to data buffer (u8 or u16 data elements). 01459 * @param Size Amount of data elements (u8 or u16) to be received. 01460 * @note When the UART parity is enabled (PCE = 1) the received data contains the parity bit. 01461 * @retval HAL status 01462 */ 01463 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01464 { 01465 /* Check that a Rx process is not already ongoing */ 01466 if (huart->RxState == HAL_UART_STATE_READY) 01467 { 01468 if ((pData == NULL) || (Size == 0U)) 01469 { 01470 return HAL_ERROR; 01471 } 01472 01473 /* Process Locked */ 01474 __HAL_LOCK(huart); 01475 01476 /* Set Reception type to Standard reception */ 01477 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 01478 01479 return(UART_Start_Receive_DMA(huart, pData, Size)); 01480 } 01481 else 01482 { 01483 return HAL_BUSY; 01484 } 01485 } 01486 01487 /** 01488 * @brief Pauses the DMA Transfer. 01489 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01490 * the configuration information for the specified UART module. 01491 * @retval HAL status 01492 */ 01493 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) 01494 { 01495 uint32_t dmarequest = 0x00U; 01496 01497 /* Process Locked */ 01498 __HAL_LOCK(huart); 01499 01500 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 01501 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 01502 { 01503 /* Disable the UART DMA Tx request */ 01504 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01505 } 01506 01507 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 01508 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 01509 { 01510 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01511 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01512 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01513 01514 /* Disable the UART DMA Rx request */ 01515 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01516 } 01517 01518 /* Process Unlocked */ 01519 __HAL_UNLOCK(huart); 01520 01521 return HAL_OK; 01522 } 01523 01524 /** 01525 * @brief Resumes the DMA Transfer. 01526 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01527 * the configuration information for the specified UART module. 01528 * @retval HAL status 01529 */ 01530 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) 01531 { 01532 /* Process Locked */ 01533 __HAL_LOCK(huart); 01534 01535 if (huart->gState == HAL_UART_STATE_BUSY_TX) 01536 { 01537 /* Enable the UART DMA Tx request */ 01538 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01539 } 01540 01541 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 01542 { 01543 /* Clear the Overrun flag before resuming the Rx transfer*/ 01544 __HAL_UART_CLEAR_OREFLAG(huart); 01545 01546 /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01547 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01548 SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 01549 01550 /* Enable the UART DMA Rx request */ 01551 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01552 } 01553 01554 /* Process Unlocked */ 01555 __HAL_UNLOCK(huart); 01556 01557 return HAL_OK; 01558 } 01559 01560 /** 01561 * @brief Stops the DMA Transfer. 01562 * @param huart Pointer to a UART_HandleTypeDef structure that contains 01563 * the configuration information for the specified UART module. 01564 * @retval HAL status 01565 */ 01566 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) 01567 { 01568 uint32_t dmarequest = 0x00U; 01569 /* The Lock is not implemented on this API to allow the user application 01570 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback(): 01571 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated 01572 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() 01573 */ 01574 01575 /* Stop UART DMA Tx request if ongoing */ 01576 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 01577 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 01578 { 01579 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01580 01581 /* Abort the UART DMA Tx channel */ 01582 if (huart->hdmatx != NULL) 01583 { 01584 HAL_DMA_Abort(huart->hdmatx); 01585 } 01586 UART_EndTxTransfer(huart); 01587 } 01588 01589 /* Stop UART DMA Rx request if ongoing */ 01590 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 01591 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 01592 { 01593 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01594 01595 /* Abort the UART DMA Rx channel */ 01596 if (huart->hdmarx != NULL) 01597 { 01598 HAL_DMA_Abort(huart->hdmarx); 01599 } 01600 UART_EndRxTransfer(huart); 01601 } 01602 01603 return HAL_OK; 01604 } 01605 01606 /** 01607 * @brief Receive an amount of data in blocking mode till either the expected number of data is received or an IDLE event occurs. 01608 * @note HAL_OK is returned if reception is completed (expected number of data has been received) 01609 * or if reception is stopped after IDLE event (less than the expected number of data has been received) 01610 * In this case, RxLen output parameter indicates number of data available in reception buffer. 01611 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M = 01), 01612 * the received data is handled as a set of uint16_t. In this case, Size must indicate the number 01613 * of uint16_t available through pData. 01614 * @param huart UART handle. 01615 * @param pData Pointer to data buffer (uint8_t or uint16_t data elements). 01616 * @param Size Amount of data elements (uint8_t or uint16_t) to be received. 01617 * @param RxLen Number of data elements finally received (could be lower than Size, in case reception ends on IDLE event) 01618 * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence). 01619 * @retval HAL status 01620 */ 01621 HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen, 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 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 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 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); 01869 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01881 01882 /* Abort the UART DMA Tx channel: 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01906 01907 /* Abort the UART DMA Rx channel: 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01963 01964 /* Abort the UART DMA Tx channel : 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 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02009 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02021 02022 /* Abort the UART DMA Rx channel : 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 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); 02072 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 02116 02117 /* Abort the UART DMA Tx channel : 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02139 02140 /* Abort the UART DMA Rx channel : 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 02210 02211 /* Abort the UART DMA Tx channel : 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 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02282 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 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 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02294 02295 /* Abort the UART DMA Rx channel : 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) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET))) 02378 { 02379 /* UART parity error interrupt occurred ----------------------------------*/ 02380 if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET)) 02381 { 02382 huart->ErrorCode |= HAL_UART_ERROR_PE; 02383 } 02384 02385 /* UART noise error interrupt occurred -----------------------------------*/ 02386 if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) 02387 { 02388 huart->ErrorCode |= HAL_UART_ERROR_NE; 02389 } 02390 02391 /* UART frame error interrupt occurred -----------------------------------*/ 02392 if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) 02393 { 02394 huart->ErrorCode |= HAL_UART_ERROR_FE; 02395 } 02396 02397 /* UART Over-Run interrupt occurred --------------------------------------*/ 02398 if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET))) 02399 { 02400 huart->ErrorCode |= HAL_UART_ERROR_ORE; 02401 } 02402 02403 /* Call UART Error Call back function if need be --------------------------*/ 02404 if (huart->ErrorCode != HAL_UART_ERROR_NONE) 02405 { 02406 /* UART in mode Receiver -----------------------------------------------*/ 02407 if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) 02408 { 02409 UART_Receive_IT(huart); 02410 } 02411 02412 /* If Overrun error occurs, or if any error occurs in DMA mode reception, 02413 consider error as blocking */ 02414 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 02415 if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest) 02416 { 02417 /* Blocking error : transfer is aborted 02418 Set the UART state ready to be able to start again the process, 02419 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ 02420 UART_EndRxTransfer(huart); 02421 02422 /* Disable the UART DMA Rx request if enabled */ 02423 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 02424 { 02425 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02426 02427 /* Abort the UART DMA Rx channel */ 02428 if (huart->hdmarx != NULL) 02429 { 02430 /* Set the UART DMA Abort callback : 02431 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */ 02432 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError; 02433 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 02434 { 02435 /* Call Directly XferAbortCallback function in case of error */ 02436 huart->hdmarx->XferAbortCallback(huart->hdmarx); 02437 } 02438 } 02439 else 02440 { 02441 /* Call user error callback */ 02442 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02443 /*Call registered error callback*/ 02444 huart->ErrorCallback(huart); 02445 #else 02446 /*Call legacy weak error callback*/ 02447 HAL_UART_ErrorCallback(huart); 02448 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02449 } 02450 } 02451 else 02452 { 02453 /* Call user error callback */ 02454 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02455 /*Call registered error callback*/ 02456 huart->ErrorCallback(huart); 02457 #else 02458 /*Call legacy weak error callback*/ 02459 HAL_UART_ErrorCallback(huart); 02460 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02461 } 02462 } 02463 else 02464 { 02465 /* Non Blocking error : transfer could go on. 02466 Error is notified to user through user error callback */ 02467 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02468 /*Call registered error callback*/ 02469 huart->ErrorCallback(huart); 02470 #else 02471 /*Call legacy weak error callback*/ 02472 HAL_UART_ErrorCallback(huart); 02473 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02474 02475 huart->ErrorCode = HAL_UART_ERROR_NONE; 02476 } 02477 } 02478 return; 02479 } /* End if some error occurs */ 02480 02481 /* Check current reception Mode : 02482 If Reception till IDLE event has been selected : */ 02483 if ( (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 02484 &&((isrflags & USART_SR_IDLE) != 0U) 02485 &&((cr1its & USART_SR_IDLE) != 0U)) 02486 { 02487 __HAL_UART_CLEAR_IDLEFLAG(huart); 02488 02489 /* Check if DMA mode is enabled in UART */ 02490 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 02491 { 02492 /* DMA mode enabled */ 02493 /* Check received length : If all expected data are received, do nothing, 02494 (DMA cplt callback will be called). 02495 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */ 02496 uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx); 02497 if ( (nb_remaining_rx_data > 0U) 02498 &&(nb_remaining_rx_data < huart->RxXferSize)) 02499 { 02500 /* Reception is not complete */ 02501 huart->RxXferCount = nb_remaining_rx_data; 02502 02503 /* In Normal mode, end DMA xfer and HAL UART Rx process*/ 02504 if (huart->hdmarx->Init.Mode != DMA_CIRCULAR) 02505 { 02506 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */ 02507 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 02508 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02509 02510 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit 02511 in the UART CR3 register */ 02512 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02513 02514 /* At end of Rx process, restore huart->RxState to Ready */ 02515 huart->RxState = HAL_UART_STATE_READY; 02516 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02517 02518 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 02519 02520 /* Last bytes received, so no need as the abort is immediate */ 02521 (void)HAL_DMA_Abort(huart->hdmarx); 02522 } 02523 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02524 /*Call registered Rx Event callback*/ 02525 huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount)); 02526 #else 02527 /*Call legacy weak Rx Event callback*/ 02528 HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount)); 02529 #endif 02530 } 02531 return; 02532 } 02533 else 02534 { 02535 /* DMA mode not enabled */ 02536 /* Check received length : If all expected data are received, do nothing. 02537 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */ 02538 uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount; 02539 if ( (huart->RxXferCount > 0U) 02540 &&(nb_rx_data > 0U) ) 02541 { 02542 /* Disable the UART Parity Error Interrupt and RXNE interrupts */ 02543 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02544 02545 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 02546 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02547 02548 /* Rx process is completed, restore huart->RxState to Ready */ 02549 huart->RxState = HAL_UART_STATE_READY; 02550 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 02551 02552 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 02553 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02554 /*Call registered Rx complete callback*/ 02555 huart->RxEventCallback(huart, nb_rx_data); 02556 #else 02557 /*Call legacy weak Rx Event callback*/ 02558 HAL_UARTEx_RxEventCallback(huart, nb_rx_data); 02559 #endif 02560 } 02561 return; 02562 } 02563 } 02564 02565 /* UART in mode Transmitter ------------------------------------------------*/ 02566 if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) 02567 { 02568 UART_Transmit_IT(huart); 02569 return; 02570 } 02571 02572 /* UART in mode Transmitter end --------------------------------------------*/ 02573 if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) 02574 { 02575 UART_EndTransmit_IT(huart); 02576 return; 02577 } 02578 } 02579 02580 /** 02581 * @brief Tx Transfer completed callbacks. 02582 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02583 * the configuration information for the specified UART module. 02584 * @retval None 02585 */ 02586 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 02587 { 02588 /* Prevent unused argument(s) compilation warning */ 02589 UNUSED(huart); 02590 /* NOTE: This function should not be modified, when the callback is needed, 02591 the HAL_UART_TxCpltCallback could be implemented in the user file 02592 */ 02593 } 02594 02595 /** 02596 * @brief Tx Half Transfer completed callbacks. 02597 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02598 * the configuration information for the specified UART module. 02599 * @retval None 02600 */ 02601 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) 02602 { 02603 /* Prevent unused argument(s) compilation warning */ 02604 UNUSED(huart); 02605 /* NOTE: This function should not be modified, when the callback is needed, 02606 the HAL_UART_TxHalfCpltCallback could be implemented in the user file 02607 */ 02608 } 02609 02610 /** 02611 * @brief Rx Transfer completed callbacks. 02612 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02613 * the configuration information for the specified UART module. 02614 * @retval None 02615 */ 02616 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 02617 { 02618 /* Prevent unused argument(s) compilation warning */ 02619 UNUSED(huart); 02620 /* NOTE: This function should not be modified, when the callback is needed, 02621 the HAL_UART_RxCpltCallback could be implemented in the user file 02622 */ 02623 } 02624 02625 /** 02626 * @brief Rx Half Transfer completed callbacks. 02627 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02628 * the configuration information for the specified UART module. 02629 * @retval None 02630 */ 02631 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) 02632 { 02633 /* Prevent unused argument(s) compilation warning */ 02634 UNUSED(huart); 02635 /* NOTE: This function should not be modified, when the callback is needed, 02636 the HAL_UART_RxHalfCpltCallback could be implemented in the user file 02637 */ 02638 } 02639 02640 /** 02641 * @brief UART error callbacks. 02642 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02643 * the configuration information for the specified UART module. 02644 * @retval None 02645 */ 02646 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 02647 { 02648 /* Prevent unused argument(s) compilation warning */ 02649 UNUSED(huart); 02650 /* NOTE: This function should not be modified, when the callback is needed, 02651 the HAL_UART_ErrorCallback could be implemented in the user file 02652 */ 02653 } 02654 02655 /** 02656 * @brief UART Abort Complete callback. 02657 * @param huart UART handle. 02658 * @retval None 02659 */ 02660 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart) 02661 { 02662 /* Prevent unused argument(s) compilation warning */ 02663 UNUSED(huart); 02664 02665 /* NOTE : This function should not be modified, when the callback is needed, 02666 the HAL_UART_AbortCpltCallback can be implemented in the user file. 02667 */ 02668 } 02669 02670 /** 02671 * @brief UART Abort Complete callback. 02672 * @param huart UART handle. 02673 * @retval None 02674 */ 02675 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart) 02676 { 02677 /* Prevent unused argument(s) compilation warning */ 02678 UNUSED(huart); 02679 02680 /* NOTE : This function should not be modified, when the callback is needed, 02681 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file. 02682 */ 02683 } 02684 02685 /** 02686 * @brief UART Abort Receive Complete callback. 02687 * @param huart UART handle. 02688 * @retval None 02689 */ 02690 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart) 02691 { 02692 /* Prevent unused argument(s) compilation warning */ 02693 UNUSED(huart); 02694 02695 /* NOTE : This function should not be modified, when the callback is needed, 02696 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file. 02697 */ 02698 } 02699 02700 /** 02701 * @brief Reception Event Callback (Rx event notification called after use of advanced reception service). 02702 * @param huart UART handle 02703 * @param Size Number of data available in application reception buffer (indicates a position in 02704 * reception buffer until which, data are available) 02705 * @retval None 02706 */ 02707 __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) 02708 { 02709 /* Prevent unused argument(s) compilation warning */ 02710 UNUSED(huart); 02711 UNUSED(Size); 02712 02713 /* NOTE : This function should not be modified, when the callback is needed, 02714 the HAL_UARTEx_RxEventCallback can be implemented in the user file. 02715 */ 02716 } 02717 02718 /** 02719 * @} 02720 */ 02721 02722 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions 02723 * @brief UART control functions 02724 * 02725 @verbatim 02726 ============================================================================== 02727 ##### Peripheral Control functions ##### 02728 ============================================================================== 02729 [..] 02730 This subsection provides a set of functions allowing to control the UART: 02731 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character. 02732 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode. 02733 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software. 02734 (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode 02735 (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode 02736 02737 @endverbatim 02738 * @{ 02739 */ 02740 02741 /** 02742 * @brief Transmits break characters. 02743 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02744 * the configuration information for the specified UART module. 02745 * @retval HAL status 02746 */ 02747 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) 02748 { 02749 /* Check the parameters */ 02750 assert_param(IS_UART_INSTANCE(huart->Instance)); 02751 02752 /* Process Locked */ 02753 __HAL_LOCK(huart); 02754 02755 huart->gState = HAL_UART_STATE_BUSY; 02756 02757 /* Send break characters */ 02758 SET_BIT(huart->Instance->CR1, USART_CR1_SBK); 02759 02760 huart->gState = HAL_UART_STATE_READY; 02761 02762 /* Process Unlocked */ 02763 __HAL_UNLOCK(huart); 02764 02765 return HAL_OK; 02766 } 02767 02768 /** 02769 * @brief Enters the UART in mute mode. 02770 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02771 * the configuration information for the specified UART module. 02772 * @retval HAL status 02773 */ 02774 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) 02775 { 02776 /* Check the parameters */ 02777 assert_param(IS_UART_INSTANCE(huart->Instance)); 02778 02779 /* Process Locked */ 02780 __HAL_LOCK(huart); 02781 02782 huart->gState = HAL_UART_STATE_BUSY; 02783 02784 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */ 02785 SET_BIT(huart->Instance->CR1, USART_CR1_RWU); 02786 02787 huart->gState = HAL_UART_STATE_READY; 02788 02789 /* Process Unlocked */ 02790 __HAL_UNLOCK(huart); 02791 02792 return HAL_OK; 02793 } 02794 02795 /** 02796 * @brief Exits the UART mute mode: wake up software. 02797 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02798 * the configuration information for the specified UART module. 02799 * @retval HAL status 02800 */ 02801 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart) 02802 { 02803 /* Check the parameters */ 02804 assert_param(IS_UART_INSTANCE(huart->Instance)); 02805 02806 /* Process Locked */ 02807 __HAL_LOCK(huart); 02808 02809 huart->gState = HAL_UART_STATE_BUSY; 02810 02811 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */ 02812 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU); 02813 02814 huart->gState = HAL_UART_STATE_READY; 02815 02816 /* Process Unlocked */ 02817 __HAL_UNLOCK(huart); 02818 02819 return HAL_OK; 02820 } 02821 02822 /** 02823 * @brief Enables the UART transmitter and disables the UART receiver. 02824 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02825 * the configuration information for the specified UART module. 02826 * @retval HAL status 02827 */ 02828 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) 02829 { 02830 uint32_t tmpreg = 0x00U; 02831 02832 /* Process Locked */ 02833 __HAL_LOCK(huart); 02834 02835 huart->gState = HAL_UART_STATE_BUSY; 02836 02837 /*-------------------------- USART CR1 Configuration -----------------------*/ 02838 tmpreg = huart->Instance->CR1; 02839 02840 /* Clear TE and RE bits */ 02841 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); 02842 02843 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */ 02844 tmpreg |= (uint32_t)USART_CR1_TE; 02845 02846 /* Write to USART CR1 */ 02847 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg); 02848 02849 huart->gState = HAL_UART_STATE_READY; 02850 02851 /* Process Unlocked */ 02852 __HAL_UNLOCK(huart); 02853 02854 return HAL_OK; 02855 } 02856 02857 /** 02858 * @brief Enables the UART receiver and disables the UART transmitter. 02859 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02860 * the configuration information for the specified UART module. 02861 * @retval HAL status 02862 */ 02863 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) 02864 { 02865 uint32_t tmpreg = 0x00U; 02866 02867 /* Process Locked */ 02868 __HAL_LOCK(huart); 02869 02870 huart->gState = HAL_UART_STATE_BUSY; 02871 02872 /*-------------------------- USART CR1 Configuration -----------------------*/ 02873 tmpreg = huart->Instance->CR1; 02874 02875 /* Clear TE and RE bits */ 02876 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); 02877 02878 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */ 02879 tmpreg |= (uint32_t)USART_CR1_RE; 02880 02881 /* Write to USART CR1 */ 02882 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg); 02883 02884 huart->gState = HAL_UART_STATE_READY; 02885 02886 /* Process Unlocked */ 02887 __HAL_UNLOCK(huart); 02888 02889 return HAL_OK; 02890 } 02891 02892 /** 02893 * @} 02894 */ 02895 02896 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions 02897 * @brief UART State and Errors functions 02898 * 02899 @verbatim 02900 ============================================================================== 02901 ##### Peripheral State and Errors functions ##### 02902 ============================================================================== 02903 [..] 02904 This subsection provides a set of functions allowing to return the State of 02905 UART communication process, return Peripheral Errors occurred during communication 02906 process 02907 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral. 02908 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication. 02909 02910 @endverbatim 02911 * @{ 02912 */ 02913 02914 /** 02915 * @brief Returns the UART state. 02916 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02917 * the configuration information for the specified UART module. 02918 * @retval HAL state 02919 */ 02920 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) 02921 { 02922 uint32_t temp1 = 0x00U, temp2 = 0x00U; 02923 temp1 = huart->gState; 02924 temp2 = huart->RxState; 02925 02926 return (HAL_UART_StateTypeDef)(temp1 | temp2); 02927 } 02928 02929 /** 02930 * @brief Return the UART error code 02931 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02932 * the configuration information for the specified UART. 02933 * @retval UART Error Code 02934 */ 02935 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart) 02936 { 02937 return huart->ErrorCode; 02938 } 02939 02940 /** 02941 * @} 02942 */ 02943 02944 /** 02945 * @} 02946 */ 02947 02948 /** @defgroup UART_Private_Functions UART Private Functions 02949 * @{ 02950 */ 02951 02952 /** 02953 * @brief Initialize the callbacks to their default values. 02954 * @param huart UART handle. 02955 * @retval none 02956 */ 02957 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 02958 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart) 02959 { 02960 /* Init the UART Callback settings */ 02961 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */ 02962 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */ 02963 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */ 02964 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */ 02965 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */ 02966 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */ 02967 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */ 02968 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */ 02969 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak RxEventCallback */ 02970 02971 } 02972 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 02973 02974 /** 02975 * @brief DMA UART transmit process complete callback. 02976 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 02977 * the configuration information for the specified DMA module. 02978 * @retval None 02979 */ 02980 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma) 02981 { 02982 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 02983 /* DMA Normal mode*/ 02984 if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U) 02985 { 02986 huart->TxXferCount = 0x00U; 02987 02988 /* Disable the DMA transfer for transmit request by setting the DMAT bit 02989 in the UART CR3 register */ 02990 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 02991 02992 /* Enable the UART Transmit Complete Interrupt */ 02993 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 02994 02995 } 02996 /* DMA Circular mode */ 02997 else 02998 { 02999 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03000 /*Call registered Tx complete callback*/ 03001 huart->TxCpltCallback(huart); 03002 #else 03003 /*Call legacy weak Tx complete callback*/ 03004 HAL_UART_TxCpltCallback(huart); 03005 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03006 } 03007 } 03008 03009 /** 03010 * @brief DMA UART transmit process half complete callback 03011 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03012 * the configuration information for the specified DMA module. 03013 * @retval None 03014 */ 03015 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 03016 { 03017 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03018 03019 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03020 /*Call registered Tx complete callback*/ 03021 huart->TxHalfCpltCallback(huart); 03022 #else 03023 /*Call legacy weak Tx complete callback*/ 03024 HAL_UART_TxHalfCpltCallback(huart); 03025 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03026 } 03027 03028 /** 03029 * @brief DMA UART receive process complete callback. 03030 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03031 * the configuration information for the specified DMA module. 03032 * @retval None 03033 */ 03034 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 03035 { 03036 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03037 /* DMA Normal mode*/ 03038 if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U) 03039 { 03040 huart->RxXferCount = 0U; 03041 03042 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 03043 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 03044 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03045 03046 /* Disable the DMA transfer for the receiver request by setting the DMAR bit 03047 in the UART CR3 register */ 03048 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 03049 03050 /* At end of Rx process, restore huart->RxState to Ready */ 03051 huart->RxState = HAL_UART_STATE_READY; 03052 03053 /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */ 03054 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03055 { 03056 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 03057 } 03058 } 03059 03060 /* Check current reception Mode : 03061 If Reception till IDLE event has been selected : use Rx Event callback */ 03062 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03063 { 03064 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03065 /*Call registered Rx Event callback*/ 03066 huart->RxEventCallback(huart, huart->RxXferSize); 03067 #else 03068 /*Call legacy weak Rx Event callback*/ 03069 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 03070 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03071 } 03072 else 03073 { 03074 /* In other cases : use Rx Complete callback */ 03075 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03076 /*Call registered Rx complete callback*/ 03077 huart->RxCpltCallback(huart); 03078 #else 03079 /*Call legacy weak Rx complete callback*/ 03080 HAL_UART_RxCpltCallback(huart); 03081 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03082 } 03083 } 03084 03085 /** 03086 * @brief DMA UART receive process half complete callback 03087 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03088 * the configuration information for the specified DMA module. 03089 * @retval None 03090 */ 03091 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 03092 { 03093 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03094 03095 /* Check current reception Mode : 03096 If Reception till IDLE event has been selected : use Rx Event callback */ 03097 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03098 { 03099 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03100 /*Call registered Rx Event callback*/ 03101 huart->RxEventCallback(huart, huart->RxXferSize/2U); 03102 #else 03103 /*Call legacy weak Rx Event callback*/ 03104 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize/2U); 03105 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03106 } 03107 else 03108 { 03109 /* In other cases : use Rx Half Complete callback */ 03110 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03111 /*Call registered Rx Half complete callback*/ 03112 huart->RxHalfCpltCallback(huart); 03113 #else 03114 /*Call legacy weak Rx Half complete callback*/ 03115 HAL_UART_RxHalfCpltCallback(huart); 03116 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03117 } 03118 } 03119 03120 /** 03121 * @brief DMA UART communication error callback. 03122 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03123 * the configuration information for the specified DMA module. 03124 * @retval None 03125 */ 03126 static void UART_DMAError(DMA_HandleTypeDef *hdma) 03127 { 03128 uint32_t dmarequest = 0x00U; 03129 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03130 03131 /* Stop UART DMA Tx request if ongoing */ 03132 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 03133 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 03134 { 03135 huart->TxXferCount = 0x00U; 03136 UART_EndTxTransfer(huart); 03137 } 03138 03139 /* Stop UART DMA Rx request if ongoing */ 03140 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 03141 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 03142 { 03143 huart->RxXferCount = 0x00U; 03144 UART_EndRxTransfer(huart); 03145 } 03146 03147 huart->ErrorCode |= HAL_UART_ERROR_DMA; 03148 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03149 /*Call registered error callback*/ 03150 huart->ErrorCallback(huart); 03151 #else 03152 /*Call legacy weak error callback*/ 03153 HAL_UART_ErrorCallback(huart); 03154 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03155 } 03156 03157 /** 03158 * @brief This function handles UART Communication Timeout. 03159 * @param huart Pointer to a UART_HandleTypeDef structure that contains 03160 * the configuration information for the specified UART module. 03161 * @param Flag specifies the UART flag to check. 03162 * @param Status The new Flag status (SET or RESET). 03163 * @param Tickstart Tick start value 03164 * @param Timeout Timeout duration 03165 * @retval HAL status 03166 */ 03167 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) 03168 { 03169 /* Wait until flag is set */ 03170 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) 03171 { 03172 /* Check for the Timeout */ 03173 if (Timeout != HAL_MAX_DELAY) 03174 { 03175 if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout)) 03176 { 03177 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ 03178 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE)); 03179 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03180 03181 huart->gState = HAL_UART_STATE_READY; 03182 huart->RxState = HAL_UART_STATE_READY; 03183 03184 /* Process Unlocked */ 03185 __HAL_UNLOCK(huart); 03186 03187 return HAL_TIMEOUT; 03188 } 03189 } 03190 } 03191 return HAL_OK; 03192 } 03193 03194 /** 03195 * @brief Start Receive operation in interrupt mode. 03196 * @note This function could be called by all HAL UART API providing reception in Interrupt mode. 03197 * @note When calling this function, parameters validity is considered as already checked, 03198 * i.e. Rx State, buffer address, ... 03199 * UART Handle is assumed as Locked. 03200 * @param huart UART handle. 03201 * @param pData Pointer to data buffer (u8 or u16 data elements). 03202 * @param Size Amount of data elements (u8 or u16) to be received. 03203 * @retval HAL status 03204 */ 03205 HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 03206 { 03207 huart->pRxBuffPtr = pData; 03208 huart->RxXferSize = Size; 03209 huart->RxXferCount = Size; 03210 03211 huart->ErrorCode = HAL_UART_ERROR_NONE; 03212 huart->RxState = HAL_UART_STATE_BUSY_RX; 03213 03214 /* Process Unlocked */ 03215 __HAL_UNLOCK(huart); 03216 03217 /* Enable the UART Parity Error Interrupt */ 03218 __HAL_UART_ENABLE_IT(huart, UART_IT_PE); 03219 03220 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 03221 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR); 03222 03223 /* Enable the UART Data Register not empty Interrupt */ 03224 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); 03225 03226 return HAL_OK; 03227 } 03228 03229 /** 03230 * @brief Start Receive operation in DMA mode. 03231 * @note This function could be called by all HAL UART API providing reception in DMA mode. 03232 * @note When calling this function, parameters validity is considered as already checked, 03233 * i.e. Rx State, buffer address, ... 03234 * UART Handle is assumed as Locked. 03235 * @param huart UART handle. 03236 * @param pData Pointer to data buffer (u8 or u16 data elements). 03237 * @param Size Amount of data elements (u8 or u16) to be received. 03238 * @retval HAL status 03239 */ 03240 HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 03241 { 03242 uint32_t *tmp; 03243 03244 huart->pRxBuffPtr = pData; 03245 huart->RxXferSize = Size; 03246 03247 huart->ErrorCode = HAL_UART_ERROR_NONE; 03248 huart->RxState = HAL_UART_STATE_BUSY_RX; 03249 03250 /* Set the UART DMA transfer complete callback */ 03251 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; 03252 03253 /* Set the UART DMA Half transfer complete callback */ 03254 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; 03255 03256 /* Set the DMA error callback */ 03257 huart->hdmarx->XferErrorCallback = UART_DMAError; 03258 03259 /* Set the DMA abort callback */ 03260 huart->hdmarx->XferAbortCallback = NULL; 03261 03262 /* Enable the DMA stream */ 03263 tmp = (uint32_t *)&pData; 03264 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t *)tmp, Size); 03265 03266 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */ 03267 __HAL_UART_CLEAR_OREFLAG(huart); 03268 03269 /* Process Unlocked */ 03270 __HAL_UNLOCK(huart); 03271 03272 /* Enable the UART Parity Error Interrupt */ 03273 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 03274 03275 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 03276 SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 03277 03278 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 03279 in the UART CR3 register */ 03280 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 03281 03282 return HAL_OK; 03283 } 03284 03285 /** 03286 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion). 03287 * @param huart UART handle. 03288 * @retval None 03289 */ 03290 static void UART_EndTxTransfer(UART_HandleTypeDef *huart) 03291 { 03292 /* Disable TXEIE and TCIE interrupts */ 03293 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 03294 03295 /* At end of Tx process, restore huart->gState to Ready */ 03296 huart->gState = HAL_UART_STATE_READY; 03297 } 03298 03299 /** 03300 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). 03301 * @param huart UART handle. 03302 * @retval None 03303 */ 03304 static void UART_EndRxTransfer(UART_HandleTypeDef *huart) 03305 { 03306 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 03307 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 03308 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 03309 03310 /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */ 03311 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03312 { 03313 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 03314 } 03315 03316 /* At end of Rx process, restore huart->RxState to Ready */ 03317 huart->RxState = HAL_UART_STATE_READY; 03318 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03319 } 03320 03321 /** 03322 * @brief DMA UART communication abort callback, when initiated by HAL services on Error 03323 * (To be called at end of DMA Abort procedure following error occurrence). 03324 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03325 * the configuration information for the specified DMA module. 03326 * @retval None 03327 */ 03328 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma) 03329 { 03330 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03331 huart->RxXferCount = 0x00U; 03332 huart->TxXferCount = 0x00U; 03333 03334 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03335 /*Call registered error callback*/ 03336 huart->ErrorCallback(huart); 03337 #else 03338 /*Call legacy weak error callback*/ 03339 HAL_UART_ErrorCallback(huart); 03340 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03341 } 03342 03343 /** 03344 * @brief DMA UART Tx communication abort callback, when initiated by user 03345 * (To be called at end of DMA Tx Abort procedure following user abort request). 03346 * @note When this callback is executed, User Abort complete call back is called only if no 03347 * Abort still ongoing for Rx DMA Handle. 03348 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03349 * the configuration information for the specified DMA module. 03350 * @retval None 03351 */ 03352 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) 03353 { 03354 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03355 03356 huart->hdmatx->XferAbortCallback = NULL; 03357 03358 /* Check if an Abort process is still ongoing */ 03359 if (huart->hdmarx != NULL) 03360 { 03361 if (huart->hdmarx->XferAbortCallback != NULL) 03362 { 03363 return; 03364 } 03365 } 03366 03367 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 03368 huart->TxXferCount = 0x00U; 03369 huart->RxXferCount = 0x00U; 03370 03371 /* Reset ErrorCode */ 03372 huart->ErrorCode = HAL_UART_ERROR_NONE; 03373 03374 /* Restore huart->gState and huart->RxState to Ready */ 03375 huart->gState = HAL_UART_STATE_READY; 03376 huart->RxState = HAL_UART_STATE_READY; 03377 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03378 03379 /* Call user Abort complete callback */ 03380 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03381 /* Call registered Abort complete callback */ 03382 huart->AbortCpltCallback(huart); 03383 #else 03384 /* Call legacy weak Abort complete callback */ 03385 HAL_UART_AbortCpltCallback(huart); 03386 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03387 } 03388 03389 /** 03390 * @brief DMA UART Rx communication abort callback, when initiated by user 03391 * (To be called at end of DMA Rx Abort procedure following user abort request). 03392 * @note When this callback is executed, User Abort complete call back is called only if no 03393 * Abort still ongoing for Tx DMA Handle. 03394 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03395 * the configuration information for the specified DMA module. 03396 * @retval None 03397 */ 03398 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) 03399 { 03400 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03401 03402 huart->hdmarx->XferAbortCallback = NULL; 03403 03404 /* Check if an Abort process is still ongoing */ 03405 if (huart->hdmatx != NULL) 03406 { 03407 if (huart->hdmatx->XferAbortCallback != NULL) 03408 { 03409 return; 03410 } 03411 } 03412 03413 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 03414 huart->TxXferCount = 0x00U; 03415 huart->RxXferCount = 0x00U; 03416 03417 /* Reset ErrorCode */ 03418 huart->ErrorCode = HAL_UART_ERROR_NONE; 03419 03420 /* Restore huart->gState and huart->RxState to Ready */ 03421 huart->gState = HAL_UART_STATE_READY; 03422 huart->RxState = HAL_UART_STATE_READY; 03423 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03424 03425 /* Call user Abort complete callback */ 03426 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03427 /* Call registered Abort complete callback */ 03428 huart->AbortCpltCallback(huart); 03429 #else 03430 /* Call legacy weak Abort complete callback */ 03431 HAL_UART_AbortCpltCallback(huart); 03432 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03433 } 03434 03435 /** 03436 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to 03437 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer) 03438 * (This callback is executed at end of DMA Tx Abort procedure following user abort request, 03439 * and leads to user Tx Abort Complete callback execution). 03440 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03441 * the configuration information for the specified DMA module. 03442 * @retval None 03443 */ 03444 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 03445 { 03446 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03447 03448 huart->TxXferCount = 0x00U; 03449 03450 /* Restore huart->gState to Ready */ 03451 huart->gState = HAL_UART_STATE_READY; 03452 03453 /* Call user Abort complete callback */ 03454 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03455 /* Call registered Abort Transmit Complete Callback */ 03456 huart->AbortTransmitCpltCallback(huart); 03457 #else 03458 /* Call legacy weak Abort Transmit Complete Callback */ 03459 HAL_UART_AbortTransmitCpltCallback(huart); 03460 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03461 } 03462 03463 /** 03464 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to 03465 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer) 03466 * (This callback is executed at end of DMA Rx Abort procedure following user abort request, 03467 * and leads to user Rx Abort Complete callback execution). 03468 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains 03469 * the configuration information for the specified DMA module. 03470 * @retval None 03471 */ 03472 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 03473 { 03474 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent; 03475 03476 huart->RxXferCount = 0x00U; 03477 03478 /* Restore huart->RxState to Ready */ 03479 huart->RxState = HAL_UART_STATE_READY; 03480 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03481 03482 /* Call user Abort complete callback */ 03483 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03484 /* Call registered Abort Receive Complete Callback */ 03485 huart->AbortReceiveCpltCallback(huart); 03486 #else 03487 /* Call legacy weak Abort Receive Complete Callback */ 03488 HAL_UART_AbortReceiveCpltCallback(huart); 03489 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03490 } 03491 03492 /** 03493 * @brief Sends an amount of data in non blocking mode. 03494 * @param huart Pointer to a UART_HandleTypeDef structure that contains 03495 * the configuration information for the specified UART module. 03496 * @retval HAL status 03497 */ 03498 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart) 03499 { 03500 uint16_t *tmp; 03501 03502 /* Check that a Tx process is ongoing */ 03503 if (huart->gState == HAL_UART_STATE_BUSY_TX) 03504 { 03505 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 03506 { 03507 tmp = (uint16_t *) huart->pTxBuffPtr; 03508 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); 03509 huart->pTxBuffPtr += 2U; 03510 } 03511 else 03512 { 03513 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF); 03514 } 03515 03516 if (--huart->TxXferCount == 0U) 03517 { 03518 /* Disable the UART Transmit Complete Interrupt */ 03519 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); 03520 03521 /* Enable the UART Transmit Complete Interrupt */ 03522 __HAL_UART_ENABLE_IT(huart, UART_IT_TC); 03523 } 03524 return HAL_OK; 03525 } 03526 else 03527 { 03528 return HAL_BUSY; 03529 } 03530 } 03531 03532 /** 03533 * @brief Wraps up transmission in non blocking mode. 03534 * @param huart Pointer to a UART_HandleTypeDef structure that contains 03535 * the configuration information for the specified UART module. 03536 * @retval HAL status 03537 */ 03538 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart) 03539 { 03540 /* Disable the UART Transmit Complete Interrupt */ 03541 __HAL_UART_DISABLE_IT(huart, UART_IT_TC); 03542 03543 /* Tx process is ended, restore huart->gState to Ready */ 03544 huart->gState = HAL_UART_STATE_READY; 03545 03546 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03547 /*Call registered Tx complete callback*/ 03548 huart->TxCpltCallback(huart); 03549 #else 03550 /*Call legacy weak Tx complete callback*/ 03551 HAL_UART_TxCpltCallback(huart); 03552 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03553 03554 return HAL_OK; 03555 } 03556 03557 /** 03558 * @brief Receives an amount of data in non blocking mode 03559 * @param huart Pointer to a UART_HandleTypeDef structure that contains 03560 * the configuration information for the specified UART module. 03561 * @retval HAL status 03562 */ 03563 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart) 03564 { 03565 uint8_t *pdata8bits; 03566 uint16_t *pdata16bits; 03567 03568 /* Check that a Rx process is ongoing */ 03569 if (huart->RxState == HAL_UART_STATE_BUSY_RX) 03570 { 03571 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE)) 03572 { 03573 pdata8bits = NULL; 03574 pdata16bits = (uint16_t *) huart->pRxBuffPtr; 03575 *pdata16bits = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); 03576 huart->pRxBuffPtr += 2U; 03577 } 03578 else 03579 { 03580 pdata8bits = (uint8_t *) huart->pRxBuffPtr; 03581 pdata16bits = NULL; 03582 03583 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) || ((huart->Init.WordLength == UART_WORDLENGTH_8B) && (huart->Init.Parity == UART_PARITY_NONE))) 03584 { 03585 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 03586 } 03587 else 03588 { 03589 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 03590 } 03591 huart->pRxBuffPtr += 1U; 03592 } 03593 03594 if (--huart->RxXferCount == 0U) 03595 { 03596 /* Disable the UART Data Register not empty Interrupt */ 03597 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); 03598 03599 /* Disable the UART Parity Error Interrupt */ 03600 __HAL_UART_DISABLE_IT(huart, UART_IT_PE); 03601 03602 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 03603 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); 03604 03605 /* Rx process is completed, restore huart->RxState to Ready */ 03606 huart->RxState = HAL_UART_STATE_READY; 03607 03608 /* Check current reception Mode : 03609 If Reception till IDLE event has been selected : */ 03610 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE) 03611 { 03612 /* Disable IDLE interrupt */ 03613 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE); 03614 03615 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03616 /*Call registered Rx Event callback*/ 03617 huart->RxEventCallback(huart, huart->RxXferSize); 03618 #else 03619 /*Call legacy weak Rx Event callback*/ 03620 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize); 03621 #endif 03622 } 03623 else 03624 { 03625 /* Standard reception API called */ 03626 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 03627 /*Call registered Rx complete callback*/ 03628 huart->RxCpltCallback(huart); 03629 #else 03630 /*Call legacy weak Rx complete callback*/ 03631 HAL_UART_RxCpltCallback(huart); 03632 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 03633 } 03634 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD; 03635 return HAL_OK; 03636 } 03637 return HAL_OK; 03638 } 03639 else 03640 { 03641 return HAL_BUSY; 03642 } 03643 } 03644 03645 /** 03646 * @brief Configures the UART peripheral. 03647 * @param huart Pointer to a UART_HandleTypeDef structure that contains 03648 * the configuration information for the specified UART module. 03649 * @retval None 03650 */ 03651 static void UART_SetConfig(UART_HandleTypeDef *huart) 03652 { 03653 uint32_t tmpreg; 03654 uint32_t pclk; 03655 03656 /* Check the parameters */ 03657 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); 03658 assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); 03659 assert_param(IS_UART_PARITY(huart->Init.Parity)); 03660 assert_param(IS_UART_MODE(huart->Init.Mode)); 03661 03662 /*-------------------------- USART CR2 Configuration -----------------------*/ 03663 /* Configure the UART Stop Bits: Set STOP[13:12] bits 03664 according to huart->Init.StopBits value */ 03665 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits); 03666 03667 /*-------------------------- USART CR1 Configuration -----------------------*/ 03668 /* Configure the UART Word Length, Parity and mode: 03669 Set the M bits according to huart->Init.WordLength value 03670 Set PCE and PS bits according to huart->Init.Parity value 03671 Set TE and RE bits according to huart->Init.Mode value 03672 Set OVER8 bit according to huart->Init.OverSampling value */ 03673 03674 #if defined(USART_CR1_OVER8) 03675 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling; 03676 MODIFY_REG(huart->Instance->CR1, 03677 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8), 03678 tmpreg); 03679 #else 03680 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode; 03681 MODIFY_REG(huart->Instance->CR1, 03682 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE), 03683 tmpreg); 03684 #endif /* USART_CR1_OVER8 */ 03685 03686 /*-------------------------- USART CR3 Configuration -----------------------*/ 03687 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */ 03688 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl); 03689 03690 03691 if(huart->Instance == USART1) 03692 { 03693 pclk = HAL_RCC_GetPCLK2Freq(); 03694 } 03695 else 03696 { 03697 pclk = HAL_RCC_GetPCLK1Freq(); 03698 } 03699 03700 /*-------------------------- USART BRR Configuration ---------------------*/ 03701 #if defined(USART_CR1_OVER8) 03702 if (huart->Init.OverSampling == UART_OVERSAMPLING_8) 03703 { 03704 huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate); 03705 } 03706 else 03707 { 03708 huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate); 03709 } 03710 #else 03711 huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate); 03712 #endif /* USART_CR1_OVER8 */ 03713 } 03714 03715 /** 03716 * @} 03717 */ 03718 03719 #endif /* HAL_UART_MODULE_ENABLED */ 03720 /** 03721 * @} 03722 */ 03723 03724 /** 03725 * @} 03726 */ 03727 03728 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/