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