]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube/ST_Code/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_uart_ex.c
Make vSetupTimerInterrupt weak in the RVDS M4 MPU port to give the
[freertos] / FreeRTOS / Demo / CORTEX_MPU_STM32L4_Discovery_Keil_STM32Cube / ST_Code / Drivers / STM32L4xx_HAL_Driver / Src / stm32l4xx_hal_uart_ex.c
1 /**\r
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
10   *\r
11   *\r
12   @verbatim\r
13   ==============================================================================\r
14                ##### UART peripheral extended features  #####\r
15   ==============================================================================\r
16 \r
17     (#) Declare a UART_HandleTypeDef handle structure.\r
18 \r
19     (#) For the UART RS485 Driver Enable mode, initialize the UART registers\r
20         by calling the HAL_RS485Ex_Init() API.\r
21 \r
22     (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.\r
23 \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
27 \r
28   @endverbatim\r
29   ******************************************************************************\r
30   * @attention\r
31   *\r
32   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.\r
33   * All rights reserved.</center></h2>\r
34   *\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
39   *\r
40   ******************************************************************************\r
41   */\r
42 \r
43 /* Includes ------------------------------------------------------------------*/\r
44 #include "stm32l4xx_hal.h"\r
45 \r
46 /** @addtogroup STM32L4xx_HAL_Driver\r
47   * @{\r
48   */\r
49 \r
50 /** @defgroup UARTEx UARTEx\r
51   * @brief UART Extended HAL module driver\r
52   * @{\r
53   */\r
54 \r
55 #ifdef HAL_UART_MODULE_ENABLED\r
56 \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
61   * @{\r
62   */\r
63 /* UART RX FIFO depth */\r
64 #define RX_FIFO_DEPTH 8U\r
65 \r
66 /* UART TX FIFO depth */\r
67 #define TX_FIFO_DEPTH 8U\r
68 /**\r
69   * @}\r
70   */\r
71 #endif /* USART_CR1_FIFOEN */\r
72 \r
73 /* Private macros ------------------------------------------------------------*/\r
74 /* Private variables ---------------------------------------------------------*/\r
75 /* Private function prototypes -----------------------------------------------*/\r
76 /** @defgroup UARTEx_Private_Functions UARTEx Private Functions\r
77   * @{\r
78   */\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
83 /**\r
84   * @}\r
85   */\r
86 \r
87 /* Exported functions --------------------------------------------------------*/\r
88 \r
89 /** @defgroup UARTEx_Exported_Functions  UARTEx Exported Functions\r
90   * @{\r
91   */\r
92 \r
93 /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions\r
94   * @brief    Extended Initialization and Configuration Functions\r
95   *\r
96 @verbatim\r
97 ===============================================================================\r
98             ##### Initialization and Configuration functions #####\r
99  ===============================================================================\r
100     [..]\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
104         (++) Baud Rate\r
105         (++) Word Length\r
106         (++) Stop Bit\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
121     [..]\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
124 \r
125 @endverbatim\r
126 \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
129   following table.\r
130 \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
147 \r
148   * @{\r
149   */\r
150 \r
151 /**\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
169   */\r
170 HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime,\r
171                                    uint32_t DeassertionTime)\r
172 {\r
173   uint32_t temp;\r
174 \r
175   /* Check the UART handle allocation */\r
176   if (huart == NULL)\r
177   {\r
178     return HAL_ERROR;\r
179   }\r
180   /* Check the Driver Enable UART instance */\r
181   assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));\r
182 \r
183   /* Check the Driver Enable polarity */\r
184   assert_param(IS_UART_DE_POLARITY(Polarity));\r
185 \r
186   /* Check the Driver Enable assertion time */\r
187   assert_param(IS_UART_ASSERTIONTIME(AssertionTime));\r
188 \r
189   /* Check the Driver Enable deassertion time */\r
190   assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));\r
191 \r
192   if (huart->gState == HAL_UART_STATE_RESET)\r
193   {\r
194     /* Allocate lock resource and initialize it */\r
195     huart->Lock = HAL_UNLOCKED;\r
196 \r
197 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)\r
198     UART_InitCallbacksToDefault(huart);\r
199 \r
200     if (huart->MspInitCallback == NULL)\r
201     {\r
202       huart->MspInitCallback = HAL_UART_MspInit;\r
203     }\r
204 \r
205     /* Init the low level hardware */\r
206     huart->MspInitCallback(huart);\r
207 #else\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
211   }\r
212 \r
213   huart->gState = HAL_UART_STATE_BUSY;\r
214 \r
215   /* Disable the Peripheral */\r
216   __HAL_UART_DISABLE(huart);\r
217 \r
218   /* Set the UART Communication parameters */\r
219   if (UART_SetConfig(huart) == HAL_ERROR)\r
220   {\r
221     return HAL_ERROR;\r
222   }\r
223 \r
224   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)\r
225   {\r
226     UART_AdvFeatureConfig(huart);\r
227   }\r
228 \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
231 \r
232   /* Set the Driver Enable polarity */\r
233   MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);\r
234 \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
239 \r
240   /* Enable the Peripheral */\r
241   __HAL_UART_ENABLE(huart);\r
242 \r
243   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */\r
244   return (UART_CheckIdleState(huart));\r
245 }\r
246 \r
247 /**\r
248   * @}\r
249   */\r
250 \r
251 /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions\r
252   *  @brief Extended functions\r
253   *\r
254 @verbatim\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
259 \r
260     (#) Wakeup from Stop mode Callback:\r
261         (+) HAL_UARTEx_WakeupCallback()\r
262 \r
263     (#) TX/RX Fifos Callbacks:\r
264         (+) HAL_UARTEx_RxFifoFullCallback()\r
265         (+) HAL_UARTEx_TxFifoEmptyCallback()\r
266 \r
267 @endverbatim\r
268   * @{\r
269   */\r
270 \r
271 /**\r
272   * @brief UART wakeup from Stop mode callback.\r
273   * @param huart UART handle.\r
274   * @retval None\r
275   */\r
276 __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)\r
277 {\r
278   /* Prevent unused argument(s) compilation warning */\r
279   UNUSED(huart);\r
280 \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
283    */\r
284 }\r
285 \r
286 #if defined(USART_CR1_FIFOEN)\r
287 /**\r
288   * @brief  UART RX Fifo full callback.\r
289   * @param  huart UART handle.\r
290   * @retval None\r
291   */\r
292 __weak void HAL_UARTEx_RxFifoFullCallback(UART_HandleTypeDef *huart)\r
293 {\r
294   /* Prevent unused argument(s) compilation warning */\r
295   UNUSED(huart);\r
296 \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
299    */\r
300 }\r
301 \r
302 /**\r
303   * @brief  UART TX Fifo empty callback.\r
304   * @param  huart UART handle.\r
305   * @retval None\r
306   */\r
307 __weak void HAL_UARTEx_TxFifoEmptyCallback(UART_HandleTypeDef *huart)\r
308 {\r
309   /* Prevent unused argument(s) compilation warning */\r
310   UNUSED(huart);\r
311 \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
314    */\r
315 }\r
316 #endif /* USART_CR1_FIFOEN */\r
317 \r
318 /**\r
319   * @}\r
320   */\r
321 \r
322 /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions\r
323   * @brief    Extended Peripheral Control functions\r
324   *\r
325 @verbatim\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
342 \r
343 @endverbatim\r
344   * @{\r
345   */\r
346 \r
347 \r
348 \r
349 #if defined(USART_CR3_UCESM)\r
350 /**\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
358   */\r
359 HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)\r
360 {\r
361   /* Process Locked */\r
362   __HAL_LOCK(huart);\r
363 \r
364   /* Set UCESM bit */\r
365   SET_BIT(huart->Instance->CR3, USART_CR3_UCESM);\r
366 \r
367   /* Process Unlocked */\r
368   __HAL_UNLOCK(huart);\r
369 \r
370   return HAL_OK;\r
371 }\r
372 \r
373 /**\r
374   * @brief  Disable UART Clock when in Stop Mode.\r
375   * @param  huart UART handle.\r
376   * @retval HAL status\r
377   */\r
378 HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)\r
379 {\r
380   /* Process Locked */\r
381   __HAL_LOCK(huart);\r
382 \r
383   /* Clear UCESM bit */\r
384   CLEAR_BIT(huart->Instance->CR3, USART_CR3_UCESM);\r
385 \r
386   /* Process Unlocked */\r
387   __HAL_UNLOCK(huart);\r
388 \r
389   return HAL_OK;\r
390 }\r
391 #endif /* USART_CR3_UCESM */\r
392 \r
393 /**\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
397   *        long).\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
405   */\r
406 HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)\r
407 {\r
408   /* Check the UART handle allocation */\r
409   if (huart == NULL)\r
410   {\r
411     return HAL_ERROR;\r
412   }\r
413 \r
414   /* Check the address length parameter */\r
415   assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));\r
416 \r
417   huart->gState = HAL_UART_STATE_BUSY;\r
418 \r
419   /* Disable the Peripheral */\r
420   __HAL_UART_DISABLE(huart);\r
421 \r
422   /* Set the address length */\r
423   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);\r
424 \r
425   /* Enable the Peripheral */\r
426   __HAL_UART_ENABLE(huart);\r
427 \r
428   /* TEACK and/or REACK to check before moving huart->gState to Ready */\r
429   return (UART_CheckIdleState(huart));\r
430 }\r
431 \r
432 /**\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
443   */\r
444 HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)\r
445 {\r
446   HAL_StatusTypeDef status = HAL_OK;\r
447   uint32_t tickstart;\r
448 \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
453 \r
454   /* Process Locked */\r
455   __HAL_LOCK(huart);\r
456 \r
457   huart->gState = HAL_UART_STATE_BUSY;\r
458 \r
459   /* Disable the Peripheral */\r
460   __HAL_UART_DISABLE(huart);\r
461 \r
462   /* Set the wake-up selection scheme */\r
463   MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);\r
464 \r
465   if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)\r
466   {\r
467     UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);\r
468   }\r
469 \r
470   /* Enable the Peripheral */\r
471   __HAL_UART_ENABLE(huart);\r
472 \r
473   /* Init tickstart for timeout managment*/\r
474   tickstart = HAL_GetTick();\r
475 \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
478   {\r
479     status = HAL_TIMEOUT;\r
480   }\r
481   else\r
482   {\r
483     /* Initialize the UART State */\r
484     huart->gState = HAL_UART_STATE_READY;\r
485   }\r
486 \r
487   /* Process Unlocked */\r
488   __HAL_UNLOCK(huart);\r
489 \r
490   return status;\r
491 }\r
492 \r
493 /**\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
498   */\r
499 HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)\r
500 {\r
501   /* Process Locked */\r
502   __HAL_LOCK(huart);\r
503 \r
504   /* Set UESM bit */\r
505   SET_BIT(huart->Instance->CR1, USART_CR1_UESM);\r
506 \r
507   /* Process Unlocked */\r
508   __HAL_UNLOCK(huart);\r
509 \r
510   return HAL_OK;\r
511 }\r
512 \r
513 /**\r
514   * @brief Disable UART Stop Mode.\r
515   * @param huart UART handle.\r
516   * @retval HAL status\r
517   */\r
518 HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)\r
519 {\r
520   /* Process Locked */\r
521   __HAL_LOCK(huart);\r
522 \r
523   /* Clear UESM bit */\r
524   CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);\r
525 \r
526   /* Process Unlocked */\r
527   __HAL_UNLOCK(huart);\r
528 \r
529   return HAL_OK;\r
530 }\r
531 \r
532 #if defined(USART_CR1_FIFOEN)\r
533 /**\r
534   * @brief  Enable the FIFO mode.\r
535   * @param huart      UART handle.\r
536   * @retval HAL status\r
537   */\r
538 HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)\r
539 {\r
540   uint32_t tmpcr1;\r
541 \r
542   /* Check parameters */\r
543   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));\r
544 \r
545   /* Process Locked */\r
546   __HAL_LOCK(huart);\r
547 \r
548   huart->gState = HAL_UART_STATE_BUSY;\r
549 \r
550   /* Save actual UART configuration */\r
551   tmpcr1 = READ_REG(huart->Instance->CR1);\r
552 \r
553   /* Disable UART */\r
554   __HAL_UART_DISABLE(huart);\r
555 \r
556   /* Enable FIFO mode */\r
557   SET_BIT(tmpcr1, USART_CR1_FIFOEN);\r
558   huart->FifoMode = UART_FIFOMODE_ENABLE;\r
559 \r
560   /* Restore UART configuration */\r
561   WRITE_REG(huart->Instance->CR1, tmpcr1);\r
562 \r
563   /* Determine the number of data to process during RX/TX ISR execution */\r
564   UARTEx_SetNbDataToProcess(huart);\r
565 \r
566   huart->gState = HAL_UART_STATE_READY;\r
567 \r
568   /* Process Unlocked */\r
569   __HAL_UNLOCK(huart);\r
570 \r
571   return HAL_OK;\r
572 }\r
573 \r
574 /**\r
575   * @brief  Disable the FIFO mode.\r
576   * @param huart      UART handle.\r
577   * @retval HAL status\r
578   */\r
579 HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)\r
580 {\r
581   uint32_t tmpcr1;\r
582 \r
583   /* Check parameters */\r
584   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));\r
585 \r
586   /* Process Locked */\r
587   __HAL_LOCK(huart);\r
588 \r
589   huart->gState = HAL_UART_STATE_BUSY;\r
590 \r
591   /* Save actual UART configuration */\r
592   tmpcr1 = READ_REG(huart->Instance->CR1);\r
593 \r
594   /* Disable UART */\r
595   __HAL_UART_DISABLE(huart);\r
596 \r
597   /* Enable FIFO mode */\r
598   CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);\r
599   huart->FifoMode = UART_FIFOMODE_DISABLE;\r
600 \r
601   /* Restore UART configuration */\r
602   WRITE_REG(huart->Instance->CR1, tmpcr1);\r
603 \r
604   huart->gState = HAL_UART_STATE_READY;\r
605 \r
606   /* Process Unlocked */\r
607   __HAL_UNLOCK(huart);\r
608 \r
609   return HAL_OK;\r
610 }\r
611 \r
612 /**\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
624   */\r
625 HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)\r
626 {\r
627   uint32_t tmpcr1;\r
628 \r
629   /* Check parameters */\r
630   assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));\r
631   assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));\r
632 \r
633   /* Process Locked */\r
634   __HAL_LOCK(huart);\r
635 \r
636   huart->gState = HAL_UART_STATE_BUSY;\r
637 \r
638   /* Save actual UART configuration */\r
639   tmpcr1 = READ_REG(huart->Instance->CR1);\r
640 \r
641   /* Disable UART */\r
642   __HAL_UART_DISABLE(huart);\r
643 \r
644   /* Update TX threshold configuration */\r
645   MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);\r
646 \r
647   /* Determine the number of data to process during RX/TX ISR execution */\r
648   UARTEx_SetNbDataToProcess(huart);\r
649 \r
650   /* Restore UART configuration */\r
651   WRITE_REG(huart->Instance->CR1, tmpcr1);\r
652 \r
653   huart->gState = HAL_UART_STATE_READY;\r
654 \r
655   /* Process Unlocked */\r
656   __HAL_UNLOCK(huart);\r
657 \r
658   return HAL_OK;\r
659 }\r
660 \r
661 /**\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
673   */\r
674 HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)\r
675 {\r
676   uint32_t tmpcr1;\r
677 \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
681 \r
682   /* Process Locked */\r
683   __HAL_LOCK(huart);\r
684 \r
685   huart->gState = HAL_UART_STATE_BUSY;\r
686 \r
687   /* Save actual UART configuration */\r
688   tmpcr1 = READ_REG(huart->Instance->CR1);\r
689 \r
690   /* Disable UART */\r
691   __HAL_UART_DISABLE(huart);\r
692 \r
693   /* Update RX threshold configuration */\r
694   MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);\r
695 \r
696   /* Determine the number of data to process during RX/TX ISR execution */\r
697   UARTEx_SetNbDataToProcess(huart);\r
698 \r
699   /* Restore UART configuration */\r
700   WRITE_REG(huart->Instance->CR1, tmpcr1);\r
701 \r
702   huart->gState = HAL_UART_STATE_READY;\r
703 \r
704   /* Process Unlocked */\r
705   __HAL_UNLOCK(huart);\r
706 \r
707   return HAL_OK;\r
708 }\r
709 #endif /* USART_CR1_FIFOEN */\r
710 \r
711 /**\r
712   * @}\r
713   */\r
714 \r
715 /**\r
716   * @}\r
717   */\r
718 \r
719 /** @addtogroup UARTEx_Private_Functions\r
720   * @{\r
721   */\r
722 \r
723 /**\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
727   * @retval None\r
728   */\r
729 static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)\r
730 {\r
731   assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));\r
732 \r
733   /* Set the USART address length */\r
734   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);\r
735 \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
738 }\r
739 \r
740 #if defined(USART_CR1_FIFOEN)\r
741 /**\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
746   * @retval None\r
747   */\r
748 static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)\r
749 {\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
756 \r
757   if (huart->FifoMode == UART_FIFOMODE_DISABLE)\r
758   {\r
759     huart->NbTxDataToProcess = 1U;\r
760     huart->NbRxDataToProcess = 1U;\r
761   }\r
762   else\r
763   {\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
770   }\r
771 }\r
772 #endif /* USART_CR1_FIFOEN */\r
773 /**\r
774   * @}\r
775   */\r
776 \r
777 #endif /* HAL_UART_MODULE_ENABLED */\r
778 \r
779 /**\r
780   * @}\r
781   */\r
782 \r
783 /**\r
784   * @}\r
785   */\r
786 \r
787 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r