Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32G4 / Drivers / STM32G4xx_HAL_Driver / Src / stm32g4xx_hal_uart.c
blobc2809748c4e73614764eb9e4abce2059ae48aa90
1 /**
2 ******************************************************************************
3 * @file stm32g4xx_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
13 @verbatim
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
17 [..]
18 The UART HAL driver can be used as follows:
20 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
21 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
22 (++) Enable the USARTx interface clock.
23 (++) UART pins configuration:
24 (+++) Enable the clock for the UART GPIOs.
25 (+++) Configure these UART pins as alternate function pull-up.
26 (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
27 and HAL_UART_Receive_IT() APIs):
28 (+++) Configure the USARTx interrupt priority.
29 (+++) Enable the NVIC USART IRQ handle.
30 (++) UART interrupts handling:
31 -@@- The specific UART interrupts (Transmission complete interrupt,
32 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
33 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
34 inside the transmit and receive processes.
35 (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
36 and HAL_UART_Receive_DMA() APIs):
37 (+++) Declare a DMA handle structure for the Tx/Rx channel.
38 (+++) Enable the DMAx interface clock.
39 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
40 (+++) Configure the DMA Tx/Rx channel.
41 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
42 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
45 flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
47 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
48 in the huart handle AdvancedInit structure.
50 (#) For the UART asynchronous mode, initialize the UART registers by calling
51 the HAL_UART_Init() API.
53 (#) For the UART Half duplex mode, initialize the UART registers by calling
54 the HAL_HalfDuplex_Init() API.
56 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
57 by calling the HAL_LIN_Init() API.
59 (#) For the UART Multiprocessor mode, initialize the UART registers
60 by calling the HAL_MultiProcessor_Init() API.
62 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
63 by calling the HAL_RS485Ex_Init() API.
65 [..]
66 (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
67 also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
68 calling the customized HAL_UART_MspInit() API.
70 ##### Callback registration #####
71 ==================================
73 [..]
74 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
75 allows the user to configure dynamically the driver callbacks.
77 [..]
78 Use Function @ref HAL_UART_RegisterCallback() to register a user callback.
79 Function @ref HAL_UART_RegisterCallback() allows to register following callbacks:
80 (+) TxHalfCpltCallback : Tx Half Complete Callback.
81 (+) TxCpltCallback : Tx Complete Callback.
82 (+) RxHalfCpltCallback : Rx Half Complete Callback.
83 (+) RxCpltCallback : Rx Complete Callback.
84 (+) ErrorCallback : Error Callback.
85 (+) AbortCpltCallback : Abort Complete Callback.
86 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
87 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
88 (+) WakeupCallback : Wakeup Callback.
89 (+) RxFifoFullCallback : Rx Fifo Full Callback.
90 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
91 (+) MspInitCallback : UART MspInit.
92 (+) MspDeInitCallback : UART MspDeInit.
93 This function takes as parameters the HAL peripheral handle, the Callback ID
94 and a pointer to the user callback function.
96 [..]
97 Use function @ref HAL_UART_UnRegisterCallback() to reset a callback to the default
98 weak (surcharged) function.
99 @ref HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
100 and the Callback ID.
101 This function allows to reset following callbacks:
102 (+) TxHalfCpltCallback : Tx Half Complete Callback.
103 (+) TxCpltCallback : Tx Complete Callback.
104 (+) RxHalfCpltCallback : Rx Half Complete Callback.
105 (+) RxCpltCallback : Rx Complete Callback.
106 (+) ErrorCallback : Error Callback.
107 (+) AbortCpltCallback : Abort Complete Callback.
108 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
109 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
110 (+) WakeupCallback : Wakeup Callback.
111 (+) RxFifoFullCallback : Rx Fifo Full Callback.
112 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
113 (+) MspInitCallback : UART MspInit.
114 (+) MspDeInitCallback : UART MspDeInit.
116 [..]
117 By default, after the @ref HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
118 all callbacks are set to the corresponding weak (surcharged) functions:
119 examples @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback().
120 Exception done for MspInit and MspDeInit functions that are respectively
121 reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init()
122 and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
123 If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref HAL_UART_DeInit()
124 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
126 [..]
127 Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
128 Exception done MspInit/MspDeInit that can be registered/unregistered
129 in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
130 MspInit/DeInit callbacks can be used during the Init/DeInit.
131 In that case first register the MspInit/MspDeInit user callbacks
132 using @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit()
133 or @ref HAL_UART_Init() function.
135 [..]
136 When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
137 not defined, the callback registration feature is not available
138 and weak (surcharged) callbacks are used.
141 @endverbatim
142 ******************************************************************************
143 * @attention
145 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
146 * All rights reserved.</center></h2>
148 * This software component is licensed by ST under BSD 3-Clause license,
149 * the "License"; You may not use this file except in compliance with the
150 * License. You may obtain a copy of the License at:
151 * opensource.org/licenses/BSD-3-Clause
153 ******************************************************************************
156 /* Includes ------------------------------------------------------------------*/
157 #include "stm32g4xx_hal.h"
159 /** @addtogroup STM32G4xx_HAL_Driver
160 * @{
163 /** @defgroup UART UART
164 * @brief HAL UART module driver
165 * @{
168 #ifdef HAL_UART_MODULE_ENABLED
170 /* Private typedef -----------------------------------------------------------*/
171 /* Private define ------------------------------------------------------------*/
172 /** @defgroup UART_Private_Constants UART Private Constants
173 * @{
175 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
176 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
177 USART_CR1_FIFOEN )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
179 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT| \
180 USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
182 #define LPUART_BRR_MIN 0x00000300U /* LPUART BRR minimum authorized value */
183 #define LPUART_BRR_MAX 0x000FFFFFU /* LPUART BRR maximum authorized value */
185 #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
186 #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
189 * @}
192 /* Private macros ------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 /* Private function prototypes -----------------------------------------------*/
195 /** @addtogroup UART_Private_Functions
196 * @{
198 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
199 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
200 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
201 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
202 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
203 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
204 static void UART_DMAError(DMA_HandleTypeDef *hdma);
205 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
206 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
207 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
208 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
209 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
210 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
211 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
212 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
213 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
214 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
215 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
216 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
217 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
218 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
220 * @}
223 /* Exported functions --------------------------------------------------------*/
225 /** @defgroup UART_Exported_Functions UART Exported Functions
226 * @{
229 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
230 * @brief Initialization and Configuration functions
232 @verbatim
233 ===============================================================================
234 ##### Initialization and Configuration functions #####
235 ===============================================================================
236 [..]
237 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
238 in asynchronous mode.
239 (+) For the asynchronous mode the parameters below can be configured:
240 (++) Baud Rate
241 (++) Word Length
242 (++) Stop Bit
243 (++) Parity: If the parity is enabled, then the MSB bit of the data written
244 in the data register is transmitted but is changed by the parity bit.
245 (++) Hardware flow control
246 (++) Receiver/transmitter modes
247 (++) Over Sampling Method
248 (++) One-Bit Sampling Method
249 (+) For the asynchronous mode, the following advanced features can be configured as well:
250 (++) TX and/or RX pin level inversion
251 (++) data logical level inversion
252 (++) RX and TX pins swap
253 (++) RX overrun detection disabling
254 (++) DMA disabling on RX error
255 (++) MSB first on communication line
256 (++) auto Baud rate detection
257 [..]
258 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
259 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
260 and UART multiprocessor mode configuration procedures (details for the procedures
261 are available in reference manual).
263 @endverbatim
265 Depending on the frame length defined by the M1 and M0 bits (7-bit,
266 8-bit or 9-bit), the possible UART formats are listed in the
267 following table.
269 Table 1. UART frame format.
270 +-----------------------------------------------------------------------+
271 | M1 bit | M0 bit | PCE bit | UART frame |
272 |---------|---------|-----------|---------------------------------------|
273 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
274 |---------|---------|-----------|---------------------------------------|
275 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
276 |---------|---------|-----------|---------------------------------------|
277 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
278 |---------|---------|-----------|---------------------------------------|
279 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
280 |---------|---------|-----------|---------------------------------------|
281 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
282 |---------|---------|-----------|---------------------------------------|
283 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
284 +-----------------------------------------------------------------------+
286 * @{
290 * @brief Initialize the UART mode according to the specified
291 * parameters in the UART_InitTypeDef and initialize the associated handle.
292 * @param huart UART handle.
293 * @retval HAL status
295 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
297 /* Check the UART handle allocation */
298 if (huart == NULL)
300 return HAL_ERROR;
303 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
305 /* Check the parameters */
306 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
308 else
310 /* Check the parameters */
311 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
314 if (huart->gState == HAL_UART_STATE_RESET)
316 /* Allocate lock resource and initialize it */
317 huart->Lock = HAL_UNLOCKED;
319 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
320 UART_InitCallbacksToDefault(huart);
322 if (huart->MspInitCallback == NULL)
324 huart->MspInitCallback = HAL_UART_MspInit;
327 /* Init the low level hardware */
328 huart->MspInitCallback(huart);
329 #else
330 /* Init the low level hardware : GPIO, CLOCK */
331 HAL_UART_MspInit(huart);
332 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
335 huart->gState = HAL_UART_STATE_BUSY;
337 __HAL_UART_DISABLE(huart);
339 /* Set the UART Communication parameters */
340 if (UART_SetConfig(huart) == HAL_ERROR)
342 return HAL_ERROR;
345 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
347 UART_AdvFeatureConfig(huart);
350 /* In asynchronous mode, the following bits must be kept cleared:
351 - LINEN and CLKEN bits in the USART_CR2 register,
352 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
353 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
354 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
356 __HAL_UART_ENABLE(huart);
358 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
359 return (UART_CheckIdleState(huart));
363 * @brief Initialize the half-duplex mode according to the specified
364 * parameters in the UART_InitTypeDef and creates the associated handle.
365 * @param huart UART handle.
366 * @retval HAL status
368 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
370 /* Check the UART handle allocation */
371 if (huart == NULL)
373 return HAL_ERROR;
376 /* Check UART instance */
377 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
379 if (huart->gState == HAL_UART_STATE_RESET)
381 /* Allocate lock resource and initialize it */
382 huart->Lock = HAL_UNLOCKED;
384 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
385 UART_InitCallbacksToDefault(huart);
387 if (huart->MspInitCallback == NULL)
389 huart->MspInitCallback = HAL_UART_MspInit;
392 /* Init the low level hardware */
393 huart->MspInitCallback(huart);
394 #else
395 /* Init the low level hardware : GPIO, CLOCK */
396 HAL_UART_MspInit(huart);
397 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
400 huart->gState = HAL_UART_STATE_BUSY;
402 __HAL_UART_DISABLE(huart);
404 /* Set the UART Communication parameters */
405 if (UART_SetConfig(huart) == HAL_ERROR)
407 return HAL_ERROR;
410 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
412 UART_AdvFeatureConfig(huart);
415 /* In half-duplex mode, the following bits must be kept cleared:
416 - LINEN and CLKEN bits in the USART_CR2 register,
417 - SCEN and IREN bits in the USART_CR3 register.*/
418 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
419 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
421 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
422 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
424 __HAL_UART_ENABLE(huart);
426 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
427 return (UART_CheckIdleState(huart));
432 * @brief Initialize the LIN mode according to the specified
433 * parameters in the UART_InitTypeDef and creates the associated handle.
434 * @param huart UART handle.
435 * @param BreakDetectLength Specifies the LIN break detection length.
436 * This parameter can be one of the following values:
437 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
438 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
439 * @retval HAL status
441 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
443 /* Check the UART handle allocation */
444 if (huart == NULL)
446 return HAL_ERROR;
449 /* Check the LIN UART instance */
450 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
451 /* Check the Break detection length parameter */
452 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
454 /* LIN mode limited to 16-bit oversampling only */
455 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
457 return HAL_ERROR;
459 /* LIN mode limited to 8-bit data length */
460 if (huart->Init.WordLength != UART_WORDLENGTH_8B)
462 return HAL_ERROR;
465 if (huart->gState == HAL_UART_STATE_RESET)
467 /* Allocate lock resource and initialize it */
468 huart->Lock = HAL_UNLOCKED;
470 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
471 UART_InitCallbacksToDefault(huart);
473 if (huart->MspInitCallback == NULL)
475 huart->MspInitCallback = HAL_UART_MspInit;
478 /* Init the low level hardware */
479 huart->MspInitCallback(huart);
480 #else
481 /* Init the low level hardware : GPIO, CLOCK */
482 HAL_UART_MspInit(huart);
483 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
486 huart->gState = HAL_UART_STATE_BUSY;
488 __HAL_UART_DISABLE(huart);
490 /* Set the UART Communication parameters */
491 if (UART_SetConfig(huart) == HAL_ERROR)
493 return HAL_ERROR;
496 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
498 UART_AdvFeatureConfig(huart);
501 /* In LIN mode, the following bits must be kept cleared:
502 - LINEN and CLKEN bits in the USART_CR2 register,
503 - SCEN and IREN bits in the USART_CR3 register.*/
504 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
505 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
507 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
508 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
510 /* Set the USART LIN Break detection length. */
511 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
513 __HAL_UART_ENABLE(huart);
515 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
516 return (UART_CheckIdleState(huart));
521 * @brief Initialize the multiprocessor mode according to the specified
522 * parameters in the UART_InitTypeDef and initialize the associated handle.
523 * @param huart UART handle.
524 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
525 * @param WakeUpMethod Specifies the UART wakeup method.
526 * This parameter can be one of the following values:
527 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
528 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
529 * @note If the user resorts to idle line detection wake up, the Address parameter
530 * is useless and ignored by the initialization function.
531 * @note If the user resorts to address mark wake up, the address length detection
532 * is configured by default to 4 bits only. For the UART to be able to
533 * manage 6-, 7- or 8-bit long addresses detection, the API
534 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
535 * HAL_MultiProcessor_Init().
536 * @retval HAL status
538 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
540 /* Check the UART handle allocation */
541 if (huart == NULL)
543 return HAL_ERROR;
546 /* Check the wake up method parameter */
547 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
549 if (huart->gState == HAL_UART_STATE_RESET)
551 /* Allocate lock resource and initialize it */
552 huart->Lock = HAL_UNLOCKED;
554 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
555 UART_InitCallbacksToDefault(huart);
557 if (huart->MspInitCallback == NULL)
559 huart->MspInitCallback = HAL_UART_MspInit;
562 /* Init the low level hardware */
563 huart->MspInitCallback(huart);
564 #else
565 /* Init the low level hardware : GPIO, CLOCK */
566 HAL_UART_MspInit(huart);
567 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
570 huart->gState = HAL_UART_STATE_BUSY;
572 __HAL_UART_DISABLE(huart);
574 /* Set the UART Communication parameters */
575 if (UART_SetConfig(huart) == HAL_ERROR)
577 return HAL_ERROR;
580 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
582 UART_AdvFeatureConfig(huart);
585 /* In multiprocessor mode, the following bits must be kept cleared:
586 - LINEN and CLKEN bits in the USART_CR2 register,
587 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
588 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
589 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
591 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
593 /* If address mark wake up method is chosen, set the USART address node */
594 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
597 /* Set the wake up method by setting the WAKE bit in the CR1 register */
598 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
600 __HAL_UART_ENABLE(huart);
602 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
603 return (UART_CheckIdleState(huart));
608 * @brief DeInitialize the UART peripheral.
609 * @param huart UART handle.
610 * @retval HAL status
612 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
614 /* Check the UART handle allocation */
615 if (huart == NULL)
617 return HAL_ERROR;
620 /* Check the parameters */
621 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
623 huart->gState = HAL_UART_STATE_BUSY;
625 __HAL_UART_DISABLE(huart);
627 huart->Instance->CR1 = 0x0U;
628 huart->Instance->CR2 = 0x0U;
629 huart->Instance->CR3 = 0x0U;
631 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
632 if (huart->MspDeInitCallback == NULL)
634 huart->MspDeInitCallback = HAL_UART_MspDeInit;
636 /* DeInit the low level hardware */
637 huart->MspDeInitCallback(huart);
638 #else
639 /* DeInit the low level hardware */
640 HAL_UART_MspDeInit(huart);
641 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
643 huart->ErrorCode = HAL_UART_ERROR_NONE;
644 huart->gState = HAL_UART_STATE_RESET;
645 huart->RxState = HAL_UART_STATE_RESET;
647 __HAL_UNLOCK(huart);
649 return HAL_OK;
653 * @brief Initialize the UART MSP.
654 * @param huart UART handle.
655 * @retval None
657 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
659 /* Prevent unused argument(s) compilation warning */
660 UNUSED(huart);
662 /* NOTE : This function should not be modified, when the callback is needed,
663 the HAL_UART_MspInit can be implemented in the user file
668 * @brief DeInitialize the UART MSP.
669 * @param huart UART handle.
670 * @retval None
672 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
674 /* Prevent unused argument(s) compilation warning */
675 UNUSED(huart);
677 /* NOTE : This function should not be modified, when the callback is needed,
678 the HAL_UART_MspDeInit can be implemented in the user file
682 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
684 * @brief Register a User UART Callback
685 * To be used instead of the weak predefined callback
686 * @param huart uart handle
687 * @param CallbackID ID of the callback to be registered
688 * This parameter can be one of the following values:
689 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
690 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
691 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
692 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
693 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
694 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
695 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
696 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
697 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
698 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
699 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
700 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
701 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
702 * @param pCallback pointer to the Callback function
703 * @retval HAL status
705 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
706 pUART_CallbackTypeDef pCallback)
708 HAL_StatusTypeDef status = HAL_OK;
710 if (pCallback == NULL)
712 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
714 return HAL_ERROR;
717 __HAL_LOCK(huart);
719 if (huart->gState == HAL_UART_STATE_READY)
721 switch (CallbackID)
723 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
724 huart->TxHalfCpltCallback = pCallback;
725 break;
727 case HAL_UART_TX_COMPLETE_CB_ID :
728 huart->TxCpltCallback = pCallback;
729 break;
731 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
732 huart->RxHalfCpltCallback = pCallback;
733 break;
735 case HAL_UART_RX_COMPLETE_CB_ID :
736 huart->RxCpltCallback = pCallback;
737 break;
739 case HAL_UART_ERROR_CB_ID :
740 huart->ErrorCallback = pCallback;
741 break;
743 case HAL_UART_ABORT_COMPLETE_CB_ID :
744 huart->AbortCpltCallback = pCallback;
745 break;
747 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
748 huart->AbortTransmitCpltCallback = pCallback;
749 break;
751 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
752 huart->AbortReceiveCpltCallback = pCallback;
753 break;
755 case HAL_UART_WAKEUP_CB_ID :
756 huart->WakeupCallback = pCallback;
757 break;
759 case HAL_UART_RX_FIFO_FULL_CB_ID :
760 huart->RxFifoFullCallback = pCallback;
761 break;
763 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
764 huart->TxFifoEmptyCallback = pCallback;
765 break;
767 case HAL_UART_MSPINIT_CB_ID :
768 huart->MspInitCallback = pCallback;
769 break;
771 case HAL_UART_MSPDEINIT_CB_ID :
772 huart->MspDeInitCallback = pCallback;
773 break;
775 default :
776 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
778 status = HAL_ERROR;
779 break;
782 else if (huart->gState == HAL_UART_STATE_RESET)
784 switch (CallbackID)
786 case HAL_UART_MSPINIT_CB_ID :
787 huart->MspInitCallback = pCallback;
788 break;
790 case HAL_UART_MSPDEINIT_CB_ID :
791 huart->MspDeInitCallback = pCallback;
792 break;
794 default :
795 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
797 status = HAL_ERROR;
798 break;
801 else
803 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
805 status = HAL_ERROR;
808 __HAL_UNLOCK(huart);
810 return status;
814 * @brief Unregister an UART Callback
815 * UART callaback is redirected to the weak predefined callback
816 * @param huart uart handle
817 * @param CallbackID ID of the callback to be unregistered
818 * This parameter can be one of the following values:
819 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
820 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
821 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
822 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
823 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
824 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
825 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
826 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
827 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
828 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
829 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
830 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
831 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
832 * @retval HAL status
834 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
836 HAL_StatusTypeDef status = HAL_OK;
838 __HAL_LOCK(huart);
840 if (HAL_UART_STATE_READY == huart->gState)
842 switch (CallbackID)
844 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
845 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
846 break;
848 case HAL_UART_TX_COMPLETE_CB_ID :
849 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
850 break;
852 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
853 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
854 break;
856 case HAL_UART_RX_COMPLETE_CB_ID :
857 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
858 break;
860 case HAL_UART_ERROR_CB_ID :
861 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
862 break;
864 case HAL_UART_ABORT_COMPLETE_CB_ID :
865 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
866 break;
868 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
869 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
870 break;
872 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
873 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
874 break;
876 case HAL_UART_WAKEUP_CB_ID :
877 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
878 break;
880 case HAL_UART_RX_FIFO_FULL_CB_ID :
881 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
882 break;
884 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
885 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
886 break;
888 case HAL_UART_MSPINIT_CB_ID :
889 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
890 break;
892 case HAL_UART_MSPDEINIT_CB_ID :
893 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
894 break;
896 default :
897 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
899 status = HAL_ERROR;
900 break;
903 else if (HAL_UART_STATE_RESET == huart->gState)
905 switch (CallbackID)
907 case HAL_UART_MSPINIT_CB_ID :
908 huart->MspInitCallback = HAL_UART_MspInit;
909 break;
911 case HAL_UART_MSPDEINIT_CB_ID :
912 huart->MspDeInitCallback = HAL_UART_MspDeInit;
913 break;
915 default :
916 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
918 status = HAL_ERROR;
919 break;
922 else
924 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
926 status = HAL_ERROR;
929 __HAL_UNLOCK(huart);
931 return status;
933 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
936 * @}
939 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
940 * @brief UART Transmit/Receive functions
942 @verbatim
943 ===============================================================================
944 ##### IO operation functions #####
945 ===============================================================================
946 This subsection provides a set of functions allowing to manage the UART asynchronous
947 and Half duplex data transfers.
949 (#) There are two mode of transfer:
950 (+) Blocking mode: The communication is performed in polling mode.
951 The HAL status of all data processing is returned by the same function
952 after finishing transfer.
953 (+) Non-Blocking mode: The communication is performed using Interrupts
954 or DMA, These API's return the HAL status.
955 The end of the data processing will be indicated through the
956 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
957 using DMA mode.
958 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
959 will be executed respectively at the end of the transmit or Receive process
960 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
962 (#) Blocking mode API's are :
963 (+) HAL_UART_Transmit()
964 (+) HAL_UART_Receive()
966 (#) Non-Blocking mode API's with Interrupt are :
967 (+) HAL_UART_Transmit_IT()
968 (+) HAL_UART_Receive_IT()
969 (+) HAL_UART_IRQHandler()
971 (#) Non-Blocking mode API's with DMA are :
972 (+) HAL_UART_Transmit_DMA()
973 (+) HAL_UART_Receive_DMA()
974 (+) HAL_UART_DMAPause()
975 (+) HAL_UART_DMAResume()
976 (+) HAL_UART_DMAStop()
978 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
979 (+) HAL_UART_TxHalfCpltCallback()
980 (+) HAL_UART_TxCpltCallback()
981 (+) HAL_UART_RxHalfCpltCallback()
982 (+) HAL_UART_RxCpltCallback()
983 (+) HAL_UART_ErrorCallback()
985 (#) Non-Blocking mode transfers could be aborted using Abort API's :
986 (+) HAL_UART_Abort()
987 (+) HAL_UART_AbortTransmit()
988 (+) HAL_UART_AbortReceive()
989 (+) HAL_UART_Abort_IT()
990 (+) HAL_UART_AbortTransmit_IT()
991 (+) HAL_UART_AbortReceive_IT()
993 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
994 (+) HAL_UART_AbortCpltCallback()
995 (+) HAL_UART_AbortTransmitCpltCallback()
996 (+) HAL_UART_AbortReceiveCpltCallback()
998 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
999 Errors are handled as follows :
1000 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1001 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
1002 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
1003 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
1004 If user wants to abort it, Abort services should be called by user.
1005 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1006 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1007 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1009 -@- In the Half duplex communication, it is forbidden to run the transmit
1010 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1012 @endverbatim
1013 * @{
1017 * @brief Send an amount of data in blocking mode.
1018 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
1019 * data to the TXFIFO. Write operations to the TDR register are performed
1020 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
1021 * TXE are mapped on the same bit-field.
1022 * @param huart UART handle.
1023 * @param pData Pointer to data buffer.
1024 * @param Size Amount of data to be sent.
1025 * @param Timeout Timeout duration.
1026 * @retval HAL status
1028 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1030 uint8_t *pdata8bits;
1031 uint16_t *pdata16bits;
1032 uint32_t tickstart;
1034 /* Check that a Tx process is not already ongoing */
1035 if (huart->gState == HAL_UART_STATE_READY)
1037 if ((pData == NULL) || (Size == 0U))
1039 return HAL_ERROR;
1042 __HAL_LOCK(huart);
1044 huart->ErrorCode = HAL_UART_ERROR_NONE;
1045 huart->gState = HAL_UART_STATE_BUSY_TX;
1047 /* Init tickstart for timeout managment*/
1048 tickstart = HAL_GetTick();
1050 huart->TxXferSize = Size;
1051 huart->TxXferCount = Size;
1053 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1054 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1056 pdata8bits = NULL;
1057 pdata16bits = (uint16_t *) pData;
1059 else
1061 pdata8bits = pData;
1062 pdata16bits = NULL;
1065 while (huart->TxXferCount > 0U)
1067 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1069 return HAL_TIMEOUT;
1071 if (pdata8bits == NULL)
1073 huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
1074 pdata16bits++;
1076 else
1078 huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
1079 pdata8bits++;
1081 huart->TxXferCount--;
1084 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
1086 return HAL_TIMEOUT;
1089 /* At end of Tx process, restore huart->gState to Ready */
1090 huart->gState = HAL_UART_STATE_READY;
1092 __HAL_UNLOCK(huart);
1094 return HAL_OK;
1096 else
1098 return HAL_BUSY;
1103 * @brief Receive an amount of data in blocking mode.
1104 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
1105 * is not empty. Read operations from the RDR register are performed when
1106 * RXFNE flag is set. From hardware perspective, RXFNE flag and
1107 * RXNE are mapped on the same bit-field.
1108 * @param huart UART handle.
1109 * @param pData Pointer to data buffer.
1110 * @param Size Amount of data to be received.
1111 * @param Timeout Timeout duration.
1112 * @retval HAL status
1114 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1116 uint8_t *pdata8bits;
1117 uint16_t *pdata16bits;
1118 uint16_t uhMask;
1119 uint32_t tickstart;
1121 /* Check that a Rx process is not already ongoing */
1122 if (huart->RxState == HAL_UART_STATE_READY)
1124 if ((pData == NULL) || (Size == 0U))
1126 return HAL_ERROR;
1129 __HAL_LOCK(huart);
1131 huart->ErrorCode = HAL_UART_ERROR_NONE;
1132 huart->RxState = HAL_UART_STATE_BUSY_RX;
1134 /* Init tickstart for timeout managment*/
1135 tickstart = HAL_GetTick();
1137 huart->RxXferSize = Size;
1138 huart->RxXferCount = Size;
1140 /* Computation of UART mask to apply to RDR register */
1141 UART_MASK_COMPUTATION(huart);
1142 uhMask = huart->Mask;
1144 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1145 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1147 pdata8bits = NULL;
1148 pdata16bits = (uint16_t *) pData;
1150 else
1152 pdata8bits = pData;
1153 pdata16bits = NULL;
1156 /* as long as data have to be received */
1157 while (huart->RxXferCount > 0U)
1159 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1161 return HAL_TIMEOUT;
1163 if (pdata8bits == NULL)
1165 *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
1166 pdata16bits++;
1168 else
1170 *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1171 pdata8bits++;
1173 huart->RxXferCount--;
1176 /* At end of Rx process, restore huart->RxState to Ready */
1177 huart->RxState = HAL_UART_STATE_READY;
1179 __HAL_UNLOCK(huart);
1181 return HAL_OK;
1183 else
1185 return HAL_BUSY;
1190 * @brief Send an amount of data in interrupt mode.
1191 * @param huart UART handle.
1192 * @param pData Pointer to data buffer.
1193 * @param Size Amount of data to be sent.
1194 * @retval HAL status
1196 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1198 /* Check that a Tx process is not already ongoing */
1199 if (huart->gState == HAL_UART_STATE_READY)
1201 if ((pData == NULL) || (Size == 0U))
1203 return HAL_ERROR;
1206 __HAL_LOCK(huart);
1208 huart->pTxBuffPtr = pData;
1209 huart->TxXferSize = Size;
1210 huart->TxXferCount = Size;
1211 huart->TxISR = NULL;
1213 huart->ErrorCode = HAL_UART_ERROR_NONE;
1214 huart->gState = HAL_UART_STATE_BUSY_TX;
1216 /* Configure Tx interrupt processing */
1217 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1219 /* Set the Tx ISR function pointer according to the data word length */
1220 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1222 huart->TxISR = UART_TxISR_16BIT_FIFOEN;
1224 else
1226 huart->TxISR = UART_TxISR_8BIT_FIFOEN;
1229 __HAL_UNLOCK(huart);
1231 /* Enable the TX FIFO threshold interrupt */
1232 SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1234 else
1236 /* Set the Tx ISR function pointer according to the data word length */
1237 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1239 huart->TxISR = UART_TxISR_16BIT;
1241 else
1243 huart->TxISR = UART_TxISR_8BIT;
1246 __HAL_UNLOCK(huart);
1248 /* Enable the Transmit Data Register Empty interrupt */
1249 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1252 return HAL_OK;
1254 else
1256 return HAL_BUSY;
1261 * @brief Receive an amount of data in interrupt mode.
1262 * @param huart UART handle.
1263 * @param pData Pointer to data buffer.
1264 * @param Size Amount of data to be received.
1265 * @retval HAL status
1267 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1269 /* Check that a Rx process is not already ongoing */
1270 if (huart->RxState == HAL_UART_STATE_READY)
1272 if ((pData == NULL) || (Size == 0U))
1274 return HAL_ERROR;
1277 __HAL_LOCK(huart);
1279 huart->pRxBuffPtr = pData;
1280 huart->RxXferSize = Size;
1281 huart->RxXferCount = Size;
1282 huart->RxISR = NULL;
1284 /* Computation of UART mask to apply to RDR register */
1285 UART_MASK_COMPUTATION(huart);
1287 huart->ErrorCode = HAL_UART_ERROR_NONE;
1288 huart->RxState = HAL_UART_STATE_BUSY_RX;
1290 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1291 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1293 /* Configure Rx interrupt processing*/
1294 if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
1296 /* Set the Rx ISR function pointer according to the data word length */
1297 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1299 huart->RxISR = UART_RxISR_16BIT_FIFOEN;
1301 else
1303 huart->RxISR = UART_RxISR_8BIT_FIFOEN;
1306 __HAL_UNLOCK(huart);
1308 /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
1309 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1310 SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
1312 else
1314 /* Set the Rx ISR function pointer according to the data word length */
1315 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1317 huart->RxISR = UART_RxISR_16BIT;
1319 else
1321 huart->RxISR = UART_RxISR_8BIT;
1324 __HAL_UNLOCK(huart);
1326 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
1327 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1330 return HAL_OK;
1332 else
1334 return HAL_BUSY;
1339 * @brief Send an amount of data in DMA mode.
1340 * @param huart UART handle.
1341 * @param pData Pointer to data buffer.
1342 * @param Size Amount of data to be sent.
1343 * @retval HAL status
1345 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1347 /* Check that a Tx process is not already ongoing */
1348 if (huart->gState == HAL_UART_STATE_READY)
1350 if ((pData == NULL) || (Size == 0U))
1352 return HAL_ERROR;
1355 __HAL_LOCK(huart);
1357 huart->pTxBuffPtr = pData;
1358 huart->TxXferSize = Size;
1359 huart->TxXferCount = Size;
1361 huart->ErrorCode = HAL_UART_ERROR_NONE;
1362 huart->gState = HAL_UART_STATE_BUSY_TX;
1364 if (huart->hdmatx != NULL)
1366 /* Set the UART DMA transfer complete callback */
1367 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
1369 /* Set the UART DMA Half transfer complete callback */
1370 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
1372 /* Set the DMA error callback */
1373 huart->hdmatx->XferErrorCallback = UART_DMAError;
1375 /* Set the DMA abort callback */
1376 huart->hdmatx->XferAbortCallback = NULL;
1378 /* Enable the UART transmit DMA channel */
1379 if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
1381 /* Set error code to DMA */
1382 huart->ErrorCode = HAL_UART_ERROR_DMA;
1384 __HAL_UNLOCK(huart);
1386 /* Restore huart->gState to ready */
1387 huart->gState = HAL_UART_STATE_READY;
1389 return HAL_ERROR;
1392 /* Clear the TC flag in the ICR register */
1393 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
1395 __HAL_UNLOCK(huart);
1397 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1398 in the UART CR3 register */
1399 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1401 return HAL_OK;
1403 else
1405 return HAL_BUSY;
1410 * @brief Receive an amount of data in DMA mode.
1411 * @note When the UART parity is enabled (PCE = 1), the received data contain
1412 * the parity bit (MSB position).
1413 * @param huart UART handle.
1414 * @param pData Pointer to data buffer.
1415 * @param Size Amount of data to be received.
1416 * @retval HAL status
1418 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1420 /* Check that a Rx process is not already ongoing */
1421 if (huart->RxState == HAL_UART_STATE_READY)
1423 if ((pData == NULL) || (Size == 0U))
1425 return HAL_ERROR;
1428 __HAL_LOCK(huart);
1430 huart->pRxBuffPtr = pData;
1431 huart->RxXferSize = Size;
1433 huart->ErrorCode = HAL_UART_ERROR_NONE;
1434 huart->RxState = HAL_UART_STATE_BUSY_RX;
1436 if (huart->hdmarx != NULL)
1438 /* Set the UART DMA transfer complete callback */
1439 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
1441 /* Set the UART DMA Half transfer complete callback */
1442 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
1444 /* Set the DMA error callback */
1445 huart->hdmarx->XferErrorCallback = UART_DMAError;
1447 /* Set the DMA abort callback */
1448 huart->hdmarx->XferAbortCallback = NULL;
1450 /* Enable the DMA channel */
1451 if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
1453 /* Set error code to DMA */
1454 huart->ErrorCode = HAL_UART_ERROR_DMA;
1456 __HAL_UNLOCK(huart);
1458 /* Restore huart->gState to ready */
1459 huart->gState = HAL_UART_STATE_READY;
1461 return HAL_ERROR;
1464 __HAL_UNLOCK(huart);
1466 /* Enable the UART Parity Error Interrupt */
1467 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1469 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1470 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1472 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1473 in the UART CR3 register */
1474 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1476 return HAL_OK;
1478 else
1480 return HAL_BUSY;
1485 * @brief Pause the DMA Transfer.
1486 * @param huart UART handle.
1487 * @retval HAL status
1489 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1491 const HAL_UART_StateTypeDef gstate = huart->gState;
1492 const HAL_UART_StateTypeDef rxstate = huart->RxState;
1494 __HAL_LOCK(huart);
1496 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1497 (gstate == HAL_UART_STATE_BUSY_TX))
1499 /* Disable the UART DMA Tx request */
1500 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1502 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1503 (rxstate == HAL_UART_STATE_BUSY_RX))
1505 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1506 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1507 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1509 /* Disable the UART DMA Rx request */
1510 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1513 __HAL_UNLOCK(huart);
1515 return HAL_OK;
1519 * @brief Resume the DMA Transfer.
1520 * @param huart UART handle.
1521 * @retval HAL status
1523 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1525 __HAL_LOCK(huart);
1527 if (huart->gState == HAL_UART_STATE_BUSY_TX)
1529 /* Enable the UART DMA Tx request */
1530 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1532 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
1534 /* Clear the Overrun flag before resuming the Rx transfer */
1535 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
1537 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1538 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1539 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1541 /* Enable the UART DMA Rx request */
1542 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1545 __HAL_UNLOCK(huart);
1547 return HAL_OK;
1551 * @brief Stop the DMA Transfer.
1552 * @param huart UART handle.
1553 * @retval HAL status
1555 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1557 /* The Lock is not implemented on this API to allow the user application
1558 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1559 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1560 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1561 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1562 the stream and the corresponding call back is executed. */
1564 const HAL_UART_StateTypeDef gstate = huart->gState;
1565 const HAL_UART_StateTypeDef rxstate = huart->RxState;
1567 /* Stop UART DMA Tx request if ongoing */
1568 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1569 (gstate == HAL_UART_STATE_BUSY_TX))
1571 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1573 /* Abort the UART DMA Tx channel */
1574 if (huart->hdmatx != NULL)
1576 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1578 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1580 /* Set error code to DMA */
1581 huart->ErrorCode = HAL_UART_ERROR_DMA;
1583 return HAL_TIMEOUT;
1588 UART_EndTxTransfer(huart);
1591 /* Stop UART DMA Rx request if ongoing */
1592 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1593 (rxstate == HAL_UART_STATE_BUSY_RX))
1595 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1597 /* Abort the UART DMA Rx channel */
1598 if (huart->hdmarx != NULL)
1600 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1602 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1604 /* Set error code to DMA */
1605 huart->ErrorCode = HAL_UART_ERROR_DMA;
1607 return HAL_TIMEOUT;
1612 UART_EndRxTransfer(huart);
1615 return HAL_OK;
1619 * @brief Abort ongoing transfers (blocking mode).
1620 * @param huart UART handle.
1621 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1622 * This procedure performs following operations :
1623 * - Disable UART Interrupts (Tx and Rx)
1624 * - Disable the DMA transfer in the peripheral register (if enabled)
1625 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1626 * - Set handle State to READY
1627 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1628 * @retval HAL status
1630 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1632 /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1633 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1634 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
1636 /* Disable the UART DMA Tx request if enabled */
1637 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1639 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1641 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1642 if (huart->hdmatx != NULL)
1644 /* Set the UART DMA Abort callback to Null.
1645 No call back execution at end of DMA abort procedure */
1646 huart->hdmatx->XferAbortCallback = NULL;
1648 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1650 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1652 /* Set error code to DMA */
1653 huart->ErrorCode = HAL_UART_ERROR_DMA;
1655 return HAL_TIMEOUT;
1661 /* Disable the UART DMA Rx request if enabled */
1662 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1664 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1666 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1667 if (huart->hdmarx != NULL)
1669 /* Set the UART DMA Abort callback to Null.
1670 No call back execution at end of DMA abort procedure */
1671 huart->hdmarx->XferAbortCallback = NULL;
1673 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1675 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1677 /* Set error code to DMA */
1678 huart->ErrorCode = HAL_UART_ERROR_DMA;
1680 return HAL_TIMEOUT;
1686 /* Reset Tx and Rx transfer counters */
1687 huart->TxXferCount = 0U;
1688 huart->RxXferCount = 0U;
1690 /* Clear the Error flags in the ICR register */
1691 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1693 /* Flush the whole TX FIFO (if needed) */
1694 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1696 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1699 /* Discard the received data */
1700 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1702 /* Restore huart->gState and huart->RxState to Ready */
1703 huart->gState = HAL_UART_STATE_READY;
1704 huart->RxState = HAL_UART_STATE_READY;
1706 huart->ErrorCode = HAL_UART_ERROR_NONE;
1708 return HAL_OK;
1712 * @brief Abort ongoing Transmit transfer (blocking mode).
1713 * @param huart UART handle.
1714 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1715 * This procedure performs following operations :
1716 * - Disable UART Interrupts (Tx)
1717 * - Disable the DMA transfer in the peripheral register (if enabled)
1718 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1719 * - Set handle State to READY
1720 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1721 * @retval HAL status
1723 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1725 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1726 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
1727 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1729 /* Disable the UART DMA Tx request if enabled */
1730 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1732 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1734 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1735 if (huart->hdmatx != NULL)
1737 /* Set the UART DMA Abort callback to Null.
1738 No call back execution at end of DMA abort procedure */
1739 huart->hdmatx->XferAbortCallback = NULL;
1741 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1743 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1745 /* Set error code to DMA */
1746 huart->ErrorCode = HAL_UART_ERROR_DMA;
1748 return HAL_TIMEOUT;
1754 /* Reset Tx transfer counter */
1755 huart->TxXferCount = 0U;
1757 /* Flush the whole TX FIFO (if needed) */
1758 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1760 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1763 /* Restore huart->gState to Ready */
1764 huart->gState = HAL_UART_STATE_READY;
1766 return HAL_OK;
1770 * @brief Abort ongoing Receive transfer (blocking mode).
1771 * @param huart UART handle.
1772 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1773 * This procedure performs following operations :
1774 * - Disable UART Interrupts (Rx)
1775 * - Disable the DMA transfer in the peripheral register (if enabled)
1776 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1777 * - Set handle State to READY
1778 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1779 * @retval HAL status
1781 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1783 /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
1784 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
1785 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
1787 /* Disable the UART DMA Rx request if enabled */
1788 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1790 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1792 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1793 if (huart->hdmarx != NULL)
1795 /* Set the UART DMA Abort callback to Null.
1796 No call back execution at end of DMA abort procedure */
1797 huart->hdmarx->XferAbortCallback = NULL;
1799 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1801 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1803 /* Set error code to DMA */
1804 huart->ErrorCode = HAL_UART_ERROR_DMA;
1806 return HAL_TIMEOUT;
1812 /* Reset Rx transfer counter */
1813 huart->RxXferCount = 0U;
1815 /* Clear the Error flags in the ICR register */
1816 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1818 /* Discard the received data */
1819 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1821 /* Restore huart->RxState to Ready */
1822 huart->RxState = HAL_UART_STATE_READY;
1824 return HAL_OK;
1828 * @brief Abort ongoing transfers (Interrupt mode).
1829 * @param huart UART handle.
1830 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1831 * This procedure performs following operations :
1832 * - Disable UART Interrupts (Tx and Rx)
1833 * - Disable the DMA transfer in the peripheral register (if enabled)
1834 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1835 * - Set handle State to READY
1836 * - At abort completion, call user abort complete callback
1837 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1838 * considered as completed only when user abort complete callback is executed (not when exiting function).
1839 * @retval HAL status
1841 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1843 uint32_t abortcplt = 1U;
1845 /* Disable interrupts */
1846 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE | USART_CR1_TXEIE_TXFNFIE));
1847 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1849 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1850 before any call to DMA Abort functions */
1851 /* DMA Tx Handle is valid */
1852 if (huart->hdmatx != NULL)
1854 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1855 Otherwise, set it to NULL */
1856 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1858 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
1860 else
1862 huart->hdmatx->XferAbortCallback = NULL;
1865 /* DMA Rx Handle is valid */
1866 if (huart->hdmarx != NULL)
1868 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1869 Otherwise, set it to NULL */
1870 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1872 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
1874 else
1876 huart->hdmarx->XferAbortCallback = NULL;
1880 /* Disable the UART DMA Tx request if enabled */
1881 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1883 /* Disable DMA Tx at UART level */
1884 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1886 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1887 if (huart->hdmatx != NULL)
1889 /* UART Tx DMA Abort callback has already been initialised :
1890 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1892 /* Abort DMA TX */
1893 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1895 huart->hdmatx->XferAbortCallback = NULL;
1897 else
1899 abortcplt = 0U;
1904 /* Disable the UART DMA Rx request if enabled */
1905 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1907 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1909 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1910 if (huart->hdmarx != NULL)
1912 /* UART Rx DMA Abort callback has already been initialised :
1913 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1915 /* Abort DMA RX */
1916 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1918 huart->hdmarx->XferAbortCallback = NULL;
1919 abortcplt = 1U;
1921 else
1923 abortcplt = 0U;
1928 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1929 if (abortcplt == 1U)
1931 /* Reset Tx and Rx transfer counters */
1932 huart->TxXferCount = 0U;
1933 huart->RxXferCount = 0U;
1935 /* Clear ISR function pointers */
1936 huart->RxISR = NULL;
1937 huart->TxISR = NULL;
1939 /* Reset errorCode */
1940 huart->ErrorCode = HAL_UART_ERROR_NONE;
1942 /* Clear the Error flags in the ICR register */
1943 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1945 /* Flush the whole TX FIFO (if needed) */
1946 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1948 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1951 /* Discard the received data */
1952 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1954 /* Restore huart->gState and huart->RxState to Ready */
1955 huart->gState = HAL_UART_STATE_READY;
1956 huart->RxState = HAL_UART_STATE_READY;
1958 /* As no DMA to be aborted, call directly user Abort complete callback */
1959 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
1960 /* Call registered Abort complete callback */
1961 huart->AbortCpltCallback(huart);
1962 #else
1963 /* Call legacy weak Abort complete callback */
1964 HAL_UART_AbortCpltCallback(huart);
1965 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1968 return HAL_OK;
1972 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1973 * @param huart UART handle.
1974 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1975 * This procedure performs following operations :
1976 * - Disable UART Interrupts (Tx)
1977 * - Disable the DMA transfer in the peripheral register (if enabled)
1978 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1979 * - Set handle State to READY
1980 * - At abort completion, call user abort complete callback
1981 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1982 * considered as completed only when user abort complete callback is executed (not when exiting function).
1983 * @retval HAL status
1985 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
1987 /* Disable interrupts */
1988 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
1989 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1991 /* Disable the UART DMA Tx request if enabled */
1992 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1994 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1996 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1997 if (huart->hdmatx != NULL)
1999 /* Set the UART DMA Abort callback :
2000 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2001 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
2003 /* Abort DMA TX */
2004 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2006 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2007 huart->hdmatx->XferAbortCallback(huart->hdmatx);
2010 else
2012 /* Reset Tx transfer counter */
2013 huart->TxXferCount = 0U;
2015 /* Clear TxISR function pointers */
2016 huart->TxISR = NULL;
2018 /* Restore huart->gState to Ready */
2019 huart->gState = HAL_UART_STATE_READY;
2021 /* As no DMA to be aborted, call directly user Abort complete callback */
2022 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2023 /* Call registered Abort Transmit Complete Callback */
2024 huart->AbortTransmitCpltCallback(huart);
2025 #else
2026 /* Call legacy weak Abort Transmit Complete Callback */
2027 HAL_UART_AbortTransmitCpltCallback(huart);
2028 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2031 else
2033 /* Reset Tx transfer counter */
2034 huart->TxXferCount = 0U;
2036 /* Clear TxISR function pointers */
2037 huart->TxISR = NULL;
2039 /* Flush the whole TX FIFO (if needed) */
2040 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2042 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2045 /* Restore huart->gState to Ready */
2046 huart->gState = HAL_UART_STATE_READY;
2048 /* As no DMA to be aborted, call directly user Abort complete callback */
2049 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2050 /* Call registered Abort Transmit Complete Callback */
2051 huart->AbortTransmitCpltCallback(huart);
2052 #else
2053 /* Call legacy weak Abort Transmit Complete Callback */
2054 HAL_UART_AbortTransmitCpltCallback(huart);
2055 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2058 return HAL_OK;
2062 * @brief Abort ongoing Receive transfer (Interrupt mode).
2063 * @param huart UART handle.
2064 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2065 * This procedure performs following operations :
2066 * - Disable UART Interrupts (Rx)
2067 * - Disable the DMA transfer in the peripheral register (if enabled)
2068 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2069 * - Set handle State to READY
2070 * - At abort completion, call user abort complete callback
2071 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2072 * considered as completed only when user abort complete callback is executed (not when exiting function).
2073 * @retval HAL status
2075 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
2077 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2078 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
2079 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2081 /* Disable the UART DMA Rx request if enabled */
2082 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2084 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2086 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2087 if (huart->hdmarx != NULL)
2089 /* Set the UART DMA Abort callback :
2090 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2091 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
2093 /* Abort DMA RX */
2094 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2096 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2097 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2100 else
2102 /* Reset Rx transfer counter */
2103 huart->RxXferCount = 0U;
2105 /* Clear RxISR function pointer */
2106 huart->pRxBuffPtr = NULL;
2108 /* Clear the Error flags in the ICR register */
2109 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2111 /* Discard the received data */
2112 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2114 /* Restore huart->RxState to Ready */
2115 huart->RxState = HAL_UART_STATE_READY;
2117 /* As no DMA to be aborted, call directly user Abort complete callback */
2118 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2119 /* Call registered Abort Receive Complete Callback */
2120 huart->AbortReceiveCpltCallback(huart);
2121 #else
2122 /* Call legacy weak Abort Receive Complete Callback */
2123 HAL_UART_AbortReceiveCpltCallback(huart);
2124 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2127 else
2129 /* Reset Rx transfer counter */
2130 huart->RxXferCount = 0U;
2132 /* Clear RxISR function pointer */
2133 huart->pRxBuffPtr = NULL;
2135 /* Clear the Error flags in the ICR register */
2136 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2138 /* Restore huart->RxState to Ready */
2139 huart->RxState = HAL_UART_STATE_READY;
2141 /* As no DMA to be aborted, call directly user Abort complete callback */
2142 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2143 /* Call registered Abort Receive Complete Callback */
2144 huart->AbortReceiveCpltCallback(huart);
2145 #else
2146 /* Call legacy weak Abort Receive Complete Callback */
2147 HAL_UART_AbortReceiveCpltCallback(huart);
2148 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2151 return HAL_OK;
2155 * @brief Handle UART interrupt request.
2156 * @param huart UART handle.
2157 * @retval None
2159 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
2161 uint32_t isrflags = READ_REG(huart->Instance->ISR);
2162 uint32_t cr1its = READ_REG(huart->Instance->CR1);
2163 uint32_t cr3its = READ_REG(huart->Instance->CR3);
2165 uint32_t errorflags;
2166 uint32_t errorcode;
2168 /* If no error occurs */
2169 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
2170 if (errorflags == 0U)
2172 /* UART in mode Receiver ---------------------------------------------------*/
2173 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2174 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2175 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2177 if (huart->RxISR != NULL)
2179 huart->RxISR(huart);
2181 return;
2185 /* If some errors occur */
2186 if ((errorflags != 0U)
2187 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2188 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
2190 /* UART parity error interrupt occurred -------------------------------------*/
2191 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2193 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
2195 huart->ErrorCode |= HAL_UART_ERROR_PE;
2198 /* UART frame error interrupt occurred --------------------------------------*/
2199 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2201 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
2203 huart->ErrorCode |= HAL_UART_ERROR_FE;
2206 /* UART noise error interrupt occurred --------------------------------------*/
2207 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2209 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
2211 huart->ErrorCode |= HAL_UART_ERROR_NE;
2214 /* UART Over-Run interrupt occurred -----------------------------------------*/
2215 if (((isrflags & USART_ISR_ORE) != 0U)
2216 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2217 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2219 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
2221 huart->ErrorCode |= HAL_UART_ERROR_ORE;
2224 /* Call UART Error Call back function if need be --------------------------*/
2225 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
2227 /* UART in mode Receiver ---------------------------------------------------*/
2228 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2229 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2230 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2232 if (huart->RxISR != NULL)
2234 huart->RxISR(huart);
2238 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2239 consider error as blocking */
2240 errorcode = huart->ErrorCode;
2241 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
2242 ((errorcode & HAL_UART_ERROR_ORE) != 0U))
2244 /* Blocking error : transfer is aborted
2245 Set the UART state ready to be able to start again the process,
2246 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2247 UART_EndRxTransfer(huart);
2249 /* Disable the UART DMA Rx request if enabled */
2250 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2252 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2254 /* Abort the UART DMA Rx channel */
2255 if (huart->hdmarx != NULL)
2257 /* Set the UART DMA Abort callback :
2258 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2259 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
2261 /* Abort DMA RX */
2262 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2264 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2265 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2268 else
2270 /* Call user error callback */
2271 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2272 /*Call registered error callback*/
2273 huart->ErrorCallback(huart);
2274 #else
2275 /*Call legacy weak error callback*/
2276 HAL_UART_ErrorCallback(huart);
2277 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2281 else
2283 /* Call user error callback */
2284 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2285 /*Call registered error callback*/
2286 huart->ErrorCallback(huart);
2287 #else
2288 /*Call legacy weak error callback*/
2289 HAL_UART_ErrorCallback(huart);
2290 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2293 else
2295 /* Non Blocking error : transfer could go on.
2296 Error is notified to user through user error callback */
2297 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2298 /*Call registered error callback*/
2299 huart->ErrorCallback(huart);
2300 #else
2301 /*Call legacy weak error callback*/
2302 HAL_UART_ErrorCallback(huart);
2303 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2304 huart->ErrorCode = HAL_UART_ERROR_NONE;
2307 return;
2309 } /* End if some error occurs */
2311 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
2312 if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
2314 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
2316 /* UART Rx state is not reset as a reception process might be ongoing.
2317 If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
2319 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2320 /* Call registered Wakeup Callback */
2321 huart->WakeupCallback(huart);
2322 #else
2323 /* Call legacy weak Wakeup Callback */
2324 HAL_UARTEx_WakeupCallback(huart);
2325 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2326 return;
2329 /* UART in mode Transmitter ------------------------------------------------*/
2330 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2331 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2332 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2334 if (huart->TxISR != NULL)
2336 huart->TxISR(huart);
2338 return;
2341 /* UART in mode Transmitter (transmission end) -----------------------------*/
2342 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2344 UART_EndTransmit_IT(huart);
2345 return;
2348 /* UART TX Fifo Empty occurred ----------------------------------------------*/
2349 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2351 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2352 /* Call registered Tx Fifo Empty Callback */
2353 huart->TxFifoEmptyCallback(huart);
2354 #else
2355 /* Call legacy weak Tx Fifo Empty Callback */
2356 HAL_UARTEx_TxFifoEmptyCallback(huart);
2357 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2358 return;
2361 /* UART RX Fifo Full occurred ----------------------------------------------*/
2362 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2364 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2365 /* Call registered Rx Fifo Full Callback */
2366 huart->RxFifoFullCallback(huart);
2367 #else
2368 /* Call legacy weak Rx Fifo Full Callback */
2369 HAL_UARTEx_RxFifoFullCallback(huart);
2370 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2371 return;
2376 * @brief Tx Transfer completed callback.
2377 * @param huart UART handle.
2378 * @retval None
2380 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
2382 /* Prevent unused argument(s) compilation warning */
2383 UNUSED(huart);
2385 /* NOTE : This function should not be modified, when the callback is needed,
2386 the HAL_UART_TxCpltCallback can be implemented in the user file.
2391 * @brief Tx Half Transfer completed callback.
2392 * @param huart UART handle.
2393 * @retval None
2395 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
2397 /* Prevent unused argument(s) compilation warning */
2398 UNUSED(huart);
2400 /* NOTE: This function should not be modified, when the callback is needed,
2401 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2406 * @brief Rx Transfer completed callback.
2407 * @param huart UART handle.
2408 * @retval None
2410 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
2412 /* Prevent unused argument(s) compilation warning */
2413 UNUSED(huart);
2415 /* NOTE : This function should not be modified, when the callback is needed,
2416 the HAL_UART_RxCpltCallback can be implemented in the user file.
2421 * @brief Rx Half Transfer completed callback.
2422 * @param huart UART handle.
2423 * @retval None
2425 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
2427 /* Prevent unused argument(s) compilation warning */
2428 UNUSED(huart);
2430 /* NOTE: This function should not be modified, when the callback is needed,
2431 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2436 * @brief UART error callback.
2437 * @param huart UART handle.
2438 * @retval None
2440 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
2442 /* Prevent unused argument(s) compilation warning */
2443 UNUSED(huart);
2445 /* NOTE : This function should not be modified, when the callback is needed,
2446 the HAL_UART_ErrorCallback can be implemented in the user file.
2451 * @brief UART Abort Complete callback.
2452 * @param huart UART handle.
2453 * @retval None
2455 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
2457 /* Prevent unused argument(s) compilation warning */
2458 UNUSED(huart);
2460 /* NOTE : This function should not be modified, when the callback is needed,
2461 the HAL_UART_AbortCpltCallback can be implemented in the user file.
2466 * @brief UART Abort Complete callback.
2467 * @param huart UART handle.
2468 * @retval None
2470 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
2472 /* Prevent unused argument(s) compilation warning */
2473 UNUSED(huart);
2475 /* NOTE : This function should not be modified, when the callback is needed,
2476 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2481 * @brief UART Abort Receive Complete callback.
2482 * @param huart UART handle.
2483 * @retval None
2485 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
2487 /* Prevent unused argument(s) compilation warning */
2488 UNUSED(huart);
2490 /* NOTE : This function should not be modified, when the callback is needed,
2491 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2496 * @}
2499 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2500 * @brief UART control functions
2502 @verbatim
2503 ===============================================================================
2504 ##### Peripheral Control functions #####
2505 ===============================================================================
2506 [..]
2507 This subsection provides a set of functions allowing to control the UART.
2508 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2509 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2510 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2511 (+) UART_SetConfig() API configures the UART peripheral
2512 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2513 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2514 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2515 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2516 (+) HAL_LIN_SendBreak() API transmits the break characters
2517 @endverbatim
2518 * @{
2522 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
2523 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2524 * @param huart UART handle.
2525 * @retval HAL status
2527 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
2529 __HAL_LOCK(huart);
2531 huart->gState = HAL_UART_STATE_BUSY;
2533 /* Enable USART mute mode by setting the MME bit in the CR1 register */
2534 SET_BIT(huart->Instance->CR1, USART_CR1_MME);
2536 huart->gState = HAL_UART_STATE_READY;
2538 return (UART_CheckIdleState(huart));
2542 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
2543 * as it may not have been in mute mode at this very moment).
2544 * @param huart UART handle.
2545 * @retval HAL status
2547 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
2549 __HAL_LOCK(huart);
2551 huart->gState = HAL_UART_STATE_BUSY;
2553 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2554 CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
2556 huart->gState = HAL_UART_STATE_READY;
2558 return (UART_CheckIdleState(huart));
2562 * @brief Enter UART mute mode (means UART actually enters mute mode).
2563 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2564 * @param huart UART handle.
2565 * @retval None
2567 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
2569 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
2573 * @brief Enable the UART transmitter and disable the UART receiver.
2574 * @param huart UART handle.
2575 * @retval HAL status
2577 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2579 __HAL_LOCK(huart);
2580 huart->gState = HAL_UART_STATE_BUSY;
2582 /* Clear TE and RE bits */
2583 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2585 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2586 SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2588 huart->gState = HAL_UART_STATE_READY;
2590 __HAL_UNLOCK(huart);
2592 return HAL_OK;
2596 * @brief Enable the UART receiver and disable the UART transmitter.
2597 * @param huart UART handle.
2598 * @retval HAL status.
2600 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2602 __HAL_LOCK(huart);
2603 huart->gState = HAL_UART_STATE_BUSY;
2605 /* Clear TE and RE bits */
2606 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2608 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2609 SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2611 huart->gState = HAL_UART_STATE_READY;
2613 __HAL_UNLOCK(huart);
2615 return HAL_OK;
2620 * @brief Transmit break characters.
2621 * @param huart UART handle.
2622 * @retval HAL status
2624 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2626 /* Check the parameters */
2627 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
2629 __HAL_LOCK(huart);
2631 huart->gState = HAL_UART_STATE_BUSY;
2633 /* Send break characters */
2634 __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
2636 huart->gState = HAL_UART_STATE_READY;
2638 __HAL_UNLOCK(huart);
2640 return HAL_OK;
2644 * @}
2647 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2648 * @brief UART Peripheral State functions
2650 @verbatim
2651 ==============================================================================
2652 ##### Peripheral State and Error functions #####
2653 ==============================================================================
2654 [..]
2655 This subsection provides functions allowing to :
2656 (+) Return the UART handle state.
2657 (+) Return the UART handle error code
2659 @endverbatim
2660 * @{
2664 * @brief Return the UART handle state.
2665 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2666 * the configuration information for the specified UART.
2667 * @retval HAL state
2669 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
2671 uint32_t temp1;
2672 uint32_t temp2;
2673 temp1 = huart->gState;
2674 temp2 = huart->RxState;
2676 return (HAL_UART_StateTypeDef)(temp1 | temp2);
2680 * @brief Return the UART handle error code.
2681 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2682 * the configuration information for the specified UART.
2683 * @retval UART Error Code
2685 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
2687 return huart->ErrorCode;
2690 * @}
2694 * @}
2697 /** @defgroup UART_Private_Functions UART Private Functions
2698 * @{
2702 * @brief Initialize the callbacks to their default values.
2703 * @param huart UART handle.
2704 * @retval none
2706 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2707 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
2709 /* Init the UART Callback settings */
2710 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
2711 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
2712 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
2713 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
2714 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
2715 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2716 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
2717 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
2718 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
2719 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
2720 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
2723 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2726 * @brief Configure the UART peripheral.
2727 * @param huart UART handle.
2728 * @retval HAL status
2730 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
2732 uint32_t tmpreg;
2733 uint16_t brrtemp;
2734 UART_ClockSourceTypeDef clocksource;
2735 uint32_t usartdiv = 0x00000000U;
2736 HAL_StatusTypeDef ret = HAL_OK;
2737 uint32_t lpuart_ker_ck_pres = 0x00000000U;
2739 /* Check the parameters */
2740 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
2741 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
2742 if (UART_INSTANCE_LOWPOWER(huart))
2744 assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
2746 else
2748 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
2749 assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
2752 assert_param(IS_UART_PARITY(huart->Init.Parity));
2753 assert_param(IS_UART_MODE(huart->Init.Mode));
2754 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
2755 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
2756 assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
2758 /*-------------------------- USART CR1 Configuration -----------------------*/
2759 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
2760 * the UART Word Length, Parity, Mode and oversampling:
2761 * set the M bits according to huart->Init.WordLength value
2762 * set PCE and PS bits according to huart->Init.Parity value
2763 * set TE and RE bits according to huart->Init.Mode value
2764 * set OVER8 bit according to huart->Init.OverSampling value */
2765 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
2766 tmpreg |= (uint32_t)huart->FifoMode;
2767 MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2769 /*-------------------------- USART CR2 Configuration -----------------------*/
2770 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
2771 * to huart->Init.StopBits value */
2772 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
2774 /*-------------------------- USART CR3 Configuration -----------------------*/
2775 /* Configure
2776 * - UART HardWare Flow Control: set CTSE and RTSE bits according
2777 * to huart->Init.HwFlowCtl value
2778 * - one-bit sampling method versus three samples' majority rule according
2779 * to huart->Init.OneBitSampling (not applicable to LPUART) */
2780 tmpreg = (uint32_t)huart->Init.HwFlowCtl;
2782 if (!(UART_INSTANCE_LOWPOWER(huart)))
2784 tmpreg |= huart->Init.OneBitSampling;
2786 MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
2788 /*-------------------------- USART PRESC Configuration -----------------------*/
2789 /* Configure
2790 * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
2791 MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
2793 /*-------------------------- USART BRR Configuration -----------------------*/
2794 UART_GETCLOCKSOURCE(huart, clocksource);
2796 /* Check LPUART instance */
2797 if (UART_INSTANCE_LOWPOWER(huart))
2799 /* Retrieve frequency clock */
2800 switch (clocksource)
2802 case UART_CLOCKSOURCE_PCLK1:
2803 lpuart_ker_ck_pres = (HAL_RCC_GetPCLK1Freq() / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
2804 break;
2805 case UART_CLOCKSOURCE_HSI:
2806 lpuart_ker_ck_pres = ((uint32_t)HSI_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
2807 break;
2808 case UART_CLOCKSOURCE_SYSCLK:
2809 lpuart_ker_ck_pres = (HAL_RCC_GetSysClockFreq() / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
2810 break;
2811 case UART_CLOCKSOURCE_LSE:
2812 lpuart_ker_ck_pres = ((uint32_t)LSE_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
2813 break;
2814 case UART_CLOCKSOURCE_UNDEFINED:
2815 default:
2816 ret = HAL_ERROR;
2817 break;
2820 /* if proper clock source reported */
2821 if (lpuart_ker_ck_pres != 0U)
2823 /* ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
2824 if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
2825 (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
2827 ret = HAL_ERROR;
2829 else
2831 switch (clocksource)
2833 case UART_CLOCKSOURCE_PCLK1:
2834 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2835 break;
2836 case UART_CLOCKSOURCE_HSI:
2837 usartdiv = (uint32_t)(UART_DIV_LPUART(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2838 break;
2839 case UART_CLOCKSOURCE_SYSCLK:
2840 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2841 break;
2842 case UART_CLOCKSOURCE_LSE:
2843 usartdiv = (uint32_t)(UART_DIV_LPUART(LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2844 break;
2845 case UART_CLOCKSOURCE_UNDEFINED:
2846 default:
2847 ret = HAL_ERROR;
2848 break;
2851 /* It is forbidden to write values lower than 0x300 in the LPUART_BRR register */
2852 if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
2854 huart->Instance->BRR = usartdiv;
2856 else
2858 ret = HAL_ERROR;
2860 } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) || (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
2861 } /* if (lpuart_ker_ck_pres != 0) */
2863 /* Check UART Over Sampling to set Baud Rate Register */
2864 else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
2866 switch (clocksource)
2868 case UART_CLOCKSOURCE_PCLK1:
2869 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2870 break;
2871 case UART_CLOCKSOURCE_PCLK2:
2872 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2873 break;
2874 case UART_CLOCKSOURCE_HSI:
2875 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2876 break;
2877 case UART_CLOCKSOURCE_SYSCLK:
2878 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2879 break;
2880 case UART_CLOCKSOURCE_LSE:
2881 usartdiv = (uint16_t)(UART_DIV_SAMPLING8((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2882 break;
2883 case UART_CLOCKSOURCE_UNDEFINED:
2884 default:
2885 ret = HAL_ERROR;
2886 break;
2889 /* USARTDIV must be greater than or equal to 0d16 */
2890 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
2892 brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
2893 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
2894 huart->Instance->BRR = brrtemp;
2896 else
2898 ret = HAL_ERROR;
2901 else
2903 switch (clocksource)
2905 case UART_CLOCKSOURCE_PCLK1:
2906 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2907 break;
2908 case UART_CLOCKSOURCE_PCLK2:
2909 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2910 break;
2911 case UART_CLOCKSOURCE_HSI:
2912 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2913 break;
2914 case UART_CLOCKSOURCE_SYSCLK:
2915 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
2916 break;
2917 case UART_CLOCKSOURCE_LSE:
2918 usartdiv = (uint16_t)(UART_DIV_SAMPLING16((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
2919 break;
2920 case UART_CLOCKSOURCE_UNDEFINED:
2921 default:
2922 ret = HAL_ERROR;
2923 break;
2926 /* USARTDIV must be greater than or equal to 0d16 */
2927 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
2929 huart->Instance->BRR = usartdiv;
2931 else
2933 ret = HAL_ERROR;
2937 /* Initialize the number of data to process during RX/TX ISR execution */
2938 huart->NbTxDataToProcess = 1;
2939 huart->NbRxDataToProcess = 1;
2941 /* Clear ISR function pointers */
2942 huart->RxISR = NULL;
2943 huart->TxISR = NULL;
2945 return ret;
2949 * @brief Configure the UART peripheral advanced features.
2950 * @param huart UART handle.
2951 * @retval None
2953 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
2955 /* Check whether the set of advanced features to configure is properly set */
2956 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
2958 /* if required, configure TX pin active level inversion */
2959 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
2961 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
2962 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
2965 /* if required, configure RX pin active level inversion */
2966 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
2968 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
2969 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
2972 /* if required, configure data inversion */
2973 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
2975 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
2976 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
2979 /* if required, configure RX/TX pins swap */
2980 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
2982 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
2983 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
2986 /* if required, configure RX overrun detection disabling */
2987 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
2989 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
2990 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
2993 /* if required, configure DMA disabling on reception error */
2994 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
2996 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
2997 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
3000 /* if required, configure auto Baud rate detection scheme */
3001 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
3003 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
3004 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
3005 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
3006 /* set auto Baudrate detection parameters if detection is enabled */
3007 if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
3009 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
3010 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
3014 /* if required, configure MSB first on communication line */
3015 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
3017 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
3018 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
3023 * @brief Check the UART Idle State.
3024 * @param huart UART handle.
3025 * @retval HAL status
3027 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
3029 uint32_t tickstart;
3031 /* Initialize the UART ErrorCode */
3032 huart->ErrorCode = HAL_UART_ERROR_NONE;
3034 /* Init tickstart for timeout managment*/
3035 tickstart = HAL_GetTick();
3037 /* Check if the Transmitter is enabled */
3038 if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
3040 /* Wait until TEACK flag is set */
3041 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3043 /* Timeout occurred */
3044 return HAL_TIMEOUT;
3048 /* Check if the Receiver is enabled */
3049 if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
3051 /* Wait until REACK flag is set */
3052 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3054 /* Timeout occurred */
3055 return HAL_TIMEOUT;
3059 /* Initialize the UART State */
3060 huart->gState = HAL_UART_STATE_READY;
3061 huart->RxState = HAL_UART_STATE_READY;
3063 __HAL_UNLOCK(huart);
3065 return HAL_OK;
3069 * @brief Handle UART Communication Timeout.
3070 * @param huart UART handle.
3071 * @param Flag Specifies the UART flag to check
3072 * @param Status Flag status (SET or RESET)
3073 * @param Tickstart Tick start value
3074 * @param Timeout Timeout duration
3075 * @retval HAL status
3077 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
3078 uint32_t Tickstart, uint32_t Timeout)
3080 /* Wait until flag is set */
3081 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
3083 /* Check for the Timeout */
3084 if (Timeout != HAL_MAX_DELAY)
3086 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
3088 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
3089 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
3090 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3092 huart->gState = HAL_UART_STATE_READY;
3093 huart->RxState = HAL_UART_STATE_READY;
3095 __HAL_UNLOCK(huart);
3097 return HAL_TIMEOUT;
3101 return HAL_OK;
3106 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3107 * @param huart UART handle.
3108 * @retval None
3110 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
3112 /* Disable TXEIE, TCIE, TXFT interrupts */
3113 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
3114 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
3116 /* At end of Tx process, restore huart->gState to Ready */
3117 huart->gState = HAL_UART_STATE_READY;
3122 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3123 * @param huart UART handle.
3124 * @retval None
3126 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
3128 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3129 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3130 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3132 /* At end of Rx process, restore huart->RxState to Ready */
3133 huart->RxState = HAL_UART_STATE_READY;
3135 /* Reset RxIsr function pointer */
3136 huart->RxISR = NULL;
3141 * @brief DMA UART transmit process complete callback.
3142 * @param hdma DMA handle.
3143 * @retval None
3145 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3147 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3149 /* DMA Normal mode */
3150 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
3152 huart->TxXferCount = 0U;
3154 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
3155 in the UART CR3 register */
3156 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
3158 /* Enable the UART Transmit Complete Interrupt */
3159 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3161 /* DMA Circular mode */
3162 else
3164 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3165 /*Call registered Tx complete callback*/
3166 huart->TxCpltCallback(huart);
3167 #else
3168 /*Call legacy weak Tx complete callback*/
3169 HAL_UART_TxCpltCallback(huart);
3170 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3175 * @brief DMA UART transmit process half complete callback.
3176 * @param hdma DMA handle.
3177 * @retval None
3179 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
3181 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3183 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3184 /*Call registered Tx Half complete callback*/
3185 huart->TxHalfCpltCallback(huart);
3186 #else
3187 /*Call legacy weak Tx Half complete callback*/
3188 HAL_UART_TxHalfCpltCallback(huart);
3189 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3193 * @brief DMA UART receive process complete callback.
3194 * @param hdma DMA handle.
3195 * @retval None
3197 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3199 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3201 /* DMA Normal mode */
3202 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
3204 huart->RxXferCount = 0U;
3206 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3207 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3208 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3210 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3211 in the UART CR3 register */
3212 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3214 /* At end of Rx process, restore huart->RxState to Ready */
3215 huart->RxState = HAL_UART_STATE_READY;
3218 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3219 /*Call registered Rx complete callback*/
3220 huart->RxCpltCallback(huart);
3221 #else
3222 /*Call legacy weak Rx complete callback*/
3223 HAL_UART_RxCpltCallback(huart);
3224 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3228 * @brief DMA UART receive process half complete callback.
3229 * @param hdma DMA handle.
3230 * @retval None
3232 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
3234 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3236 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3237 /*Call registered Rx Half complete callback*/
3238 huart->RxHalfCpltCallback(huart);
3239 #else
3240 /*Call legacy weak Rx Half complete callback*/
3241 HAL_UART_RxHalfCpltCallback(huart);
3242 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3246 * @brief DMA UART communication error callback.
3247 * @param hdma DMA handle.
3248 * @retval None
3250 static void UART_DMAError(DMA_HandleTypeDef *hdma)
3252 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3254 const HAL_UART_StateTypeDef gstate = huart->gState;
3255 const HAL_UART_StateTypeDef rxstate = huart->RxState;
3257 /* Stop UART DMA Tx request if ongoing */
3258 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
3259 (gstate == HAL_UART_STATE_BUSY_TX))
3261 huart->TxXferCount = 0U;
3262 UART_EndTxTransfer(huart);
3265 /* Stop UART DMA Rx request if ongoing */
3266 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
3267 (rxstate == HAL_UART_STATE_BUSY_RX))
3269 huart->RxXferCount = 0U;
3270 UART_EndRxTransfer(huart);
3273 huart->ErrorCode |= HAL_UART_ERROR_DMA;
3275 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3276 /*Call registered error callback*/
3277 huart->ErrorCallback(huart);
3278 #else
3279 /*Call legacy weak error callback*/
3280 HAL_UART_ErrorCallback(huart);
3281 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3285 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
3286 * (To be called at end of DMA Abort procedure following error occurrence).
3287 * @param hdma DMA handle.
3288 * @retval None
3290 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3292 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3293 huart->RxXferCount = 0U;
3294 huart->TxXferCount = 0U;
3296 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3297 /*Call registered error callback*/
3298 huart->ErrorCallback(huart);
3299 #else
3300 /*Call legacy weak error callback*/
3301 HAL_UART_ErrorCallback(huart);
3302 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3306 * @brief DMA UART Tx communication abort callback, when initiated by user
3307 * (To be called at end of DMA Tx Abort procedure following user abort request).
3308 * @note When this callback is executed, User Abort complete call back is called only if no
3309 * Abort still ongoing for Rx DMA Handle.
3310 * @param hdma DMA handle.
3311 * @retval None
3313 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3315 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3317 huart->hdmatx->XferAbortCallback = NULL;
3319 /* Check if an Abort process is still ongoing */
3320 if (huart->hdmarx != NULL)
3322 if (huart->hdmarx->XferAbortCallback != NULL)
3324 return;
3328 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3329 huart->TxXferCount = 0U;
3330 huart->RxXferCount = 0U;
3332 /* Reset errorCode */
3333 huart->ErrorCode = HAL_UART_ERROR_NONE;
3335 /* Clear the Error flags in the ICR register */
3336 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3338 /* Flush the whole TX FIFO (if needed) */
3339 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3341 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3344 /* Restore huart->gState and huart->RxState to Ready */
3345 huart->gState = HAL_UART_STATE_READY;
3346 huart->RxState = HAL_UART_STATE_READY;
3348 /* Call user Abort complete callback */
3349 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3350 /* Call registered Abort complete callback */
3351 huart->AbortCpltCallback(huart);
3352 #else
3353 /* Call legacy weak Abort complete callback */
3354 HAL_UART_AbortCpltCallback(huart);
3355 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3360 * @brief DMA UART Rx communication abort callback, when initiated by user
3361 * (To be called at end of DMA Rx Abort procedure following user abort request).
3362 * @note When this callback is executed, User Abort complete call back is called only if no
3363 * Abort still ongoing for Tx DMA Handle.
3364 * @param hdma DMA handle.
3365 * @retval None
3367 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3369 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3371 huart->hdmarx->XferAbortCallback = NULL;
3373 /* Check if an Abort process is still ongoing */
3374 if (huart->hdmatx != NULL)
3376 if (huart->hdmatx->XferAbortCallback != NULL)
3378 return;
3382 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3383 huart->TxXferCount = 0U;
3384 huart->RxXferCount = 0U;
3386 /* Reset errorCode */
3387 huart->ErrorCode = HAL_UART_ERROR_NONE;
3389 /* Clear the Error flags in the ICR register */
3390 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3392 /* Discard the received data */
3393 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3395 /* Restore huart->gState and huart->RxState to Ready */
3396 huart->gState = HAL_UART_STATE_READY;
3397 huart->RxState = HAL_UART_STATE_READY;
3399 /* Call user Abort complete callback */
3400 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3401 /* Call registered Abort complete callback */
3402 huart->AbortCpltCallback(huart);
3403 #else
3404 /* Call legacy weak Abort complete callback */
3405 HAL_UART_AbortCpltCallback(huart);
3406 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3411 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
3412 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3413 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3414 * and leads to user Tx Abort Complete callback execution).
3415 * @param hdma DMA handle.
3416 * @retval None
3418 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3420 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3422 huart->TxXferCount = 0U;
3424 /* Flush the whole TX FIFO (if needed) */
3425 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3427 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3430 /* Restore huart->gState to Ready */
3431 huart->gState = HAL_UART_STATE_READY;
3433 /* Call user Abort complete callback */
3434 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3435 /* Call registered Abort Transmit Complete Callback */
3436 huart->AbortTransmitCpltCallback(huart);
3437 #else
3438 /* Call legacy weak Abort Transmit Complete Callback */
3439 HAL_UART_AbortTransmitCpltCallback(huart);
3440 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3444 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
3445 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
3446 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3447 * and leads to user Rx Abort Complete callback execution).
3448 * @param hdma DMA handle.
3449 * @retval None
3451 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3453 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3455 huart->RxXferCount = 0U;
3457 /* Clear the Error flags in the ICR register */
3458 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3460 /* Discard the received data */
3461 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3463 /* Restore huart->RxState to Ready */
3464 huart->RxState = HAL_UART_STATE_READY;
3466 /* Call user Abort complete callback */
3467 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3468 /* Call registered Abort Receive Complete Callback */
3469 huart->AbortReceiveCpltCallback(huart);
3470 #else
3471 /* Call legacy weak Abort Receive Complete Callback */
3472 HAL_UART_AbortReceiveCpltCallback(huart);
3473 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3477 * @brief TX interrrupt handler for 7 or 8 bits data word length .
3478 * @note Function is called under interruption only, once
3479 * interruptions have been enabled by HAL_UART_Transmit_IT().
3480 * @param huart UART handle.
3481 * @retval None
3483 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
3485 /* Check that a Tx process is ongoing */
3486 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3488 if (huart->TxXferCount == 0U)
3490 /* Disable the UART Transmit Data Register Empty Interrupt */
3491 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3493 /* Enable the UART Transmit Complete Interrupt */
3494 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3496 else
3498 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
3499 huart->pTxBuffPtr++;
3500 huart->TxXferCount--;
3506 * @brief TX interrrupt handler for 9 bits data word length.
3507 * @note Function is called under interruption only, once
3508 * interruptions have been enabled by HAL_UART_Transmit_IT().
3509 * @param huart UART handle.
3510 * @retval None
3512 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
3514 uint16_t *tmp;
3516 /* Check that a Tx process is ongoing */
3517 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3519 if (huart->TxXferCount == 0U)
3521 /* Disable the UART Transmit Data Register Empty Interrupt */
3522 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3524 /* Enable the UART Transmit Complete Interrupt */
3525 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3527 else
3529 tmp = (uint16_t *) huart->pTxBuffPtr;
3530 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
3531 huart->pTxBuffPtr += 2U;
3532 huart->TxXferCount--;
3538 * @brief TX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
3539 * @note Function is called under interruption only, once
3540 * interruptions have been enabled by HAL_UART_Transmit_IT().
3541 * @param huart UART handle.
3542 * @retval None
3544 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
3546 uint16_t nb_tx_data;
3548 /* Check that a Tx process is ongoing */
3549 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3551 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3553 if (huart->TxXferCount == 0U)
3555 /* Disable the TX FIFO threshold interrupt */
3556 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
3558 /* Enable the UART Transmit Complete Interrupt */
3559 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3561 break; /* force exit loop */
3563 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
3565 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
3566 huart->pTxBuffPtr++;
3567 huart->TxXferCount--;
3569 else
3571 /* Nothing to do */
3578 * @brief TX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
3579 * @note Function is called under interruption only, once
3580 * interruptions have been enabled by HAL_UART_Transmit_IT().
3581 * @param huart UART handle.
3582 * @retval None
3584 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
3586 uint16_t *tmp;
3587 uint16_t nb_tx_data;
3589 /* Check that a Tx process is ongoing */
3590 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3592 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3594 if (huart->TxXferCount == 0U)
3596 /* Disable the TX FIFO threshold interrupt */
3597 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
3599 /* Enable the UART Transmit Complete Interrupt */
3600 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3602 break; /* force exit loop */
3604 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
3606 tmp = (uint16_t *) huart->pTxBuffPtr;
3607 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
3608 huart->pTxBuffPtr += 2U;
3609 huart->TxXferCount--;
3611 else
3613 /* Nothing to do */
3620 * @brief Wrap up transmission in non-blocking mode.
3621 * @param huart pointer to a UART_HandleTypeDef structure that contains
3622 * the configuration information for the specified UART module.
3623 * @retval None
3625 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
3627 /* Disable the UART Transmit Complete Interrupt */
3628 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3630 /* Tx process is ended, restore huart->gState to Ready */
3631 huart->gState = HAL_UART_STATE_READY;
3633 /* Cleat TxISR function pointer */
3634 huart->TxISR = NULL;
3636 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3637 /*Call registered Tx complete callback*/
3638 huart->TxCpltCallback(huart);
3639 #else
3640 /*Call legacy weak Tx complete callback*/
3641 HAL_UART_TxCpltCallback(huart);
3642 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3646 * @brief RX interrrupt handler for 7 or 8 bits data word length .
3647 * @param huart UART handle.
3648 * @retval None
3650 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
3652 uint16_t uhMask = huart->Mask;
3653 uint16_t uhdata;
3655 /* Check that a Rx process is ongoing */
3656 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
3658 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
3659 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
3660 huart->pRxBuffPtr++;
3661 huart->RxXferCount--;
3663 if (huart->RxXferCount == 0U)
3665 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
3666 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3668 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3669 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3671 /* Rx process is completed, restore huart->RxState to Ready */
3672 huart->RxState = HAL_UART_STATE_READY;
3674 /* Clear RxISR function pointer */
3675 huart->RxISR = NULL;
3677 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3678 /*Call registered Rx complete callback*/
3679 huart->RxCpltCallback(huart);
3680 #else
3681 /*Call legacy weak Rx complete callback*/
3682 HAL_UART_RxCpltCallback(huart);
3683 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3686 else
3688 /* Clear RXNE interrupt flag */
3689 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3694 * @brief RX interrrupt handler for 9 bits data word length .
3695 * @note Function is called under interruption only, once
3696 * interruptions have been enabled by HAL_UART_Receive_IT()
3697 * @param huart UART handle.
3698 * @retval None
3700 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
3702 uint16_t *tmp;
3703 uint16_t uhMask = huart->Mask;
3704 uint16_t uhdata;
3706 /* Check that a Rx process is ongoing */
3707 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
3709 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
3710 tmp = (uint16_t *) huart->pRxBuffPtr ;
3711 *tmp = (uint16_t)(uhdata & uhMask);
3712 huart->pRxBuffPtr += 2U;
3713 huart->RxXferCount--;
3715 if (huart->RxXferCount == 0U)
3717 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
3718 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3720 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3721 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3723 /* Rx process is completed, restore huart->RxState to Ready */
3724 huart->RxState = HAL_UART_STATE_READY;
3726 /* Clear RxISR function pointer */
3727 huart->RxISR = NULL;
3729 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3730 /*Call registered Rx complete callback*/
3731 huart->RxCpltCallback(huart);
3732 #else
3733 /*Call legacy weak Rx complete callback*/
3734 HAL_UART_RxCpltCallback(huart);
3735 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3738 else
3740 /* Clear RXNE interrupt flag */
3741 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3746 * @brief RX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
3747 * @note Function is called under interruption only, once
3748 * interruptions have been enabled by HAL_UART_Receive_IT()
3749 * @param huart UART handle.
3750 * @retval None
3752 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
3754 uint16_t uhMask = huart->Mask;
3755 uint16_t uhdata;
3756 uint16_t nb_rx_data;
3757 uint16_t rxdatacount;
3759 /* Check that a Rx process is ongoing */
3760 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
3762 for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3764 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
3765 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
3766 huart->pRxBuffPtr++;
3767 huart->RxXferCount--;
3769 if (huart->RxXferCount == 0U)
3771 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
3772 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3774 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3775 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3777 /* Rx process is completed, restore huart->RxState to Ready */
3778 huart->RxState = HAL_UART_STATE_READY;
3780 /* Clear RxISR function pointer */
3781 huart->RxISR = NULL;
3783 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3784 /*Call registered Rx complete callback*/
3785 huart->RxCpltCallback(huart);
3786 #else
3787 /*Call legacy weak Rx complete callback*/
3788 HAL_UART_RxCpltCallback(huart);
3789 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3793 /* When remaining number of bytes to receive is less than the RX FIFO
3794 threshold, next incoming frames are processed as if FIFO mode was
3795 disabled (i.e. one interrupt per received frame).
3797 rxdatacount = huart->RxXferCount;
3798 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
3800 /* Disable the UART RXFT interrupt*/
3801 CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
3803 /* Update the RxISR function pointer */
3804 huart->RxISR = UART_RxISR_8BIT;
3806 /* Enable the UART Data Register Not Empty interrupt */
3807 SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3810 else
3812 /* Clear RXNE interrupt flag */
3813 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3818 * @brief RX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
3819 * @note Function is called under interruption only, once
3820 * interruptions have been enabled by HAL_UART_Receive_IT()
3821 * @param huart UART handle.
3822 * @retval None
3824 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
3826 uint16_t *tmp;
3827 uint16_t uhMask = huart->Mask;
3828 uint16_t uhdata;
3829 uint16_t nb_rx_data;
3830 uint16_t rxdatacount;
3832 /* Check that a Rx process is ongoing */
3833 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
3835 for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3837 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
3838 tmp = (uint16_t *) huart->pRxBuffPtr ;
3839 *tmp = (uint16_t)(uhdata & uhMask);
3840 huart->pRxBuffPtr += 2U;
3841 huart->RxXferCount--;
3843 if (huart->RxXferCount == 0U)
3845 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
3846 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3848 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3849 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3851 /* Rx process is completed, restore huart->RxState to Ready */
3852 huart->RxState = HAL_UART_STATE_READY;
3854 /* Clear RxISR function pointer */
3855 huart->RxISR = NULL;
3857 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3858 /*Call registered Rx complete callback*/
3859 huart->RxCpltCallback(huart);
3860 #else
3861 /*Call legacy weak Rx complete callback*/
3862 HAL_UART_RxCpltCallback(huart);
3863 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3867 /* When remaining number of bytes to receive is less than the RX FIFO
3868 threshold, next incoming frames are processed as if FIFO mode was
3869 disabled (i.e. one interrupt per received frame).
3871 rxdatacount = huart->RxXferCount;
3872 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
3874 /* Disable the UART RXFT interrupt*/
3875 CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
3877 /* Update the RxISR function pointer */
3878 huart->RxISR = UART_RxISR_16BIT;
3880 /* Enable the UART Data Register Not Empty interrupt */
3881 SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3884 else
3886 /* Clear RXNE interrupt flag */
3887 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3892 * @}
3895 #endif /* HAL_UART_MODULE_ENABLED */
3897 * @}
3901 * @}
3904 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/