STM32F439xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_uart.c 00004 * @author MCD Application Team 00005 * @brief UART HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral: 00008 * + Initialization and de-initialization functions 00009 * + IO operation functions 00010 * + Peripheral Control functions 00011 * + Peripheral State and Errors functions 00012 * 00013 @verbatim 00014 ============================================================================== 00015 ##### How to use this driver ##### 00016 ============================================================================== 00017 [..] 00018 The UART HAL driver can be used as follows: 00019 00020 (#) Declare a UART_HandleTypeDef handle structure. 00021 00022 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API: 00023 (##) Enable the USARTx interface clock. 00024 (##) UART pins configuration: 00025 (+++) Enable the clock for the UART GPIOs. 00026 (+++) Configure these UART pins as alternate function pull-up. 00027 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT() 00028 and HAL_UART_Receive_IT() APIs): 00029 (+++) Configure the USARTx interrupt priority. 00030 (+++) Enable the NVIC USART IRQ handle. 00031 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA() 00032 and HAL_UART_Receive_DMA() APIs): 00033 (+++) Declare a DMA handle structure for the Tx/Rx stream. 00034 (+++) Enable the DMAx interface clock. 00035 (+++) Configure the declared DMA handle structure with the required 00036 Tx/Rx parameters. 00037 (+++) Configure the DMA Tx/Rx Stream. 00038 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle. 00039 (+++) Configure the priority and enable the NVIC for the transfer complete 00040 interrupt on the DMA Tx/Rx Stream. 00041 00042 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware 00043 flow control and Mode(Receiver/Transmitter) in the 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 [..] 00068 Three operation modes are available within this driver : 00069 00070 *** Polling mode IO operation *** 00071 ================================= 00072 [..] 00073 (+) Send an amount of data in blocking mode using HAL_UART_Transmit() 00074 (+) Receive an amount of data in blocking mode using HAL_UART_Receive() 00075 00076 *** Interrupt mode IO operation *** 00077 =================================== 00078 [..] 00079 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT() 00080 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 00081 add his own code by customization of function pointer HAL_UART_TxCpltCallback 00082 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT() 00083 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 00084 add his own code by customization of function pointer HAL_UART_RxCpltCallback 00085 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 00086 add his own code by customization of function pointer HAL_UART_ErrorCallback 00087 00088 *** DMA mode IO operation *** 00089 ============================== 00090 [..] 00091 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA() 00092 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can 00093 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback 00094 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 00095 add his own code by customization of function pointer HAL_UART_TxCpltCallback 00096 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA() 00097 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can 00098 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback 00099 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 00100 add his own code by customization of function pointer HAL_UART_RxCpltCallback 00101 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 00102 add his own code by customization of function pointer HAL_UART_ErrorCallback 00103 (+) Pause the DMA Transfer using HAL_UART_DMAPause() 00104 (+) Resume the DMA Transfer using HAL_UART_DMAResume() 00105 (+) Stop the DMA Transfer using HAL_UART_DMAStop() 00106 00107 *** UART HAL driver macros list *** 00108 ============================================= 00109 [..] 00110 Below the list of most used macros in UART HAL driver. 00111 00112 (+) __HAL_UART_ENABLE: Enable the UART peripheral 00113 (+) __HAL_UART_DISABLE: Disable the UART peripheral 00114 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not 00115 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag 00116 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt 00117 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt 00118 (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not 00119 00120 [..] 00121 (@) You can refer to the UART HAL driver header file for more useful macros 00122 00123 @endverbatim 00124 ****************************************************************************** 00125 * @attention 00126 * 00127 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00128 * 00129 * Redistribution and use in source and binary forms, with or without modification, 00130 * are permitted provided that the following conditions are met: 00131 * 1. Redistributions of source code must retain the above copyright notice, 00132 * this list of conditions and the following disclaimer. 00133 * 2. Redistributions in binary form must reproduce the above copyright notice, 00134 * this list of conditions and the following disclaimer in the documentation 00135 * and/or other materials provided with the distribution. 00136 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00137 * may be used to endorse or promote products derived from this software 00138 * without specific prior written permission. 00139 * 00140 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00141 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00142 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00143 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00144 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00145 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00146 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00147 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00148 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00149 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00150 * 00151 ****************************************************************************** 00152 */ 00153 00154 /* Includes ------------------------------------------------------------------*/ 00155 #include "stm32f4xx_hal.h" 00156 00157 /** @addtogroup STM32F4xx_HAL_Driver 00158 * @{ 00159 */ 00160 00161 /** @defgroup UART UART 00162 * @brief HAL UART module driver 00163 * @{ 00164 */ 00165 #ifdef HAL_UART_MODULE_ENABLED 00166 00167 /* Private typedef -----------------------------------------------------------*/ 00168 /* Private define ------------------------------------------------------------*/ 00169 /** @addtogroup UART_Private_Constants 00170 * @{ 00171 */ 00172 /** 00173 * @} 00174 */ 00175 /* Private macro -------------------------------------------------------------*/ 00176 /* Private variables ---------------------------------------------------------*/ 00177 /* Private function prototypes -----------------------------------------------*/ 00178 /** @addtogroup UART_Private_Functions UART Private Functions 00179 * @{ 00180 */ 00181 static void UART_EndTxTransfer(UART_HandleTypeDef *huart); 00182 static void UART_EndRxTransfer(UART_HandleTypeDef *huart); 00183 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00184 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00185 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00186 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00187 static void UART_DMAError(DMA_HandleTypeDef *hdma); 00188 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma); 00189 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); 00190 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma); 00191 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00192 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma); 00193 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart); 00194 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart); 00195 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart); 00196 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout); 00197 static void UART_SetConfig (UART_HandleTypeDef *huart); 00198 /** 00199 * @} 00200 */ 00201 00202 /* Exported functions ---------------------------------------------------------*/ 00203 /** @defgroup UART_Exported_Functions UART Exported Functions 00204 * @{ 00205 */ 00206 00207 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions 00208 * @brief Initialization and Configuration functions 00209 * 00210 @verbatim 00211 =============================================================================== 00212 ##### Initialization and Configuration functions ##### 00213 =============================================================================== 00214 [..] 00215 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy 00216 in asynchronous mode. 00217 (+) For the asynchronous mode only these parameters can be configured: 00218 (++) Baud Rate 00219 (++) Word Length 00220 (++) Stop Bit 00221 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00222 in the data register is transmitted but is changed by the parity bit. 00223 Depending on the frame length defined by the M bit (8-bits or 9-bits), 00224 please refer to Reference manual for possible UART frame formats. 00225 (++) Hardware flow control 00226 (++) Receiver/transmitter modes 00227 (++) Over Sampling Method 00228 [..] 00229 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs 00230 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor 00231 configuration procedures (details for the procedures are available in reference manual (RM0329)). 00232 00233 @endverbatim 00234 * @{ 00235 */ 00236 00237 /** 00238 * @brief Initializes the UART mode according to the specified parameters in 00239 * the UART_InitTypeDef and create the associated handle. 00240 * @param huart pointer to a UART_HandleTypeDef structure that contains 00241 * the configuration information for the specified UART module. 00242 * @retval HAL status 00243 */ 00244 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) 00245 { 00246 /* Check the UART handle allocation */ 00247 if(huart == NULL) 00248 { 00249 return HAL_ERROR; 00250 } 00251 00252 /* Check the parameters */ 00253 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) 00254 { 00255 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */ 00256 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); 00257 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); 00258 } 00259 else 00260 { 00261 assert_param(IS_UART_INSTANCE(huart->Instance)); 00262 } 00263 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00264 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00265 00266 if(huart->gState == HAL_UART_STATE_RESET) 00267 { 00268 /* Allocate lock resource and initialize it */ 00269 huart->Lock = HAL_UNLOCKED; 00270 /* Init the low level hardware */ 00271 HAL_UART_MspInit(huart); 00272 } 00273 00274 huart->gState = HAL_UART_STATE_BUSY; 00275 00276 /* Disable the peripheral */ 00277 __HAL_UART_DISABLE(huart); 00278 00279 /* Set the UART Communication parameters */ 00280 UART_SetConfig(huart); 00281 00282 /* In asynchronous mode, the following bits must be kept cleared: 00283 - LINEN and CLKEN bits in the USART_CR2 register, 00284 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ 00285 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00286 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00287 00288 /* Enable the peripheral */ 00289 __HAL_UART_ENABLE(huart); 00290 00291 /* Initialize the UART state */ 00292 huart->ErrorCode = HAL_UART_ERROR_NONE; 00293 huart->gState= HAL_UART_STATE_READY; 00294 huart->RxState= HAL_UART_STATE_READY; 00295 00296 return HAL_OK; 00297 } 00298 00299 /** 00300 * @brief Initializes the half-duplex mode according to the specified 00301 * parameters in the UART_InitTypeDef and create the associated handle. 00302 * @param huart pointer to a UART_HandleTypeDef structure that contains 00303 * the configuration information for the specified UART module. 00304 * @retval HAL status 00305 */ 00306 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) 00307 { 00308 /* Check the UART handle allocation */ 00309 if(huart == NULL) 00310 { 00311 return HAL_ERROR; 00312 } 00313 00314 /* Check the parameters */ 00315 assert_param(IS_UART_INSTANCE(huart->Instance)); 00316 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00317 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00318 00319 if(huart->gState == HAL_UART_STATE_RESET) 00320 { 00321 /* Allocate lock resource and initialize it */ 00322 huart->Lock = HAL_UNLOCKED; 00323 /* Init the low level hardware */ 00324 HAL_UART_MspInit(huart); 00325 } 00326 00327 huart->gState = HAL_UART_STATE_BUSY; 00328 00329 /* Disable the peripheral */ 00330 __HAL_UART_DISABLE(huart); 00331 00332 /* Set the UART Communication parameters */ 00333 UART_SetConfig(huart); 00334 00335 /* In half-duplex mode, the following bits must be kept cleared: 00336 - LINEN and CLKEN bits in the USART_CR2 register, 00337 - SCEN and IREN bits in the USART_CR3 register.*/ 00338 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00339 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN)); 00340 00341 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ 00342 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL); 00343 00344 /* Enable the peripheral */ 00345 __HAL_UART_ENABLE(huart); 00346 00347 /* Initialize the UART state*/ 00348 huart->ErrorCode = HAL_UART_ERROR_NONE; 00349 huart->gState= HAL_UART_STATE_READY; 00350 huart->RxState= HAL_UART_STATE_READY; 00351 00352 return HAL_OK; 00353 } 00354 00355 /** 00356 * @brief Initializes the LIN mode according to the specified 00357 * parameters in the UART_InitTypeDef and create the associated handle. 00358 * @param huart pointer to a UART_HandleTypeDef structure that contains 00359 * the configuration information for the specified UART module. 00360 * @param BreakDetectLength Specifies the LIN break detection length. 00361 * This parameter can be one of the following values: 00362 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection 00363 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection 00364 * @retval HAL status 00365 */ 00366 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength) 00367 { 00368 /* Check the UART handle allocation */ 00369 if(huart == NULL) 00370 { 00371 return HAL_ERROR; 00372 } 00373 00374 /* Check the parameters */ 00375 assert_param(IS_UART_INSTANCE(huart->Instance)); 00376 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength)); 00377 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength)); 00378 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling)); 00379 00380 if(huart->gState == HAL_UART_STATE_RESET) 00381 { 00382 /* Allocate lock resource and initialize it */ 00383 huart->Lock = HAL_UNLOCKED; 00384 /* Init the low level hardware */ 00385 HAL_UART_MspInit(huart); 00386 } 00387 00388 huart->gState = HAL_UART_STATE_BUSY; 00389 00390 /* Disable the peripheral */ 00391 __HAL_UART_DISABLE(huart); 00392 00393 /* Set the UART Communication parameters */ 00394 UART_SetConfig(huart); 00395 00396 /* In LIN mode, the following bits must be kept cleared: 00397 - LINEN and CLKEN bits in the USART_CR2 register, 00398 - SCEN and IREN bits in the USART_CR3 register.*/ 00399 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN); 00400 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN)); 00401 00402 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */ 00403 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN); 00404 00405 /* Set the USART LIN Break detection length. */ 00406 CLEAR_BIT(huart->Instance->CR2, USART_CR2_LBDL); 00407 SET_BIT(huart->Instance->CR2, BreakDetectLength); 00408 00409 /* Enable the peripheral */ 00410 __HAL_UART_ENABLE(huart); 00411 00412 /* Initialize the UART state*/ 00413 huart->ErrorCode = HAL_UART_ERROR_NONE; 00414 huart->gState= HAL_UART_STATE_READY; 00415 huart->RxState= HAL_UART_STATE_READY; 00416 00417 return HAL_OK; 00418 } 00419 00420 /** 00421 * @brief Initializes the Multi-Processor mode according to the specified 00422 * parameters in the UART_InitTypeDef and create the associated handle. 00423 * @param huart pointer to a UART_HandleTypeDef structure that contains 00424 * the configuration information for the specified UART module. 00425 * @param Address USART address 00426 * @param WakeUpMethod specifies the USART wake-up method. 00427 * This parameter can be one of the following values: 00428 * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection 00429 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark 00430 * @retval HAL status 00431 */ 00432 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod) 00433 { 00434 /* Check the UART handle allocation */ 00435 if(huart == NULL) 00436 { 00437 return HAL_ERROR; 00438 } 00439 00440 /* Check the parameters */ 00441 assert_param(IS_UART_INSTANCE(huart->Instance)); 00442 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod)); 00443 assert_param(IS_UART_ADDRESS(Address)); 00444 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00445 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00446 00447 if(huart->gState == HAL_UART_STATE_RESET) 00448 { 00449 /* Allocate lock resource and initialize it */ 00450 huart->Lock = HAL_UNLOCKED; 00451 /* Init the low level hardware */ 00452 HAL_UART_MspInit(huart); 00453 } 00454 00455 huart->gState = HAL_UART_STATE_BUSY; 00456 00457 /* Disable the peripheral */ 00458 __HAL_UART_DISABLE(huart); 00459 00460 /* Set the UART Communication parameters */ 00461 UART_SetConfig(huart); 00462 00463 /* In Multi-Processor mode, the following bits must be kept cleared: 00464 - LINEN and CLKEN bits in the USART_CR2 register, 00465 - SCEN, HDSEL and IREN bits in the USART_CR3 register */ 00466 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00467 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00468 00469 /* Clear the USART address */ 00470 CLEAR_BIT(huart->Instance->CR2, USART_CR2_ADD); 00471 /* Set the USART address node */ 00472 SET_BIT(huart->Instance->CR2, Address); 00473 00474 /* Set the wake up method by setting the WAKE bit in the CR1 register */ 00475 CLEAR_BIT(huart->Instance->CR1, USART_CR1_WAKE); 00476 SET_BIT(huart->Instance->CR1, WakeUpMethod); 00477 00478 /* Enable the peripheral */ 00479 __HAL_UART_ENABLE(huart); 00480 00481 /* Initialize the UART state */ 00482 huart->ErrorCode = HAL_UART_ERROR_NONE; 00483 huart->gState= HAL_UART_STATE_READY; 00484 huart->RxState= HAL_UART_STATE_READY; 00485 00486 return HAL_OK; 00487 } 00488 00489 /** 00490 * @brief DeInitializes the UART peripheral. 00491 * @param huart pointer to a UART_HandleTypeDef structure that contains 00492 * the configuration information for the specified UART module. 00493 * @retval HAL status 00494 */ 00495 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) 00496 { 00497 /* Check the UART handle allocation */ 00498 if(huart == NULL) 00499 { 00500 return HAL_ERROR; 00501 } 00502 00503 /* Check the parameters */ 00504 assert_param(IS_UART_INSTANCE(huart->Instance)); 00505 00506 huart->gState = HAL_UART_STATE_BUSY; 00507 00508 /* DeInit the low level hardware */ 00509 HAL_UART_MspDeInit(huart); 00510 00511 huart->ErrorCode = HAL_UART_ERROR_NONE; 00512 huart->gState = HAL_UART_STATE_RESET; 00513 huart->RxState = HAL_UART_STATE_RESET; 00514 00515 /* Process Lock */ 00516 __HAL_UNLOCK(huart); 00517 00518 return HAL_OK; 00519 } 00520 00521 /** 00522 * @brief UART MSP Init. 00523 * @param huart pointer to a UART_HandleTypeDef structure that contains 00524 * the configuration information for the specified UART module. 00525 * @retval None 00526 */ 00527 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) 00528 { 00529 /* Prevent unused argument(s) compilation warning */ 00530 UNUSED(huart); 00531 /* NOTE: This function Should not be modified, when the callback is needed, 00532 the HAL_UART_MspInit could be implemented in the user file 00533 */ 00534 } 00535 00536 /** 00537 * @brief UART MSP DeInit. 00538 * @param huart pointer to a UART_HandleTypeDef structure that contains 00539 * the configuration information for the specified UART module. 00540 * @retval None 00541 */ 00542 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) 00543 { 00544 /* Prevent unused argument(s) compilation warning */ 00545 UNUSED(huart); 00546 /* NOTE: This function Should not be modified, when the callback is needed, 00547 the HAL_UART_MspDeInit could be implemented in the user file 00548 */ 00549 } 00550 00551 /** 00552 * @} 00553 */ 00554 00555 /** @defgroup UART_Exported_Functions_Group2 IO operation functions 00556 * @brief UART Transmit and Receive functions 00557 * 00558 @verbatim 00559 ============================================================================== 00560 ##### IO operation functions ##### 00561 ============================================================================== 00562 [..] 00563 This subsection provides a set of functions allowing to manage the UART asynchronous 00564 and Half duplex data transfers. 00565 00566 (#) There are two modes of transfer: 00567 (++) Blocking mode: The communication is performed in polling mode. 00568 The HAL status of all data processing is returned by the same function 00569 after finishing transfer. 00570 (++) Non blocking mode: The communication is performed using Interrupts 00571 or DMA, these APIs return the HAL status. 00572 The end of the data processing will be indicated through the 00573 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when 00574 using DMA mode. 00575 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks 00576 will be executed respectively at the end of the transmit or receive process. 00577 The HAL_UART_ErrorCallback() user callback will be executed when 00578 a communication error is detected. 00579 00580 (#) Blocking mode APIs are: 00581 (++) HAL_UART_Transmit() 00582 (++) HAL_UART_Receive() 00583 00584 (#) Non Blocking mode APIs with Interrupt are: 00585 (++) HAL_UART_Transmit_IT() 00586 (++) HAL_UART_Receive_IT() 00587 (++) HAL_UART_IRQHandler() 00588 00589 (#) Non Blocking mode functions with DMA are: 00590 (++) HAL_UART_Transmit_DMA() 00591 (++) HAL_UART_Receive_DMA() 00592 00593 (#) A set of Transfer Complete Callbacks are provided in non blocking mode: 00594 (++) HAL_UART_TxCpltCallback() 00595 (++) HAL_UART_RxCpltCallback() 00596 (++) HAL_UART_ErrorCallback() 00597 00598 [..] 00599 (@) In the Half duplex communication, it is forbidden to run the transmit 00600 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX 00601 can't be useful. 00602 00603 @endverbatim 00604 * @{ 00605 */ 00606 00607 /** 00608 * @brief Sends an amount of data in blocking mode. 00609 * @param huart pointer to a UART_HandleTypeDef structure that contains 00610 * the configuration information for the specified UART module. 00611 * @param pData Pointer to data buffer 00612 * @param Size Amount of data to be sent 00613 * @param Timeout Timeout duration 00614 * @retval HAL status 00615 */ 00616 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 00617 { 00618 uint16_t* tmp; 00619 uint32_t tickstart = 0U; 00620 00621 /* Check that a Tx process is not already ongoing */ 00622 if(huart->gState == HAL_UART_STATE_READY) 00623 { 00624 if((pData == NULL ) || (Size == 0)) 00625 { 00626 return HAL_ERROR; 00627 } 00628 00629 /* Process Locked */ 00630 __HAL_LOCK(huart); 00631 00632 huart->ErrorCode = HAL_UART_ERROR_NONE; 00633 huart->gState = HAL_UART_STATE_BUSY_TX; 00634 00635 /* Init tickstart for timeout managment */ 00636 tickstart = HAL_GetTick(); 00637 00638 huart->TxXferSize = Size; 00639 huart->TxXferCount = Size; 00640 while(huart->TxXferCount > 0U) 00641 { 00642 huart->TxXferCount--; 00643 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 00644 { 00645 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 00646 { 00647 return HAL_TIMEOUT; 00648 } 00649 tmp = (uint16_t*) pData; 00650 huart->Instance->DR = (*tmp & (uint16_t)0x01FF); 00651 if(huart->Init.Parity == UART_PARITY_NONE) 00652 { 00653 pData +=2U; 00654 } 00655 else 00656 { 00657 pData +=1U; 00658 } 00659 } 00660 else 00661 { 00662 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK) 00663 { 00664 return HAL_TIMEOUT; 00665 } 00666 huart->Instance->DR = (*pData++ & (uint8_t)0xFF); 00667 } 00668 } 00669 00670 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK) 00671 { 00672 return HAL_TIMEOUT; 00673 } 00674 00675 /* At end of Tx process, restore huart->gState to Ready */ 00676 huart->gState = HAL_UART_STATE_READY; 00677 00678 /* Process Unlocked */ 00679 __HAL_UNLOCK(huart); 00680 00681 return HAL_OK; 00682 } 00683 else 00684 { 00685 return HAL_BUSY; 00686 } 00687 } 00688 00689 /** 00690 * @brief Receives an amount of data in blocking mode. 00691 * @param huart pointer to a UART_HandleTypeDef structure that contains 00692 * the configuration information for the specified UART module. 00693 * @param pData Pointer to data buffer 00694 * @param Size Amount of data to be received 00695 * @param Timeout Timeout duration 00696 * @retval HAL status 00697 */ 00698 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 00699 { 00700 uint16_t* tmp; 00701 uint32_t tickstart = 0U; 00702 00703 /* Check that a Rx process is not already ongoing */ 00704 if(huart->RxState == HAL_UART_STATE_READY) 00705 { 00706 if((pData == NULL ) || (Size == 0)) 00707 { 00708 return HAL_ERROR; 00709 } 00710 00711 /* Process Locked */ 00712 __HAL_LOCK(huart); 00713 00714 huart->ErrorCode = HAL_UART_ERROR_NONE; 00715 huart->RxState = HAL_UART_STATE_BUSY_RX; 00716 00717 /* Init tickstart for timeout managment */ 00718 tickstart = HAL_GetTick(); 00719 00720 huart->RxXferSize = Size; 00721 huart->RxXferCount = Size; 00722 00723 /* Check the remain data to be received */ 00724 while(huart->RxXferCount > 0U) 00725 { 00726 huart->RxXferCount--; 00727 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 00728 { 00729 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 00730 { 00731 return HAL_TIMEOUT; 00732 } 00733 tmp = (uint16_t*) pData; 00734 if(huart->Init.Parity == UART_PARITY_NONE) 00735 { 00736 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); 00737 pData +=2U; 00738 } 00739 else 00740 { 00741 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF); 00742 pData +=1U; 00743 } 00744 00745 } 00746 else 00747 { 00748 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK) 00749 { 00750 return HAL_TIMEOUT; 00751 } 00752 if(huart->Init.Parity == UART_PARITY_NONE) 00753 { 00754 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 00755 } 00756 else 00757 { 00758 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 00759 } 00760 00761 } 00762 } 00763 00764 /* At end of Rx process, restore huart->RxState to Ready */ 00765 huart->RxState = HAL_UART_STATE_READY; 00766 00767 /* Process Unlocked */ 00768 __HAL_UNLOCK(huart); 00769 00770 return HAL_OK; 00771 } 00772 else 00773 { 00774 return HAL_BUSY; 00775 } 00776 } 00777 00778 /** 00779 * @brief Sends an amount of data in non blocking mode. 00780 * @param huart pointer to a UART_HandleTypeDef structure that contains 00781 * the configuration information for the specified UART module. 00782 * @param pData Pointer to data buffer 00783 * @param Size Amount of data to be sent 00784 * @retval HAL status 00785 */ 00786 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00787 { 00788 /* Check that a Tx process is not already ongoing */ 00789 if(huart->gState == HAL_UART_STATE_READY) 00790 { 00791 if((pData == NULL ) || (Size == 0)) 00792 { 00793 return HAL_ERROR; 00794 } 00795 00796 /* Process Locked */ 00797 __HAL_LOCK(huart); 00798 00799 huart->pTxBuffPtr = pData; 00800 huart->TxXferSize = Size; 00801 huart->TxXferCount = Size; 00802 00803 huart->ErrorCode = HAL_UART_ERROR_NONE; 00804 huart->gState = HAL_UART_STATE_BUSY_TX; 00805 00806 /* Process Unlocked */ 00807 __HAL_UNLOCK(huart); 00808 00809 /* Enable the UART Transmit data register empty Interrupt */ 00810 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE); 00811 00812 return HAL_OK; 00813 } 00814 else 00815 { 00816 return HAL_BUSY; 00817 } 00818 } 00819 00820 /** 00821 * @brief Receives an amount of data in non blocking mode 00822 * @param huart pointer to a UART_HandleTypeDef structure that contains 00823 * the configuration information for the specified UART module. 00824 * @param pData Pointer to data buffer 00825 * @param Size Amount of data to be received 00826 * @retval HAL status 00827 */ 00828 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00829 { 00830 /* Check that a Rx process is not already ongoing */ 00831 if(huart->RxState == HAL_UART_STATE_READY) 00832 { 00833 if((pData == NULL ) || (Size == 0)) 00834 { 00835 return HAL_ERROR; 00836 } 00837 00838 /* Process Locked */ 00839 __HAL_LOCK(huart); 00840 00841 huart->pRxBuffPtr = pData; 00842 huart->RxXferSize = Size; 00843 huart->RxXferCount = Size; 00844 00845 huart->ErrorCode = HAL_UART_ERROR_NONE; 00846 huart->RxState = HAL_UART_STATE_BUSY_RX; 00847 00848 /* Process Unlocked */ 00849 __HAL_UNLOCK(huart); 00850 00851 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 00852 SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 00853 00854 /* Enable the UART Parity Error and Data Register not empty Interrupts */ 00855 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE); 00856 00857 return HAL_OK; 00858 } 00859 else 00860 { 00861 return HAL_BUSY; 00862 } 00863 } 00864 00865 /** 00866 * @brief Sends an amount of data in non blocking mode. 00867 * @param huart pointer to a UART_HandleTypeDef structure that contains 00868 * the configuration information for the specified UART module. 00869 * @param pData Pointer to data buffer 00870 * @param Size Amount of data to be sent 00871 * @retval HAL status 00872 */ 00873 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00874 { 00875 uint32_t *tmp; 00876 00877 /* Check that a Tx process is not already ongoing */ 00878 if(huart->gState == HAL_UART_STATE_READY) 00879 { 00880 if((pData == NULL ) || (Size == 0)) 00881 { 00882 return HAL_ERROR; 00883 } 00884 00885 /* Process Locked */ 00886 __HAL_LOCK(huart); 00887 00888 huart->pTxBuffPtr = pData; 00889 huart->TxXferSize = Size; 00890 huart->TxXferCount = Size; 00891 00892 huart->ErrorCode = HAL_UART_ERROR_NONE; 00893 huart->gState = HAL_UART_STATE_BUSY_TX; 00894 00895 /* Set the UART DMA transfer complete callback */ 00896 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; 00897 00898 /* Set the UART DMA Half transfer complete callback */ 00899 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; 00900 00901 /* Set the DMA error callback */ 00902 huart->hdmatx->XferErrorCallback = UART_DMAError; 00903 00904 /* Set the DMA abort callback */ 00905 huart->hdmatx->XferAbortCallback = NULL; 00906 00907 /* Enable the UART transmit DMA Stream */ 00908 tmp = (uint32_t*)&pData; 00909 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size); 00910 00911 /* Clear the TC flag in the SR register by writing 0 to it */ 00912 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); 00913 00914 /* Process Unlocked */ 00915 __HAL_UNLOCK(huart); 00916 00917 /* Enable the DMA transfer for transmit request by setting the DMAT bit 00918 in the UART CR3 register */ 00919 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 00920 00921 return HAL_OK; 00922 } 00923 else 00924 { 00925 return HAL_BUSY; 00926 } 00927 } 00928 00929 /** 00930 * @brief Receives an amount of data in non blocking mode. 00931 * @param huart pointer to a UART_HandleTypeDef structure that contains 00932 * the configuration information for the specified UART module. 00933 * @param pData Pointer to data buffer 00934 * @param Size Amount of data to be received 00935 * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit. 00936 * @retval HAL status 00937 */ 00938 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00939 { 00940 uint32_t *tmp; 00941 00942 /* Check that a Rx process is not already ongoing */ 00943 if(huart->RxState == HAL_UART_STATE_READY) 00944 { 00945 if((pData == NULL ) || (Size == 0)) 00946 { 00947 return HAL_ERROR; 00948 } 00949 00950 /* Process Locked */ 00951 __HAL_LOCK(huart); 00952 00953 huart->pRxBuffPtr = pData; 00954 huart->RxXferSize = Size; 00955 00956 huart->ErrorCode = HAL_UART_ERROR_NONE; 00957 huart->RxState = HAL_UART_STATE_BUSY_RX; 00958 00959 /* Set the UART DMA transfer complete callback */ 00960 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; 00961 00962 /* Set the UART DMA Half transfer complete callback */ 00963 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; 00964 00965 /* Set the DMA error callback */ 00966 huart->hdmarx->XferErrorCallback = UART_DMAError; 00967 00968 /* Set the DMA abort callback */ 00969 huart->hdmarx->XferAbortCallback = NULL; 00970 00971 /* Enable the DMA Stream */ 00972 tmp = (uint32_t*)&pData; 00973 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size); 00974 00975 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */ 00976 __HAL_UART_CLEAR_OREFLAG(huart); 00977 00978 /* Process Unlocked */ 00979 __HAL_UNLOCK(huart); 00980 00981 /* Enable the UART Parity Error Interrupt */ 00982 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 00983 00984 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 00985 SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 00986 00987 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 00988 in the UART CR3 register */ 00989 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 00990 00991 return HAL_OK; 00992 } 00993 else 00994 { 00995 return HAL_BUSY; 00996 } 00997 } 00998 00999 /** 01000 * @brief Pauses the DMA Transfer. 01001 * @param huart pointer to a UART_HandleTypeDef structure that contains 01002 * the configuration information for the specified UART module. 01003 * @retval HAL status 01004 */ 01005 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) 01006 { 01007 uint32_t dmarequest = 0x00U; 01008 01009 /* Process Locked */ 01010 __HAL_LOCK(huart); 01011 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 01012 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 01013 { 01014 /* Disable the UART DMA Tx request */ 01015 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01016 } 01017 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 01018 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 01019 { 01020 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01021 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01022 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01023 01024 /* Disable the UART DMA Rx request */ 01025 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01026 } 01027 01028 /* Process Unlocked */ 01029 __HAL_UNLOCK(huart); 01030 01031 return HAL_OK; 01032 } 01033 01034 /** 01035 * @brief Resumes the DMA Transfer. 01036 * @param huart pointer to a UART_HandleTypeDef structure that contains 01037 * the configuration information for the specified UART module. 01038 * @retval HAL status 01039 */ 01040 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) 01041 { 01042 /* Process Locked */ 01043 __HAL_LOCK(huart); 01044 01045 if(huart->gState == HAL_UART_STATE_BUSY_TX) 01046 { 01047 /* Enable the UART DMA Tx request */ 01048 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01049 } 01050 if(huart->RxState == HAL_UART_STATE_BUSY_RX) 01051 { 01052 /* Clear the Overrun flag before resuming the Rx transfer*/ 01053 __HAL_UART_CLEAR_OREFLAG(huart); 01054 01055 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */ 01056 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE); 01057 SET_BIT(huart->Instance->CR3, USART_CR3_EIE); 01058 01059 /* Enable the UART DMA Rx request */ 01060 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01061 } 01062 01063 /* Process Unlocked */ 01064 __HAL_UNLOCK(huart); 01065 01066 return HAL_OK; 01067 } 01068 01069 /** 01070 * @brief Stops the DMA Transfer. 01071 * @param huart pointer to a UART_HandleTypeDef structure that contains 01072 * the configuration information for the specified UART module. 01073 * @retval HAL status 01074 */ 01075 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) 01076 { 01077 uint32_t dmarequest = 0x00U; 01078 /* The Lock is not implemented on this API to allow the user application 01079 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback(): 01080 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated 01081 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() 01082 */ 01083 01084 /* Stop UART DMA Tx request if ongoing */ 01085 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 01086 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 01087 { 01088 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01089 01090 /* Abort the UART DMA Tx channel */ 01091 if(huart->hdmatx != NULL) 01092 { 01093 HAL_DMA_Abort(huart->hdmatx); 01094 } 01095 UART_EndTxTransfer(huart); 01096 } 01097 01098 /* Stop UART DMA Rx request if ongoing */ 01099 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 01100 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 01101 { 01102 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01103 01104 /* Abort the UART DMA Rx channel */ 01105 if(huart->hdmarx != NULL) 01106 { 01107 HAL_DMA_Abort(huart->hdmarx); 01108 } 01109 UART_EndRxTransfer(huart); 01110 } 01111 01112 return HAL_OK; 01113 } 01114 01115 /** 01116 * @brief Abort ongoing transfers (blocking mode). 01117 * @param huart UART handle. 01118 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01119 * This procedure performs following operations : 01120 * - Disable PPP Interrupts 01121 * - Disable the DMA transfer in the peripheral register (if enabled) 01122 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01123 * - Set handle State to READY 01124 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01125 * @retval HAL status 01126 */ 01127 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart) 01128 { 01129 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01130 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); 01131 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01132 01133 /* Disable the UART DMA Tx request if enabled */ 01134 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01135 { 01136 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01137 01138 /* Abort the UART DMA Tx channel: use blocking DMA Abort API (no callback) */ 01139 if(huart->hdmatx != NULL) 01140 { 01141 /* Set the UART DMA Abort callback to Null. 01142 No call back execution at end of DMA abort procedure */ 01143 huart->hdmatx->XferAbortCallback = NULL; 01144 01145 HAL_DMA_Abort(huart->hdmatx); 01146 } 01147 } 01148 01149 /* Disable the UART DMA Rx request if enabled */ 01150 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01151 { 01152 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01153 01154 /* Abort the UART DMA Rx channel: use blocking DMA Abort API (no callback) */ 01155 if(huart->hdmarx != NULL) 01156 { 01157 /* Set the UART DMA Abort callback to Null. 01158 No call back execution at end of DMA abort procedure */ 01159 huart->hdmarx->XferAbortCallback = NULL; 01160 01161 HAL_DMA_Abort(huart->hdmarx); 01162 } 01163 } 01164 01165 /* Reset Tx and Rx transfer counters */ 01166 huart->TxXferCount = 0x00U; 01167 huart->RxXferCount = 0x00U; 01168 01169 /* Reset ErrorCode */ 01170 huart->ErrorCode = HAL_UART_ERROR_NONE; 01171 01172 /* Restore huart->RxState and huart->gState to Ready */ 01173 huart->RxState = HAL_UART_STATE_READY; 01174 huart->gState = HAL_UART_STATE_READY; 01175 01176 return HAL_OK; 01177 } 01178 01179 /** 01180 * @brief Abort ongoing Transmit transfer (blocking mode). 01181 * @param huart UART handle. 01182 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01183 * This procedure performs following operations : 01184 * - Disable PPP Interrupts 01185 * - Disable the DMA transfer in the peripheral register (if enabled) 01186 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01187 * - Set handle State to READY 01188 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01189 * @retval HAL status 01190 */ 01191 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart) 01192 { 01193 /* Disable TXEIE and TCIE interrupts */ 01194 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 01195 01196 /* Disable the UART DMA Tx request if enabled */ 01197 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01198 { 01199 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01200 01201 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */ 01202 if(huart->hdmatx != NULL) 01203 { 01204 /* Set the UART DMA Abort callback to Null. 01205 No call back execution at end of DMA abort procedure */ 01206 huart->hdmatx->XferAbortCallback = NULL; 01207 01208 HAL_DMA_Abort(huart->hdmatx); 01209 } 01210 } 01211 01212 /* Reset Tx transfer counter */ 01213 huart->TxXferCount = 0x00U; 01214 01215 /* Restore huart->gState to Ready */ 01216 huart->gState = HAL_UART_STATE_READY; 01217 01218 return HAL_OK; 01219 } 01220 01221 /** 01222 * @brief Abort ongoing Receive transfer (blocking mode). 01223 * @param huart UART handle. 01224 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01225 * This procedure performs following operations : 01226 * - Disable PPP Interrupts 01227 * - Disable the DMA transfer in the peripheral register (if enabled) 01228 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode) 01229 * - Set handle State to READY 01230 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed. 01231 * @retval HAL status 01232 */ 01233 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart) 01234 { 01235 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01236 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 01237 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01238 01239 /* Disable the UART DMA Rx request if enabled */ 01240 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01241 { 01242 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01243 01244 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */ 01245 if(huart->hdmarx != NULL) 01246 { 01247 /* Set the UART DMA Abort callback to Null. 01248 No call back execution at end of DMA abort procedure */ 01249 huart->hdmarx->XferAbortCallback = NULL; 01250 01251 HAL_DMA_Abort(huart->hdmarx); 01252 } 01253 } 01254 01255 /* Reset Rx transfer counter */ 01256 huart->RxXferCount = 0x00U; 01257 01258 /* Restore huart->RxState to Ready */ 01259 huart->RxState = HAL_UART_STATE_READY; 01260 01261 return HAL_OK; 01262 } 01263 01264 /** 01265 * @brief Abort ongoing transfers (Interrupt mode). 01266 * @param huart UART handle. 01267 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01268 * This procedure performs following operations : 01269 * - Disable PPP Interrupts 01270 * - Disable the DMA transfer in the peripheral register (if enabled) 01271 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01272 * - Set handle State to READY 01273 * - At abort completion, call user abort complete callback 01274 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01275 * considered as completed only when user abort complete callback is executed (not when exiting function). 01276 * @retval HAL status 01277 */ 01278 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart) 01279 { 01280 uint32_t AbortCplt = 0x01U; 01281 01282 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01283 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE)); 01284 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01285 01286 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised 01287 before any call to DMA Abort functions */ 01288 /* DMA Tx Handle is valid */ 01289 if(huart->hdmatx != NULL) 01290 { 01291 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled. 01292 Otherwise, set it to NULL */ 01293 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01294 { 01295 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback; 01296 } 01297 else 01298 { 01299 huart->hdmatx->XferAbortCallback = NULL; 01300 } 01301 } 01302 /* DMA Rx Handle is valid */ 01303 if(huart->hdmarx != NULL) 01304 { 01305 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled. 01306 Otherwise, set it to NULL */ 01307 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01308 { 01309 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback; 01310 } 01311 else 01312 { 01313 huart->hdmarx->XferAbortCallback = NULL; 01314 } 01315 } 01316 01317 /* Disable the UART DMA Tx request if enabled */ 01318 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01319 { 01320 /* Disable DMA Tx at UART level */ 01321 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01322 01323 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */ 01324 if(huart->hdmatx != NULL) 01325 { 01326 /* UART Tx DMA Abort callback has already been initialised : 01327 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01328 01329 /* Abort DMA TX */ 01330 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK) 01331 { 01332 huart->hdmatx->XferAbortCallback = NULL; 01333 } 01334 else 01335 { 01336 AbortCplt = 0x00U; 01337 } 01338 } 01339 } 01340 01341 /* Disable the UART DMA Rx request if enabled */ 01342 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01343 { 01344 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01345 01346 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */ 01347 if(huart->hdmarx != NULL) 01348 { 01349 /* UART Rx DMA Abort callback has already been initialised : 01350 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01351 01352 /* Abort DMA RX */ 01353 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 01354 { 01355 huart->hdmarx->XferAbortCallback = NULL; 01356 AbortCplt = 0x01U; 01357 } 01358 else 01359 { 01360 AbortCplt = 0x00U; 01361 } 01362 } 01363 } 01364 01365 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */ 01366 if(AbortCplt == 0x01U) 01367 { 01368 /* Reset Tx and Rx transfer counters */ 01369 huart->TxXferCount = 0x00U; 01370 huart->RxXferCount = 0x00U; 01371 01372 /* Reset ErrorCode */ 01373 huart->ErrorCode = HAL_UART_ERROR_NONE; 01374 01375 /* Restore huart->gState and huart->RxState to Ready */ 01376 huart->gState = HAL_UART_STATE_READY; 01377 huart->RxState = HAL_UART_STATE_READY; 01378 01379 /* As no DMA to be aborted, call directly user Abort complete callback */ 01380 HAL_UART_AbortCpltCallback(huart); 01381 } 01382 01383 return HAL_OK; 01384 } 01385 01386 /** 01387 * @brief Abort ongoing Transmit transfer (Interrupt mode). 01388 * @param huart UART handle. 01389 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01390 * This procedure performs following operations : 01391 * - Disable PPP Interrupts 01392 * - Disable the DMA transfer in the peripheral register (if enabled) 01393 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01394 * - Set handle State to READY 01395 * - At abort completion, call user abort complete callback 01396 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01397 * considered as completed only when user abort complete callback is executed (not when exiting function). 01398 * @retval HAL status 01399 */ 01400 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart) 01401 { 01402 /* Disable TXEIE and TCIE interrupts */ 01403 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 01404 01405 /* Disable the UART DMA Tx request if enabled */ 01406 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) 01407 { 01408 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01409 01410 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */ 01411 if(huart->hdmatx != NULL) 01412 { 01413 /* Set the UART DMA Abort callback : 01414 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01415 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback; 01416 01417 /* Abort DMA TX */ 01418 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK) 01419 { 01420 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */ 01421 huart->hdmatx->XferAbortCallback(huart->hdmatx); 01422 } 01423 } 01424 else 01425 { 01426 /* Reset Tx transfer counter */ 01427 huart->TxXferCount = 0x00U; 01428 01429 /* Restore huart->gState to Ready */ 01430 huart->gState = HAL_UART_STATE_READY; 01431 01432 /* As no DMA to be aborted, call directly user Abort complete callback */ 01433 HAL_UART_AbortTransmitCpltCallback(huart); 01434 } 01435 } 01436 else 01437 { 01438 /* Reset Tx transfer counter */ 01439 huart->TxXferCount = 0x00U; 01440 01441 /* Restore huart->gState to Ready */ 01442 huart->gState = HAL_UART_STATE_READY; 01443 01444 /* As no DMA to be aborted, call directly user Abort complete callback */ 01445 HAL_UART_AbortTransmitCpltCallback(huart); 01446 } 01447 01448 return HAL_OK; 01449 } 01450 01451 /** 01452 * @brief Abort ongoing Receive transfer (Interrupt mode). 01453 * @param huart UART handle. 01454 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode. 01455 * This procedure performs following operations : 01456 * - Disable PPP Interrupts 01457 * - Disable the DMA transfer in the peripheral register (if enabled) 01458 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode) 01459 * - Set handle State to READY 01460 * - At abort completion, call user abort complete callback 01461 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be 01462 * considered as completed only when user abort complete callback is executed (not when exiting function). 01463 * @retval HAL status 01464 */ 01465 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart) 01466 { 01467 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 01468 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 01469 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 01470 01471 /* Disable the UART DMA Rx request if enabled */ 01472 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01473 { 01474 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01475 01476 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */ 01477 if(huart->hdmarx != NULL) 01478 { 01479 /* Set the UART DMA Abort callback : 01480 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */ 01481 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback; 01482 01483 /* Abort DMA RX */ 01484 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 01485 { 01486 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */ 01487 huart->hdmarx->XferAbortCallback(huart->hdmarx); 01488 } 01489 } 01490 else 01491 { 01492 /* Reset Rx transfer counter */ 01493 huart->RxXferCount = 0x00U; 01494 01495 /* Restore huart->RxState to Ready */ 01496 huart->RxState = HAL_UART_STATE_READY; 01497 01498 /* As no DMA to be aborted, call directly user Abort complete callback */ 01499 HAL_UART_AbortReceiveCpltCallback(huart); 01500 } 01501 } 01502 else 01503 { 01504 /* Reset Rx transfer counter */ 01505 huart->RxXferCount = 0x00U; 01506 01507 /* Restore huart->RxState to Ready */ 01508 huart->RxState = HAL_UART_STATE_READY; 01509 01510 /* As no DMA to be aborted, call directly user Abort complete callback */ 01511 HAL_UART_AbortReceiveCpltCallback(huart); 01512 } 01513 01514 return HAL_OK; 01515 } 01516 01517 /** 01518 * @brief This function handles UART interrupt request. 01519 * @param huart pointer to a UART_HandleTypeDef structure that contains 01520 * the configuration information for the specified UART module. 01521 * @retval None 01522 */ 01523 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) 01524 { 01525 uint32_t isrflags = READ_REG(huart->Instance->SR); 01526 uint32_t cr1its = READ_REG(huart->Instance->CR1); 01527 uint32_t cr3its = READ_REG(huart->Instance->CR3); 01528 uint32_t errorflags = 0x00U; 01529 uint32_t dmarequest = 0x00U; 01530 01531 /* If no error occurs */ 01532 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE)); 01533 if(errorflags == RESET) 01534 { 01535 /* UART in mode Receiver -------------------------------------------------*/ 01536 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) 01537 { 01538 UART_Receive_IT(huart); 01539 return; 01540 } 01541 } 01542 01543 /* If some errors occur */ 01544 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET))) 01545 { 01546 /* UART parity error interrupt occurred ----------------------------------*/ 01547 if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET)) 01548 { 01549 huart->ErrorCode |= HAL_UART_ERROR_PE; 01550 } 01551 01552 /* UART noise error interrupt occurred -----------------------------------*/ 01553 if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) 01554 { 01555 huart->ErrorCode |= HAL_UART_ERROR_NE; 01556 } 01557 01558 /* UART frame error interrupt occurred -----------------------------------*/ 01559 if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) 01560 { 01561 huart->ErrorCode |= HAL_UART_ERROR_FE; 01562 } 01563 01564 /* UART Over-Run interrupt occurred --------------------------------------*/ 01565 if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET)) 01566 { 01567 huart->ErrorCode |= HAL_UART_ERROR_ORE; 01568 } 01569 01570 /* Call UART Error Call back function if need be --------------------------*/ 01571 if(huart->ErrorCode != HAL_UART_ERROR_NONE) 01572 { 01573 /* UART in mode Receiver -----------------------------------------------*/ 01574 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET)) 01575 { 01576 UART_Receive_IT(huart); 01577 } 01578 01579 /* If Overrun error occurs, or if any error occurs in DMA mode reception, 01580 consider error as blocking */ 01581 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 01582 if(((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest) 01583 { 01584 /* Blocking error : transfer is aborted 01585 Set the UART state ready to be able to start again the process, 01586 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */ 01587 UART_EndRxTransfer(huart); 01588 01589 /* Disable the UART DMA Rx request if enabled */ 01590 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) 01591 { 01592 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01593 01594 /* Abort the UART DMA Rx channel */ 01595 if(huart->hdmarx != NULL) 01596 { 01597 /* Set the UART DMA Abort callback : 01598 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */ 01599 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError; 01600 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK) 01601 { 01602 /* Call Directly XferAbortCallback function in case of error */ 01603 huart->hdmarx->XferAbortCallback(huart->hdmarx); 01604 } 01605 } 01606 else 01607 { 01608 /* Call user error callback */ 01609 HAL_UART_ErrorCallback(huart); 01610 } 01611 } 01612 else 01613 { 01614 /* Call user error callback */ 01615 HAL_UART_ErrorCallback(huart); 01616 } 01617 } 01618 else 01619 { 01620 /* Non Blocking error : transfer could go on. 01621 Error is notified to user through user error callback */ 01622 HAL_UART_ErrorCallback(huart); 01623 huart->ErrorCode = HAL_UART_ERROR_NONE; 01624 } 01625 } 01626 return; 01627 } /* End if some error occurs */ 01628 01629 /* UART in mode Transmitter ------------------------------------------------*/ 01630 if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET)) 01631 { 01632 UART_Transmit_IT(huart); 01633 return; 01634 } 01635 01636 /* UART in mode Transmitter end --------------------------------------------*/ 01637 if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) 01638 { 01639 UART_EndTransmit_IT(huart); 01640 return; 01641 } 01642 } 01643 01644 /** 01645 * @brief Tx Transfer completed callbacks. 01646 * @param huart pointer to a UART_HandleTypeDef structure that contains 01647 * the configuration information for the specified UART module. 01648 * @retval None 01649 */ 01650 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 01651 { 01652 /* Prevent unused argument(s) compilation warning */ 01653 UNUSED(huart); 01654 /* NOTE: This function Should not be modified, when the callback is needed, 01655 the HAL_UART_TxCpltCallback could be implemented in the user file 01656 */ 01657 } 01658 01659 /** 01660 * @brief Tx Half Transfer completed callbacks. 01661 * @param huart pointer to a UART_HandleTypeDef structure that contains 01662 * the configuration information for the specified UART module. 01663 * @retval None 01664 */ 01665 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) 01666 { 01667 /* Prevent unused argument(s) compilation warning */ 01668 UNUSED(huart); 01669 /* NOTE: This function Should not be modified, when the callback is needed, 01670 the HAL_UART_TxCpltCallback could be implemented in the user file 01671 */ 01672 } 01673 01674 /** 01675 * @brief Rx Transfer completed callbacks. 01676 * @param huart pointer to a UART_HandleTypeDef structure that contains 01677 * the configuration information for the specified UART module. 01678 * @retval None 01679 */ 01680 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 01681 { 01682 /* Prevent unused argument(s) compilation warning */ 01683 UNUSED(huart); 01684 /* NOTE: This function Should not be modified, when the callback is needed, 01685 the HAL_UART_TxCpltCallback could be implemented in the user file 01686 */ 01687 } 01688 01689 /** 01690 * @brief Rx Half Transfer completed callbacks. 01691 * @param huart pointer to a UART_HandleTypeDef structure that contains 01692 * the configuration information for the specified UART module. 01693 * @retval None 01694 */ 01695 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) 01696 { 01697 /* Prevent unused argument(s) compilation warning */ 01698 UNUSED(huart); 01699 /* NOTE: This function Should not be modified, when the callback is needed, 01700 the HAL_UART_TxCpltCallback could be implemented in the user file 01701 */ 01702 } 01703 01704 /** 01705 * @brief UART error callbacks. 01706 * @param huart pointer to a UART_HandleTypeDef structure that contains 01707 * the configuration information for the specified UART module. 01708 * @retval None 01709 */ 01710 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 01711 { 01712 /* Prevent unused argument(s) compilation warning */ 01713 UNUSED(huart); 01714 /* NOTE: This function Should not be modified, when the callback is needed, 01715 the HAL_UART_ErrorCallback could be implemented in the user file 01716 */ 01717 } 01718 01719 /** 01720 * @brief UART Abort Complete callback. 01721 * @param huart UART handle. 01722 * @retval None 01723 */ 01724 __weak void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart) 01725 { 01726 /* Prevent unused argument(s) compilation warning */ 01727 UNUSED(huart); 01728 01729 /* NOTE : This function should not be modified, when the callback is needed, 01730 the HAL_UART_AbortCpltCallback can be implemented in the user file. 01731 */ 01732 } 01733 /** 01734 * @brief UART Abort Complete callback. 01735 * @param huart UART handle. 01736 * @retval None 01737 */ 01738 __weak void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart) 01739 { 01740 /* Prevent unused argument(s) compilation warning */ 01741 UNUSED(huart); 01742 01743 /* NOTE : This function should not be modified, when the callback is needed, 01744 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file. 01745 */ 01746 } 01747 01748 /** 01749 * @brief UART Abort Receive Complete callback. 01750 * @param huart UART handle. 01751 * @retval None 01752 */ 01753 __weak void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart) 01754 { 01755 /* Prevent unused argument(s) compilation warning */ 01756 UNUSED(huart); 01757 01758 /* NOTE : This function should not be modified, when the callback is needed, 01759 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file. 01760 */ 01761 } 01762 01763 /** 01764 * @} 01765 */ 01766 01767 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions 01768 * @brief UART control functions 01769 * 01770 @verbatim 01771 ============================================================================== 01772 ##### Peripheral Control functions ##### 01773 ============================================================================== 01774 [..] 01775 This subsection provides a set of functions allowing to control the UART: 01776 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character. 01777 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode. 01778 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software. 01779 01780 @endverbatim 01781 * @{ 01782 */ 01783 01784 /** 01785 * @brief Transmits break characters. 01786 * @param huart pointer to a UART_HandleTypeDef structure that contains 01787 * the configuration information for the specified UART module. 01788 * @retval HAL status 01789 */ 01790 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) 01791 { 01792 /* Check the parameters */ 01793 assert_param(IS_UART_INSTANCE(huart->Instance)); 01794 01795 /* Process Locked */ 01796 __HAL_LOCK(huart); 01797 01798 huart->gState = HAL_UART_STATE_BUSY; 01799 01800 /* Send break characters */ 01801 SET_BIT(huart->Instance->CR1, USART_CR1_SBK); 01802 01803 huart->gState = HAL_UART_STATE_READY; 01804 01805 /* Process Unlocked */ 01806 __HAL_UNLOCK(huart); 01807 01808 return HAL_OK; 01809 } 01810 01811 /** 01812 * @brief Enters the UART in mute mode. 01813 * @param huart pointer to a UART_HandleTypeDef structure that contains 01814 * the configuration information for the specified UART module. 01815 * @retval HAL status 01816 */ 01817 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) 01818 { 01819 /* Check the parameters */ 01820 assert_param(IS_UART_INSTANCE(huart->Instance)); 01821 01822 /* Process Locked */ 01823 __HAL_LOCK(huart); 01824 01825 huart->gState = HAL_UART_STATE_BUSY; 01826 01827 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */ 01828 SET_BIT(huart->Instance->CR1, USART_CR1_RWU); 01829 01830 huart->gState = HAL_UART_STATE_READY; 01831 01832 /* Process Unlocked */ 01833 __HAL_UNLOCK(huart); 01834 01835 return HAL_OK; 01836 } 01837 01838 /** 01839 * @brief Exits the UART mute mode: wake up software. 01840 * @param huart pointer to a UART_HandleTypeDef structure that contains 01841 * the configuration information for the specified UART module. 01842 * @retval HAL status 01843 */ 01844 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart) 01845 { 01846 /* Check the parameters */ 01847 assert_param(IS_UART_INSTANCE(huart->Instance)); 01848 01849 /* Process Locked */ 01850 __HAL_LOCK(huart); 01851 01852 huart->gState = HAL_UART_STATE_BUSY; 01853 01854 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */ 01855 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU); 01856 01857 huart->gState = HAL_UART_STATE_READY; 01858 01859 /* Process Unlocked */ 01860 __HAL_UNLOCK(huart); 01861 01862 return HAL_OK; 01863 } 01864 01865 /** 01866 * @brief Enables the UART transmitter and disables the UART receiver. 01867 * @param huart pointer to a UART_HandleTypeDef structure that contains 01868 * the configuration information for the specified UART module. 01869 * @retval HAL status 01870 */ 01871 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) 01872 { 01873 uint32_t tmpreg = 0x00U; 01874 01875 /* Process Locked */ 01876 __HAL_LOCK(huart); 01877 01878 huart->gState = HAL_UART_STATE_BUSY; 01879 01880 /*-------------------------- USART CR1 Configuration -----------------------*/ 01881 tmpreg = huart->Instance->CR1; 01882 01883 /* Clear TE and RE bits */ 01884 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); 01885 01886 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */ 01887 tmpreg |= (uint32_t)USART_CR1_TE; 01888 01889 /* Write to USART CR1 */ 01890 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg); 01891 01892 huart->gState = HAL_UART_STATE_READY; 01893 01894 /* Process Unlocked */ 01895 __HAL_UNLOCK(huart); 01896 01897 return HAL_OK; 01898 } 01899 01900 /** 01901 * @brief Enables the UART receiver and disables the UART transmitter. 01902 * @param huart pointer to a UART_HandleTypeDef structure that contains 01903 * the configuration information for the specified UART module. 01904 * @retval HAL status 01905 */ 01906 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) 01907 { 01908 uint32_t tmpreg = 0x00U; 01909 01910 /* Process Locked */ 01911 __HAL_LOCK(huart); 01912 01913 huart->gState = HAL_UART_STATE_BUSY; 01914 01915 /*-------------------------- USART CR1 Configuration -----------------------*/ 01916 tmpreg = huart->Instance->CR1; 01917 01918 /* Clear TE and RE bits */ 01919 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE)); 01920 01921 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */ 01922 tmpreg |= (uint32_t)USART_CR1_RE; 01923 01924 /* Write to USART CR1 */ 01925 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg); 01926 01927 huart->gState = HAL_UART_STATE_READY; 01928 01929 /* Process Unlocked */ 01930 __HAL_UNLOCK(huart); 01931 01932 return HAL_OK; 01933 } 01934 01935 /** 01936 * @} 01937 */ 01938 01939 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions 01940 * @brief UART State and Errors functions 01941 * 01942 @verbatim 01943 ============================================================================== 01944 ##### Peripheral State and Errors functions ##### 01945 ============================================================================== 01946 [..] 01947 This subsection provides a set of functions allowing to return the State of 01948 UART communication process, return Peripheral Errors occurred during communication 01949 process 01950 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral. 01951 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication. 01952 01953 @endverbatim 01954 * @{ 01955 */ 01956 01957 /** 01958 * @brief Returns the UART state. 01959 * @param huart pointer to a UART_HandleTypeDef structure that contains 01960 * the configuration information for the specified UART module. 01961 * @retval HAL state 01962 */ 01963 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) 01964 { 01965 uint32_t temp1= 0x00U, temp2 = 0x00U; 01966 temp1 = huart->gState; 01967 temp2 = huart->RxState; 01968 01969 return (HAL_UART_StateTypeDef)(temp1 | temp2); 01970 } 01971 01972 /** 01973 * @brief Return the UART error code 01974 * @param huart pointer to a UART_HandleTypeDef structure that contains 01975 * the configuration information for the specified UART. 01976 * @retval UART Error Code 01977 */ 01978 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart) 01979 { 01980 return huart->ErrorCode; 01981 } 01982 01983 /** 01984 * @} 01985 */ 01986 01987 /** 01988 * @brief DMA UART transmit process complete callback. 01989 * @param hdma DMA handle 01990 * @retval None 01991 */ 01992 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma) 01993 { 01994 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 01995 /* DMA Normal mode*/ 01996 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U) 01997 { 01998 huart->TxXferCount = 0U; 01999 02000 /* Disable the DMA transfer for transmit request by setting the DMAT bit 02001 in the UART CR3 register */ 02002 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 02003 02004 /* Enable the UART Transmit Complete Interrupt */ 02005 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 02006 02007 } 02008 /* DMA Circular mode */ 02009 else 02010 { 02011 HAL_UART_TxCpltCallback(huart); 02012 } 02013 } 02014 02015 /** 02016 * @brief DMA UART transmit process half complete callback 02017 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 02018 * the configuration information for the specified DMA module. 02019 * @retval None 02020 */ 02021 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 02022 { 02023 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 02024 02025 HAL_UART_TxHalfCpltCallback(huart); 02026 } 02027 02028 /** 02029 * @brief DMA UART receive process complete callback. 02030 * @param hdma DMA handle 02031 * @retval None 02032 */ 02033 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 02034 { 02035 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02036 /* DMA Normal mode*/ 02037 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U) 02038 { 02039 huart->RxXferCount = 0U; 02040 02041 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 02042 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE); 02043 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02044 02045 /* Disable the DMA transfer for the receiver request by setting the DMAR bit 02046 in the UART CR3 register */ 02047 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 02048 02049 /* At end of Rx process, restore huart->RxState to Ready */ 02050 huart->RxState = HAL_UART_STATE_READY; 02051 } 02052 HAL_UART_RxCpltCallback(huart); 02053 } 02054 02055 /** 02056 * @brief DMA UART receive process half complete callback 02057 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 02058 * the configuration information for the specified DMA module. 02059 * @retval None 02060 */ 02061 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 02062 { 02063 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 02064 02065 HAL_UART_RxHalfCpltCallback(huart); 02066 } 02067 02068 /** 02069 * @brief DMA UART communication error callback. 02070 * @param hdma DMA handle 02071 * @retval None 02072 */ 02073 static void UART_DMAError(DMA_HandleTypeDef *hdma) 02074 { 02075 uint32_t dmarequest = 0x00U; 02076 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02077 02078 /* Stop UART DMA Tx request if ongoing */ 02079 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT); 02080 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest) 02081 { 02082 huart->TxXferCount = 0U; 02083 UART_EndTxTransfer(huart); 02084 } 02085 02086 /* Stop UART DMA Rx request if ongoing */ 02087 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR); 02088 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest) 02089 { 02090 huart->RxXferCount = 0U; 02091 UART_EndRxTransfer(huart); 02092 } 02093 02094 huart->ErrorCode |= HAL_UART_ERROR_DMA; 02095 HAL_UART_ErrorCallback(huart); 02096 } 02097 02098 /** 02099 * @brief This function handles UART Communication Timeout. 02100 * @param huart pointer to a UART_HandleTypeDef structure that contains 02101 * the configuration information for the specified UART module. 02102 * @param Flag specifies the UART flag to check. 02103 * @param Status The new Flag status (SET or RESET). 02104 * @param Tickstart Tick start value 02105 * @param Timeout Timeout duration 02106 * @retval HAL status 02107 */ 02108 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout) 02109 { 02110 /* Wait until flag is set */ 02111 while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status) 02112 { 02113 /* Check for the Timeout */ 02114 if(Timeout != HAL_MAX_DELAY) 02115 { 02116 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout)) 02117 { 02118 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ 02119 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE)); 02120 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02121 02122 huart->gState = HAL_UART_STATE_READY; 02123 huart->RxState = HAL_UART_STATE_READY; 02124 02125 /* Process Unlocked */ 02126 __HAL_UNLOCK(huart); 02127 02128 return HAL_TIMEOUT; 02129 } 02130 } 02131 } 02132 02133 return HAL_OK; 02134 } 02135 02136 /** 02137 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion). 02138 * @param huart UART handle. 02139 * @retval None 02140 */ 02141 static void UART_EndTxTransfer(UART_HandleTypeDef *huart) 02142 { 02143 /* Disable TXEIE and TCIE interrupts */ 02144 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE)); 02145 02146 /* At end of Tx process, restore huart->gState to Ready */ 02147 huart->gState = HAL_UART_STATE_READY; 02148 } 02149 02150 /** 02151 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion). 02152 * @param huart UART handle. 02153 * @retval None 02154 */ 02155 static void UART_EndRxTransfer(UART_HandleTypeDef *huart) 02156 { 02157 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */ 02158 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02159 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02160 02161 /* At end of Rx process, restore huart->RxState to Ready */ 02162 huart->RxState = HAL_UART_STATE_READY; 02163 } 02164 02165 /** 02166 * @brief DMA UART communication abort callback, when initiated by HAL services on Error 02167 * (To be called at end of DMA Abort procedure following error occurrence). 02168 * @param hdma DMA handle. 02169 * @retval None 02170 */ 02171 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma) 02172 { 02173 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02174 huart->RxXferCount = 0U; 02175 huart->TxXferCount = 0U; 02176 02177 HAL_UART_ErrorCallback(huart); 02178 } 02179 02180 /** 02181 * @brief DMA UART Tx communication abort callback, when initiated by user 02182 * (To be called at end of DMA Tx Abort procedure following user abort request). 02183 * @note When this callback is executed, User Abort complete call back is called only if no 02184 * Abort still ongoing for Rx DMA Handle. 02185 * @param hdma DMA handle. 02186 * @retval None 02187 */ 02188 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma) 02189 { 02190 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02191 02192 huart->hdmatx->XferAbortCallback = NULL; 02193 02194 /* Check if an Abort process is still ongoing */ 02195 if(huart->hdmarx != NULL) 02196 { 02197 if(huart->hdmarx->XferAbortCallback != NULL) 02198 { 02199 return; 02200 } 02201 } 02202 02203 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02204 huart->TxXferCount = 0x00U; 02205 huart->RxXferCount = 0x00U; 02206 02207 /* Reset ErrorCode */ 02208 huart->ErrorCode = HAL_UART_ERROR_NONE; 02209 02210 /* Restore huart->gState and huart->RxState to Ready */ 02211 huart->gState = HAL_UART_STATE_READY; 02212 huart->RxState = HAL_UART_STATE_READY; 02213 02214 /* Call user Abort complete callback */ 02215 HAL_UART_AbortCpltCallback(huart); 02216 } 02217 02218 /** 02219 * @brief DMA UART Rx communication abort callback, when initiated by user 02220 * (To be called at end of DMA Rx Abort procedure following user abort request). 02221 * @note When this callback is executed, User Abort complete call back is called only if no 02222 * Abort still ongoing for Tx DMA Handle. 02223 * @param hdma DMA handle. 02224 * @retval None 02225 */ 02226 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma) 02227 { 02228 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02229 02230 huart->hdmarx->XferAbortCallback = NULL; 02231 02232 /* Check if an Abort process is still ongoing */ 02233 if(huart->hdmatx != NULL) 02234 { 02235 if(huart->hdmatx->XferAbortCallback != NULL) 02236 { 02237 return; 02238 } 02239 } 02240 02241 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */ 02242 huart->TxXferCount = 0x00U; 02243 huart->RxXferCount = 0x00U; 02244 02245 /* Reset ErrorCode */ 02246 huart->ErrorCode = HAL_UART_ERROR_NONE; 02247 02248 /* Restore huart->gState and huart->RxState to Ready */ 02249 huart->gState = HAL_UART_STATE_READY; 02250 huart->RxState = HAL_UART_STATE_READY; 02251 02252 /* Call user Abort complete callback */ 02253 HAL_UART_AbortCpltCallback(huart); 02254 } 02255 02256 /** 02257 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to 02258 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer) 02259 * (This callback is executed at end of DMA Tx Abort procedure following user abort request, 02260 * and leads to user Tx Abort Complete callback execution). 02261 * @param hdma DMA handle. 02262 * @retval None 02263 */ 02264 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 02265 { 02266 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02267 02268 huart->TxXferCount = 0x00U; 02269 02270 /* Restore huart->gState to Ready */ 02271 huart->gState = HAL_UART_STATE_READY; 02272 02273 /* Call user Abort complete callback */ 02274 HAL_UART_AbortTransmitCpltCallback(huart); 02275 } 02276 02277 /** 02278 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to 02279 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer) 02280 * (This callback is executed at end of DMA Rx Abort procedure following user abort request, 02281 * and leads to user Rx Abort Complete callback execution). 02282 * @param hdma DMA handle. 02283 * @retval None 02284 */ 02285 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma) 02286 { 02287 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 02288 02289 huart->RxXferCount = 0x00U; 02290 02291 /* Restore huart->RxState to Ready */ 02292 huart->RxState = HAL_UART_STATE_READY; 02293 02294 /* Call user Abort complete callback */ 02295 HAL_UART_AbortReceiveCpltCallback(huart); 02296 } 02297 02298 /** 02299 * @brief Sends an amount of data in non blocking mode. 02300 * @param huart Pointer to a UART_HandleTypeDef structure that contains 02301 * the configuration information for the specified UART module. 02302 * @retval HAL status 02303 */ 02304 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart) 02305 { 02306 uint16_t* tmp; 02307 02308 /* Check that a Tx process is ongoing */ 02309 if(huart->gState == HAL_UART_STATE_BUSY_TX) 02310 { 02311 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 02312 { 02313 tmp = (uint16_t*) huart->pTxBuffPtr; 02314 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); 02315 if(huart->Init.Parity == UART_PARITY_NONE) 02316 { 02317 huart->pTxBuffPtr += 2U; 02318 } 02319 else 02320 { 02321 huart->pTxBuffPtr += 1U; 02322 } 02323 } 02324 else 02325 { 02326 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF); 02327 } 02328 02329 if(--huart->TxXferCount == 0U) 02330 { 02331 /* Disable the UART Transmit Complete Interrupt */ 02332 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE); 02333 02334 /* Enable the UART Transmit Complete Interrupt */ 02335 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE); 02336 } 02337 return HAL_OK; 02338 } 02339 else 02340 { 02341 return HAL_BUSY; 02342 } 02343 } 02344 02345 /** 02346 * @brief Wraps up transmission in non blocking mode. 02347 * @param huart pointer to a UART_HandleTypeDef structure that contains 02348 * the configuration information for the specified UART module. 02349 * @retval HAL status 02350 */ 02351 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart) 02352 { 02353 /* Disable the UART Transmit Complete Interrupt */ 02354 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE); 02355 02356 /* Tx process is ended, restore huart->gState to Ready */ 02357 huart->gState = HAL_UART_STATE_READY; 02358 02359 HAL_UART_TxCpltCallback(huart); 02360 02361 return HAL_OK; 02362 } 02363 02364 /** 02365 * @brief Receives an amount of data in non blocking mode 02366 * @param huart pointer to a UART_HandleTypeDef structure that contains 02367 * the configuration information for the specified UART module. 02368 * @retval HAL status 02369 */ 02370 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart) 02371 { 02372 uint16_t* tmp; 02373 02374 /* Check that a Rx process is ongoing */ 02375 if(huart->RxState == HAL_UART_STATE_BUSY_RX) 02376 { 02377 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 02378 { 02379 tmp = (uint16_t*) huart->pRxBuffPtr; 02380 if(huart->Init.Parity == UART_PARITY_NONE) 02381 { 02382 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); 02383 huart->pRxBuffPtr += 2U; 02384 } 02385 else 02386 { 02387 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF); 02388 huart->pRxBuffPtr += 1U; 02389 } 02390 } 02391 else 02392 { 02393 if(huart->Init.Parity == UART_PARITY_NONE) 02394 { 02395 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 02396 } 02397 else 02398 { 02399 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 02400 } 02401 } 02402 02403 if(--huart->RxXferCount == 0U) 02404 { 02405 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/ 02406 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE)); 02407 02408 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 02409 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE); 02410 02411 /* Rx process is completed, restore huart->RxState to Ready */ 02412 huart->RxState = HAL_UART_STATE_READY; 02413 02414 HAL_UART_RxCpltCallback(huart); 02415 02416 return HAL_OK; 02417 } 02418 return HAL_OK; 02419 } 02420 else 02421 { 02422 return HAL_BUSY; 02423 } 02424 } 02425 02426 /** 02427 * @brief Configures the UART peripheral. 02428 * @param huart pointer to a UART_HandleTypeDef structure that contains 02429 * the configuration information for the specified UART module. 02430 * @retval None 02431 */ 02432 static void UART_SetConfig(UART_HandleTypeDef *huart) 02433 { 02434 uint32_t tmpreg = 0x00U; 02435 02436 /* Check the parameters */ 02437 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); 02438 assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); 02439 assert_param(IS_UART_PARITY(huart->Init.Parity)); 02440 assert_param(IS_UART_MODE(huart->Init.Mode)); 02441 02442 /*-------------------------- USART CR2 Configuration -----------------------*/ 02443 tmpreg = huart->Instance->CR2; 02444 02445 /* Clear STOP[13:12] bits */ 02446 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP); 02447 02448 /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */ 02449 tmpreg |= (uint32_t)huart->Init.StopBits; 02450 02451 /* Write to USART CR2 */ 02452 WRITE_REG(huart->Instance->CR2, (uint32_t)tmpreg); 02453 02454 /*-------------------------- USART CR1 Configuration -----------------------*/ 02455 tmpreg = huart->Instance->CR1; 02456 02457 /* Clear M, PCE, PS, TE and RE bits */ 02458 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \ 02459 USART_CR1_RE | USART_CR1_OVER8)); 02460 02461 /* Configure the UART Word Length, Parity and mode: 02462 Set the M bits according to huart->Init.WordLength value 02463 Set PCE and PS bits according to huart->Init.Parity value 02464 Set TE and RE bits according to huart->Init.Mode value 02465 Set OVER8 bit according to huart->Init.OverSampling value */ 02466 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling; 02467 02468 /* Write to USART CR1 */ 02469 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg); 02470 02471 /*-------------------------- USART CR3 Configuration -----------------------*/ 02472 tmpreg = huart->Instance->CR3; 02473 02474 /* Clear CTSE and RTSE bits */ 02475 tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE)); 02476 02477 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */ 02478 tmpreg |= huart->Init.HwFlowCtl; 02479 02480 /* Write to USART CR3 */ 02481 WRITE_REG(huart->Instance->CR3, (uint32_t)tmpreg); 02482 02483 /* Check the Over Sampling */ 02484 if(huart->Init.OverSampling == UART_OVERSAMPLING_8) 02485 { 02486 /*-------------------------- USART BRR Configuration ---------------------*/ 02487 #if defined(USART6) 02488 if((huart->Instance == USART1) || (huart->Instance == USART6)) 02489 { 02490 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); 02491 } 02492 #else 02493 if(huart->Instance == USART1) 02494 { 02495 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); 02496 } 02497 #endif /* USART6 */ 02498 else 02499 { 02500 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate); 02501 } 02502 } 02503 else 02504 { 02505 /*-------------------------- USART BRR Configuration ---------------------*/ 02506 #if defined(USART6) 02507 if((huart->Instance == USART1) || (huart->Instance == USART6)) 02508 { 02509 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); 02510 } 02511 #else 02512 if(huart->Instance == USART1) 02513 { 02514 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); 02515 } 02516 #endif /* USART6 */ 02517 else 02518 { 02519 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate); 02520 } 02521 } 02522 } 02523 02524 /** 02525 * @} 02526 */ 02527 02528 #endif /* HAL_UART_MODULE_ENABLED */ 02529 /** 02530 * @} 02531 */ 02532 02533 /** 02534 * @} 02535 */ 02536 02537 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/