Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
stm32f1xx_hal_uart.c
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f1xx_hal_uart.c 00004 * @author MCD Application Team 00005 * @version V1.0.4 00006 * @date 29-April-2016 00007 * @brief UART HAL module driver. 00008 * This file provides firmware functions to manage the following 00009 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral: 00010 * + Initialization and de-initialization functions 00011 * + IO operation functions 00012 * + Peripheral Control functions 00013 * + Peripheral State and Errors functions 00014 @verbatim 00015 ============================================================================== 00016 ##### How to use this driver ##### 00017 ============================================================================== 00018 [..] 00019 The UART HAL driver can be used as follows: 00020 00021 (#) Declare a UART_HandleTypeDef handle structure. 00022 00023 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API: 00024 (##) Enable the USARTx interface clock. 00025 (##) UART pins configuration: 00026 (+++) Enable the clock for the UART GPIOs. 00027 (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input). 00028 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT() 00029 and HAL_UART_Receive_IT() APIs): 00030 (+++) Configure the USARTx interrupt priority. 00031 (+++) Enable the NVIC USART IRQ handle. 00032 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA() 00033 and HAL_UART_Receive_DMA() APIs): 00034 (+++) Declare a DMA handle structure for the Tx/Rx channel. 00035 (+++) Enable the DMAx interface clock. 00036 (+++) Configure the declared DMA handle structure with the required 00037 Tx/Rx parameters. 00038 (+++) Configure the DMA Tx/Rx channel. 00039 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle. 00040 (+++) Configure the priority and enable the NVIC for the transfer complete 00041 interrupt on the DMA Tx/Rx channel. 00042 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle 00043 (used for last byte sending completion detection in DMA non circular mode) 00044 00045 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware 00046 flow control and Mode(Receiver/Transmitter) in the huart Init structure. 00047 00048 (#) For the UART asynchronous mode, initialize the UART registers by calling 00049 the HAL_UART_Init() API. 00050 00051 (#) For the UART Half duplex mode, initialize the UART registers by calling 00052 the HAL_HalfDuplex_Init() API. 00053 00054 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API. 00055 00056 (#) For the Multi-Processor mode, initialize the UART registers by calling 00057 the HAL_MultiProcessor_Init() API. 00058 00059 [..] 00060 (@) The specific UART interrupts (Transmission complete interrupt, 00061 RXNE interrupt and Error Interrupts) will be managed using the macros 00062 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit 00063 and receive process. 00064 00065 [..] 00066 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the 00067 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed 00068 HAL_UART_MspInit() API. 00069 00070 [..] 00071 Three operation modes are available within this driver : 00072 00073 *** Polling mode IO operation *** 00074 ================================= 00075 [..] 00076 (+) Send an amount of data in blocking mode using HAL_UART_Transmit() 00077 (+) Receive an amount of data in blocking mode using HAL_UART_Receive() 00078 00079 *** Interrupt mode IO operation *** 00080 =================================== 00081 [..] 00082 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT() 00083 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 00084 add his own code by customization of function pointer HAL_UART_TxCpltCallback 00085 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT() 00086 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 00087 add his own code by customization of function pointer HAL_UART_RxCpltCallback 00088 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 00089 add his own code by customization of function pointer HAL_UART_ErrorCallback 00090 00091 *** DMA mode IO operation *** 00092 ============================== 00093 [..] 00094 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA() 00095 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can 00096 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback 00097 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can 00098 add his own code by customization of function pointer HAL_UART_TxCpltCallback 00099 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA() 00100 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can 00101 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback 00102 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can 00103 add his own code by customization of function pointer HAL_UART_RxCpltCallback 00104 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can 00105 add his own code by customization of function pointer HAL_UART_ErrorCallback 00106 (+) Pause the DMA Transfer using HAL_UART_DMAPause() 00107 (+) Resume the DMA Transfer using HAL_UART_DMAResume() 00108 (+) Stop the DMA Transfer using HAL_UART_DMAStop() 00109 00110 *** UART HAL driver macros list *** 00111 ============================================= 00112 [..] 00113 Below the list of most used macros in UART HAL driver. 00114 00115 (+) __HAL_UART_ENABLE: Enable the UART peripheral 00116 (+) __HAL_UART_DISABLE: Disable the UART peripheral 00117 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not 00118 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag 00119 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt 00120 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt 00121 (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not 00122 00123 [..] 00124 (@) You can refer to the UART HAL driver header file for more useful macros 00125 00126 @endverbatim 00127 ****************************************************************************** 00128 * @attention 00129 * 00130 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00131 * 00132 * Redistribution and use in source and binary forms, with or without modification, 00133 * are permitted provided that the following conditions are met: 00134 * 1. Redistributions of source code must retain the above copyright notice, 00135 * this list of conditions and the following disclaimer. 00136 * 2. Redistributions in binary form must reproduce the above copyright notice, 00137 * this list of conditions and the following disclaimer in the documentation 00138 * and/or other materials provided with the distribution. 00139 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00140 * may be used to endorse or promote products derived from this software 00141 * without specific prior written permission. 00142 * 00143 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00144 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00145 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00146 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00147 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00148 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00149 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00150 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00151 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00152 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00153 * 00154 ****************************************************************************** 00155 */ 00156 00157 /* Includes ------------------------------------------------------------------*/ 00158 #include "stm32f1xx_hal.h" 00159 00160 /** @addtogroup STM32F1xx_HAL_Driver 00161 * @{ 00162 */ 00163 00164 /** @defgroup UART UART 00165 * @brief HAL UART module driver 00166 * @{ 00167 */ 00168 #ifdef HAL_UART_MODULE_ENABLED 00169 00170 /* Private typedef -----------------------------------------------------------*/ 00171 /* Private define ------------------------------------------------------------*/ 00172 /* Private macros ------------------------------------------------------------*/ 00173 /* Private variables ---------------------------------------------------------*/ 00174 /* Private function prototypes -----------------------------------------------*/ 00175 /** @addtogroup UART_Private_Functions UART Private Functions 00176 * @{ 00177 */ 00178 static void UART_SetConfig (UART_HandleTypeDef *huart); 00179 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart); 00180 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart); 00181 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart); 00182 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma); 00183 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); 00184 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); 00185 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); 00186 static void UART_DMAError(DMA_HandleTypeDef *hdma); 00187 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout); 00188 /** 00189 * @} 00190 */ 00191 00192 /* Exported functions ---------------------------------------------------------*/ 00193 00194 /** @defgroup UART_Exported_Functions UART Exported Functions 00195 * @{ 00196 */ 00197 00198 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions 00199 * @brief Initialization and Configuration functions 00200 * 00201 @verbatim 00202 =============================================================================== 00203 ##### Initialization and Configuration functions ##### 00204 =============================================================================== 00205 [..] 00206 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy 00207 in asynchronous mode. 00208 (+) For the asynchronous mode only these parameters can be configured: 00209 (++) Baud Rate 00210 (++) Word Length 00211 (++) Stop Bit 00212 (++) Parity 00213 (++) Hardware flow control 00214 (++) Receiver/transmitter modes 00215 [..] 00216 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs 00217 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor 00218 configuration procedures (details for the procedures are available in reference manuals 00219 (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)). 00220 00221 00222 @endverbatim 00223 * @{ 00224 */ 00225 00226 /* 00227 Additionnal 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 00244 /** 00245 * @brief Initializes the UART mode according to the specified parameters in 00246 * the UART_InitTypeDef and create the associated handle. 00247 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00248 * the configuration information for the specified UART module. 00249 * @retval HAL status 00250 */ 00251 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart) 00252 { 00253 /* Check the UART handle allocation */ 00254 if(huart == NULL) 00255 { 00256 return HAL_ERROR; 00257 } 00258 00259 /* Check the parameters */ 00260 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE) 00261 { 00262 /* The hardware flow control is available only for USART1, USART2, USART3 */ 00263 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance)); 00264 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)); 00265 } 00266 else 00267 { 00268 assert_param(IS_UART_INSTANCE(huart->Instance)); 00269 } 00270 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00271 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00272 00273 if(huart->State == HAL_UART_STATE_RESET) 00274 { 00275 /* Allocate lock resource and initialize it */ 00276 huart->Lock = HAL_UNLOCKED; 00277 00278 /* Init the low level hardware */ 00279 HAL_UART_MspInit(huart); 00280 } 00281 00282 huart->State = HAL_UART_STATE_BUSY; 00283 00284 /* Disable the peripheral */ 00285 __HAL_UART_DISABLE(huart); 00286 00287 /* Set the UART Communication parameters */ 00288 UART_SetConfig(huart); 00289 00290 /* In asynchronous mode, the following bits must be kept cleared: 00291 - LINEN and CLKEN bits in the USART_CR2 register, 00292 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/ 00293 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00294 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00295 00296 /* Enable the peripheral */ 00297 __HAL_UART_ENABLE(huart); 00298 00299 /* Initialize the UART state */ 00300 huart->ErrorCode = HAL_UART_ERROR_NONE; 00301 huart->State= HAL_UART_STATE_READY; 00302 00303 return HAL_OK; 00304 } 00305 00306 /** 00307 * @brief Initializes the half-duplex mode according to the specified 00308 * parameters in the UART_InitTypeDef and create the associated handle. 00309 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00310 * the configuration information for the specified UART module. 00311 * @retval HAL status 00312 */ 00313 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart) 00314 { 00315 /* Check the UART handle allocation */ 00316 if(huart == NULL) 00317 { 00318 return HAL_ERROR; 00319 } 00320 00321 /* Check UART instance */ 00322 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance)); 00323 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00324 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00325 00326 if(huart->State == HAL_UART_STATE_RESET) 00327 { 00328 /* Allocate lock resource and initialize it */ 00329 huart->Lock = HAL_UNLOCKED; 00330 00331 /* Init the low level hardware */ 00332 HAL_UART_MspInit(huart); 00333 } 00334 00335 huart->State = HAL_UART_STATE_BUSY; 00336 00337 /* Disable the peripheral */ 00338 __HAL_UART_DISABLE(huart); 00339 00340 /* Set the UART Communication parameters */ 00341 UART_SetConfig(huart); 00342 00343 /* In half-duplex mode, the following bits must be kept cleared: 00344 - LINEN and CLKEN bits in the USART_CR2 register, 00345 - SCEN and IREN bits in the USART_CR3 register.*/ 00346 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN)); 00347 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN)); 00348 00349 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */ 00350 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL); 00351 00352 /* Enable the peripheral */ 00353 __HAL_UART_ENABLE(huart); 00354 00355 /* Initialize the UART state*/ 00356 huart->ErrorCode = HAL_UART_ERROR_NONE; 00357 huart->State= HAL_UART_STATE_READY; 00358 00359 return HAL_OK; 00360 } 00361 00362 /** 00363 * @brief Initializes the LIN mode according to the specified 00364 * parameters in the UART_InitTypeDef and create the associated handle. 00365 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00366 * the configuration information for the specified UART module. 00367 * @param BreakDetectLength: Specifies the LIN break detection length. 00368 * This parameter can be one of the following values: 00369 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection 00370 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection 00371 * @retval HAL status 00372 */ 00373 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength) 00374 { 00375 /* Check the UART handle allocation */ 00376 if(huart == NULL) 00377 { 00378 return HAL_ERROR; 00379 } 00380 00381 /* Check the LIN UART instance */ 00382 assert_param(IS_UART_LIN_INSTANCE(huart->Instance)); 00383 /* Check the Break detection length parameter */ 00384 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength)); 00385 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength)); 00386 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling)); 00387 00388 if(huart->State == HAL_UART_STATE_RESET) 00389 { 00390 /* Allocate lock resource and initialize it */ 00391 huart->Lock = HAL_UNLOCKED; 00392 00393 /* Init the low level hardware */ 00394 HAL_UART_MspInit(huart); 00395 } 00396 00397 huart->State = 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 LIN mode, the following bits must be kept cleared: 00406 - CLKEN bits in the USART_CR2 register, 00407 - SCEN and IREN bits in the USART_CR3 register.*/ 00408 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN); 00409 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN)); 00410 00411 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */ 00412 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN); 00413 00414 /* Set the USART LIN Break detection length. */ 00415 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength); 00416 00417 /* Enable the peripheral */ 00418 __HAL_UART_ENABLE(huart); 00419 00420 /* Initialize the UART state*/ 00421 huart->ErrorCode = HAL_UART_ERROR_NONE; 00422 huart->State= HAL_UART_STATE_READY; 00423 00424 return HAL_OK; 00425 } 00426 00427 /** 00428 * @brief Initializes the Multi-Processor mode according to the specified 00429 * parameters in the UART_InitTypeDef and create the associated handle. 00430 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00431 * the configuration information for the specified UART module. 00432 * @param Address: UART node address 00433 * @param WakeUpMethod: specifies the UART wakeup method. 00434 * This parameter can be one of the following values: 00435 * @arg UART_WAKEUPMETHOD_IDLELINE: Wakeup by an idle line detection 00436 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wakeup by an address mark 00437 * @retval HAL status 00438 */ 00439 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod) 00440 { 00441 /* Check the UART handle allocation */ 00442 if(huart == NULL) 00443 { 00444 return HAL_ERROR; 00445 } 00446 00447 /* Check UART instance capabilities */ 00448 assert_param(IS_UART_MULTIPROCESSOR_INSTANCE(huart->Instance)); 00449 00450 /* Check the Address & wake up method parameters */ 00451 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod)); 00452 assert_param(IS_UART_ADDRESS(Address)); 00453 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength)); 00454 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling)); 00455 00456 if(huart->State == HAL_UART_STATE_RESET) 00457 { 00458 /* Allocate lock resource and initialize it */ 00459 huart->Lock = HAL_UNLOCKED; 00460 00461 /* Init the low level hardware */ 00462 HAL_UART_MspInit(huart); 00463 } 00464 00465 huart->State = 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 Multi-Processor mode, the following bits must be kept cleared: 00474 - LINEN and CLKEN bits in the USART_CR2 register, 00475 - SCEN, HDSEL 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_SCEN | USART_CR3_HDSEL | USART_CR3_IREN)); 00478 00479 /* Set the USART address node */ 00480 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, Address); 00481 00482 /* Set the wake up method by setting the WAKE bit in the CR1 register */ 00483 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod); 00484 00485 /* Enable the peripheral */ 00486 __HAL_UART_ENABLE(huart); 00487 00488 /* Initialize the UART state */ 00489 huart->ErrorCode = HAL_UART_ERROR_NONE; 00490 huart->State= HAL_UART_STATE_READY; 00491 00492 return HAL_OK; 00493 } 00494 00495 /** 00496 * @brief DeInitializes the UART peripheral. 00497 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00498 * the configuration information for the specified UART module. 00499 * @retval HAL status 00500 */ 00501 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart) 00502 { 00503 /* Check the UART handle allocation */ 00504 if(huart == NULL) 00505 { 00506 return HAL_ERROR; 00507 } 00508 00509 /* Check the parameters */ 00510 assert_param(IS_UART_INSTANCE(huart->Instance)); 00511 00512 huart->State = HAL_UART_STATE_BUSY; 00513 00514 /* Disable the Peripheral */ 00515 __HAL_UART_DISABLE(huart); 00516 00517 huart->Instance->CR1 = 0x0; 00518 huart->Instance->CR2 = 0x0; 00519 huart->Instance->CR3 = 0x0; 00520 00521 /* DeInit the low level hardware */ 00522 HAL_UART_MspDeInit(huart); 00523 00524 huart->ErrorCode = HAL_UART_ERROR_NONE; 00525 huart->State = HAL_UART_STATE_RESET; 00526 00527 /* Process Unlock */ 00528 __HAL_UNLOCK(huart); 00529 00530 return HAL_OK; 00531 } 00532 00533 /** 00534 * @brief UART MSP Init. 00535 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00536 * the configuration information for the specified UART module. 00537 * @retval None 00538 */ 00539 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart) 00540 { 00541 /* Prevent unused argument(s) compilation warning */ 00542 UNUSED(huart); 00543 /* NOTE: This function should not be modified, when the callback is needed, 00544 the HAL_UART_MspInit can be implemented in the user file 00545 */ 00546 } 00547 00548 /** 00549 * @brief UART MSP DeInit. 00550 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00551 * the configuration information for the specified UART module. 00552 * @retval None 00553 */ 00554 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart) 00555 { 00556 /* Prevent unused argument(s) compilation warning */ 00557 UNUSED(huart); 00558 /* NOTE: This function should not be modified, when the callback is needed, 00559 the HAL_UART_MspDeInit can be implemented in the user file 00560 */ 00561 } 00562 00563 /** 00564 * @} 00565 */ 00566 00567 /** @defgroup UART_Exported_Functions_Group2 IO operation functions 00568 * @brief UART Transmit and Receive functions 00569 * 00570 @verbatim 00571 ============================================================================== 00572 ##### IO operation functions ##### 00573 ============================================================================== 00574 [..] 00575 This subsection provides a set of functions allowing to manage the UART asynchronous 00576 and Half duplex data transfers. 00577 00578 (#) There are two modes of transfer: 00579 (++) Blocking mode: The communication is performed in polling mode. 00580 The HAL status of all data processing is returned by the same function 00581 after finishing transfer. 00582 (++) Non blocking mode: The communication is performed using Interrupts 00583 or DMA, these APIs return the HAL status. 00584 The end of the data processing will be indicated through the 00585 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when 00586 using DMA mode. 00587 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks 00588 will be executed respectively at the end of the transmit or receive process. 00589 The HAL_UART_ErrorCallback() user callback will be executed when 00590 a communication error is detected. 00591 00592 (#) Blocking mode APIs are: 00593 (++) HAL_UART_Transmit() 00594 (++) HAL_UART_Receive() 00595 00596 (#) Non Blocking mode APIs with Interrupt are: 00597 (++) HAL_UART_Transmit_IT() 00598 (++) HAL_UART_Receive_IT() 00599 (++) HAL_UART_IRQHandler() 00600 00601 (#) Non Blocking mode functions with DMA are: 00602 (++) HAL_UART_Transmit_DMA() 00603 (++) HAL_UART_Receive_DMA() 00604 (++) HAL_UART_DMAPause() 00605 (++) HAL_UART_DMAResume() 00606 (++) HAL_UART_DMAStop() 00607 00608 (#) A set of Transfer Complete Callbacks are provided in non blocking mode: 00609 (++) HAL_UART_TxHalfCpltCallback() 00610 (++) HAL_UART_TxCpltCallback() 00611 (++) HAL_UART_RxHalfCpltCallback() 00612 (++) HAL_UART_RxCpltCallback() 00613 (++) HAL_UART_ErrorCallback() 00614 00615 [..] 00616 (@) In the Half duplex communication, it is forbidden to run the transmit 00617 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX 00618 can't be useful. 00619 00620 @endverbatim 00621 * @{ 00622 */ 00623 00624 /** 00625 * @brief Sends an amount of data in blocking mode. 00626 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00627 * the configuration information for the specified UART module. 00628 * @param pData: Pointer to data buffer 00629 * @param Size: Amount of data to be sent 00630 * @param Timeout: Timeout duration 00631 * @retval HAL status 00632 */ 00633 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 00634 { 00635 uint16_t* tmp; 00636 uint32_t tmp_state = 0; 00637 00638 tmp_state = huart->State; 00639 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX)) 00640 { 00641 if((pData == NULL) || (Size == 0)) 00642 { 00643 return HAL_ERROR; 00644 } 00645 00646 /* Process Locked */ 00647 __HAL_LOCK(huart); 00648 00649 huart->ErrorCode = HAL_UART_ERROR_NONE; 00650 /* Check if a non-blocking receive process is ongoing or not */ 00651 if(huart->State == HAL_UART_STATE_BUSY_RX) 00652 { 00653 huart->State = HAL_UART_STATE_BUSY_TX_RX; 00654 } 00655 else 00656 { 00657 huart->State = HAL_UART_STATE_BUSY_TX; 00658 } 00659 00660 huart->TxXferSize = Size; 00661 huart->TxXferCount = Size; 00662 while(huart->TxXferCount > 0) 00663 { 00664 huart->TxXferCount--; 00665 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 00666 { 00667 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK) 00668 { 00669 return HAL_TIMEOUT; 00670 } 00671 tmp = (uint16_t*) pData; 00672 huart->Instance->DR = (*tmp & (uint16_t)0x01FF); 00673 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 00674 { 00675 pData +=2; 00676 } 00677 else 00678 { 00679 pData +=1; 00680 } 00681 } 00682 else 00683 { 00684 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK) 00685 { 00686 return HAL_TIMEOUT; 00687 } 00688 huart->Instance->DR = (*pData++ & (uint8_t)0xFF); 00689 } 00690 } 00691 00692 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK) 00693 { 00694 return HAL_TIMEOUT; 00695 } 00696 00697 /* Check if a non-blocking receive process is ongoing or not */ 00698 if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 00699 { 00700 huart->State = HAL_UART_STATE_BUSY_RX; 00701 } 00702 else 00703 { 00704 huart->State = HAL_UART_STATE_READY; 00705 } 00706 00707 /* Process Unlocked */ 00708 __HAL_UNLOCK(huart); 00709 00710 return HAL_OK; 00711 } 00712 else 00713 { 00714 return HAL_BUSY; 00715 } 00716 } 00717 00718 /** 00719 * @brief Receives an amount of data in blocking mode. 00720 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00721 * the configuration information for the specified UART module. 00722 * @param pData: Pointer to data buffer 00723 * @param Size: Amount of data to be received 00724 * @param Timeout: Timeout duration 00725 * @retval HAL status 00726 */ 00727 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout) 00728 { 00729 uint16_t* tmp; 00730 uint32_t tmp_state = 0; 00731 00732 tmp_state = huart->State; 00733 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX)) 00734 { 00735 if((pData == NULL ) || (Size == 0)) 00736 { 00737 return HAL_ERROR; 00738 } 00739 00740 /* Process Locked */ 00741 __HAL_LOCK(huart); 00742 00743 huart->ErrorCode = HAL_UART_ERROR_NONE; 00744 /* Check if a non-blocking transmit process is ongoing or not */ 00745 if(huart->State == HAL_UART_STATE_BUSY_TX) 00746 { 00747 huart->State = HAL_UART_STATE_BUSY_TX_RX; 00748 } 00749 else 00750 { 00751 huart->State = HAL_UART_STATE_BUSY_RX; 00752 } 00753 00754 huart->RxXferSize = Size; 00755 huart->RxXferCount = Size; 00756 00757 /* Check the remain data to be received */ 00758 while(huart->RxXferCount > 0) 00759 { 00760 huart->RxXferCount--; 00761 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 00762 { 00763 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK) 00764 { 00765 return HAL_TIMEOUT; 00766 } 00767 tmp = (uint16_t*) pData ; 00768 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 00769 { 00770 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); 00771 pData +=2; 00772 } 00773 else 00774 { 00775 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF); 00776 pData +=1; 00777 } 00778 00779 } 00780 else 00781 { 00782 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK) 00783 { 00784 return HAL_TIMEOUT; 00785 } 00786 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 00787 { 00788 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 00789 } 00790 else 00791 { 00792 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 00793 } 00794 00795 } 00796 } 00797 00798 /* Check if a non-blocking transmit process is ongoing or not */ 00799 if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 00800 { 00801 huart->State = HAL_UART_STATE_BUSY_TX; 00802 } 00803 else 00804 { 00805 huart->State = HAL_UART_STATE_READY; 00806 } 00807 /* Process Unlocked */ 00808 __HAL_UNLOCK(huart); 00809 00810 return HAL_OK; 00811 } 00812 else 00813 { 00814 return HAL_BUSY; 00815 } 00816 } 00817 00818 /** 00819 * @brief Sends an amount of data in non blocking mode. 00820 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00821 * the configuration information for the specified UART module. 00822 * @param pData: Pointer to data buffer 00823 * @param Size: Amount of data to be sent 00824 * @retval HAL status 00825 */ 00826 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00827 { 00828 uint32_t tmp_state = 0; 00829 00830 tmp_state = huart->State; 00831 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX)) 00832 { 00833 if((pData == NULL ) || (Size == 0)) 00834 { 00835 return HAL_ERROR; 00836 } 00837 00838 /* Process Locked */ 00839 __HAL_LOCK(huart); 00840 00841 huart->pTxBuffPtr = pData; 00842 huart->TxXferSize = Size; 00843 huart->TxXferCount = Size; 00844 00845 huart->ErrorCode = HAL_UART_ERROR_NONE; 00846 /* Check if a receive process is ongoing or not */ 00847 if(huart->State == HAL_UART_STATE_BUSY_RX) 00848 { 00849 huart->State = HAL_UART_STATE_BUSY_TX_RX; 00850 } 00851 else 00852 { 00853 huart->State = HAL_UART_STATE_BUSY_TX; 00854 } 00855 00856 /* Process Unlocked */ 00857 __HAL_UNLOCK(huart); 00858 00859 /* Enable the UART Transmit data register empty Interrupt */ 00860 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE); 00861 00862 return HAL_OK; 00863 } 00864 else 00865 { 00866 return HAL_BUSY; 00867 } 00868 } 00869 00870 /** 00871 * @brief Receives an amount of data in non blocking mode 00872 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00873 * the configuration information for the specified UART module. 00874 * @param pData: Pointer to data buffer 00875 * @param Size: Amount of data to be received 00876 * @retval HAL status 00877 */ 00878 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00879 { 00880 uint32_t tmp_state = 0; 00881 00882 tmp_state = huart->State; 00883 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX)) 00884 { 00885 if((pData == NULL ) || (Size == 0)) 00886 { 00887 return HAL_ERROR; 00888 } 00889 00890 /* Process Locked */ 00891 __HAL_LOCK(huart); 00892 00893 huart->pRxBuffPtr = pData; 00894 huart->RxXferSize = Size; 00895 huart->RxXferCount = Size; 00896 00897 huart->ErrorCode = HAL_UART_ERROR_NONE; 00898 /* Check if a transmit process is ongoing or not */ 00899 if(huart->State == HAL_UART_STATE_BUSY_TX) 00900 { 00901 huart->State = HAL_UART_STATE_BUSY_TX_RX; 00902 } 00903 else 00904 { 00905 huart->State = HAL_UART_STATE_BUSY_RX; 00906 } 00907 00908 /* Process Unlocked */ 00909 __HAL_UNLOCK(huart); 00910 00911 /* Enable the UART Parity Error Interrupt */ 00912 __HAL_UART_ENABLE_IT(huart, UART_IT_PE); 00913 00914 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 00915 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR); 00916 00917 /* Enable the UART Data Register not empty Interrupt */ 00918 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE); 00919 00920 return HAL_OK; 00921 } 00922 else 00923 { 00924 return HAL_BUSY; 00925 } 00926 } 00927 00928 /** 00929 * @brief Sends an amount of data in non blocking mode. 00930 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 00931 * the configuration information for the specified UART module. 00932 * @param pData: Pointer to data buffer 00933 * @param Size: Amount of data to be sent 00934 * @retval HAL status 00935 */ 00936 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 00937 { 00938 uint32_t *tmp; 00939 uint32_t tmp_state = 0; 00940 00941 tmp_state = huart->State; 00942 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX)) 00943 { 00944 if((pData == NULL ) || (Size == 0)) 00945 { 00946 return HAL_ERROR; 00947 } 00948 00949 /* Process Locked */ 00950 __HAL_LOCK(huart); 00951 00952 huart->pTxBuffPtr = pData; 00953 huart->TxXferSize = Size; 00954 huart->TxXferCount = Size; 00955 00956 huart->ErrorCode = HAL_UART_ERROR_NONE; 00957 /* Check if a receive process is ongoing or not */ 00958 if(huart->State == HAL_UART_STATE_BUSY_RX) 00959 { 00960 huart->State = HAL_UART_STATE_BUSY_TX_RX; 00961 } 00962 else 00963 { 00964 huart->State = HAL_UART_STATE_BUSY_TX; 00965 } 00966 00967 /* Set the UART DMA transfer complete callback */ 00968 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; 00969 00970 /* Set the UART DMA Half transfer complete callback */ 00971 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; 00972 00973 /* Set the DMA error callback */ 00974 huart->hdmatx->XferErrorCallback = UART_DMAError; 00975 00976 /* Enable the UART transmit DMA channel */ 00977 tmp = (uint32_t*)&pData; 00978 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size); 00979 00980 /* Clear the TC flag in the SR register by writing 0 to it */ 00981 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC); 00982 00983 /* Enable the DMA transfer for transmit request by setting the DMAT bit 00984 in the UART CR3 register */ 00985 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 00986 00987 /* Process Unlocked */ 00988 __HAL_UNLOCK(huart); 00989 00990 return HAL_OK; 00991 } 00992 else 00993 { 00994 return HAL_BUSY; 00995 } 00996 } 00997 00998 /** 00999 * @brief Receives an amount of data in non blocking mode. 01000 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01001 * the configuration information for the specified UART module. 01002 * @param pData: Pointer to data buffer 01003 * @param Size: Amount of data to be received 01004 * @note When the UART parity is enabled (PCE = 1), the received data contain 01005 * the parity bit (MSB position) 01006 * @retval HAL status 01007 */ 01008 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size) 01009 { 01010 uint32_t *tmp; 01011 uint32_t tmp_state = 0; 01012 01013 tmp_state = huart->State; 01014 if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX)) 01015 { 01016 if((pData == NULL ) || (Size == 0)) 01017 { 01018 return HAL_ERROR; 01019 } 01020 01021 /* Process Locked */ 01022 __HAL_LOCK(huart); 01023 01024 huart->pRxBuffPtr = pData; 01025 huart->RxXferSize = Size; 01026 01027 huart->ErrorCode = HAL_UART_ERROR_NONE; 01028 /* Check if a transmit process is ongoing or not */ 01029 if(huart->State == HAL_UART_STATE_BUSY_TX) 01030 { 01031 huart->State = HAL_UART_STATE_BUSY_TX_RX; 01032 } 01033 else 01034 { 01035 huart->State = HAL_UART_STATE_BUSY_RX; 01036 } 01037 01038 /* Set the UART DMA transfer complete callback */ 01039 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; 01040 01041 /* Set the UART DMA Half transfer complete callback */ 01042 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; 01043 01044 /* Set the DMA error callback */ 01045 huart->hdmarx->XferErrorCallback = UART_DMAError; 01046 01047 /* Enable the DMA channel */ 01048 tmp = (uint32_t*)&pData; 01049 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size); 01050 01051 /* Enable the DMA transfer for the receiver request by setting the DMAR bit 01052 in the UART CR3 register */ 01053 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01054 01055 /* Process Unlocked */ 01056 __HAL_UNLOCK(huart); 01057 01058 return HAL_OK; 01059 } 01060 else 01061 { 01062 return HAL_BUSY; 01063 } 01064 } 01065 01066 /** 01067 * @brief Pauses the DMA Transfer. 01068 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01069 * the configuration information for the specified UART module. 01070 * @retval HAL status 01071 */ 01072 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart) 01073 { 01074 /* Process Locked */ 01075 __HAL_LOCK(huart); 01076 01077 if(huart->State == HAL_UART_STATE_BUSY_TX) 01078 { 01079 /* Disable the UART DMA Tx request */ 01080 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01081 } 01082 else if(huart->State == HAL_UART_STATE_BUSY_RX) 01083 { 01084 /* Disable the UART DMA Rx request */ 01085 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01086 } 01087 else if (huart->State == HAL_UART_STATE_BUSY_TX_RX) 01088 { 01089 /* Disable the UART DMA Tx & Rx requests */ 01090 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR)); 01091 } 01092 else 01093 { 01094 /* Process Unlocked */ 01095 __HAL_UNLOCK(huart); 01096 01097 return HAL_ERROR; 01098 } 01099 01100 /* Process Unlocked */ 01101 __HAL_UNLOCK(huart); 01102 01103 return HAL_OK; 01104 } 01105 01106 /** 01107 * @brief Resumes the DMA Transfer. 01108 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01109 * the configuration information for the specified UART module. 01110 * @retval HAL status 01111 */ 01112 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart) 01113 { 01114 /* Process Locked */ 01115 __HAL_LOCK(huart); 01116 01117 if(huart->State == HAL_UART_STATE_BUSY_TX) 01118 { 01119 /* Enable the UART DMA Tx request */ 01120 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01121 } 01122 else if(huart->State == HAL_UART_STATE_BUSY_RX) 01123 { 01124 /* Clear the Overrun flag before resumming the Rx transfer*/ 01125 __HAL_UART_CLEAR_OREFLAG(huart); 01126 /* Enable the UART DMA Rx request */ 01127 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01128 } 01129 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 01130 { 01131 /* Clear the Overrun flag before resumming the Rx transfer*/ 01132 __HAL_UART_CLEAR_OREFLAG(huart); 01133 /* Enable the UART DMA Tx & Rx request */ 01134 SET_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR)); 01135 } 01136 else 01137 { 01138 /* Process Unlocked */ 01139 __HAL_UNLOCK(huart); 01140 01141 return HAL_ERROR; 01142 } 01143 01144 /* Process Unlocked */ 01145 __HAL_UNLOCK(huart); 01146 01147 return HAL_OK; 01148 } 01149 01150 /** 01151 * @brief Stops the DMA Transfer. 01152 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01153 * the configuration information for the specified UART module. 01154 * @retval HAL status 01155 */ 01156 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart) 01157 { 01158 /* The Lock is not implemented on this API to allow the user application 01159 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback(): 01160 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated 01161 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() 01162 */ 01163 01164 /* Disable the UART Tx/Rx DMA requests */ 01165 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR)); 01166 01167 /* Abort the UART DMA tx channel */ 01168 if(huart->hdmatx != NULL) 01169 { 01170 HAL_DMA_Abort(huart->hdmatx); 01171 } 01172 /* Abort the UART DMA rx channel */ 01173 if(huart->hdmarx != NULL) 01174 { 01175 HAL_DMA_Abort(huart->hdmarx); 01176 } 01177 01178 huart->State = HAL_UART_STATE_READY; 01179 01180 return HAL_OK; 01181 } 01182 01183 /** 01184 * @brief This function handles UART interrupt request. 01185 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01186 * the configuration information for the specified UART module. 01187 * @retval None 01188 */ 01189 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart) 01190 { 01191 uint32_t tmp_flag = 0, tmp_it_source = 0; 01192 01193 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE); 01194 tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE); 01195 /* UART parity error interrupt occurred ------------------------------------*/ 01196 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01197 { 01198 huart->ErrorCode |= HAL_UART_ERROR_PE; 01199 } 01200 01201 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE); 01202 tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR); 01203 /* UART frame error interrupt occurred -------------------------------------*/ 01204 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01205 { 01206 huart->ErrorCode |= HAL_UART_ERROR_FE; 01207 } 01208 01209 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE); 01210 /* UART noise error interrupt occurred -------------------------------------*/ 01211 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01212 { 01213 huart->ErrorCode |= HAL_UART_ERROR_NE; 01214 } 01215 01216 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE); 01217 /* UART Over-Run interrupt occurred ----------------------------------------*/ 01218 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01219 { 01220 huart->ErrorCode |= HAL_UART_ERROR_ORE; 01221 } 01222 01223 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE); 01224 tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE); 01225 /* UART in mode Receiver ---------------------------------------------------*/ 01226 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01227 { 01228 UART_Receive_IT(huart); 01229 } 01230 01231 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE); 01232 tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE); 01233 /* UART in mode Transmitter ------------------------------------------------*/ 01234 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01235 { 01236 UART_Transmit_IT(huart); 01237 } 01238 01239 tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_TC); 01240 tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC); 01241 /* UART in mode Transmitter end --------------------------------------------*/ 01242 if((tmp_flag != RESET) && (tmp_it_source != RESET)) 01243 { 01244 UART_EndTransmit_IT(huart); 01245 } 01246 01247 if(huart->ErrorCode != HAL_UART_ERROR_NONE) 01248 { 01249 /* Clear all the error flag at once */ 01250 __HAL_UART_CLEAR_PEFLAG(huart); 01251 01252 /* Set the UART state ready to be able to start again the process */ 01253 huart->State = HAL_UART_STATE_READY; 01254 01255 HAL_UART_ErrorCallback(huart); 01256 } 01257 } 01258 01259 /** 01260 * @brief Tx Transfer completed callbacks. 01261 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01262 * the configuration information for the specified UART module. 01263 * @retval None 01264 */ 01265 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 01266 { 01267 /* Prevent unused argument(s) compilation warning */ 01268 UNUSED(huart); 01269 /* NOTE: This function should not be modified, when the callback is needed, 01270 the HAL_UART_TxCpltCallback can be implemented in the user file 01271 */ 01272 } 01273 01274 /** 01275 * @brief Tx Half Transfer completed callbacks. 01276 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01277 * the configuration information for the specified UART module. 01278 * @retval None 01279 */ 01280 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart) 01281 { 01282 /* Prevent unused argument(s) compilation warning */ 01283 UNUSED(huart); 01284 /* NOTE: This function should not be modified, when the callback is needed, 01285 the HAL_UART_TxHalfCpltCallback can be implemented in the user file 01286 */ 01287 } 01288 01289 /** 01290 * @brief Rx Transfer completed callbacks. 01291 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01292 * the configuration information for the specified UART module. 01293 * @retval None 01294 */ 01295 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 01296 { 01297 /* Prevent unused argument(s) compilation warning */ 01298 UNUSED(huart); 01299 /* NOTE: This function should not be modified, when the callback is needed, 01300 the HAL_UART_RxCpltCallback can be implemented in the user file 01301 */ 01302 } 01303 01304 /** 01305 * @brief Rx Half Transfer completed callbacks. 01306 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01307 * the configuration information for the specified UART module. 01308 * @retval None 01309 */ 01310 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) 01311 { 01312 /* Prevent unused argument(s) compilation warning */ 01313 UNUSED(huart); 01314 /* NOTE: This function should not be modified, when the callback is needed, 01315 the HAL_UART_RxHalfCpltCallback can be implemented in the user file 01316 */ 01317 } 01318 01319 /** 01320 * @brief UART error callbacks. 01321 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01322 * the configuration information for the specified UART module. 01323 * @retval None 01324 */ 01325 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) 01326 { 01327 /* Prevent unused argument(s) compilation warning */ 01328 UNUSED(huart); 01329 /* NOTE: This function should not be modified, when the callback is needed, 01330 the HAL_UART_ErrorCallback can be implemented in the user file 01331 */ 01332 } 01333 01334 /** 01335 * @} 01336 */ 01337 01338 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions 01339 * @brief UART control functions 01340 * 01341 @verbatim 01342 ============================================================================== 01343 ##### Peripheral Control functions ##### 01344 ============================================================================== 01345 [..] 01346 This subsection provides a set of functions allowing to control the UART: 01347 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character. 01348 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode. 01349 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software. 01350 (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode 01351 (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode 01352 01353 @endverbatim 01354 * @{ 01355 */ 01356 01357 /** 01358 * @brief Transmits break characters. 01359 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01360 * the configuration information for the specified UART module. 01361 * @retval HAL status 01362 */ 01363 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart) 01364 { 01365 /* Check the parameters */ 01366 assert_param(IS_UART_INSTANCE(huart->Instance)); 01367 01368 /* Process Locked */ 01369 __HAL_LOCK(huart); 01370 01371 huart->State = HAL_UART_STATE_BUSY; 01372 01373 /* Send break characters */ 01374 SET_BIT(huart->Instance->CR1, USART_CR1_SBK); 01375 01376 huart->State = HAL_UART_STATE_READY; 01377 01378 /* Process Unlocked */ 01379 __HAL_UNLOCK(huart); 01380 01381 return HAL_OK; 01382 } 01383 01384 /** 01385 * @brief Enters the UART in mute mode. 01386 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01387 * the configuration information for the specified UART module. 01388 * @retval HAL status 01389 */ 01390 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart) 01391 { 01392 /* Check the parameters */ 01393 assert_param(IS_UART_INSTANCE(huart->Instance)); 01394 01395 /* Process Locked */ 01396 __HAL_LOCK(huart); 01397 01398 huart->State = HAL_UART_STATE_BUSY; 01399 01400 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */ 01401 SET_BIT(huart->Instance->CR1, USART_CR1_RWU); 01402 01403 huart->State = HAL_UART_STATE_READY; 01404 01405 /* Process Unlocked */ 01406 __HAL_UNLOCK(huart); 01407 01408 return HAL_OK; 01409 } 01410 01411 /** 01412 * @brief Exits the UART mute mode: wake up software. 01413 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01414 * the configuration information for the specified UART module. 01415 * @retval HAL status 01416 */ 01417 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart) 01418 { 01419 /* Check the parameters */ 01420 assert_param(IS_UART_INSTANCE(huart->Instance)); 01421 01422 /* Process Locked */ 01423 __HAL_LOCK(huart); 01424 01425 huart->State = HAL_UART_STATE_BUSY; 01426 01427 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */ 01428 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU); 01429 01430 huart->State = HAL_UART_STATE_READY; 01431 01432 /* Process Unlocked */ 01433 __HAL_UNLOCK(huart); 01434 01435 return HAL_OK; 01436 } 01437 01438 /** 01439 * @brief Enables the UART transmitter and disables the UART receiver. 01440 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01441 * the configuration information for the specified UART module. 01442 * @retval HAL status 01443 */ 01444 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart) 01445 { 01446 /* Process Locked */ 01447 __HAL_LOCK(huart); 01448 01449 huart->State = HAL_UART_STATE_BUSY; 01450 01451 /*-------------------------- USART CR1 Configuration -----------------------*/ 01452 /* Clear TE and RE bits */ 01453 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */ 01454 MODIFY_REG(huart->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE), USART_CR1_TE); 01455 01456 huart->State = HAL_UART_STATE_READY; 01457 01458 /* Process Unlocked */ 01459 __HAL_UNLOCK(huart); 01460 01461 return HAL_OK; 01462 } 01463 01464 /** 01465 * @brief Enables the UART receiver and disables the UART transmitter. 01466 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01467 * the configuration information for the specified UART module. 01468 * @retval HAL status 01469 */ 01470 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart) 01471 { 01472 /* Process Locked */ 01473 __HAL_LOCK(huart); 01474 01475 huart->State = HAL_UART_STATE_BUSY; 01476 01477 /*-------------------------- USART CR1 Configuration -----------------------*/ 01478 /* Clear TE and RE bits */ 01479 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */ 01480 MODIFY_REG(huart->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE), USART_CR1_RE); 01481 01482 huart->State = HAL_UART_STATE_READY; 01483 01484 /* Process Unlocked */ 01485 __HAL_UNLOCK(huart); 01486 01487 return HAL_OK; 01488 } 01489 01490 /** 01491 * @} 01492 */ 01493 01494 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions 01495 * @brief UART State and Errors functions 01496 * 01497 @verbatim 01498 ============================================================================== 01499 ##### Peripheral State and Errors functions ##### 01500 ============================================================================== 01501 [..] 01502 This subsection provides a set of functions allowing to return the State of 01503 UART communication process, return Peripheral Errors occurred during communication 01504 process 01505 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral. 01506 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication. 01507 01508 @endverbatim 01509 * @{ 01510 */ 01511 01512 /** 01513 * @brief Returns the UART state. 01514 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01515 * the configuration information for the specified UART module. 01516 * @retval HAL state 01517 */ 01518 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart) 01519 { 01520 return huart->State; 01521 } 01522 01523 /** 01524 * @brief Return the UART error code 01525 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01526 * the configuration information for the specified UART. 01527 * @retval UART Error Code 01528 */ 01529 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart) 01530 { 01531 return huart->ErrorCode; 01532 } 01533 01534 /** 01535 * @} 01536 */ 01537 01538 /** 01539 * @} 01540 */ 01541 01542 /** @defgroup UART_Private_Functions UART Private Functions 01543 * @brief UART Private functions 01544 * @{ 01545 */ 01546 /** 01547 * @brief DMA UART transmit process complete callback. 01548 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains 01549 * the configuration information for the specified DMA module. 01550 * @retval None 01551 */ 01552 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma) 01553 { 01554 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 01555 /* DMA Normal mode*/ 01556 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) ) 01557 { 01558 huart->TxXferCount = 0; 01559 01560 /* Disable the DMA transfer for transmit request by setting the DMAT bit 01561 in the UART CR3 register */ 01562 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT); 01563 01564 /* Enable the UART Transmit Complete Interrupt */ 01565 __HAL_UART_ENABLE_IT(huart, UART_IT_TC); 01566 } 01567 /* DMA Circular mode */ 01568 else 01569 { 01570 HAL_UART_TxCpltCallback(huart); 01571 } 01572 } 01573 01574 /** 01575 * @brief DMA UART transmit process half complete callback 01576 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains 01577 * the configuration information for the specified DMA module. 01578 * @retval None 01579 */ 01580 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma) 01581 { 01582 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 01583 01584 HAL_UART_TxHalfCpltCallback(huart); 01585 } 01586 01587 /** 01588 * @brief DMA UART receive process complete callback. 01589 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains 01590 * the configuration information for the specified DMA module. 01591 * @retval None 01592 */ 01593 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma) 01594 { 01595 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 01596 /* DMA Normal mode*/ 01597 if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) ) 01598 { 01599 huart->RxXferCount = 0; 01600 01601 /* Disable the DMA transfer for the receiver request by setting the DMAR bit 01602 in the UART CR3 register */ 01603 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR); 01604 01605 /* Check if a transmit process is ongoing or not */ 01606 if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 01607 { 01608 huart->State = HAL_UART_STATE_BUSY_TX; 01609 } 01610 else 01611 { 01612 huart->State = HAL_UART_STATE_READY; 01613 } 01614 } 01615 HAL_UART_RxCpltCallback(huart); 01616 } 01617 01618 /** 01619 * @brief DMA UART receive process half complete callback 01620 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains 01621 * the configuration information for the specified DMA module. 01622 * @retval None 01623 */ 01624 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) 01625 { 01626 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; 01627 01628 HAL_UART_RxHalfCpltCallback(huart); 01629 } 01630 01631 /** 01632 * @brief DMA UART communication error callback. 01633 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains 01634 * the configuration information for the specified DMA module. 01635 * @retval None 01636 */ 01637 static void UART_DMAError(DMA_HandleTypeDef *hdma) 01638 { 01639 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 01640 huart->RxXferCount = 0; 01641 huart->TxXferCount = 0; 01642 huart->State= HAL_UART_STATE_READY; 01643 huart->ErrorCode |= HAL_UART_ERROR_DMA; 01644 HAL_UART_ErrorCallback(huart); 01645 } 01646 01647 /** 01648 * @brief This function handles UART Communication Timeout. 01649 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01650 * the configuration information for the specified UART module. 01651 * @param Flag: specifies the UART flag to check. 01652 * @param Status: The new Flag status (SET or RESET). 01653 * @param Timeout: Timeout duration 01654 * @retval HAL status 01655 */ 01656 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout) 01657 { 01658 uint32_t tickstart = 0; 01659 01660 /* Get tick */ 01661 tickstart = HAL_GetTick(); 01662 01663 /* Wait until flag is set */ 01664 if(Status == RESET) 01665 { 01666 while(__HAL_UART_GET_FLAG(huart, Flag) == RESET) 01667 { 01668 /* Check for the Timeout */ 01669 if(Timeout != HAL_MAX_DELAY) 01670 { 01671 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) 01672 { 01673 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ 01674 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); 01675 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); 01676 __HAL_UART_DISABLE_IT(huart, UART_IT_PE); 01677 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); 01678 01679 huart->State= HAL_UART_STATE_READY; 01680 01681 /* Process Unlocked */ 01682 __HAL_UNLOCK(huart); 01683 01684 return HAL_TIMEOUT; 01685 } 01686 } 01687 } 01688 } 01689 else 01690 { 01691 while(__HAL_UART_GET_FLAG(huart, Flag) != RESET) 01692 { 01693 /* Check for the Timeout */ 01694 if(Timeout != HAL_MAX_DELAY) 01695 { 01696 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout)) 01697 { 01698 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */ 01699 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); 01700 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); 01701 __HAL_UART_DISABLE_IT(huart, UART_IT_PE); 01702 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); 01703 01704 huart->State= HAL_UART_STATE_READY; 01705 01706 /* Process Unlocked */ 01707 __HAL_UNLOCK(huart); 01708 01709 return HAL_TIMEOUT; 01710 } 01711 } 01712 } 01713 } 01714 return HAL_OK; 01715 } 01716 01717 /** 01718 * @brief Sends an amount of data in non blocking mode. 01719 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01720 * the configuration information for the specified UART module. 01721 * @retval HAL status 01722 */ 01723 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart) 01724 { 01725 uint16_t* tmp; 01726 uint32_t tmp_state = 0; 01727 01728 tmp_state = huart->State; 01729 if((tmp_state == HAL_UART_STATE_BUSY_TX) || (tmp_state == HAL_UART_STATE_BUSY_TX_RX)) 01730 { 01731 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 01732 { 01733 tmp = (uint16_t*) huart->pTxBuffPtr; 01734 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF); 01735 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 01736 { 01737 huart->pTxBuffPtr += 2; 01738 } 01739 else 01740 { 01741 huart->pTxBuffPtr += 1; 01742 } 01743 } 01744 else 01745 { 01746 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF); 01747 } 01748 01749 if(--huart->TxXferCount == 0) 01750 { 01751 /* Disable the UART Transmit Complete Interrupt */ 01752 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE); 01753 01754 /* Enable the UART Transmit Complete Interrupt */ 01755 __HAL_UART_ENABLE_IT(huart, UART_IT_TC); 01756 } 01757 return HAL_OK; 01758 } 01759 else 01760 { 01761 return HAL_BUSY; 01762 } 01763 } 01764 01765 01766 /** 01767 * @brief Wraps up transmission in non blocking mode. 01768 * @param huart: pointer to a UART_HandleTypeDef structure that contains 01769 * the configuration information for the specified UART module. 01770 * @retval HAL status 01771 */ 01772 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart) 01773 { 01774 /* Disable the UART Transmit Complete Interrupt */ 01775 __HAL_UART_DISABLE_IT(huart, UART_IT_TC); 01776 01777 /* Check if a receive process is ongoing or not */ 01778 if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 01779 { 01780 huart->State = HAL_UART_STATE_BUSY_RX; 01781 } 01782 else 01783 { 01784 huart->State = HAL_UART_STATE_READY; 01785 } 01786 01787 HAL_UART_TxCpltCallback(huart); 01788 01789 return HAL_OK; 01790 } 01791 01792 /** 01793 * @brief Receives an amount of data in non blocking mode 01794 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01795 * the configuration information for the specified UART module. 01796 * @retval HAL status 01797 */ 01798 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart) 01799 { 01800 uint16_t* tmp; 01801 uint32_t tmp_state = 0; 01802 01803 tmp_state = huart->State; 01804 if((tmp_state == HAL_UART_STATE_BUSY_RX) || (tmp_state == HAL_UART_STATE_BUSY_TX_RX)) 01805 { 01806 if(huart->Init.WordLength == UART_WORDLENGTH_9B) 01807 { 01808 tmp = (uint16_t*) huart->pRxBuffPtr; 01809 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 01810 { 01811 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF); 01812 huart->pRxBuffPtr += 2; 01813 } 01814 else 01815 { 01816 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF); 01817 huart->pRxBuffPtr += 1; 01818 } 01819 } 01820 else 01821 { 01822 if(huart->Init.Parity == HAL_UART_PARITY_NONE) 01823 { 01824 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF); 01825 } 01826 else 01827 { 01828 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F); 01829 } 01830 } 01831 01832 if(--huart->RxXferCount == 0) 01833 { 01834 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE); 01835 01836 /* Check if a transmit process is ongoing or not */ 01837 if(huart->State == HAL_UART_STATE_BUSY_TX_RX) 01838 { 01839 huart->State = HAL_UART_STATE_BUSY_TX; 01840 } 01841 else 01842 { 01843 /* Disable the UART Parity Error Interrupt */ 01844 __HAL_UART_DISABLE_IT(huart, UART_IT_PE); 01845 01846 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */ 01847 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR); 01848 01849 huart->State = HAL_UART_STATE_READY; 01850 } 01851 HAL_UART_RxCpltCallback(huart); 01852 01853 return HAL_OK; 01854 } 01855 return HAL_OK; 01856 } 01857 else 01858 { 01859 return HAL_BUSY; 01860 } 01861 } 01862 01863 /** 01864 * @brief Configures the UART peripheral. 01865 * @param huart: Pointer to a UART_HandleTypeDef structure that contains 01866 * the configuration information for the specified UART module. 01867 * @retval None 01868 */ 01869 static void UART_SetConfig(UART_HandleTypeDef *huart) 01870 { 01871 uint32_t tmpreg = 0x00; 01872 01873 /* Check the parameters */ 01874 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate)); 01875 assert_param(IS_UART_STOPBITS(huart->Init.StopBits)); 01876 assert_param(IS_UART_PARITY(huart->Init.Parity)); 01877 assert_param(IS_UART_MODE(huart->Init.Mode)); 01878 01879 /*------- UART-associated USART registers setting : CR2 Configuration ------*/ 01880 /* Configure the UART Stop Bits: Set STOP[13:12] bits according 01881 * to huart->Init.StopBits value */ 01882 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits); 01883 01884 /*------- UART-associated USART registers setting : CR1 Configuration ------*/ 01885 /* Configure the UART Word Length, Parity and mode: 01886 Set the M bits according to huart->Init.WordLength value 01887 Set PCE and PS bits according to huart->Init.Parity value 01888 Set TE and RE bits according to huart->Init.Mode value */ 01889 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode ; 01890 MODIFY_REG(huart->Instance->CR1, 01891 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE), 01892 tmpreg); 01893 01894 /*------- UART-associated USART registers setting : CR3 Configuration ------*/ 01895 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */ 01896 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl); 01897 01898 /*------- UART-associated USART registers setting : BRR Configuration ------*/ 01899 if((huart->Instance == USART1)) 01900 { 01901 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate); 01902 } 01903 else 01904 { 01905 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate); 01906 } 01907 } 01908 /** 01909 * @} 01910 */ 01911 01912 #endif /* HAL_UART_MODULE_ENABLED */ 01913 /** 01914 * @} 01915 */ 01916 01917 /** 01918 * @} 01919 */ 01920 01921 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 12 2022 15:37:24 by
1.7.2