2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_uart_ex.c
\r
4 * @author MCD Application Team
\r
5 * @brief Extended UART HAL module driver.
\r
6 * This file provides firmware functions to manage the following extended
\r
7 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
\r
8 * + Initialization and de-initialization functions
\r
9 * + Peripheral Control functions
\r
13 ==============================================================================
\r
14 ##### UART peripheral extended features #####
\r
15 ==============================================================================
\r
17 (#) Declare a UART_HandleTypeDef handle structure.
\r
19 (#) For the UART RS485 Driver Enable mode, initialize the UART registers
\r
20 by calling the HAL_RS485Ex_Init() API.
\r
22 (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
\r
24 -@- When UART operates in FIFO mode, FIFO mode must be enabled prior
\r
25 starting RX/TX transfers. Also RX/TX FIFO thresholds must be
\r
26 configured prior starting RX/TX transfers.
\r
29 ******************************************************************************
\r
32 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
33 * All rights reserved.</center></h2>
\r
35 * This software component is licensed by ST under BSD 3-Clause license,
\r
36 * the "License"; You may not use this file except in compliance with the
\r
37 * License. You may obtain a copy of the License at:
\r
38 * opensource.org/licenses/BSD-3-Clause
\r
40 ******************************************************************************
\r
43 /* Includes ------------------------------------------------------------------*/
\r
44 #include "stm32l4xx_hal.h"
\r
46 /** @addtogroup STM32L4xx_HAL_Driver
\r
50 /** @defgroup UARTEx UARTEx
\r
51 * @brief UART Extended HAL module driver
\r
55 #ifdef HAL_UART_MODULE_ENABLED
\r
57 /* Private typedef -----------------------------------------------------------*/
\r
58 /* Private define ------------------------------------------------------------*/
\r
59 #if defined(USART_CR1_FIFOEN)
\r
60 /** @defgroup UARTEX_Private_Constants UARTEx Private Constants
\r
63 /* UART RX FIFO depth */
\r
64 #define RX_FIFO_DEPTH 8U
\r
66 /* UART TX FIFO depth */
\r
67 #define TX_FIFO_DEPTH 8U
\r
71 #endif /* USART_CR1_FIFOEN */
\r
73 /* Private macros ------------------------------------------------------------*/
\r
74 /* Private variables ---------------------------------------------------------*/
\r
75 /* Private function prototypes -----------------------------------------------*/
\r
76 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
\r
79 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
\r
80 #if defined(USART_CR1_FIFOEN)
\r
81 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart);
\r
82 #endif /* USART_CR1_FIFOEN */
\r
87 /* Exported functions --------------------------------------------------------*/
\r
89 /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
\r
93 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
\r
94 * @brief Extended Initialization and Configuration Functions
\r
97 ===============================================================================
\r
98 ##### Initialization and Configuration functions #####
\r
99 ===============================================================================
\r
101 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
\r
102 in asynchronous mode.
\r
103 (+) For the asynchronous mode the parameters below can be configured:
\r
107 (++) Parity: If the parity is enabled, then the MSB bit of the data written
\r
108 in the data register is transmitted but is changed by the parity bit.
\r
109 (++) Hardware flow control
\r
110 (++) Receiver/transmitter modes
\r
111 (++) Over Sampling Method
\r
112 (++) One-Bit Sampling Method
\r
113 (+) For the asynchronous mode, the following advanced features can be configured as well:
\r
114 (++) TX and/or RX pin level inversion
\r
115 (++) data logical level inversion
\r
116 (++) RX and TX pins swap
\r
117 (++) RX overrun detection disabling
\r
118 (++) DMA disabling on RX error
\r
119 (++) MSB first on communication line
\r
120 (++) auto Baud rate detection
\r
122 The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
\r
123 procedures (details for the procedures are available in reference manual).
\r
127 Depending on the frame length defined by the M1 and M0 bits (7-bit,
\r
128 8-bit or 9-bit), the possible UART formats are listed in the
\r
131 Table 1. UART frame format.
\r
132 +-----------------------------------------------------------------------+
\r
133 | M1 bit | M0 bit | PCE bit | UART frame |
\r
134 |---------|---------|-----------|---------------------------------------|
\r
135 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
\r
136 |---------|---------|-----------|---------------------------------------|
\r
137 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
\r
138 |---------|---------|-----------|---------------------------------------|
\r
139 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
\r
140 |---------|---------|-----------|---------------------------------------|
\r
141 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
\r
142 |---------|---------|-----------|---------------------------------------|
\r
143 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
\r
144 |---------|---------|-----------|---------------------------------------|
\r
145 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
\r
146 +-----------------------------------------------------------------------+
\r
152 * @brief Initialize the RS485 Driver enable feature according to the specified
\r
153 * parameters in the UART_InitTypeDef and creates the associated handle.
\r
154 * @param huart UART handle.
\r
155 * @param Polarity Select the driver enable polarity.
\r
156 * This parameter can be one of the following values:
\r
157 * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
\r
158 * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
\r
159 * @param AssertionTime Driver Enable assertion time:
\r
160 * 5-bit value defining the time between the activation of the DE (Driver Enable)
\r
161 * signal and the beginning of the start bit. It is expressed in sample time
\r
162 * units (1/8 or 1/16 bit time, depending on the oversampling rate)
\r
163 * @param DeassertionTime Driver Enable deassertion time:
\r
164 * 5-bit value defining the time between the end of the last stop bit, in a
\r
165 * transmitted message, and the de-activation of the DE (Driver Enable) signal.
\r
166 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
\r
167 * oversampling rate).
\r
168 * @retval HAL status
\r
170 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
\r
171 uint32_t DeassertionTime)
\r
175 /* Check the UART handle allocation */
\r
180 /* Check the Driver Enable UART instance */
\r
181 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
\r
183 /* Check the Driver Enable polarity */
\r
184 assert_param(IS_UART_DE_POLARITY(Polarity));
\r
186 /* Check the Driver Enable assertion time */
\r
187 assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
\r
189 /* Check the Driver Enable deassertion time */
\r
190 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
\r
192 if (huart->gState == HAL_UART_STATE_RESET)
\r
194 /* Allocate lock resource and initialize it */
\r
195 huart->Lock = HAL_UNLOCKED;
\r
197 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
198 UART_InitCallbacksToDefault(huart);
\r
200 if (huart->MspInitCallback == NULL)
\r
202 huart->MspInitCallback = HAL_UART_MspInit;
\r
205 /* Init the low level hardware */
\r
206 huart->MspInitCallback(huart);
\r
208 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
\r
209 HAL_UART_MspInit(huart);
\r
210 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
213 huart->gState = HAL_UART_STATE_BUSY;
\r
215 /* Disable the Peripheral */
\r
216 __HAL_UART_DISABLE(huart);
\r
218 /* Set the UART Communication parameters */
\r
219 if (UART_SetConfig(huart) == HAL_ERROR)
\r
224 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
226 UART_AdvFeatureConfig(huart);
\r
229 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
\r
230 SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
\r
232 /* Set the Driver Enable polarity */
\r
233 MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
\r
235 /* Set the Driver Enable assertion and deassertion times */
\r
236 temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
\r
237 temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
\r
238 MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
\r
240 /* Enable the Peripheral */
\r
241 __HAL_UART_ENABLE(huart);
\r
243 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
244 return (UART_CheckIdleState(huart));
\r
251 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
\r
252 * @brief Extended functions
\r
255 ===============================================================================
\r
256 ##### IO operation functions #####
\r
257 ===============================================================================
\r
258 This subsection provides a set of Wakeup and FIFO mode related callback functions.
\r
260 (#) Wakeup from Stop mode Callback:
\r
261 (+) HAL_UARTEx_WakeupCallback()
\r
263 (#) TX/RX Fifos Callbacks:
\r
264 (+) HAL_UARTEx_RxFifoFullCallback()
\r
265 (+) HAL_UARTEx_TxFifoEmptyCallback()
\r
272 * @brief UART wakeup from Stop mode callback.
\r
273 * @param huart UART handle.
\r
276 __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
\r
278 /* Prevent unused argument(s) compilation warning */
\r
281 /* NOTE : This function should not be modified, when the callback is needed,
\r
282 the HAL_UARTEx_WakeupCallback can be implemented in the user file.
\r
286 #if defined(USART_CR1_FIFOEN)
\r
288 * @brief UART RX Fifo full callback.
\r
289 * @param huart UART handle.
\r
292 __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)
\r
294 /* Prevent unused argument(s) compilation warning */
\r
297 /* NOTE : This function should not be modified, when the callback is needed,
\r
298 the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
\r
303 * @brief UART TX Fifo empty callback.
\r
304 * @param huart UART handle.
\r
307 __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)
\r
309 /* Prevent unused argument(s) compilation warning */
\r
312 /* NOTE : This function should not be modified, when the callback is needed,
\r
313 the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
\r
316 #endif /* USART_CR1_FIFOEN */
\r
322 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
\r
323 * @brief Extended Peripheral Control functions
\r
326 ===============================================================================
\r
327 ##### Peripheral Control functions #####
\r
328 ===============================================================================
\r
329 [..] This section provides the following functions:
\r
330 (+) HAL_UARTEx_EnableClockStopMode() API enables the UART clock (HSI or LSE only) during stop mode
\r
331 (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality
\r
332 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
\r
333 detection length to more than 4 bits for multiprocessor address mark wake up.
\r
334 (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
\r
335 trigger: address match, Start Bit detection or RXNE bit status.
\r
336 (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
\r
337 (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
\r
338 (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
\r
339 (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
\r
340 (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
\r
341 (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
\r
349 #if defined(USART_CR3_UCESM)
\r
351 * @brief Keep UART Clock enabled when in Stop Mode.
\r
352 * @note When the USART clock source is configured to be LSE or HSI, it is possible to keep enabled
\r
353 * this clock during STOP mode by setting the UCESM bit in USART_CR3 control register.
\r
354 * @note When LPUART is used to wakeup from stop with LSE is selected as LPUART clock source,
\r
355 * and desired baud rate is 9600 baud, the bit UCESM bit in LPUART_CR3 control register must be set.
\r
356 * @param huart UART handle.
\r
357 * @retval HAL status
\r
359 HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
\r
361 /* Process Locked */
\r
364 /* Set UCESM bit */
\r
365 SET_BIT(huart->Instance->CR3, USART_CR3_UCESM);
\r
367 /* Process Unlocked */
\r
368 __HAL_UNLOCK(huart);
\r
374 * @brief Disable UART Clock when in Stop Mode.
\r
375 * @param huart UART handle.
\r
376 * @retval HAL status
\r
378 HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
\r
380 /* Process Locked */
\r
383 /* Clear UCESM bit */
\r
384 CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM);
\r
386 /* Process Unlocked */
\r
387 __HAL_UNLOCK(huart);
\r
391 #endif /* USART_CR3_UCESM */
\r
394 * @brief By default in multiprocessor mode, when the wake up method is set
\r
395 * to address mark, the UART handles only 4-bit long addresses detection;
\r
396 * this API allows to enable longer addresses detection (6-, 7- or 8-bit
\r
398 * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
\r
399 * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
\r
400 * @param huart UART handle.
\r
401 * @param AddressLength This parameter can be one of the following values:
\r
402 * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
\r
403 * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
\r
404 * @retval HAL status
\r
406 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
\r
408 /* Check the UART handle allocation */
\r
414 /* Check the address length parameter */
\r
415 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
\r
417 huart->gState = HAL_UART_STATE_BUSY;
\r
419 /* Disable the Peripheral */
\r
420 __HAL_UART_DISABLE(huart);
\r
422 /* Set the address length */
\r
423 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
\r
425 /* Enable the Peripheral */
\r
426 __HAL_UART_ENABLE(huart);
\r
428 /* TEACK and/or REACK to check before moving huart->gState to Ready */
\r
429 return (UART_CheckIdleState(huart));
\r
433 * @brief Set Wakeup from Stop mode interrupt flag selection.
\r
434 * @note It is the application responsibility to enable the interrupt used as
\r
435 * usart_wkup interrupt source before entering low-power mode.
\r
436 * @param huart UART handle.
\r
437 * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
\r
438 * This parameter can be one of the following values:
\r
439 * @arg @ref UART_WAKEUP_ON_ADDRESS
\r
440 * @arg @ref UART_WAKEUP_ON_STARTBIT
\r
441 * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
\r
442 * @retval HAL status
\r
444 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
\r
446 HAL_StatusTypeDef status = HAL_OK;
\r
447 uint32_t tickstart;
\r
449 /* check the wake-up from stop mode UART instance */
\r
450 assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
\r
451 /* check the wake-up selection parameter */
\r
452 assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
\r
454 /* Process Locked */
\r
457 huart->gState = HAL_UART_STATE_BUSY;
\r
459 /* Disable the Peripheral */
\r
460 __HAL_UART_DISABLE(huart);
\r
462 /* Set the wake-up selection scheme */
\r
463 MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
\r
465 if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
\r
467 UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
\r
470 /* Enable the Peripheral */
\r
471 __HAL_UART_ENABLE(huart);
\r
473 /* Init tickstart for timeout managment*/
\r
474 tickstart = HAL_GetTick();
\r
476 /* Wait until REACK flag is set */
\r
477 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
\r
479 status = HAL_TIMEOUT;
\r
483 /* Initialize the UART State */
\r
484 huart->gState = HAL_UART_STATE_READY;
\r
487 /* Process Unlocked */
\r
488 __HAL_UNLOCK(huart);
\r
494 * @brief Enable UART Stop Mode.
\r
495 * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
\r
496 * @param huart UART handle.
\r
497 * @retval HAL status
\r
499 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
\r
501 /* Process Locked */
\r
505 SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
\r
507 /* Process Unlocked */
\r
508 __HAL_UNLOCK(huart);
\r
514 * @brief Disable UART Stop Mode.
\r
515 * @param huart UART handle.
\r
516 * @retval HAL status
\r
518 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
\r
520 /* Process Locked */
\r
523 /* Clear UESM bit */
\r
524 CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
\r
526 /* Process Unlocked */
\r
527 __HAL_UNLOCK(huart);
\r
532 #if defined(USART_CR1_FIFOEN)
\r
534 * @brief Enable the FIFO mode.
\r
535 * @param huart UART handle.
\r
536 * @retval HAL status
\r
538 HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
\r
542 /* Check parameters */
\r
543 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
\r
545 /* Process Locked */
\r
548 huart->gState = HAL_UART_STATE_BUSY;
\r
550 /* Save actual UART configuration */
\r
551 tmpcr1 = READ_REG(huart->Instance->CR1);
\r
554 __HAL_UART_DISABLE(huart);
\r
556 /* Enable FIFO mode */
\r
557 SET_BIT(tmpcr1, USART_CR1_FIFOEN);
\r
558 huart->FifoMode = UART_FIFOMODE_ENABLE;
\r
560 /* Restore UART configuration */
\r
561 WRITE_REG(huart->Instance->CR1, tmpcr1);
\r
563 /* Determine the number of data to process during RX/TX ISR execution */
\r
564 UARTEx_SetNbDataToProcess(huart);
\r
566 huart->gState = HAL_UART_STATE_READY;
\r
568 /* Process Unlocked */
\r
569 __HAL_UNLOCK(huart);
\r
575 * @brief Disable the FIFO mode.
\r
576 * @param huart UART handle.
\r
577 * @retval HAL status
\r
579 HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
\r
583 /* Check parameters */
\r
584 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
\r
586 /* Process Locked */
\r
589 huart->gState = HAL_UART_STATE_BUSY;
\r
591 /* Save actual UART configuration */
\r
592 tmpcr1 = READ_REG(huart->Instance->CR1);
\r
595 __HAL_UART_DISABLE(huart);
\r
597 /* Enable FIFO mode */
\r
598 CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
\r
599 huart->FifoMode = UART_FIFOMODE_DISABLE;
\r
601 /* Restore UART configuration */
\r
602 WRITE_REG(huart->Instance->CR1, tmpcr1);
\r
604 huart->gState = HAL_UART_STATE_READY;
\r
606 /* Process Unlocked */
\r
607 __HAL_UNLOCK(huart);
\r
613 * @brief Set the TXFIFO threshold.
\r
614 * @param huart UART handle.
\r
615 * @param Threshold TX FIFO threshold value
\r
616 * This parameter can be one of the following values:
\r
617 * @arg @ref UART_TXFIFO_THRESHOLD_1_8
\r
618 * @arg @ref UART_TXFIFO_THRESHOLD_1_4
\r
619 * @arg @ref UART_TXFIFO_THRESHOLD_1_2
\r
620 * @arg @ref UART_TXFIFO_THRESHOLD_3_4
\r
621 * @arg @ref UART_TXFIFO_THRESHOLD_7_8
\r
622 * @arg @ref UART_TXFIFO_THRESHOLD_8_8
\r
623 * @retval HAL status
\r
625 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
\r
629 /* Check parameters */
\r
630 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
\r
631 assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
\r
633 /* Process Locked */
\r
636 huart->gState = HAL_UART_STATE_BUSY;
\r
638 /* Save actual UART configuration */
\r
639 tmpcr1 = READ_REG(huart->Instance->CR1);
\r
642 __HAL_UART_DISABLE(huart);
\r
644 /* Update TX threshold configuration */
\r
645 MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
\r
647 /* Determine the number of data to process during RX/TX ISR execution */
\r
648 UARTEx_SetNbDataToProcess(huart);
\r
650 /* Restore UART configuration */
\r
651 WRITE_REG(huart->Instance->CR1, tmpcr1);
\r
653 huart->gState = HAL_UART_STATE_READY;
\r
655 /* Process Unlocked */
\r
656 __HAL_UNLOCK(huart);
\r
662 * @brief Set the RXFIFO threshold.
\r
663 * @param huart UART handle.
\r
664 * @param Threshold RX FIFO threshold value
\r
665 * This parameter can be one of the following values:
\r
666 * @arg @ref UART_RXFIFO_THRESHOLD_1_8
\r
667 * @arg @ref UART_RXFIFO_THRESHOLD_1_4
\r
668 * @arg @ref UART_RXFIFO_THRESHOLD_1_2
\r
669 * @arg @ref UART_RXFIFO_THRESHOLD_3_4
\r
670 * @arg @ref UART_RXFIFO_THRESHOLD_7_8
\r
671 * @arg @ref UART_RXFIFO_THRESHOLD_8_8
\r
672 * @retval HAL status
\r
674 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
\r
678 /* Check the parameters */
\r
679 assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
\r
680 assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
\r
682 /* Process Locked */
\r
685 huart->gState = HAL_UART_STATE_BUSY;
\r
687 /* Save actual UART configuration */
\r
688 tmpcr1 = READ_REG(huart->Instance->CR1);
\r
691 __HAL_UART_DISABLE(huart);
\r
693 /* Update RX threshold configuration */
\r
694 MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
\r
696 /* Determine the number of data to process during RX/TX ISR execution */
\r
697 UARTEx_SetNbDataToProcess(huart);
\r
699 /* Restore UART configuration */
\r
700 WRITE_REG(huart->Instance->CR1, tmpcr1);
\r
702 huart->gState = HAL_UART_STATE_READY;
\r
704 /* Process Unlocked */
\r
705 __HAL_UNLOCK(huart);
\r
709 #endif /* USART_CR1_FIFOEN */
\r
719 /** @addtogroup UARTEx_Private_Functions
\r
724 * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
\r
725 * @param huart UART handle.
\r
726 * @param WakeUpSelection UART wake up from stop mode parameters.
\r
729 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
\r
731 assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
\r
733 /* Set the USART address length */
\r
734 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
\r
736 /* Set the USART address node */
\r
737 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
\r
740 #if defined(USART_CR1_FIFOEN)
\r
742 * @brief Calculate the number of data to process in RX/TX ISR.
\r
743 * @note The RX FIFO depth and the TX FIFO depth is extracted from
\r
744 * the UART configuration registers.
\r
745 * @param huart UART handle.
\r
748 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
\r
750 uint8_t rx_fifo_depth;
\r
751 uint8_t tx_fifo_depth;
\r
752 uint8_t rx_fifo_threshold;
\r
753 uint8_t tx_fifo_threshold;
\r
754 uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
\r
755 uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
\r
757 if (huart->FifoMode == UART_FIFOMODE_DISABLE)
\r
759 huart->NbTxDataToProcess = 1U;
\r
760 huart->NbRxDataToProcess = 1U;
\r
764 rx_fifo_depth = RX_FIFO_DEPTH;
\r
765 tx_fifo_depth = TX_FIFO_DEPTH;
\r
766 rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
\r
767 tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
\r
768 huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / (uint16_t)denominator[tx_fifo_threshold];
\r
769 huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / (uint16_t)denominator[rx_fifo_threshold];
\r
772 #endif /* USART_CR1_FIFOEN */
\r
777 #endif /* HAL_UART_MODULE_ENABLED */
\r
787 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r