STM32L486xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32l4xx_hal_uart_ex.c 00004 * @author MCD Application Team 00005 * @brief Extended UART HAL module driver. 00006 * This file provides firmware functions to manage the following extended 00007 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART). 00008 * + Initialization and de-initialization functions 00009 * + Peripheral Control functions 00010 * 00011 * 00012 @verbatim 00013 ============================================================================== 00014 ##### UART peripheral extended features ##### 00015 ============================================================================== 00016 00017 (#) Declare a UART_HandleTypeDef handle structure. 00018 00019 (#) For the UART RS485 Driver Enable mode, initialize the UART registers 00020 by calling the HAL_RS485Ex_Init() API. 00021 00022 (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming. 00023 00024 -@- When UART operates in FIFO mode, FIFO mode must be enabled prior 00025 starting RX/TX transfers. Also RX/TX FIFO thresholds must be 00026 configured prior starting RX/TX transfers. 00027 00028 @endverbatim 00029 ****************************************************************************** 00030 * @attention 00031 * 00032 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00033 * 00034 * Redistribution and use in source and binary forms, with or without modification, 00035 * are permitted provided that the following conditions are met: 00036 * 1. Redistributions of source code must retain the above copyright notice, 00037 * this list of conditions and the following disclaimer. 00038 * 2. Redistributions in binary form must reproduce the above copyright notice, 00039 * this list of conditions and the following disclaimer in the documentation 00040 * and/or other materials provided with the distribution. 00041 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00042 * may be used to endorse or promote products derived from this software 00043 * without specific prior written permission. 00044 * 00045 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00046 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00047 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00048 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00049 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00050 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00051 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00052 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00053 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00054 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00055 * 00056 ****************************************************************************** 00057 */ 00058 00059 /* Includes ------------------------------------------------------------------*/ 00060 #include "stm32l4xx_hal.h" 00061 00062 /** @addtogroup STM32L4xx_HAL_Driver 00063 * @{ 00064 */ 00065 00066 /** @defgroup UARTEx UARTEx 00067 * @brief UART Extended HAL module driver 00068 * @{ 00069 */ 00070 00071 #ifdef HAL_UART_MODULE_ENABLED 00072 00073 /* Private typedef -----------------------------------------------------------*/ 00074 /* Private define ------------------------------------------------------------*/ 00075 #if defined(USART_CR1_FIFOEN) 00076 /* UART RX FIFO depth */ 00077 #define RX_FIFO_DEPTH 8U 00078 00079 /* UART TX FIFO depth */ 00080 #define TX_FIFO_DEPTH 8U 00081 #endif 00082 00083 /* Private macros ------------------------------------------------------------*/ 00084 /* Private variables ---------------------------------------------------------*/ 00085 /* Private function prototypes -----------------------------------------------*/ 00086 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions 00087 * @{ 00088 */ 00089 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00090 extern void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart); 00091 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */ 00092 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection); 00093 #if defined(USART_CR1_FIFOEN) 00094 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart); 00095 #endif 00096 /** 00097 * @} 00098 */ 00099 00100 /* Exported functions --------------------------------------------------------*/ 00101 00102 /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions 00103 * @{ 00104 */ 00105 00106 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions 00107 * @brief Extended Initialization and Configuration Functions 00108 * 00109 @verbatim 00110 =============================================================================== 00111 ##### Initialization and Configuration functions ##### 00112 =============================================================================== 00113 [..] 00114 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy 00115 in asynchronous mode. 00116 (+) For the asynchronous mode the parameters below can be configured: 00117 (++) Baud Rate 00118 (++) Word Length 00119 (++) Stop Bit 00120 (++) Parity: If the parity is enabled, then the MSB bit of the data written 00121 in the data register is transmitted but is changed by the parity bit. 00122 (++) Hardware flow control 00123 (++) Receiver/transmitter modes 00124 (++) Over Sampling Method 00125 (++) One-Bit Sampling Method 00126 (+) For the asynchronous mode, the following advanced features can be configured as well: 00127 (++) TX and/or RX pin level inversion 00128 (++) data logical level inversion 00129 (++) RX and TX pins swap 00130 (++) RX overrun detection disabling 00131 (++) DMA disabling on RX error 00132 (++) MSB first on communication line 00133 (++) auto Baud rate detection 00134 [..] 00135 The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration 00136 procedures (details for the procedures are available in reference manual). 00137 00138 @endverbatim 00139 00140 Depending on the frame length defined by the M1 and M0 bits (7-bit, 00141 8-bit or 9-bit), the possible UART formats are listed in the 00142 following table. 00143 00144 Table 1. UART frame format. 00145 +-----------------------------------------------------------------------+ 00146 | M1 bit | M0 bit | PCE bit | UART frame | 00147 |---------|---------|-----------|---------------------------------------| 00148 | 0 | 0 | 0 | | SB | 8 bit data | STB | | 00149 |---------|---------|-----------|---------------------------------------| 00150 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | | 00151 |---------|---------|-----------|---------------------------------------| 00152 | 0 | 1 | 0 | | SB | 9 bit data | STB | | 00153 |---------|---------|-----------|---------------------------------------| 00154 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | | 00155 |---------|---------|-----------|---------------------------------------| 00156 | 1 | 0 | 0 | | SB | 7 bit data | STB | | 00157 |---------|---------|-----------|---------------------------------------| 00158 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | | 00159 +-----------------------------------------------------------------------+ 00160 00161 * @{ 00162 */ 00163 00164 /** 00165 * @brief Initialize the RS485 Driver enable feature according to the specified 00166 * parameters in the UART_InitTypeDef and creates the associated handle. 00167 * @param huart UART handle. 00168 * @param Polarity Select the driver enable polarity. 00169 * This parameter can be one of the following values: 00170 * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high 00171 * @arg @ref UART_DE_POLARITY_LOW DE signal is active low 00172 * @param AssertionTime Driver Enable assertion time: 00173 * 5-bit value defining the time between the activation of the DE (Driver Enable) 00174 * signal and the beginning of the start bit. It is expressed in sample time 00175 * units (1/8 or 1/16 bit time, depending on the oversampling rate) 00176 * @param DeassertionTime Driver Enable deassertion time: 00177 * 5-bit value defining the time between the end of the last stop bit, in a 00178 * transmitted message, and the de-activation of the DE (Driver Enable) signal. 00179 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the 00180 * oversampling rate). 00181 * @retval HAL status 00182 */ 00183 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime) 00184 { 00185 uint32_t temp = 0x0U; 00186 00187 /* Check the UART handle allocation */ 00188 if (huart == NULL) 00189 { 00190 return HAL_ERROR; 00191 } 00192 /* Check the Driver Enable UART instance */ 00193 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance)); 00194 00195 /* Check the Driver Enable polarity */ 00196 assert_param(IS_UART_DE_POLARITY(Polarity)); 00197 00198 /* Check the Driver Enable assertion time */ 00199 assert_param(IS_UART_ASSERTIONTIME(AssertionTime)); 00200 00201 /* Check the Driver Enable deassertion time */ 00202 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime)); 00203 00204 if (huart->gState == HAL_UART_STATE_RESET) 00205 { 00206 /* Allocate lock resource and initialize it */ 00207 huart->Lock = HAL_UNLOCKED; 00208 00209 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1) 00210 UART_InitCallbacksToDefault(huart); 00211 00212 if (huart->MspInitCallback == NULL) 00213 { 00214 huart->MspInitCallback = HAL_UART_MspInit; 00215 } 00216 00217 /* Init the low level hardware */ 00218 huart->MspInitCallback(huart); 00219 #else 00220 /* Init the low level hardware : GPIO, CLOCK, CORTEX */ 00221 HAL_UART_MspInit(huart); 00222 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */ 00223 } 00224 00225 huart->gState = HAL_UART_STATE_BUSY; 00226 00227 /* Disable the Peripheral */ 00228 __HAL_UART_DISABLE(huart); 00229 00230 /* Set the UART Communication parameters */ 00231 if (UART_SetConfig(huart) == HAL_ERROR) 00232 { 00233 return HAL_ERROR; 00234 } 00235 00236 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT) 00237 { 00238 UART_AdvFeatureConfig(huart); 00239 } 00240 00241 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */ 00242 SET_BIT(huart->Instance->CR3, USART_CR3_DEM); 00243 00244 /* Set the Driver Enable polarity */ 00245 MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity); 00246 00247 /* Set the Driver Enable assertion and deassertion times */ 00248 temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS); 00249 temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS); 00250 MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp); 00251 00252 /* Enable the Peripheral */ 00253 __HAL_UART_ENABLE(huart); 00254 00255 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */ 00256 return (UART_CheckIdleState(huart)); 00257 } 00258 00259 00260 /** 00261 * @} 00262 */ 00263 00264 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions 00265 * @brief Extended functions 00266 * 00267 @verbatim 00268 =============================================================================== 00269 ##### IO operation functions ##### 00270 =============================================================================== 00271 This subsection provides a set of Wakeup and FIFO mode related callback functions. 00272 00273 (#) Wakeup from Stop mode Callback: 00274 (+) HAL_UARTEx_WakeupCallback() 00275 00276 (#) TX/RX Fifos Callbacks: 00277 (+) HAL_UARTEx_RxFifoFullCallback() 00278 (+) HAL_UARTEx_TxFifoEmptyCallback() 00279 00280 @endverbatim 00281 * @{ 00282 */ 00283 00284 /** 00285 * @brief UART wakeup from Stop mode callback. 00286 * @param huart UART handle. 00287 * @retval None 00288 */ 00289 __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart) 00290 { 00291 /* Prevent unused argument(s) compilation warning */ 00292 UNUSED(huart); 00293 00294 /* NOTE : This function should not be modified, when the callback is needed, 00295 the HAL_UARTEx_WakeupCallback can be implemented in the user file. 00296 */ 00297 } 00298 00299 #if defined(USART_CR1_FIFOEN) 00300 /** 00301 * @brief UART RX Fifo full callback. 00302 * @param huart UART handle. 00303 * @retval None 00304 */ 00305 __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart) 00306 { 00307 /* Prevent unused argument(s) compilation warning */ 00308 UNUSED(huart); 00309 00310 /* NOTE : This function should not be modified, when the callback is needed, 00311 the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file. 00312 */ 00313 } 00314 00315 /** 00316 * @brief UART TX Fifo empty callback. 00317 * @param huart UART handle. 00318 * @retval None 00319 */ 00320 __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart) 00321 { 00322 /* Prevent unused argument(s) compilation warning */ 00323 UNUSED(huart); 00324 00325 /* NOTE : This function should not be modified, when the callback is needed, 00326 the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file. 00327 */ 00328 } 00329 #endif 00330 00331 /** 00332 * @} 00333 */ 00334 00335 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions 00336 * @brief Extended Peripheral Control functions 00337 * 00338 @verbatim 00339 =============================================================================== 00340 ##### Peripheral Control functions ##### 00341 =============================================================================== 00342 [..] This section provides the following functions: 00343 (+) HAL_UARTEx_EnableClockStopMode() API enables the UART clock (HSI or LSE only) during stop mode 00344 (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality 00345 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address 00346 detection length to more than 4 bits for multiprocessor address mark wake up. 00347 (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode 00348 trigger: address match, Start Bit detection or RXNE bit status. 00349 (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode 00350 (+) HAL_UARTEx_DisableStopMode() API disables the above functionality 00351 (+) HAL_UARTEx_WakeupCallback() called upon UART wakeup interrupt 00352 (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode 00353 (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode 00354 (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold 00355 (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold 00356 00357 @endverbatim 00358 * @{ 00359 */ 00360 00361 00362 00363 #if defined(USART_CR3_UCESM) 00364 /** 00365 * @brief Keep UART Clock enabled when in Stop Mode. 00366 * @note When the USART clock source is configured to be LSE or HSI, it is possible to keep enabled 00367 * this clock during STOP mode by setting the UCESM bit in USART_CR3 control register. 00368 * @note When LPUART is used to wakeup from stop with LSE is selected as LPUART clock source, 00369 * and desired baud rate is 9600 baud, the bit UCESM bit in LPUART_CR3 control register must be set. 00370 * @param huart UART handle. 00371 * @retval HAL status 00372 */ 00373 HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart) 00374 { 00375 /* Process Locked */ 00376 __HAL_LOCK(huart); 00377 00378 /* Set UCESM bit */ 00379 SET_BIT(huart->Instance->CR3, USART_CR3_UCESM); 00380 00381 /* Process Unlocked */ 00382 __HAL_UNLOCK(huart); 00383 00384 return HAL_OK; 00385 } 00386 00387 /** 00388 * @brief Disable UART Clock when in Stop Mode. 00389 * @param huart UART handle. 00390 * @retval HAL status 00391 */ 00392 HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart) 00393 { 00394 /* Process Locked */ 00395 __HAL_LOCK(huart); 00396 00397 /* Clear UCESM bit */ 00398 CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM); 00399 00400 /* Process Unlocked */ 00401 __HAL_UNLOCK(huart); 00402 00403 return HAL_OK; 00404 } 00405 #endif /* USART_CR3_UCESM */ 00406 00407 /** 00408 * @brief By default in multiprocessor mode, when the wake up method is set 00409 * to address mark, the UART handles only 4-bit long addresses detection; 00410 * this API allows to enable longer addresses detection (6-, 7- or 8-bit 00411 * long). 00412 * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode, 00413 * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode. 00414 * @param huart UART handle. 00415 * @param AddressLength This parameter can be one of the following values: 00416 * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address 00417 * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address 00418 * @retval HAL status 00419 */ 00420 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength) 00421 { 00422 /* Check the UART handle allocation */ 00423 if (huart == NULL) 00424 { 00425 return HAL_ERROR; 00426 } 00427 00428 /* Check the address length parameter */ 00429 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength)); 00430 00431 huart->gState = HAL_UART_STATE_BUSY; 00432 00433 /* Disable the Peripheral */ 00434 __HAL_UART_DISABLE(huart); 00435 00436 /* Set the address length */ 00437 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength); 00438 00439 /* Enable the Peripheral */ 00440 __HAL_UART_ENABLE(huart); 00441 00442 /* TEACK and/or REACK to check before moving huart->gState to Ready */ 00443 return (UART_CheckIdleState(huart)); 00444 } 00445 00446 00447 /** 00448 * @brief Set Wakeup from Stop mode interrupt flag selection. 00449 * @note It is the application responsibility to enable the interrupt used as 00450 * usart_wkup interrupt source before entering low-power mode. 00451 * @param huart UART handle. 00452 * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status. 00453 * This parameter can be one of the following values: 00454 * @arg @ref UART_WAKEUP_ON_ADDRESS 00455 * @arg @ref UART_WAKEUP_ON_STARTBIT 00456 * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY 00457 * @retval HAL status 00458 */ 00459 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection) 00460 { 00461 HAL_StatusTypeDef status = HAL_OK; 00462 uint32_t tickstart = 0U; 00463 00464 /* check the wake-up from stop mode UART instance */ 00465 assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance)); 00466 /* check the wake-up selection parameter */ 00467 assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent)); 00468 00469 /* Process Locked */ 00470 __HAL_LOCK(huart); 00471 00472 huart->gState = HAL_UART_STATE_BUSY; 00473 00474 /* Disable the Peripheral */ 00475 __HAL_UART_DISABLE(huart); 00476 00477 /* Set the wake-up selection scheme */ 00478 MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent); 00479 00480 if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS) 00481 { 00482 UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection); 00483 } 00484 00485 /* Enable the Peripheral */ 00486 __HAL_UART_ENABLE(huart); 00487 00488 /* Init tickstart for timeout managment*/ 00489 tickstart = HAL_GetTick(); 00490 00491 /* Wait until REACK flag is set */ 00492 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK) 00493 { 00494 status = HAL_TIMEOUT; 00495 } 00496 else 00497 { 00498 /* Initialize the UART State */ 00499 huart->gState = HAL_UART_STATE_READY; 00500 } 00501 00502 /* Process Unlocked */ 00503 __HAL_UNLOCK(huart); 00504 00505 return status; 00506 } 00507 00508 00509 /** 00510 * @brief Enable UART Stop Mode. 00511 * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE. 00512 * @param huart UART handle. 00513 * @retval HAL status 00514 */ 00515 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart) 00516 { 00517 /* Process Locked */ 00518 __HAL_LOCK(huart); 00519 00520 /* Set UESM bit */ 00521 SET_BIT(huart->Instance->CR1, USART_CR1_UESM); 00522 00523 /* Process Unlocked */ 00524 __HAL_UNLOCK(huart); 00525 00526 return HAL_OK; 00527 } 00528 00529 /** 00530 * @brief Disable UART Stop Mode. 00531 * @param huart UART handle. 00532 * @retval HAL status 00533 */ 00534 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart) 00535 { 00536 /* Process Locked */ 00537 __HAL_LOCK(huart); 00538 00539 /* Clear UESM bit */ 00540 CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM); 00541 00542 /* Process Unlocked */ 00543 __HAL_UNLOCK(huart); 00544 00545 return HAL_OK; 00546 } 00547 00548 #if defined(USART_CR1_FIFOEN) 00549 /** 00550 * @brief Enable the FIFO mode. 00551 * @param huart UART handle. 00552 * @retval HAL status 00553 */ 00554 HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart) 00555 { 00556 uint32_t tmpcr1 = 0U; 00557 00558 /* Check parameters */ 00559 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance)); 00560 00561 /* Process Locked */ 00562 __HAL_LOCK(huart); 00563 00564 huart->gState = HAL_UART_STATE_BUSY; 00565 00566 /* Save actual UART configuration */ 00567 tmpcr1 = READ_REG(huart->Instance->CR1); 00568 00569 /* Disable UART */ 00570 __HAL_UART_DISABLE(huart); 00571 00572 /* Enable FIFO mode */ 00573 SET_BIT(tmpcr1, USART_CR1_FIFOEN); 00574 huart->FifoMode = UART_FIFOMODE_ENABLE; 00575 00576 /* Restore UART configuration */ 00577 WRITE_REG(huart->Instance->CR1, tmpcr1); 00578 00579 /* Determine the number of data to process during RX/TX ISR execution */ 00580 UARTEx_SetNbDataToProcess(huart); 00581 00582 huart->gState = HAL_UART_STATE_READY; 00583 00584 /* Process Unlocked */ 00585 __HAL_UNLOCK(huart); 00586 00587 return HAL_OK; 00588 } 00589 00590 /** 00591 * @brief Disable the FIFO mode. 00592 * @param huart UART handle. 00593 * @retval HAL status 00594 */ 00595 HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart) 00596 { 00597 uint32_t tmpcr1 = 0U; 00598 00599 /* Check parameters */ 00600 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance)); 00601 00602 /* Process Locked */ 00603 __HAL_LOCK(huart); 00604 00605 huart->gState = HAL_UART_STATE_BUSY; 00606 00607 /* Save actual UART configuration */ 00608 tmpcr1 = READ_REG(huart->Instance->CR1); 00609 00610 /* Disable UART */ 00611 __HAL_UART_DISABLE(huart); 00612 00613 /* Enable FIFO mode */ 00614 CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN); 00615 huart->FifoMode = UART_FIFOMODE_DISABLE; 00616 00617 /* Restore UART configuration */ 00618 WRITE_REG(huart->Instance->CR1, tmpcr1); 00619 00620 huart->gState = HAL_UART_STATE_READY; 00621 00622 /* Process Unlocked */ 00623 __HAL_UNLOCK(huart); 00624 00625 return HAL_OK; 00626 } 00627 00628 /** 00629 * @brief Set the TXFIFO threshold. 00630 * @param huart UART handle. 00631 * @param Threshold TX FIFO threshold value 00632 * This parameter can be one of the following values: 00633 * @arg @ref UART_TXFIFO_THRESHOLD_1_8 00634 * @arg @ref UART_TXFIFO_THRESHOLD_1_4 00635 * @arg @ref UART_TXFIFO_THRESHOLD_1_2 00636 * @arg @ref UART_TXFIFO_THRESHOLD_3_4 00637 * @arg @ref UART_TXFIFO_THRESHOLD_7_8 00638 * @arg @ref UART_TXFIFO_THRESHOLD_8_8 00639 * @retval HAL status 00640 */ 00641 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold) 00642 { 00643 uint32_t tmpcr1 = 0U; 00644 00645 /* Check parameters */ 00646 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance)); 00647 assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold)); 00648 00649 /* Process Locked */ 00650 __HAL_LOCK(huart); 00651 00652 huart->gState = HAL_UART_STATE_BUSY; 00653 00654 /* Save actual UART configuration */ 00655 tmpcr1 = READ_REG(huart->Instance->CR1); 00656 00657 /* Disable UART */ 00658 __HAL_UART_DISABLE(huart); 00659 00660 /* Update TX threshold configuration */ 00661 MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold); 00662 00663 /* Determine the number of data to process during RX/TX ISR execution */ 00664 UARTEx_SetNbDataToProcess(huart); 00665 00666 /* Restore UART configuration */ 00667 WRITE_REG(huart->Instance->CR1, tmpcr1); 00668 00669 huart->gState = HAL_UART_STATE_READY; 00670 00671 /* Process Unlocked */ 00672 __HAL_UNLOCK(huart); 00673 00674 return HAL_OK; 00675 } 00676 00677 /** 00678 * @brief Set the RXFIFO threshold. 00679 * @param huart UART handle. 00680 * @param Threshold RX FIFO threshold value 00681 * This parameter can be one of the following values: 00682 * @arg @ref UART_RXFIFO_THRESHOLD_1_8 00683 * @arg @ref UART_RXFIFO_THRESHOLD_1_4 00684 * @arg @ref UART_RXFIFO_THRESHOLD_1_2 00685 * @arg @ref UART_RXFIFO_THRESHOLD_3_4 00686 * @arg @ref UART_RXFIFO_THRESHOLD_7_8 00687 * @arg @ref UART_RXFIFO_THRESHOLD_8_8 00688 * @retval HAL status 00689 */ 00690 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold) 00691 { 00692 uint32_t tmpcr1 = 0U; 00693 00694 /* Check the parameters */ 00695 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance)); 00696 assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold)); 00697 00698 /* Process Locked */ 00699 __HAL_LOCK(huart); 00700 00701 huart->gState = HAL_UART_STATE_BUSY; 00702 00703 /* Save actual UART configuration */ 00704 tmpcr1 = READ_REG(huart->Instance->CR1); 00705 00706 /* Disable UART */ 00707 __HAL_UART_DISABLE(huart); 00708 00709 /* Update RX threshold configuration */ 00710 MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold); 00711 00712 /* Determine the number of data to process during RX/TX ISR execution */ 00713 UARTEx_SetNbDataToProcess(huart); 00714 00715 /* Restore UART configuration */ 00716 WRITE_REG(huart->Instance->CR1, tmpcr1); 00717 00718 huart->gState = HAL_UART_STATE_READY; 00719 00720 /* Process Unlocked */ 00721 __HAL_UNLOCK(huart); 00722 00723 return HAL_OK; 00724 } 00725 #endif 00726 00727 /** 00728 * @} 00729 */ 00730 00731 /** 00732 * @} 00733 */ 00734 00735 /** @addtogroup UARTEx_Private_Functions 00736 * @{ 00737 */ 00738 00739 /** 00740 * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection. 00741 * @param huart UART handle. 00742 * @param WakeUpSelection UART wake up from stop mode parameters. 00743 * @retval None 00744 */ 00745 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection) 00746 { 00747 assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength)); 00748 00749 /* Set the USART address length */ 00750 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength); 00751 00752 /* Set the USART address node */ 00753 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS)); 00754 } 00755 00756 #if defined(USART_CR1_FIFOEN) 00757 /** 00758 * @brief Calculate the number of data to process in RX/TX ISR. 00759 * @note The RX FIFO depth and the TX FIFO depth is extracted from 00760 * the UART configuration registers. 00761 * @param huart UART handle. 00762 * @retval None 00763 */ 00764 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart) 00765 { 00766 uint8_t rx_fifo_depth; 00767 uint8_t tx_fifo_depth; 00768 uint8_t rx_fifo_threshold; 00769 uint8_t tx_fifo_threshold; 00770 uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U}; 00771 uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U}; 00772 00773 if (huart->FifoMode == UART_FIFOMODE_DISABLE) 00774 { 00775 huart->NbTxDataToProcess = 1U; 00776 huart->NbRxDataToProcess = 1U; 00777 } 00778 else 00779 { 00780 rx_fifo_depth = RX_FIFO_DEPTH; 00781 tx_fifo_depth = TX_FIFO_DEPTH; 00782 rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos); 00783 tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos); 00784 huart->NbTxDataToProcess = (uint8_t)(tx_fifo_depth * numerator[tx_fifo_threshold]) / denominator[tx_fifo_threshold]; 00785 huart->NbRxDataToProcess = (uint8_t)(rx_fifo_depth * numerator[rx_fifo_threshold]) / denominator[rx_fifo_threshold]; 00786 } 00787 } 00788 #endif 00789 /** 00790 * @} 00791 */ 00792 00793 #endif /* HAL_UART_MODULE_ENABLED */ 00794 00795 /** 00796 * @} 00797 */ 00798 00799 /** 00800 * @} 00801 */ 00802 00803 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/