1 /**
2   ******************************************************************************
3   * @file    stm32wbxx_hal_uart_ex.c
4   * @author  MCD Application Team
5   * @brief   Extended UART HAL module driver.
6   *          This file provides firmware functions to manage the following extended
7   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
8   *           + Initialization and de-initialization functions
9   *           + Peripheral Control functions
10   *
11   *
12   @verbatim
13   ==============================================================================
14                ##### UART peripheral extended features  #####
15   ==============================================================================
16 
17     (#) Declare a UART_HandleTypeDef handle structure.
18 
19     (#) For the UART RS485 Driver Enable mode, initialize the UART registers
20         by calling the HAL_RS485Ex_Init() API.
21 
22     (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
23 
24         -@- When UART operates in FIFO mode, FIFO mode must be enabled prior
25             starting RX/TX transfers. Also RX/TX FIFO thresholds must be
26             configured prior starting RX/TX transfers.
27 
28   @endverbatim
29   ******************************************************************************
30   * @attention
31   *
32   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
33   * All rights reserved.</center></h2>
34   *
35   * This software component is licensed by ST under BSD 3-Clause license,
36   * the "License"; You may not use this file except in compliance with the
37   * License. You may obtain a copy of the License at:
38   *                        opensource.org/licenses/BSD-3-Clause
39   *
40   ******************************************************************************
41   */
42 
43 /* Includes ------------------------------------------------------------------*/
44 #include "stm32wbxx_hal.h"
45 
46 /** @addtogroup STM32WBxx_HAL_Driver
47   * @{
48   */
49 
50 /** @defgroup UARTEx UARTEx
51   * @brief UART Extended HAL module driver
52   * @{
53   */
54 
55 #ifdef HAL_UART_MODULE_ENABLED
56 
57 /* Private typedef -----------------------------------------------------------*/
58 /* Private define ------------------------------------------------------------*/
59 /** @defgroup UARTEX_Private_Constants UARTEx Private Constants
60   * @{
61   */
62 /* UART RX FIFO depth */
63 #define RX_FIFO_DEPTH 8U
64 
65 /* UART TX FIFO depth */
66 #define TX_FIFO_DEPTH 8U
67 /**
68   * @}
69   */
70 
71 /* Private macros ------------------------------------------------------------*/
72 /* Private variables ---------------------------------------------------------*/
73 /* Private function prototypes -----------------------------------------------*/
74 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
75   * @{
76   */
77 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
78 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart);
79 /**
80   * @}
81   */
82 
83 /* Exported functions --------------------------------------------------------*/
84 
85 /** @defgroup UARTEx_Exported_Functions  UARTEx Exported Functions
86   * @{
87   */
88 
89 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
90   * @brief    Extended Initialization and Configuration Functions
91   *
92 @verbatim
93 ===============================================================================
94             ##### Initialization and Configuration functions #####
95  ===============================================================================
96     [..]
97     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
98     in asynchronous mode.
99       (+) For the asynchronous mode the parameters below can be configured:
100         (++) Baud Rate
101         (++) Word Length
102         (++) Stop Bit
103         (++) Parity: If the parity is enabled, then the MSB bit of the data written
104              in the data register is transmitted but is changed by the parity bit.
105         (++) Hardware flow control
106         (++) Receiver/transmitter modes
107         (++) Over Sampling Method
108         (++) One-Bit Sampling Method
109       (+) For the asynchronous mode, the following advanced features can be configured as well:
110         (++) TX and/or RX pin level inversion
111         (++) data logical level inversion
112         (++) RX and TX pins swap
113         (++) RX overrun detection disabling
114         (++) DMA disabling on RX error
115         (++) MSB first on communication line
116         (++) auto Baud rate detection
117     [..]
118     The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
119      procedures (details for the procedures are available in reference manual).
120 
121 @endverbatim
122 
123   Depending on the frame length defined by the M1 and M0 bits (7-bit,
124   8-bit or 9-bit), the possible UART formats are listed in the
125   following table.
126 
127     Table 1. UART frame format.
128     +-----------------------------------------------------------------------+
129     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
130     |---------|---------|-----------|---------------------------------------|
131     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
132     |---------|---------|-----------|---------------------------------------|
133     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
134     |---------|---------|-----------|---------------------------------------|
135     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
136     |---------|---------|-----------|---------------------------------------|
137     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
138     |---------|---------|-----------|---------------------------------------|
139     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
140     |---------|---------|-----------|---------------------------------------|
141     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
142     +-----------------------------------------------------------------------+
143 
144   * @{
145   */
146 
147 /**
148   * @brief Initialize the RS485 Driver enable feature according to the specified
149   *         parameters in the UART_InitTypeDef and creates the associated handle.
150   * @param huart            UART handle.
151   * @param Polarity         Select the driver enable polarity.
152   *          This parameter can be one of the following values:
153   *          @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
154   *          @arg @ref UART_DE_POLARITY_LOW  DE signal is active low
155   * @param AssertionTime    Driver Enable assertion time:
156   *       5-bit value defining the time between the activation of the DE (Driver Enable)
157   *       signal and the beginning of the start bit. It is expressed in sample time
158   *       units (1/8 or 1/16 bit time, depending on the oversampling rate)
159   * @param DeassertionTime  Driver Enable deassertion time:
160   *       5-bit value defining the time between the end of the last stop bit, in a
161   *       transmitted message, and the de-activation of the DE (Driver Enable) signal.
162   *       It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
163   *       oversampling rate).
164   * @retval HAL status
165   */
HAL_RS485Ex_Init(UART_HandleTypeDef * huart,uint32_t Polarity,uint32_t AssertionTime,uint32_t DeassertionTime)166 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,
167                                    uint32_t DeassertionTime)
168 {
169   uint32_t temp;
170 
171   /* Check the UART handle allocation */
172   if (huart == NULL)
173   {
174     return HAL_ERROR;
175   }
176   /* Check the Driver Enable UART instance */
177   assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
178 
179   /* Check the Driver Enable polarity */
180   assert_param(IS_UART_DE_POLARITY(Polarity));
181 
182   /* Check the Driver Enable assertion time */
183   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
184 
185   /* Check the Driver Enable deassertion time */
186   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
187 
188   if (huart->gState == HAL_UART_STATE_RESET)
189   {
190     /* Allocate lock resource and initialize it */
191     huart->Lock = HAL_UNLOCKED;
192 
193 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
194     UART_InitCallbacksToDefault(huart);
195 
196     if (huart->MspInitCallback == NULL)
197     {
198       huart->MspInitCallback = HAL_UART_MspInit;
199     }
200 
201     /* Init the low level hardware */
202     huart->MspInitCallback(huart);
203 #else
204     /* Init the low level hardware : GPIO, CLOCK, CORTEX */
205     HAL_UART_MspInit(huart);
206 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
207   }
208 
209   huart->gState = HAL_UART_STATE_BUSY;
210 
211   /* Disable the Peripheral */
212   __HAL_UART_DISABLE(huart);
213 
214   /* Set the UART Communication parameters */
215   if (UART_SetConfig(huart) == HAL_ERROR)
216   {
217     return HAL_ERROR;
218   }
219 
220   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
221   {
222     UART_AdvFeatureConfig(huart);
223   }
224 
225   /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
226   SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
227 
228   /* Set the Driver Enable polarity */
229   MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
230 
231   /* Set the Driver Enable assertion and deassertion times */
232   temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
233   temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
234   MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT | USART_CR1_DEAT), temp);
235 
236   /* Enable the Peripheral */
237   __HAL_UART_ENABLE(huart);
238 
239   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
240   return (UART_CheckIdleState(huart));
241 }
242 
243 /**
244   * @}
245   */
246 
247 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
248   *  @brief Extended functions
249   *
250 @verbatim
251  ===============================================================================
252                       ##### IO operation functions #####
253  ===============================================================================
254     This subsection provides a set of Wakeup and FIFO mode related callback functions.
255 
256     (#) Wakeup from Stop mode Callback:
257         (+) HAL_UARTEx_WakeupCallback()
258 
259     (#) TX/RX Fifos Callbacks:
260         (+) HAL_UARTEx_RxFifoFullCallback()
261         (+) HAL_UARTEx_TxFifoEmptyCallback()
262 
263 @endverbatim
264   * @{
265   */
266 
267 /**
268   * @brief UART wakeup from Stop mode callback.
269   * @param huart UART handle.
270   * @retval None
271   */
HAL_UARTEx_WakeupCallback(UART_HandleTypeDef * huart)272 __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
273 {
274   /* Prevent unused argument(s) compilation warning */
275   UNUSED(huart);
276 
277   /* NOTE : This function should not be modified, when the callback is needed,
278             the HAL_UARTEx_WakeupCallback can be implemented in the user file.
279    */
280 }
281 
282 /**
283   * @brief  UART RX Fifo full callback.
284   * @param  huart UART handle.
285   * @retval None
286   */
HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef * huart)287 __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)
288 {
289   /* Prevent unused argument(s) compilation warning */
290   UNUSED(huart);
291 
292   /* NOTE : This function should not be modified, when the callback is needed,
293             the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
294    */
295 }
296 
297 /**
298   * @brief  UART TX Fifo empty callback.
299   * @param  huart UART handle.
300   * @retval None
301   */
HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef * huart)302 __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)
303 {
304   /* Prevent unused argument(s) compilation warning */
305   UNUSED(huart);
306 
307   /* NOTE : This function should not be modified, when the callback is needed,
308             the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
309    */
310 }
311 
312 /**
313   * @}
314   */
315 
316 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
317   * @brief    Extended Peripheral Control functions
318   *
319 @verbatim
320  ===============================================================================
321                       ##### Peripheral Control functions #####
322  ===============================================================================
323     [..] This section provides the following functions:
324      (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
325          detection length to more than 4 bits for multiprocessor address mark wake up.
326      (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
327          trigger: address match, Start Bit detection or RXNE bit status.
328      (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
329      (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
330      (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
331      (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
332      (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
333      (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
334 
335 @endverbatim
336   * @{
337   */
338 
339 /**
340   * @brief By default in multiprocessor mode, when the wake up method is set
341   *        to address mark, the UART handles only 4-bit long addresses detection;
342   *        this API allows to enable longer addresses detection (6-, 7- or 8-bit
343   *        long).
344   * @note  Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
345   *        7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
346   * @param huart         UART handle.
347   * @param AddressLength This parameter can be one of the following values:
348   *          @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
349   *          @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
350   * @retval HAL status
351   */
HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef * huart,uint32_t AddressLength)352 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
353 {
354   /* Check the UART handle allocation */
355   if (huart == NULL)
356   {
357     return HAL_ERROR;
358   }
359 
360   /* Check the address length parameter */
361   assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
362 
363   huart->gState = HAL_UART_STATE_BUSY;
364 
365   /* Disable the Peripheral */
366   __HAL_UART_DISABLE(huart);
367 
368   /* Set the address length */
369   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
370 
371   /* Enable the Peripheral */
372   __HAL_UART_ENABLE(huart);
373 
374   /* TEACK and/or REACK to check before moving huart->gState to Ready */
375   return (UART_CheckIdleState(huart));
376 }
377 
378 /**
379   * @brief Set Wakeup from Stop mode interrupt flag selection.
380   * @note It is the application responsibility to enable the interrupt used as
381   *       usart_wkup interrupt source before entering low-power mode.
382   * @param huart           UART handle.
383   * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
384   *          This parameter can be one of the following values:
385   *          @arg @ref UART_WAKEUP_ON_ADDRESS
386   *          @arg @ref UART_WAKEUP_ON_STARTBIT
387   *          @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
388   * @retval HAL status
389   */
HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef * huart,UART_WakeUpTypeDef WakeUpSelection)390 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
391 {
392   HAL_StatusTypeDef status = HAL_OK;
393   uint32_t tickstart;
394 
395   /* check the wake-up from stop mode UART instance */
396   assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
397   /* check the wake-up selection parameter */
398   assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
399 
400   /* Process Locked */
401   __HAL_LOCK(huart);
402 
403   huart->gState = HAL_UART_STATE_BUSY;
404 
405   /* Disable the Peripheral */
406   __HAL_UART_DISABLE(huart);
407 
408   /* Set the wake-up selection scheme */
409   MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
410 
411   if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
412   {
413     UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
414   }
415 
416   /* Enable the Peripheral */
417   __HAL_UART_ENABLE(huart);
418 
419   /* Init tickstart for timeout managment*/
420   tickstart = HAL_GetTick();
421 
422   /* Wait until REACK flag is set */
423   if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
424   {
425     status = HAL_TIMEOUT;
426   }
427   else
428   {
429     /* Initialize the UART State */
430     huart->gState = HAL_UART_STATE_READY;
431   }
432 
433   /* Process Unlocked */
434   __HAL_UNLOCK(huart);
435 
436   return status;
437 }
438 
439 /**
440   * @brief Enable UART Stop Mode.
441   * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
442   * @param huart UART handle.
443   * @retval HAL status
444   */
HAL_UARTEx_EnableStopMode(UART_HandleTypeDef * huart)445 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
446 {
447   /* Process Locked */
448   __HAL_LOCK(huart);
449 
450   /* Set UESM bit */
451   SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
452 
453   /* Process Unlocked */
454   __HAL_UNLOCK(huart);
455 
456   return HAL_OK;
457 }
458 
459 /**
460   * @brief Disable UART Stop Mode.
461   * @param huart UART handle.
462   * @retval HAL status
463   */
HAL_UARTEx_DisableStopMode(UART_HandleTypeDef * huart)464 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
465 {
466   /* Process Locked */
467   __HAL_LOCK(huart);
468 
469   /* Clear UESM bit */
470   CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
471 
472   /* Process Unlocked */
473   __HAL_UNLOCK(huart);
474 
475   return HAL_OK;
476 }
477 
478 /**
479   * @brief  Enable the FIFO mode.
480   * @param huart      UART handle.
481   * @retval HAL status
482   */
HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef * huart)483 HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
484 {
485   uint32_t tmpcr1;
486 
487   /* Check parameters */
488   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
489 
490   /* Process Locked */
491   __HAL_LOCK(huart);
492 
493   huart->gState = HAL_UART_STATE_BUSY;
494 
495   /* Save actual UART configuration */
496   tmpcr1 = READ_REG(huart->Instance->CR1);
497 
498   /* Disable UART */
499   __HAL_UART_DISABLE(huart);
500 
501   /* Enable FIFO mode */
502   SET_BIT(tmpcr1, USART_CR1_FIFOEN);
503   huart->FifoMode = UART_FIFOMODE_ENABLE;
504 
505   /* Restore UART configuration */
506   WRITE_REG(huart->Instance->CR1, tmpcr1);
507 
508   /* Determine the number of data to process during RX/TX ISR execution */
509   UARTEx_SetNbDataToProcess(huart);
510 
511   huart->gState = HAL_UART_STATE_READY;
512 
513   /* Process Unlocked */
514   __HAL_UNLOCK(huart);
515 
516   return HAL_OK;
517 }
518 
519 /**
520   * @brief  Disable the FIFO mode.
521   * @param huart      UART handle.
522   * @retval HAL status
523   */
HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef * huart)524 HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
525 {
526   uint32_t tmpcr1;
527 
528   /* Check parameters */
529   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
530 
531   /* Process Locked */
532   __HAL_LOCK(huart);
533 
534   huart->gState = HAL_UART_STATE_BUSY;
535 
536   /* Save actual UART configuration */
537   tmpcr1 = READ_REG(huart->Instance->CR1);
538 
539   /* Disable UART */
540   __HAL_UART_DISABLE(huart);
541 
542   /* Enable FIFO mode */
543   CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
544   huart->FifoMode = UART_FIFOMODE_DISABLE;
545 
546   /* Restore UART configuration */
547   WRITE_REG(huart->Instance->CR1, tmpcr1);
548 
549   huart->gState = HAL_UART_STATE_READY;
550 
551   /* Process Unlocked */
552   __HAL_UNLOCK(huart);
553 
554   return HAL_OK;
555 }
556 
557 /**
558   * @brief  Set the TXFIFO threshold.
559   * @param huart      UART handle.
560   * @param Threshold  TX FIFO threshold value
561   *          This parameter can be one of the following values:
562   *            @arg @ref UART_TXFIFO_THRESHOLD_1_8
563   *            @arg @ref UART_TXFIFO_THRESHOLD_1_4
564   *            @arg @ref UART_TXFIFO_THRESHOLD_1_2
565   *            @arg @ref UART_TXFIFO_THRESHOLD_3_4
566   *            @arg @ref UART_TXFIFO_THRESHOLD_7_8
567   *            @arg @ref UART_TXFIFO_THRESHOLD_8_8
568   * @retval HAL status
569   */
HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef * huart,uint32_t Threshold)570 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
571 {
572   uint32_t tmpcr1;
573 
574   /* Check parameters */
575   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
576   assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
577 
578   /* Process Locked */
579   __HAL_LOCK(huart);
580 
581   huart->gState = HAL_UART_STATE_BUSY;
582 
583   /* Save actual UART configuration */
584   tmpcr1 = READ_REG(huart->Instance->CR1);
585 
586   /* Disable UART */
587   __HAL_UART_DISABLE(huart);
588 
589   /* Update TX threshold configuration */
590   MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
591 
592   /* Determine the number of data to process during RX/TX ISR execution */
593   UARTEx_SetNbDataToProcess(huart);
594 
595   /* Restore UART configuration */
596   WRITE_REG(huart->Instance->CR1, tmpcr1);
597 
598   huart->gState = HAL_UART_STATE_READY;
599 
600   /* Process Unlocked */
601   __HAL_UNLOCK(huart);
602 
603   return HAL_OK;
604 }
605 
606 /**
607   * @brief  Set the RXFIFO threshold.
608   * @param huart      UART handle.
609   * @param Threshold  RX FIFO threshold value
610   *          This parameter can be one of the following values:
611   *            @arg @ref UART_RXFIFO_THRESHOLD_1_8
612   *            @arg @ref UART_RXFIFO_THRESHOLD_1_4
613   *            @arg @ref UART_RXFIFO_THRESHOLD_1_2
614   *            @arg @ref UART_RXFIFO_THRESHOLD_3_4
615   *            @arg @ref UART_RXFIFO_THRESHOLD_7_8
616   *            @arg @ref UART_RXFIFO_THRESHOLD_8_8
617   * @retval HAL status
618   */
HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef * huart,uint32_t Threshold)619 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
620 {
621   uint32_t tmpcr1;
622 
623   /* Check the parameters */
624   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
625   assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
626 
627   /* Process Locked */
628   __HAL_LOCK(huart);
629 
630   huart->gState = HAL_UART_STATE_BUSY;
631 
632   /* Save actual UART configuration */
633   tmpcr1 = READ_REG(huart->Instance->CR1);
634 
635   /* Disable UART */
636   __HAL_UART_DISABLE(huart);
637 
638   /* Update RX threshold configuration */
639   MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
640 
641   /* Determine the number of data to process during RX/TX ISR execution */
642   UARTEx_SetNbDataToProcess(huart);
643 
644   /* Restore UART configuration */
645   WRITE_REG(huart->Instance->CR1, tmpcr1);
646 
647   huart->gState = HAL_UART_STATE_READY;
648 
649   /* Process Unlocked */
650   __HAL_UNLOCK(huart);
651 
652   return HAL_OK;
653 }
654 
655 /**
656   * @}
657   */
658 
659 /**
660   * @}
661   */
662 
663 /** @addtogroup UARTEx_Private_Functions
664   * @{
665   */
666 
667 /**
668   * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
669   * @param huart           UART handle.
670   * @param WakeUpSelection UART wake up from stop mode parameters.
671   * @retval None
672   */
UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef * huart,UART_WakeUpTypeDef WakeUpSelection)673 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
674 {
675   assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
676 
677   /* Set the USART address length */
678   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
679 
680   /* Set the USART address node */
681   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
682 }
683 
684 /**
685   * @brief Calculate the number of data to process in RX/TX ISR.
686   * @note The RX FIFO depth and the TX FIFO depth is extracted from
687   *       the UART configuration registers.
688   * @param huart UART handle.
689   * @retval None
690   */
UARTEx_SetNbDataToProcess(UART_HandleTypeDef * huart)691 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
692 {
693   uint8_t rx_fifo_depth;
694   uint8_t tx_fifo_depth;
695   uint8_t rx_fifo_threshold;
696   uint8_t tx_fifo_threshold;
697   uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
698   uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
699 
700   if (huart->FifoMode == UART_FIFOMODE_DISABLE)
701   {
702     huart->NbTxDataToProcess = 1U;
703     huart->NbRxDataToProcess = 1U;
704   }
705   else
706   {
707     rx_fifo_depth = RX_FIFO_DEPTH;
708     tx_fifo_depth = TX_FIFO_DEPTH;
709     rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
710     tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
711     huart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) / (uint16_t)denominator[tx_fifo_threshold];
712     huart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) / (uint16_t)denominator[rx_fifo_threshold];
713   }
714 }
715 /**
716   * @}
717   */
718 
719 #endif /* HAL_UART_MODULE_ENABLED */
720 
721 /**
722   * @}
723   */
724 
725 /**
726   * @}
727   */
728 
729 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
730