2 ******************************************************************************
3 * @file stm32f7xx_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, 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 "stm32f7xx_hal.h"
159 /** @addtogroup STM32F7xx_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 )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
178 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
181 #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
182 #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
188 /* Private macros ------------------------------------------------------------*/
189 /* Private variables ---------------------------------------------------------*/
190 /* Private function prototypes -----------------------------------------------*/
191 /** @addtogroup UART_Private_Functions
194 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
);
195 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
);
196 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
197 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
198 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
199 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
200 static void UART_DMAError(DMA_HandleTypeDef
*hdma
);
201 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
202 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
203 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
204 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
205 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
);
206 static void UART_TxISR_8BIT(UART_HandleTypeDef
*huart
);
207 static void UART_TxISR_16BIT(UART_HandleTypeDef
*huart
);
208 static void UART_EndTransmit_IT(UART_HandleTypeDef
*huart
);
209 static void UART_RxISR_8BIT(UART_HandleTypeDef
*huart
);
210 static void UART_RxISR_16BIT(UART_HandleTypeDef
*huart
);
215 /* Exported functions --------------------------------------------------------*/
217 /** @defgroup UART_Exported_Functions UART Exported Functions
221 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
222 * @brief Initialization and Configuration functions
225 ===============================================================================
226 ##### Initialization and Configuration functions #####
227 ===============================================================================
229 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
230 in asynchronous mode.
231 (+) For the asynchronous mode the parameters below can be configured:
235 (++) Parity: If the parity is enabled, then the MSB bit of the data written
236 in the data register is transmitted but is changed by the parity bit.
237 (++) Hardware flow control
238 (++) Receiver/transmitter modes
239 (++) Over Sampling Method
240 (++) One-Bit Sampling Method
241 (+) For the asynchronous mode, the following advanced features can be configured as well:
242 (++) TX and/or RX pin level inversion
243 (++) data logical level inversion
244 (++) RX and TX pins swap
245 (++) RX overrun detection disabling
246 (++) DMA disabling on RX error
247 (++) MSB first on communication line
248 (++) auto Baud rate detection
250 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
251 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
252 and UART multiprocessor mode configuration procedures (details for the procedures
253 are available in reference manual).
257 Depending on the frame length defined by the M1 and M0 bits (7-bit,
258 8-bit or 9-bit), the possible UART formats are listed in the
261 Table 1. UART frame format.
262 +-----------------------------------------------------------------------+
263 | M1 bit | M0 bit | PCE bit | UART frame |
264 |---------|---------|-----------|---------------------------------------|
265 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
266 |---------|---------|-----------|---------------------------------------|
267 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
268 |---------|---------|-----------|---------------------------------------|
269 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
270 |---------|---------|-----------|---------------------------------------|
271 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
272 |---------|---------|-----------|---------------------------------------|
273 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
274 |---------|---------|-----------|---------------------------------------|
275 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
276 +-----------------------------------------------------------------------+
282 * @brief Initialize the UART mode according to the specified
283 * parameters in the UART_InitTypeDef and initialize the associated handle.
284 * @param huart UART handle.
287 HAL_StatusTypeDef
HAL_UART_Init(UART_HandleTypeDef
*huart
)
289 /* Check the UART handle allocation */
295 if (huart
->Init
.HwFlowCtl
!= UART_HWCONTROL_NONE
)
297 /* Check the parameters */
298 assert_param(IS_UART_HWFLOW_INSTANCE(huart
->Instance
));
302 /* Check the parameters */
303 assert_param(IS_UART_INSTANCE(huart
->Instance
));
306 if (huart
->gState
== HAL_UART_STATE_RESET
)
308 /* Allocate lock resource and initialize it */
309 huart
->Lock
= HAL_UNLOCKED
;
311 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
312 UART_InitCallbacksToDefault(huart
);
314 if (huart
->MspInitCallback
== NULL
)
316 huart
->MspInitCallback
= HAL_UART_MspInit
;
319 /* Init the low level hardware */
320 huart
->MspInitCallback(huart
);
322 /* Init the low level hardware : GPIO, CLOCK */
323 HAL_UART_MspInit(huart
);
324 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
327 huart
->gState
= HAL_UART_STATE_BUSY
;
329 /* Disable the Peripheral */
330 __HAL_UART_DISABLE(huart
);
332 /* Set the UART Communication parameters */
333 if (UART_SetConfig(huart
) == HAL_ERROR
)
338 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
340 UART_AdvFeatureConfig(huart
);
343 /* In asynchronous mode, the following bits must be kept cleared:
344 - LINEN and CLKEN bits in the USART_CR2 register,
345 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
346 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
347 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
349 /* Enable the Peripheral */
350 __HAL_UART_ENABLE(huart
);
352 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
353 return (UART_CheckIdleState(huart
));
357 * @brief Initialize the half-duplex mode according to the specified
358 * parameters in the UART_InitTypeDef and creates the associated handle.
359 * @param huart UART handle.
362 HAL_StatusTypeDef
HAL_HalfDuplex_Init(UART_HandleTypeDef
*huart
)
364 /* Check the UART handle allocation */
370 /* Check UART instance */
371 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart
->Instance
));
373 if (huart
->gState
== HAL_UART_STATE_RESET
)
375 /* Allocate lock resource and initialize it */
376 huart
->Lock
= HAL_UNLOCKED
;
378 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
379 UART_InitCallbacksToDefault(huart
);
381 if (huart
->MspInitCallback
== NULL
)
383 huart
->MspInitCallback
= HAL_UART_MspInit
;
386 /* Init the low level hardware */
387 huart
->MspInitCallback(huart
);
389 /* Init the low level hardware : GPIO, CLOCK */
390 HAL_UART_MspInit(huart
);
391 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
394 huart
->gState
= HAL_UART_STATE_BUSY
;
396 /* Disable the Peripheral */
397 __HAL_UART_DISABLE(huart
);
399 /* Set the UART Communication parameters */
400 if (UART_SetConfig(huart
) == HAL_ERROR
)
405 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
407 UART_AdvFeatureConfig(huart
);
410 /* In half-duplex mode, the following bits must be kept cleared:
411 - LINEN and CLKEN bits in the USART_CR2 register,
412 - SCEN and IREN bits in the USART_CR3 register.*/
413 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
414 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_SCEN
));
416 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
417 SET_BIT(huart
->Instance
->CR3
, USART_CR3_HDSEL
);
419 /* Enable the Peripheral */
420 __HAL_UART_ENABLE(huart
);
422 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
423 return (UART_CheckIdleState(huart
));
428 * @brief Initialize the LIN mode according to the specified
429 * parameters in the UART_InitTypeDef and creates the associated handle.
430 * @param huart UART handle.
431 * @param BreakDetectLength Specifies the LIN break detection length.
432 * This parameter can be one of the following values:
433 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
434 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
437 HAL_StatusTypeDef
HAL_LIN_Init(UART_HandleTypeDef
*huart
, uint32_t BreakDetectLength
)
439 /* Check the UART handle allocation */
445 /* Check the LIN UART instance */
446 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
447 /* Check the Break detection length parameter */
448 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength
));
450 /* LIN mode limited to 16-bit oversampling only */
451 if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
455 /* LIN mode limited to 8-bit data length */
456 if (huart
->Init
.WordLength
!= UART_WORDLENGTH_8B
)
461 if (huart
->gState
== HAL_UART_STATE_RESET
)
463 /* Allocate lock resource and initialize it */
464 huart
->Lock
= HAL_UNLOCKED
;
466 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
467 UART_InitCallbacksToDefault(huart
);
469 if (huart
->MspInitCallback
== NULL
)
471 huart
->MspInitCallback
= HAL_UART_MspInit
;
474 /* Init the low level hardware */
475 huart
->MspInitCallback(huart
);
477 /* Init the low level hardware : GPIO, CLOCK */
478 HAL_UART_MspInit(huart
);
479 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
482 huart
->gState
= HAL_UART_STATE_BUSY
;
484 /* Disable the Peripheral */
485 __HAL_UART_DISABLE(huart
);
487 /* Set the UART Communication parameters */
488 if (UART_SetConfig(huart
) == HAL_ERROR
)
493 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
495 UART_AdvFeatureConfig(huart
);
498 /* In LIN mode, the following bits must be kept cleared:
499 - LINEN and CLKEN bits in the USART_CR2 register,
500 - SCEN and IREN bits in the USART_CR3 register.*/
501 CLEAR_BIT(huart
->Instance
->CR2
, USART_CR2_CLKEN
);
502 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
| USART_CR3_SCEN
));
504 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
505 SET_BIT(huart
->Instance
->CR2
, USART_CR2_LINEN
);
507 /* Set the USART LIN Break detection length. */
508 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_LBDL
, BreakDetectLength
);
510 /* Enable the Peripheral */
511 __HAL_UART_ENABLE(huart
);
513 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
514 return (UART_CheckIdleState(huart
));
519 * @brief Initialize the multiprocessor mode according to the specified
520 * parameters in the UART_InitTypeDef and initialize the associated handle.
521 * @param huart UART handle.
522 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
523 * @param WakeUpMethod Specifies the UART wakeup method.
524 * This parameter can be one of the following values:
525 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
526 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
527 * @note If the user resorts to idle line detection wake up, the Address parameter
528 * is useless and ignored by the initialization function.
529 * @note If the user resorts to address mark wake up, the address length detection
530 * is configured by default to 4 bits only. For the UART to be able to
531 * manage 6-, 7- or 8-bit long addresses detection, the API
532 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
533 * HAL_MultiProcessor_Init().
536 HAL_StatusTypeDef
HAL_MultiProcessor_Init(UART_HandleTypeDef
*huart
, uint8_t Address
, uint32_t WakeUpMethod
)
538 /* Check the UART handle allocation */
544 /* Check the wake up method parameter */
545 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod
));
547 if (huart
->gState
== HAL_UART_STATE_RESET
)
549 /* Allocate lock resource and initialize it */
550 huart
->Lock
= HAL_UNLOCKED
;
552 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
553 UART_InitCallbacksToDefault(huart
);
555 if (huart
->MspInitCallback
== NULL
)
557 huart
->MspInitCallback
= HAL_UART_MspInit
;
560 /* Init the low level hardware */
561 huart
->MspInitCallback(huart
);
563 /* Init the low level hardware : GPIO, CLOCK */
564 HAL_UART_MspInit(huart
);
565 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
568 huart
->gState
= HAL_UART_STATE_BUSY
;
570 /* Disable the Peripheral */
571 __HAL_UART_DISABLE(huart
);
573 /* Set the UART Communication parameters */
574 if (UART_SetConfig(huart
) == HAL_ERROR
)
579 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
581 UART_AdvFeatureConfig(huart
);
584 /* In multiprocessor mode, the following bits must be kept cleared:
585 - LINEN and CLKEN bits in the USART_CR2 register,
586 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
587 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
588 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
590 if (WakeUpMethod
== UART_WAKEUPMETHOD_ADDRESSMARK
)
592 /* If address mark wake up method is chosen, set the USART address node */
593 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)Address
<< UART_CR2_ADDRESS_LSB_POS
));
596 /* Set the wake up method by setting the WAKE bit in the CR1 register */
597 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_WAKE
, WakeUpMethod
);
599 /* Enable the Peripheral */
600 __HAL_UART_ENABLE(huart
);
602 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
603 return (UART_CheckIdleState(huart
));
608 * @brief DeInitialize the UART peripheral.
609 * @param huart UART handle.
612 HAL_StatusTypeDef
HAL_UART_DeInit(UART_HandleTypeDef
*huart
)
614 /* Check the UART handle allocation */
620 /* Check the parameters */
621 assert_param(IS_UART_INSTANCE(huart
->Instance
));
623 huart
->gState
= HAL_UART_STATE_BUSY
;
625 /* Disable the Peripheral */
626 __HAL_UART_DISABLE(huart
);
628 huart
->Instance
->CR1
= 0x0U
;
629 huart
->Instance
->CR2
= 0x0U
;
630 huart
->Instance
->CR3
= 0x0U
;
632 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
633 if (huart
->MspDeInitCallback
== NULL
)
635 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
;
637 /* DeInit the low level hardware */
638 huart
->MspDeInitCallback(huart
);
640 /* DeInit the low level hardware */
641 HAL_UART_MspDeInit(huart
);
642 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
644 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
645 huart
->gState
= HAL_UART_STATE_RESET
;
646 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
, pUART_CallbackTypeDef pCallback
)
709 HAL_StatusTypeDef status
= HAL_OK
;
711 if (pCallback
== NULL
)
713 /* Update the error code */
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
;
762 case HAL_UART_MSPINIT_CB_ID
:
763 huart
->MspInitCallback
= pCallback
;
766 case HAL_UART_MSPDEINIT_CB_ID
:
767 huart
->MspDeInitCallback
= pCallback
;
771 /* Update the error code */
772 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
774 /* Return error status */
779 else if (huart
->gState
== HAL_UART_STATE_RESET
)
783 case HAL_UART_MSPINIT_CB_ID
:
784 huart
->MspInitCallback
= pCallback
;
787 case HAL_UART_MSPDEINIT_CB_ID
:
788 huart
->MspDeInitCallback
= pCallback
;
792 /* Update the error code */
793 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
795 /* Return error status */
802 /* Update the error code */
803 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
805 /* Return error status */
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
;
843 if (HAL_UART_STATE_READY
== huart
->gState
)
847 case HAL_UART_TX_HALFCOMPLETE_CB_ID
:
848 huart
->TxHalfCpltCallback
= HAL_UART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
851 case HAL_UART_TX_COMPLETE_CB_ID
:
852 huart
->TxCpltCallback
= HAL_UART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
855 case HAL_UART_RX_HALFCOMPLETE_CB_ID
:
856 huart
->RxHalfCpltCallback
= HAL_UART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
859 case HAL_UART_RX_COMPLETE_CB_ID
:
860 huart
->RxCpltCallback
= HAL_UART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
863 case HAL_UART_ERROR_CB_ID
:
864 huart
->ErrorCallback
= HAL_UART_ErrorCallback
; /* Legacy weak ErrorCallback */
867 case HAL_UART_ABORT_COMPLETE_CB_ID
:
868 huart
->AbortCpltCallback
= HAL_UART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
871 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID
:
872 huart
->AbortTransmitCpltCallback
= HAL_UART_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
875 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID
:
876 huart
->AbortReceiveCpltCallback
= HAL_UART_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
879 case HAL_UART_MSPINIT_CB_ID
:
880 huart
->MspInitCallback
= HAL_UART_MspInit
; /* Legacy weak MspInitCallback */
883 case HAL_UART_MSPDEINIT_CB_ID
:
884 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
; /* Legacy weak MspDeInitCallback */
888 /* Update the error code */
889 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
891 /* Return error status */
896 else if (HAL_UART_STATE_RESET
== huart
->gState
)
900 case HAL_UART_MSPINIT_CB_ID
:
901 huart
->MspInitCallback
= HAL_UART_MspInit
;
904 case HAL_UART_MSPDEINIT_CB_ID
:
905 huart
->MspDeInitCallback
= HAL_UART_MspDeInit
;
909 /* Update the error code */
910 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
912 /* Return error status */
919 /* Update the error code */
920 huart
->ErrorCode
|= HAL_UART_ERROR_INVALID_CALLBACK
;
922 /* Return error status */
931 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
937 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
938 * @brief UART Transmit/Receive functions
941 ===============================================================================
942 ##### IO operation functions #####
943 ===============================================================================
944 This subsection provides a set of functions allowing to manage the UART asynchronous
945 and Half duplex data transfers.
947 (#) There are two mode of transfer:
948 (+) Blocking mode: The communication is performed in polling mode.
949 The HAL status of all data processing is returned by the same function
950 after finishing transfer.
951 (+) Non-Blocking mode: The communication is performed using Interrupts
952 or DMA, These API's return the HAL status.
953 The end of the data processing will be indicated through the
954 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
956 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
957 will be executed respectively at the end of the transmit or Receive process
958 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
960 (#) Blocking mode API's are :
961 (+) HAL_UART_Transmit()
962 (+) HAL_UART_Receive()
964 (#) Non-Blocking mode API's with Interrupt are :
965 (+) HAL_UART_Transmit_IT()
966 (+) HAL_UART_Receive_IT()
967 (+) HAL_UART_IRQHandler()
969 (#) Non-Blocking mode API's with DMA are :
970 (+) HAL_UART_Transmit_DMA()
971 (+) HAL_UART_Receive_DMA()
972 (+) HAL_UART_DMAPause()
973 (+) HAL_UART_DMAResume()
974 (+) HAL_UART_DMAStop()
976 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
977 (+) HAL_UART_TxHalfCpltCallback()
978 (+) HAL_UART_TxCpltCallback()
979 (+) HAL_UART_RxHalfCpltCallback()
980 (+) HAL_UART_RxCpltCallback()
981 (+) HAL_UART_ErrorCallback()
983 (#) Non-Blocking mode transfers could be aborted using Abort API's :
985 (+) HAL_UART_AbortTransmit()
986 (+) HAL_UART_AbortReceive()
987 (+) HAL_UART_Abort_IT()
988 (+) HAL_UART_AbortTransmit_IT()
989 (+) HAL_UART_AbortReceive_IT()
991 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
992 (+) HAL_UART_AbortCpltCallback()
993 (+) HAL_UART_AbortTransmitCpltCallback()
994 (+) HAL_UART_AbortReceiveCpltCallback()
996 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
997 Errors are handled as follows :
998 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
999 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
1000 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
1001 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
1002 If user wants to abort it, Abort services should be called by user.
1003 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1004 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1005 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1007 -@- In the Half duplex communication, it is forbidden to run the transmit
1008 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1015 * @brief Send an amount of data in blocking mode.
1016 * @param huart UART handle.
1017 * @param pData Pointer to data buffer.
1018 * @param Size Amount of data to be sent.
1019 * @param Timeout Timeout duration.
1020 * @retval HAL status
1022 HAL_StatusTypeDef
HAL_UART_Transmit(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
1024 uint8_t *pdata8bits
;
1025 uint16_t *pdata16bits
;
1028 /* Check that a Tx process is not already ongoing */
1029 if (huart
->gState
== HAL_UART_STATE_READY
)
1031 if ((pData
== NULL
) || (Size
== 0U))
1036 /* Process Locked */
1039 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1040 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1042 /* Init tickstart for timeout managment*/
1043 tickstart
= HAL_GetTick();
1045 huart
->TxXferSize
= Size
;
1046 huart
->TxXferCount
= Size
;
1048 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1049 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1052 pdata16bits
= (uint16_t *) pData
;
1060 while (huart
->TxXferCount
> 0U)
1062 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1066 if (pdata8bits
== NULL
)
1068 huart
->Instance
->TDR
= (uint16_t)(*pdata16bits
& 0x01FFU
);
1073 huart
->Instance
->TDR
= (uint8_t)(*pdata8bits
& 0xFFU
);
1076 huart
->TxXferCount
--;
1079 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1084 /* At end of Tx process, restore huart->gState to Ready */
1085 huart
->gState
= HAL_UART_STATE_READY
;
1087 /* Process Unlocked */
1088 __HAL_UNLOCK(huart
);
1099 * @brief Receive an amount of data in blocking mode.
1100 * @param huart UART handle.
1101 * @param pData Pointer to data buffer.
1102 * @param Size Amount of data to be received.
1103 * @param Timeout Timeout duration.
1104 * @retval HAL status
1106 HAL_StatusTypeDef
HAL_UART_Receive(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
1108 uint8_t *pdata8bits
;
1109 uint16_t *pdata16bits
;
1113 /* Check that a Rx process is not already ongoing */
1114 if (huart
->RxState
== HAL_UART_STATE_READY
)
1116 if ((pData
== NULL
) || (Size
== 0U))
1121 /* Process Locked */
1124 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1125 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1127 /* Init tickstart for timeout managment*/
1128 tickstart
= HAL_GetTick();
1130 huart
->RxXferSize
= Size
;
1131 huart
->RxXferCount
= Size
;
1133 /* Computation of UART mask to apply to RDR register */
1134 UART_MASK_COMPUTATION(huart
);
1135 uhMask
= huart
->Mask
;
1137 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1138 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1141 pdata16bits
= (uint16_t *) pData
;
1149 /* as long as data have to be received */
1150 while (huart
->RxXferCount
> 0U)
1152 if (UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1156 if (pdata8bits
== NULL
)
1158 *pdata16bits
= (uint16_t)(huart
->Instance
->RDR
& uhMask
);
1163 *pdata8bits
= (uint8_t)(huart
->Instance
->RDR
& (uint8_t)uhMask
);
1166 huart
->RxXferCount
--;
1169 /* At end of Rx process, restore huart->RxState to Ready */
1170 huart
->RxState
= HAL_UART_STATE_READY
;
1172 /* Process Unlocked */
1173 __HAL_UNLOCK(huart
);
1184 * @brief Send an amount of data in interrupt mode.
1185 * @param huart UART handle.
1186 * @param pData Pointer to data buffer.
1187 * @param Size Amount of data to be sent.
1188 * @retval HAL status
1190 HAL_StatusTypeDef
HAL_UART_Transmit_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1192 /* Check that a Tx process is not already ongoing */
1193 if (huart
->gState
== HAL_UART_STATE_READY
)
1195 if ((pData
== NULL
) || (Size
== 0U))
1200 /* Process Locked */
1203 huart
->pTxBuffPtr
= pData
;
1204 huart
->TxXferSize
= Size
;
1205 huart
->TxXferCount
= Size
;
1206 huart
->TxISR
= NULL
;
1208 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1209 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1211 /* Set the Tx ISR function pointer according to the data word length */
1212 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1214 huart
->TxISR
= UART_TxISR_16BIT
;
1218 huart
->TxISR
= UART_TxISR_8BIT
;
1221 /* Process Unlocked */
1222 __HAL_UNLOCK(huart
);
1224 /* Enable the Transmit Data Register Empty interrupt */
1225 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
1236 * @brief Receive an amount of data in interrupt mode.
1237 * @param huart UART handle.
1238 * @param pData Pointer to data buffer.
1239 * @param Size Amount of data to be received.
1240 * @retval HAL status
1242 HAL_StatusTypeDef
HAL_UART_Receive_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1244 /* Check that a Rx process is not already ongoing */
1245 if (huart
->RxState
== HAL_UART_STATE_READY
)
1247 if ((pData
== NULL
) || (Size
== 0U))
1252 /* Process Locked */
1255 huart
->pRxBuffPtr
= pData
;
1256 huart
->RxXferSize
= Size
;
1257 huart
->RxXferCount
= Size
;
1258 huart
->RxISR
= NULL
;
1260 /* Computation of UART mask to apply to RDR register */
1261 UART_MASK_COMPUTATION(huart
);
1263 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1264 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1266 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1267 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1269 /* Set the Rx ISR function pointer according to the data word length */
1270 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1272 huart
->RxISR
= UART_RxISR_16BIT
;
1276 huart
->RxISR
= UART_RxISR_8BIT
;
1279 /* Process Unlocked */
1280 __HAL_UNLOCK(huart
);
1282 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
1283 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE
);
1294 * @brief Send an amount of data in DMA mode.
1295 * @param huart UART handle.
1296 * @param pData Pointer to data buffer.
1297 * @param Size Amount of data to be sent.
1298 * @retval HAL status
1300 HAL_StatusTypeDef
HAL_UART_Transmit_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1302 /* Check that a Tx process is not already ongoing */
1303 if (huart
->gState
== HAL_UART_STATE_READY
)
1305 if ((pData
== NULL
) || (Size
== 0U))
1310 /* Process Locked */
1313 huart
->pTxBuffPtr
= pData
;
1314 huart
->TxXferSize
= Size
;
1315 huart
->TxXferCount
= Size
;
1317 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1318 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
1320 if (huart
->hdmatx
!= NULL
)
1322 /* Set the UART DMA transfer complete callback */
1323 huart
->hdmatx
->XferCpltCallback
= UART_DMATransmitCplt
;
1325 /* Set the UART DMA Half transfer complete callback */
1326 huart
->hdmatx
->XferHalfCpltCallback
= UART_DMATxHalfCplt
;
1328 /* Set the DMA error callback */
1329 huart
->hdmatx
->XferErrorCallback
= UART_DMAError
;
1331 /* Set the DMA abort callback */
1332 huart
->hdmatx
->XferAbortCallback
= NULL
;
1334 /* Enable the UART transmit DMA channel */
1335 if (HAL_DMA_Start_IT(huart
->hdmatx
, (uint32_t)huart
->pTxBuffPtr
, (uint32_t)&huart
->Instance
->TDR
, Size
) != HAL_OK
)
1337 /* Set error code to DMA */
1338 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1340 /* Process Unlocked */
1341 __HAL_UNLOCK(huart
);
1343 /* Restore huart->gState to ready */
1344 huart
->gState
= HAL_UART_STATE_READY
;
1349 /* Clear the TC flag in the ICR register */
1350 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_TCF
);
1352 /* Process Unlocked */
1353 __HAL_UNLOCK(huart
);
1355 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1356 in the UART CR3 register */
1357 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1368 * @brief Receive an amount of data in DMA mode.
1369 * @note When the UART parity is enabled (PCE = 1), the received data contain
1370 * the parity bit (MSB position).
1371 * @param huart UART handle.
1372 * @param pData Pointer to data buffer.
1373 * @param Size Amount of data to be received.
1374 * @retval HAL status
1376 HAL_StatusTypeDef
HAL_UART_Receive_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1378 /* Check that a Rx process is not already ongoing */
1379 if (huart
->RxState
== HAL_UART_STATE_READY
)
1381 if ((pData
== NULL
) || (Size
== 0U))
1386 /* Process Locked */
1389 huart
->pRxBuffPtr
= pData
;
1390 huart
->RxXferSize
= Size
;
1392 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1393 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1395 if (huart
->hdmarx
!= NULL
)
1397 /* Set the UART DMA transfer complete callback */
1398 huart
->hdmarx
->XferCpltCallback
= UART_DMAReceiveCplt
;
1400 /* Set the UART DMA Half transfer complete callback */
1401 huart
->hdmarx
->XferHalfCpltCallback
= UART_DMARxHalfCplt
;
1403 /* Set the DMA error callback */
1404 huart
->hdmarx
->XferErrorCallback
= UART_DMAError
;
1406 /* Set the DMA abort callback */
1407 huart
->hdmarx
->XferAbortCallback
= NULL
;
1409 /* Enable the DMA channel */
1410 if (HAL_DMA_Start_IT(huart
->hdmarx
, (uint32_t)&huart
->Instance
->RDR
, (uint32_t)huart
->pRxBuffPtr
, Size
) != HAL_OK
)
1412 /* Set error code to DMA */
1413 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1415 /* Process Unlocked */
1416 __HAL_UNLOCK(huart
);
1418 /* Restore huart->gState to ready */
1419 huart
->gState
= HAL_UART_STATE_READY
;
1424 /* Process Unlocked */
1425 __HAL_UNLOCK(huart
);
1427 /* Enable the UART Parity Error Interrupt */
1428 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1430 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1431 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1433 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1434 in the UART CR3 register */
1435 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1446 * @brief Pause the DMA Transfer.
1447 * @param huart UART handle.
1448 * @retval HAL status
1450 HAL_StatusTypeDef
HAL_UART_DMAPause(UART_HandleTypeDef
*huart
)
1452 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
1453 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
1455 /* Process Locked */
1458 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
1459 (gstate
== HAL_UART_STATE_BUSY_TX
))
1461 /* Disable the UART DMA Tx request */
1462 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1464 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
1465 (rxstate
== HAL_UART_STATE_BUSY_RX
))
1467 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1468 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1469 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1471 /* Disable the UART DMA Rx request */
1472 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1475 /* Process Unlocked */
1476 __HAL_UNLOCK(huart
);
1482 * @brief Resume the DMA Transfer.
1483 * @param huart UART handle.
1484 * @retval HAL status
1486 HAL_StatusTypeDef
HAL_UART_DMAResume(UART_HandleTypeDef
*huart
)
1488 /* Process Locked */
1491 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1493 /* Enable the UART DMA Tx request */
1494 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1496 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1498 /* Clear the Overrun flag before resuming the Rx transfer */
1499 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
);
1501 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1502 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1503 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1505 /* Enable the UART DMA Rx request */
1506 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1509 /* Process Unlocked */
1510 __HAL_UNLOCK(huart
);
1516 * @brief Stop the DMA Transfer.
1517 * @param huart UART handle.
1518 * @retval HAL status
1520 HAL_StatusTypeDef
HAL_UART_DMAStop(UART_HandleTypeDef
*huart
)
1522 /* The Lock is not implemented on this API to allow the user application
1523 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1524 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1525 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1526 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1527 the stream and the corresponding call back is executed. */
1529 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
1530 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
1532 /* Stop UART DMA Tx request if ongoing */
1533 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
1534 (gstate
== HAL_UART_STATE_BUSY_TX
))
1536 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1538 /* Abort the UART DMA Tx channel */
1539 if (huart
->hdmatx
!= NULL
)
1541 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1543 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1545 /* Set error code to DMA */
1546 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1553 UART_EndTxTransfer(huart
);
1556 /* Stop UART DMA Rx request if ongoing */
1557 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
1558 (rxstate
== HAL_UART_STATE_BUSY_RX
))
1560 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1562 /* Abort the UART DMA Rx channel */
1563 if (huart
->hdmarx
!= NULL
)
1565 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1567 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1569 /* Set error code to DMA */
1570 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1577 UART_EndRxTransfer(huart
);
1584 * @brief Abort ongoing transfers (blocking mode).
1585 * @param huart UART handle.
1586 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1587 * This procedure performs following operations :
1588 * - Disable UART Interrupts (Tx and Rx)
1589 * - Disable the DMA transfer in the peripheral register (if enabled)
1590 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1591 * - Set handle State to READY
1592 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1593 * @retval HAL status
1595 HAL_StatusTypeDef
HAL_UART_Abort(UART_HandleTypeDef
*huart
)
1597 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1598 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
1599 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1601 /* Disable the UART DMA Tx request if enabled */
1602 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1604 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1606 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1607 if (huart
->hdmatx
!= NULL
)
1609 /* Set the UART DMA Abort callback to Null.
1610 No call back execution at end of DMA abort procedure */
1611 huart
->hdmatx
->XferAbortCallback
= NULL
;
1613 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1615 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1617 /* Set error code to DMA */
1618 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1626 /* Disable the UART DMA Rx request if enabled */
1627 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1629 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1631 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1632 if (huart
->hdmarx
!= NULL
)
1634 /* Set the UART DMA Abort callback to Null.
1635 No call back execution at end of DMA abort procedure */
1636 huart
->hdmarx
->XferAbortCallback
= NULL
;
1638 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1640 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1642 /* Set error code to DMA */
1643 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1651 /* Reset Tx and Rx transfer counters */
1652 huart
->TxXferCount
= 0U;
1653 huart
->RxXferCount
= 0U;
1655 /* Clear the Error flags in the ICR register */
1656 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1659 /* Discard the received data */
1660 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1662 /* Restore huart->gState and huart->RxState to Ready */
1663 huart
->gState
= HAL_UART_STATE_READY
;
1664 huart
->RxState
= HAL_UART_STATE_READY
;
1666 /* Reset Handle ErrorCode to No Error */
1667 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1673 * @brief Abort ongoing Transmit transfer (blocking mode).
1674 * @param huart UART handle.
1675 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1676 * This procedure performs following operations :
1677 * - Disable UART Interrupts (Tx)
1678 * - Disable the DMA transfer in the peripheral register (if enabled)
1679 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1680 * - Set handle State to READY
1681 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1682 * @retval HAL status
1684 HAL_StatusTypeDef
HAL_UART_AbortTransmit(UART_HandleTypeDef
*huart
)
1686 /* Disable TXEIE and TCIE interrupts */
1687 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1689 /* Disable the UART DMA Tx request if enabled */
1690 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1692 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1694 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1695 if (huart
->hdmatx
!= NULL
)
1697 /* Set the UART DMA Abort callback to Null.
1698 No call back execution at end of DMA abort procedure */
1699 huart
->hdmatx
->XferAbortCallback
= NULL
;
1701 if (HAL_DMA_Abort(huart
->hdmatx
) != HAL_OK
)
1703 if (HAL_DMA_GetError(huart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1705 /* Set error code to DMA */
1706 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1714 /* Reset Tx transfer counter */
1715 huart
->TxXferCount
= 0U;
1718 /* Restore huart->gState to Ready */
1719 huart
->gState
= HAL_UART_STATE_READY
;
1725 * @brief Abort ongoing Receive transfer (blocking mode).
1726 * @param huart UART handle.
1727 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1728 * This procedure performs following operations :
1729 * - Disable UART Interrupts (Rx)
1730 * - Disable the DMA transfer in the peripheral register (if enabled)
1731 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1732 * - Set handle State to READY
1733 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1734 * @retval HAL status
1736 HAL_StatusTypeDef
HAL_UART_AbortReceive(UART_HandleTypeDef
*huart
)
1738 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1739 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1740 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1742 /* Disable the UART DMA Rx request if enabled */
1743 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1745 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1747 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1748 if (huart
->hdmarx
!= NULL
)
1750 /* Set the UART DMA Abort callback to Null.
1751 No call back execution at end of DMA abort procedure */
1752 huart
->hdmarx
->XferAbortCallback
= NULL
;
1754 if (HAL_DMA_Abort(huart
->hdmarx
) != HAL_OK
)
1756 if (HAL_DMA_GetError(huart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1758 /* Set error code to DMA */
1759 huart
->ErrorCode
= HAL_UART_ERROR_DMA
;
1767 /* Reset Rx transfer counter */
1768 huart
->RxXferCount
= 0U;
1770 /* Clear the Error flags in the ICR register */
1771 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1773 /* Discard the received data */
1774 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1776 /* Restore huart->RxState to Ready */
1777 huart
->RxState
= HAL_UART_STATE_READY
;
1783 * @brief Abort ongoing transfers (Interrupt mode).
1784 * @param huart UART handle.
1785 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1786 * This procedure performs following operations :
1787 * - Disable UART Interrupts (Tx and Rx)
1788 * - Disable the DMA transfer in the peripheral register (if enabled)
1789 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1790 * - Set handle State to READY
1791 * - At abort completion, call user abort complete callback
1792 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1793 * considered as completed only when user abort complete callback is executed (not when exiting function).
1794 * @retval HAL status
1796 HAL_StatusTypeDef
HAL_UART_Abort_IT(UART_HandleTypeDef
*huart
)
1798 uint32_t abortcplt
= 1U;
1800 /* Disable interrupts */
1801 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
| USART_CR1_TCIE
));
1802 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1804 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1805 before any call to DMA Abort functions */
1806 /* DMA Tx Handle is valid */
1807 if (huart
->hdmatx
!= NULL
)
1809 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1810 Otherwise, set it to NULL */
1811 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1813 huart
->hdmatx
->XferAbortCallback
= UART_DMATxAbortCallback
;
1817 huart
->hdmatx
->XferAbortCallback
= NULL
;
1820 /* DMA Rx Handle is valid */
1821 if (huart
->hdmarx
!= NULL
)
1823 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1824 Otherwise, set it to NULL */
1825 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1827 huart
->hdmarx
->XferAbortCallback
= UART_DMARxAbortCallback
;
1831 huart
->hdmarx
->XferAbortCallback
= NULL
;
1835 /* Disable the UART DMA Tx request if enabled */
1836 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1838 /* Disable DMA Tx at UART level */
1839 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1841 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1842 if (huart
->hdmatx
!= NULL
)
1844 /* UART Tx DMA Abort callback has already been initialised :
1845 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1848 if (HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
1850 huart
->hdmatx
->XferAbortCallback
= NULL
;
1859 /* Disable the UART DMA Rx request if enabled */
1860 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1862 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1864 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1865 if (huart
->hdmarx
!= NULL
)
1867 /* UART Rx DMA Abort callback has already been initialised :
1868 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1871 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1873 huart
->hdmarx
->XferAbortCallback
= NULL
;
1883 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1884 if (abortcplt
== 1U)
1886 /* Reset Tx and Rx transfer counters */
1887 huart
->TxXferCount
= 0U;
1888 huart
->RxXferCount
= 0U;
1890 /* Clear ISR function pointers */
1891 huart
->RxISR
= NULL
;
1892 huart
->TxISR
= NULL
;
1894 /* Reset errorCode */
1895 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1897 /* Clear the Error flags in the ICR register */
1898 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
1901 /* Discard the received data */
1902 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1904 /* Restore huart->gState and huart->RxState to Ready */
1905 huart
->gState
= HAL_UART_STATE_READY
;
1906 huart
->RxState
= HAL_UART_STATE_READY
;
1908 /* As no DMA to be aborted, call directly user Abort complete callback */
1909 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
1910 /* Call registered Abort complete callback */
1911 huart
->AbortCpltCallback(huart
);
1913 /* Call legacy weak Abort complete callback */
1914 HAL_UART_AbortCpltCallback(huart
);
1915 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1922 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1923 * @param huart UART handle.
1924 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1925 * This procedure performs following operations :
1926 * - Disable UART Interrupts (Tx)
1927 * - Disable the DMA transfer in the peripheral register (if enabled)
1928 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1929 * - Set handle State to READY
1930 * - At abort completion, call user abort complete callback
1931 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1932 * considered as completed only when user abort complete callback is executed (not when exiting function).
1933 * @retval HAL status
1935 HAL_StatusTypeDef
HAL_UART_AbortTransmit_IT(UART_HandleTypeDef
*huart
)
1937 /* Disable interrupts */
1938 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1940 /* Disable the UART DMA Tx request if enabled */
1941 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
))
1943 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1945 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1946 if (huart
->hdmatx
!= NULL
)
1948 /* Set the UART DMA Abort callback :
1949 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1950 huart
->hdmatx
->XferAbortCallback
= UART_DMATxOnlyAbortCallback
;
1953 if (HAL_DMA_Abort_IT(huart
->hdmatx
) != HAL_OK
)
1955 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
1956 huart
->hdmatx
->XferAbortCallback(huart
->hdmatx
);
1961 /* Reset Tx transfer counter */
1962 huart
->TxXferCount
= 0U;
1964 /* Clear TxISR function pointers */
1965 huart
->TxISR
= NULL
;
1967 /* Restore huart->gState to Ready */
1968 huart
->gState
= HAL_UART_STATE_READY
;
1970 /* As no DMA to be aborted, call directly user Abort complete callback */
1971 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
1972 /* Call registered Abort Transmit Complete Callback */
1973 huart
->AbortTransmitCpltCallback(huart
);
1975 /* Call legacy weak Abort Transmit Complete Callback */
1976 HAL_UART_AbortTransmitCpltCallback(huart
);
1977 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1982 /* Reset Tx transfer counter */
1983 huart
->TxXferCount
= 0U;
1985 /* Clear TxISR function pointers */
1986 huart
->TxISR
= NULL
;
1989 /* Restore huart->gState to Ready */
1990 huart
->gState
= HAL_UART_STATE_READY
;
1992 /* As no DMA to be aborted, call directly user Abort complete callback */
1993 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
1994 /* Call registered Abort Transmit Complete Callback */
1995 huart
->AbortTransmitCpltCallback(huart
);
1997 /* Call legacy weak Abort Transmit Complete Callback */
1998 HAL_UART_AbortTransmitCpltCallback(huart
);
1999 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2006 * @brief Abort ongoing Receive transfer (Interrupt mode).
2007 * @param huart UART handle.
2008 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2009 * This procedure performs following operations :
2010 * - Disable UART Interrupts (Rx)
2011 * - Disable the DMA transfer in the peripheral register (if enabled)
2012 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2013 * - Set handle State to READY
2014 * - At abort completion, call user abort complete callback
2015 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2016 * considered as completed only when user abort complete callback is executed (not when exiting function).
2017 * @retval HAL status
2019 HAL_StatusTypeDef
HAL_UART_AbortReceive_IT(UART_HandleTypeDef
*huart
)
2021 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2022 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2023 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2025 /* Disable the UART DMA Rx request if enabled */
2026 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
2028 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
2030 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2031 if (huart
->hdmarx
!= NULL
)
2033 /* Set the UART DMA Abort callback :
2034 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2035 huart
->hdmarx
->XferAbortCallback
= UART_DMARxOnlyAbortCallback
;
2038 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
2040 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2041 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
2046 /* Reset Rx transfer counter */
2047 huart
->RxXferCount
= 0U;
2049 /* Clear RxISR function pointer */
2050 huart
->pRxBuffPtr
= NULL
;
2052 /* Clear the Error flags in the ICR register */
2053 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2055 /* Discard the received data */
2056 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
2058 /* Restore huart->RxState to Ready */
2059 huart
->RxState
= HAL_UART_STATE_READY
;
2061 /* As no DMA to be aborted, call directly user Abort complete callback */
2062 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2063 /* Call registered Abort Receive Complete Callback */
2064 huart
->AbortReceiveCpltCallback(huart
);
2066 /* Call legacy weak Abort Receive Complete Callback */
2067 HAL_UART_AbortReceiveCpltCallback(huart
);
2068 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2073 /* Reset Rx transfer counter */
2074 huart
->RxXferCount
= 0U;
2076 /* Clear RxISR function pointer */
2077 huart
->pRxBuffPtr
= NULL
;
2079 /* Clear the Error flags in the ICR register */
2080 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
2082 /* Restore huart->RxState to Ready */
2083 huart
->RxState
= HAL_UART_STATE_READY
;
2085 /* As no DMA to be aborted, call directly user Abort complete callback */
2086 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2087 /* Call registered Abort Receive Complete Callback */
2088 huart
->AbortReceiveCpltCallback(huart
);
2090 /* Call legacy weak Abort Receive Complete Callback */
2091 HAL_UART_AbortReceiveCpltCallback(huart
);
2092 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2099 * @brief Handle UART interrupt request.
2100 * @param huart UART handle.
2103 void HAL_UART_IRQHandler(UART_HandleTypeDef
*huart
)
2105 uint32_t isrflags
= READ_REG(huart
->Instance
->ISR
);
2106 uint32_t cr1its
= READ_REG(huart
->Instance
->CR1
);
2107 uint32_t cr3its
= READ_REG(huart
->Instance
->CR3
);
2109 uint32_t errorflags
;
2112 /* If no error occurs */
2113 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
2114 if (errorflags
== 0U)
2116 /* UART in mode Receiver ---------------------------------------------------*/
2117 if (((isrflags
& USART_ISR_RXNE
) != 0U)
2118 && ((cr1its
& USART_CR1_RXNEIE
) != 0U))
2120 if (huart
->RxISR
!= NULL
)
2122 huart
->RxISR(huart
);
2128 /* If some errors occur */
2129 if ((errorflags
!= 0U)
2130 && (((cr3its
& USART_CR3_EIE
) != 0U)
2131 || ((cr1its
& (USART_CR1_RXNEIE
| USART_CR1_PEIE
)) != 0U)))
2133 /* UART parity error interrupt occurred -------------------------------------*/
2134 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
2136 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_PEF
);
2138 huart
->ErrorCode
|= HAL_UART_ERROR_PE
;
2141 /* UART frame error interrupt occurred --------------------------------------*/
2142 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2144 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_FEF
);
2146 huart
->ErrorCode
|= HAL_UART_ERROR_FE
;
2149 /* UART noise error interrupt occurred --------------------------------------*/
2150 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2152 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_NEF
);
2154 huart
->ErrorCode
|= HAL_UART_ERROR_NE
;
2157 /* UART Over-Run interrupt occurred -----------------------------------------*/
2158 if (((isrflags
& USART_ISR_ORE
) != 0U)
2159 && (((cr1its
& USART_CR1_RXNEIE
) != 0U) ||
2160 ((cr3its
& USART_CR3_EIE
) != 0U)))
2162 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
);
2164 huart
->ErrorCode
|= HAL_UART_ERROR_ORE
;
2167 /* Call UART Error Call back function if need be --------------------------*/
2168 if (huart
->ErrorCode
!= HAL_UART_ERROR_NONE
)
2170 /* UART in mode Receiver ---------------------------------------------------*/
2171 if (((isrflags
& USART_ISR_RXNE
) != 0U)
2172 && ((cr1its
& USART_CR1_RXNEIE
) != 0U))
2174 if (huart
->RxISR
!= NULL
)
2176 huart
->RxISR(huart
);
2180 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2181 consider error as blocking */
2182 errorcode
= huart
->ErrorCode
;
2183 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) ||
2184 ((errorcode
& HAL_UART_ERROR_ORE
) != 0U))
2186 /* Blocking error : transfer is aborted
2187 Set the UART state ready to be able to start again the process,
2188 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2189 UART_EndRxTransfer(huart
);
2191 /* Disable the UART DMA Rx request if enabled */
2192 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
2194 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
2196 /* Abort the UART DMA Rx channel */
2197 if (huart
->hdmarx
!= NULL
)
2199 /* Set the UART DMA Abort callback :
2200 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2201 huart
->hdmarx
->XferAbortCallback
= UART_DMAAbortOnError
;
2204 if (HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
2206 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2207 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
2212 /* Call user error callback */
2213 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2214 /*Call registered error callback*/
2215 huart
->ErrorCallback(huart
);
2217 /*Call legacy weak error callback*/
2218 HAL_UART_ErrorCallback(huart
);
2219 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2225 /* Call user error callback */
2226 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2227 /*Call registered error callback*/
2228 huart
->ErrorCallback(huart
);
2230 /*Call legacy weak error callback*/
2231 HAL_UART_ErrorCallback(huart
);
2232 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2237 /* Non Blocking error : transfer could go on.
2238 Error is notified to user through user error callback */
2239 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2240 /*Call registered error callback*/
2241 huart
->ErrorCallback(huart
);
2243 /*Call legacy weak error callback*/
2244 HAL_UART_ErrorCallback(huart
);
2245 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2246 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2251 } /* End if some error occurs */
2253 /* UART in mode Transmitter ------------------------------------------------*/
2254 if (((isrflags
& USART_ISR_TXE
) != 0U)
2255 && ((cr1its
& USART_CR1_TXEIE
) != 0U))
2257 if (huart
->TxISR
!= NULL
)
2259 huart
->TxISR(huart
);
2264 /* UART in mode Transmitter (transmission end) -----------------------------*/
2265 if (((isrflags
& USART_ISR_TC
) != 0U) && ((cr1its
& USART_CR1_TCIE
) != 0U))
2267 UART_EndTransmit_IT(huart
);
2274 * @brief Tx Transfer completed callback.
2275 * @param huart UART handle.
2278 __weak
void HAL_UART_TxCpltCallback(UART_HandleTypeDef
*huart
)
2280 /* Prevent unused argument(s) compilation warning */
2283 /* NOTE : This function should not be modified, when the callback is needed,
2284 the HAL_UART_TxCpltCallback can be implemented in the user file.
2289 * @brief Tx Half Transfer completed callback.
2290 * @param huart UART handle.
2293 __weak
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef
*huart
)
2295 /* Prevent unused argument(s) compilation warning */
2298 /* NOTE: This function should not be modified, when the callback is needed,
2299 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2304 * @brief Rx Transfer completed callback.
2305 * @param huart UART handle.
2308 __weak
void HAL_UART_RxCpltCallback(UART_HandleTypeDef
*huart
)
2310 /* Prevent unused argument(s) compilation warning */
2313 /* NOTE : This function should not be modified, when the callback is needed,
2314 the HAL_UART_RxCpltCallback can be implemented in the user file.
2319 * @brief Rx Half Transfer completed callback.
2320 * @param huart UART handle.
2323 __weak
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef
*huart
)
2325 /* Prevent unused argument(s) compilation warning */
2328 /* NOTE: This function should not be modified, when the callback is needed,
2329 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2334 * @brief UART error callback.
2335 * @param huart UART handle.
2338 __weak
void HAL_UART_ErrorCallback(UART_HandleTypeDef
*huart
)
2340 /* Prevent unused argument(s) compilation warning */
2343 /* NOTE : This function should not be modified, when the callback is needed,
2344 the HAL_UART_ErrorCallback can be implemented in the user file.
2349 * @brief UART Abort Complete callback.
2350 * @param huart UART handle.
2353 __weak
void HAL_UART_AbortCpltCallback(UART_HandleTypeDef
*huart
)
2355 /* Prevent unused argument(s) compilation warning */
2358 /* NOTE : This function should not be modified, when the callback is needed,
2359 the HAL_UART_AbortCpltCallback can be implemented in the user file.
2364 * @brief UART Abort Complete callback.
2365 * @param huart UART handle.
2368 __weak
void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef
*huart
)
2370 /* Prevent unused argument(s) compilation warning */
2373 /* NOTE : This function should not be modified, when the callback is needed,
2374 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2379 * @brief UART Abort Receive Complete callback.
2380 * @param huart UART handle.
2383 __weak
void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef
*huart
)
2385 /* Prevent unused argument(s) compilation warning */
2388 /* NOTE : This function should not be modified, when the callback is needed,
2389 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2397 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2398 * @brief UART control functions
2401 ===============================================================================
2402 ##### Peripheral Control functions #####
2403 ===============================================================================
2405 This subsection provides a set of functions allowing to control the UART.
2406 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2407 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2408 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2409 (+) UART_SetConfig() API configures the UART peripheral
2410 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2411 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2412 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2413 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2414 (+) HAL_LIN_SendBreak() API transmits the break characters
2420 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
2421 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2422 * @param huart UART handle.
2423 * @retval HAL status
2425 HAL_StatusTypeDef
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef
*huart
)
2427 /* Process Locked */
2430 huart
->gState
= HAL_UART_STATE_BUSY
;
2432 /* Enable USART mute mode by setting the MME bit in the CR1 register */
2433 SET_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
2435 huart
->gState
= HAL_UART_STATE_READY
;
2437 return (UART_CheckIdleState(huart
));
2441 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
2442 * as it may not have been in mute mode at this very moment).
2443 * @param huart UART handle.
2444 * @retval HAL status
2446 HAL_StatusTypeDef
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef
*huart
)
2448 /* Process Locked */
2451 huart
->gState
= HAL_UART_STATE_BUSY
;
2453 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2454 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
2456 huart
->gState
= HAL_UART_STATE_READY
;
2458 return (UART_CheckIdleState(huart
));
2462 * @brief Enter UART mute mode (means UART actually enters mute mode).
2463 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2464 * @param huart UART handle.
2467 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef
*huart
)
2469 __HAL_UART_SEND_REQ(huart
, UART_MUTE_MODE_REQUEST
);
2473 * @brief Enable the UART transmitter and disable the UART receiver.
2474 * @param huart UART handle.
2475 * @retval HAL status
2477 HAL_StatusTypeDef
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef
*huart
)
2479 /* Process Locked */
2481 huart
->gState
= HAL_UART_STATE_BUSY
;
2483 /* Clear TE and RE bits */
2484 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2486 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2487 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TE
);
2489 huart
->gState
= HAL_UART_STATE_READY
;
2491 /* Process Unlocked */
2492 __HAL_UNLOCK(huart
);
2498 * @brief Enable the UART receiver and disable the UART transmitter.
2499 * @param huart UART handle.
2500 * @retval HAL status.
2502 HAL_StatusTypeDef
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
*huart
)
2504 /* Process Locked */
2506 huart
->gState
= HAL_UART_STATE_BUSY
;
2508 /* Clear TE and RE bits */
2509 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2511 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2512 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RE
);
2514 huart
->gState
= HAL_UART_STATE_READY
;
2516 /* Process Unlocked */
2517 __HAL_UNLOCK(huart
);
2524 * @brief Transmit break characters.
2525 * @param huart UART handle.
2526 * @retval HAL status
2528 HAL_StatusTypeDef
HAL_LIN_SendBreak(UART_HandleTypeDef
*huart
)
2530 /* Check the parameters */
2531 assert_param(IS_UART_LIN_INSTANCE(huart
->Instance
));
2533 /* Process Locked */
2536 huart
->gState
= HAL_UART_STATE_BUSY
;
2538 /* Send break characters */
2539 __HAL_UART_SEND_REQ(huart
, UART_SENDBREAK_REQUEST
);
2541 huart
->gState
= HAL_UART_STATE_READY
;
2543 /* Process Unlocked */
2544 __HAL_UNLOCK(huart
);
2553 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2554 * @brief UART Peripheral State functions
2557 ==============================================================================
2558 ##### Peripheral State and Error functions #####
2559 ==============================================================================
2561 This subsection provides functions allowing to :
2562 (+) Return the UART handle state.
2563 (+) Return the UART handle error code
2570 * @brief Return the UART handle state.
2571 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2572 * the configuration information for the specified UART.
2575 HAL_UART_StateTypeDef
HAL_UART_GetState(UART_HandleTypeDef
*huart
)
2577 uint32_t temp1
, temp2
;
2578 temp1
= huart
->gState
;
2579 temp2
= huart
->RxState
;
2581 return (HAL_UART_StateTypeDef
)(temp1
| temp2
);
2585 * @brief Return the UART handle error code.
2586 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2587 * the configuration information for the specified UART.
2588 * @retval UART Error Code
2590 uint32_t HAL_UART_GetError(UART_HandleTypeDef
*huart
)
2592 return huart
->ErrorCode
;
2602 /** @defgroup UART_Private_Functions UART Private Functions
2607 * @brief Initialize the callbacks to their default values.
2608 * @param huart UART handle.
2611 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2612 void UART_InitCallbacksToDefault(UART_HandleTypeDef
*huart
)
2614 /* Init the UART Callback settings */
2615 huart
->TxHalfCpltCallback
= HAL_UART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
2616 huart
->TxCpltCallback
= HAL_UART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2617 huart
->RxHalfCpltCallback
= HAL_UART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
2618 huart
->RxCpltCallback
= HAL_UART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2619 huart
->ErrorCallback
= HAL_UART_ErrorCallback
; /* Legacy weak ErrorCallback */
2620 huart
->AbortCpltCallback
= HAL_UART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2621 huart
->AbortTransmitCpltCallback
= HAL_UART_AbortTransmitCpltCallback
; /* Legacy weak AbortTransmitCpltCallback */
2622 huart
->AbortReceiveCpltCallback
= HAL_UART_AbortReceiveCpltCallback
; /* Legacy weak AbortReceiveCpltCallback */
2625 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2628 * @brief Configure the UART peripheral.
2629 * @param huart UART handle.
2630 * @retval HAL status
2632 HAL_StatusTypeDef
UART_SetConfig(UART_HandleTypeDef
*huart
)
2636 UART_ClockSourceTypeDef clocksource
;
2637 uint32_t usartdiv
= 0x00000000U
;
2638 HAL_StatusTypeDef ret
= HAL_OK
;
2640 /* Check the parameters */
2641 assert_param(IS_UART_BAUDRATE(huart
->Init
.BaudRate
));
2642 assert_param(IS_UART_WORD_LENGTH(huart
->Init
.WordLength
));
2643 assert_param(IS_UART_STOPBITS(huart
->Init
.StopBits
));
2644 assert_param(IS_UART_ONE_BIT_SAMPLE(huart
->Init
.OneBitSampling
));
2646 assert_param(IS_UART_PARITY(huart
->Init
.Parity
));
2647 assert_param(IS_UART_MODE(huart
->Init
.Mode
));
2648 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart
->Init
.HwFlowCtl
));
2649 assert_param(IS_UART_OVERSAMPLING(huart
->Init
.OverSampling
));
2651 /*-------------------------- USART CR1 Configuration -----------------------*/
2652 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
2653 * the UART Word Length, Parity, Mode and oversampling:
2654 * set the M bits according to huart->Init.WordLength value
2655 * set PCE and PS bits according to huart->Init.Parity value
2656 * set TE and RE bits according to huart->Init.Mode value
2657 * set OVER8 bit according to huart->Init.OverSampling value */
2658 tmpreg
= (uint32_t)huart
->Init
.WordLength
| huart
->Init
.Parity
| huart
->Init
.Mode
| huart
->Init
.OverSampling
;
2659 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_FIELDS
, tmpreg
);
2661 /*-------------------------- USART CR2 Configuration -----------------------*/
2662 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
2663 * to huart->Init.StopBits value */
2664 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_STOP
, huart
->Init
.StopBits
);
2666 /*-------------------------- USART CR3 Configuration -----------------------*/
2668 * - UART HardWare Flow Control: set CTSE and RTSE bits according
2669 * to huart->Init.HwFlowCtl value
2670 * - one-bit sampling method versus three samples' majority rule according
2671 * to huart->Init.OneBitSampling (not applicable to LPUART) */
2672 tmpreg
= (uint32_t)huart
->Init
.HwFlowCtl
;
2674 tmpreg
|= huart
->Init
.OneBitSampling
;
2675 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_FIELDS
, tmpreg
);
2678 /*-------------------------- USART BRR Configuration -----------------------*/
2679 UART_GETCLOCKSOURCE(huart
, clocksource
);
2681 if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
2683 switch (clocksource
)
2685 case UART_CLOCKSOURCE_PCLK1
:
2686 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
2688 case UART_CLOCKSOURCE_PCLK2
:
2689 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
2691 case UART_CLOCKSOURCE_HSI
:
2692 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE
, huart
->Init
.BaudRate
));
2694 case UART_CLOCKSOURCE_SYSCLK
:
2695 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
2697 case UART_CLOCKSOURCE_LSE
:
2698 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE
, huart
->Init
.BaudRate
));
2700 case UART_CLOCKSOURCE_UNDEFINED
:
2706 /* USARTDIV must be greater than or equal to 0d16 */
2707 if ((usartdiv
>= UART_BRR_MIN
) && (usartdiv
<= UART_BRR_MAX
))
2709 brrtemp
= (uint16_t)(usartdiv
& 0xFFF0U
);
2710 brrtemp
|= (uint16_t)((usartdiv
& (uint16_t)0x000FU
) >> 1U);
2711 huart
->Instance
->BRR
= brrtemp
;
2720 switch (clocksource
)
2722 case UART_CLOCKSOURCE_PCLK1
:
2723 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
2725 case UART_CLOCKSOURCE_PCLK2
:
2726 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
2728 case UART_CLOCKSOURCE_HSI
:
2729 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE
, huart
->Init
.BaudRate
));
2731 case UART_CLOCKSOURCE_SYSCLK
:
2732 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
2734 case UART_CLOCKSOURCE_LSE
:
2735 usartdiv
= (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE
, huart
->Init
.BaudRate
));
2737 case UART_CLOCKSOURCE_UNDEFINED
:
2743 /* USARTDIV must be greater than or equal to 0d16 */
2744 if ((usartdiv
>= UART_BRR_MIN
) && (usartdiv
<= UART_BRR_MAX
))
2746 huart
->Instance
->BRR
= usartdiv
;
2755 /* Clear ISR function pointers */
2756 huart
->RxISR
= NULL
;
2757 huart
->TxISR
= NULL
;
2763 * @brief Configure the UART peripheral advanced features.
2764 * @param huart UART handle.
2767 void UART_AdvFeatureConfig(UART_HandleTypeDef
*huart
)
2769 /* Check whether the set of advanced features to configure is properly set */
2770 assert_param(IS_UART_ADVFEATURE_INIT(huart
->AdvancedInit
.AdvFeatureInit
));
2772 /* if required, configure TX pin active level inversion */
2773 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_TXINVERT_INIT
))
2775 assert_param(IS_UART_ADVFEATURE_TXINV(huart
->AdvancedInit
.TxPinLevelInvert
));
2776 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_TXINV
, huart
->AdvancedInit
.TxPinLevelInvert
);
2779 /* if required, configure RX pin active level inversion */
2780 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXINVERT_INIT
))
2782 assert_param(IS_UART_ADVFEATURE_RXINV(huart
->AdvancedInit
.RxPinLevelInvert
));
2783 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_RXINV
, huart
->AdvancedInit
.RxPinLevelInvert
);
2786 /* if required, configure data inversion */
2787 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DATAINVERT_INIT
))
2789 assert_param(IS_UART_ADVFEATURE_DATAINV(huart
->AdvancedInit
.DataInvert
));
2790 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_DATAINV
, huart
->AdvancedInit
.DataInvert
);
2793 /* if required, configure RX/TX pins swap */
2794 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_SWAP_INIT
))
2796 assert_param(IS_UART_ADVFEATURE_SWAP(huart
->AdvancedInit
.Swap
));
2797 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_SWAP
, huart
->AdvancedInit
.Swap
);
2800 /* if required, configure RX overrun detection disabling */
2801 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
2803 assert_param(IS_UART_OVERRUN(huart
->AdvancedInit
.OverrunDisable
));
2804 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_OVRDIS
, huart
->AdvancedInit
.OverrunDisable
);
2807 /* if required, configure DMA disabling on reception error */
2808 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DMADISABLEONERROR_INIT
))
2810 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart
->AdvancedInit
.DMADisableonRxError
));
2811 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DDRE
, huart
->AdvancedInit
.DMADisableonRxError
);
2814 /* if required, configure auto Baud rate detection scheme */
2815 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_AUTOBAUDRATE_INIT
))
2817 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart
->Instance
));
2818 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart
->AdvancedInit
.AutoBaudRateEnable
));
2819 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABREN
, huart
->AdvancedInit
.AutoBaudRateEnable
);
2820 /* set auto Baudrate detection parameters if detection is enabled */
2821 if (huart
->AdvancedInit
.AutoBaudRateEnable
== UART_ADVFEATURE_AUTOBAUDRATE_ENABLE
)
2823 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart
->AdvancedInit
.AutoBaudRateMode
));
2824 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABRMODE
, huart
->AdvancedInit
.AutoBaudRateMode
);
2828 /* if required, configure MSB first on communication line */
2829 if (HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_MSBFIRST_INIT
))
2831 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart
->AdvancedInit
.MSBFirst
));
2832 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_MSBFIRST
, huart
->AdvancedInit
.MSBFirst
);
2837 * @brief Check the UART Idle State.
2838 * @param huart UART handle.
2839 * @retval HAL status
2841 HAL_StatusTypeDef
UART_CheckIdleState(UART_HandleTypeDef
*huart
)
2845 /* Initialize the UART ErrorCode */
2846 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2848 /* Init tickstart for timeout managment*/
2849 tickstart
= HAL_GetTick();
2851 /* Check if the Transmitter is enabled */
2852 if ((huart
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2854 /* Wait until TEACK flag is set */
2855 if (UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_TEACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
2857 /* Timeout occurred */
2862 /* Initialize the UART State */
2863 huart
->gState
= HAL_UART_STATE_READY
;
2864 huart
->RxState
= HAL_UART_STATE_READY
;
2866 /* Process Unlocked */
2867 __HAL_UNLOCK(huart
);
2873 * @brief Handle UART Communication Timeout.
2874 * @param huart UART handle.
2875 * @param Flag Specifies the UART flag to check
2876 * @param Status Flag status (SET or RESET)
2877 * @param Tickstart Tick start value
2878 * @param Timeout Timeout duration
2879 * @retval HAL status
2881 HAL_StatusTypeDef
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef
*huart
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
2883 /* Wait until flag is set */
2884 while ((__HAL_UART_GET_FLAG(huart
, Flag
) ? SET
: RESET
) == Status
)
2886 /* Check for the Timeout */
2887 if (Timeout
!= HAL_MAX_DELAY
)
2889 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2891 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2892 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
2893 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2895 huart
->gState
= HAL_UART_STATE_READY
;
2896 huart
->RxState
= HAL_UART_STATE_READY
;
2898 /* Process Unlocked */
2899 __HAL_UNLOCK(huart
);
2910 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
2911 * @param huart UART handle.
2914 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
)
2916 /* Disable TXEIE and TCIE interrupts */
2917 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
2919 /* At end of Tx process, restore huart->gState to Ready */
2920 huart
->gState
= HAL_UART_STATE_READY
;
2925 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2926 * @param huart UART handle.
2929 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
)
2931 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2932 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
2933 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
2935 /* At end of Rx process, restore huart->RxState to Ready */
2936 huart
->RxState
= HAL_UART_STATE_READY
;
2938 /* Reset RxIsr function pointer */
2939 huart
->RxISR
= NULL
;
2944 * @brief DMA UART transmit process complete callback.
2945 * @param hdma DMA handle.
2948 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2950 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2952 /* DMA Normal mode */
2953 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
2955 huart
->TxXferCount
= 0U;
2957 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2958 in the UART CR3 register */
2959 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
2961 /* Enable the UART Transmit Complete Interrupt */
2962 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
2964 /* DMA Circular mode */
2967 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2968 /*Call registered Tx complete callback*/
2969 huart
->TxCpltCallback(huart
);
2971 /*Call legacy weak Tx complete callback*/
2972 HAL_UART_TxCpltCallback(huart
);
2973 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2978 * @brief DMA UART transmit process half complete callback.
2979 * @param hdma DMA handle.
2982 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
2984 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
2986 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2987 /*Call registered Tx Half complete callback*/
2988 huart
->TxHalfCpltCallback(huart
);
2990 /*Call legacy weak Tx Half complete callback*/
2991 HAL_UART_TxHalfCpltCallback(huart
);
2992 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2996 * @brief DMA UART receive process complete callback.
2997 * @param hdma DMA handle.
3000 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
3002 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3004 /* DMA Normal mode */
3005 if (hdma
->Init
.Mode
!= DMA_CIRCULAR
)
3007 huart
->RxXferCount
= 0U;
3009 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3010 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
3011 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3013 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3014 in the UART CR3 register */
3015 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
3017 /* At end of Rx process, restore huart->RxState to Ready */
3018 huart
->RxState
= HAL_UART_STATE_READY
;
3021 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3022 /*Call registered Rx complete callback*/
3023 huart
->RxCpltCallback(huart
);
3025 /*Call legacy weak Rx complete callback*/
3026 HAL_UART_RxCpltCallback(huart
);
3027 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3031 * @brief DMA UART receive process half complete callback.
3032 * @param hdma DMA handle.
3035 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
3037 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3039 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3040 /*Call registered Rx Half complete callback*/
3041 huart
->RxHalfCpltCallback(huart
);
3043 /*Call legacy weak Rx Half complete callback*/
3044 HAL_UART_RxHalfCpltCallback(huart
);
3045 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3049 * @brief DMA UART communication error callback.
3050 * @param hdma DMA handle.
3053 static void UART_DMAError(DMA_HandleTypeDef
*hdma
)
3055 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3057 const HAL_UART_StateTypeDef gstate
= huart
->gState
;
3058 const HAL_UART_StateTypeDef rxstate
= huart
->RxState
;
3060 /* Stop UART DMA Tx request if ongoing */
3061 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
3062 (gstate
== HAL_UART_STATE_BUSY_TX
))
3064 huart
->TxXferCount
= 0U;
3065 UART_EndTxTransfer(huart
);
3068 /* Stop UART DMA Rx request if ongoing */
3069 if ((HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) &&
3070 (rxstate
== HAL_UART_STATE_BUSY_RX
))
3072 huart
->RxXferCount
= 0U;
3073 UART_EndRxTransfer(huart
);
3076 huart
->ErrorCode
|= HAL_UART_ERROR_DMA
;
3078 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3079 /*Call registered error callback*/
3080 huart
->ErrorCallback(huart
);
3082 /*Call legacy weak error callback*/
3083 HAL_UART_ErrorCallback(huart
);
3084 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3088 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
3089 * (To be called at end of DMA Abort procedure following error occurrence).
3090 * @param hdma DMA handle.
3093 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
3095 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3096 huart
->RxXferCount
= 0U;
3097 huart
->TxXferCount
= 0U;
3099 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3100 /*Call registered error callback*/
3101 huart
->ErrorCallback(huart
);
3103 /*Call legacy weak error callback*/
3104 HAL_UART_ErrorCallback(huart
);
3105 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3109 * @brief DMA UART Tx communication abort callback, when initiated by user
3110 * (To be called at end of DMA Tx Abort procedure following user abort request).
3111 * @note When this callback is executed, User Abort complete call back is called only if no
3112 * Abort still ongoing for Rx DMA Handle.
3113 * @param hdma DMA handle.
3116 static void UART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
3118 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3120 huart
->hdmatx
->XferAbortCallback
= NULL
;
3122 /* Check if an Abort process is still ongoing */
3123 if (huart
->hdmarx
!= NULL
)
3125 if (huart
->hdmarx
->XferAbortCallback
!= NULL
)
3131 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3132 huart
->TxXferCount
= 0U;
3133 huart
->RxXferCount
= 0U;
3135 /* Reset errorCode */
3136 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
3138 /* Clear the Error flags in the ICR register */
3139 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3142 /* Restore huart->gState and huart->RxState to Ready */
3143 huart
->gState
= HAL_UART_STATE_READY
;
3144 huart
->RxState
= HAL_UART_STATE_READY
;
3146 /* Call user Abort complete callback */
3147 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3148 /* Call registered Abort complete callback */
3149 huart
->AbortCpltCallback(huart
);
3151 /* Call legacy weak Abort complete callback */
3152 HAL_UART_AbortCpltCallback(huart
);
3153 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3158 * @brief DMA UART Rx communication abort callback, when initiated by user
3159 * (To be called at end of DMA Rx Abort procedure following user abort request).
3160 * @note When this callback is executed, User Abort complete call back is called only if no
3161 * Abort still ongoing for Tx DMA Handle.
3162 * @param hdma DMA handle.
3165 static void UART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
3167 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3169 huart
->hdmarx
->XferAbortCallback
= NULL
;
3171 /* Check if an Abort process is still ongoing */
3172 if (huart
->hdmatx
!= NULL
)
3174 if (huart
->hdmatx
->XferAbortCallback
!= NULL
)
3180 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3181 huart
->TxXferCount
= 0U;
3182 huart
->RxXferCount
= 0U;
3184 /* Reset errorCode */
3185 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
3187 /* Clear the Error flags in the ICR register */
3188 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3190 /* Discard the received data */
3191 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3193 /* Restore huart->gState and huart->RxState to Ready */
3194 huart
->gState
= HAL_UART_STATE_READY
;
3195 huart
->RxState
= HAL_UART_STATE_READY
;
3197 /* Call user Abort complete callback */
3198 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3199 /* Call registered Abort complete callback */
3200 huart
->AbortCpltCallback(huart
);
3202 /* Call legacy weak Abort complete callback */
3203 HAL_UART_AbortCpltCallback(huart
);
3204 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3209 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
3210 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3211 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3212 * and leads to user Tx Abort Complete callback execution).
3213 * @param hdma DMA handle.
3216 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
3218 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
3220 huart
->TxXferCount
= 0U;
3223 /* Restore huart->gState to Ready */
3224 huart
->gState
= HAL_UART_STATE_READY
;
3226 /* Call user Abort complete callback */
3227 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3228 /* Call registered Abort Transmit Complete Callback */
3229 huart
->AbortTransmitCpltCallback(huart
);
3231 /* Call legacy weak Abort Transmit Complete Callback */
3232 HAL_UART_AbortTransmitCpltCallback(huart
);
3233 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3237 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
3238 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
3239 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3240 * and leads to user Rx Abort Complete callback execution).
3241 * @param hdma DMA handle.
3244 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef
*hdma
)
3246 UART_HandleTypeDef
*huart
= (UART_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
3248 huart
->RxXferCount
= 0U;
3250 /* Clear the Error flags in the ICR register */
3251 __HAL_UART_CLEAR_FLAG(huart
, UART_CLEAR_OREF
| UART_CLEAR_NEF
| UART_CLEAR_PEF
| UART_CLEAR_FEF
);
3253 /* Discard the received data */
3254 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3256 /* Restore huart->RxState to Ready */
3257 huart
->RxState
= HAL_UART_STATE_READY
;
3259 /* Call user Abort complete callback */
3260 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3261 /* Call registered Abort Receive Complete Callback */
3262 huart
->AbortReceiveCpltCallback(huart
);
3264 /* Call legacy weak Abort Receive Complete Callback */
3265 HAL_UART_AbortReceiveCpltCallback(huart
);
3266 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3270 * @brief TX interrrupt handler for 7 or 8 bits data word length .
3271 * @note Function is called under interruption only, once
3272 * interruptions have been enabled by HAL_UART_Transmit_IT().
3273 * @param huart UART handle.
3276 static void UART_TxISR_8BIT(UART_HandleTypeDef
*huart
)
3278 /* Check that a Tx process is ongoing */
3279 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3281 if (huart
->TxXferCount
== 0U)
3283 /* Disable the UART Transmit Data Register Empty Interrupt */
3284 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
3286 /* Enable the UART Transmit Complete Interrupt */
3287 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3291 huart
->Instance
->TDR
= (uint8_t)(*huart
->pTxBuffPtr
& (uint8_t)0xFF);
3292 huart
->pTxBuffPtr
++;
3293 huart
->TxXferCount
--;
3299 * @brief TX interrrupt handler for 9 bits data word length.
3300 * @note Function is called under interruption only, once
3301 * interruptions have been enabled by HAL_UART_Transmit_IT().
3302 * @param huart UART handle.
3305 static void UART_TxISR_16BIT(UART_HandleTypeDef
*huart
)
3309 /* Check that a Tx process is ongoing */
3310 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
3312 if (huart
->TxXferCount
== 0U)
3314 /* Disable the UART Transmit Data Register Empty Interrupt */
3315 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
3317 /* Enable the UART Transmit Complete Interrupt */
3318 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3322 tmp
= (uint16_t *) huart
->pTxBuffPtr
;
3323 huart
->Instance
->TDR
= (((uint32_t)(*tmp
)) & 0x01FFUL
);
3324 huart
->pTxBuffPtr
+= 2U;
3325 huart
->TxXferCount
--;
3332 * @brief Wrap up transmission in non-blocking mode.
3333 * @param huart pointer to a UART_HandleTypeDef structure that contains
3334 * the configuration information for the specified UART module.
3337 static void UART_EndTransmit_IT(UART_HandleTypeDef
*huart
)
3339 /* Disable the UART Transmit Complete Interrupt */
3340 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
3342 /* Tx process is ended, restore huart->gState to Ready */
3343 huart
->gState
= HAL_UART_STATE_READY
;
3345 /* Cleat TxISR function pointer */
3346 huart
->TxISR
= NULL
;
3348 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3349 /*Call registered Tx complete callback*/
3350 huart
->TxCpltCallback(huart
);
3352 /*Call legacy weak Tx complete callback*/
3353 HAL_UART_TxCpltCallback(huart
);
3354 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3358 * @brief RX interrrupt handler for 7 or 8 bits data word length .
3359 * @param huart UART handle.
3362 static void UART_RxISR_8BIT(UART_HandleTypeDef
*huart
)
3364 uint16_t uhMask
= huart
->Mask
;
3367 /* Check that a Rx process is ongoing */
3368 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
3370 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
3371 *huart
->pRxBuffPtr
= (uint8_t)(uhdata
& (uint8_t)uhMask
);
3372 huart
->pRxBuffPtr
++;
3373 huart
->RxXferCount
--;
3375 if (huart
->RxXferCount
== 0U)
3377 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
3378 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
3380 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3381 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3383 /* Rx process is completed, restore huart->RxState to Ready */
3384 huart
->RxState
= HAL_UART_STATE_READY
;
3386 /* Clear RxISR function pointer */
3387 huart
->RxISR
= NULL
;
3389 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3390 /*Call registered Rx complete callback*/
3391 huart
->RxCpltCallback(huart
);
3393 /*Call legacy weak Rx complete callback*/
3394 HAL_UART_RxCpltCallback(huart
);
3395 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3400 /* Clear RXNE interrupt flag */
3401 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3406 * @brief RX interrrupt handler for 9 bits data word length .
3407 * @note Function is called under interruption only, once
3408 * interruptions have been enabled by HAL_UART_Receive_IT()
3409 * @param huart UART handle.
3412 static void UART_RxISR_16BIT(UART_HandleTypeDef
*huart
)
3415 uint16_t uhMask
= huart
->Mask
;
3418 /* Check that a Rx process is ongoing */
3419 if (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
3421 uhdata
= (uint16_t) READ_REG(huart
->Instance
->RDR
);
3422 tmp
= (uint16_t *) huart
->pRxBuffPtr
;
3423 *tmp
= (uint16_t)(uhdata
& uhMask
);
3424 huart
->pRxBuffPtr
+= 2U;
3425 huart
->RxXferCount
--;
3427 if (huart
->RxXferCount
== 0U)
3429 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
3430 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
3432 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3433 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
3435 /* Rx process is completed, restore huart->RxState to Ready */
3436 huart
->RxState
= HAL_UART_STATE_READY
;
3438 /* Clear RxISR function pointer */
3439 huart
->RxISR
= NULL
;
3441 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3442 /*Call registered Rx complete callback*/
3443 huart
->RxCpltCallback(huart
);
3445 /*Call legacy weak Rx complete callback*/
3446 HAL_UART_RxCpltCallback(huart
);
3447 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3452 /* Clear RXNE interrupt flag */
3453 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
3462 #endif /* HAL_UART_MODULE_ENABLED */
3471 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/