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