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