2 ******************************************************************************
3 * @file stm32h7xx_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
14 ===============================================================================
15 ##### How to use this driver #####
16 ===============================================================================
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.
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 ==================================
74 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
75 allows the user to configure dynamically the driver callbacks.
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.
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,
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.
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).
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.
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.
142 ******************************************************************************
145 * <h2><center>© 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 "stm32h7xx_hal.h"
159 /** @addtogroup STM32H7xx_HAL_Driver
163 /** @defgroup UART UART
164 * @brief HAL UART module driver
168 #ifdef HAL_UART_MODULE_ENABLED
170 /* Private typedef -----------------------------------------------------------*/
171 /* Private define ------------------------------------------------------------*/
172 /** @defgroup UART_Private_Constants UART Private Constants
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 */
192 /* Private macros ------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 const uint16_t UARTPrescTable
[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
196 /* Private function prototypes -----------------------------------------------*/
197 /** @addtogroup UART_Private_Functions
200 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
);
201 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
);
202 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
203 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
204 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
205 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
206 static void UART_DMAError(DMA_HandleTypeDef
*hdma
);
207 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
208 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
209 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
210 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
211 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
212 static void UART_TxISR_8BIT(UART_HandleTypeDef
*huart
);
213 static void UART_TxISR_16BIT(UART_HandleTypeDef
*huart
);
214 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef
*huart
);
215 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef
*huart
);
216 static void UART_EndTransmit_IT(UART_HandleTypeDef
*huart
);
217 static void UART_RxISR_8BIT(UART_HandleTypeDef
*huart
);
218 static void UART_RxISR_16BIT(UART_HandleTypeDef
*huart
);
219 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef
*huart
);
220 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef
*huart
);
225 /* Exported functions --------------------------------------------------------*/
227 /** @defgroup UART_Exported_Functions UART Exported Functions
231 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
232 * @brief Initialization and Configuration functions
235 ===============================================================================
236 ##### Initialization and Configuration functions #####
237 ===============================================================================
239 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
240 in asynchronous mode.
241 (+) For the asynchronous mode the parameters below can be configured:
245 (++) Parity: If the parity is enabled, then the MSB bit of the data written
246 in the data register is transmitted but is changed by the parity bit.
247 (++) Hardware flow control
248 (++) Receiver/transmitter modes
249 (++) Over Sampling Method
250 (++) One-Bit Sampling Method
251 (+) For the asynchronous mode, the following advanced features can be configured as well:
252 (++) TX and/or RX pin level inversion
253 (++) data logical level inversion
254 (++) RX and TX pins swap
255 (++) RX overrun detection disabling
256 (++) DMA disabling on RX error
257 (++) MSB first on communication line
258 (++) auto Baud rate detection
260 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
261 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
262 and UART multiprocessor mode configuration procedures (details for the procedures
263 are available in reference manual).
267 Depending on the frame length defined by the M1 and M0 bits (7-bit,
268 8-bit or 9-bit), the possible UART formats are listed in the
271 Table 1. UART frame format.
272 +-----------------------------------------------------------------------+
273 | M1 bit | M0 bit | PCE bit | UART frame |
274 |---------|---------|-----------|---------------------------------------|
275 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
276 |---------|---------|-----------|---------------------------------------|
277 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
278 |---------|---------|-----------|---------------------------------------|
279 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
280 |---------|---------|-----------|---------------------------------------|
281 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
282 |---------|---------|-----------|---------------------------------------|
283 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
284 |---------|---------|-----------|---------------------------------------|
285 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
286 +-----------------------------------------------------------------------+
292 * @brief Initialize the UART mode according to the specified
293 * parameters in the UART_InitTypeDef and initialize the associated handle.
294 * @param huart UART handle.
297 HAL_StatusTypeDef
HAL_UART_Init(UART_HandleTypeDef
*huart
)
299 /* Check the UART handle allocation */
305 if (huart
->Init
.HwFlowCtl
!= UART_HWCONTROL_NONE
)
307 /* Check the parameters */
308 assert_param(IS_UART_HWFLOW_INSTANCE(huart
->Instance
));
312 /* Check the parameters */
313 assert_param((IS_UART_INSTANCE(huart
->Instance
)) || (IS_LPUART_INSTANCE(huart
->Instance
)));
316 if (huart
->gState
== HAL_UART_STATE_RESET
)
318 /* Allocate lock resource and initialize it */
319 huart
->Lock
= HAL_UNLOCKED
;
321 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
322 UART_InitCallbacksToDefault(huart
);
324 if (huart
->MspInitCallback
== NULL
)
326 huart
->MspInitCallback
= HAL_UART_MspInit
;
329 /* Init the low level hardware */
330 huart
->MspInitCallback(huart
);
332 /* Init the low level hardware : GPIO, CLOCK */
333 HAL_UART_MspInit(huart
);
334 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
337 huart
->gState
= HAL_UART_STATE_BUSY
;
339 __HAL_UART_DISABLE(huart
);
341 /* Set the UART Communication parameters */
342 if (UART_SetConfig(huart
) == HAL_ERROR
)
347 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
349 UART_AdvFeatureConfig(huart
);
352 /* In asynchronous mode, the following bits must be kept cleared:
353 - LINEN and CLKEN bits in the USART_CR2 register,
354 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
355 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
356 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
358 __HAL_UART_ENABLE(huart
);
360 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
361 return (UART_CheckIdleState(huart
));
365 * @brief Initialize the half-duplex mode according to the specified
366 * parameters in the UART_InitTypeDef and creates the associated handle.
367 * @param huart UART handle.
370 HAL_StatusTypeDef
HAL_HalfDuplex_Init(UART_HandleTypeDef
*huart
)
372 /* Check the UART handle allocation */
378 /* Check UART instance */
379 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart
->Instance
));
381 if (huart
->gState
== HAL_UART_STATE_RESET
)
383 /* Allocate lock resource and initialize it */
384 huart
->Lock
= HAL_UNLOCKED
;
386 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
387 UART_InitCallbacksToDefault(huart
);
389 if (huart
->MspInitCallback
== NULL
)
391 huart
->MspInitCallback
= HAL_UART_MspInit
;
394 /* Init the low level hardware */
395 huart
->MspInitCallback(huart
);
397 /* Init the low level hardware : GPIO, CLOCK */
398 HAL_UART_MspInit(huart
);
399 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
402 huart
->gState
= HAL_UART_STATE_BUSY
;
404 __HAL_UART_DISABLE(huart
);
406 /* Set the UART Communication parameters */
407 if (UART_SetConfig(huart
) == HAL_ERROR
)
412 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
414 UART_AdvFeatureConfig(huart
);
417 /* In half-duplex mode, the following bits must be kept cleared:
418 - LINEN and CLKEN bits in the USART_CR2 register,
419 - SCEN and IREN bits in the USART_CR3 register.*/
420 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
421 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_SCEN
));
423 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
424 SET_BIT(huart
->Instance
->CR3
, USART_CR3_HDSEL
);
426 __HAL_UART_ENABLE(huart
);
428 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
429 return (UART_CheckIdleState(huart
));
434 * @brief Initialize the LIN mode according to the specified
435 * parameters in the UART_InitTypeDef and creates the associated handle.
436 * @param huart UART handle.
437 * @param BreakDetectLength Specifies the LIN break detection length.
438 * This parameter can be one of the following values:
439 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
440 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
443 HAL_StatusTypeDef
HAL_LIN_Init(UART_HandleTypeDef
*huart
, uint32_t BreakDetectLength
)
445 /* Check the UART handle allocation */
451 /* Check the LIN UART instance */
452 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
453 /* Check the Break detection length parameter */
454 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength
));
456 /* LIN mode limited to 16-bit oversampling only */
457 if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
461 /* LIN mode limited to 8-bit data length */
462 if (huart
->Init
.WordLength
!= UART_WORDLENGTH_8B
)
467 if (huart
->gState
== HAL_UART_STATE_RESET
)
469 /* Allocate lock resource and initialize it */
470 huart
->Lock
= HAL_UNLOCKED
;
472 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
473 UART_InitCallbacksToDefault(huart
);
475 if (huart
->MspInitCallback
== NULL
)
477 huart
->MspInitCallback
= HAL_UART_MspInit
;
480 /* Init the low level hardware */
481 huart
->MspInitCallback(huart
);
483 /* Init the low level hardware : GPIO, CLOCK */
484 HAL_UART_MspInit(huart
);
485 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
488 huart
->gState
= HAL_UART_STATE_BUSY
;
490 __HAL_UART_DISABLE(huart
);
492 /* Set the UART Communication parameters */
493 if (UART_SetConfig(huart
) == HAL_ERROR
)
498 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
500 UART_AdvFeatureConfig(huart
);
503 /* In LIN mode, the following bits must be kept cleared:
504 - LINEN and CLKEN bits in the USART_CR2 register,
505 - SCEN and IREN bits in the USART_CR3 register.*/
506 CLEAR_BIT(huart
->Instance
->CR2
, USART_CR2_CLKEN
);
507 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
| USART_CR3_SCEN
));
509 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
510 SET_BIT(huart
->Instance
->CR2
, USART_CR2_LINEN
);
512 /* Set the USART LIN Break detection length. */
513 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_LBDL
, BreakDetectLength
);
515 __HAL_UART_ENABLE(huart
);
517 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
518 return (UART_CheckIdleState(huart
));
523 * @brief Initialize the multiprocessor mode according to the specified
524 * parameters in the UART_InitTypeDef and initialize the associated handle.
525 * @param huart UART handle.
526 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
527 * @param WakeUpMethod Specifies the UART wakeup method.
528 * This parameter can be one of the following values:
529 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
530 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
531 * @note If the user resorts to idle line detection wake up, the Address parameter
532 * is useless and ignored by the initialization function.
533 * @note If the user resorts to address mark wake up, the address length detection
534 * is configured by default to 4 bits only. For the UART to be able to
535 * manage 6-, 7- or 8-bit long addresses detection, the API
536 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
537 * HAL_MultiProcessor_Init().
540 HAL_StatusTypeDef
HAL_MultiProcessor_Init(UART_HandleTypeDef
*huart
, uint8_t Address
, uint32_t WakeUpMethod
)
542 /* Check the UART handle allocation */
548 /* Check the wake up method parameter */
549 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod
));
551 if (huart
->gState
== HAL_UART_STATE_RESET
)
553 /* Allocate lock resource and initialize it */
554 huart
->Lock
= HAL_UNLOCKED
;
556 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
557 UART_InitCallbacksToDefault(huart
);
559 if (huart
->MspInitCallback
== NULL
)
561 huart
->MspInitCallback
= HAL_UART_MspInit
;
564 /* Init the low level hardware */
565 huart
->MspInitCallback(huart
);
567 /* Init the low level hardware : GPIO, CLOCK */
568 HAL_UART_MspInit(huart
);
569 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
572 huart
->gState
= HAL_UART_STATE_BUSY
;
574 __HAL_UART_DISABLE(huart
);
576 /* Set the UART Communication parameters */
577 if (UART_SetConfig(huart
) == HAL_ERROR
)
582 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
584 UART_AdvFeatureConfig(huart
);
587 /* In multiprocessor mode, the following bits must be kept cleared:
588 - LINEN and CLKEN bits in the USART_CR2 register,
589 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
590 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
591 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
593 if (WakeUpMethod
== UART_WAKEUPMETHOD_ADDRESSMARK
)
595 /* If address mark wake up method is chosen, set the USART address node */
596 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)Address
<< UART_CR2_ADDRESS_LSB_POS
));
599 /* Set the wake up method by setting the WAKE bit in the CR1 register */
600 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_WAKE
, WakeUpMethod
);
602 __HAL_UART_ENABLE(huart
);
604 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
605 return (UART_CheckIdleState(huart
));
610 * @brief DeInitialize the UART peripheral.
611 * @param huart UART handle.
614 HAL_StatusTypeDef
HAL_UART_DeInit(UART_HandleTypeDef
*huart
)
616 /* Check the UART handle allocation */
622 /* Check the parameters */
623 assert_param((IS_UART_INSTANCE(huart
->Instance
)) || (IS_LPUART_INSTANCE(huart
->Instance
)));
625 huart
->gState
= HAL_UART_STATE_BUSY
;
627 __HAL_UART_DISABLE(huart
);
629 huart
->Instance
->CR1
= 0x0U
;
630 huart
->Instance
->CR2
= 0x0U
;
631 huart
->Instance
->CR3
= 0x0U
;
633 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
634 if (huart
->MspDeInitCallback
== NULL
)
636 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
;
638 /* DeInit the low level hardware */
639 huart
->MspDeInitCallback(huart
);
641 /* DeInit the low level hardware */
642 HAL_UART_MspDeInit(huart
);
643 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
645 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
646 huart
->gState
= HAL_UART_STATE_RESET
;
647 huart
->RxState
= HAL_UART_STATE_RESET
;
655 * @brief Initialize the UART MSP.
656 * @param huart UART handle.
659 __weak
void HAL_UART_MspInit(UART_HandleTypeDef
*huart
)
661 /* Prevent unused argument(s) compilation warning */
664 /* NOTE : This function should not be modified, when the callback is needed,
665 the HAL_UART_MspInit can be implemented in the user file
670 * @brief DeInitialize the UART MSP.
671 * @param huart UART handle.
674 __weak
void HAL_UART_MspDeInit(UART_HandleTypeDef
*huart
)
676 /* Prevent unused argument(s) compilation warning */
679 /* NOTE : This function should not be modified, when the callback is needed,
680 the HAL_UART_MspDeInit can be implemented in the user file
684 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
686 * @brief Register a User UART Callback
687 * To be used instead of the weak predefined callback
688 * @param huart uart handle
689 * @param CallbackID ID of the callback to be registered
690 * This parameter can be one of the following values:
691 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
692 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
693 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
694 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
695 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
696 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
697 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
698 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
699 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
700 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
701 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
702 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
703 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
704 * @param pCallback pointer to the Callback function
707 HAL_StatusTypeDef
HAL_UART_RegisterCallback(UART_HandleTypeDef
*huart
, HAL_UART_CallbackIDTypeDef CallbackID
,
708 pUART_CallbackTypeDef pCallback
)
710 HAL_StatusTypeDef status
= HAL_OK
;
712 if (pCallback
== NULL
)
714 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
721 if (huart
->gState
== HAL_UART_STATE_READY
)
725 case HAL_UART_TX_HALFCOMPLETE_CB_ID
:
726 huart
->TxHalfCpltCallback
= pCallback
;
729 case HAL_UART_TX_COMPLETE_CB_ID
:
730 huart
->TxCpltCallback
= pCallback
;
733 case HAL_UART_RX_HALFCOMPLETE_CB_ID
:
734 huart
->RxHalfCpltCallback
= pCallback
;
737 case HAL_UART_RX_COMPLETE_CB_ID
:
738 huart
->RxCpltCallback
= pCallback
;
741 case HAL_UART_ERROR_CB_ID
:
742 huart
->ErrorCallback
= pCallback
;
745 case HAL_UART_ABORT_COMPLETE_CB_ID
:
746 huart
->AbortCpltCallback
= pCallback
;
749 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID
:
750 huart
->AbortTransmitCpltCallback
= pCallback
;
753 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID
:
754 huart
->AbortReceiveCpltCallback
= pCallback
;
757 case HAL_UART_WAKEUP_CB_ID
:
758 huart
->WakeupCallback
= pCallback
;
761 case HAL_UART_RX_FIFO_FULL_CB_ID
:
762 huart
->RxFifoFullCallback
= pCallback
;
765 case HAL_UART_TX_FIFO_EMPTY_CB_ID
:
766 huart
->TxFifoEmptyCallback
= pCallback
;
769 case HAL_UART_MSPINIT_CB_ID
:
770 huart
->MspInitCallback
= pCallback
;
773 case HAL_UART_MSPDEINIT_CB_ID
:
774 huart
->MspDeInitCallback
= pCallback
;
778 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
784 else if (huart
->gState
== HAL_UART_STATE_RESET
)
788 case HAL_UART_MSPINIT_CB_ID
:
789 huart
->MspInitCallback
= pCallback
;
792 case HAL_UART_MSPDEINIT_CB_ID
:
793 huart
->MspDeInitCallback
= pCallback
;
797 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
805 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
816 * @brief Unregister an UART Callback
817 * UART callaback is redirected to the weak predefined callback
818 * @param huart uart handle
819 * @param CallbackID ID of the callback to be unregistered
820 * This parameter can be one of the following values:
821 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
822 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
823 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
824 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
825 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
826 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
827 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
828 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
829 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
830 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
831 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
832 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
833 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
836 HAL_StatusTypeDef
HAL_UART_UnRegisterCallback(UART_HandleTypeDef
*huart
, HAL_UART_CallbackIDTypeDef CallbackID
)
838 HAL_StatusTypeDef status
= HAL_OK
;
842 if (HAL_UART_STATE_READY
== huart
->gState
)
846 case HAL_UART_TX_HALFCOMPLETE_CB_ID
:
847 huart
->TxHalfCpltCallback
= HAL_UART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
850 case HAL_UART_TX_COMPLETE_CB_ID
:
851 huart
->TxCpltCallback
= HAL_UART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
854 case HAL_UART_RX_HALFCOMPLETE_CB_ID
:
855 huart
->RxHalfCpltCallback
= HAL_UART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
858 case HAL_UART_RX_COMPLETE_CB_ID
:
859 huart
->RxCpltCallback
= HAL_UART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
862 case HAL_UART_ERROR_CB_ID
:
863 huart
->ErrorCallback
= HAL_UART_ErrorCallback
; /* Legacy weak ErrorCallback */
866 case HAL_UART_ABORT_COMPLETE_CB_ID
:
867 huart
->AbortCpltCallback
= HAL_UART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
870 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID
:
871 huart
->AbortTransmitCpltCallback
= HAL_UART_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
874 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID
:
875 huart
->AbortReceiveCpltCallback
= HAL_UART_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
878 case HAL_UART_WAKEUP_CB_ID
:
879 huart
->WakeupCallback
= HAL_UARTEx_WakeupCallback
; /* Legacy weak WakeupCallback */
882 case HAL_UART_RX_FIFO_FULL_CB_ID
:
883 huart
->RxFifoFullCallback
= HAL_UARTEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
886 case HAL_UART_TX_FIFO_EMPTY_CB_ID
:
887 huart
->TxFifoEmptyCallback
= HAL_UARTEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
890 case HAL_UART_MSPINIT_CB_ID
:
891 huart
->MspInitCallback
= HAL_UART_MspInit
; /* Legacy weak MspInitCallback */
894 case HAL_UART_MSPDEINIT_CB_ID
:
895 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
; /* Legacy weak MspDeInitCallback */
899 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
905 else if (HAL_UART_STATE_RESET
== huart
->gState
)
909 case HAL_UART_MSPINIT_CB_ID
:
910 huart
->MspInitCallback
= HAL_UART_MspInit
;
913 case HAL_UART_MSPDEINIT_CB_ID
:
914 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
;
918 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
926 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
935 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
941 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
942 * @brief UART Transmit/Receive functions
945 ===============================================================================
946 ##### IO operation functions #####
947 ===============================================================================
948 This subsection provides a set of functions allowing to manage the UART asynchronous
949 and Half duplex data transfers.
951 (#) There are two mode of transfer:
952 (+) Blocking mode: The communication is performed in polling mode.
953 The HAL status of all data processing is returned by the same function
954 after finishing transfer.
955 (+) Non-Blocking mode: The communication is performed using Interrupts
956 or DMA, These API's return the HAL status.
957 The end of the data processing will be indicated through the
958 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
960 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
961 will be executed respectively at the end of the transmit or Receive process
962 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
964 (#) Blocking mode API's are :
965 (+) HAL_UART_Transmit()
966 (+) HAL_UART_Receive()
968 (#) Non-Blocking mode API's with Interrupt are :
969 (+) HAL_UART_Transmit_IT()
970 (+) HAL_UART_Receive_IT()
971 (+) HAL_UART_IRQHandler()
973 (#) Non-Blocking mode API's with DMA are :
974 (+) HAL_UART_Transmit_DMA()
975 (+) HAL_UART_Receive_DMA()
976 (+) HAL_UART_DMAPause()
977 (+) HAL_UART_DMAResume()
978 (+) HAL_UART_DMAStop()
980 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
981 (+) HAL_UART_TxHalfCpltCallback()
982 (+) HAL_UART_TxCpltCallback()
983 (+) HAL_UART_RxHalfCpltCallback()
984 (+) HAL_UART_RxCpltCallback()
985 (+) HAL_UART_ErrorCallback()
987 (#) Non-Blocking mode transfers could be aborted using Abort API's :
989 (+) HAL_UART_AbortTransmit()
990 (+) HAL_UART_AbortReceive()
991 (+) HAL_UART_Abort_IT()
992 (+) HAL_UART_AbortTransmit_IT()
993 (+) HAL_UART_AbortReceive_IT()
995 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
996 (+) HAL_UART_AbortCpltCallback()
997 (+) HAL_UART_AbortTransmitCpltCallback()
998 (+) HAL_UART_AbortReceiveCpltCallback()
1000 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
1001 Errors are handled as follows :
1002 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1003 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
1004 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
1005 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
1006 If user wants to abort it, Abort services should be called by user.
1007 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1008 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1009 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1011 -@- In the Half duplex communication, it is forbidden to run the transmit
1012 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1019 * @brief Send an amount of data in blocking mode.
1020 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1021 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1022 * of u16 provided through pData.
1023 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
1024 * data to the TXFIFO. Write operations to the TDR register are performed
1025 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
1026 * TXE are mapped on the same bit-field.
1027 * @param huart UART handle.
1028 * @param pData Pointer to data buffer (u8 or u16 data elements).
1029 * @param Size Amount of data elements (u8 or u16) to be sent.
1030 * @param Timeout Timeout duration.
1031 * @retval HAL status
1033 HAL_StatusTypeDef
HAL_UART_Transmit(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
1035 uint8_t *pdata8bits
;
1036 uint16_t *pdata16bits
;
1039 /* Check that a Tx process is not already ongoing */
1040 if (huart
->gState
== HAL_UART_STATE_READY
)
1042 if ((pData
== NULL
) || (Size
== 0U))
1049 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1050 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1052 /* Init tickstart for timeout managment*/
1053 tickstart
= HAL_GetTick();
1055 huart
->TxXferSize
= Size
;
1056 huart
->TxXferCount
= Size
;
1058 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1059 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1062 pdata16bits
= (uint16_t *) pData
;
1070 __HAL_UNLOCK(huart
);
1072 while (huart
->TxXferCount
> 0U)
1074 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1078 if (pdata8bits
== NULL
)
1080 huart
->Instance
->TDR
= (uint16_t)(*pdata16bits
& 0x01FFU
);
1085 huart
->Instance
->TDR
= (uint8_t)(*pdata8bits
& 0xFFU
);
1088 huart
->TxXferCount
--;
1091 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1096 /* At end of Tx process, restore huart->gState to Ready */
1097 huart
->gState
= HAL_UART_STATE_READY
;
1108 * @brief Receive an amount of data in blocking mode.
1109 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1110 * the received data is handled as a set of u16. In this case, Size must indicate the number
1111 * of u16 available through pData.
1112 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
1113 * is not empty. Read operations from the RDR register are performed when
1114 * RXFNE flag is set. From hardware perspective, RXFNE flag and
1115 * RXNE are mapped on the same bit-field.
1116 * @param huart UART handle.
1117 * @param pData Pointer to data buffer (u8 or u16 data elements).
1118 * @param Size Amount of data elements (u8 or u16) to be received.
1119 * @param Timeout Timeout duration.
1120 * @retval HAL status
1122 HAL_StatusTypeDef
HAL_UART_Receive(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
1124 uint8_t *pdata8bits
;
1125 uint16_t *pdata16bits
;
1129 /* Check that a Rx process is not already ongoing */
1130 if (huart
->RxState
== HAL_UART_STATE_READY
)
1132 if ((pData
== NULL
) || (Size
== 0U))
1139 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1140 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1142 /* Init tickstart for timeout managment*/
1143 tickstart
= HAL_GetTick();
1145 huart
->RxXferSize
= Size
;
1146 huart
->RxXferCount
= Size
;
1148 /* Computation of UART mask to apply to RDR register */
1149 UART_MASK_COMPUTATION(huart
);
1150 uhMask
= huart
->Mask
;
1152 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1153 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1156 pdata16bits
= (uint16_t *) pData
;
1164 __HAL_UNLOCK(huart
);
1166 /* as long as data have to be received */
1167 while (huart
->RxXferCount
> 0U)
1169 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1173 if (pdata8bits
== NULL
)
1175 *pdata16bits
= (uint16_t)(huart
->Instance
->RDR
& uhMask
);
1180 *pdata8bits
= (uint8_t)(huart
->Instance
->RDR
& (uint8_t)uhMask
);
1183 huart
->RxXferCount
--;
1186 /* At end of Rx process, restore huart->RxState to Ready */
1187 huart
->RxState
= HAL_UART_STATE_READY
;
1198 * @brief Send an amount of data in interrupt mode.
1199 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1200 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1201 * of u16 provided through pData.
1202 * @param huart UART handle.
1203 * @param pData Pointer to data buffer (u8 or u16 data elements).
1204 * @param Size Amount of data elements (u8 or u16) to be sent.
1205 * @retval HAL status
1207 HAL_StatusTypeDef
HAL_UART_Transmit_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1209 /* Check that a Tx process is not already ongoing */
1210 if (huart
->gState
== HAL_UART_STATE_READY
)
1212 if ((pData
== NULL
) || (Size
== 0U))
1219 huart
->pTxBuffPtr
= pData
;
1220 huart
->TxXferSize
= Size
;
1221 huart
->TxXferCount
= Size
;
1222 huart
->TxISR
= NULL
;
1224 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1225 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1227 /* Configure Tx interrupt processing */
1228 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
1230 /* Set the Tx ISR function pointer according to the data word length */
1231 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1233 huart
->TxISR
= UART_TxISR_16BIT_FIFOEN
;
1237 huart
->TxISR
= UART_TxISR_8BIT_FIFOEN
;
1240 __HAL_UNLOCK(huart
);
1242 /* Enable the TX FIFO threshold interrupt */
1243 SET_BIT(huart
->Instance
->CR3
, USART_CR3_TXFTIE
);
1247 /* Set the Tx ISR function pointer according to the data word length */
1248 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1250 huart
->TxISR
= UART_TxISR_16BIT
;
1254 huart
->TxISR
= UART_TxISR_8BIT
;
1257 __HAL_UNLOCK(huart
);
1259 /* Enable the Transmit Data Register Empty interrupt */
1260 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
1272 * @brief Receive an amount of data in interrupt mode.
1273 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1274 * the received data is handled as a set of u16. In this case, Size must indicate the number
1275 * of u16 available through pData.
1276 * @param huart UART handle.
1277 * @param pData Pointer to data buffer (u8 or u16 data elements).
1278 * @param Size Amount of data elements (u8 or u16) to be received.
1279 * @retval HAL status
1281 HAL_StatusTypeDef
HAL_UART_Receive_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1283 /* Check that a Rx process is not already ongoing */
1284 if (huart
->RxState
== HAL_UART_STATE_READY
)
1286 if ((pData
== NULL
) || (Size
== 0U))
1293 huart
->pRxBuffPtr
= pData
;
1294 huart
->RxXferSize
= Size
;
1295 huart
->RxXferCount
= Size
;
1296 huart
->RxISR
= NULL
;
1298 /* Computation of UART mask to apply to RDR register */
1299 UART_MASK_COMPUTATION(huart
);
1301 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1302 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1304 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1305 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1307 /* Configure Rx interrupt processing*/
1308 if ((huart
->FifoMode
== UART_FIFOMODE_ENABLE
) && (Size
>= huart
->NbRxDataToProcess
))
1310 /* Set the Rx ISR function pointer according to the data word length */
1311 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1313 huart
->RxISR
= UART_RxISR_16BIT_FIFOEN
;
1317 huart
->RxISR
= UART_RxISR_8BIT_FIFOEN
;
1320 __HAL_UNLOCK(huart
);
1322 /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
1323 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1324 SET_BIT(huart
->Instance
->CR3
, USART_CR3_RXFTIE
);
1328 /* Set the Rx ISR function pointer according to the data word length */
1329 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1331 huart
->RxISR
= UART_RxISR_16BIT
;
1335 huart
->RxISR
= UART_RxISR_8BIT
;
1338 __HAL_UNLOCK(huart
);
1340 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
1341 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1353 * @brief Send an amount of data in DMA mode.
1354 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1355 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1356 * of u16 provided through pData.
1357 * @param huart UART handle.
1358 * @param pData Pointer to data buffer (u8 or u16 data elements).
1359 * @param Size Amount of data elements (u8 or u16) to be sent.
1360 * @retval HAL status
1362 HAL_StatusTypeDef
HAL_UART_Transmit_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1364 /* Check that a Tx process is not already ongoing */
1365 if (huart
->gState
== HAL_UART_STATE_READY
)
1367 if ((pData
== NULL
) || (Size
== 0U))
1374 huart
->pTxBuffPtr
= pData
;
1375 huart
->TxXferSize
= Size
;
1376 huart
->TxXferCount
= Size
;
1378 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1379 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1381 if (huart
->hdmatx
!= NULL
)
1383 /* Set the UART DMA transfer complete callback */
1384 huart
->hdmatx
->XferCpltCallback
= UART_DMATransmitCplt
;
1386 /* Set the UART DMA Half transfer complete callback */
1387 huart
->hdmatx
->XferHalfCpltCallback
= UART_DMATxHalfCplt
;
1389 /* Set the DMA error callback */
1390 huart
->hdmatx
->XferErrorCallback
= UART_DMAError
;
1392 /* Set the DMA abort callback */
1393 huart
->hdmatx
->XferAbortCallback
= NULL
;
1395 /* Enable the UART transmit DMA channel */
1396 if (HAL_DMA_Start_IT(huart
->hdmatx
, (uint32_t)huart
->pTxBuffPtr
, (uint32_t)&huart
->Instance
->TDR
, Size
) != HAL_OK
)
1398 /* Set error code to DMA */
1399 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1401 __HAL_UNLOCK(huart
);
1403 /* Restore huart->gState to ready */
1404 huart
->gState
= HAL_UART_STATE_READY
;
1409 /* Clear the TC flag in the ICR register */
1410 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_TCF
);
1412 __HAL_UNLOCK(huart
);
1414 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1415 in the UART CR3 register */
1416 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1427 * @brief Receive an amount of data in DMA mode.
1428 * @note When the UART parity is enabled (PCE = 1), the received data contain
1429 * the parity bit (MSB position).
1430 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1431 * the received data is handled as a set of u16. In this case, Size must indicate the number
1432 * of u16 available through pData.
1433 * @param huart UART handle.
1434 * @param pData Pointer to data buffer (u8 or u16 data elements).
1435 * @param Size Amount of data elements (u8 or u16) to be received.
1436 * @retval HAL status
1438 HAL_StatusTypeDef
HAL_UART_Receive_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1440 /* Check that a Rx process is not already ongoing */
1441 if (huart
->RxState
== HAL_UART_STATE_READY
)
1443 if ((pData
== NULL
) || (Size
== 0U))
1450 huart
->pRxBuffPtr
= pData
;
1451 huart
->RxXferSize
= Size
;
1453 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1454 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1456 if (huart
->hdmarx
!= NULL
)
1458 /* Set the UART DMA transfer complete callback */
1459 huart
->hdmarx
->XferCpltCallback
= UART_DMAReceiveCplt
;
1461 /* Set the UART DMA Half transfer complete callback */
1462 huart
->hdmarx
->XferHalfCpltCallback
= UART_DMARxHalfCplt
;
1464 /* Set the DMA error callback */
1465 huart
->hdmarx
->XferErrorCallback
= UART_DMAError
;
1467 /* Set the DMA abort callback */
1468 huart
->hdmarx
->XferAbortCallback
= NULL
;
1470 /* Enable the DMA channel */
1471 if (HAL_DMA_Start_IT(huart
->hdmarx
, (uint32_t)&huart
->Instance
->RDR
, (uint32_t)huart
->pRxBuffPtr
, Size
) != HAL_OK
)
1473 /* Set error code to DMA */
1474 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1476 __HAL_UNLOCK(huart
);
1478 /* Restore huart->gState to ready */
1479 huart
->gState
= HAL_UART_STATE_READY
;
1484 __HAL_UNLOCK(huart
);
1486 /* Enable the UART Parity Error Interrupt */
1487 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1489 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1490 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1492 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1493 in the UART CR3 register */
1494 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1505 * @brief Pause the DMA Transfer.
1506 * @param huart UART handle.
1507 * @retval HAL status
1509 HAL_StatusTypeDef
HAL_UART_DMAPause(UART_HandleTypeDef
*huart
)
1511 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
1512 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
1516 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
1517 (gstate
== HAL_UART_STATE_BUSY_TX
))
1519 /* Disable the UART DMA Tx request */
1520 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1522 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
1523 (rxstate
== HAL_UART_STATE_BUSY_RX
))
1525 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1526 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1527 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1529 /* Disable the UART DMA Rx request */
1530 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1533 __HAL_UNLOCK(huart
);
1539 * @brief Resume the DMA Transfer.
1540 * @param huart UART handle.
1541 * @retval HAL status
1543 HAL_StatusTypeDef
HAL_UART_DMAResume(UART_HandleTypeDef
*huart
)
1547 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1549 /* Enable the UART DMA Tx request */
1550 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1552 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1554 /* Clear the Overrun flag before resuming the Rx transfer */
1555 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
);
1557 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1558 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1559 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1561 /* Enable the UART DMA Rx request */
1562 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1565 __HAL_UNLOCK(huart
);
1571 * @brief Stop the DMA Transfer.
1572 * @param huart UART handle.
1573 * @retval HAL status
1575 HAL_StatusTypeDef
HAL_UART_DMAStop(UART_HandleTypeDef
*huart
)
1577 /* The Lock is not implemented on this API to allow the user application
1578 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1579 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1580 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1581 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1582 the stream and the corresponding call back is executed. */
1584 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
1585 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
1587 /* Stop UART DMA Tx request if ongoing */
1588 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
1589 (gstate
== HAL_UART_STATE_BUSY_TX
))
1591 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1593 /* Abort the UART DMA Tx channel */
1594 if (huart
->hdmatx
!= NULL
)
1596 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1598 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1600 /* Set error code to DMA */
1601 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1608 UART_EndTxTransfer(huart
);
1611 /* Stop UART DMA Rx request if ongoing */
1612 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
1613 (rxstate
== HAL_UART_STATE_BUSY_RX
))
1615 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1617 /* Abort the UART DMA Rx channel */
1618 if (huart
->hdmarx
!= NULL
)
1620 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1622 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1624 /* Set error code to DMA */
1625 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1632 UART_EndRxTransfer(huart
);
1639 * @brief Abort ongoing transfers (blocking mode).
1640 * @param huart UART handle.
1641 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1642 * This procedure performs following operations :
1643 * - Disable UART Interrupts (Tx and Rx)
1644 * - Disable the DMA transfer in the peripheral register (if enabled)
1645 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1646 * - Set handle State to READY
1647 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1648 * @retval HAL status
1650 HAL_StatusTypeDef
HAL_UART_Abort(UART_HandleTypeDef
*huart
)
1652 /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1653 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
1654 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
);
1656 /* Disable the UART DMA Tx request if enabled */
1657 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1659 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1661 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1662 if (huart
->hdmatx
!= NULL
)
1664 /* Set the UART DMA Abort callback to Null.
1665 No call back execution at end of DMA abort procedure */
1666 huart
->hdmatx
->XferAbortCallback
= NULL
;
1668 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1670 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1672 /* Set error code to DMA */
1673 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1681 /* Disable the UART DMA Rx request if enabled */
1682 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1684 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1686 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1687 if (huart
->hdmarx
!= NULL
)
1689 /* Set the UART DMA Abort callback to Null.
1690 No call back execution at end of DMA abort procedure */
1691 huart
->hdmarx
->XferAbortCallback
= NULL
;
1693 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1695 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1697 /* Set error code to DMA */
1698 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1706 /* Reset Tx and Rx transfer counters */
1707 huart
->TxXferCount
= 0U;
1708 huart
->RxXferCount
= 0U;
1710 /* Clear the Error flags in the ICR register */
1711 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1713 /* Flush the whole TX FIFO (if needed) */
1714 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
1716 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
1719 /* Discard the received data */
1720 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1722 /* Restore huart->gState and huart->RxState to Ready */
1723 huart
->gState
= HAL_UART_STATE_READY
;
1724 huart
->RxState
= HAL_UART_STATE_READY
;
1726 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1732 * @brief Abort ongoing Transmit transfer (blocking mode).
1733 * @param huart UART handle.
1734 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1735 * This procedure performs following operations :
1736 * - Disable UART Interrupts (Tx)
1737 * - Disable the DMA transfer in the peripheral register (if enabled)
1738 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1739 * - Set handle State to READY
1740 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1741 * @retval HAL status
1743 HAL_StatusTypeDef
HAL_UART_AbortTransmit(UART_HandleTypeDef
*huart
)
1745 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1746 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TCIE
| USART_CR1_TXEIE_TXFNFIE
));
1747 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_TXFTIE
);
1749 /* Disable the UART DMA Tx request if enabled */
1750 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1752 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1754 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1755 if (huart
->hdmatx
!= NULL
)
1757 /* Set the UART DMA Abort callback to Null.
1758 No call back execution at end of DMA abort procedure */
1759 huart
->hdmatx
->XferAbortCallback
= NULL
;
1761 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1763 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1765 /* Set error code to DMA */
1766 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1774 /* Reset Tx transfer counter */
1775 huart
->TxXferCount
= 0U;
1777 /* Flush the whole TX FIFO (if needed) */
1778 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
1780 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
1783 /* Restore huart->gState to Ready */
1784 huart
->gState
= HAL_UART_STATE_READY
;
1790 * @brief Abort ongoing Receive transfer (blocking mode).
1791 * @param huart UART handle.
1792 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1793 * This procedure performs following operations :
1794 * - Disable UART Interrupts (Rx)
1795 * - Disable the DMA transfer in the peripheral register (if enabled)
1796 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1797 * - Set handle State to READY
1798 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1799 * @retval HAL status
1801 HAL_StatusTypeDef
HAL_UART_AbortReceive(UART_HandleTypeDef
*huart
)
1803 /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
1804 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
));
1805 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
| USART_CR3_RXFTIE
);
1807 /* Disable the UART DMA Rx request if enabled */
1808 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1810 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1812 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1813 if (huart
->hdmarx
!= NULL
)
1815 /* Set the UART DMA Abort callback to Null.
1816 No call back execution at end of DMA abort procedure */
1817 huart
->hdmarx
->XferAbortCallback
= NULL
;
1819 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1821 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1823 /* Set error code to DMA */
1824 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1832 /* Reset Rx transfer counter */
1833 huart
->RxXferCount
= 0U;
1835 /* Clear the Error flags in the ICR register */
1836 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1838 /* Discard the received data */
1839 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1841 /* Restore huart->RxState to Ready */
1842 huart
->RxState
= HAL_UART_STATE_READY
;
1848 * @brief Abort ongoing transfers (Interrupt mode).
1849 * @param huart UART handle.
1850 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1851 * This procedure performs following operations :
1852 * - Disable UART Interrupts (Tx and Rx)
1853 * - Disable the DMA transfer in the peripheral register (if enabled)
1854 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1855 * - Set handle State to READY
1856 * - At abort completion, call user abort complete callback
1857 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1858 * considered as completed only when user abort complete callback is executed (not when exiting function).
1859 * @retval HAL status
1861 HAL_StatusTypeDef
HAL_UART_Abort_IT(UART_HandleTypeDef
*huart
)
1863 uint32_t abortcplt
= 1U;
1865 /* Disable interrupts */
1866 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_PEIE
| USART_CR1_TCIE
| USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_TXEIE_TXFNFIE
));
1867 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
1869 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1870 before any call to DMA Abort functions */
1871 /* DMA Tx Handle is valid */
1872 if (huart
->hdmatx
!= NULL
)
1874 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1875 Otherwise, set it to NULL */
1876 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1878 huart
->hdmatx
->XferAbortCallback
= UART_DMATxAbortCallback
;
1882 huart
->hdmatx
->XferAbortCallback
= NULL
;
1885 /* DMA Rx Handle is valid */
1886 if (huart
->hdmarx
!= NULL
)
1888 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1889 Otherwise, set it to NULL */
1890 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1892 huart
->hdmarx
->XferAbortCallback
= UART_DMARxAbortCallback
;
1896 huart
->hdmarx
->XferAbortCallback
= NULL
;
1900 /* Disable the UART DMA Tx request if enabled */
1901 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1903 /* Disable DMA Tx at UART level */
1904 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1906 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1907 if (huart
->hdmatx
!= NULL
)
1909 /* UART Tx DMA Abort callback has already been initialised :
1910 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1913 if (HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
1915 huart
->hdmatx
->XferAbortCallback
= NULL
;
1924 /* Disable the UART DMA Rx request if enabled */
1925 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1927 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1929 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1930 if (huart
->hdmarx
!= NULL
)
1932 /* UART Rx DMA Abort callback has already been initialised :
1933 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1936 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1938 huart
->hdmarx
->XferAbortCallback
= NULL
;
1948 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1949 if (abortcplt
== 1U)
1951 /* Reset Tx and Rx transfer counters */
1952 huart
->TxXferCount
= 0U;
1953 huart
->RxXferCount
= 0U;
1955 /* Clear ISR function pointers */
1956 huart
->RxISR
= NULL
;
1957 huart
->TxISR
= NULL
;
1959 /* Reset errorCode */
1960 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1962 /* Clear the Error flags in the ICR register */
1963 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1965 /* Flush the whole TX FIFO (if needed) */
1966 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
1968 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
1971 /* Discard the received data */
1972 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1974 /* Restore huart->gState and huart->RxState to Ready */
1975 huart
->gState
= HAL_UART_STATE_READY
;
1976 huart
->RxState
= HAL_UART_STATE_READY
;
1978 /* As no DMA to be aborted, call directly user Abort complete callback */
1979 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
1980 /* Call registered Abort complete callback */
1981 huart
->AbortCpltCallback(huart
);
1983 /* Call legacy weak Abort complete callback */
1984 HAL_UART_AbortCpltCallback(huart
);
1985 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1992 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1993 * @param huart UART handle.
1994 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1995 * This procedure performs following operations :
1996 * - Disable UART Interrupts (Tx)
1997 * - Disable the DMA transfer in the peripheral register (if enabled)
1998 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1999 * - Set handle State to READY
2000 * - At abort completion, call user abort complete callback
2001 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2002 * considered as completed only when user abort complete callback is executed (not when exiting function).
2003 * @retval HAL status
2005 HAL_StatusTypeDef
HAL_UART_AbortTransmit_IT(UART_HandleTypeDef
*huart
)
2007 /* Disable interrupts */
2008 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TCIE
| USART_CR1_TXEIE_TXFNFIE
));
2009 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_TXFTIE
);
2011 /* Disable the UART DMA Tx request if enabled */
2012 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
2014 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
2016 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
2017 if (huart
->hdmatx
!= NULL
)
2019 /* Set the UART DMA Abort callback :
2020 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2021 huart
->hdmatx
->XferAbortCallback
= UART_DMATxOnlyAbortCallback
;
2024 if (HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
2026 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2027 huart
->hdmatx
->XferAbortCallback(huart
->hdmatx
);
2032 /* Reset Tx transfer counter */
2033 huart
->TxXferCount
= 0U;
2035 /* Clear TxISR function pointers */
2036 huart
->TxISR
= NULL
;
2038 /* Restore huart->gState to Ready */
2039 huart
->gState
= HAL_UART_STATE_READY
;
2041 /* As no DMA to be aborted, call directly user Abort complete callback */
2042 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2043 /* Call registered Abort Transmit Complete Callback */
2044 huart
->AbortTransmitCpltCallback(huart
);
2046 /* Call legacy weak Abort Transmit Complete Callback */
2047 HAL_UART_AbortTransmitCpltCallback(huart
);
2048 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2053 /* Reset Tx transfer counter */
2054 huart
->TxXferCount
= 0U;
2056 /* Clear TxISR function pointers */
2057 huart
->TxISR
= NULL
;
2059 /* Flush the whole TX FIFO (if needed) */
2060 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
2062 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
2065 /* Restore huart->gState to Ready */
2066 huart
->gState
= HAL_UART_STATE_READY
;
2068 /* As no DMA to be aborted, call directly user Abort complete callback */
2069 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2070 /* Call registered Abort Transmit Complete Callback */
2071 huart
->AbortTransmitCpltCallback(huart
);
2073 /* Call legacy weak Abort Transmit Complete Callback */
2074 HAL_UART_AbortTransmitCpltCallback(huart
);
2075 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2082 * @brief Abort ongoing Receive transfer (Interrupt mode).
2083 * @param huart UART handle.
2084 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2085 * This procedure performs following operations :
2086 * - Disable UART Interrupts (Rx)
2087 * - Disable the DMA transfer in the peripheral register (if enabled)
2088 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2089 * - Set handle State to READY
2090 * - At abort completion, call user abort complete callback
2091 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2092 * considered as completed only when user abort complete callback is executed (not when exiting function).
2093 * @retval HAL status
2095 HAL_StatusTypeDef
HAL_UART_AbortReceive_IT(UART_HandleTypeDef
*huart
)
2097 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2098 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
));
2099 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
2101 /* Disable the UART DMA Rx request if enabled */
2102 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
2104 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
2106 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2107 if (huart
->hdmarx
!= NULL
)
2109 /* Set the UART DMA Abort callback :
2110 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2111 huart
->hdmarx
->XferAbortCallback
= UART_DMARxOnlyAbortCallback
;
2114 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
2116 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2117 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
2122 /* Reset Rx transfer counter */
2123 huart
->RxXferCount
= 0U;
2125 /* Clear RxISR function pointer */
2126 huart
->pRxBuffPtr
= NULL
;
2128 /* Clear the Error flags in the ICR register */
2129 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2131 /* Discard the received data */
2132 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
2134 /* Restore huart->RxState to Ready */
2135 huart
->RxState
= HAL_UART_STATE_READY
;
2137 /* As no DMA to be aborted, call directly user Abort complete callback */
2138 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2139 /* Call registered Abort Receive Complete Callback */
2140 huart
->AbortReceiveCpltCallback(huart
);
2142 /* Call legacy weak Abort Receive Complete Callback */
2143 HAL_UART_AbortReceiveCpltCallback(huart
);
2144 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2149 /* Reset Rx transfer counter */
2150 huart
->RxXferCount
= 0U;
2152 /* Clear RxISR function pointer */
2153 huart
->pRxBuffPtr
= NULL
;
2155 /* Clear the Error flags in the ICR register */
2156 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2158 /* Restore huart->RxState to Ready */
2159 huart
->RxState
= HAL_UART_STATE_READY
;
2161 /* As no DMA to be aborted, call directly user Abort complete callback */
2162 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2163 /* Call registered Abort Receive Complete Callback */
2164 huart
->AbortReceiveCpltCallback(huart
);
2166 /* Call legacy weak Abort Receive Complete Callback */
2167 HAL_UART_AbortReceiveCpltCallback(huart
);
2168 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2175 * @brief Handle UART interrupt request.
2176 * @param huart UART handle.
2179 void HAL_UART_IRQHandler(UART_HandleTypeDef
*huart
)
2181 uint32_t isrflags
= READ_REG(huart
->Instance
->ISR
);
2182 uint32_t cr1its
= READ_REG(huart
->Instance
->CR1
);
2183 uint32_t cr3its
= READ_REG(huart
->Instance
->CR3
);
2185 uint32_t errorflags
;
2188 /* If no error occurs */
2189 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
| USART_ISR_RTOF
));
2190 if (errorflags
== 0U)
2192 /* UART in mode Receiver ---------------------------------------------------*/
2193 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
2194 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
2195 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
2197 if (huart
->RxISR
!= NULL
)
2199 huart
->RxISR(huart
);
2205 /* If some errors occur */
2206 if ((errorflags
!= 0U)
2207 && ((((cr3its
& (USART_CR3_RXFTIE
| USART_CR3_EIE
)) != 0U)
2208 || ((cr1its
& (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_RTOIE
)) != 0U))))
2210 /* UART parity error interrupt occurred -------------------------------------*/
2211 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
2213 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_PEF
);
2215 huart
->ErrorCode
|= HAL_UART_ERROR_PE
;
2218 /* UART frame error interrupt occurred --------------------------------------*/
2219 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2221 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_FEF
);
2223 huart
->ErrorCode
|= HAL_UART_ERROR_FE
;
2226 /* UART noise error interrupt occurred --------------------------------------*/
2227 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2229 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_NEF
);
2231 huart
->ErrorCode
|= HAL_UART_ERROR_NE
;
2234 /* UART Over-Run interrupt occurred -----------------------------------------*/
2235 if (((isrflags
& USART_ISR_ORE
) != 0U)
2236 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U) ||
2237 ((cr3its
& (USART_CR3_RXFTIE
| USART_CR3_EIE
)) != 0U)))
2239 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
);
2241 huart
->ErrorCode
|= HAL_UART_ERROR_ORE
;
2244 /* UART Receiver Timeout interrupt occurred ---------------------------------*/
2245 if (((isrflags
& USART_ISR_RTOF
) != 0U) && ((cr1its
& USART_CR1_RTOIE
) != 0U))
2247 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_RTOF
);
2249 huart
->ErrorCode
|= HAL_UART_ERROR_RTO
;
2252 /* Call UART Error Call back function if need be ----------------------------*/
2253 if (huart
->ErrorCode
!= HAL_UART_ERROR_NONE
)
2255 /* UART in mode Receiver --------------------------------------------------*/
2256 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
2257 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
2258 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
2260 if (huart
->RxISR
!= NULL
)
2262 huart
->RxISR(huart
);
2266 /* If Error is to be considered as blocking :
2267 - Receiver Timeout error in Reception
2268 - Overrun error in Reception
2269 - any error occurs in DMA mode reception
2271 errorcode
= huart
->ErrorCode
;
2272 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) ||
2273 ((errorcode
& (HAL_UART_ERROR_RTO
| HAL_UART_ERROR_ORE
)) != 0U))
2275 /* Blocking error : transfer is aborted
2276 Set the UART state ready to be able to start again the process,
2277 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2278 UART_EndRxTransfer(huart
);
2280 /* Disable the UART DMA Rx request if enabled */
2281 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
2283 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
2285 /* Abort the UART DMA Rx channel */
2286 if (huart
->hdmarx
!= NULL
)
2288 /* Set the UART DMA Abort callback :
2289 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2290 huart
->hdmarx
->XferAbortCallback
= UART_DMAAbortOnError
;
2293 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
2295 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2296 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
2301 /* Call user error callback */
2302 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2303 /*Call registered error callback*/
2304 huart
->ErrorCallback(huart
);
2306 /*Call legacy weak error callback*/
2307 HAL_UART_ErrorCallback(huart
);
2308 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2314 /* Call user error callback */
2315 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2316 /*Call registered error callback*/
2317 huart
->ErrorCallback(huart
);
2319 /*Call legacy weak error callback*/
2320 HAL_UART_ErrorCallback(huart
);
2321 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2326 /* Non Blocking error : transfer could go on.
2327 Error is notified to user through user error callback */
2328 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2329 /*Call registered error callback*/
2330 huart
->ErrorCallback(huart
);
2332 /*Call legacy weak error callback*/
2333 HAL_UART_ErrorCallback(huart
);
2334 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2335 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2340 } /* End if some error occurs */
2342 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
2343 if (((isrflags
& USART_ISR_WUF
) != 0U) && ((cr3its
& USART_CR3_WUFIE
) != 0U))
2345 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_WUF
);
2347 /* UART Rx state is not reset as a reception process might be ongoing.
2348 If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
2350 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2351 /* Call registered Wakeup Callback */
2352 huart
->WakeupCallback(huart
);
2354 /* Call legacy weak Wakeup Callback */
2355 HAL_UARTEx_WakeupCallback(huart
);
2356 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2360 /* UART in mode Transmitter ------------------------------------------------*/
2361 if (((isrflags
& USART_ISR_TXE_TXFNF
) != 0U)
2362 && (((cr1its
& USART_CR1_TXEIE_TXFNFIE
) != 0U)
2363 || ((cr3its
& USART_CR3_TXFTIE
) != 0U)))
2365 if (huart
->TxISR
!= NULL
)
2367 huart
->TxISR(huart
);
2372 /* UART in mode Transmitter (transmission end) -----------------------------*/
2373 if (((isrflags
& USART_ISR_TC
) != 0U) && ((cr1its
& USART_CR1_TCIE
) != 0U))
2375 UART_EndTransmit_IT(huart
);
2379 /* UART TX Fifo Empty occurred ----------------------------------------------*/
2380 if (((isrflags
& USART_ISR_TXFE
) != 0U) && ((cr1its
& USART_CR1_TXFEIE
) != 0U))
2382 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2383 /* Call registered Tx Fifo Empty Callback */
2384 huart
->TxFifoEmptyCallback(huart
);
2386 /* Call legacy weak Tx Fifo Empty Callback */
2387 HAL_UARTEx_TxFifoEmptyCallback(huart
);
2388 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2392 /* UART RX Fifo Full occurred ----------------------------------------------*/
2393 if (((isrflags
& USART_ISR_RXFF
) != 0U) && ((cr1its
& USART_CR1_RXFFIE
) != 0U))
2395 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2396 /* Call registered Rx Fifo Full Callback */
2397 huart
->RxFifoFullCallback(huart
);
2399 /* Call legacy weak Rx Fifo Full Callback */
2400 HAL_UARTEx_RxFifoFullCallback(huart
);
2401 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2407 * @brief Tx Transfer completed callback.
2408 * @param huart UART handle.
2411 __weak
void HAL_UART_TxCpltCallback(UART_HandleTypeDef
*huart
)
2413 /* Prevent unused argument(s) compilation warning */
2416 /* NOTE : This function should not be modified, when the callback is needed,
2417 the HAL_UART_TxCpltCallback can be implemented in the user file.
2422 * @brief Tx Half Transfer completed callback.
2423 * @param huart UART handle.
2426 __weak
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef
*huart
)
2428 /* Prevent unused argument(s) compilation warning */
2431 /* NOTE: This function should not be modified, when the callback is needed,
2432 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2437 * @brief Rx Transfer completed callback.
2438 * @param huart UART handle.
2441 __weak
void HAL_UART_RxCpltCallback(UART_HandleTypeDef
*huart
)
2443 /* Prevent unused argument(s) compilation warning */
2446 /* NOTE : This function should not be modified, when the callback is needed,
2447 the HAL_UART_RxCpltCallback can be implemented in the user file.
2452 * @brief Rx Half Transfer completed callback.
2453 * @param huart UART handle.
2456 __weak
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef
*huart
)
2458 /* Prevent unused argument(s) compilation warning */
2461 /* NOTE: This function should not be modified, when the callback is needed,
2462 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2467 * @brief UART error callback.
2468 * @param huart UART handle.
2471 __weak
void HAL_UART_ErrorCallback(UART_HandleTypeDef
*huart
)
2473 /* Prevent unused argument(s) compilation warning */
2476 /* NOTE : This function should not be modified, when the callback is needed,
2477 the HAL_UART_ErrorCallback can be implemented in the user file.
2482 * @brief UART Abort Complete callback.
2483 * @param huart UART handle.
2486 __weak
void HAL_UART_AbortCpltCallback(UART_HandleTypeDef
*huart
)
2488 /* Prevent unused argument(s) compilation warning */
2491 /* NOTE : This function should not be modified, when the callback is needed,
2492 the HAL_UART_AbortCpltCallback can be implemented in the user file.
2497 * @brief UART Abort Complete callback.
2498 * @param huart UART handle.
2501 __weak
void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef
*huart
)
2503 /* Prevent unused argument(s) compilation warning */
2506 /* NOTE : This function should not be modified, when the callback is needed,
2507 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2512 * @brief UART Abort Receive Complete callback.
2513 * @param huart UART handle.
2516 __weak
void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef
*huart
)
2518 /* Prevent unused argument(s) compilation warning */
2521 /* NOTE : This function should not be modified, when the callback is needed,
2522 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2530 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2531 * @brief UART control functions
2534 ===============================================================================
2535 ##### Peripheral Control functions #####
2536 ===============================================================================
2538 This subsection provides a set of functions allowing to control the UART.
2539 (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
2540 (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
2541 (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
2542 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2543 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2544 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2545 (+) UART_SetConfig() API configures the UART peripheral
2546 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2547 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2548 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2549 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2550 (+) HAL_LIN_SendBreak() API transmits the break characters
2556 * @brief Update on the fly the receiver timeout value in RTOR register.
2557 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2558 * the configuration information for the specified UART module.
2559 * @param TimeoutValue receiver timeout value in number of baud blocks. The timeout
2560 * value must be less or equal to 0x0FFFFFFFF.
2563 void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef
*huart
, uint32_t TimeoutValue
)
2565 if (!(IS_LPUART_INSTANCE(huart
->Instance
)))
2567 assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue
));
2568 MODIFY_REG(huart
->Instance
->RTOR
, USART_RTOR_RTO
, TimeoutValue
);
2573 * @brief Enable the UART receiver timeout feature.
2574 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2575 * the configuration information for the specified UART module.
2576 * @retval HAL status
2578 HAL_StatusTypeDef
HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef
*huart
)
2580 if (!(IS_LPUART_INSTANCE(huart
->Instance
)))
2582 if (huart
->gState
== HAL_UART_STATE_READY
)
2584 /* Process Locked */
2587 huart
->gState
= HAL_UART_STATE_BUSY
;
2589 /* Set the USART RTOEN bit */
2590 SET_BIT(huart
->Instance
->CR2
, USART_CR2_RTOEN
);
2592 huart
->gState
= HAL_UART_STATE_READY
;
2594 /* Process Unlocked */
2595 __HAL_UNLOCK(huart
);
2611 * @brief Disable the UART receiver timeout feature.
2612 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2613 * the configuration information for the specified UART module.
2614 * @retval HAL status
2616 HAL_StatusTypeDef
HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef
*huart
)
2618 if (!(IS_LPUART_INSTANCE(huart
->Instance
)))
2620 if (huart
->gState
== HAL_UART_STATE_READY
)
2622 /* Process Locked */
2625 huart
->gState
= HAL_UART_STATE_BUSY
;
2627 /* Clear the USART RTOEN bit */
2628 CLEAR_BIT(huart
->Instance
->CR2
, USART_CR2_RTOEN
);
2630 huart
->gState
= HAL_UART_STATE_READY
;
2632 /* Process Unlocked */
2633 __HAL_UNLOCK(huart
);
2649 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
2650 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2651 * @param huart UART handle.
2652 * @retval HAL status
2654 HAL_StatusTypeDef
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef
*huart
)
2658 huart
->gState
= HAL_UART_STATE_BUSY
;
2660 /* Enable USART mute mode by setting the MME bit in the CR1 register */
2661 SET_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
2663 huart
->gState
= HAL_UART_STATE_READY
;
2665 return (UART_CheckIdleState(huart
));
2669 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
2670 * as it may not have been in mute mode at this very moment).
2671 * @param huart UART handle.
2672 * @retval HAL status
2674 HAL_StatusTypeDef
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef
*huart
)
2678 huart
->gState
= HAL_UART_STATE_BUSY
;
2680 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2681 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
2683 huart
->gState
= HAL_UART_STATE_READY
;
2685 return (UART_CheckIdleState(huart
));
2689 * @brief Enter UART mute mode (means UART actually enters mute mode).
2690 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2691 * @param huart UART handle.
2694 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef
*huart
)
2696 __HAL_UART_SEND_REQ(huart
, UART_MUTE_MODE_REQUEST
);
2700 * @brief Enable the UART transmitter and disable the UART receiver.
2701 * @param huart UART handle.
2702 * @retval HAL status
2704 HAL_StatusTypeDef
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef
*huart
)
2707 huart
->gState
= HAL_UART_STATE_BUSY
;
2709 /* Clear TE and RE bits */
2710 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2712 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2713 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TE
);
2715 huart
->gState
= HAL_UART_STATE_READY
;
2717 __HAL_UNLOCK(huart
);
2723 * @brief Enable the UART receiver and disable the UART transmitter.
2724 * @param huart UART handle.
2725 * @retval HAL status.
2727 HAL_StatusTypeDef
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
*huart
)
2730 huart
->gState
= HAL_UART_STATE_BUSY
;
2732 /* Clear TE and RE bits */
2733 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2735 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2736 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RE
);
2738 huart
->gState
= HAL_UART_STATE_READY
;
2740 __HAL_UNLOCK(huart
);
2747 * @brief Transmit break characters.
2748 * @param huart UART handle.
2749 * @retval HAL status
2751 HAL_StatusTypeDef
HAL_LIN_SendBreak(UART_HandleTypeDef
*huart
)
2753 /* Check the parameters */
2754 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
2758 huart
->gState
= HAL_UART_STATE_BUSY
;
2760 /* Send break characters */
2761 __HAL_UART_SEND_REQ(huart
, UART_SENDBREAK_REQUEST
);
2763 huart
->gState
= HAL_UART_STATE_READY
;
2765 __HAL_UNLOCK(huart
);
2774 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2775 * @brief UART Peripheral State functions
2778 ==============================================================================
2779 ##### Peripheral State and Error functions #####
2780 ==============================================================================
2782 This subsection provides functions allowing to :
2783 (+) Return the UART handle state.
2784 (+) Return the UART handle error code
2791 * @brief Return the UART handle state.
2792 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2793 * the configuration information for the specified UART.
2796 HAL_UART_StateTypeDef
HAL_UART_GetState(UART_HandleTypeDef
*huart
)
2800 temp1
= huart
->gState
;
2801 temp2
= huart
->RxState
;
2803 return (HAL_UART_StateTypeDef
)(temp1
| temp2
);
2807 * @brief Return the UART handle error code.
2808 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2809 * the configuration information for the specified UART.
2810 * @retval UART Error Code
2812 uint32_t HAL_UART_GetError(UART_HandleTypeDef
*huart
)
2814 return huart
->ErrorCode
;
2824 /** @defgroup UART_Private_Functions UART Private Functions
2829 * @brief Initialize the callbacks to their default values.
2830 * @param huart UART handle.
2833 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2834 void UART_InitCallbacksToDefault(UART_HandleTypeDef
*huart
)
2836 /* Init the UART Callback settings */
2837 huart
->TxHalfCpltCallback
= HAL_UART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
2838 huart
->TxCpltCallback
= HAL_UART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2839 huart
->RxHalfCpltCallback
= HAL_UART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
2840 huart
->RxCpltCallback
= HAL_UART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2841 huart
->ErrorCallback
= HAL_UART_ErrorCallback
; /* Legacy weak ErrorCallback */
2842 huart
->AbortCpltCallback
= HAL_UART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2843 huart
->AbortTransmitCpltCallback
= HAL_UART_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2844 huart
->AbortReceiveCpltCallback
= HAL_UART_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2845 huart
->WakeupCallback
= HAL_UARTEx_WakeupCallback
; /* Legacy weak WakeupCallback */
2846 huart
->RxFifoFullCallback
= HAL_UARTEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
2847 huart
->TxFifoEmptyCallback
= HAL_UARTEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
2850 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2853 * @brief Configure the UART peripheral.
2854 * @param huart UART handle.
2855 * @retval HAL status
2857 HAL_StatusTypeDef
UART_SetConfig(UART_HandleTypeDef
*huart
)
2861 UART_ClockSourceTypeDef clocksource
;
2863 HAL_StatusTypeDef ret
= HAL_OK
;
2864 uint32_t lpuart_ker_ck_pres
;
2865 PLL2_ClocksTypeDef pll2_clocks
;
2866 PLL3_ClocksTypeDef pll3_clocks
;
2869 /* Check the parameters */
2870 assert_param(IS_UART_BAUDRATE(huart
->Init
.BaudRate
));
2871 assert_param(IS_UART_WORD_LENGTH(huart
->Init
.WordLength
));
2872 if (UART_INSTANCE_LOWPOWER(huart
))
2874 assert_param(IS_LPUART_STOPBITS(huart
->Init
.StopBits
));
2878 assert_param(IS_UART_STOPBITS(huart
->Init
.StopBits
));
2879 assert_param(IS_UART_ONE_BIT_SAMPLE(huart
->Init
.OneBitSampling
));
2882 assert_param(IS_UART_PARITY(huart
->Init
.Parity
));
2883 assert_param(IS_UART_MODE(huart
->Init
.Mode
));
2884 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart
->Init
.HwFlowCtl
));
2885 assert_param(IS_UART_OVERSAMPLING(huart
->Init
.OverSampling
));
2886 assert_param(IS_UART_PRESCALER(huart
->Init
.ClockPrescaler
));
2888 /*-------------------------- USART CR1 Configuration -----------------------*/
2889 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
2890 * the UART Word Length, Parity, Mode and oversampling:
2891 * set the M bits according to huart->Init.WordLength value
2892 * set PCE and PS bits according to huart->Init.Parity value
2893 * set TE and RE bits according to huart->Init.Mode value
2894 * set OVER8 bit according to huart->Init.OverSampling value */
2895 tmpreg
= (uint32_t)huart
->Init
.WordLength
| huart
->Init
.Parity
| huart
->Init
.Mode
| huart
->Init
.OverSampling
;
2896 tmpreg
|= (uint32_t)huart
->FifoMode
;
2897 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_FIELDS
, tmpreg
);
2899 /*-------------------------- USART CR2 Configuration -----------------------*/
2900 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
2901 * to huart->Init.StopBits value */
2902 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_STOP
, huart
->Init
.StopBits
);
2904 /*-------------------------- USART CR3 Configuration -----------------------*/
2906 * - UART HardWare Flow Control: set CTSE and RTSE bits according
2907 * to huart->Init.HwFlowCtl value
2908 * - one-bit sampling method versus three samples' majority rule according
2909 * to huart->Init.OneBitSampling (not applicable to LPUART) */
2910 tmpreg
= (uint32_t)huart
->Init
.HwFlowCtl
;
2912 if (!(UART_INSTANCE_LOWPOWER(huart
)))
2914 tmpreg
|= huart
->Init
.OneBitSampling
;
2916 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_FIELDS
, tmpreg
);
2918 /*-------------------------- USART PRESC Configuration -----------------------*/
2920 * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
2921 MODIFY_REG(huart
->Instance
->PRESC
, USART_PRESC_PRESCALER
, huart
->Init
.ClockPrescaler
);
2923 /*-------------------------- USART BRR Configuration -----------------------*/
2924 UART_GETCLOCKSOURCE(huart
, clocksource
);
2926 /* Check LPUART instance */
2927 if (UART_INSTANCE_LOWPOWER(huart
))
2929 /* Retrieve frequency clock */
2930 switch (clocksource
)
2932 case UART_CLOCKSOURCE_D3PCLK1
:
2933 pclk
= HAL_RCCEx_GetD3PCLK1Freq();
2935 case UART_CLOCKSOURCE_PLL2
:
2936 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks
);
2937 pclk
= pll2_clocks
.PLL2_Q_Frequency
;
2939 case UART_CLOCKSOURCE_PLL3
:
2940 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks
);
2941 pclk
= pll3_clocks
.PLL3_Q_Frequency
;
2943 case UART_CLOCKSOURCE_HSI
:
2944 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV
) != 0U)
2946 pclk
= (uint32_t)(HSI_VALUE
>> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
2950 pclk
= (uint32_t) HSI_VALUE
;
2953 case UART_CLOCKSOURCE_CSI
:
2954 pclk
= (uint32_t) CSI_VALUE
;
2956 case UART_CLOCKSOURCE_LSE
:
2957 pclk
= (uint32_t) LSE_VALUE
;
2965 /* If proper clock source reported */
2968 /* Compute clock after Prescaler */
2969 lpuart_ker_ck_pres
= (pclk
/ UARTPrescTable
[huart
->Init
.ClockPrescaler
]);
2971 /* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
2972 if ((lpuart_ker_ck_pres
< (3U * huart
->Init
.BaudRate
)) ||
2973 (lpuart_ker_ck_pres
> (4096U * huart
->Init
.BaudRate
)))
2979 /* Check computed UsartDiv value is in allocated range
2980 (it is forbidden to write values lower than 0x300 in the LPUART_BRR register) */
2981 usartdiv
= (uint32_t)(UART_DIV_LPUART(pclk
, (uint64_t)huart
->Init
.BaudRate
, huart
->Init
.ClockPrescaler
));
2982 if ((usartdiv
>= LPUART_BRR_MIN
) && (usartdiv
<= LPUART_BRR_MAX
))
2984 huart
->Instance
->BRR
= usartdiv
;
2990 } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) || (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
2991 } /* if (pclk != 0) */
2993 /* Check UART Over Sampling to set Baud Rate Register */
2994 else if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
2996 switch (clocksource
)
2998 case UART_CLOCKSOURCE_D2PCLK1
:
2999 pclk
= HAL_RCC_GetPCLK1Freq();
3001 case UART_CLOCKSOURCE_D2PCLK2
:
3002 pclk
= HAL_RCC_GetPCLK2Freq();
3004 case UART_CLOCKSOURCE_PLL2
:
3005 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks
);
3006 pclk
= pll2_clocks
.PLL2_Q_Frequency
;
3008 case UART_CLOCKSOURCE_PLL3
:
3009 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks
);
3010 pclk
= pll3_clocks
.PLL3_Q_Frequency
;
3012 case UART_CLOCKSOURCE_HSI
:
3013 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV
) != 0U)
3015 pclk
= (uint32_t)(HSI_VALUE
>> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
3019 pclk
= (uint32_t) HSI_VALUE
;
3022 case UART_CLOCKSOURCE_CSI
:
3023 pclk
= (uint32_t) CSI_VALUE
;
3025 case UART_CLOCKSOURCE_LSE
:
3026 pclk
= (uint32_t) LSE_VALUE
;
3034 /* USARTDIV must be greater than or equal to 0d16 */
3037 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(pclk
, huart
->Init
.BaudRate
, huart
->Init
.ClockPrescaler
));
3038 if ((usartdiv
>= UART_BRR_MIN
) && (usartdiv
<= UART_BRR_MAX
))
3040 brrtemp
= (uint16_t)(usartdiv
& 0xFFF0U
);
3041 brrtemp
|= (uint16_t)((usartdiv
& (uint16_t)0x000FU
) >> 1U);
3042 huart
->Instance
->BRR
= brrtemp
;
3052 switch (clocksource
)
3054 case UART_CLOCKSOURCE_D2PCLK1
:
3055 pclk
= HAL_RCC_GetPCLK1Freq();
3057 case UART_CLOCKSOURCE_D2PCLK2
:
3058 pclk
= HAL_RCC_GetPCLK2Freq();
3060 case UART_CLOCKSOURCE_PLL2
:
3061 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks
);
3062 pclk
= pll2_clocks
.PLL2_Q_Frequency
;
3064 case UART_CLOCKSOURCE_PLL3
:
3065 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks
);
3066 pclk
= pll3_clocks
.PLL3_Q_Frequency
;
3068 case UART_CLOCKSOURCE_HSI
:
3069 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV
) != 0U)
3071 pclk
= (uint32_t)(HSI_VALUE
>> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
3075 pclk
= (uint32_t) HSI_VALUE
;
3078 case UART_CLOCKSOURCE_CSI
:
3079 pclk
= (uint32_t) CSI_VALUE
;
3081 case UART_CLOCKSOURCE_LSE
:
3082 pclk
= (uint32_t) LSE_VALUE
;
3092 /* USARTDIV must be greater than or equal to 0d16 */
3093 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(pclk
, huart
->Init
.BaudRate
, huart
->Init
.ClockPrescaler
));
3094 if ((usartdiv
>= UART_BRR_MIN
) && (usartdiv
<= UART_BRR_MAX
))
3096 huart
->Instance
->BRR
= usartdiv
;
3105 /* Initialize the number of data to process during RX/TX ISR execution */
3106 huart
->NbTxDataToProcess
= 1;
3107 huart
->NbRxDataToProcess
= 1;
3109 /* Clear ISR function pointers */
3110 huart
->RxISR
= NULL
;
3111 huart
->TxISR
= NULL
;
3117 * @brief Configure the UART peripheral advanced features.
3118 * @param huart UART handle.
3121 void UART_AdvFeatureConfig(UART_HandleTypeDef
*huart
)
3123 /* Check whether the set of advanced features to configure is properly set */
3124 assert_param(IS_UART_ADVFEATURE_INIT(huart
->AdvancedInit
.AdvFeatureInit
));
3126 /* if required, configure TX pin active level inversion */
3127 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_TXINVERT_INIT
))
3129 assert_param(IS_UART_ADVFEATURE_TXINV(huart
->AdvancedInit
.TxPinLevelInvert
));
3130 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_TXINV
, huart
->AdvancedInit
.TxPinLevelInvert
);
3133 /* if required, configure RX pin active level inversion */
3134 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXINVERT_INIT
))
3136 assert_param(IS_UART_ADVFEATURE_RXINV(huart
->AdvancedInit
.RxPinLevelInvert
));
3137 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_RXINV
, huart
->AdvancedInit
.RxPinLevelInvert
);
3140 /* if required, configure data inversion */
3141 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DATAINVERT_INIT
))
3143 assert_param(IS_UART_ADVFEATURE_DATAINV(huart
->AdvancedInit
.DataInvert
));
3144 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_DATAINV
, huart
->AdvancedInit
.DataInvert
);
3147 /* if required, configure RX/TX pins swap */
3148 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_SWAP_INIT
))
3150 assert_param(IS_UART_ADVFEATURE_SWAP(huart
->AdvancedInit
.Swap
));
3151 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_SWAP
, huart
->AdvancedInit
.Swap
);
3154 /* if required, configure RX overrun detection disabling */
3155 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
3157 assert_param(IS_UART_OVERRUN(huart
->AdvancedInit
.OverrunDisable
));
3158 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_OVRDIS
, huart
->AdvancedInit
.OverrunDisable
);
3161 /* if required, configure DMA disabling on reception error */
3162 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DMADISABLEONERROR_INIT
))
3164 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart
->AdvancedInit
.DMADisableonRxError
));
3165 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DDRE
, huart
->AdvancedInit
.DMADisableonRxError
);
3168 /* if required, configure auto Baud rate detection scheme */
3169 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_AUTOBAUDRATE_INIT
))
3171 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart
->Instance
));
3172 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart
->AdvancedInit
.AutoBaudRateEnable
));
3173 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABREN
, huart
->AdvancedInit
.AutoBaudRateEnable
);
3174 /* set auto Baudrate detection parameters if detection is enabled */
3175 if (huart
->AdvancedInit
.AutoBaudRateEnable
== UART_ADVFEATURE_AUTOBAUDRATE_ENABLE
)
3177 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart
->AdvancedInit
.AutoBaudRateMode
));
3178 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABRMODE
, huart
->AdvancedInit
.AutoBaudRateMode
);
3182 /* if required, configure MSB first on communication line */
3183 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_MSBFIRST_INIT
))
3185 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart
->AdvancedInit
.MSBFirst
));
3186 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_MSBFIRST
, huart
->AdvancedInit
.MSBFirst
);
3191 * @brief Check the UART Idle State.
3192 * @param huart UART handle.
3193 * @retval HAL status
3195 HAL_StatusTypeDef
UART_CheckIdleState(UART_HandleTypeDef
*huart
)
3199 /* Initialize the UART ErrorCode */
3200 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
3202 /* Init tickstart for timeout managment*/
3203 tickstart
= HAL_GetTick();
3205 /* Check if the Transmitter is enabled */
3206 if ((huart
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
3208 /* Wait until TEACK flag is set */
3209 if (UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_TEACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
3211 /* Timeout occurred */
3216 /* Check if the Receiver is enabled */
3217 if ((huart
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
3219 /* Wait until REACK flag is set */
3220 if (UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_REACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
3222 /* Timeout occurred */
3227 /* Initialize the UART State */
3228 huart
->gState
= HAL_UART_STATE_READY
;
3229 huart
->RxState
= HAL_UART_STATE_READY
;
3231 __HAL_UNLOCK(huart
);
3237 * @brief Handle UART Communication Timeout.
3238 * @param huart UART handle.
3239 * @param Flag Specifies the UART flag to check
3240 * @param Status Flag status (SET or RESET)
3241 * @param Tickstart Tick start value
3242 * @param Timeout Timeout duration
3243 * @retval HAL status
3245 HAL_StatusTypeDef
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef
*huart
, uint32_t Flag
, FlagStatus Status
,
3246 uint32_t Tickstart
, uint32_t Timeout
)
3248 /* Wait until flag is set */
3249 while ((__HAL_UART_GET_FLAG(huart
, Flag
) ? SET
: RESET
) == Status
)
3251 /* Check for the Timeout */
3252 if (Timeout
!= HAL_MAX_DELAY
)
3254 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
3256 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
3257 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
));
3258 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3260 huart
->gState
= HAL_UART_STATE_READY
;
3261 huart
->RxState
= HAL_UART_STATE_READY
;
3263 __HAL_UNLOCK(huart
);
3268 if (READ_BIT(huart
->Instance
->CR1
, USART_CR1_RE
) != 0U)
3270 if (__HAL_UART_GET_FLAG(huart
, UART_FLAG_RTOF
) == SET
)
3272 /* Clear Receiver Timeout flag*/
3273 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_RTOF
);
3275 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
3276 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
));
3277 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3279 huart
->gState
= HAL_UART_STATE_READY
;
3280 huart
->RxState
= HAL_UART_STATE_READY
;
3281 huart
->ErrorCode
= HAL_UART_ERROR_RTO
;
3283 /* Process Unlocked */
3284 __HAL_UNLOCK(huart
);
3296 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3297 * @param huart UART handle.
3300 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
)
3302 /* Disable TXEIE, TCIE, TXFT interrupts */
3303 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE_TXFNFIE
| USART_CR1_TCIE
));
3304 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_TXFTIE
));
3306 /* At end of Tx process, restore huart->gState to Ready */
3307 huart
->gState
= HAL_UART_STATE_READY
;
3312 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3313 * @param huart UART handle.
3316 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
)
3318 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3319 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
3320 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
3322 /* At end of Rx process, restore huart->RxState to Ready */
3323 huart
->RxState
= HAL_UART_STATE_READY
;
3325 /* Reset RxIsr function pointer */
3326 huart
->RxISR
= NULL
;
3331 * @brief DMA UART transmit process complete callback.
3332 * @param hdma DMA handle.
3335 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
3337 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3339 /* DMA Normal mode */
3340 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
3342 huart
->TxXferCount
= 0U;
3344 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
3345 in the UART CR3 register */
3346 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
3348 /* Enable the UART Transmit Complete Interrupt */
3349 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3351 /* DMA Circular mode */
3354 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3355 /*Call registered Tx complete callback*/
3356 huart
->TxCpltCallback(huart
);
3358 /*Call legacy weak Tx complete callback*/
3359 HAL_UART_TxCpltCallback(huart
);
3360 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3365 * @brief DMA UART transmit process half complete callback.
3366 * @param hdma DMA handle.
3369 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
3371 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3373 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3374 /*Call registered Tx Half complete callback*/
3375 huart
->TxHalfCpltCallback(huart
);
3377 /*Call legacy weak Tx Half complete callback*/
3378 HAL_UART_TxHalfCpltCallback(huart
);
3379 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3383 * @brief DMA UART receive process complete callback.
3384 * @param hdma DMA handle.
3387 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
3389 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3391 /* DMA Normal mode */
3392 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
3394 huart
->RxXferCount
= 0U;
3396 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3397 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
3398 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3400 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3401 in the UART CR3 register */
3402 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
3404 /* At end of Rx process, restore huart->RxState to Ready */
3405 huart
->RxState
= HAL_UART_STATE_READY
;
3408 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3409 /*Call registered Rx complete callback*/
3410 huart
->RxCpltCallback(huart
);
3412 /*Call legacy weak Rx complete callback*/
3413 HAL_UART_RxCpltCallback(huart
);
3414 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3418 * @brief DMA UART receive process half complete callback.
3419 * @param hdma DMA handle.
3422 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
3424 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3426 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3427 /*Call registered Rx Half complete callback*/
3428 huart
->RxHalfCpltCallback(huart
);
3430 /*Call legacy weak Rx Half complete callback*/
3431 HAL_UART_RxHalfCpltCallback(huart
);
3432 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3436 * @brief DMA UART communication error callback.
3437 * @param hdma DMA handle.
3440 static void UART_DMAError(DMA_HandleTypeDef
*hdma
)
3442 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3444 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
3445 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
3447 /* Stop UART DMA Tx request if ongoing */
3448 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
3449 (gstate
== HAL_UART_STATE_BUSY_TX
))
3451 huart
->TxXferCount
= 0U;
3452 UART_EndTxTransfer(huart
);
3455 /* Stop UART DMA Rx request if ongoing */
3456 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
3457 (rxstate
== HAL_UART_STATE_BUSY_RX
))
3459 huart
->RxXferCount
= 0U;
3460 UART_EndRxTransfer(huart
);
3463 huart
->ErrorCode
|= HAL_UART_ERROR_DMA
;
3465 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3466 /*Call registered error callback*/
3467 huart
->ErrorCallback(huart
);
3469 /*Call legacy weak error callback*/
3470 HAL_UART_ErrorCallback(huart
);
3471 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3475 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
3476 * (To be called at end of DMA Abort procedure following error occurrence).
3477 * @param hdma DMA handle.
3480 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
3482 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3483 huart
->RxXferCount
= 0U;
3484 huart
->TxXferCount
= 0U;
3486 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3487 /*Call registered error callback*/
3488 huart
->ErrorCallback(huart
);
3490 /*Call legacy weak error callback*/
3491 HAL_UART_ErrorCallback(huart
);
3492 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3496 * @brief DMA UART Tx communication abort callback, when initiated by user
3497 * (To be called at end of DMA Tx Abort procedure following user abort request).
3498 * @note When this callback is executed, User Abort complete call back is called only if no
3499 * Abort still ongoing for Rx DMA Handle.
3500 * @param hdma DMA handle.
3503 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
3505 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3507 huart
->hdmatx
->XferAbortCallback
= NULL
;
3509 /* Check if an Abort process is still ongoing */
3510 if (huart
->hdmarx
!= NULL
)
3512 if (huart
->hdmarx
->XferAbortCallback
!= NULL
)
3518 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3519 huart
->TxXferCount
= 0U;
3520 huart
->RxXferCount
= 0U;
3522 /* Reset errorCode */
3523 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
3525 /* Clear the Error flags in the ICR register */
3526 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3528 /* Flush the whole TX FIFO (if needed) */
3529 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
3531 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
3534 /* Restore huart->gState and huart->RxState to Ready */
3535 huart
->gState
= HAL_UART_STATE_READY
;
3536 huart
->RxState
= HAL_UART_STATE_READY
;
3538 /* Call user Abort complete callback */
3539 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3540 /* Call registered Abort complete callback */
3541 huart
->AbortCpltCallback(huart
);
3543 /* Call legacy weak Abort complete callback */
3544 HAL_UART_AbortCpltCallback(huart
);
3545 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3550 * @brief DMA UART Rx communication abort callback, when initiated by user
3551 * (To be called at end of DMA Rx Abort procedure following user abort request).
3552 * @note When this callback is executed, User Abort complete call back is called only if no
3553 * Abort still ongoing for Tx DMA Handle.
3554 * @param hdma DMA handle.
3557 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
3559 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3561 huart
->hdmarx
->XferAbortCallback
= NULL
;
3563 /* Check if an Abort process is still ongoing */
3564 if (huart
->hdmatx
!= NULL
)
3566 if (huart
->hdmatx
->XferAbortCallback
!= NULL
)
3572 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3573 huart
->TxXferCount
= 0U;
3574 huart
->RxXferCount
= 0U;
3576 /* Reset errorCode */
3577 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
3579 /* Clear the Error flags in the ICR register */
3580 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3582 /* Discard the received data */
3583 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3585 /* Restore huart->gState and huart->RxState to Ready */
3586 huart
->gState
= HAL_UART_STATE_READY
;
3587 huart
->RxState
= HAL_UART_STATE_READY
;
3589 /* Call user Abort complete callback */
3590 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3591 /* Call registered Abort complete callback */
3592 huart
->AbortCpltCallback(huart
);
3594 /* Call legacy weak Abort complete callback */
3595 HAL_UART_AbortCpltCallback(huart
);
3596 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3601 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
3602 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3603 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3604 * and leads to user Tx Abort Complete callback execution).
3605 * @param hdma DMA handle.
3608 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
3610 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3612 huart
->TxXferCount
= 0U;
3614 /* Flush the whole TX FIFO (if needed) */
3615 if (huart
->FifoMode
== UART_FIFOMODE_ENABLE
)
3617 __HAL_UART_SEND_REQ(huart
, UART_TXDATA_FLUSH_REQUEST
);
3620 /* Restore huart->gState to Ready */
3621 huart
->gState
= HAL_UART_STATE_READY
;
3623 /* Call user Abort complete callback */
3624 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3625 /* Call registered Abort Transmit Complete Callback */
3626 huart
->AbortTransmitCpltCallback(huart
);
3628 /* Call legacy weak Abort Transmit Complete Callback */
3629 HAL_UART_AbortTransmitCpltCallback(huart
);
3630 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3634 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
3635 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
3636 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3637 * and leads to user Rx Abort Complete callback execution).
3638 * @param hdma DMA handle.
3641 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
3643 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
3645 huart
->RxXferCount
= 0U;
3647 /* Clear the Error flags in the ICR register */
3648 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3650 /* Discard the received data */
3651 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3653 /* Restore huart->RxState to Ready */
3654 huart
->RxState
= HAL_UART_STATE_READY
;
3656 /* Call user Abort complete callback */
3657 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3658 /* Call registered Abort Receive Complete Callback */
3659 huart
->AbortReceiveCpltCallback(huart
);
3661 /* Call legacy weak Abort Receive Complete Callback */
3662 HAL_UART_AbortReceiveCpltCallback(huart
);
3663 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3667 * @brief TX interrrupt handler for 7 or 8 bits data word length .
3668 * @note Function is called under interruption only, once
3669 * interruptions have been enabled by HAL_UART_Transmit_IT().
3670 * @param huart UART handle.
3673 static void UART_TxISR_8BIT(UART_HandleTypeDef
*huart
)
3675 /* Check that a Tx process is ongoing */
3676 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3678 if (huart
->TxXferCount
== 0U)
3680 /* Disable the UART Transmit Data Register Empty Interrupt */
3681 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
3683 /* Enable the UART Transmit Complete Interrupt */
3684 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3688 huart
->Instance
->TDR
= (uint8_t)(*huart
->pTxBuffPtr
& (uint8_t)0xFF);
3689 huart
->pTxBuffPtr
++;
3690 huart
->TxXferCount
--;
3696 * @brief TX interrrupt handler for 9 bits data word length.
3697 * @note Function is called under interruption only, once
3698 * interruptions have been enabled by HAL_UART_Transmit_IT().
3699 * @param huart UART handle.
3702 static void UART_TxISR_16BIT(UART_HandleTypeDef
*huart
)
3706 /* Check that a Tx process is ongoing */
3707 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3709 if (huart
->TxXferCount
== 0U)
3711 /* Disable the UART Transmit Data Register Empty Interrupt */
3712 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
3714 /* Enable the UART Transmit Complete Interrupt */
3715 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3719 tmp
= (uint16_t *) huart
->pTxBuffPtr
;
3720 huart
->Instance
->TDR
= (((uint32_t)(*tmp
)) & 0x01FFUL
);
3721 huart
->pTxBuffPtr
+= 2U;
3722 huart
->TxXferCount
--;
3728 * @brief TX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
3729 * @note Function is called under interruption only, once
3730 * interruptions have been enabled by HAL_UART_Transmit_IT().
3731 * @param huart UART handle.
3734 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef
*huart
)
3736 uint16_t nb_tx_data
;
3738 /* Check that a Tx process is ongoing */
3739 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3741 for (nb_tx_data
= huart
->NbTxDataToProcess
; nb_tx_data
> 0U ; nb_tx_data
--)
3743 if (huart
->TxXferCount
== 0U)
3745 /* Disable the TX FIFO threshold interrupt */
3746 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3748 /* Enable the UART Transmit Complete Interrupt */
3749 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3751 break; /* force exit loop */
3753 else if (READ_BIT(huart
->Instance
->ISR
, USART_ISR_TXE_TXFNF
) != 0U)
3755 huart
->Instance
->TDR
= (uint8_t)(*huart
->pTxBuffPtr
& (uint8_t)0xFF);
3756 huart
->pTxBuffPtr
++;
3757 huart
->TxXferCount
--;
3768 * @brief TX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
3769 * @note Function is called under interruption only, once
3770 * interruptions have been enabled by HAL_UART_Transmit_IT().
3771 * @param huart UART handle.
3774 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef
*huart
)
3777 uint16_t nb_tx_data
;
3779 /* Check that a Tx process is ongoing */
3780 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3782 for (nb_tx_data
= huart
->NbTxDataToProcess
; nb_tx_data
> 0U ; nb_tx_data
--)
3784 if (huart
->TxXferCount
== 0U)
3786 /* Disable the TX FIFO threshold interrupt */
3787 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3789 /* Enable the UART Transmit Complete Interrupt */
3790 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3792 break; /* force exit loop */
3794 else if (READ_BIT(huart
->Instance
->ISR
, USART_ISR_TXE_TXFNF
) != 0U)
3796 tmp
= (uint16_t *) huart
->pTxBuffPtr
;
3797 huart
->Instance
->TDR
= (((uint32_t)(*tmp
)) & 0x01FFUL
);
3798 huart
->pTxBuffPtr
+= 2U;
3799 huart
->TxXferCount
--;
3810 * @brief Wrap up transmission in non-blocking mode.
3811 * @param huart pointer to a UART_HandleTypeDef structure that contains
3812 * the configuration information for the specified UART module.
3815 static void UART_EndTransmit_IT(UART_HandleTypeDef
*huart
)
3817 /* Disable the UART Transmit Complete Interrupt */
3818 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3820 /* Tx process is ended, restore huart->gState to Ready */
3821 huart
->gState
= HAL_UART_STATE_READY
;
3823 /* Cleat TxISR function pointer */
3824 huart
->TxISR
= NULL
;
3826 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3827 /*Call registered Tx complete callback*/
3828 huart
->TxCpltCallback(huart
);
3830 /*Call legacy weak Tx complete callback*/
3831 HAL_UART_TxCpltCallback(huart
);
3832 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3836 * @brief RX interrrupt handler for 7 or 8 bits data word length .
3837 * @param huart UART handle.
3840 static void UART_RxISR_8BIT(UART_HandleTypeDef
*huart
)
3842 uint16_t uhMask
= huart
->Mask
;
3845 /* Check that a Rx process is ongoing */
3846 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
3848 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
3849 *huart
->pRxBuffPtr
= (uint8_t)(uhdata
& (uint8_t)uhMask
);
3850 huart
->pRxBuffPtr
++;
3851 huart
->RxXferCount
--;
3853 if (huart
->RxXferCount
== 0U)
3855 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
3856 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
3858 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3859 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3861 /* Rx process is completed, restore huart->RxState to Ready */
3862 huart
->RxState
= HAL_UART_STATE_READY
;
3864 /* Clear RxISR function pointer */
3865 huart
->RxISR
= NULL
;
3867 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3868 /*Call registered Rx complete callback*/
3869 huart
->RxCpltCallback(huart
);
3871 /*Call legacy weak Rx complete callback*/
3872 HAL_UART_RxCpltCallback(huart
);
3873 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3878 /* Clear RXNE interrupt flag */
3879 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3884 * @brief RX interrrupt handler for 9 bits data word length .
3885 * @note Function is called under interruption only, once
3886 * interruptions have been enabled by HAL_UART_Receive_IT()
3887 * @param huart UART handle.
3890 static void UART_RxISR_16BIT(UART_HandleTypeDef
*huart
)
3893 uint16_t uhMask
= huart
->Mask
;
3896 /* Check that a Rx process is ongoing */
3897 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
3899 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
3900 tmp
= (uint16_t *) huart
->pRxBuffPtr
;
3901 *tmp
= (uint16_t)(uhdata
& uhMask
);
3902 huart
->pRxBuffPtr
+= 2U;
3903 huart
->RxXferCount
--;
3905 if (huart
->RxXferCount
== 0U)
3907 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
3908 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
3910 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3911 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3913 /* Rx process is completed, restore huart->RxState to Ready */
3914 huart
->RxState
= HAL_UART_STATE_READY
;
3916 /* Clear RxISR function pointer */
3917 huart
->RxISR
= NULL
;
3919 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3920 /*Call registered Rx complete callback*/
3921 huart
->RxCpltCallback(huart
);
3923 /*Call legacy weak Rx complete callback*/
3924 HAL_UART_RxCpltCallback(huart
);
3925 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3930 /* Clear RXNE interrupt flag */
3931 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3936 * @brief RX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
3937 * @note Function is called under interruption only, once
3938 * interruptions have been enabled by HAL_UART_Receive_IT()
3939 * @param huart UART handle.
3942 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef
*huart
)
3944 uint16_t uhMask
= huart
->Mask
;
3946 uint16_t nb_rx_data
;
3947 uint16_t rxdatacount
;
3949 /* Check that a Rx process is ongoing */
3950 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
3952 for (nb_rx_data
= huart
->NbRxDataToProcess
; nb_rx_data
> 0U ; nb_rx_data
--)
3954 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
3955 *huart
->pRxBuffPtr
= (uint8_t)(uhdata
& (uint8_t)uhMask
);
3956 huart
->pRxBuffPtr
++;
3957 huart
->RxXferCount
--;
3959 if (huart
->RxXferCount
== 0U)
3961 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
3962 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
3964 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3965 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
3967 /* Rx process is completed, restore huart->RxState to Ready */
3968 huart
->RxState
= HAL_UART_STATE_READY
;
3970 /* Clear RxISR function pointer */
3971 huart
->RxISR
= NULL
;
3973 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3974 /*Call registered Rx complete callback*/
3975 huart
->RxCpltCallback(huart
);
3977 /*Call legacy weak Rx complete callback*/
3978 HAL_UART_RxCpltCallback(huart
);
3979 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3983 /* When remaining number of bytes to receive is less than the RX FIFO
3984 threshold, next incoming frames are processed as if FIFO mode was
3985 disabled (i.e. one interrupt per received frame).
3987 rxdatacount
= huart
->RxXferCount
;
3988 if ((rxdatacount
!= 0U) && (rxdatacount
< huart
->NbRxDataToProcess
))
3990 /* Disable the UART RXFT interrupt*/
3991 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_RXFTIE
);
3993 /* Update the RxISR function pointer */
3994 huart
->RxISR
= UART_RxISR_8BIT
;
3996 /* Enable the UART Data Register Not Empty interrupt */
3997 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
4002 /* Clear RXNE interrupt flag */
4003 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
4008 * @brief RX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
4009 * @note Function is called under interruption only, once
4010 * interruptions have been enabled by HAL_UART_Receive_IT()
4011 * @param huart UART handle.
4014 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef
*huart
)
4017 uint16_t uhMask
= huart
->Mask
;
4019 uint16_t nb_rx_data
;
4020 uint16_t rxdatacount
;
4022 /* Check that a Rx process is ongoing */
4023 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
4025 for (nb_rx_data
= huart
->NbRxDataToProcess
; nb_rx_data
> 0U ; nb_rx_data
--)
4027 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
4028 tmp
= (uint16_t *) huart
->pRxBuffPtr
;
4029 *tmp
= (uint16_t)(uhdata
& uhMask
);
4030 huart
->pRxBuffPtr
+= 2U;
4031 huart
->RxXferCount
--;
4033 if (huart
->RxXferCount
== 0U)
4035 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4036 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
4038 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
4039 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
4041 /* Rx process is completed, restore huart->RxState to Ready */
4042 huart
->RxState
= HAL_UART_STATE_READY
;
4044 /* Clear RxISR function pointer */
4045 huart
->RxISR
= NULL
;
4047 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4048 /*Call registered Rx complete callback*/
4049 huart
->RxCpltCallback(huart
);
4051 /*Call legacy weak Rx complete callback*/
4052 HAL_UART_RxCpltCallback(huart
);
4053 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4057 /* When remaining number of bytes to receive is less than the RX FIFO
4058 threshold, next incoming frames are processed as if FIFO mode was
4059 disabled (i.e. one interrupt per received frame).
4061 rxdatacount
= huart
->RxXferCount
;
4062 if ((rxdatacount
!= 0U) && (rxdatacount
< huart
->NbRxDataToProcess
))
4064 /* Disable the UART RXFT interrupt*/
4065 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_RXFTIE
);
4067 /* Update the RxISR function pointer */
4068 huart
->RxISR
= UART_RxISR_16BIT
;
4070 /* Enable the UART Data Register Not Empty interrupt */
4071 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
4076 /* Clear RXNE interrupt flag */
4077 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
4085 #endif /* HAL_UART_MODULE_ENABLED */
4094 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/