1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_hal_uart.c
4   * @author  MCD Application Team
5   * @brief   UART HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
8   *           + Initialization and de-initialization functions
9   *           + IO operation functions
10   *           + Peripheral Control functions
11   *
12   *
13   ******************************************************************************
14   * @attention
15   *
16   * Copyright (c) 2022 STMicroelectronics.
17   * All rights reserved.
18   *
19   * This software is licensed under terms that can be found in the LICENSE file
20   * in the root directory of this software component.
21   * If no LICENSE file comes with this software, it is provided AS-IS.
22   *
23   ******************************************************************************
24   @verbatim
25  ===============================================================================
26                         ##### How to use this driver #####
27  ===============================================================================
28   [..]
29     The UART HAL driver can be used as follows:
30 
31     (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
32     (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
33         (++) Enable the USARTx interface clock.
34         (++) UART pins configuration:
35             (+++) Enable the clock for the UART GPIOs.
36             (+++) Configure these UART pins as alternate function pull-up.
37         (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
38              and HAL_UART_Receive_IT() APIs):
39             (+++) Configure the USARTx interrupt priority.
40             (+++) Enable the NVIC USART IRQ handle.
41         (++) UART interrupts handling:
42               -@@-  The specific UART interrupts (Transmission complete interrupt,
43                 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
44                 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
45                 inside the transmit and receive processes.
46         (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
47              and HAL_UART_Receive_DMA() APIs):
48             (+++) Declare a DMA handle structure for the Tx/Rx channel.
49             (+++) Enable the DMAx interface clock.
50             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
51             (+++) Configure the DMA Tx/Rx channel.
52             (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
53             (+++) Configure the priority and enable the NVIC for the transfer complete
54                   interrupt on the DMA Tx/Rx channel.
55 
56     (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
57         flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
58 
59     (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
60         in the huart handle AdvancedInit structure.
61 
62     (#) For the UART asynchronous mode, initialize the UART registers by calling
63         the HAL_UART_Init() API.
64 
65     (#) For the UART Half duplex mode, initialize the UART registers by calling
66         the HAL_HalfDuplex_Init() API.
67 
68     (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
69         by calling the HAL_LIN_Init() API.
70 
71     (#) For the UART Multiprocessor mode, initialize the UART registers
72         by calling the HAL_MultiProcessor_Init() API.
73 
74     (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
75         by calling the HAL_RS485Ex_Init() API.
76 
77     [..]
78     (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
79         also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
80         calling the customized HAL_UART_MspInit() API.
81 
82     ##### Callback registration #####
83     ==================================
84 
85     [..]
86     The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
87     allows the user to configure dynamically the driver callbacks.
88 
89     [..]
90     Use Function HAL_UART_RegisterCallback() to register a user callback.
91     Function HAL_UART_RegisterCallback() allows to register following callbacks:
92     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
93     (+) TxCpltCallback            : Tx Complete Callback.
94     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
95     (+) RxCpltCallback            : Rx Complete Callback.
96     (+) ErrorCallback             : Error Callback.
97     (+) AbortCpltCallback         : Abort Complete Callback.
98     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
99     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
100     (+) WakeupCallback            : Wakeup Callback.
101     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
102     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
103     (+) MspInitCallback           : UART MspInit.
104     (+) MspDeInitCallback         : UART MspDeInit.
105     This function takes as parameters the HAL peripheral handle, the Callback ID
106     and a pointer to the user callback function.
107 
108     [..]
109     Use function HAL_UART_UnRegisterCallback() to reset a callback to the default
110     weak function.
111     HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
112     and the Callback ID.
113     This function allows to reset following callbacks:
114     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
115     (+) TxCpltCallback            : Tx Complete Callback.
116     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
117     (+) RxCpltCallback            : Rx Complete Callback.
118     (+) ErrorCallback             : Error Callback.
119     (+) AbortCpltCallback         : Abort Complete Callback.
120     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
121     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
122     (+) WakeupCallback            : Wakeup Callback.
123     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
124     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
125     (+) MspInitCallback           : UART MspInit.
126     (+) MspDeInitCallback         : UART MspDeInit.
127 
128     [..]
129     For specific callback RxEventCallback, use dedicated registration/reset functions:
130     respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback().
131 
132     [..]
133     By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
134     all callbacks are set to the corresponding weak functions:
135     examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback().
136     Exception done for MspInit and MspDeInit functions that are respectively
137     reset to the legacy weak functions in the HAL_UART_Init()
138     and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
139     If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit()
140     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
141 
142     [..]
143     Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
144     Exception done MspInit/MspDeInit that can be registered/unregistered
145     in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
146     MspInit/DeInit callbacks can be used during the Init/DeInit.
147     In that case first register the MspInit/MspDeInit user callbacks
148     using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit()
149     or HAL_UART_Init() function.
150 
151     [..]
152     When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
153     not defined, the callback registration feature is not available
154     and weak callbacks are used.
155 
156 
157   @endverbatim
158   ******************************************************************************
159   */
160 
161 /* Includes ------------------------------------------------------------------*/
162 #include "stm32wbaxx_hal.h"
163 
164 /** @addtogroup STM32WBAxx_HAL_Driver
165   * @{
166   */
167 
168 /** @defgroup UART UART
169   * @brief HAL UART module driver
170   * @{
171   */
172 
173 #ifdef HAL_UART_MODULE_ENABLED
174 
175 /* Private typedef -----------------------------------------------------------*/
176 /* Private define ------------------------------------------------------------*/
177 /** @defgroup UART_Private_Constants UART Private Constants
178   * @{
179   */
180 #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | \
181                                       USART_CR1_OVER8 | USART_CR1_FIFOEN)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
182 
183 #define USART_CR3_FIELDS  ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT | USART_CR3_TXFTCFG | \
184                                       USART_CR3_RXFTCFG)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
185 
186 #define LPUART_BRR_MIN  0x00000300U  /* LPUART BRR minimum authorized value */
187 #define LPUART_BRR_MAX  0x000FFFFFU  /* LPUART BRR maximum authorized value */
188 
189 #define UART_BRR_MIN    0x10U        /* UART BRR minimum authorized value */
190 #define UART_BRR_MAX    0x0000FFFFU  /* UART BRR maximum authorized value */
191 /**
192   * @}
193   */
194 
195 /* Private macros ------------------------------------------------------------*/
196 /* Private function prototypes -----------------------------------------------*/
197 /** @addtogroup UART_Private_Functions
198   * @{
199   */
200 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
201 #if defined(HAL_DMA_MODULE_ENABLED)
202 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
203 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
204 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
205 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
206 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
207 static void UART_DMAError(DMA_HandleTypeDef *hdma);
208 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
209 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
210 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
211 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
212 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
213 #endif /* HAL_DMA_MODULE_ENABLED */
214 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
215 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
216 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
217 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
218 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
219 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
220 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
221 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
222 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
223 /**
224   * @}
225   */
226 
227 /* Private variables ---------------------------------------------------------*/
228 /** @addtogroup UART_Private_variables
229   * @{
230   */
231 const uint16_t UARTPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
232 /**
233   * @}
234   */
235 
236 /* Exported Constants --------------------------------------------------------*/
237 /* Exported functions --------------------------------------------------------*/
238 
239 /** @defgroup UART_Exported_Functions UART Exported Functions
240   * @{
241   */
242 
243 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
244   *  @brief    Initialization and Configuration functions
245   *
246 @verbatim
247 ===============================================================================
248             ##### Initialization and Configuration functions #####
249  ===============================================================================
250     [..]
251     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
252     in asynchronous mode.
253       (+) For the asynchronous mode the parameters below can be configured:
254         (++) Baud Rate
255         (++) Word Length
256         (++) Stop Bit
257         (++) Parity: If the parity is enabled, then the MSB bit of the data written
258              in the data register is transmitted but is changed by the parity bit.
259         (++) Hardware flow control
260         (++) Receiver/transmitter modes
261         (++) Over Sampling Method
262         (++) One-Bit Sampling Method
263       (+) For the asynchronous mode, the following advanced features can be configured as well:
264         (++) TX and/or RX pin level inversion
265         (++) data logical level inversion
266         (++) RX and TX pins swap
267         (++) RX overrun detection disabling
268         (++) DMA disabling on RX error
269         (++) MSB first on communication line
270         (++) auto Baud rate detection
271     [..]
272     The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
273     follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
274     and UART multiprocessor mode configuration procedures (details for the procedures
275     are available in reference manual).
276 
277 @endverbatim
278 
279   Depending on the frame length defined by the M1 and M0 bits (7-bit,
280   8-bit or 9-bit), the possible UART formats are listed in the
281   following table.
282 
283   Table 1. UART frame format.
284     +-----------------------------------------------------------------------+
285     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
286     |---------|---------|-----------|---------------------------------------|
287     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
288     |---------|---------|-----------|---------------------------------------|
289     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
290     |---------|---------|-----------|---------------------------------------|
291     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
292     |---------|---------|-----------|---------------------------------------|
293     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
294     |---------|---------|-----------|---------------------------------------|
295     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
296     |---------|---------|-----------|---------------------------------------|
297     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
298     +-----------------------------------------------------------------------+
299 
300   * @{
301   */
302 
303 /**
304   * @brief Initialize the UART mode according to the specified
305   *        parameters in the UART_InitTypeDef and initialize the associated handle.
306   * @param huart UART handle.
307   * @retval HAL status
308   */
HAL_UART_Init(UART_HandleTypeDef * huart)309 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
310 {
311   /* Check the UART handle allocation */
312   if (huart == NULL)
313   {
314     return HAL_ERROR;
315   }
316 
317   if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
318   {
319     /* Check the parameters */
320     assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
321   }
322   else
323   {
324     /* Check the parameters */
325     assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
326   }
327 
328   if (huart->gState == HAL_UART_STATE_RESET)
329   {
330     /* Allocate lock resource and initialize it */
331     huart->Lock = HAL_UNLOCKED;
332 
333 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
334     UART_InitCallbacksToDefault(huart);
335 
336     if (huart->MspInitCallback == NULL)
337     {
338       huart->MspInitCallback = HAL_UART_MspInit;
339     }
340 
341     /* Init the low level hardware */
342     huart->MspInitCallback(huart);
343 #else
344     /* Init the low level hardware : GPIO, CLOCK */
345     HAL_UART_MspInit(huart);
346 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
347   }
348 
349   huart->gState = HAL_UART_STATE_BUSY;
350 
351   __HAL_UART_DISABLE(huart);
352 
353   /* Perform advanced settings configuration */
354   /* For some items, configuration requires to be done prior TE and RE bits are set */
355   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
356   {
357     UART_AdvFeatureConfig(huart);
358   }
359 
360   /* Set the UART Communication parameters */
361   if (UART_SetConfig(huart) == HAL_ERROR)
362   {
363     return HAL_ERROR;
364   }
365 
366   /* In asynchronous mode, the following bits must be kept cleared:
367   - LINEN and CLKEN bits in the USART_CR2 register,
368   - SCEN, HDSEL and IREN  bits in the USART_CR3 register.*/
369   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
370   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
371 
372   __HAL_UART_ENABLE(huart);
373 
374   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
375   return (UART_CheckIdleState(huart));
376 }
377 
378 /**
379   * @brief Initialize the half-duplex mode according to the specified
380   *        parameters in the UART_InitTypeDef and creates the associated handle.
381   * @param huart UART handle.
382   * @retval HAL status
383   */
HAL_HalfDuplex_Init(UART_HandleTypeDef * huart)384 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
385 {
386   /* Check the UART handle allocation */
387   if (huart == NULL)
388   {
389     return HAL_ERROR;
390   }
391 
392   /* Check UART instance */
393   assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
394 
395   if (huart->gState == HAL_UART_STATE_RESET)
396   {
397     /* Allocate lock resource and initialize it */
398     huart->Lock = HAL_UNLOCKED;
399 
400 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
401     UART_InitCallbacksToDefault(huart);
402 
403     if (huart->MspInitCallback == NULL)
404     {
405       huart->MspInitCallback = HAL_UART_MspInit;
406     }
407 
408     /* Init the low level hardware */
409     huart->MspInitCallback(huart);
410 #else
411     /* Init the low level hardware : GPIO, CLOCK */
412     HAL_UART_MspInit(huart);
413 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
414   }
415 
416   huart->gState = HAL_UART_STATE_BUSY;
417 
418   __HAL_UART_DISABLE(huart);
419 
420   /* Perform advanced settings configuration */
421   /* For some items, configuration requires to be done prior TE and RE bits are set */
422   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
423   {
424     UART_AdvFeatureConfig(huart);
425   }
426 
427   /* Set the UART Communication parameters */
428   if (UART_SetConfig(huart) == HAL_ERROR)
429   {
430     return HAL_ERROR;
431   }
432 
433   /* In half-duplex mode, the following bits must be kept cleared:
434   - LINEN and CLKEN bits in the USART_CR2 register,
435   - SCEN and IREN bits in the USART_CR3 register.*/
436   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
437   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
438 
439   /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
440   SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
441 
442   __HAL_UART_ENABLE(huart);
443 
444   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
445   return (UART_CheckIdleState(huart));
446 }
447 
448 
449 /**
450   * @brief Initialize the LIN mode according to the specified
451   *        parameters in the UART_InitTypeDef and creates the associated handle.
452   * @param huart             UART handle.
453   * @param BreakDetectLength Specifies the LIN break detection length.
454   *        This parameter can be one of the following values:
455   *          @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
456   *          @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
457   * @retval HAL status
458   */
HAL_LIN_Init(UART_HandleTypeDef * huart,uint32_t BreakDetectLength)459 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
460 {
461   /* Check the UART handle allocation */
462   if (huart == NULL)
463   {
464     return HAL_ERROR;
465   }
466 
467   /* Check the LIN UART instance */
468   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
469   /* Check the Break detection length parameter */
470   assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
471 
472   /* LIN mode limited to 16-bit oversampling only */
473   if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
474   {
475     return HAL_ERROR;
476   }
477   /* LIN mode limited to 8-bit data length */
478   if (huart->Init.WordLength != UART_WORDLENGTH_8B)
479   {
480     return HAL_ERROR;
481   }
482 
483   if (huart->gState == HAL_UART_STATE_RESET)
484   {
485     /* Allocate lock resource and initialize it */
486     huart->Lock = HAL_UNLOCKED;
487 
488 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
489     UART_InitCallbacksToDefault(huart);
490 
491     if (huart->MspInitCallback == NULL)
492     {
493       huart->MspInitCallback = HAL_UART_MspInit;
494     }
495 
496     /* Init the low level hardware */
497     huart->MspInitCallback(huart);
498 #else
499     /* Init the low level hardware : GPIO, CLOCK */
500     HAL_UART_MspInit(huart);
501 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
502   }
503 
504   huart->gState = HAL_UART_STATE_BUSY;
505 
506   __HAL_UART_DISABLE(huart);
507 
508   /* Perform advanced settings configuration */
509   /* For some items, configuration requires to be done prior TE and RE bits are set */
510   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
511   {
512     UART_AdvFeatureConfig(huart);
513   }
514 
515   /* Set the UART Communication parameters */
516   if (UART_SetConfig(huart) == HAL_ERROR)
517   {
518     return HAL_ERROR;
519   }
520 
521   /* In LIN mode, the following bits must be kept cleared:
522   - LINEN and CLKEN bits in the USART_CR2 register,
523   - SCEN and IREN bits in the USART_CR3 register.*/
524   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
525   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
526 
527   /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
528   SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
529 
530   /* Set the USART LIN Break detection length. */
531   MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
532 
533   __HAL_UART_ENABLE(huart);
534 
535   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
536   return (UART_CheckIdleState(huart));
537 }
538 
539 
540 /**
541   * @brief Initialize the multiprocessor mode according to the specified
542   *        parameters in the UART_InitTypeDef and initialize the associated handle.
543   * @param huart        UART handle.
544   * @param Address      UART node address (4-, 6-, 7- or 8-bit long).
545   * @param WakeUpMethod Specifies the UART wakeup method.
546   *        This parameter can be one of the following values:
547   *          @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
548   *          @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
549   * @note  If the user resorts to idle line detection wake up, the Address parameter
550   *        is useless and ignored by the initialization function.
551   * @note  If the user resorts to address mark wake up, the address length detection
552   *        is configured by default to 4 bits only. For the UART to be able to
553   *        manage 6-, 7- or 8-bit long addresses detection, the API
554   *        HAL_MultiProcessorEx_AddressLength_Set() must be called after
555   *        HAL_MultiProcessor_Init().
556   * @retval HAL status
557   */
HAL_MultiProcessor_Init(UART_HandleTypeDef * huart,uint8_t Address,uint32_t WakeUpMethod)558 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
559 {
560   /* Check the UART handle allocation */
561   if (huart == NULL)
562   {
563     return HAL_ERROR;
564   }
565 
566   /* Check the wake up method parameter */
567   assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
568 
569   if (huart->gState == HAL_UART_STATE_RESET)
570   {
571     /* Allocate lock resource and initialize it */
572     huart->Lock = HAL_UNLOCKED;
573 
574 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
575     UART_InitCallbacksToDefault(huart);
576 
577     if (huart->MspInitCallback == NULL)
578     {
579       huart->MspInitCallback = HAL_UART_MspInit;
580     }
581 
582     /* Init the low level hardware */
583     huart->MspInitCallback(huart);
584 #else
585     /* Init the low level hardware : GPIO, CLOCK */
586     HAL_UART_MspInit(huart);
587 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
588   }
589 
590   huart->gState = HAL_UART_STATE_BUSY;
591 
592   __HAL_UART_DISABLE(huart);
593 
594   /* Perform advanced settings configuration */
595   /* For some items, configuration requires to be done prior TE and RE bits are set */
596   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
597   {
598     UART_AdvFeatureConfig(huart);
599   }
600 
601   /* Set the UART Communication parameters */
602   if (UART_SetConfig(huart) == HAL_ERROR)
603   {
604     return HAL_ERROR;
605   }
606 
607   /* In multiprocessor mode, the following bits must be kept cleared:
608   - LINEN and CLKEN bits in the USART_CR2 register,
609   - SCEN, HDSEL and IREN  bits in the USART_CR3 register. */
610   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
611   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
612 
613   if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
614   {
615     /* If address mark wake up method is chosen, set the USART address node */
616     MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
617   }
618 
619   /* Set the wake up method by setting the WAKE bit in the CR1 register */
620   MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
621 
622   __HAL_UART_ENABLE(huart);
623 
624   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
625   return (UART_CheckIdleState(huart));
626 }
627 
628 
629 /**
630   * @brief DeInitialize the UART peripheral.
631   * @param huart UART handle.
632   * @retval HAL status
633   */
HAL_UART_DeInit(UART_HandleTypeDef * huart)634 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
635 {
636   /* Check the UART handle allocation */
637   if (huart == NULL)
638   {
639     return HAL_ERROR;
640   }
641 
642   /* Check the parameters */
643   assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
644 
645   huart->gState = HAL_UART_STATE_BUSY;
646 
647   __HAL_UART_DISABLE(huart);
648 
649   huart->Instance->CR1 = 0x0U;
650   huart->Instance->CR2 = 0x0U;
651   huart->Instance->CR3 = 0x0U;
652 
653 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
654   if (huart->MspDeInitCallback == NULL)
655   {
656     huart->MspDeInitCallback = HAL_UART_MspDeInit;
657   }
658   /* DeInit the low level hardware */
659   huart->MspDeInitCallback(huart);
660 #else
661   /* DeInit the low level hardware */
662   HAL_UART_MspDeInit(huart);
663 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
664 
665   huart->ErrorCode = HAL_UART_ERROR_NONE;
666   huart->gState = HAL_UART_STATE_RESET;
667   huart->RxState = HAL_UART_STATE_RESET;
668   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
669   huart->RxEventType = HAL_UART_RXEVENT_TC;
670 
671   __HAL_UNLOCK(huart);
672 
673   return HAL_OK;
674 }
675 
676 /**
677   * @brief Initialize the UART MSP.
678   * @param huart UART handle.
679   * @retval None
680   */
HAL_UART_MspInit(UART_HandleTypeDef * huart)681 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
682 {
683   /* Prevent unused argument(s) compilation warning */
684   UNUSED(huart);
685 
686   /* NOTE : This function should not be modified, when the callback is needed,
687             the HAL_UART_MspInit can be implemented in the user file
688    */
689 }
690 
691 /**
692   * @brief DeInitialize the UART MSP.
693   * @param huart UART handle.
694   * @retval None
695   */
HAL_UART_MspDeInit(UART_HandleTypeDef * huart)696 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
697 {
698   /* Prevent unused argument(s) compilation warning */
699   UNUSED(huart);
700 
701   /* NOTE : This function should not be modified, when the callback is needed,
702             the HAL_UART_MspDeInit can be implemented in the user file
703    */
704 }
705 
706 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
707 /**
708   * @brief  Register a User UART Callback
709   *         To be used to override the weak predefined callback
710   * @note   The HAL_UART_RegisterCallback() may be called before HAL_UART_Init(), HAL_HalfDuplex_Init(),
711   *         HAL_LIN_Init(), HAL_MultiProcessor_Init() or HAL_RS485Ex_Init() in HAL_UART_STATE_RESET to register
712   *         callbacks for HAL_UART_MSPINIT_CB_ID and HAL_UART_MSPDEINIT_CB_ID
713   * @param  huart uart handle
714   * @param  CallbackID ID of the callback to be registered
715   *         This parameter can be one of the following values:
716   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
717   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
718   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
719   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
720   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
721   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
722   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
723   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
724   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
725   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
726   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
727   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
728   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
729   * @param  pCallback pointer to the Callback function
730   * @retval HAL status
731   */
HAL_UART_RegisterCallback(UART_HandleTypeDef * huart,HAL_UART_CallbackIDTypeDef CallbackID,pUART_CallbackTypeDef pCallback)732 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
733                                             pUART_CallbackTypeDef pCallback)
734 {
735   HAL_StatusTypeDef status = HAL_OK;
736 
737   if (pCallback == NULL)
738   {
739     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
740 
741     return HAL_ERROR;
742   }
743 
744   if (huart->gState == HAL_UART_STATE_READY)
745   {
746     switch (CallbackID)
747     {
748       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
749         huart->TxHalfCpltCallback = pCallback;
750         break;
751 
752       case HAL_UART_TX_COMPLETE_CB_ID :
753         huart->TxCpltCallback = pCallback;
754         break;
755 
756       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
757         huart->RxHalfCpltCallback = pCallback;
758         break;
759 
760       case HAL_UART_RX_COMPLETE_CB_ID :
761         huart->RxCpltCallback = pCallback;
762         break;
763 
764       case HAL_UART_ERROR_CB_ID :
765         huart->ErrorCallback = pCallback;
766         break;
767 
768       case HAL_UART_ABORT_COMPLETE_CB_ID :
769         huart->AbortCpltCallback = pCallback;
770         break;
771 
772       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
773         huart->AbortTransmitCpltCallback = pCallback;
774         break;
775 
776       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
777         huart->AbortReceiveCpltCallback = pCallback;
778         break;
779 
780       case HAL_UART_RX_FIFO_FULL_CB_ID :
781         huart->RxFifoFullCallback = pCallback;
782         break;
783 
784       case HAL_UART_TX_FIFO_EMPTY_CB_ID :
785         huart->TxFifoEmptyCallback = pCallback;
786         break;
787 
788       case HAL_UART_MSPINIT_CB_ID :
789         huart->MspInitCallback = pCallback;
790         break;
791 
792       case HAL_UART_MSPDEINIT_CB_ID :
793         huart->MspDeInitCallback = pCallback;
794         break;
795 
796       default :
797         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
798 
799         status =  HAL_ERROR;
800         break;
801     }
802   }
803   else if (huart->gState == HAL_UART_STATE_RESET)
804   {
805     switch (CallbackID)
806     {
807       case HAL_UART_MSPINIT_CB_ID :
808         huart->MspInitCallback = pCallback;
809         break;
810 
811       case HAL_UART_MSPDEINIT_CB_ID :
812         huart->MspDeInitCallback = pCallback;
813         break;
814 
815       default :
816         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
817 
818         status =  HAL_ERROR;
819         break;
820     }
821   }
822   else
823   {
824     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
825 
826     status =  HAL_ERROR;
827   }
828 
829   return status;
830 }
831 
832 /**
833   * @brief  Unregister an UART Callback
834   *         UART callaback is redirected to the weak predefined callback
835   * @note   The HAL_UART_UnRegisterCallback() may be called before HAL_UART_Init(), HAL_HalfDuplex_Init(),
836   *         HAL_LIN_Init(), HAL_MultiProcessor_Init() or HAL_RS485Ex_Init() in HAL_UART_STATE_RESET to un-register
837   *         callbacks for HAL_UART_MSPINIT_CB_ID and HAL_UART_MSPDEINIT_CB_ID
838   * @param  huart uart handle
839   * @param  CallbackID ID of the callback to be unregistered
840   *         This parameter can be one of the following values:
841   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
842   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
843   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
844   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
845   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
846   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
847   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
848   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
849   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
850   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
851   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
852   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
853   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
854   * @retval HAL status
855   */
HAL_UART_UnRegisterCallback(UART_HandleTypeDef * huart,HAL_UART_CallbackIDTypeDef CallbackID)856 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
857 {
858   HAL_StatusTypeDef status = HAL_OK;
859 
860   if (HAL_UART_STATE_READY == huart->gState)
861   {
862     switch (CallbackID)
863     {
864       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
865         huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback    */
866         break;
867 
868       case HAL_UART_TX_COMPLETE_CB_ID :
869         huart->TxCpltCallback = HAL_UART_TxCpltCallback;                       /* Legacy weak TxCpltCallback         */
870         break;
871 
872       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
873         huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback     */
874         break;
875 
876       case HAL_UART_RX_COMPLETE_CB_ID :
877         huart->RxCpltCallback = HAL_UART_RxCpltCallback;                       /* Legacy weak RxCpltCallback         */
878         break;
879 
880       case HAL_UART_ERROR_CB_ID :
881         huart->ErrorCallback = HAL_UART_ErrorCallback;                         /* Legacy weak ErrorCallback          */
882         break;
883 
884       case HAL_UART_ABORT_COMPLETE_CB_ID :
885         huart->AbortCpltCallback = HAL_UART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback      */
886         break;
887 
888       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
889         huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak
890                                                                                   AbortTransmitCpltCallback          */
891         break;
892 
893       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
894         huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback;   /* Legacy weak
895                                                                                   AbortReceiveCpltCallback           */
896         break;
897 
898       case HAL_UART_RX_FIFO_FULL_CB_ID :
899         huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback;             /* Legacy weak RxFifoFullCallback     */
900         break;
901 
902       case HAL_UART_TX_FIFO_EMPTY_CB_ID :
903         huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback;           /* Legacy weak TxFifoEmptyCallback    */
904         break;
905 
906       case HAL_UART_MSPINIT_CB_ID :
907         huart->MspInitCallback = HAL_UART_MspInit;                             /* Legacy weak MspInitCallback        */
908         break;
909 
910       case HAL_UART_MSPDEINIT_CB_ID :
911         huart->MspDeInitCallback = HAL_UART_MspDeInit;                         /* Legacy weak MspDeInitCallback      */
912         break;
913 
914       default :
915         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
916 
917         status =  HAL_ERROR;
918         break;
919     }
920   }
921   else if (HAL_UART_STATE_RESET == huart->gState)
922   {
923     switch (CallbackID)
924     {
925       case HAL_UART_MSPINIT_CB_ID :
926         huart->MspInitCallback = HAL_UART_MspInit;
927         break;
928 
929       case HAL_UART_MSPDEINIT_CB_ID :
930         huart->MspDeInitCallback = HAL_UART_MspDeInit;
931         break;
932 
933       default :
934         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
935 
936         status =  HAL_ERROR;
937         break;
938     }
939   }
940   else
941   {
942     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
943 
944     status =  HAL_ERROR;
945   }
946 
947   return status;
948 }
949 
950 /**
951   * @brief  Register a User UART Rx Event Callback
952   *         To be used instead of the weak predefined callback
953   * @param  huart     Uart handle
954   * @param  pCallback Pointer to the Rx Event Callback function
955   * @retval HAL status
956   */
HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef * huart,pUART_RxEventCallbackTypeDef pCallback)957 HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback)
958 {
959   HAL_StatusTypeDef status = HAL_OK;
960 
961   if (pCallback == NULL)
962   {
963     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
964 
965     return HAL_ERROR;
966   }
967 
968   /* Process locked */
969   __HAL_LOCK(huart);
970 
971   if (huart->gState == HAL_UART_STATE_READY)
972   {
973     huart->RxEventCallback = pCallback;
974   }
975   else
976   {
977     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
978 
979     status =  HAL_ERROR;
980   }
981 
982   /* Release Lock */
983   __HAL_UNLOCK(huart);
984 
985   return status;
986 }
987 
988 /**
989   * @brief  UnRegister the UART Rx Event Callback
990   *         UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback
991   * @param  huart     Uart handle
992   * @retval HAL status
993   */
HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef * huart)994 HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart)
995 {
996   HAL_StatusTypeDef status = HAL_OK;
997 
998   /* Process locked */
999   __HAL_LOCK(huart);
1000 
1001   if (huart->gState == HAL_UART_STATE_READY)
1002   {
1003     huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback  */
1004   }
1005   else
1006   {
1007     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
1008 
1009     status =  HAL_ERROR;
1010   }
1011 
1012   /* Release Lock */
1013   __HAL_UNLOCK(huart);
1014   return status;
1015 }
1016 
1017 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1018 
1019 /**
1020   * @}
1021   */
1022 
1023 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
1024   * @brief UART Transmit/Receive functions
1025   *
1026 @verbatim
1027  ===============================================================================
1028                       ##### IO operation functions #####
1029  ===============================================================================
1030     This subsection provides a set of functions allowing to manage the UART asynchronous
1031     and Half duplex data transfers.
1032 
1033     (#) There are two mode of transfer:
1034        (+) Blocking mode: The communication is performed in polling mode.
1035            The HAL status of all data processing is returned by the same function
1036            after finishing transfer.
1037        (+) Non-Blocking mode: The communication is performed using Interrupts
1038            or DMA, These API's return the HAL status.
1039            The end of the data processing will be indicated through the
1040            dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
1041            using DMA mode.
1042            The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
1043            will be executed respectively at the end of the transmit or Receive process
1044            The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
1045 
1046     (#) Blocking mode API's are :
1047         (+) HAL_UART_Transmit()
1048         (+) HAL_UART_Receive()
1049 
1050     (#) Non-Blocking mode API's with Interrupt are :
1051         (+) HAL_UART_Transmit_IT()
1052         (+) HAL_UART_Receive_IT()
1053         (+) HAL_UART_IRQHandler()
1054 
1055     (#) Non-Blocking mode API's with DMA are :
1056         (+) HAL_UART_Transmit_DMA()
1057         (+) HAL_UART_Receive_DMA()
1058         (+) HAL_UART_DMAPause()
1059         (+) HAL_UART_DMAResume()
1060         (+) HAL_UART_DMAStop()
1061 
1062     (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
1063         (+) HAL_UART_TxHalfCpltCallback()
1064         (+) HAL_UART_TxCpltCallback()
1065         (+) HAL_UART_RxHalfCpltCallback()
1066         (+) HAL_UART_RxCpltCallback()
1067         (+) HAL_UART_ErrorCallback()
1068 
1069     (#) Non-Blocking mode transfers could be aborted using Abort API's :
1070         (+) HAL_UART_Abort()
1071         (+) HAL_UART_AbortTransmit()
1072         (+) HAL_UART_AbortReceive()
1073         (+) HAL_UART_Abort_IT()
1074         (+) HAL_UART_AbortTransmit_IT()
1075         (+) HAL_UART_AbortReceive_IT()
1076 
1077     (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
1078         (+) HAL_UART_AbortCpltCallback()
1079         (+) HAL_UART_AbortTransmitCpltCallback()
1080         (+) HAL_UART_AbortReceiveCpltCallback()
1081 
1082     (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced
1083         reception services:
1084         (+) HAL_UARTEx_RxEventCallback()
1085 
1086     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
1087         Errors are handled as follows :
1088        (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1089            to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
1090            in Interrupt mode reception .
1091            Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
1092            to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1093            Transfer is kept ongoing on UART side.
1094            If user wants to abort it, Abort services should be called by user.
1095        (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1096            This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1097            Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback()
1098            user callback is executed.
1099 
1100     -@- In the Half duplex communication, it is forbidden to run the transmit
1101         and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1102 
1103 @endverbatim
1104   * @{
1105   */
1106 
1107 /**
1108   * @brief Send an amount of data in blocking mode.
1109   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1110   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1111   *         of u16 provided through pData.
1112   * @note When FIFO mode is enabled, writing a data in the TDR register adds one
1113   *       data to the TXFIFO. Write operations to the TDR register are performed
1114   *       when TXFNF flag is set. From hardware perspective, TXFNF flag and
1115   *       TXE are mapped on the same bit-field.
1116   * @param huart   UART handle.
1117   * @param pData   Pointer to data buffer (u8 or u16 data elements).
1118   * @param Size    Amount of data elements (u8 or u16) to be sent.
1119   * @param Timeout Timeout duration.
1120   * @retval HAL status
1121   */
HAL_UART_Transmit(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size,uint32_t Timeout)1122 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
1123 {
1124   const uint8_t  *pdata8bits;
1125   const uint16_t *pdata16bits;
1126   uint32_t tickstart;
1127 
1128   /* Check that a Tx process is not already ongoing */
1129   if (huart->gState == HAL_UART_STATE_READY)
1130   {
1131     if ((pData == NULL) || (Size == 0U))
1132     {
1133       return  HAL_ERROR;
1134     }
1135 
1136     huart->ErrorCode = HAL_UART_ERROR_NONE;
1137     huart->gState = HAL_UART_STATE_BUSY_TX;
1138 
1139     /* Init tickstart for timeout management */
1140     tickstart = HAL_GetTick();
1141 
1142     huart->TxXferSize  = Size;
1143     huart->TxXferCount = Size;
1144 
1145     /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1146     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1147     {
1148       pdata8bits  = NULL;
1149       pdata16bits = (const uint16_t *) pData;
1150     }
1151     else
1152     {
1153       pdata8bits  = pData;
1154       pdata16bits = NULL;
1155     }
1156 
1157     while (huart->TxXferCount > 0U)
1158     {
1159       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1160       {
1161 
1162         huart->gState = HAL_UART_STATE_READY;
1163 
1164         return HAL_TIMEOUT;
1165       }
1166       if (pdata8bits == NULL)
1167       {
1168         huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
1169         pdata16bits++;
1170       }
1171       else
1172       {
1173         huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
1174         pdata8bits++;
1175       }
1176       huart->TxXferCount--;
1177     }
1178 
1179     if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
1180     {
1181       huart->gState = HAL_UART_STATE_READY;
1182 
1183       return HAL_TIMEOUT;
1184     }
1185 
1186     /* At end of Tx process, restore huart->gState to Ready */
1187     huart->gState = HAL_UART_STATE_READY;
1188 
1189     return HAL_OK;
1190   }
1191   else
1192   {
1193     return HAL_BUSY;
1194   }
1195 }
1196 
1197 /**
1198   * @brief Receive an amount of data in blocking mode.
1199   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1200   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1201   *         of u16 available through pData.
1202   * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
1203   *       is not empty. Read operations from the RDR register are performed when
1204   *       RXFNE flag is set. From hardware perspective, RXFNE flag and
1205   *       RXNE are mapped on the same bit-field.
1206   * @param huart   UART handle.
1207   * @param pData   Pointer to data buffer (u8 or u16 data elements).
1208   * @param Size    Amount of data elements (u8 or u16) to be received.
1209   * @param Timeout Timeout duration.
1210   * @retval HAL status
1211   */
HAL_UART_Receive(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size,uint32_t Timeout)1212 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1213 {
1214   uint8_t  *pdata8bits;
1215   uint16_t *pdata16bits;
1216   uint16_t uhMask;
1217   uint32_t tickstart;
1218 
1219   /* Check that a Rx process is not already ongoing */
1220   if (huart->RxState == HAL_UART_STATE_READY)
1221   {
1222     if ((pData == NULL) || (Size == 0U))
1223     {
1224       return  HAL_ERROR;
1225     }
1226 
1227     huart->ErrorCode = HAL_UART_ERROR_NONE;
1228     huart->RxState = HAL_UART_STATE_BUSY_RX;
1229     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1230 
1231     /* Init tickstart for timeout management */
1232     tickstart = HAL_GetTick();
1233 
1234     huart->RxXferSize  = Size;
1235     huart->RxXferCount = Size;
1236 
1237     /* Computation of UART mask to apply to RDR register */
1238     UART_MASK_COMPUTATION(huart);
1239     uhMask = huart->Mask;
1240 
1241     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1242     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1243     {
1244       pdata8bits  = NULL;
1245       pdata16bits = (uint16_t *) pData;
1246     }
1247     else
1248     {
1249       pdata8bits  = pData;
1250       pdata16bits = NULL;
1251     }
1252 
1253     /* as long as data have to be received */
1254     while (huart->RxXferCount > 0U)
1255     {
1256       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1257       {
1258         huart->RxState = HAL_UART_STATE_READY;
1259 
1260         return HAL_TIMEOUT;
1261       }
1262       if (pdata8bits == NULL)
1263       {
1264         *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
1265         pdata16bits++;
1266       }
1267       else
1268       {
1269         *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1270         pdata8bits++;
1271       }
1272       huart->RxXferCount--;
1273     }
1274 
1275     /* At end of Rx process, restore huart->RxState to Ready */
1276     huart->RxState = HAL_UART_STATE_READY;
1277 
1278     return HAL_OK;
1279   }
1280   else
1281   {
1282     return HAL_BUSY;
1283   }
1284 }
1285 
1286 /**
1287   * @brief Send an amount of data in interrupt mode.
1288   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1289   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1290   *         of u16 provided through pData.
1291   * @param huart UART handle.
1292   * @param pData Pointer to data buffer (u8 or u16 data elements).
1293   * @param Size  Amount of data elements (u8 or u16) to be sent.
1294   * @retval HAL status
1295   */
HAL_UART_Transmit_IT(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size)1296 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1297 {
1298   /* Check that a Tx process is not already ongoing */
1299   if (huart->gState == HAL_UART_STATE_READY)
1300   {
1301     if ((pData == NULL) || (Size == 0U))
1302     {
1303       return HAL_ERROR;
1304     }
1305 
1306     huart->pTxBuffPtr  = pData;
1307     huart->TxXferSize  = Size;
1308     huart->TxXferCount = Size;
1309     huart->TxISR       = NULL;
1310 
1311     huart->ErrorCode = HAL_UART_ERROR_NONE;
1312     huart->gState = HAL_UART_STATE_BUSY_TX;
1313 
1314     /* Configure Tx interrupt processing */
1315     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1316     {
1317       /* Set the Tx ISR function pointer according to the data word length */
1318       if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1319       {
1320         huart->TxISR = UART_TxISR_16BIT_FIFOEN;
1321       }
1322       else
1323       {
1324         huart->TxISR = UART_TxISR_8BIT_FIFOEN;
1325       }
1326 
1327       /* Enable the TX FIFO threshold interrupt */
1328       ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1329     }
1330     else
1331     {
1332       /* Set the Tx ISR function pointer according to the data word length */
1333       if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1334       {
1335         huart->TxISR = UART_TxISR_16BIT;
1336       }
1337       else
1338       {
1339         huart->TxISR = UART_TxISR_8BIT;
1340       }
1341 
1342       /* Enable the Transmit Data Register Empty interrupt */
1343       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1344     }
1345 
1346     return HAL_OK;
1347   }
1348   else
1349   {
1350     return HAL_BUSY;
1351   }
1352 }
1353 
1354 /**
1355   * @brief Receive an amount of data in interrupt mode.
1356   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1357   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1358   *         of u16 available through pData.
1359   * @param huart UART handle.
1360   * @param pData Pointer to data buffer (u8 or u16 data elements).
1361   * @param Size  Amount of data elements (u8 or u16) to be received.
1362   * @retval HAL status
1363   */
HAL_UART_Receive_IT(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)1364 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1365 {
1366   /* Check that a Rx process is not already ongoing */
1367   if (huart->RxState == HAL_UART_STATE_READY)
1368   {
1369     if ((pData == NULL) || (Size == 0U))
1370     {
1371       return HAL_ERROR;
1372     }
1373 
1374     /* Set Reception type to Standard reception */
1375     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1376 
1377     if (!(IS_LPUART_INSTANCE(huart->Instance)))
1378     {
1379       /* Check that USART RTOEN bit is set */
1380       if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1381       {
1382         /* Enable the UART Receiver Timeout Interrupt */
1383         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1384       }
1385     }
1386 
1387     return (UART_Start_Receive_IT(huart, pData, Size));
1388   }
1389   else
1390   {
1391     return HAL_BUSY;
1392   }
1393 }
1394 
1395 #if defined(HAL_DMA_MODULE_ENABLED)
1396 /**
1397   * @brief Send an amount of data in DMA mode.
1398   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1399   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1400   *         of u16 provided through pData.
1401   * @param huart UART handle.
1402   * @param pData Pointer to data buffer (u8 or u16 data elements).
1403   * @param Size  Amount of data elements (u8 or u16) to be sent.
1404   * @retval HAL status
1405   */
HAL_UART_Transmit_DMA(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size)1406 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1407 {
1408   HAL_StatusTypeDef status;
1409   uint16_t nbByte = Size;
1410 
1411   /* Check that a Tx process is not already ongoing */
1412   if (huart->gState == HAL_UART_STATE_READY)
1413   {
1414     if ((pData == NULL) || (Size == 0U))
1415     {
1416       return HAL_ERROR;
1417     }
1418 
1419     huart->pTxBuffPtr  = pData;
1420     huart->TxXferSize  = Size;
1421     huart->TxXferCount = Size;
1422 
1423     huart->ErrorCode = HAL_UART_ERROR_NONE;
1424     huart->gState = HAL_UART_STATE_BUSY_TX;
1425 
1426     if (huart->hdmatx != NULL)
1427     {
1428       /* Set the UART DMA transfer complete callback */
1429       huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
1430 
1431       /* Set the UART DMA Half transfer complete callback */
1432       huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
1433 
1434       /* Set the DMA error callback */
1435       huart->hdmatx->XferErrorCallback = UART_DMAError;
1436 
1437       /* Set the DMA abort callback */
1438       huart->hdmatx->XferAbortCallback = NULL;
1439 
1440       /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
1441          should be aligned on a u16 frontier, so nbByte should be equal to Size * 2 */
1442       if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1443       {
1444         nbByte = Size * 2U;
1445       }
1446 
1447       /* Check linked list mode */
1448       if ((huart->hdmatx->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
1449       {
1450         if ((huart->hdmatx->LinkedListQueue != NULL) && (huart->hdmatx->LinkedListQueue->Head != NULL))
1451         {
1452           /* Set DMA data size */
1453           huart->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = nbByte;
1454 
1455           /* Set DMA source address */
1456           huart->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] = (uint32_t)huart->pTxBuffPtr;
1457 
1458           /* Set DMA destination address */
1459           huart->hdmatx->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] =
1460             (uint32_t)&huart->Instance->TDR;
1461 
1462           /* Enable the UART transmit DMA channel */
1463           status = HAL_DMAEx_List_Start_IT(huart->hdmatx);
1464         }
1465         else
1466         {
1467           /* Update status */
1468           status = HAL_ERROR;
1469         }
1470       }
1471       else
1472       {
1473         /* Enable the UART transmit DMA channel */
1474         status = HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, nbByte);
1475       }
1476 
1477       if (status != HAL_OK)
1478       {
1479         /* Set error code to DMA */
1480         huart->ErrorCode = HAL_UART_ERROR_DMA;
1481 
1482         /* Restore huart->gState to ready */
1483         huart->gState = HAL_UART_STATE_READY;
1484 
1485         return HAL_ERROR;
1486       }
1487     }
1488     /* Clear the TC flag in the ICR register */
1489     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
1490 
1491     /* Enable the DMA transfer for transmit request by setting the DMAT bit
1492     in the UART CR3 register */
1493     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1494 
1495     return HAL_OK;
1496   }
1497   else
1498   {
1499     return HAL_BUSY;
1500   }
1501 }
1502 
1503 /**
1504   * @brief Receive an amount of data in DMA mode.
1505   * @note   When the UART parity is enabled (PCE = 1), the received data contain
1506   *         the parity bit (MSB position).
1507   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1508   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1509   *         of u16 available through pData.
1510   * @param huart UART handle.
1511   * @param pData Pointer to data buffer (u8 or u16 data elements).
1512   * @param Size  Amount of data elements (u8 or u16) to be received.
1513   * @retval HAL status
1514   */
HAL_UART_Receive_DMA(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)1515 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1516 {
1517   /* Check that a Rx process is not already ongoing */
1518   if (huart->RxState == HAL_UART_STATE_READY)
1519   {
1520     if ((pData == NULL) || (Size == 0U))
1521     {
1522       return HAL_ERROR;
1523     }
1524 
1525     /* Set Reception type to Standard reception */
1526     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1527 
1528     if (!(IS_LPUART_INSTANCE(huart->Instance)))
1529     {
1530       /* Check that USART RTOEN bit is set */
1531       if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1532       {
1533         /* Enable the UART Receiver Timeout Interrupt */
1534         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1535       }
1536     }
1537 
1538     return (UART_Start_Receive_DMA(huart, pData, Size));
1539   }
1540   else
1541   {
1542     return HAL_BUSY;
1543   }
1544 }
1545 
1546 /**
1547   * @brief Pause the DMA Transfer.
1548   * @param huart UART handle.
1549   * @retval HAL status
1550   */
HAL_UART_DMAPause(UART_HandleTypeDef * huart)1551 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1552 {
1553   const HAL_UART_StateTypeDef gstate = huart->gState;
1554   const HAL_UART_StateTypeDef rxstate = huart->RxState;
1555 
1556   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1557       (gstate == HAL_UART_STATE_BUSY_TX))
1558   {
1559     /* Suspend the UART DMA Tx channel : use blocking DMA Suspend API (no callback) */
1560     if (huart->hdmatx != NULL)
1561     {
1562       /* Set the UART DMA Suspend callback to Null.
1563          No call back execution at end of DMA Suspend procedure */
1564       huart->hdmatx->XferSuspendCallback = NULL;
1565 
1566       if (HAL_DMAEx_Suspend(huart->hdmatx) != HAL_OK)
1567       {
1568         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1569         {
1570           /* Set error code to DMA */
1571           huart->ErrorCode = HAL_UART_ERROR_DMA;
1572 
1573           return HAL_TIMEOUT;
1574         }
1575       }
1576     }
1577   }
1578   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1579       (rxstate == HAL_UART_STATE_BUSY_RX))
1580   {
1581     /* Suspend the UART DMA Rx channel : use blocking DMA Suspend API (no callback) */
1582     if (huart->hdmarx != NULL)
1583     {
1584       /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1585       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1586       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1587 
1588       /* Set the UART DMA Suspend callback to Null.
1589          No call back execution at end of DMA Suspend procedure */
1590       huart->hdmarx->XferSuspendCallback = NULL;
1591 
1592       if (HAL_DMAEx_Suspend(huart->hdmarx) != HAL_OK)
1593       {
1594         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1595         {
1596           /* Set error code to DMA */
1597           huart->ErrorCode = HAL_UART_ERROR_DMA;
1598 
1599           return HAL_TIMEOUT;
1600         }
1601       }
1602     }
1603   }
1604 
1605   return HAL_OK;
1606 }
1607 
1608 /**
1609   * @brief Resume the DMA Transfer.
1610   * @param huart UART handle.
1611   * @retval HAL status
1612   */
HAL_UART_DMAResume(UART_HandleTypeDef * huart)1613 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1614 {
1615   if (huart->gState == HAL_UART_STATE_BUSY_TX)
1616   {
1617     /* Resume the UART DMA Tx channel */
1618     if (huart->hdmatx != NULL)
1619     {
1620       if (HAL_DMAEx_Resume(huart->hdmatx) != HAL_OK)
1621       {
1622         /* Set error code to DMA */
1623         huart->ErrorCode = HAL_UART_ERROR_DMA;
1624 
1625         return HAL_ERROR;
1626       }
1627     }
1628   }
1629   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
1630   {
1631     /* Clear the Overrun flag before resuming the Rx transfer */
1632     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
1633 
1634     /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1635     if (huart->Init.Parity != UART_PARITY_NONE)
1636     {
1637       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1638     }
1639     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1640 
1641     /* Resume the UART DMA Rx channel */
1642     if (huart->hdmarx != NULL)
1643     {
1644       if (HAL_DMAEx_Resume(huart->hdmarx) != HAL_OK)
1645       {
1646         /* Set error code to DMA */
1647         huart->ErrorCode = HAL_UART_ERROR_DMA;
1648 
1649         return HAL_ERROR;
1650       }
1651     }
1652   }
1653 
1654   return HAL_OK;
1655 }
1656 
1657 /**
1658   * @brief Stop the DMA Transfer.
1659   * @param huart UART handle.
1660   * @retval HAL status
1661   */
HAL_UART_DMAStop(UART_HandleTypeDef * huart)1662 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1663 {
1664   /* The Lock is not implemented on this API to allow the user application
1665      to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1666      HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1667      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1668      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1669      the stream and the corresponding call back is executed. */
1670 
1671   const HAL_UART_StateTypeDef gstate = huart->gState;
1672   const HAL_UART_StateTypeDef rxstate = huart->RxState;
1673 
1674   /* Stop UART DMA Tx request if ongoing */
1675   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1676       (gstate == HAL_UART_STATE_BUSY_TX))
1677   {
1678     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1679 
1680     /* Abort the UART DMA Tx channel */
1681     if (huart->hdmatx != NULL)
1682     {
1683       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1684       {
1685         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1686         {
1687           /* Set error code to DMA */
1688           huart->ErrorCode = HAL_UART_ERROR_DMA;
1689 
1690           return HAL_TIMEOUT;
1691         }
1692       }
1693     }
1694 
1695     UART_EndTxTransfer(huart);
1696   }
1697 
1698   /* Stop UART DMA Rx request if ongoing */
1699   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1700       (rxstate == HAL_UART_STATE_BUSY_RX))
1701   {
1702     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1703 
1704     /* Abort the UART DMA Rx channel */
1705     if (huart->hdmarx != NULL)
1706     {
1707       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1708       {
1709         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1710         {
1711           /* Set error code to DMA */
1712           huart->ErrorCode = HAL_UART_ERROR_DMA;
1713 
1714           return HAL_TIMEOUT;
1715         }
1716       }
1717     }
1718 
1719     UART_EndRxTransfer(huart);
1720   }
1721 
1722   return HAL_OK;
1723 }
1724 #endif /* HAL_DMA_MODULE_ENABLED */
1725 
1726 /**
1727   * @brief  Abort ongoing transfers (blocking mode).
1728   * @param  huart UART handle.
1729   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1730   *         This procedure performs following operations :
1731   *           - Disable UART Interrupts (Tx and Rx)
1732   *           - Disable the DMA transfer in the peripheral register (if enabled)
1733   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1734   *           - Set handle State to READY
1735   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1736   * @retval HAL status
1737   */
HAL_UART_Abort(UART_HandleTypeDef * huart)1738 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1739 {
1740   /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1741   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE |
1742                                           USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1743   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
1744 
1745   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1746   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1747   {
1748     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1749   }
1750 
1751 #if defined(HAL_DMA_MODULE_ENABLED)
1752   /* Abort the UART DMA Tx channel if enabled */
1753   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1754   {
1755     /* Disable the UART DMA Tx request if enabled */
1756     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1757 
1758     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1759     if (huart->hdmatx != NULL)
1760     {
1761       /* Set the UART DMA Abort callback to Null.
1762          No call back execution at end of DMA abort procedure */
1763       huart->hdmatx->XferAbortCallback = NULL;
1764 
1765       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1766       {
1767         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1768         {
1769           /* Set error code to DMA */
1770           huart->ErrorCode = HAL_UART_ERROR_DMA;
1771 
1772           return HAL_TIMEOUT;
1773         }
1774       }
1775     }
1776   }
1777 
1778   /* Abort the UART DMA Rx channel if enabled */
1779   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1780   {
1781     /* Disable the UART DMA Rx request if enabled */
1782     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1783 
1784     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1785     if (huart->hdmarx != NULL)
1786     {
1787       /* Set the UART DMA Abort callback to Null.
1788          No call back execution at end of DMA abort procedure */
1789       huart->hdmarx->XferAbortCallback = NULL;
1790 
1791       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1792       {
1793         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1794         {
1795           /* Set error code to DMA */
1796           huart->ErrorCode = HAL_UART_ERROR_DMA;
1797 
1798           return HAL_TIMEOUT;
1799         }
1800       }
1801     }
1802   }
1803 #endif /* HAL_DMA_MODULE_ENABLED */
1804 
1805   /* Reset Tx and Rx transfer counters */
1806   huart->TxXferCount = 0U;
1807   huart->RxXferCount = 0U;
1808 
1809   /* Clear the Error flags in the ICR register */
1810   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1811 
1812   /* Flush the whole TX FIFO (if needed) */
1813   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1814   {
1815     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1816   }
1817 
1818   /* Discard the received data */
1819   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1820 
1821   /* Restore huart->gState and huart->RxState to Ready */
1822   huart->gState  = HAL_UART_STATE_READY;
1823   huart->RxState = HAL_UART_STATE_READY;
1824   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1825 
1826   huart->ErrorCode = HAL_UART_ERROR_NONE;
1827 
1828   return HAL_OK;
1829 }
1830 
1831 /**
1832   * @brief  Abort ongoing Transmit transfer (blocking mode).
1833   * @param  huart UART handle.
1834   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1835   *         This procedure performs following operations :
1836   *           - Disable UART Interrupts (Tx)
1837   *           - Disable the DMA transfer in the peripheral register (if enabled)
1838   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1839   *           - Set handle State to READY
1840   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1841   * @retval HAL status
1842   */
HAL_UART_AbortTransmit(UART_HandleTypeDef * huart)1843 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1844 {
1845   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1846   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
1847   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1848 
1849 #if defined(HAL_DMA_MODULE_ENABLED)
1850   /* Abort the UART DMA Tx channel if enabled */
1851   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1852   {
1853     /* Disable the UART DMA Tx request if enabled */
1854     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1855 
1856     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1857     if (huart->hdmatx != NULL)
1858     {
1859       /* Set the UART DMA Abort callback to Null.
1860          No call back execution at end of DMA abort procedure */
1861       huart->hdmatx->XferAbortCallback = NULL;
1862 
1863       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1864       {
1865         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1866         {
1867           /* Set error code to DMA */
1868           huart->ErrorCode = HAL_UART_ERROR_DMA;
1869 
1870           return HAL_TIMEOUT;
1871         }
1872       }
1873     }
1874   }
1875 #endif /* HAL_DMA_MODULE_ENABLED */
1876 
1877   /* Reset Tx transfer counter */
1878   huart->TxXferCount = 0U;
1879 
1880   /* Flush the whole TX FIFO (if needed) */
1881   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1882   {
1883     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1884   }
1885 
1886   /* Restore huart->gState to Ready */
1887   huart->gState = HAL_UART_STATE_READY;
1888 
1889   return HAL_OK;
1890 }
1891 
1892 /**
1893   * @brief  Abort ongoing Receive transfer (blocking mode).
1894   * @param  huart UART handle.
1895   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1896   *         This procedure performs following operations :
1897   *           - Disable UART Interrupts (Rx)
1898   *           - Disable the DMA transfer in the peripheral register (if enabled)
1899   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1900   *           - Set handle State to READY
1901   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1902   * @retval HAL status
1903   */
HAL_UART_AbortReceive(UART_HandleTypeDef * huart)1904 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1905 {
1906   /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
1907   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
1908   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
1909 
1910   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1911   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1912   {
1913     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1914   }
1915 
1916 #if defined(HAL_DMA_MODULE_ENABLED)
1917   /* Abort the UART DMA Rx channel if enabled */
1918   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1919   {
1920     /* Disable the UART DMA Rx request if enabled */
1921     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1922 
1923     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1924     if (huart->hdmarx != NULL)
1925     {
1926       /* Set the UART DMA Abort callback to Null.
1927          No call back execution at end of DMA abort procedure */
1928       huart->hdmarx->XferAbortCallback = NULL;
1929 
1930       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1931       {
1932         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1933         {
1934           /* Set error code to DMA */
1935           huart->ErrorCode = HAL_UART_ERROR_DMA;
1936 
1937           return HAL_TIMEOUT;
1938         }
1939       }
1940     }
1941   }
1942 #endif /* HAL_DMA_MODULE_ENABLED */
1943 
1944   /* Reset Rx transfer counter */
1945   huart->RxXferCount = 0U;
1946 
1947   /* Clear the Error flags in the ICR register */
1948   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1949 
1950   /* Discard the received data */
1951   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1952 
1953   /* Restore huart->RxState to Ready */
1954   huart->RxState = HAL_UART_STATE_READY;
1955   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1956 
1957   return HAL_OK;
1958 }
1959 
1960 /**
1961   * @brief  Abort ongoing transfers (Interrupt mode).
1962   * @param  huart UART handle.
1963   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1964   *         This procedure performs following operations :
1965   *           - Disable UART Interrupts (Tx and Rx)
1966   *           - Disable the DMA transfer in the peripheral register (if enabled)
1967   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1968   *           - Set handle State to READY
1969   *           - At abort completion, call user abort complete callback
1970   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1971   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1972   * @retval HAL status
1973   */
HAL_UART_Abort_IT(UART_HandleTypeDef * huart)1974 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1975 {
1976   uint32_t abortcplt = 1U;
1977 
1978   /* Disable interrupts */
1979   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE |
1980                                           USART_CR1_TXEIE_TXFNFIE));
1981   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1982 
1983   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1984   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1985   {
1986     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1987   }
1988 
1989 #if defined(HAL_DMA_MODULE_ENABLED)
1990   /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1991      before any call to DMA Abort functions */
1992   /* DMA Tx Handle is valid */
1993   if (huart->hdmatx != NULL)
1994   {
1995     /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1996        Otherwise, set it to NULL */
1997     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1998     {
1999       huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
2000     }
2001     else
2002     {
2003       huart->hdmatx->XferAbortCallback = NULL;
2004     }
2005   }
2006   /* DMA Rx Handle is valid */
2007   if (huart->hdmarx != NULL)
2008   {
2009     /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
2010        Otherwise, set it to NULL */
2011     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2012     {
2013       huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
2014     }
2015     else
2016     {
2017       huart->hdmarx->XferAbortCallback = NULL;
2018     }
2019   }
2020 
2021   /* Abort the UART DMA Tx channel if enabled */
2022   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2023   {
2024     /* Disable DMA Tx at UART level */
2025     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2026 
2027     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
2028     if (huart->hdmatx != NULL)
2029     {
2030       /* UART Tx DMA Abort callback has already been initialised :
2031          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2032 
2033       /* Abort DMA TX */
2034       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2035       {
2036         huart->hdmatx->XferAbortCallback = NULL;
2037       }
2038       else
2039       {
2040         abortcplt = 0U;
2041       }
2042     }
2043   }
2044 
2045   /* Abort the UART DMA Rx channel if enabled */
2046   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2047   {
2048     /* Disable the UART DMA Rx request if enabled */
2049     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2050 
2051     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2052     if (huart->hdmarx != NULL)
2053     {
2054       /* UART Rx DMA Abort callback has already been initialised :
2055          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2056 
2057       /* Abort DMA RX */
2058       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2059       {
2060         huart->hdmarx->XferAbortCallback = NULL;
2061         abortcplt = 1U;
2062       }
2063       else
2064       {
2065         abortcplt = 0U;
2066       }
2067     }
2068   }
2069 #endif /* HAL_DMA_MODULE_ENABLED */
2070 
2071   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
2072   if (abortcplt == 1U)
2073   {
2074     /* Reset Tx and Rx transfer counters */
2075     huart->TxXferCount = 0U;
2076     huart->RxXferCount = 0U;
2077 
2078     /* Clear ISR function pointers */
2079     huart->RxISR = NULL;
2080     huart->TxISR = NULL;
2081 
2082     /* Reset errorCode */
2083     huart->ErrorCode = HAL_UART_ERROR_NONE;
2084 
2085     /* Clear the Error flags in the ICR register */
2086     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2087 
2088     /* Flush the whole TX FIFO (if needed) */
2089     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2090     {
2091       __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2092     }
2093 
2094     /* Discard the received data */
2095     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2096 
2097     /* Restore huart->gState and huart->RxState to Ready */
2098     huart->gState  = HAL_UART_STATE_READY;
2099     huart->RxState = HAL_UART_STATE_READY;
2100     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2101 
2102     /* As no DMA to be aborted, call directly user Abort complete callback */
2103 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2104     /* Call registered Abort complete callback */
2105     huart->AbortCpltCallback(huart);
2106 #else
2107     /* Call legacy weak Abort complete callback */
2108     HAL_UART_AbortCpltCallback(huart);
2109 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2110   }
2111 
2112   return HAL_OK;
2113 }
2114 
2115 /**
2116   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
2117   * @param  huart UART handle.
2118   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
2119   *         This procedure performs following operations :
2120   *           - Disable UART Interrupts (Tx)
2121   *           - Disable the DMA transfer in the peripheral register (if enabled)
2122   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2123   *           - Set handle State to READY
2124   *           - At abort completion, call user abort complete callback
2125   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
2126   *         considered as completed only when user abort complete callback is executed (not when exiting function).
2127   * @retval HAL status
2128   */
HAL_UART_AbortTransmit_IT(UART_HandleTypeDef * huart)2129 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
2130 {
2131   /* Disable interrupts */
2132   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
2133   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
2134 
2135 #if defined(HAL_DMA_MODULE_ENABLED)
2136   /* Abort the UART DMA Tx channel if enabled */
2137   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2138   {
2139     /* Disable the UART DMA Tx request if enabled */
2140     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2141 
2142     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
2143     if (huart->hdmatx != NULL)
2144     {
2145       /* Set the UART DMA Abort callback :
2146          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2147       huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
2148 
2149       /* Abort DMA TX */
2150       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2151       {
2152         /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2153         huart->hdmatx->XferAbortCallback(huart->hdmatx);
2154       }
2155     }
2156     else
2157     {
2158       /* Reset Tx transfer counter */
2159       huart->TxXferCount = 0U;
2160 
2161       /* Clear TxISR function pointers */
2162       huart->TxISR = NULL;
2163 
2164       /* Restore huart->gState to Ready */
2165       huart->gState = HAL_UART_STATE_READY;
2166 
2167       /* As no DMA to be aborted, call directly user Abort complete callback */
2168 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2169       /* Call registered Abort Transmit Complete Callback */
2170       huart->AbortTransmitCpltCallback(huart);
2171 #else
2172       /* Call legacy weak Abort Transmit Complete Callback */
2173       HAL_UART_AbortTransmitCpltCallback(huart);
2174 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2175     }
2176   }
2177   else
2178 #endif /* HAL_DMA_MODULE_ENABLED */
2179   {
2180     /* Reset Tx transfer counter */
2181     huart->TxXferCount = 0U;
2182 
2183     /* Clear TxISR function pointers */
2184     huart->TxISR = NULL;
2185 
2186     /* Flush the whole TX FIFO (if needed) */
2187     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2188     {
2189       __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2190     }
2191 
2192     /* Restore huart->gState to Ready */
2193     huart->gState = HAL_UART_STATE_READY;
2194 
2195     /* As no DMA to be aborted, call directly user Abort complete callback */
2196 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2197     /* Call registered Abort Transmit Complete Callback */
2198     huart->AbortTransmitCpltCallback(huart);
2199 #else
2200     /* Call legacy weak Abort Transmit Complete Callback */
2201     HAL_UART_AbortTransmitCpltCallback(huart);
2202 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2203   }
2204 
2205   return HAL_OK;
2206 }
2207 
2208 /**
2209   * @brief  Abort ongoing Receive transfer (Interrupt mode).
2210   * @param  huart UART handle.
2211   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2212   *         This procedure performs following operations :
2213   *           - Disable UART Interrupts (Rx)
2214   *           - Disable the DMA transfer in the peripheral register (if enabled)
2215   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2216   *           - Set handle State to READY
2217   *           - At abort completion, call user abort complete callback
2218   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
2219   *         considered as completed only when user abort complete callback is executed (not when exiting function).
2220   * @retval HAL status
2221   */
HAL_UART_AbortReceive_IT(UART_HandleTypeDef * huart)2222 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
2223 {
2224   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2225   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
2226   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2227 
2228   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2229   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2230   {
2231     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2232   }
2233 
2234 #if defined(HAL_DMA_MODULE_ENABLED)
2235   /* Abort the UART DMA Rx channel if enabled */
2236   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2237   {
2238     /* Disable the UART DMA Rx request if enabled */
2239     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2240 
2241     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2242     if (huart->hdmarx != NULL)
2243     {
2244       /* Set the UART DMA Abort callback :
2245          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2246       huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
2247 
2248       /* Abort DMA RX */
2249       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2250       {
2251         /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2252         huart->hdmarx->XferAbortCallback(huart->hdmarx);
2253       }
2254     }
2255     else
2256     {
2257       /* Reset Rx transfer counter */
2258       huart->RxXferCount = 0U;
2259 
2260       /* Clear RxISR function pointer */
2261       huart->pRxBuffPtr = NULL;
2262 
2263       /* Clear the Error flags in the ICR register */
2264       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2265 
2266       /* Discard the received data */
2267       __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2268 
2269       /* Restore huart->RxState to Ready */
2270       huart->RxState = HAL_UART_STATE_READY;
2271       huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2272 
2273       /* As no DMA to be aborted, call directly user Abort complete callback */
2274 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2275       /* Call registered Abort Receive Complete Callback */
2276       huart->AbortReceiveCpltCallback(huart);
2277 #else
2278       /* Call legacy weak Abort Receive Complete Callback */
2279       HAL_UART_AbortReceiveCpltCallback(huart);
2280 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2281     }
2282   }
2283   else
2284 #endif /* HAL_DMA_MODULE_ENABLED */
2285   {
2286     /* Reset Rx transfer counter */
2287     huart->RxXferCount = 0U;
2288 
2289     /* Clear RxISR function pointer */
2290     huart->pRxBuffPtr = NULL;
2291 
2292     /* Clear the Error flags in the ICR register */
2293     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2294 
2295     /* Restore huart->RxState to Ready */
2296     huart->RxState = HAL_UART_STATE_READY;
2297     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2298 
2299     /* As no DMA to be aborted, call directly user Abort complete callback */
2300 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2301     /* Call registered Abort Receive Complete Callback */
2302     huart->AbortReceiveCpltCallback(huart);
2303 #else
2304     /* Call legacy weak Abort Receive Complete Callback */
2305     HAL_UART_AbortReceiveCpltCallback(huart);
2306 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2307   }
2308 
2309   return HAL_OK;
2310 }
2311 
2312 /**
2313   * @brief Handle UART interrupt request.
2314   * @param huart UART handle.
2315   * @retval None
2316   */
HAL_UART_IRQHandler(UART_HandleTypeDef * huart)2317 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
2318 {
2319   uint32_t isrflags   = READ_REG(huart->Instance->ISR);
2320   uint32_t cr1its     = READ_REG(huart->Instance->CR1);
2321   uint32_t cr3its     = READ_REG(huart->Instance->CR3);
2322 
2323   uint32_t errorflags;
2324   uint32_t errorcode;
2325 
2326   /* If no error occurs */
2327   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
2328   if (errorflags == 0U)
2329   {
2330     /* UART in mode Receiver ---------------------------------------------------*/
2331     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2332         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2333             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2334     {
2335       if (huart->RxISR != NULL)
2336       {
2337         huart->RxISR(huart);
2338       }
2339       return;
2340     }
2341   }
2342 
2343   /* If some errors occur */
2344   if ((errorflags != 0U)
2345       && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2346            || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U))))
2347   {
2348     /* UART parity error interrupt occurred -------------------------------------*/
2349     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2350     {
2351       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
2352 
2353       huart->ErrorCode |= HAL_UART_ERROR_PE;
2354     }
2355 
2356     /* UART frame error interrupt occurred --------------------------------------*/
2357     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2358     {
2359       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
2360 
2361       huart->ErrorCode |= HAL_UART_ERROR_FE;
2362     }
2363 
2364     /* UART noise error interrupt occurred --------------------------------------*/
2365     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2366     {
2367       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
2368 
2369       huart->ErrorCode |= HAL_UART_ERROR_NE;
2370     }
2371 
2372     /* UART Over-Run interrupt occurred -----------------------------------------*/
2373     if (((isrflags & USART_ISR_ORE) != 0U)
2374         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2375             ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2376     {
2377       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
2378 
2379       huart->ErrorCode |= HAL_UART_ERROR_ORE;
2380     }
2381 
2382     /* UART Receiver Timeout interrupt occurred ---------------------------------*/
2383     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
2384     {
2385       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
2386 
2387       huart->ErrorCode |= HAL_UART_ERROR_RTO;
2388     }
2389 
2390     /* Call UART Error Call back function if need be ----------------------------*/
2391     if (huart->ErrorCode != HAL_UART_ERROR_NONE)
2392     {
2393       /* UART in mode Receiver --------------------------------------------------*/
2394       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2395           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2396               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2397       {
2398         if (huart->RxISR != NULL)
2399         {
2400           huart->RxISR(huart);
2401         }
2402       }
2403 
2404       /* If Error is to be considered as blocking :
2405           - Receiver Timeout error in Reception
2406           - Overrun error in Reception
2407           - any error occurs in DMA mode reception
2408       */
2409       errorcode = huart->ErrorCode;
2410       if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
2411           ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
2412       {
2413         /* Blocking error : transfer is aborted
2414            Set the UART state ready to be able to start again the process,
2415            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2416         UART_EndRxTransfer(huart);
2417 
2418 #if defined(HAL_DMA_MODULE_ENABLED)
2419         /* Abort the UART DMA Rx channel if enabled */
2420         if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2421         {
2422           /* Disable the UART DMA Rx request if enabled */
2423           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2424 
2425           /* Abort the UART DMA Rx channel */
2426           if (huart->hdmarx != NULL)
2427           {
2428             /* Set the UART DMA Abort callback :
2429                will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2430             huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
2431 
2432             /* Abort DMA RX */
2433             if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2434             {
2435               /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2436               huart->hdmarx->XferAbortCallback(huart->hdmarx);
2437             }
2438           }
2439           else
2440           {
2441             /* Call user error callback */
2442 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2443             /*Call registered error callback*/
2444             huart->ErrorCallback(huart);
2445 #else
2446             /*Call legacy weak error callback*/
2447             HAL_UART_ErrorCallback(huart);
2448 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2449 
2450           }
2451         }
2452         else
2453 #endif /* HAL_DMA_MODULE_ENABLED */
2454         {
2455           /* Call user error callback */
2456 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2457           /*Call registered error callback*/
2458           huart->ErrorCallback(huart);
2459 #else
2460           /*Call legacy weak error callback*/
2461           HAL_UART_ErrorCallback(huart);
2462 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2463         }
2464       }
2465       else
2466       {
2467         /* Non Blocking error : transfer could go on.
2468            Error is notified to user through user error callback */
2469 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2470         /*Call registered error callback*/
2471         huart->ErrorCallback(huart);
2472 #else
2473         /*Call legacy weak error callback*/
2474         HAL_UART_ErrorCallback(huart);
2475 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2476         huart->ErrorCode = HAL_UART_ERROR_NONE;
2477       }
2478     }
2479     return;
2480 
2481   } /* End if some error occurs */
2482 
2483   /* Check current reception Mode :
2484      If Reception till IDLE event has been selected : */
2485   if ((huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2486       && ((isrflags & USART_ISR_IDLE) != 0U)
2487       && ((cr1its & USART_ISR_IDLE) != 0U))
2488   {
2489     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
2490 
2491 #if defined(HAL_DMA_MODULE_ENABLED)
2492     /* Check if DMA mode is enabled in UART */
2493     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2494     {
2495       /* DMA mode enabled */
2496       /* Check received length : If all expected data are received, do nothing,
2497          (DMA cplt callback will be called).
2498          Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2499       uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx);
2500       if ((nb_remaining_rx_data > 0U)
2501           && (nb_remaining_rx_data < huart->RxXferSize))
2502       {
2503         /* Reception is not complete */
2504         huart->RxXferCount = nb_remaining_rx_data;
2505 
2506         /* In Normal mode, end DMA xfer and HAL UART Rx process*/
2507         if (huart->hdmarx->Mode != DMA_LINKEDLIST_CIRCULAR)
2508         {
2509           /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2510           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2511           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2512 
2513           /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2514              in the UART CR3 register */
2515           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2516 
2517           /* At end of Rx process, restore huart->RxState to Ready */
2518           huart->RxState = HAL_UART_STATE_READY;
2519           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2520 
2521           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2522 
2523           /* Last bytes received, so no need as the abort is immediate */
2524           (void)HAL_DMA_Abort(huart->hdmarx);
2525         }
2526 
2527         /* Initialize type of RxEvent that correspond to RxEvent callback execution;
2528            In this case, Rx Event type is Idle Event */
2529         huart->RxEventType = HAL_UART_RXEVENT_IDLE;
2530 
2531 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2532         /*Call registered Rx Event callback*/
2533         huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2534 #else
2535         /*Call legacy weak Rx Event callback*/
2536         HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2537 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2538       }
2539       return;
2540     }
2541     else
2542     {
2543 #endif /* HAL_DMA_MODULE_ENABLED */
2544       /* DMA mode not enabled */
2545       /* Check received length : If all expected data are received, do nothing.
2546          Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2547       uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount;
2548       if ((huart->RxXferCount > 0U)
2549           && (nb_rx_data > 0U))
2550       {
2551         /* Disable the UART Parity Error Interrupt and RXNE interrupts */
2552         ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2553 
2554         /* Disable the UART Error Interrupt:(Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
2555         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2556 
2557         /* Rx process is completed, restore huart->RxState to Ready */
2558         huart->RxState = HAL_UART_STATE_READY;
2559         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2560 
2561         /* Clear RxISR function pointer */
2562         huart->RxISR = NULL;
2563 
2564         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2565 
2566         /* Initialize type of RxEvent that correspond to RxEvent callback execution;
2567            In this case, Rx Event type is Idle Event */
2568         huart->RxEventType = HAL_UART_RXEVENT_IDLE;
2569 
2570 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2571         /*Call registered Rx complete callback*/
2572         huart->RxEventCallback(huart, nb_rx_data);
2573 #else
2574         /*Call legacy weak Rx Event callback*/
2575         HAL_UARTEx_RxEventCallback(huart, nb_rx_data);
2576 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2577       }
2578       return;
2579 #if defined(HAL_DMA_MODULE_ENABLED)
2580     }
2581 #endif /* HAL_DMA_MODULE_ENABLED */
2582   }
2583 
2584   /* UART in mode Transmitter ------------------------------------------------*/
2585   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2586       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2587           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2588   {
2589     if (huart->TxISR != NULL)
2590     {
2591       huart->TxISR(huart);
2592     }
2593     return;
2594   }
2595 
2596   /* UART in mode Transmitter (transmission end) -----------------------------*/
2597   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2598   {
2599     UART_EndTransmit_IT(huart);
2600     return;
2601   }
2602 
2603   /* UART TX Fifo Empty occurred ----------------------------------------------*/
2604   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2605   {
2606 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2607     /* Call registered Tx Fifo Empty Callback */
2608     huart->TxFifoEmptyCallback(huart);
2609 #else
2610     /* Call legacy weak Tx Fifo Empty Callback */
2611     HAL_UARTEx_TxFifoEmptyCallback(huart);
2612 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2613     return;
2614   }
2615 
2616   /* UART RX Fifo Full occurred ----------------------------------------------*/
2617   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2618   {
2619 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2620     /* Call registered Rx Fifo Full Callback */
2621     huart->RxFifoFullCallback(huart);
2622 #else
2623     /* Call legacy weak Rx Fifo Full Callback */
2624     HAL_UARTEx_RxFifoFullCallback(huart);
2625 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2626     return;
2627   }
2628 }
2629 
2630 /**
2631   * @brief Tx Transfer completed callback.
2632   * @param huart UART handle.
2633   * @retval None
2634   */
HAL_UART_TxCpltCallback(UART_HandleTypeDef * huart)2635 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
2636 {
2637   /* Prevent unused argument(s) compilation warning */
2638   UNUSED(huart);
2639 
2640   /* NOTE : This function should not be modified, when the callback is needed,
2641             the HAL_UART_TxCpltCallback can be implemented in the user file.
2642    */
2643 }
2644 
2645 /**
2646   * @brief  Tx Half Transfer completed callback.
2647   * @param  huart UART handle.
2648   * @retval None
2649   */
HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef * huart)2650 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
2651 {
2652   /* Prevent unused argument(s) compilation warning */
2653   UNUSED(huart);
2654 
2655   /* NOTE: This function should not be modified, when the callback is needed,
2656            the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2657    */
2658 }
2659 
2660 /**
2661   * @brief  Rx Transfer completed callback.
2662   * @param  huart UART handle.
2663   * @retval None
2664   */
HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart)2665 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
2666 {
2667   /* Prevent unused argument(s) compilation warning */
2668   UNUSED(huart);
2669 
2670   /* NOTE : This function should not be modified, when the callback is needed,
2671             the HAL_UART_RxCpltCallback can be implemented in the user file.
2672    */
2673 }
2674 
2675 /**
2676   * @brief  Rx Half Transfer completed callback.
2677   * @param  huart UART handle.
2678   * @retval None
2679   */
HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef * huart)2680 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
2681 {
2682   /* Prevent unused argument(s) compilation warning */
2683   UNUSED(huart);
2684 
2685   /* NOTE: This function should not be modified, when the callback is needed,
2686            the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2687    */
2688 }
2689 
2690 /**
2691   * @brief  UART error callback.
2692   * @param  huart UART handle.
2693   * @retval None
2694   */
HAL_UART_ErrorCallback(UART_HandleTypeDef * huart)2695 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
2696 {
2697   /* Prevent unused argument(s) compilation warning */
2698   UNUSED(huart);
2699 
2700   /* NOTE : This function should not be modified, when the callback is needed,
2701             the HAL_UART_ErrorCallback can be implemented in the user file.
2702    */
2703 }
2704 
2705 /**
2706   * @brief  UART Abort Complete callback.
2707   * @param  huart UART handle.
2708   * @retval None
2709   */
HAL_UART_AbortCpltCallback(UART_HandleTypeDef * huart)2710 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
2711 {
2712   /* Prevent unused argument(s) compilation warning */
2713   UNUSED(huart);
2714 
2715   /* NOTE : This function should not be modified, when the callback is needed,
2716             the HAL_UART_AbortCpltCallback can be implemented in the user file.
2717    */
2718 }
2719 
2720 /**
2721   * @brief  UART Abort Complete callback.
2722   * @param  huart UART handle.
2723   * @retval None
2724   */
HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef * huart)2725 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
2726 {
2727   /* Prevent unused argument(s) compilation warning */
2728   UNUSED(huart);
2729 
2730   /* NOTE : This function should not be modified, when the callback is needed,
2731             the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2732    */
2733 }
2734 
2735 /**
2736   * @brief  UART Abort Receive Complete callback.
2737   * @param  huart UART handle.
2738   * @retval None
2739   */
HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef * huart)2740 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
2741 {
2742   /* Prevent unused argument(s) compilation warning */
2743   UNUSED(huart);
2744 
2745   /* NOTE : This function should not be modified, when the callback is needed,
2746             the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2747    */
2748 }
2749 
2750 /**
2751   * @brief  Reception Event Callback (Rx event notification called after use of advanced reception service).
2752   * @param  huart UART handle
2753   * @param  Size  Number of data available in application reception buffer (indicates a position in
2754   *               reception buffer until which, data are available)
2755   * @retval None
2756   */
HAL_UARTEx_RxEventCallback(UART_HandleTypeDef * huart,uint16_t Size)2757 __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
2758 {
2759   /* Prevent unused argument(s) compilation warning */
2760   UNUSED(huart);
2761   UNUSED(Size);
2762 
2763   /* NOTE : This function should not be modified, when the callback is needed,
2764             the HAL_UARTEx_RxEventCallback can be implemented in the user file.
2765    */
2766 }
2767 
2768 /**
2769   * @}
2770   */
2771 
2772 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2773   *  @brief   UART control functions
2774   *
2775 @verbatim
2776  ===============================================================================
2777                       ##### Peripheral Control functions #####
2778  ===============================================================================
2779     [..]
2780     This subsection provides a set of functions allowing to control the UART.
2781      (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
2782      (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
2783      (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
2784      (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2785      (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2786      (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2787      (+) UART_SetConfig() API configures the UART peripheral
2788      (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2789      (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2790      (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2791      (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2792      (+) HAL_LIN_SendBreak() API transmits the break characters
2793 @endverbatim
2794   * @{
2795   */
2796 
2797 /**
2798   * @brief  Update on the fly the receiver timeout value in RTOR register.
2799   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2800   *                    the configuration information for the specified UART module.
2801   * @param  TimeoutValue receiver timeout value in number of baud blocks. The timeout
2802   *                     value must be less or equal to 0x0FFFFFFFF.
2803   * @retval None
2804   */
HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef * huart,uint32_t TimeoutValue)2805 void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
2806 {
2807   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2808   {
2809     assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
2810     MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
2811   }
2812 }
2813 
2814 /**
2815   * @brief  Enable the UART receiver timeout feature.
2816   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2817   *                    the configuration information for the specified UART module.
2818   * @retval HAL status
2819   */
HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef * huart)2820 HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
2821 {
2822   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2823   {
2824     if (huart->gState == HAL_UART_STATE_READY)
2825     {
2826       /* Process Locked */
2827       __HAL_LOCK(huart);
2828 
2829       huart->gState = HAL_UART_STATE_BUSY;
2830 
2831       /* Set the USART RTOEN bit */
2832       SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2833 
2834       huart->gState = HAL_UART_STATE_READY;
2835 
2836       /* Process Unlocked */
2837       __HAL_UNLOCK(huart);
2838 
2839       return HAL_OK;
2840     }
2841     else
2842     {
2843       return HAL_BUSY;
2844     }
2845   }
2846   else
2847   {
2848     return HAL_ERROR;
2849   }
2850 }
2851 
2852 /**
2853   * @brief  Disable the UART receiver timeout feature.
2854   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2855   *                    the configuration information for the specified UART module.
2856   * @retval HAL status
2857   */
HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef * huart)2858 HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
2859 {
2860   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2861   {
2862     if (huart->gState == HAL_UART_STATE_READY)
2863     {
2864       /* Process Locked */
2865       __HAL_LOCK(huart);
2866 
2867       huart->gState = HAL_UART_STATE_BUSY;
2868 
2869       /* Clear the USART RTOEN bit */
2870       CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2871 
2872       huart->gState = HAL_UART_STATE_READY;
2873 
2874       /* Process Unlocked */
2875       __HAL_UNLOCK(huart);
2876 
2877       return HAL_OK;
2878     }
2879     else
2880     {
2881       return HAL_BUSY;
2882     }
2883   }
2884   else
2885   {
2886     return HAL_ERROR;
2887   }
2888 }
2889 
2890 /**
2891   * @brief  Enable UART in mute mode (does not mean UART enters mute mode;
2892   *         to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2893   * @param  huart UART handle.
2894   * @retval HAL status
2895   */
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef * huart)2896 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
2897 {
2898   __HAL_LOCK(huart);
2899 
2900   huart->gState = HAL_UART_STATE_BUSY;
2901 
2902   /* Enable USART mute mode by setting the MME bit in the CR1 register */
2903   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_MME);
2904 
2905   huart->gState = HAL_UART_STATE_READY;
2906 
2907   return (UART_CheckIdleState(huart));
2908 }
2909 
2910 /**
2911   * @brief  Disable UART mute mode (does not mean the UART actually exits mute mode
2912   *         as it may not have been in mute mode at this very moment).
2913   * @param  huart UART handle.
2914   * @retval HAL status
2915   */
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef * huart)2916 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
2917 {
2918   __HAL_LOCK(huart);
2919 
2920   huart->gState = HAL_UART_STATE_BUSY;
2921 
2922   /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2923   ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
2924 
2925   huart->gState = HAL_UART_STATE_READY;
2926 
2927   return (UART_CheckIdleState(huart));
2928 }
2929 
2930 /**
2931   * @brief Enter UART mute mode (means UART actually enters mute mode).
2932   * @note  To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2933   * @param huart UART handle.
2934   * @retval None
2935   */
HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef * huart)2936 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
2937 {
2938   __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
2939 }
2940 
2941 /**
2942   * @brief  Enable the UART transmitter and disable the UART receiver.
2943   * @param  huart UART handle.
2944   * @retval HAL status
2945   */
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef * huart)2946 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2947 {
2948   __HAL_LOCK(huart);
2949   huart->gState = HAL_UART_STATE_BUSY;
2950 
2951   /* Clear TE and RE bits */
2952   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2953 
2954   /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2955   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2956 
2957   huart->gState = HAL_UART_STATE_READY;
2958 
2959   __HAL_UNLOCK(huart);
2960 
2961   return HAL_OK;
2962 }
2963 
2964 /**
2965   * @brief  Enable the UART receiver and disable the UART transmitter.
2966   * @param  huart UART handle.
2967   * @retval HAL status.
2968   */
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef * huart)2969 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2970 {
2971   __HAL_LOCK(huart);
2972   huart->gState = HAL_UART_STATE_BUSY;
2973 
2974   /* Clear TE and RE bits */
2975   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2976 
2977   /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2978   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2979 
2980   huart->gState = HAL_UART_STATE_READY;
2981 
2982   __HAL_UNLOCK(huart);
2983 
2984   return HAL_OK;
2985 }
2986 
2987 
2988 /**
2989   * @brief  Transmit break characters.
2990   * @param  huart UART handle.
2991   * @retval HAL status
2992   */
HAL_LIN_SendBreak(UART_HandleTypeDef * huart)2993 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2994 {
2995   /* Check the parameters */
2996   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
2997 
2998   __HAL_LOCK(huart);
2999 
3000   huart->gState = HAL_UART_STATE_BUSY;
3001 
3002   /* Send break characters */
3003   __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
3004 
3005   huart->gState = HAL_UART_STATE_READY;
3006 
3007   __HAL_UNLOCK(huart);
3008 
3009   return HAL_OK;
3010 }
3011 
3012 /**
3013   * @}
3014   */
3015 
3016 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
3017   *  @brief   UART Peripheral State functions
3018   *
3019 @verbatim
3020   ==============================================================================
3021             ##### Peripheral State and Error functions #####
3022   ==============================================================================
3023     [..]
3024     This subsection provides functions allowing to :
3025       (+) Return the UART handle state.
3026       (+) Return the UART handle error code
3027 
3028 @endverbatim
3029   * @{
3030   */
3031 
3032 /**
3033   * @brief Return the UART handle state.
3034   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
3035   *               the configuration information for the specified UART.
3036   * @retval HAL state
3037   */
HAL_UART_GetState(const UART_HandleTypeDef * huart)3038 HAL_UART_StateTypeDef HAL_UART_GetState(const UART_HandleTypeDef *huart)
3039 {
3040   uint32_t temp1;
3041   uint32_t temp2;
3042   temp1 = huart->gState;
3043   temp2 = huart->RxState;
3044 
3045   return (HAL_UART_StateTypeDef)(temp1 | temp2);
3046 }
3047 
3048 /**
3049   * @brief  Return the UART handle error code.
3050   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
3051   *               the configuration information for the specified UART.
3052   * @retval UART Error Code
3053   */
HAL_UART_GetError(const UART_HandleTypeDef * huart)3054 uint32_t HAL_UART_GetError(const UART_HandleTypeDef *huart)
3055 {
3056   return huart->ErrorCode;
3057 }
3058 /**
3059   * @}
3060   */
3061 
3062 /**
3063   * @}
3064   */
3065 
3066 /** @defgroup UART_Private_Functions UART Private Functions
3067   * @{
3068   */
3069 
3070 /**
3071   * @brief  Initialize the callbacks to their default values.
3072   * @param  huart UART handle.
3073   * @retval none
3074   */
3075 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
UART_InitCallbacksToDefault(UART_HandleTypeDef * huart)3076 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
3077 {
3078   /* Init the UART Callback settings */
3079   huart->TxHalfCpltCallback        = HAL_UART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
3080   huart->TxCpltCallback            = HAL_UART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
3081   huart->RxHalfCpltCallback        = HAL_UART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
3082   huart->RxCpltCallback            = HAL_UART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
3083   huart->ErrorCallback             = HAL_UART_ErrorCallback;             /* Legacy weak ErrorCallback             */
3084   huart->AbortCpltCallback         = HAL_UART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
3085   huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
3086   huart->AbortReceiveCpltCallback  = HAL_UART_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
3087   huart->RxFifoFullCallback        = HAL_UARTEx_RxFifoFullCallback;      /* Legacy weak RxFifoFullCallback        */
3088   huart->TxFifoEmptyCallback       = HAL_UARTEx_TxFifoEmptyCallback;     /* Legacy weak TxFifoEmptyCallback       */
3089   huart->RxEventCallback           = HAL_UARTEx_RxEventCallback;         /* Legacy weak RxEventCallback           */
3090 
3091 }
3092 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3093 
3094 /**
3095   * @brief Configure the UART peripheral.
3096   * @param huart UART handle.
3097   * @retval HAL status
3098   */
UART_SetConfig(UART_HandleTypeDef * huart)3099 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
3100 {
3101   uint32_t tmpreg;
3102   uint16_t brrtemp;
3103   UART_ClockSourceTypeDef clocksource;
3104   uint32_t usartdiv;
3105   HAL_StatusTypeDef ret               = HAL_OK;
3106   uint32_t lpuart_ker_ck_pres;
3107   uint32_t pclk;
3108 
3109   /* Check the parameters */
3110   assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
3111   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
3112   if (UART_INSTANCE_LOWPOWER(huart))
3113   {
3114     assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
3115   }
3116   else
3117   {
3118     assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
3119     assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
3120   }
3121 
3122   assert_param(IS_UART_PARITY(huart->Init.Parity));
3123   assert_param(IS_UART_MODE(huart->Init.Mode));
3124   assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
3125   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
3126   assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
3127 
3128   /*-------------------------- USART CR1 Configuration -----------------------*/
3129   /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
3130   *  the UART Word Length, Parity, Mode and oversampling:
3131   *  set the M bits according to huart->Init.WordLength value
3132   *  set PCE and PS bits according to huart->Init.Parity value
3133   *  set TE and RE bits according to huart->Init.Mode value
3134   *  set OVER8 bit according to huart->Init.OverSampling value */
3135   tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
3136   MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
3137 
3138   /*-------------------------- USART CR2 Configuration -----------------------*/
3139   /* Configure the UART Stop Bits: Set STOP[13:12] bits according
3140   * to huart->Init.StopBits value */
3141   MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
3142 
3143   /*-------------------------- USART CR3 Configuration -----------------------*/
3144   /* Configure
3145   * - UART HardWare Flow Control: set CTSE and RTSE bits according
3146   *   to huart->Init.HwFlowCtl value
3147   * - one-bit sampling method versus three samples' majority rule according
3148   *   to huart->Init.OneBitSampling (not applicable to LPUART) */
3149   tmpreg = (uint32_t)huart->Init.HwFlowCtl;
3150 
3151   if (!(UART_INSTANCE_LOWPOWER(huart)))
3152   {
3153     tmpreg |= huart->Init.OneBitSampling;
3154   }
3155   MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
3156 
3157   /*-------------------------- USART PRESC Configuration -----------------------*/
3158   /* Configure
3159   * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
3160   MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
3161 
3162   /*-------------------------- USART BRR Configuration -----------------------*/
3163   UART_GETCLOCKSOURCE(huart, clocksource);
3164 
3165   /* Check LPUART instance */
3166   if (UART_INSTANCE_LOWPOWER(huart))
3167   {
3168     /* Retrieve frequency clock */
3169     switch (clocksource)
3170     {
3171       case UART_CLOCKSOURCE_PCLK7:
3172         pclk = HAL_RCC_GetPCLK7Freq();
3173         break;
3174       case UART_CLOCKSOURCE_HSI:
3175         pclk = (uint32_t) HSI_VALUE;
3176         break;
3177       case UART_CLOCKSOURCE_SYSCLK:
3178         pclk = HAL_RCC_GetSysClockFreq();
3179         break;
3180       case UART_CLOCKSOURCE_LSE:
3181         pclk = (uint32_t) LSE_VALUE;
3182         break;
3183       default:
3184         pclk = 0U;
3185         ret = HAL_ERROR;
3186         break;
3187     }
3188 
3189     /* If proper clock source reported */
3190     if (pclk != 0U)
3191     {
3192       /* Compute clock after Prescaler */
3193       lpuart_ker_ck_pres = (pclk / UARTPrescTable[huart->Init.ClockPrescaler]);
3194 
3195       /* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
3196       if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
3197           (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
3198       {
3199         ret = HAL_ERROR;
3200       }
3201       else
3202       {
3203         /* Check computed UsartDiv value is in allocated range
3204            (it is forbidden to write values lower than 0x300 in the LPUART_BRR register) */
3205         usartdiv = (uint32_t)(UART_DIV_LPUART(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3206         if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
3207         {
3208           huart->Instance->BRR = usartdiv;
3209         }
3210         else
3211         {
3212           ret = HAL_ERROR;
3213         }
3214       } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) ||
3215                 (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
3216     } /* if (pclk != 0) */
3217   }
3218   /* Check UART Over Sampling to set Baud Rate Register */
3219   else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
3220   {
3221     switch (clocksource)
3222     {
3223 #if defined(USART2)
3224       case UART_CLOCKSOURCE_PCLK1:
3225         pclk = HAL_RCC_GetPCLK1Freq();
3226         break;
3227 #endif /* USART2 */
3228       case UART_CLOCKSOURCE_PCLK2:
3229         pclk = HAL_RCC_GetPCLK2Freq();
3230         break;
3231       case UART_CLOCKSOURCE_HSI:
3232         pclk = (uint32_t) HSI_VALUE;
3233         break;
3234       case UART_CLOCKSOURCE_SYSCLK:
3235         pclk = HAL_RCC_GetSysClockFreq();
3236         break;
3237       case UART_CLOCKSOURCE_LSE:
3238         pclk = (uint32_t) LSE_VALUE;
3239         break;
3240       default:
3241         pclk = 0U;
3242         ret = HAL_ERROR;
3243         break;
3244     }
3245 
3246     /* USARTDIV must be greater than or equal to 0d16 */
3247     if (pclk != 0U)
3248     {
3249       usartdiv = (uint32_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3250       if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3251       {
3252         brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
3253         brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
3254         huart->Instance->BRR = brrtemp;
3255       }
3256       else
3257       {
3258         ret = HAL_ERROR;
3259       }
3260     }
3261   }
3262   else
3263   {
3264     switch (clocksource)
3265     {
3266 #if defined(USART2)
3267       case UART_CLOCKSOURCE_PCLK1:
3268         pclk = HAL_RCC_GetPCLK1Freq();
3269         break;
3270 #endif /* USART2 */
3271       case UART_CLOCKSOURCE_PCLK2:
3272         pclk = HAL_RCC_GetPCLK2Freq();
3273         break;
3274       case UART_CLOCKSOURCE_HSI:
3275         pclk = (uint32_t) HSI_VALUE;
3276         break;
3277       case UART_CLOCKSOURCE_SYSCLK:
3278         pclk = HAL_RCC_GetSysClockFreq();
3279         break;
3280       case UART_CLOCKSOURCE_LSE:
3281         pclk = (uint32_t) LSE_VALUE;
3282         break;
3283       default:
3284         pclk = 0U;
3285         ret = HAL_ERROR;
3286         break;
3287     }
3288 
3289     if (pclk != 0U)
3290     {
3291       /* USARTDIV must be greater than or equal to 0d16 */
3292       usartdiv = (uint32_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3293       if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3294       {
3295         huart->Instance->BRR = (uint16_t)usartdiv;
3296       }
3297       else
3298       {
3299         ret = HAL_ERROR;
3300       }
3301     }
3302   }
3303 
3304   /* Initialize the number of data to process during RX/TX ISR execution */
3305   huart->NbTxDataToProcess = 1;
3306   huart->NbRxDataToProcess = 1;
3307 
3308   /* Clear ISR function pointers */
3309   huart->RxISR = NULL;
3310   huart->TxISR = NULL;
3311 
3312   return ret;
3313 }
3314 
3315 /**
3316   * @brief Configure the UART peripheral advanced features.
3317   * @param huart UART handle.
3318   * @retval None
3319   */
UART_AdvFeatureConfig(UART_HandleTypeDef * huart)3320 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
3321 {
3322   /* Check whether the set of advanced features to configure is properly set */
3323   assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
3324 
3325   /* if required, configure RX/TX pins swap */
3326   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
3327   {
3328     assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
3329     MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
3330   }
3331 
3332   /* if required, configure TX pin active level inversion */
3333   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
3334   {
3335     assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
3336     MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
3337   }
3338 
3339   /* if required, configure RX pin active level inversion */
3340   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
3341   {
3342     assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
3343     MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
3344   }
3345 
3346   /* if required, configure data inversion */
3347   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
3348   {
3349     assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
3350     MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
3351   }
3352 
3353   /* if required, configure RX overrun detection disabling */
3354   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
3355   {
3356     assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
3357     MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
3358   }
3359 
3360 #if defined(HAL_DMA_MODULE_ENABLED)
3361   /* if required, configure DMA disabling on reception error */
3362   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
3363   {
3364     assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
3365     MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
3366   }
3367 #endif /* HAL_DMA_MODULE_ENABLED */
3368 
3369   /* if required, configure auto Baud rate detection scheme */
3370   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
3371   {
3372     assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
3373     assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
3374     MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
3375     /* set auto Baudrate detection parameters if detection is enabled */
3376     if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
3377     {
3378       assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
3379       MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
3380     }
3381   }
3382 
3383   /* if required, configure MSB first on communication line */
3384   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
3385   {
3386     assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
3387     MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
3388   }
3389 }
3390 
3391 /**
3392   * @brief Check the UART Idle State.
3393   * @param huart UART handle.
3394   * @retval HAL status
3395   */
UART_CheckIdleState(UART_HandleTypeDef * huart)3396 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
3397 {
3398   uint32_t tickstart;
3399 
3400   /* Initialize the UART ErrorCode */
3401   huart->ErrorCode = HAL_UART_ERROR_NONE;
3402 
3403   /* Init tickstart for timeout management */
3404   tickstart = HAL_GetTick();
3405 
3406   /* Check if the Transmitter is enabled */
3407   if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
3408   {
3409     /* Wait until TEACK flag is set */
3410     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3411     {
3412       /* Disable TXE interrupt for the interrupt process */
3413       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE));
3414 
3415       huart->gState = HAL_UART_STATE_READY;
3416 
3417       __HAL_UNLOCK(huart);
3418 
3419       /* Timeout occurred */
3420       return HAL_TIMEOUT;
3421     }
3422   }
3423 
3424   /* Check if the Receiver is enabled */
3425   if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
3426   {
3427     /* Wait until REACK flag is set */
3428     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3429     {
3430       /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error)
3431       interrupts for the interrupt process */
3432       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3433       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3434 
3435       huart->RxState = HAL_UART_STATE_READY;
3436 
3437       __HAL_UNLOCK(huart);
3438 
3439       /* Timeout occurred */
3440       return HAL_TIMEOUT;
3441     }
3442   }
3443 
3444   /* Initialize the UART State */
3445   huart->gState = HAL_UART_STATE_READY;
3446   huart->RxState = HAL_UART_STATE_READY;
3447   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3448   huart->RxEventType = HAL_UART_RXEVENT_TC;
3449 
3450   __HAL_UNLOCK(huart);
3451 
3452   return HAL_OK;
3453 }
3454 
3455 /**
3456   * @brief  This function handles UART Communication Timeout. It waits
3457   *                  until a flag is no longer in the specified status.
3458   * @param huart     UART handle.
3459   * @param Flag      Specifies the UART flag to check
3460   * @param Status    The actual Flag status (SET or RESET)
3461   * @param Tickstart Tick start value
3462   * @param Timeout   Timeout duration
3463   * @retval HAL status
3464   */
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef * huart,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)3465 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
3466                                               uint32_t Tickstart, uint32_t Timeout)
3467 {
3468   /* Wait until flag is set */
3469   while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
3470   {
3471     /* Check for the Timeout */
3472     if (Timeout != HAL_MAX_DELAY)
3473     {
3474       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
3475       {
3476 
3477         return HAL_TIMEOUT;
3478       }
3479 
3480       if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U)
3481       {
3482         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
3483         {
3484           /* Clear Overrun Error flag*/
3485           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
3486 
3487           /* Blocking error : transfer is aborted
3488           Set the UART state ready to be able to start again the process,
3489           Disable Rx Interrupts if ongoing */
3490           UART_EndRxTransfer(huart);
3491 
3492           huart->ErrorCode = HAL_UART_ERROR_ORE;
3493 
3494           /* Process Unlocked */
3495           __HAL_UNLOCK(huart);
3496 
3497           return HAL_ERROR;
3498         }
3499         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
3500         {
3501           /* Clear Receiver Timeout flag*/
3502           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
3503 
3504           /* Blocking error : transfer is aborted
3505           Set the UART state ready to be able to start again the process,
3506           Disable Rx Interrupts if ongoing */
3507           UART_EndRxTransfer(huart);
3508 
3509           huart->ErrorCode = HAL_UART_ERROR_RTO;
3510 
3511           /* Process Unlocked */
3512           __HAL_UNLOCK(huart);
3513 
3514           return HAL_TIMEOUT;
3515         }
3516       }
3517     }
3518   }
3519   return HAL_OK;
3520 }
3521 
3522 /**
3523   * @brief  Start Receive operation in interrupt mode.
3524   * @note   This function could be called by all HAL UART API providing reception in Interrupt mode.
3525   * @note   When calling this function, parameters validity is considered as already checked,
3526   *         i.e. Rx State, buffer address, ...
3527   *         UART Handle is assumed as Locked.
3528   * @param  huart UART handle.
3529   * @param  pData Pointer to data buffer (u8 or u16 data elements).
3530   * @param  Size  Amount of data elements (u8 or u16) to be received.
3531   * @retval HAL status
3532   */
UART_Start_Receive_IT(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)3533 HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3534 {
3535   huart->pRxBuffPtr  = pData;
3536   huart->RxXferSize  = Size;
3537   huart->RxXferCount = Size;
3538   huart->RxISR       = NULL;
3539 
3540   /* Computation of UART mask to apply to RDR register */
3541   UART_MASK_COMPUTATION(huart);
3542 
3543   huart->ErrorCode = HAL_UART_ERROR_NONE;
3544   huart->RxState = HAL_UART_STATE_BUSY_RX;
3545 
3546   /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3547   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3548 
3549   /* Configure Rx interrupt processing */
3550   if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
3551   {
3552     /* Set the Rx ISR function pointer according to the data word length */
3553     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3554     {
3555       huart->RxISR = UART_RxISR_16BIT_FIFOEN;
3556     }
3557     else
3558     {
3559       huart->RxISR = UART_RxISR_8BIT_FIFOEN;
3560     }
3561 
3562     /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
3563     if (huart->Init.Parity != UART_PARITY_NONE)
3564     {
3565       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3566     }
3567     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
3568   }
3569   else
3570   {
3571     /* Set the Rx ISR function pointer according to the data word length */
3572     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3573     {
3574       huart->RxISR = UART_RxISR_16BIT;
3575     }
3576     else
3577     {
3578       huart->RxISR = UART_RxISR_8BIT;
3579     }
3580 
3581     /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
3582     if (huart->Init.Parity != UART_PARITY_NONE)
3583     {
3584       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
3585     }
3586     else
3587     {
3588       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3589     }
3590   }
3591   return HAL_OK;
3592 }
3593 
3594 #if defined(HAL_DMA_MODULE_ENABLED)
3595 /**
3596   * @brief  Start Receive operation in DMA mode.
3597   * @note   This function could be called by all HAL UART API providing reception in DMA mode.
3598   * @note   When calling this function, parameters validity is considered as already checked,
3599   *         i.e. Rx State, buffer address, ...
3600   *         UART Handle is assumed as Locked.
3601   * @param  huart UART handle.
3602   * @param  pData Pointer to data buffer (u8 or u16 data elements).
3603   * @param  Size  Amount of data elements (u8 or u16) to be received.
3604   * @retval HAL status
3605   */
UART_Start_Receive_DMA(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)3606 HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3607 {
3608   HAL_StatusTypeDef status;
3609   uint16_t nbByte = Size;
3610 
3611   huart->pRxBuffPtr = pData;
3612   huart->RxXferSize = Size;
3613 
3614   huart->ErrorCode = HAL_UART_ERROR_NONE;
3615   huart->RxState = HAL_UART_STATE_BUSY_RX;
3616 
3617   if (huart->hdmarx != NULL)
3618   {
3619     /* Set the UART DMA transfer complete callback */
3620     huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
3621 
3622     /* Set the UART DMA Half transfer complete callback */
3623     huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
3624 
3625     /* Set the DMA error callback */
3626     huart->hdmarx->XferErrorCallback = UART_DMAError;
3627 
3628     /* Set the DMA abort callback */
3629     huart->hdmarx->XferAbortCallback = NULL;
3630 
3631     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
3632        should be aligned on a u16 frontier, so nbByte should be equal to Size * 2 */
3633     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3634     {
3635       nbByte = Size * 2U;
3636     }
3637 
3638     /* Check linked list mode */
3639     if ((huart->hdmarx->Mode & DMA_LINKEDLIST) == DMA_LINKEDLIST)
3640     {
3641       if ((huart->hdmarx->LinkedListQueue != NULL) && (huart->hdmarx->LinkedListQueue->Head != NULL))
3642       {
3643         /* Set DMA data size */
3644         huart->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CBR1_DEFAULT_OFFSET] = nbByte;
3645 
3646         /* Set DMA source address */
3647         huart->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CSAR_DEFAULT_OFFSET] =
3648           (uint32_t)&huart->Instance->RDR;
3649 
3650         /* Set DMA destination address */
3651         huart->hdmarx->LinkedListQueue->Head->LinkRegisters[NODE_CDAR_DEFAULT_OFFSET] = (uint32_t)huart->pRxBuffPtr;
3652 
3653         /* Enable the UART receive DMA channel */
3654         status = HAL_DMAEx_List_Start_IT(huart->hdmarx);
3655       }
3656       else
3657       {
3658         /* Update status */
3659         status = HAL_ERROR;
3660       }
3661     }
3662     else
3663     {
3664       /* Enable the UART receive DMA channel */
3665       status = HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, nbByte);
3666     }
3667 
3668     if (status != HAL_OK)
3669     {
3670       /* Set error code to DMA */
3671       huart->ErrorCode = HAL_UART_ERROR_DMA;
3672 
3673       /* Restore huart->RxState to ready */
3674       huart->RxState = HAL_UART_STATE_READY;
3675 
3676       return HAL_ERROR;
3677     }
3678   }
3679 
3680   /* Enable the UART Parity Error Interrupt */
3681   if (huart->Init.Parity != UART_PARITY_NONE)
3682   {
3683     ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3684   }
3685 
3686   /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3687   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3688 
3689   /* Enable the DMA transfer for the receiver request by setting the DMAR bit
3690   in the UART CR3 register */
3691   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3692 
3693   return HAL_OK;
3694 }
3695 
3696 
3697 /**
3698   * @brief  End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3699   * @param  huart UART handle.
3700   * @retval None
3701   */
UART_EndTxTransfer(UART_HandleTypeDef * huart)3702 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
3703 {
3704   /* Disable TXEIE, TCIE, TXFT interrupts */
3705   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
3706   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
3707 
3708   /* At end of Tx process, restore huart->gState to Ready */
3709   huart->gState = HAL_UART_STATE_READY;
3710 }
3711 #endif /* HAL_DMA_MODULE_ENABLED */
3712 
3713 
3714 /**
3715   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3716   * @param  huart UART handle.
3717   * @retval None
3718   */
UART_EndRxTransfer(UART_HandleTypeDef * huart)3719 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
3720 {
3721   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3722   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3723   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3724 
3725   /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */
3726   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3727   {
3728     ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3729   }
3730 
3731   /* At end of Rx process, restore huart->RxState to Ready */
3732   huart->RxState = HAL_UART_STATE_READY;
3733   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3734 
3735   /* Reset RxIsr function pointer */
3736   huart->RxISR = NULL;
3737 }
3738 
3739 
3740 #if defined(HAL_DMA_MODULE_ENABLED)
3741 /**
3742   * @brief DMA UART transmit process complete callback.
3743   * @param hdma DMA handle.
3744   * @retval None
3745   */
UART_DMATransmitCplt(DMA_HandleTypeDef * hdma)3746 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3747 {
3748   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3749 
3750   /* Check if DMA in circular mode */
3751   if (hdma->Mode != DMA_LINKEDLIST_CIRCULAR)
3752   {
3753     huart->TxXferCount = 0U;
3754 
3755     /* Disable the DMA transfer for transmit request by resetting the DMAT bit
3756        in the UART CR3 register */
3757     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
3758 
3759     /* Enable the UART Transmit Complete Interrupt */
3760     ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3761   }
3762   /* DMA Circular mode */
3763   else
3764   {
3765 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3766     /*Call registered Tx complete callback*/
3767     huart->TxCpltCallback(huart);
3768 #else
3769     /*Call legacy weak Tx complete callback*/
3770     HAL_UART_TxCpltCallback(huart);
3771 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3772   }
3773 }
3774 
3775 /**
3776   * @brief DMA UART transmit process half complete callback.
3777   * @param hdma DMA handle.
3778   * @retval None
3779   */
UART_DMATxHalfCplt(DMA_HandleTypeDef * hdma)3780 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
3781 {
3782   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3783 
3784 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3785   /*Call registered Tx Half complete callback*/
3786   huart->TxHalfCpltCallback(huart);
3787 #else
3788   /*Call legacy weak Tx Half complete callback*/
3789   HAL_UART_TxHalfCpltCallback(huart);
3790 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3791 }
3792 
3793 /**
3794   * @brief DMA UART receive process complete callback.
3795   * @param hdma DMA handle.
3796   * @retval None
3797   */
UART_DMAReceiveCplt(DMA_HandleTypeDef * hdma)3798 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3799 {
3800   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3801 
3802   /* Check if DMA in circular mode */
3803   if (hdma->Mode != DMA_LINKEDLIST_CIRCULAR)
3804   {
3805     huart->RxXferCount = 0U;
3806 
3807     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3808     ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3809     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3810 
3811     /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3812        in the UART CR3 register */
3813     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3814 
3815     /* At end of Rx process, restore huart->RxState to Ready */
3816     huart->RxState = HAL_UART_STATE_READY;
3817 
3818     /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */
3819     if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3820     {
3821       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3822     }
3823   }
3824 
3825   /* Initialize type of RxEvent that correspond to RxEvent callback execution;
3826      In this case, Rx Event type is Transfer Complete */
3827   huart->RxEventType = HAL_UART_RXEVENT_TC;
3828 
3829   /* Check current reception Mode :
3830      If Reception till IDLE event has been selected : use Rx Event callback */
3831   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3832   {
3833 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3834     /*Call registered Rx Event callback*/
3835     huart->RxEventCallback(huart, huart->RxXferSize);
3836 #else
3837     /*Call legacy weak Rx Event callback*/
3838     HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3839 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3840   }
3841   else
3842   {
3843     /* In other cases : use Rx Complete callback */
3844 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3845     /*Call registered Rx complete callback*/
3846     huart->RxCpltCallback(huart);
3847 #else
3848     /*Call legacy weak Rx complete callback*/
3849     HAL_UART_RxCpltCallback(huart);
3850 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3851   }
3852 }
3853 
3854 /**
3855   * @brief DMA UART receive process half complete callback.
3856   * @param hdma DMA handle.
3857   * @retval None
3858   */
UART_DMARxHalfCplt(DMA_HandleTypeDef * hdma)3859 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
3860 {
3861   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3862 
3863   /* Initialize type of RxEvent that correspond to RxEvent callback execution;
3864      In this case, Rx Event type is Half Transfer */
3865   huart->RxEventType = HAL_UART_RXEVENT_HT;
3866 
3867   /* Check current reception Mode :
3868      If Reception till IDLE event has been selected : use Rx Event callback */
3869   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3870   {
3871 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3872     /*Call registered Rx Event callback*/
3873     huart->RxEventCallback(huart, huart->RxXferSize / 2U);
3874 #else
3875     /*Call legacy weak Rx Event callback*/
3876     HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U);
3877 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3878   }
3879   else
3880   {
3881     /* In other cases : use Rx Half Complete callback */
3882 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3883     /*Call registered Rx Half complete callback*/
3884     huart->RxHalfCpltCallback(huart);
3885 #else
3886     /*Call legacy weak Rx Half complete callback*/
3887     HAL_UART_RxHalfCpltCallback(huart);
3888 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3889   }
3890 }
3891 
3892 /**
3893   * @brief DMA UART communication error callback.
3894   * @param hdma DMA handle.
3895   * @retval None
3896   */
UART_DMAError(DMA_HandleTypeDef * hdma)3897 static void UART_DMAError(DMA_HandleTypeDef *hdma)
3898 {
3899   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3900 
3901   const HAL_UART_StateTypeDef gstate = huart->gState;
3902   const HAL_UART_StateTypeDef rxstate = huart->RxState;
3903 
3904   /* Stop UART DMA Tx request if ongoing */
3905   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
3906       (gstate == HAL_UART_STATE_BUSY_TX))
3907   {
3908     huart->TxXferCount = 0U;
3909     UART_EndTxTransfer(huart);
3910   }
3911 
3912   /* Stop UART DMA Rx request if ongoing */
3913   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
3914       (rxstate == HAL_UART_STATE_BUSY_RX))
3915   {
3916     huart->RxXferCount = 0U;
3917     UART_EndRxTransfer(huart);
3918   }
3919 
3920   huart->ErrorCode |= HAL_UART_ERROR_DMA;
3921 
3922 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3923   /*Call registered error callback*/
3924   huart->ErrorCallback(huart);
3925 #else
3926   /*Call legacy weak error callback*/
3927   HAL_UART_ErrorCallback(huart);
3928 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3929 }
3930 
3931 /**
3932   * @brief  DMA UART communication abort callback, when initiated by HAL services on Error
3933   *         (To be called at end of DMA Abort procedure following error occurrence).
3934   * @param  hdma DMA handle.
3935   * @retval None
3936   */
UART_DMAAbortOnError(DMA_HandleTypeDef * hdma)3937 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3938 {
3939   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3940   huart->RxXferCount = 0U;
3941   huart->TxXferCount = 0U;
3942 
3943 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3944   /*Call registered error callback*/
3945   huart->ErrorCallback(huart);
3946 #else
3947   /*Call legacy weak error callback*/
3948   HAL_UART_ErrorCallback(huart);
3949 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3950 }
3951 
3952 /**
3953   * @brief  DMA UART Tx communication abort callback, when initiated by user
3954   *         (To be called at end of DMA Tx Abort procedure following user abort request).
3955   * @note   When this callback is executed, User Abort complete call back is called only if no
3956   *         Abort still ongoing for Rx DMA Handle.
3957   * @param  hdma DMA handle.
3958   * @retval None
3959   */
UART_DMATxAbortCallback(DMA_HandleTypeDef * hdma)3960 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3961 {
3962   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3963 
3964   huart->hdmatx->XferAbortCallback = NULL;
3965 
3966   /* Check if an Abort process is still ongoing */
3967   if (huart->hdmarx != NULL)
3968   {
3969     if (huart->hdmarx->XferAbortCallback != NULL)
3970     {
3971       return;
3972     }
3973   }
3974 
3975   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3976   huart->TxXferCount = 0U;
3977   huart->RxXferCount = 0U;
3978 
3979   /* Reset errorCode */
3980   huart->ErrorCode = HAL_UART_ERROR_NONE;
3981 
3982   /* Clear the Error flags in the ICR register */
3983   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3984 
3985   /* Flush the whole TX FIFO (if needed) */
3986   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3987   {
3988     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3989   }
3990 
3991   /* Restore huart->gState and huart->RxState to Ready */
3992   huart->gState  = HAL_UART_STATE_READY;
3993   huart->RxState = HAL_UART_STATE_READY;
3994   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3995 
3996   /* Call user Abort complete callback */
3997 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3998   /* Call registered Abort complete callback */
3999   huart->AbortCpltCallback(huart);
4000 #else
4001   /* Call legacy weak Abort complete callback */
4002   HAL_UART_AbortCpltCallback(huart);
4003 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4004 }
4005 
4006 
4007 /**
4008   * @brief  DMA UART Rx communication abort callback, when initiated by user
4009   *         (To be called at end of DMA Rx Abort procedure following user abort request).
4010   * @note   When this callback is executed, User Abort complete call back is called only if no
4011   *         Abort still ongoing for Tx DMA Handle.
4012   * @param  hdma DMA handle.
4013   * @retval None
4014   */
UART_DMARxAbortCallback(DMA_HandleTypeDef * hdma)4015 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
4016 {
4017   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
4018 
4019   huart->hdmarx->XferAbortCallback = NULL;
4020 
4021   /* Check if an Abort process is still ongoing */
4022   if (huart->hdmatx != NULL)
4023   {
4024     if (huart->hdmatx->XferAbortCallback != NULL)
4025     {
4026       return;
4027     }
4028   }
4029 
4030   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
4031   huart->TxXferCount = 0U;
4032   huart->RxXferCount = 0U;
4033 
4034   /* Reset errorCode */
4035   huart->ErrorCode = HAL_UART_ERROR_NONE;
4036 
4037   /* Clear the Error flags in the ICR register */
4038   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
4039 
4040   /* Discard the received data */
4041   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4042 
4043   /* Restore huart->gState and huart->RxState to Ready */
4044   huart->gState  = HAL_UART_STATE_READY;
4045   huart->RxState = HAL_UART_STATE_READY;
4046   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4047 
4048   /* Call user Abort complete callback */
4049 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4050   /* Call registered Abort complete callback */
4051   huart->AbortCpltCallback(huart);
4052 #else
4053   /* Call legacy weak Abort complete callback */
4054   HAL_UART_AbortCpltCallback(huart);
4055 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4056 }
4057 
4058 
4059 /**
4060   * @brief  DMA UART Tx communication abort callback, when initiated by user by a call to
4061   *         HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
4062   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
4063   *         and leads to user Tx Abort Complete callback execution).
4064   * @param  hdma DMA handle.
4065   * @retval None
4066   */
UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)4067 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
4068 {
4069   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
4070 
4071   huart->TxXferCount = 0U;
4072 
4073   /* Flush the whole TX FIFO (if needed) */
4074   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
4075   {
4076     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
4077   }
4078 
4079   /* Restore huart->gState to Ready */
4080   huart->gState = HAL_UART_STATE_READY;
4081 
4082   /* Call user Abort complete callback */
4083 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4084   /* Call registered Abort Transmit Complete Callback */
4085   huart->AbortTransmitCpltCallback(huart);
4086 #else
4087   /* Call legacy weak Abort Transmit Complete Callback */
4088   HAL_UART_AbortTransmitCpltCallback(huart);
4089 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4090 }
4091 
4092 /**
4093   * @brief  DMA UART Rx communication abort callback, when initiated by user by a call to
4094   *         HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
4095   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
4096   *         and leads to user Rx Abort Complete callback execution).
4097   * @param  hdma DMA handle.
4098   * @retval None
4099   */
UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)4100 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
4101 {
4102   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4103 
4104   huart->RxXferCount = 0U;
4105 
4106   /* Clear the Error flags in the ICR register */
4107   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
4108 
4109   /* Discard the received data */
4110   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4111 
4112   /* Restore huart->RxState to Ready */
4113   huart->RxState = HAL_UART_STATE_READY;
4114   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4115 
4116   /* Call user Abort complete callback */
4117 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4118   /* Call registered Abort Receive Complete Callback */
4119   huart->AbortReceiveCpltCallback(huart);
4120 #else
4121   /* Call legacy weak Abort Receive Complete Callback */
4122   HAL_UART_AbortReceiveCpltCallback(huart);
4123 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4124 }
4125 #endif /* HAL_DMA_MODULE_ENABLED */
4126 
4127 /**
4128   * @brief TX interrupt handler for 7 or 8 bits data word length .
4129   * @note   Function is called under interruption only, once
4130   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4131   * @param huart UART handle.
4132   * @retval None
4133   */
UART_TxISR_8BIT(UART_HandleTypeDef * huart)4134 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
4135 {
4136   /* Check that a Tx process is ongoing */
4137   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4138   {
4139     if (huart->TxXferCount == 0U)
4140     {
4141       /* Disable the UART Transmit Data Register Empty Interrupt */
4142       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
4143 
4144       /* Enable the UART Transmit Complete Interrupt */
4145       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4146     }
4147     else
4148     {
4149       huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4150       huart->pTxBuffPtr++;
4151       huart->TxXferCount--;
4152     }
4153   }
4154 }
4155 
4156 /**
4157   * @brief TX interrupt handler for 9 bits data word length.
4158   * @note   Function is called under interruption only, once
4159   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4160   * @param huart UART handle.
4161   * @retval None
4162   */
UART_TxISR_16BIT(UART_HandleTypeDef * huart)4163 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
4164 {
4165   const uint16_t *tmp;
4166 
4167   /* Check that a Tx process is ongoing */
4168   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4169   {
4170     if (huart->TxXferCount == 0U)
4171     {
4172       /* Disable the UART Transmit Data Register Empty Interrupt */
4173       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
4174 
4175       /* Enable the UART Transmit Complete Interrupt */
4176       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4177     }
4178     else
4179     {
4180       tmp = (const uint16_t *) huart->pTxBuffPtr;
4181       huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4182       huart->pTxBuffPtr += 2U;
4183       huart->TxXferCount--;
4184     }
4185   }
4186 }
4187 
4188 /**
4189   * @brief TX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
4190   * @note   Function is called under interruption only, once
4191   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4192   * @param huart UART handle.
4193   * @retval None
4194   */
UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef * huart)4195 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4196 {
4197   uint16_t  nb_tx_data;
4198 
4199   /* Check that a Tx process is ongoing */
4200   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4201   {
4202     for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4203     {
4204       if (huart->TxXferCount == 0U)
4205       {
4206         /* Disable the TX FIFO threshold interrupt */
4207         ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4208 
4209         /* Enable the UART Transmit Complete Interrupt */
4210         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4211 
4212         break; /* force exit loop */
4213       }
4214       else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4215       {
4216         huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4217         huart->pTxBuffPtr++;
4218         huart->TxXferCount--;
4219       }
4220       else
4221       {
4222         /* Nothing to do */
4223       }
4224     }
4225   }
4226 }
4227 
4228 /**
4229   * @brief TX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4230   * @note   Function is called under interruption only, once
4231   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4232   * @param huart UART handle.
4233   * @retval None
4234   */
UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef * huart)4235 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4236 {
4237   const uint16_t *tmp;
4238   uint16_t  nb_tx_data;
4239 
4240   /* Check that a Tx process is ongoing */
4241   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4242   {
4243     for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4244     {
4245       if (huart->TxXferCount == 0U)
4246       {
4247         /* Disable the TX FIFO threshold interrupt */
4248         ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4249 
4250         /* Enable the UART Transmit Complete Interrupt */
4251         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4252 
4253         break; /* force exit loop */
4254       }
4255       else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4256       {
4257         tmp = (const uint16_t *) huart->pTxBuffPtr;
4258         huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4259         huart->pTxBuffPtr += 2U;
4260         huart->TxXferCount--;
4261       }
4262       else
4263       {
4264         /* Nothing to do */
4265       }
4266     }
4267   }
4268 }
4269 
4270 /**
4271   * @brief  Wrap up transmission in non-blocking mode.
4272   * @param  huart pointer to a UART_HandleTypeDef structure that contains
4273   *                the configuration information for the specified UART module.
4274   * @retval None
4275   */
UART_EndTransmit_IT(UART_HandleTypeDef * huart)4276 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
4277 {
4278   /* Disable the UART Transmit Complete Interrupt */
4279   ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4280 
4281   /* Tx process is ended, restore huart->gState to Ready */
4282   huart->gState = HAL_UART_STATE_READY;
4283 
4284   /* Cleat TxISR function pointer */
4285   huart->TxISR = NULL;
4286 
4287 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4288   /*Call registered Tx complete callback*/
4289   huart->TxCpltCallback(huart);
4290 #else
4291   /*Call legacy weak Tx complete callback*/
4292   HAL_UART_TxCpltCallback(huart);
4293 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4294 }
4295 
4296 /**
4297   * @brief RX interrupt handler for 7 or 8 bits data word length .
4298   * @param huart UART handle.
4299   * @retval None
4300   */
UART_RxISR_8BIT(UART_HandleTypeDef * huart)4301 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
4302 {
4303   uint16_t uhMask = huart->Mask;
4304   uint16_t  uhdata;
4305 
4306   /* Check that a Rx process is ongoing */
4307   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4308   {
4309     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4310     *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4311     huart->pRxBuffPtr++;
4312     huart->RxXferCount--;
4313 
4314     if (huart->RxXferCount == 0U)
4315     {
4316       /* Disable the UART Parity Error Interrupt and RXNE interrupts */
4317       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4318 
4319       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4320       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4321 
4322       /* Rx process is completed, restore huart->RxState to Ready */
4323       huart->RxState = HAL_UART_STATE_READY;
4324 
4325       /* Clear RxISR function pointer */
4326       huart->RxISR = NULL;
4327 
4328       /* Initialize type of RxEvent to Transfer Complete */
4329       huart->RxEventType = HAL_UART_RXEVENT_TC;
4330 
4331       if (!(IS_LPUART_INSTANCE(huart->Instance)))
4332       {
4333         /* Check that USART RTOEN bit is set */
4334         if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4335         {
4336           /* Enable the UART Receiver Timeout Interrupt */
4337           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4338         }
4339       }
4340 
4341       /* Check current reception Mode :
4342          If Reception till IDLE event has been selected : */
4343       if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4344       {
4345         /* Set reception type to Standard */
4346         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4347 
4348         /* Disable IDLE interrupt */
4349         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4350 
4351         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4352         {
4353           /* Clear IDLE Flag */
4354           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4355         }
4356 
4357 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4358         /*Call registered Rx Event callback*/
4359         huart->RxEventCallback(huart, huart->RxXferSize);
4360 #else
4361         /*Call legacy weak Rx Event callback*/
4362         HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4363 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4364       }
4365       else
4366       {
4367         /* Standard reception API called */
4368 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4369         /*Call registered Rx complete callback*/
4370         huart->RxCpltCallback(huart);
4371 #else
4372         /*Call legacy weak Rx complete callback*/
4373         HAL_UART_RxCpltCallback(huart);
4374 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4375       }
4376     }
4377   }
4378   else
4379   {
4380     /* Clear RXNE interrupt flag */
4381     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4382   }
4383 }
4384 
4385 /**
4386   * @brief RX interrupt handler for 9 bits data word length .
4387   * @note   Function is called under interruption only, once
4388   *         interruptions have been enabled by HAL_UART_Receive_IT()
4389   * @param huart UART handle.
4390   * @retval None
4391   */
UART_RxISR_16BIT(UART_HandleTypeDef * huart)4392 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
4393 {
4394   uint16_t *tmp;
4395   uint16_t uhMask = huart->Mask;
4396   uint16_t  uhdata;
4397 
4398   /* Check that a Rx process is ongoing */
4399   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4400   {
4401     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4402     tmp = (uint16_t *) huart->pRxBuffPtr ;
4403     *tmp = (uint16_t)(uhdata & uhMask);
4404     huart->pRxBuffPtr += 2U;
4405     huart->RxXferCount--;
4406 
4407     if (huart->RxXferCount == 0U)
4408     {
4409       /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
4410       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4411 
4412       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4413       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4414 
4415       /* Rx process is completed, restore huart->RxState to Ready */
4416       huart->RxState = HAL_UART_STATE_READY;
4417 
4418       /* Clear RxISR function pointer */
4419       huart->RxISR = NULL;
4420 
4421       /* Initialize type of RxEvent to Transfer Complete */
4422       huart->RxEventType = HAL_UART_RXEVENT_TC;
4423 
4424       if (!(IS_LPUART_INSTANCE(huart->Instance)))
4425       {
4426         /* Check that USART RTOEN bit is set */
4427         if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4428         {
4429           /* Enable the UART Receiver Timeout Interrupt */
4430           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4431         }
4432       }
4433 
4434       /* Check current reception Mode :
4435          If Reception till IDLE event has been selected : */
4436       if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4437       {
4438         /* Set reception type to Standard */
4439         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4440 
4441         /* Disable IDLE interrupt */
4442         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4443 
4444         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4445         {
4446           /* Clear IDLE Flag */
4447           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4448         }
4449 
4450 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4451         /*Call registered Rx Event callback*/
4452         huart->RxEventCallback(huart, huart->RxXferSize);
4453 #else
4454         /*Call legacy weak Rx Event callback*/
4455         HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4456 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4457       }
4458       else
4459       {
4460         /* Standard reception API called */
4461 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4462         /*Call registered Rx complete callback*/
4463         huart->RxCpltCallback(huart);
4464 #else
4465         /*Call legacy weak Rx complete callback*/
4466         HAL_UART_RxCpltCallback(huart);
4467 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4468       }
4469     }
4470   }
4471   else
4472   {
4473     /* Clear RXNE interrupt flag */
4474     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4475   }
4476 }
4477 
4478 /**
4479   * @brief RX interrupt handler for 7 or 8  bits data word length and FIFO mode is enabled.
4480   * @note   Function is called under interruption only, once
4481   *         interruptions have been enabled by HAL_UART_Receive_IT()
4482   * @param huart UART handle.
4483   * @retval None
4484   */
UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef * huart)4485 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4486 {
4487   uint16_t  uhMask = huart->Mask;
4488   uint16_t  uhdata;
4489   uint16_t  nb_rx_data;
4490   uint16_t  rxdatacount;
4491   uint32_t  isrflags = READ_REG(huart->Instance->ISR);
4492   uint32_t  cr1its   = READ_REG(huart->Instance->CR1);
4493   uint32_t  cr3its   = READ_REG(huart->Instance->CR3);
4494 
4495   /* Check that a Rx process is ongoing */
4496   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4497   {
4498     nb_rx_data = huart->NbRxDataToProcess;
4499     while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4500     {
4501       uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4502       *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4503       huart->pRxBuffPtr++;
4504       huart->RxXferCount--;
4505       isrflags = READ_REG(huart->Instance->ISR);
4506 
4507       /* If some non blocking errors occurred */
4508       if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4509       {
4510         /* UART parity error interrupt occurred -------------------------------------*/
4511         if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4512         {
4513           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4514 
4515           huart->ErrorCode |= HAL_UART_ERROR_PE;
4516         }
4517 
4518         /* UART frame error interrupt occurred --------------------------------------*/
4519         if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4520         {
4521           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4522 
4523           huart->ErrorCode |= HAL_UART_ERROR_FE;
4524         }
4525 
4526         /* UART noise error interrupt occurred --------------------------------------*/
4527         if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4528         {
4529           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4530 
4531           huart->ErrorCode |= HAL_UART_ERROR_NE;
4532         }
4533 
4534         /* Call UART Error Call back function if need be ----------------------------*/
4535         if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4536         {
4537           /* Non Blocking error : transfer could go on.
4538           Error is notified to user through user error callback */
4539 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4540           /*Call registered error callback*/
4541           huart->ErrorCallback(huart);
4542 #else
4543           /*Call legacy weak error callback*/
4544           HAL_UART_ErrorCallback(huart);
4545 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4546           huart->ErrorCode = HAL_UART_ERROR_NONE;
4547         }
4548       }
4549 
4550       if (huart->RxXferCount == 0U)
4551       {
4552         /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4553         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4554 
4555         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4556            and RX FIFO Threshold interrupt */
4557         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4558 
4559         /* Rx process is completed, restore huart->RxState to Ready */
4560         huart->RxState = HAL_UART_STATE_READY;
4561 
4562         /* Clear RxISR function pointer */
4563         huart->RxISR = NULL;
4564 
4565         /* Initialize type of RxEvent to Transfer Complete */
4566         huart->RxEventType = HAL_UART_RXEVENT_TC;
4567 
4568         if (!(IS_LPUART_INSTANCE(huart->Instance)))
4569         {
4570           /* Check that USART RTOEN bit is set */
4571           if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4572           {
4573             /* Enable the UART Receiver Timeout Interrupt */
4574             ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4575           }
4576         }
4577 
4578         /* Check current reception Mode :
4579            If Reception till IDLE event has been selected : */
4580         if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4581         {
4582           /* Set reception type to Standard */
4583           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4584 
4585           /* Disable IDLE interrupt */
4586           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4587 
4588           if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4589           {
4590             /* Clear IDLE Flag */
4591             __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4592           }
4593 
4594 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4595           /*Call registered Rx Event callback*/
4596           huart->RxEventCallback(huart, huart->RxXferSize);
4597 #else
4598           /*Call legacy weak Rx Event callback*/
4599           HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4600 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4601         }
4602         else
4603         {
4604           /* Standard reception API called */
4605 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4606           /*Call registered Rx complete callback*/
4607           huart->RxCpltCallback(huart);
4608 #else
4609           /*Call legacy weak Rx complete callback*/
4610           HAL_UART_RxCpltCallback(huart);
4611 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4612         }
4613       }
4614     }
4615 
4616     /* When remaining number of bytes to receive is less than the RX FIFO
4617     threshold, next incoming frames are processed as if FIFO mode was
4618     disabled (i.e. one interrupt per received frame).
4619     */
4620     rxdatacount = huart->RxXferCount;
4621     if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4622     {
4623       /* Disable the UART RXFT interrupt*/
4624       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4625 
4626       /* Update the RxISR function pointer */
4627       huart->RxISR = UART_RxISR_8BIT;
4628 
4629       /* Enable the UART Data Register Not Empty interrupt */
4630       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4631     }
4632   }
4633   else
4634   {
4635     /* Clear RXNE interrupt flag */
4636     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4637   }
4638 }
4639 
4640 /**
4641   * @brief RX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4642   * @note   Function is called under interruption only, once
4643   *         interruptions have been enabled by HAL_UART_Receive_IT()
4644   * @param huart UART handle.
4645   * @retval None
4646   */
UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef * huart)4647 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4648 {
4649   uint16_t *tmp;
4650   uint16_t  uhMask = huart->Mask;
4651   uint16_t  uhdata;
4652   uint16_t  nb_rx_data;
4653   uint16_t  rxdatacount;
4654   uint32_t  isrflags = READ_REG(huart->Instance->ISR);
4655   uint32_t  cr1its   = READ_REG(huart->Instance->CR1);
4656   uint32_t  cr3its   = READ_REG(huart->Instance->CR3);
4657 
4658   /* Check that a Rx process is ongoing */
4659   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4660   {
4661     nb_rx_data = huart->NbRxDataToProcess;
4662     while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4663     {
4664       uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4665       tmp = (uint16_t *) huart->pRxBuffPtr ;
4666       *tmp = (uint16_t)(uhdata & uhMask);
4667       huart->pRxBuffPtr += 2U;
4668       huart->RxXferCount--;
4669       isrflags = READ_REG(huart->Instance->ISR);
4670 
4671       /* If some non blocking errors occurred */
4672       if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4673       {
4674         /* UART parity error interrupt occurred -------------------------------------*/
4675         if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4676         {
4677           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4678 
4679           huart->ErrorCode |= HAL_UART_ERROR_PE;
4680         }
4681 
4682         /* UART frame error interrupt occurred --------------------------------------*/
4683         if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4684         {
4685           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4686 
4687           huart->ErrorCode |= HAL_UART_ERROR_FE;
4688         }
4689 
4690         /* UART noise error interrupt occurred --------------------------------------*/
4691         if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4692         {
4693           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4694 
4695           huart->ErrorCode |= HAL_UART_ERROR_NE;
4696         }
4697 
4698         /* Call UART Error Call back function if need be ----------------------------*/
4699         if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4700         {
4701           /* Non Blocking error : transfer could go on.
4702           Error is notified to user through user error callback */
4703 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4704           /*Call registered error callback*/
4705           huart->ErrorCallback(huart);
4706 #else
4707           /*Call legacy weak error callback*/
4708           HAL_UART_ErrorCallback(huart);
4709 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4710           huart->ErrorCode = HAL_UART_ERROR_NONE;
4711         }
4712       }
4713 
4714       if (huart->RxXferCount == 0U)
4715       {
4716         /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4717         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4718 
4719         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4720            and RX FIFO Threshold interrupt */
4721         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4722 
4723         /* Rx process is completed, restore huart->RxState to Ready */
4724         huart->RxState = HAL_UART_STATE_READY;
4725 
4726         /* Clear RxISR function pointer */
4727         huart->RxISR = NULL;
4728 
4729         /* Initialize type of RxEvent to Transfer Complete */
4730         huart->RxEventType = HAL_UART_RXEVENT_TC;
4731 
4732         if (!(IS_LPUART_INSTANCE(huart->Instance)))
4733         {
4734           /* Check that USART RTOEN bit is set */
4735           if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4736           {
4737             /* Enable the UART Receiver Timeout Interrupt */
4738             ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4739           }
4740         }
4741 
4742         /* Check current reception Mode :
4743            If Reception till IDLE event has been selected : */
4744         if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4745         {
4746           /* Set reception type to Standard */
4747           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4748 
4749           /* Disable IDLE interrupt */
4750           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4751 
4752           if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4753           {
4754             /* Clear IDLE Flag */
4755             __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4756           }
4757 
4758 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4759           /*Call registered Rx Event callback*/
4760           huart->RxEventCallback(huart, huart->RxXferSize);
4761 #else
4762           /*Call legacy weak Rx Event callback*/
4763           HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4764 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4765         }
4766         else
4767         {
4768           /* Standard reception API called */
4769 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4770           /*Call registered Rx complete callback*/
4771           huart->RxCpltCallback(huart);
4772 #else
4773           /*Call legacy weak Rx complete callback*/
4774           HAL_UART_RxCpltCallback(huart);
4775 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4776         }
4777       }
4778     }
4779 
4780     /* When remaining number of bytes to receive is less than the RX FIFO
4781     threshold, next incoming frames are processed as if FIFO mode was
4782     disabled (i.e. one interrupt per received frame).
4783     */
4784     rxdatacount = huart->RxXferCount;
4785     if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4786     {
4787       /* Disable the UART RXFT interrupt*/
4788       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4789 
4790       /* Update the RxISR function pointer */
4791       huart->RxISR = UART_RxISR_16BIT;
4792 
4793       /* Enable the UART Data Register Not Empty interrupt */
4794       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4795     }
4796   }
4797   else
4798   {
4799     /* Clear RXNE interrupt flag */
4800     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4801   }
4802 }
4803 
4804 /**
4805   * @}
4806   */
4807 
4808 #endif /* HAL_UART_MODULE_ENABLED */
4809 /**
4810   * @}
4811   */
4812 
4813 /**
4814   * @}
4815   */
4816 
4817