Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / STM32H7 / Drivers / STM32H7xx_HAL_Driver / Src / stm32h7xx_hal_usart.c
blob0458da47f73a1b3f3b271e0fc17503d4dfad8a95
1 /**
2 ******************************************************************************
3 * @file stm32h7xx_hal_usart.c
4 * @author MCD Application Team
5 * @brief USART HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
8 * Peripheral (USART).
9 * + Initialization and de-initialization functions
10 * + IO operation functions
11 * + Peripheral Control functions
12 * + Peripheral State and Error functions
14 @verbatim
15 ===============================================================================
16 ##### How to use this driver #####
17 ===============================================================================
18 [..]
19 The USART HAL driver can be used as follows:
21 (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
22 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
23 (++) Enable the USARTx interface clock.
24 (++) USART pins configuration:
25 (+++) Enable the clock for the USART GPIOs.
26 (+++) Configure these USART pins as alternate function pull-up.
27 (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
28 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
29 (+++) Configure the USARTx interrupt priority.
30 (+++) Enable the NVIC USART IRQ handle.
31 (++) USART interrupts handling:
32 -@@- The specific USART interrupts (Transmission complete interrupt,
33 RXNE interrupt and Error Interrupts) will be managed using the macros
34 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
35 (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
36 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_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 USART 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, and Mode
45 (Receiver/Transmitter) in the husart handle Init structure.
47 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
48 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
49 by calling the customized HAL_USART_MspInit(&husart) API.
51 [..]
52 (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
53 HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
54 HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
56 ##### Callback registration #####
57 ==================================
59 [..]
60 The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
61 allows the user to configure dynamically the driver callbacks.
63 [..]
64 Use Function @ref HAL_USART_RegisterCallback() to register a user callback.
65 Function @ref HAL_USART_RegisterCallback() allows to register following callbacks:
66 (+) TxHalfCpltCallback : Tx Half Complete Callback.
67 (+) TxCpltCallback : Tx Complete Callback.
68 (+) RxHalfCpltCallback : Rx Half Complete Callback.
69 (+) RxCpltCallback : Rx Complete Callback.
70 (+) TxRxCpltCallback : Tx Rx Complete Callback.
71 (+) ErrorCallback : Error Callback.
72 (+) AbortCpltCallback : Abort Complete Callback.
73 (+) RxFifoFullCallback : Rx Fifo Full Callback.
74 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
75 (+) MspInitCallback : USART MspInit.
76 (+) MspDeInitCallback : USART MspDeInit.
77 This function takes as parameters the HAL peripheral handle, the Callback ID
78 and a pointer to the user callback function.
80 [..]
81 Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default
82 weak (surcharged) function.
83 @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
84 and the Callback ID.
85 This function allows to reset following callbacks:
86 (+) TxHalfCpltCallback : Tx Half Complete Callback.
87 (+) TxCpltCallback : Tx Complete Callback.
88 (+) RxHalfCpltCallback : Rx Half Complete Callback.
89 (+) RxCpltCallback : Rx Complete Callback.
90 (+) TxRxCpltCallback : Tx Rx Complete Callback.
91 (+) ErrorCallback : Error Callback.
92 (+) AbortCpltCallback : Abort Complete Callback.
93 (+) RxFifoFullCallback : Rx Fifo Full Callback.
94 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
95 (+) MspInitCallback : USART MspInit.
96 (+) MspDeInitCallback : USART MspDeInit.
98 [..]
99 By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
100 all callbacks are set to the corresponding weak (surcharged) functions:
101 examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback().
102 Exception done for MspInit and MspDeInit functions that are respectively
103 reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init()
104 and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
105 If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit()
106 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
108 [..]
109 Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
110 Exception done MspInit/MspDeInit that can be registered/unregistered
111 in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
112 MspInit/DeInit callbacks can be used during the Init/DeInit.
113 In that case first register the MspInit/MspDeInit user callbacks
114 using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit()
115 or @ref HAL_USART_Init() function.
117 [..]
118 When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
119 not defined, the callback registration feature is not available
120 and weak (surcharged) callbacks are used.
123 @endverbatim
124 ******************************************************************************
125 * @attention
127 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
128 * All rights reserved.</center></h2>
130 * This software component is licensed by ST under BSD 3-Clause license,
131 * the "License"; You may not use this file except in compliance with the
132 * License. You may obtain a copy of the License at:
133 * opensource.org/licenses/BSD-3-Clause
135 ******************************************************************************
138 /* Includes ------------------------------------------------------------------*/
139 #include "stm32h7xx_hal.h"
141 /** @addtogroup STM32H7xx_HAL_Driver
142 * @{
145 /** @defgroup USART USART
146 * @brief HAL USART Synchronous module driver
147 * @{
150 #ifdef HAL_USART_MODULE_ENABLED
152 /* Private typedef -----------------------------------------------------------*/
153 /* Private define ------------------------------------------------------------*/
154 /** @defgroup USART_Private_Constants USART Private Constants
155 * @{
157 #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */
158 #define USART_TEACK_REACK_TIMEOUT 1000U /*!< USART TX or RX enable acknowledge time-out value */
159 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
160 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 | \
161 USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by USART_SetConfig API */
163 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \
164 USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \
165 USART_CR2_DIS_NSS)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
167 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */
169 #define USART_BRR_MIN 0x10U /* USART BRR minimum authorized value */
170 #define USART_BRR_MAX 0xFFFFU /* USART BRR maximum authorized value */
172 * @}
175 /* Private macros ------------------------------------------------------------*/
176 /* Private variables ---------------------------------------------------------*/
177 /* Private function prototypes -----------------------------------------------*/
178 /** @addtogroup USART_Private_Functions
179 * @{
181 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
182 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
183 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
184 static void USART_EndTransfer(USART_HandleTypeDef *husart);
185 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
186 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
187 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
188 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
189 static void USART_DMAError(DMA_HandleTypeDef *hdma);
190 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
191 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
192 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
193 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
194 uint32_t Tickstart, uint32_t Timeout);
195 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
196 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
197 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart);
198 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart);
199 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
200 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
201 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart);
202 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart);
203 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart);
204 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
205 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
209 * @}
212 /* Exported functions --------------------------------------------------------*/
214 /** @defgroup USART_Exported_Functions USART Exported Functions
215 * @{
218 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
219 * @brief Initialization and Configuration functions
221 @verbatim
222 ===============================================================================
223 ##### Initialization and Configuration functions #####
224 ===============================================================================
225 [..]
226 This subsection provides a set of functions allowing to initialize the USART
227 in asynchronous and in synchronous modes.
228 (+) For the asynchronous mode only these parameters can be configured:
229 (++) Baud Rate
230 (++) Word Length
231 (++) Stop Bit
232 (++) Parity: If the parity is enabled, then the MSB bit of the data written
233 in the data register is transmitted but is changed by the parity bit.
234 (++) USART polarity
235 (++) USART phase
236 (++) USART LastBit
237 (++) Receiver/transmitter modes
239 [..]
240 The HAL_USART_Init() function follows the USART synchronous configuration
241 procedure (details for the procedure are available in reference manual).
243 @endverbatim
245 Depending on the frame length defined by the M1 and M0 bits (7-bit,
246 8-bit or 9-bit), the possible USART formats are listed in the
247 following table.
249 Table 1. USART frame format.
250 +-----------------------------------------------------------------------+
251 | M1 bit | M0 bit | PCE bit | USART frame |
252 |---------|---------|-----------|---------------------------------------|
253 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
254 |---------|---------|-----------|---------------------------------------|
255 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
256 |---------|---------|-----------|---------------------------------------|
257 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
258 |---------|---------|-----------|---------------------------------------|
259 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
260 |---------|---------|-----------|---------------------------------------|
261 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
262 |---------|---------|-----------|---------------------------------------|
263 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
264 +-----------------------------------------------------------------------+
266 * @{
270 * @brief Initialize the USART mode according to the specified
271 * parameters in the USART_InitTypeDef and initialize the associated handle.
272 * @param husart USART handle.
273 * @retval HAL status
275 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
277 /* Check the USART handle allocation */
278 if (husart == NULL)
280 return HAL_ERROR;
283 /* Check the parameters */
284 assert_param(IS_USART_INSTANCE(husart->Instance));
286 if (husart->State == HAL_USART_STATE_RESET)
288 /* Allocate lock resource and initialize it */
289 husart->Lock = HAL_UNLOCKED;
291 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
292 USART_InitCallbacksToDefault(husart);
294 if (husart->MspInitCallback == NULL)
296 husart->MspInitCallback = HAL_USART_MspInit;
299 /* Init the low level hardware */
300 husart->MspInitCallback(husart);
301 #else
302 /* Init the low level hardware : GPIO, CLOCK */
303 HAL_USART_MspInit(husart);
304 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
307 husart->State = HAL_USART_STATE_BUSY;
309 /* Disable the Peripheral */
310 __HAL_USART_DISABLE(husart);
312 /* Set the Usart Communication parameters */
313 if (USART_SetConfig(husart) == HAL_ERROR)
315 return HAL_ERROR;
318 /* In Synchronous mode, the following bits must be kept cleared:
319 - LINEN bit in the USART_CR2 register
320 - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
321 husart->Instance->CR2 &= ~USART_CR2_LINEN;
322 husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
324 /* Enable the Peripheral */
325 __HAL_USART_ENABLE(husart);
327 /* TEACK and/or REACK to check before moving husart->State to Ready */
328 return (USART_CheckIdleState(husart));
332 * @brief DeInitialize the USART peripheral.
333 * @param husart USART handle.
334 * @retval HAL status
336 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
338 /* Check the USART handle allocation */
339 if (husart == NULL)
341 return HAL_ERROR;
344 /* Check the parameters */
345 assert_param(IS_USART_INSTANCE(husart->Instance));
347 husart->State = HAL_USART_STATE_BUSY;
349 husart->Instance->CR1 = 0x0U;
350 husart->Instance->CR2 = 0x0U;
351 husart->Instance->CR3 = 0x0U;
353 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
354 if (husart->MspDeInitCallback == NULL)
356 husart->MspDeInitCallback = HAL_USART_MspDeInit;
358 /* DeInit the low level hardware */
359 husart->MspDeInitCallback(husart);
360 #else
361 /* DeInit the low level hardware */
362 HAL_USART_MspDeInit(husart);
363 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
365 husart->ErrorCode = HAL_USART_ERROR_NONE;
366 husart->State = HAL_USART_STATE_RESET;
368 /* Process Unlock */
369 __HAL_UNLOCK(husart);
371 return HAL_OK;
375 * @brief Initialize the USART MSP.
376 * @param husart USART handle.
377 * @retval None
379 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
381 /* Prevent unused argument(s) compilation warning */
382 UNUSED(husart);
384 /* NOTE : This function should not be modified, when the callback is needed,
385 the HAL_USART_MspInit can be implemented in the user file
390 * @brief DeInitialize the USART MSP.
391 * @param husart USART handle.
392 * @retval None
394 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
396 /* Prevent unused argument(s) compilation warning */
397 UNUSED(husart);
399 /* NOTE : This function should not be modified, when the callback is needed,
400 the HAL_USART_MspDeInit can be implemented in the user file
404 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
406 * @brief Register a User USART Callback
407 * To be used instead of the weak predefined callback
408 * @param husart usart handle
409 * @param CallbackID ID of the callback to be registered
410 * This parameter can be one of the following values:
411 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
412 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
413 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
414 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
415 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
416 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
417 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
418 * @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
419 * @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
420 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
421 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
422 * @param pCallback pointer to the Callback function
423 * @retval HAL status
424 + */
425 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
426 pUSART_CallbackTypeDef pCallback)
428 HAL_StatusTypeDef status = HAL_OK;
430 if (pCallback == NULL)
432 /* Update the error code */
433 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
435 return HAL_ERROR;
437 /* Process locked */
438 __HAL_LOCK(husart);
440 if (husart->State == HAL_USART_STATE_READY)
442 switch (CallbackID)
444 case HAL_USART_TX_HALFCOMPLETE_CB_ID :
445 husart->TxHalfCpltCallback = pCallback;
446 break;
448 case HAL_USART_TX_COMPLETE_CB_ID :
449 husart->TxCpltCallback = pCallback;
450 break;
452 case HAL_USART_RX_HALFCOMPLETE_CB_ID :
453 husart->RxHalfCpltCallback = pCallback;
454 break;
456 case HAL_USART_RX_COMPLETE_CB_ID :
457 husart->RxCpltCallback = pCallback;
458 break;
460 case HAL_USART_TX_RX_COMPLETE_CB_ID :
461 husart->TxRxCpltCallback = pCallback;
462 break;
464 case HAL_USART_ERROR_CB_ID :
465 husart->ErrorCallback = pCallback;
466 break;
468 case HAL_USART_ABORT_COMPLETE_CB_ID :
469 husart->AbortCpltCallback = pCallback;
470 break;
472 case HAL_USART_RX_FIFO_FULL_CB_ID :
473 husart->RxFifoFullCallback = pCallback;
474 break;
476 case HAL_USART_TX_FIFO_EMPTY_CB_ID :
477 husart->TxFifoEmptyCallback = pCallback;
478 break;
480 case HAL_USART_MSPINIT_CB_ID :
481 husart->MspInitCallback = pCallback;
482 break;
484 case HAL_USART_MSPDEINIT_CB_ID :
485 husart->MspDeInitCallback = pCallback;
486 break;
488 default :
489 /* Update the error code */
490 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
492 /* Return error status */
493 status = HAL_ERROR;
494 break;
497 else if (husart->State == HAL_USART_STATE_RESET)
499 switch (CallbackID)
501 case HAL_USART_MSPINIT_CB_ID :
502 husart->MspInitCallback = pCallback;
503 break;
505 case HAL_USART_MSPDEINIT_CB_ID :
506 husart->MspDeInitCallback = pCallback;
507 break;
509 default :
510 /* Update the error code */
511 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
513 /* Return error status */
514 status = HAL_ERROR;
515 break;
518 else
520 /* Update the error code */
521 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
523 /* Return error status */
524 status = HAL_ERROR;
527 /* Release Lock */
528 __HAL_UNLOCK(husart);
530 return status;
534 * @brief Unregister an UART Callback
535 * UART callaback is redirected to the weak predefined callback
536 * @param husart uart handle
537 * @param CallbackID ID of the callback to be unregistered
538 * This parameter can be one of the following values:
539 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
540 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
541 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
542 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
543 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
544 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
545 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
546 * @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
547 * @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
548 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
549 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
550 * @retval HAL status
552 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
554 HAL_StatusTypeDef status = HAL_OK;
556 /* Process locked */
557 __HAL_LOCK(husart);
559 if (HAL_USART_STATE_READY == husart->State)
561 switch (CallbackID)
563 case HAL_USART_TX_HALFCOMPLETE_CB_ID :
564 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
565 break;
567 case HAL_USART_TX_COMPLETE_CB_ID :
568 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
569 break;
571 case HAL_USART_RX_HALFCOMPLETE_CB_ID :
572 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
573 break;
575 case HAL_USART_RX_COMPLETE_CB_ID :
576 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
577 break;
579 case HAL_USART_TX_RX_COMPLETE_CB_ID :
580 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
581 break;
583 case HAL_USART_ERROR_CB_ID :
584 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
585 break;
587 case HAL_USART_ABORT_COMPLETE_CB_ID :
588 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
589 break;
591 case HAL_USART_RX_FIFO_FULL_CB_ID :
592 husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
593 break;
595 case HAL_USART_TX_FIFO_EMPTY_CB_ID :
596 husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
597 break;
599 case HAL_USART_MSPINIT_CB_ID :
600 husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */
601 break;
603 case HAL_USART_MSPDEINIT_CB_ID :
604 husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */
605 break;
607 default :
608 /* Update the error code */
609 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
611 /* Return error status */
612 status = HAL_ERROR;
613 break;
616 else if (HAL_USART_STATE_RESET == husart->State)
618 switch (CallbackID)
620 case HAL_USART_MSPINIT_CB_ID :
621 husart->MspInitCallback = HAL_USART_MspInit;
622 break;
624 case HAL_USART_MSPDEINIT_CB_ID :
625 husart->MspDeInitCallback = HAL_USART_MspDeInit;
626 break;
628 default :
629 /* Update the error code */
630 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
632 /* Return error status */
633 status = HAL_ERROR;
634 break;
637 else
639 /* Update the error code */
640 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
642 /* Return error status */
643 status = HAL_ERROR;
646 /* Release Lock */
647 __HAL_UNLOCK(husart);
649 return status;
651 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
655 * @}
658 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
659 * @brief USART Transmit and Receive functions
661 @verbatim
662 ===============================================================================
663 ##### IO operation functions #####
664 ===============================================================================
665 [..] This subsection provides a set of functions allowing to manage the USART synchronous
666 data transfers.
668 [..] The USART supports master mode only: it cannot receive or send data related to an input
669 clock (SCLK is always an output).
671 [..]
673 (#) There are two modes of transfer:
674 (++) Blocking mode: The communication is performed in polling mode.
675 The HAL status of all data processing is returned by the same function
676 after finishing transfer.
677 (++) No-Blocking mode: The communication is performed using Interrupts
678 or DMA, These API's return the HAL status.
679 The end of the data processing will be indicated through the
680 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
681 using DMA mode.
682 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
683 will be executed respectively at the end of the transmit or Receive process
684 The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
686 (#) Blocking mode API's are :
687 (++) HAL_USART_Transmit() in simplex mode
688 (++) HAL_USART_Receive() in full duplex receive only
689 (++) HAL_USART_TransmitReceive() in full duplex mode
691 (#) Non-Blocking mode API's with Interrupt are :
692 (++) HAL_USART_Transmit_IT() in simplex mode
693 (++) HAL_USART_Receive_IT() in full duplex receive only
694 (++) HAL_USART_TransmitReceive_IT() in full duplex mode
695 (++) HAL_USART_IRQHandler()
697 (#) No-Blocking mode API's with DMA are :
698 (++) HAL_USART_Transmit_DMA() in simplex mode
699 (++) HAL_USART_Receive_DMA() in full duplex receive only
700 (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
701 (++) HAL_USART_DMAPause()
702 (++) HAL_USART_DMAResume()
703 (++) HAL_USART_DMAStop()
705 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
706 (++) HAL_USART_TxCpltCallback()
707 (++) HAL_USART_RxCpltCallback()
708 (++) HAL_USART_TxHalfCpltCallback()
709 (++) HAL_USART_RxHalfCpltCallback()
710 (++) HAL_USART_ErrorCallback()
711 (++) HAL_USART_TxRxCpltCallback()
713 (#) Non-Blocking mode transfers could be aborted using Abort API's :
714 (++) HAL_USART_Abort()
715 (++) HAL_USART_Abort_IT()
717 (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
718 (++) HAL_USART_AbortCpltCallback()
720 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
721 Errors are handled as follows :
722 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
723 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
724 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
725 and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
726 If user wants to abort it, Abort services should be called by user.
727 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
728 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
729 Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
731 @endverbatim
732 * @{
736 * @brief Simplex send an amount of data in blocking mode.
737 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
738 * the sent data is handled as a set of u16. In this case, Size must indicate the number
739 * of u16 provided through pTxData.
740 * @param husart USART handle.
741 * @param pTxData Pointer to data buffer (u8 or u16 data elements).
742 * @param Size Amount of data elements (u8 or u16) to be sent.
743 * @param Timeout Timeout duration.
744 * @retval HAL status
746 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
748 uint8_t *ptxdata8bits;
749 uint16_t *ptxdata16bits;
750 uint32_t tickstart;
752 if (husart->State == HAL_USART_STATE_READY)
754 if ((pTxData == NULL) || (Size == 0U))
756 return HAL_ERROR;
759 /* Process Locked */
760 __HAL_LOCK(husart);
762 husart->ErrorCode = HAL_USART_ERROR_NONE;
763 husart->State = HAL_USART_STATE_BUSY_TX;
765 /* Init tickstart for timeout managment*/
766 tickstart = HAL_GetTick();
768 husart->TxXferSize = Size;
769 husart->TxXferCount = Size;
771 /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
772 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
774 ptxdata8bits = NULL;
775 ptxdata16bits = (uint16_t *) pTxData;
777 else
779 ptxdata8bits = pTxData;
780 ptxdata16bits = NULL;
783 /* Check the remaining data to be sent */
784 while (husart->TxXferCount > 0U)
786 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
788 return HAL_TIMEOUT;
790 if (ptxdata8bits == NULL)
792 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & 0x01FFU);
793 ptxdata16bits++;
795 else
797 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & 0xFFU);
798 ptxdata8bits++;
801 husart->TxXferCount--;
804 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
806 return HAL_TIMEOUT;
809 /* Clear Transmission Complete Flag */
810 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
812 /* Clear overrun flag and discard the received data */
813 __HAL_USART_CLEAR_OREFLAG(husart);
814 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
815 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
817 /* At end of Tx process, restore husart->State to Ready */
818 husart->State = HAL_USART_STATE_READY;
820 /* Process Unlocked */
821 __HAL_UNLOCK(husart);
823 return HAL_OK;
825 else
827 return HAL_BUSY;
832 * @brief Receive an amount of data in blocking mode.
833 * @note To receive synchronous data, dummy data are simultaneously transmitted.
834 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
835 * the received data is handled as a set of u16. In this case, Size must indicate the number
836 * of u16 available through pRxData.
837 * @param husart USART handle.
838 * @param pRxData Pointer to data buffer (u8 or u16 data elements).
839 * @param Size Amount of data elements (u8 or u16) to be received.
840 * @param Timeout Timeout duration.
841 * @retval HAL status
843 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
845 uint8_t *prxdata8bits;
846 uint16_t *prxdata16bits;
847 uint16_t uhMask;
848 uint32_t tickstart;
850 if (husart->State == HAL_USART_STATE_READY)
852 if ((pRxData == NULL) || (Size == 0U))
854 return HAL_ERROR;
857 /* Process Locked */
858 __HAL_LOCK(husart);
860 husart->ErrorCode = HAL_USART_ERROR_NONE;
861 husart->State = HAL_USART_STATE_BUSY_RX;
863 /* Init tickstart for timeout managment*/
864 tickstart = HAL_GetTick();
866 husart->RxXferSize = Size;
867 husart->RxXferCount = Size;
869 /* Computation of USART mask to apply to RDR register */
870 USART_MASK_COMPUTATION(husart);
871 uhMask = husart->Mask;
873 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
874 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
876 prxdata8bits = NULL;
877 prxdata16bits = (uint16_t *) pRxData;
879 else
881 prxdata8bits = pRxData;
882 prxdata16bits = NULL;
885 /* as long as data have to be received */
886 while (husart->RxXferCount > 0U)
888 if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
890 /* Wait until TXE flag is set to send dummy byte in order to generate the
891 * clock for the slave to send data.
892 * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
893 * can be written for all the cases. */
894 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
896 return HAL_TIMEOUT;
898 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
901 /* Wait for RXNE Flag */
902 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
904 return HAL_TIMEOUT;
907 if (prxdata8bits == NULL)
909 *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
910 prxdata16bits++;
912 else
914 *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
915 prxdata8bits++;
918 husart->RxXferCount--;
922 /* Clear SPI slave underrun flag and discard transmit data */
923 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
925 __HAL_USART_CLEAR_UDRFLAG(husart);
926 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
929 /* At end of Rx process, restore husart->State to Ready */
930 husart->State = HAL_USART_STATE_READY;
932 /* Process Unlocked */
933 __HAL_UNLOCK(husart);
935 return HAL_OK;
937 else
939 return HAL_BUSY;
944 * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
945 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
946 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
947 * of u16 available through pTxData and through pRxData.
948 * @param husart USART handle.
949 * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
950 * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
951 * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received).
952 * @param Timeout Timeout duration.
953 * @retval HAL status
955 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,
956 uint16_t Size, uint32_t Timeout)
958 uint8_t *prxdata8bits;
959 uint16_t *prxdata16bits;
960 uint8_t *ptxdata8bits;
961 uint16_t *ptxdata16bits;
962 uint16_t uhMask;
963 uint16_t rxdatacount;
964 uint32_t tickstart;
966 if (husart->State == HAL_USART_STATE_READY)
968 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
970 return HAL_ERROR;
973 /* Process Locked */
974 __HAL_LOCK(husart);
976 husart->ErrorCode = HAL_USART_ERROR_NONE;
977 husart->State = HAL_USART_STATE_BUSY_RX;
979 /* Init tickstart for timeout managment*/
980 tickstart = HAL_GetTick();
982 husart->RxXferSize = Size;
983 husart->TxXferSize = Size;
984 husart->TxXferCount = Size;
985 husart->RxXferCount = Size;
987 /* Computation of USART mask to apply to RDR register */
988 USART_MASK_COMPUTATION(husart);
989 uhMask = husart->Mask;
991 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
992 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
994 prxdata8bits = NULL;
995 ptxdata8bits = NULL;
996 ptxdata16bits = (uint16_t *) pTxData;
997 prxdata16bits = (uint16_t *) pRxData;
999 else
1001 prxdata8bits = pRxData;
1002 ptxdata8bits = pTxData;
1003 ptxdata16bits = NULL;
1004 prxdata16bits = NULL;
1007 if ((husart->TxXferCount == 0x01U) || (husart->SlaveMode == USART_SLAVEMODE_ENABLE))
1009 /* Wait until TXE flag is set to send data */
1010 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1012 return HAL_TIMEOUT;
1014 if (ptxdata8bits == NULL)
1016 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
1017 ptxdata16bits++;
1019 else
1021 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
1022 ptxdata8bits++;
1025 husart->TxXferCount--;
1028 /* Check the remain data to be sent */
1029 /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
1030 rxdatacount = husart->RxXferCount;
1031 while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
1033 if (husart->TxXferCount > 0U)
1035 /* Wait until TXE flag is set to send data */
1036 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1038 return HAL_TIMEOUT;
1040 if (ptxdata8bits == NULL)
1042 husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
1043 ptxdata16bits++;
1045 else
1047 husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
1048 ptxdata8bits++;
1051 husart->TxXferCount--;
1054 if (husart->RxXferCount > 0U)
1056 /* Wait for RXNE Flag */
1057 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1059 return HAL_TIMEOUT;
1062 if (prxdata8bits == NULL)
1064 *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
1065 prxdata16bits++;
1067 else
1069 *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
1070 prxdata8bits++;
1073 husart->RxXferCount--;
1075 rxdatacount = husart->RxXferCount;
1078 /* At end of TxRx process, restore husart->State to Ready */
1079 husart->State = HAL_USART_STATE_READY;
1081 /* Process Unlocked */
1082 __HAL_UNLOCK(husart);
1084 return HAL_OK;
1086 else
1088 return HAL_BUSY;
1093 * @brief Send an amount of data in interrupt mode.
1094 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1095 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1096 * of u16 provided through pTxData.
1097 * @param husart USART handle.
1098 * @param pTxData pointer to data buffer (u8 or u16 data elements).
1099 * @param Size amount of data elements (u8 or u16) to be sent.
1100 * @retval HAL status
1102 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
1104 if (husart->State == HAL_USART_STATE_READY)
1106 if ((pTxData == NULL) || (Size == 0U))
1108 return HAL_ERROR;
1111 /* Process Locked */
1112 __HAL_LOCK(husart);
1114 husart->pTxBuffPtr = pTxData;
1115 husart->TxXferSize = Size;
1116 husart->TxXferCount = Size;
1117 husart->TxISR = NULL;
1119 husart->ErrorCode = HAL_USART_ERROR_NONE;
1120 husart->State = HAL_USART_STATE_BUSY_TX;
1122 /* The USART Error Interrupts: (Frame error, noise error, overrun error)
1123 are not managed by the USART Transmit Process to avoid the overrun interrupt
1124 when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
1125 to benefit for the frame error and noise interrupts the usart mode should be
1126 configured only for transmit "USART_MODE_TX" */
1128 /* Configure Tx interrupt processing */
1129 if (husart->FifoMode == USART_FIFOMODE_ENABLE)
1131 /* Set the Tx ISR function pointer according to the data word length */
1132 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1134 husart->TxISR = USART_TxISR_16BIT_FIFOEN;
1136 else
1138 husart->TxISR = USART_TxISR_8BIT_FIFOEN;
1141 /* Process Unlocked */
1142 __HAL_UNLOCK(husart);
1144 /* Enable the TX FIFO threshold interrupt */
1145 __HAL_USART_ENABLE_IT(husart, USART_IT_TXFT);
1147 else
1149 /* Set the Tx ISR function pointer according to the data word length */
1150 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1152 husart->TxISR = USART_TxISR_16BIT;
1154 else
1156 husart->TxISR = USART_TxISR_8BIT;
1159 /* Process Unlocked */
1160 __HAL_UNLOCK(husart);
1162 /* Enable the USART Transmit Data Register Empty Interrupt */
1163 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
1166 return HAL_OK;
1168 else
1170 return HAL_BUSY;
1175 * @brief Receive an amount of data in interrupt mode.
1176 * @note To receive synchronous data, dummy data are simultaneously transmitted.
1177 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1178 * the received data is handled as a set of u16. In this case, Size must indicate the number
1179 * of u16 available through pRxData.
1180 * @param husart USART handle.
1181 * @param pRxData pointer to data buffer (u8 or u16 data elements).
1182 * @param Size amount of data elements (u8 or u16) to be received.
1183 * @retval HAL status
1185 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1187 uint16_t nb_dummy_data;
1189 if (husart->State == HAL_USART_STATE_READY)
1191 if ((pRxData == NULL) || (Size == 0U))
1193 return HAL_ERROR;
1196 /* Process Locked */
1197 __HAL_LOCK(husart);
1199 husart->pRxBuffPtr = pRxData;
1200 husart->RxXferSize = Size;
1201 husart->RxXferCount = Size;
1202 husart->RxISR = NULL;
1204 USART_MASK_COMPUTATION(husart);
1206 husart->ErrorCode = HAL_USART_ERROR_NONE;
1207 husart->State = HAL_USART_STATE_BUSY_RX;
1209 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1210 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1212 /* Configure Rx interrupt processing */
1213 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1215 /* Set the Rx ISR function pointer according to the data word length */
1216 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1218 husart->RxISR = USART_RxISR_16BIT_FIFOEN;
1220 else
1222 husart->RxISR = USART_RxISR_8BIT_FIFOEN;
1225 /* Process Unlocked */
1226 __HAL_UNLOCK(husart);
1228 /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */
1229 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1230 SET_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
1232 else
1234 /* Set the Rx ISR function pointer according to the data word length */
1235 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1237 husart->RxISR = USART_RxISR_16BIT;
1239 else
1241 husart->RxISR = USART_RxISR_8BIT;
1244 /* Process Unlocked */
1245 __HAL_UNLOCK(husart);
1247 /* Enable the USART Parity Error and Data Register not empty Interrupts */
1248 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1251 if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
1253 /* Send dummy data in order to generate the clock for the Slave to send the next data.
1254 When FIFO mode is disabled only one data must be transferred.
1255 When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold.
1257 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1259 for (nb_dummy_data = husart->NbRxDataToProcess ; nb_dummy_data > 0U ; nb_dummy_data--)
1261 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
1264 else
1266 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
1270 return HAL_OK;
1272 else
1274 return HAL_BUSY;
1279 * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
1280 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1281 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1282 * of u16 available through pTxData and through pRxData.
1283 * @param husart USART handle.
1284 * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
1285 * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
1286 * @param Size amount of data elements (u8 or u16) to be sent (same amount to be received).
1287 * @retval HAL status
1289 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,
1290 uint16_t Size)
1293 if (husart->State == HAL_USART_STATE_READY)
1295 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1297 return HAL_ERROR;
1300 /* Process Locked */
1301 __HAL_LOCK(husart);
1303 husart->pRxBuffPtr = pRxData;
1304 husart->RxXferSize = Size;
1305 husart->RxXferCount = Size;
1306 husart->pTxBuffPtr = pTxData;
1307 husart->TxXferSize = Size;
1308 husart->TxXferCount = Size;
1310 /* Computation of USART mask to apply to RDR register */
1311 USART_MASK_COMPUTATION(husart);
1313 husart->ErrorCode = HAL_USART_ERROR_NONE;
1314 husart->State = HAL_USART_STATE_BUSY_TX_RX;
1316 /* Configure TxRx interrupt processing */
1317 if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1319 /* Set the Rx ISR function pointer according to the data word length */
1320 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1322 husart->TxISR = USART_TxISR_16BIT_FIFOEN;
1323 husart->RxISR = USART_RxISR_16BIT_FIFOEN;
1325 else
1327 husart->TxISR = USART_TxISR_8BIT_FIFOEN;
1328 husart->RxISR = USART_RxISR_8BIT_FIFOEN;
1331 /* Process Locked */
1332 __HAL_UNLOCK(husart);
1334 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1335 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1337 /* Enable the USART Parity Error interrupt */
1338 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1340 /* Enable the TX and RX FIFO Threshold interrupts */
1341 SET_BIT(husart->Instance->CR3, (USART_CR3_TXFTIE | USART_CR3_RXFTIE));
1343 else
1345 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1347 husart->TxISR = USART_TxISR_16BIT;
1348 husart->RxISR = USART_RxISR_16BIT;
1350 else
1352 husart->TxISR = USART_TxISR_8BIT;
1353 husart->RxISR = USART_RxISR_8BIT;
1356 /* Process Locked */
1357 __HAL_UNLOCK(husart);
1359 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1360 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1362 /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
1363 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1365 /* Enable the USART Transmit Data Register Empty Interrupt */
1366 SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1369 return HAL_OK;
1371 else
1373 return HAL_BUSY;
1378 * @brief Send an amount of data in DMA mode.
1379 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1380 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1381 * of u16 provided through pTxData.
1382 * @param husart USART handle.
1383 * @param pTxData pointer to data buffer (u8 or u16 data elements).
1384 * @param Size amount of data elements (u8 or u16) to be sent.
1385 * @retval HAL status
1387 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
1389 HAL_StatusTypeDef status = HAL_OK;
1390 uint32_t *tmp;
1392 if (husart->State == HAL_USART_STATE_READY)
1394 if ((pTxData == NULL) || (Size == 0U))
1396 return HAL_ERROR;
1399 /* Process Locked */
1400 __HAL_LOCK(husart);
1402 husart->pTxBuffPtr = pTxData;
1403 husart->TxXferSize = Size;
1404 husart->TxXferCount = Size;
1406 husart->ErrorCode = HAL_USART_ERROR_NONE;
1407 husart->State = HAL_USART_STATE_BUSY_TX;
1409 if (husart->hdmatx != NULL)
1411 /* Set the USART DMA transfer complete callback */
1412 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1414 /* Set the USART DMA Half transfer complete callback */
1415 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1417 /* Set the DMA error callback */
1418 husart->hdmatx->XferErrorCallback = USART_DMAError;
1420 /* Enable the USART transmit DMA channel */
1421 tmp = (uint32_t *)&pTxData;
1422 status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1425 if (status == HAL_OK)
1427 /* Clear the TC flag in the ICR register */
1428 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
1430 /* Process Unlocked */
1431 __HAL_UNLOCK(husart);
1433 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1434 in the USART CR3 register */
1435 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1437 return HAL_OK;
1439 else
1441 /* Set error code to DMA */
1442 husart->ErrorCode = HAL_USART_ERROR_DMA;
1444 /* Process Unlocked */
1445 __HAL_UNLOCK(husart);
1447 /* Restore husart->State to ready */
1448 husart->State = HAL_USART_STATE_READY;
1450 return HAL_ERROR;
1453 else
1455 return HAL_BUSY;
1460 * @brief Receive an amount of data in DMA mode.
1461 * @note When the USART parity is enabled (PCE = 1), the received data contain
1462 * the parity bit (MSB position).
1463 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
1464 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1465 * the received data is handled as a set of u16. In this case, Size must indicate the number
1466 * of u16 available through pRxData.
1467 * @param husart USART handle.
1468 * @param pRxData pointer to data buffer (u8 or u16 data elements).
1469 * @param Size amount of data elements (u8 or u16) to be received.
1470 * @retval HAL status
1472 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1474 HAL_StatusTypeDef status = HAL_OK;
1475 uint32_t *tmp = (uint32_t *)&pRxData;
1477 /* Check that a Rx process is not already ongoing */
1478 if (husart->State == HAL_USART_STATE_READY)
1480 if ((pRxData == NULL) || (Size == 0U))
1482 return HAL_ERROR;
1485 /* Process Locked */
1486 __HAL_LOCK(husart);
1488 husart->pRxBuffPtr = pRxData;
1489 husart->RxXferSize = Size;
1490 husart->pTxBuffPtr = pRxData;
1491 husart->TxXferSize = Size;
1493 husart->ErrorCode = HAL_USART_ERROR_NONE;
1494 husart->State = HAL_USART_STATE_BUSY_RX;
1496 if (husart->hdmarx != NULL)
1498 /* Set the USART DMA Rx transfer complete callback */
1499 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1501 /* Set the USART DMA Half transfer complete callback */
1502 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1504 /* Set the USART DMA Rx transfer error callback */
1505 husart->hdmarx->XferErrorCallback = USART_DMAError;
1507 /* Enable the USART receive DMA channel */
1508 status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size);
1511 if ((status == HAL_OK) &&
1512 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
1514 /* Enable the USART transmit DMA channel: the transmit channel is used in order
1515 to generate in the non-blocking mode the clock to the slave device,
1516 this mode isn't a simplex receive mode but a full-duplex receive mode */
1518 /* Set the USART DMA Tx Complete and Error callback to Null */
1519 if (husart->hdmatx != NULL)
1521 husart->hdmatx->XferErrorCallback = NULL;
1522 husart->hdmatx->XferHalfCpltCallback = NULL;
1523 husart->hdmatx->XferCpltCallback = NULL;
1524 status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1528 if (status == HAL_OK)
1530 /* Process Unlocked */
1531 __HAL_UNLOCK(husart);
1533 /* Enable the USART Parity Error Interrupt */
1534 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1536 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1537 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1539 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1540 in the USART CR3 register */
1541 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1543 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1544 in the USART CR3 register */
1545 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1547 return HAL_OK;
1549 else
1551 if (husart->hdmarx != NULL)
1553 status = HAL_DMA_Abort(husart->hdmarx);
1556 /* No need to check on error code */
1557 UNUSED(status);
1559 /* Set error code to DMA */
1560 husart->ErrorCode = HAL_USART_ERROR_DMA;
1562 /* Process Unlocked */
1563 __HAL_UNLOCK(husart);
1565 /* Restore husart->State to ready */
1566 husart->State = HAL_USART_STATE_READY;
1568 return HAL_ERROR;
1571 else
1573 return HAL_BUSY;
1578 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
1579 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1580 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1581 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1582 * of u16 available through pTxData and through pRxData.
1583 * @param husart USART handle.
1584 * @param pTxData pointer to TX data buffer (u8 or u16 data elements).
1585 * @param pRxData pointer to RX data buffer (u8 or u16 data elements).
1586 * @param Size amount of data elements (u8 or u16) to be received/sent.
1587 * @retval HAL status
1589 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,
1590 uint16_t Size)
1592 HAL_StatusTypeDef status;
1593 uint32_t *tmp;
1595 if (husart->State == HAL_USART_STATE_READY)
1597 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1599 return HAL_ERROR;
1602 /* Process Locked */
1603 __HAL_LOCK(husart);
1605 husart->pRxBuffPtr = pRxData;
1606 husart->RxXferSize = Size;
1607 husart->pTxBuffPtr = pTxData;
1608 husart->TxXferSize = Size;
1610 husart->ErrorCode = HAL_USART_ERROR_NONE;
1611 husart->State = HAL_USART_STATE_BUSY_TX_RX;
1613 if ((husart->hdmarx != NULL) && (husart->hdmatx != NULL))
1615 /* Set the USART DMA Rx transfer complete callback */
1616 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1618 /* Set the USART DMA Half transfer complete callback */
1619 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1621 /* Set the USART DMA Tx transfer complete callback */
1622 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1624 /* Set the USART DMA Half transfer complete callback */
1625 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1627 /* Set the USART DMA Tx transfer error callback */
1628 husart->hdmatx->XferErrorCallback = USART_DMAError;
1630 /* Set the USART DMA Rx transfer error callback */
1631 husart->hdmarx->XferErrorCallback = USART_DMAError;
1633 /* Enable the USART receive DMA channel */
1634 tmp = (uint32_t *)&pRxData;
1635 status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size);
1637 /* Enable the USART transmit DMA channel */
1638 if (status == HAL_OK)
1640 tmp = (uint32_t *)&pTxData;
1641 status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1644 else
1646 status = HAL_ERROR;
1649 if (status == HAL_OK)
1651 /* Process Unlocked */
1652 __HAL_UNLOCK(husart);
1654 /* Enable the USART Parity Error Interrupt */
1655 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1657 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1658 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1660 /* Clear the TC flag in the ICR register */
1661 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
1663 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1664 in the USART CR3 register */
1665 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1667 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1668 in the USART CR3 register */
1669 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1671 return HAL_OK;
1673 else
1675 if (husart->hdmarx != NULL)
1677 status = HAL_DMA_Abort(husart->hdmarx);
1680 /* No need to check on error code */
1681 UNUSED(status);
1683 /* Set error code to DMA */
1684 husart->ErrorCode = HAL_USART_ERROR_DMA;
1686 /* Process Unlocked */
1687 __HAL_UNLOCK(husart);
1689 /* Restore husart->State to ready */
1690 husart->State = HAL_USART_STATE_READY;
1692 return HAL_ERROR;
1695 else
1697 return HAL_BUSY;
1702 * @brief Pause the DMA Transfer.
1703 * @param husart USART handle.
1704 * @retval HAL status
1706 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
1708 const HAL_USART_StateTypeDef state = husart->State;
1710 /* Process Locked */
1711 __HAL_LOCK(husart);
1713 if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) &&
1714 (state == HAL_USART_STATE_BUSY_TX))
1716 /* Disable the USART DMA Tx request */
1717 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1719 else if ((state == HAL_USART_STATE_BUSY_RX) ||
1720 (state == HAL_USART_STATE_BUSY_TX_RX))
1722 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1724 /* Disable the USART DMA Tx request */
1725 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1727 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1729 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1730 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1731 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1733 /* Disable the USART DMA Rx request */
1734 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1737 else
1739 /* Nothing to do */
1742 /* Process Unlocked */
1743 __HAL_UNLOCK(husart);
1745 return HAL_OK;
1749 * @brief Resume the DMA Transfer.
1750 * @param husart USART handle.
1751 * @retval HAL status
1753 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
1755 const HAL_USART_StateTypeDef state = husart->State;
1757 /* Process Locked */
1758 __HAL_LOCK(husart);
1760 if (state == HAL_USART_STATE_BUSY_TX)
1762 /* Enable the USART DMA Tx request */
1763 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1765 else if ((state == HAL_USART_STATE_BUSY_RX) ||
1766 (state == HAL_USART_STATE_BUSY_TX_RX))
1768 /* Clear the Overrun flag before resuming the Rx transfer*/
1769 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
1771 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1772 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1773 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1775 /* Enable the USART DMA Rx request before the DMA Tx request */
1776 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1778 /* Enable the USART DMA Tx request */
1779 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1781 else
1783 /* Nothing to do */
1786 /* Process Unlocked */
1787 __HAL_UNLOCK(husart);
1789 return HAL_OK;
1793 * @brief Stop the DMA Transfer.
1794 * @param husart USART handle.
1795 * @retval HAL status
1797 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
1799 /* The Lock is not implemented on this API to allow the user application
1800 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
1801 HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
1802 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1803 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1804 the stream and the corresponding call back is executed. */
1806 /* Disable the USART Tx/Rx DMA requests */
1807 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1808 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1810 /* Abort the USART DMA tx channel */
1811 if (husart->hdmatx != NULL)
1813 if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
1815 if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1817 /* Set error code to DMA */
1818 husart->ErrorCode = HAL_USART_ERROR_DMA;
1820 return HAL_TIMEOUT;
1824 /* Abort the USART DMA rx channel */
1825 if (husart->hdmarx != NULL)
1827 if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
1829 if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1831 /* Set error code to DMA */
1832 husart->ErrorCode = HAL_USART_ERROR_DMA;
1834 return HAL_TIMEOUT;
1839 USART_EndTransfer(husart);
1840 husart->State = HAL_USART_STATE_READY;
1842 return HAL_OK;
1846 * @brief Abort ongoing transfers (blocking mode).
1847 * @param husart USART handle.
1848 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1849 * This procedure performs following operations :
1850 * - Disable USART Interrupts (Tx and Rx)
1851 * - Disable the DMA transfer in the peripheral register (if enabled)
1852 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1853 * - Set handle State to READY
1854 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1855 * @retval HAL status
1857 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
1859 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1860 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
1861 USART_CR1_TCIE));
1862 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1864 /* Disable the USART DMA Tx request if enabled */
1865 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1867 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1869 /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
1870 if (husart->hdmatx != NULL)
1872 /* Set the USART DMA Abort callback to Null.
1873 No call back execution at end of DMA abort procedure */
1874 husart->hdmatx->XferAbortCallback = NULL;
1876 if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
1878 if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1880 /* Set error code to DMA */
1881 husart->ErrorCode = HAL_USART_ERROR_DMA;
1883 return HAL_TIMEOUT;
1889 /* Disable the USART DMA Rx request if enabled */
1890 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1892 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1894 /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
1895 if (husart->hdmarx != NULL)
1897 /* Set the USART DMA Abort callback to Null.
1898 No call back execution at end of DMA abort procedure */
1899 husart->hdmarx->XferAbortCallback = NULL;
1901 if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
1903 if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1905 /* Set error code to DMA */
1906 husart->ErrorCode = HAL_USART_ERROR_DMA;
1908 return HAL_TIMEOUT;
1914 /* Reset Tx and Rx transfer counters */
1915 husart->TxXferCount = 0U;
1916 husart->RxXferCount = 0U;
1918 /* Clear the Error flags in the ICR register */
1919 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
1921 /* Flush the whole TX FIFO (if needed) */
1922 if (husart->FifoMode == USART_FIFOMODE_ENABLE)
1924 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
1927 /* Discard the received data */
1928 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
1930 /* Restore husart->State to Ready */
1931 husart->State = HAL_USART_STATE_READY;
1933 /* Reset Handle ErrorCode to No Error */
1934 husart->ErrorCode = HAL_USART_ERROR_NONE;
1936 return HAL_OK;
1940 * @brief Abort ongoing transfers (Interrupt mode).
1941 * @param husart USART handle.
1942 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1943 * This procedure performs following operations :
1944 * - Disable USART Interrupts (Tx and Rx)
1945 * - Disable the DMA transfer in the peripheral register (if enabled)
1946 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1947 * - Set handle State to READY
1948 * - At abort completion, call user abort complete callback
1949 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1950 * considered as completed only when user abort complete callback is executed (not when exiting function).
1951 * @retval HAL status
1953 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
1955 uint32_t abortcplt = 1U;
1957 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1958 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
1959 USART_CR1_TCIE));
1960 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1962 /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
1963 before any call to DMA Abort functions */
1964 /* DMA Tx Handle is valid */
1965 if (husart->hdmatx != NULL)
1967 /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
1968 Otherwise, set it to NULL */
1969 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1971 husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
1973 else
1975 husart->hdmatx->XferAbortCallback = NULL;
1978 /* DMA Rx Handle is valid */
1979 if (husart->hdmarx != NULL)
1981 /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
1982 Otherwise, set it to NULL */
1983 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1985 husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
1987 else
1989 husart->hdmarx->XferAbortCallback = NULL;
1993 /* Disable the USART DMA Tx request if enabled */
1994 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1996 /* Disable DMA Tx at USART level */
1997 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1999 /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
2000 if (husart->hdmatx != NULL)
2002 /* USART Tx DMA Abort callback has already been initialised :
2003 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
2005 /* Abort DMA TX */
2006 if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
2008 husart->hdmatx->XferAbortCallback = NULL;
2010 else
2012 abortcplt = 0U;
2017 /* Disable the USART DMA Rx request if enabled */
2018 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
2020 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
2022 /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
2023 if (husart->hdmarx != NULL)
2025 /* USART Rx DMA Abort callback has already been initialised :
2026 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
2028 /* Abort DMA RX */
2029 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
2031 husart->hdmarx->XferAbortCallback = NULL;
2032 abortcplt = 1U;
2034 else
2036 abortcplt = 0U;
2041 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
2042 if (abortcplt == 1U)
2044 /* Reset Tx and Rx transfer counters */
2045 husart->TxXferCount = 0U;
2046 husart->RxXferCount = 0U;
2048 /* Reset errorCode */
2049 husart->ErrorCode = HAL_USART_ERROR_NONE;
2051 /* Clear the Error flags in the ICR register */
2052 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2054 /* Flush the whole TX FIFO (if needed) */
2055 if (husart->FifoMode == USART_FIFOMODE_ENABLE)
2057 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
2060 /* Discard the received data */
2061 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
2063 /* Restore husart->State to Ready */
2064 husart->State = HAL_USART_STATE_READY;
2066 /* As no DMA to be aborted, call directly user Abort complete callback */
2067 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2068 /* Call registered Abort Complete Callback */
2069 husart->AbortCpltCallback(husart);
2070 #else
2071 /* Call legacy weak Abort Complete Callback */
2072 HAL_USART_AbortCpltCallback(husart);
2073 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2076 return HAL_OK;
2080 * @brief Handle USART interrupt request.
2081 * @param husart USART handle.
2082 * @retval None
2084 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
2086 uint32_t isrflags = READ_REG(husart->Instance->ISR);
2087 uint32_t cr1its = READ_REG(husart->Instance->CR1);
2088 uint32_t cr3its = READ_REG(husart->Instance->CR3);
2090 uint32_t errorflags;
2091 uint32_t errorcode;
2093 /* If no error occurs */
2094 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_UDR));
2095 if (errorflags == 0U)
2097 /* USART in mode Receiver ---------------------------------------------------*/
2098 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2099 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2100 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2102 if (husart->RxISR != NULL)
2104 husart->RxISR(husart);
2106 return;
2110 /* If some errors occur */
2111 if ((errorflags != 0U)
2112 && (((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2113 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))
2115 /* USART parity error interrupt occurred -------------------------------------*/
2116 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2118 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
2120 husart->ErrorCode |= HAL_USART_ERROR_PE;
2123 /* USART frame error interrupt occurred --------------------------------------*/
2124 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2126 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
2128 husart->ErrorCode |= HAL_USART_ERROR_FE;
2131 /* USART noise error interrupt occurred --------------------------------------*/
2132 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2134 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
2136 husart->ErrorCode |= HAL_USART_ERROR_NE;
2139 /* USART Over-Run interrupt occurred -----------------------------------------*/
2140 if (((isrflags & USART_ISR_ORE) != 0U)
2141 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2142 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2144 __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
2146 husart->ErrorCode |= HAL_USART_ERROR_ORE;
2149 /* USART SPI slave underrun error interrupt occurred -------------------------*/
2150 if (((isrflags & USART_ISR_UDR) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2152 /* Ignore SPI slave underrun errors when reception is going on */
2153 if (husart->State == HAL_USART_STATE_BUSY_RX)
2155 __HAL_USART_CLEAR_UDRFLAG(husart);
2156 return;
2158 else
2160 __HAL_USART_CLEAR_UDRFLAG(husart);
2161 husart->ErrorCode |= HAL_USART_ERROR_UDR;
2165 /* Call USART Error Call back function if need be --------------------------*/
2166 if (husart->ErrorCode != HAL_USART_ERROR_NONE)
2168 /* USART in mode Receiver ---------------------------------------------------*/
2169 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2170 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2171 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2173 if (husart->RxISR != NULL)
2175 husart->RxISR(husart);
2179 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2180 consider error as blocking */
2181 errorcode = husart->ErrorCode & HAL_USART_ERROR_ORE;
2182 if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) ||
2183 (errorcode != 0U))
2185 /* Blocking error : transfer is aborted
2186 Set the USART state ready to be able to start again the process,
2187 Disable Interrupts, and disable DMA requests, if ongoing */
2188 USART_EndTransfer(husart);
2190 /* Disable the USART DMA Rx request if enabled */
2191 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
2193 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR);
2195 /* Abort the USART DMA Tx channel */
2196 if (husart->hdmatx != NULL)
2198 /* Set the USART Tx DMA Abort callback to NULL : no callback
2199 executed at end of DMA abort procedure */
2200 husart->hdmatx->XferAbortCallback = NULL;
2202 /* Abort DMA TX */
2203 (void)HAL_DMA_Abort_IT(husart->hdmatx);
2206 /* Abort the USART DMA Rx channel */
2207 if (husart->hdmarx != NULL)
2209 /* Set the USART Rx DMA Abort callback :
2210 will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
2211 husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
2213 /* Abort DMA RX */
2214 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
2216 /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
2217 husart->hdmarx->XferAbortCallback(husart->hdmarx);
2220 else
2222 /* Call user error callback */
2223 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2224 /* Call registered Error Callback */
2225 husart->ErrorCallback(husart);
2226 #else
2227 /* Call legacy weak Error Callback */
2228 HAL_USART_ErrorCallback(husart);
2229 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2232 else
2234 /* Call user error callback */
2235 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2236 /* Call registered Error Callback */
2237 husart->ErrorCallback(husart);
2238 #else
2239 /* Call legacy weak Error Callback */
2240 HAL_USART_ErrorCallback(husart);
2241 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2244 else
2246 /* Non Blocking error : transfer could go on.
2247 Error is notified to user through user error callback */
2248 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2249 /* Call registered Error Callback */
2250 husart->ErrorCallback(husart);
2251 #else
2252 /* Call legacy weak Error Callback */
2253 HAL_USART_ErrorCallback(husart);
2254 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2255 husart->ErrorCode = HAL_USART_ERROR_NONE;
2258 return;
2260 } /* End if some error occurs */
2263 /* USART in mode Transmitter ------------------------------------------------*/
2264 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2265 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2266 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2268 if (husart->TxISR != NULL)
2270 husart->TxISR(husart);
2272 return;
2275 /* USART in mode Transmitter (transmission end) -----------------------------*/
2276 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2278 USART_EndTransmit_IT(husart);
2279 return;
2282 /* USART TX Fifo Empty occurred ----------------------------------------------*/
2283 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2285 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2286 /* Call registered Tx Fifo Empty Callback */
2287 husart->TxFifoEmptyCallback(husart);
2288 #else
2289 /* Call legacy weak Tx Fifo Empty Callback */
2290 HAL_USARTEx_TxFifoEmptyCallback(husart);
2291 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2292 return;
2295 /* USART RX Fifo Full occurred ----------------------------------------------*/
2296 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2298 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2299 /* Call registered Rx Fifo Full Callback */
2300 husart->RxFifoFullCallback(husart);
2301 #else
2302 /* Call legacy weak Rx Fifo Full Callback */
2303 HAL_USARTEx_RxFifoFullCallback(husart);
2304 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2305 return;
2310 * @brief Tx Transfer completed callback.
2311 * @param husart USART handle.
2312 * @retval None
2314 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
2316 /* Prevent unused argument(s) compilation warning */
2317 UNUSED(husart);
2319 /* NOTE : This function should not be modified, when the callback is needed,
2320 the HAL_USART_TxCpltCallback can be implemented in the user file.
2325 * @brief Tx Half Transfer completed callback.
2326 * @param husart USART handle.
2327 * @retval None
2329 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
2331 /* Prevent unused argument(s) compilation warning */
2332 UNUSED(husart);
2334 /* NOTE: This function should not be modified, when the callback is needed,
2335 the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
2340 * @brief Rx Transfer completed callback.
2341 * @param husart USART handle.
2342 * @retval None
2344 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
2346 /* Prevent unused argument(s) compilation warning */
2347 UNUSED(husart);
2349 /* NOTE: This function should not be modified, when the callback is needed,
2350 the HAL_USART_RxCpltCallback can be implemented in the user file.
2355 * @brief Rx Half Transfer completed callback.
2356 * @param husart USART handle.
2357 * @retval None
2359 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
2361 /* Prevent unused argument(s) compilation warning */
2362 UNUSED(husart);
2364 /* NOTE : This function should not be modified, when the callback is needed,
2365 the HAL_USART_RxHalfCpltCallback can be implemented in the user file
2370 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
2371 * @param husart USART handle.
2372 * @retval None
2374 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
2376 /* Prevent unused argument(s) compilation warning */
2377 UNUSED(husart);
2379 /* NOTE : This function should not be modified, when the callback is needed,
2380 the HAL_USART_TxRxCpltCallback can be implemented in the user file
2385 * @brief USART error callback.
2386 * @param husart USART handle.
2387 * @retval None
2389 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
2391 /* Prevent unused argument(s) compilation warning */
2392 UNUSED(husart);
2394 /* NOTE : This function should not be modified, when the callback is needed,
2395 the HAL_USART_ErrorCallback can be implemented in the user file.
2400 * @brief USART Abort Complete callback.
2401 * @param husart USART handle.
2402 * @retval None
2404 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
2406 /* Prevent unused argument(s) compilation warning */
2407 UNUSED(husart);
2409 /* NOTE : This function should not be modified, when the callback is needed,
2410 the HAL_USART_AbortCpltCallback can be implemented in the user file.
2415 * @}
2418 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
2419 * @brief USART Peripheral State and Error functions
2421 @verbatim
2422 ==============================================================================
2423 ##### Peripheral State and Error functions #####
2424 ==============================================================================
2425 [..]
2426 This subsection provides functions allowing to :
2427 (+) Return the USART handle state
2428 (+) Return the USART handle error code
2430 @endverbatim
2431 * @{
2436 * @brief Return the USART handle state.
2437 * @param husart pointer to a USART_HandleTypeDef structure that contains
2438 * the configuration information for the specified USART.
2439 * @retval USART handle state
2441 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
2443 return husart->State;
2447 * @brief Return the USART error code.
2448 * @param husart pointer to a USART_HandleTypeDef structure that contains
2449 * the configuration information for the specified USART.
2450 * @retval USART handle Error Code
2452 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
2454 return husart->ErrorCode;
2458 * @}
2462 * @}
2465 /** @defgroup USART_Private_Functions USART Private Functions
2466 * @{
2470 * @brief Initialize the callbacks to their default values.
2471 * @param husart USART handle.
2472 * @retval none
2474 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2475 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
2477 /* Init the USART Callback settings */
2478 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
2479 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
2480 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
2481 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
2482 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
2483 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
2484 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2485 husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
2486 husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
2488 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2491 * @brief End ongoing transfer on USART peripheral (following error detection or Transfer completion).
2492 * @param husart USART handle.
2493 * @retval None
2495 static void USART_EndTransfer(USART_HandleTypeDef *husart)
2497 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
2498 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
2499 USART_CR1_TCIE));
2500 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
2502 /* At end of process, restore husart->State to Ready */
2503 husart->State = HAL_USART_STATE_READY;
2507 * @brief DMA USART transmit process complete callback.
2508 * @param hdma DMA handle.
2509 * @retval None
2511 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2513 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2515 /* DMA Normal mode */
2516 if (hdma->Init.Mode != DMA_CIRCULAR)
2518 husart->TxXferCount = 0U;
2520 if (husart->State == HAL_USART_STATE_BUSY_TX)
2522 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2523 in the USART CR3 register */
2524 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2526 /* Enable the USART Transmit Complete Interrupt */
2527 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
2530 /* DMA Circular mode */
2531 else
2533 if (husart->State == HAL_USART_STATE_BUSY_TX)
2535 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2536 /* Call registered Tx Complete Callback */
2537 husart->TxCpltCallback(husart);
2538 #else
2539 /* Call legacy weak Tx Complete Callback */
2540 HAL_USART_TxCpltCallback(husart);
2541 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2547 * @brief DMA USART transmit process half complete callback.
2548 * @param hdma DMA handle.
2549 * @retval None
2551 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2553 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2555 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2556 /* Call registered Tx Half Complete Callback */
2557 husart->TxHalfCpltCallback(husart);
2558 #else
2559 /* Call legacy weak Tx Half Complete Callback */
2560 HAL_USART_TxHalfCpltCallback(husart);
2561 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2565 * @brief DMA USART receive process complete callback.
2566 * @param hdma DMA handle.
2567 * @retval None
2569 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2571 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2573 /* DMA Normal mode */
2574 if (hdma->Init.Mode != DMA_CIRCULAR)
2576 husart->RxXferCount = 0U;
2578 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2579 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2580 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2582 /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
2583 in USART CR3 register */
2584 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
2585 /* similarly, disable the DMA TX transfer that was started to provide the
2586 clock to the slave device */
2587 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2589 if (husart->State == HAL_USART_STATE_BUSY_RX)
2591 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2592 /* Call registered Rx Complete Callback */
2593 husart->RxCpltCallback(husart);
2594 #else
2595 /* Call legacy weak Rx Complete Callback */
2596 HAL_USART_RxCpltCallback(husart);
2597 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2599 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2600 else
2602 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2603 /* Call registered Tx Rx Complete Callback */
2604 husart->TxRxCpltCallback(husart);
2605 #else
2606 /* Call legacy weak Tx Rx Complete Callback */
2607 HAL_USART_TxRxCpltCallback(husart);
2608 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2610 husart->State = HAL_USART_STATE_READY;
2612 /* DMA circular mode */
2613 else
2615 if (husart->State == HAL_USART_STATE_BUSY_RX)
2617 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2618 /* Call registered Rx Complete Callback */
2619 husart->RxCpltCallback(husart);
2620 #else
2621 /* Call legacy weak Rx Complete Callback */
2622 HAL_USART_RxCpltCallback(husart);
2623 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2625 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2626 else
2628 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2629 /* Call registered Tx Rx Complete Callback */
2630 husart->TxRxCpltCallback(husart);
2631 #else
2632 /* Call legacy weak Tx Rx Complete Callback */
2633 HAL_USART_TxRxCpltCallback(husart);
2634 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2640 * @brief DMA USART receive process half complete callback.
2641 * @param hdma DMA handle.
2642 * @retval None
2644 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2646 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2648 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2649 /* Call registered Rx Half Complete Callback */
2650 husart->RxHalfCpltCallback(husart);
2651 #else
2652 /* Call legacy weak Rx Half Complete Callback */
2653 HAL_USART_RxHalfCpltCallback(husart);
2654 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2658 * @brief DMA USART communication error callback.
2659 * @param hdma DMA handle.
2660 * @retval None
2662 static void USART_DMAError(DMA_HandleTypeDef *hdma)
2664 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2666 husart->RxXferCount = 0U;
2667 husart->TxXferCount = 0U;
2668 USART_EndTransfer(husart);
2670 husart->ErrorCode |= HAL_USART_ERROR_DMA;
2671 husart->State = HAL_USART_STATE_READY;
2673 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2674 /* Call registered Error Callback */
2675 husart->ErrorCallback(husart);
2676 #else
2677 /* Call legacy weak Error Callback */
2678 HAL_USART_ErrorCallback(husart);
2679 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2683 * @brief DMA USART communication abort callback, when initiated by HAL services on Error
2684 * (To be called at end of DMA Abort procedure following error occurrence).
2685 * @param hdma DMA handle.
2686 * @retval None
2688 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2690 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2691 husart->RxXferCount = 0U;
2692 husart->TxXferCount = 0U;
2694 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2695 /* Call registered Error Callback */
2696 husart->ErrorCallback(husart);
2697 #else
2698 /* Call legacy weak Error Callback */
2699 HAL_USART_ErrorCallback(husart);
2700 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2704 * @brief DMA USART Tx communication abort callback, when initiated by user
2705 * (To be called at end of DMA Tx Abort procedure following user abort request).
2706 * @note When this callback is executed, User Abort complete call back is called only if no
2707 * Abort still ongoing for Rx DMA Handle.
2708 * @param hdma DMA handle.
2709 * @retval None
2711 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2713 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2715 husart->hdmatx->XferAbortCallback = NULL;
2717 /* Check if an Abort process is still ongoing */
2718 if (husart->hdmarx != NULL)
2720 if (husart->hdmarx->XferAbortCallback != NULL)
2722 return;
2726 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2727 husart->TxXferCount = 0U;
2728 husart->RxXferCount = 0U;
2730 /* Reset errorCode */
2731 husart->ErrorCode = HAL_USART_ERROR_NONE;
2733 /* Clear the Error flags in the ICR register */
2734 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2736 /* Restore husart->State to Ready */
2737 husart->State = HAL_USART_STATE_READY;
2739 /* Call user Abort complete callback */
2740 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2741 /* Call registered Abort Complete Callback */
2742 husart->AbortCpltCallback(husart);
2743 #else
2744 /* Call legacy weak Abort Complete Callback */
2745 HAL_USART_AbortCpltCallback(husart);
2746 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2752 * @brief DMA USART Rx communication abort callback, when initiated by user
2753 * (To be called at end of DMA Rx Abort procedure following user abort request).
2754 * @note When this callback is executed, User Abort complete call back is called only if no
2755 * Abort still ongoing for Tx DMA Handle.
2756 * @param hdma DMA handle.
2757 * @retval None
2759 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2761 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2763 husart->hdmarx->XferAbortCallback = NULL;
2765 /* Check if an Abort process is still ongoing */
2766 if (husart->hdmatx != NULL)
2768 if (husart->hdmatx->XferAbortCallback != NULL)
2770 return;
2774 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2775 husart->TxXferCount = 0U;
2776 husart->RxXferCount = 0U;
2778 /* Reset errorCode */
2779 husart->ErrorCode = HAL_USART_ERROR_NONE;
2781 /* Clear the Error flags in the ICR register */
2782 __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2784 /* Restore husart->State to Ready */
2785 husart->State = HAL_USART_STATE_READY;
2787 /* Call user Abort complete callback */
2788 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2789 /* Call registered Abort Complete Callback */
2790 husart->AbortCpltCallback(husart);
2791 #else
2792 /* Call legacy weak Abort Complete Callback */
2793 HAL_USART_AbortCpltCallback(husart);
2794 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2799 * @brief Handle USART Communication Timeout.
2800 * @param husart USART handle.
2801 * @param Flag Specifies the USART flag to check.
2802 * @param Status the Flag status (SET or RESET).
2803 * @param Tickstart Tick start value
2804 * @param Timeout timeout duration.
2805 * @retval HAL status
2807 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
2808 uint32_t Tickstart, uint32_t Timeout)
2810 /* Wait until flag is set */
2811 while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
2813 /* Check for the Timeout */
2814 if (Timeout != HAL_MAX_DELAY)
2816 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2818 husart->State = HAL_USART_STATE_READY;
2820 /* Process Unlocked */
2821 __HAL_UNLOCK(husart);
2823 return HAL_TIMEOUT;
2827 return HAL_OK;
2831 * @brief Configure the USART peripheral.
2832 * @param husart USART handle.
2833 * @retval HAL status
2835 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
2837 uint32_t tmpreg;
2838 USART_ClockSourceTypeDef clocksource;
2839 HAL_StatusTypeDef ret = HAL_OK;
2840 uint16_t brrtemp;
2841 uint32_t usartdiv = 0x00000000;
2842 PLL2_ClocksTypeDef pll2_clocks;
2843 PLL3_ClocksTypeDef pll3_clocks;
2844 uint32_t pclk;
2846 /* Check the parameters */
2847 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
2848 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
2849 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
2850 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
2851 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
2852 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
2853 assert_param(IS_USART_PARITY(husart->Init.Parity));
2854 assert_param(IS_USART_MODE(husart->Init.Mode));
2855 assert_param(IS_USART_PRESCALER(husart->Init.ClockPrescaler));
2857 /*-------------------------- USART CR1 Configuration -----------------------*/
2858 /* Clear M, PCE, PS, TE and RE bits and configure
2859 * the USART Word Length, Parity and Mode:
2860 * set the M bits according to husart->Init.WordLength value
2861 * set PCE and PS bits according to husart->Init.Parity value
2862 * set TE and RE bits according to husart->Init.Mode value
2863 * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
2864 tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
2865 MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2867 /*---------------------------- USART CR2 Configuration ---------------------*/
2868 /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits:
2869 * set CPOL bit according to husart->Init.CLKPolarity value
2870 * set CPHA bit according to husart->Init.CLKPhase value
2871 * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
2872 * set STOP[13:12] bits according to husart->Init.StopBits value */
2873 tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
2874 tmpreg |= (uint32_t)husart->Init.CLKLastBit;
2875 tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
2876 tmpreg |= (uint32_t)husart->Init.StopBits;
2877 MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2879 /*-------------------------- USART PRESC Configuration -----------------------*/
2880 /* Configure
2881 * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */
2882 MODIFY_REG(husart->Instance->PRESC, USART_PRESC_PRESCALER, husart->Init.ClockPrescaler);
2884 /*-------------------------- USART BRR Configuration -----------------------*/
2885 /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
2886 USART_GETCLOCKSOURCE(husart, clocksource);
2888 switch (clocksource)
2890 case USART_CLOCKSOURCE_D2PCLK1:
2891 pclk = HAL_RCC_GetPCLK1Freq();
2892 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2893 break;
2894 case USART_CLOCKSOURCE_D2PCLK2:
2895 pclk = HAL_RCC_GetPCLK2Freq();
2896 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2897 break;
2898 case USART_CLOCKSOURCE_PLL2:
2899 HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2900 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll2_clocks.PLL2_Q_Frequency, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2901 break;
2902 case USART_CLOCKSOURCE_PLL3:
2903 HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2904 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll3_clocks.PLL3_Q_Frequency, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2905 break;
2906 case USART_CLOCKSOURCE_HSI:
2907 if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
2909 usartdiv = (uint32_t)(USART_DIV_SAMPLING8((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)), husart->Init.BaudRate, husart->Init.ClockPrescaler));
2911 else
2913 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2915 break;
2916 case USART_CLOCKSOURCE_CSI:
2917 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(CSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2918 break;
2919 case USART_CLOCKSOURCE_LSE:
2920 usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2921 break;
2922 default:
2923 ret = HAL_ERROR;
2924 break;
2927 /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */
2928 if ((usartdiv >= USART_BRR_MIN) && (usartdiv <= USART_BRR_MAX))
2930 brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
2931 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
2932 husart->Instance->BRR = brrtemp;
2934 else
2936 ret = HAL_ERROR;
2939 /* Initialize the number of data to process during RX/TX ISR execution */
2940 husart->NbTxDataToProcess = 1U;
2941 husart->NbRxDataToProcess = 1U;
2943 /* Clear ISR function pointers */
2944 husart->RxISR = NULL;
2945 husart->TxISR = NULL;
2947 return ret;
2951 * @brief Check the USART Idle State.
2952 * @param husart USART handle.
2953 * @retval HAL status
2955 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
2957 uint32_t tickstart;
2959 /* Initialize the USART ErrorCode */
2960 husart->ErrorCode = HAL_USART_ERROR_NONE;
2962 /* Init tickstart for timeout managment*/
2963 tickstart = HAL_GetTick();
2965 /* Check if the Transmitter is enabled */
2966 if ((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
2968 /* Wait until TEACK flag is set */
2969 if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
2971 /* Timeout occurred */
2972 return HAL_TIMEOUT;
2975 /* Check if the Receiver is enabled */
2976 if ((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
2978 /* Wait until REACK flag is set */
2979 if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
2981 /* Timeout occurred */
2982 return HAL_TIMEOUT;
2986 /* Initialize the USART state*/
2987 husart->State = HAL_USART_STATE_READY;
2989 /* Process Unlocked */
2990 __HAL_UNLOCK(husart);
2992 return HAL_OK;
2996 * @brief Simplex send an amount of data in non-blocking mode.
2997 * @note Function called under interruption only, once
2998 * interruptions have been enabled by HAL_USART_Transmit_IT().
2999 * @note The USART errors are not managed to avoid the overrun error.
3000 * @note ISR function executed when FIFO mode is disabled and when the
3001 * data word length is less than 9 bits long.
3002 * @param husart USART handle.
3003 * @retval None
3005 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart)
3007 const HAL_USART_StateTypeDef state = husart->State;
3009 /* Check that a Tx process is ongoing */
3010 if ((state == HAL_USART_STATE_BUSY_TX) ||
3011 (state == HAL_USART_STATE_BUSY_TX_RX))
3013 if (husart->TxXferCount == 0U)
3015 /* Disable the USART Transmit data register empty interrupt */
3016 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
3018 /* Enable the USART Transmit Complete Interrupt */
3019 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3021 else
3023 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
3024 husart->pTxBuffPtr++;
3025 husart->TxXferCount--;
3031 * @brief Simplex send an amount of data in non-blocking mode.
3032 * @note Function called under interruption only, once
3033 * interruptions have been enabled by HAL_USART_Transmit_IT().
3034 * @note The USART errors are not managed to avoid the overrun error.
3035 * @note ISR function executed when FIFO mode is disabled and when the
3036 * data word length is 9 bits long.
3037 * @param husart USART handle.
3038 * @retval None
3040 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart)
3042 const HAL_USART_StateTypeDef state = husart->State;
3043 uint16_t *tmp;
3045 if ((state == HAL_USART_STATE_BUSY_TX) ||
3046 (state == HAL_USART_STATE_BUSY_TX_RX))
3048 if (husart->TxXferCount == 0U)
3050 /* Disable the USART Transmit data register empty interrupt */
3051 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
3053 /* Enable the USART Transmit Complete Interrupt */
3054 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3056 else
3058 tmp = (uint16_t *) husart->pTxBuffPtr;
3059 husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
3060 husart->pTxBuffPtr += 2U;
3061 husart->TxXferCount--;
3067 * @brief Simplex send an amount of data in non-blocking mode.
3068 * @note Function called under interruption only, once
3069 * interruptions have been enabled by HAL_USART_Transmit_IT().
3070 * @note The USART errors are not managed to avoid the overrun error.
3071 * @note ISR function executed when FIFO mode is enabled and when the
3072 * data word length is less than 9 bits long.
3073 * @param husart USART handle.
3074 * @retval None
3076 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
3078 const HAL_USART_StateTypeDef state = husart->State;
3079 uint16_t nb_tx_data;
3081 /* Check that a Tx process is ongoing */
3082 if ((state == HAL_USART_STATE_BUSY_TX) ||
3083 (state == HAL_USART_STATE_BUSY_TX_RX))
3085 for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3087 if (husart->TxXferCount == 0U)
3089 /* Disable the TX FIFO threshold interrupt */
3090 __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
3092 /* Enable the USART Transmit Complete Interrupt */
3093 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3095 break; /* force exit loop */
3097 else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
3099 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
3100 husart->pTxBuffPtr++;
3101 husart->TxXferCount--;
3103 else
3105 /* Nothing to do */
3112 * @brief Simplex send an amount of data in non-blocking mode.
3113 * @note Function called under interruption only, once
3114 * interruptions have been enabled by HAL_USART_Transmit_IT().
3115 * @note The USART errors are not managed to avoid the overrun error.
3116 * @note ISR function executed when FIFO mode is enabled and when the
3117 * data word length is 9 bits long.
3118 * @param husart USART handle.
3119 * @retval None
3121 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
3123 const HAL_USART_StateTypeDef state = husart->State;
3124 uint16_t *tmp;
3125 uint16_t nb_tx_data;
3127 /* Check that a Tx process is ongoing */
3128 if ((state == HAL_USART_STATE_BUSY_TX) ||
3129 (state == HAL_USART_STATE_BUSY_TX_RX))
3131 for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3133 if (husart->TxXferCount == 0U)
3135 /* Disable the TX FIFO threshold interrupt */
3136 __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
3138 /* Enable the USART Transmit Complete Interrupt */
3139 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3141 break; /* force exit loop */
3143 else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
3145 tmp = (uint16_t *) husart->pTxBuffPtr;
3146 husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
3147 husart->pTxBuffPtr += 2U;
3148 husart->TxXferCount--;
3150 else
3152 /* Nothing to do */
3159 * @brief Wraps up transmission in non-blocking mode.
3160 * @param husart Pointer to a USART_HandleTypeDef structure that contains
3161 * the configuration information for the specified USART module.
3162 * @retval None
3164 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart)
3166 /* Disable the USART Transmit Complete Interrupt */
3167 __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
3169 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3170 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
3172 /* Clear TxISR function pointer */
3173 husart->TxISR = NULL;
3175 if (husart->State == HAL_USART_STATE_BUSY_TX)
3177 /* Clear overrun flag and discard the received data */
3178 __HAL_USART_CLEAR_OREFLAG(husart);
3179 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3181 /* Tx process is completed, restore husart->State to Ready */
3182 husart->State = HAL_USART_STATE_READY;
3184 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3185 /* Call registered Tx Complete Callback */
3186 husart->TxCpltCallback(husart);
3187 #else
3188 /* Call legacy weak Tx Complete Callback */
3189 HAL_USART_TxCpltCallback(husart);
3190 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3192 else if (husart->RxXferCount == 0U)
3194 /* TxRx process is completed, restore husart->State to Ready */
3195 husart->State = HAL_USART_STATE_READY;
3197 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3198 /* Call registered Tx Rx Complete Callback */
3199 husart->TxRxCpltCallback(husart);
3200 #else
3201 /* Call legacy weak Tx Rx Complete Callback */
3202 HAL_USART_TxRxCpltCallback(husart);
3203 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3205 else
3207 /* Nothing to do */
3213 * @brief Simplex receive an amount of data in non-blocking mode.
3214 * @note Function called under interruption only, once
3215 * interruptions have been enabled by HAL_USART_Receive_IT().
3216 * @note ISR function executed when FIFO mode is disabled and when the
3217 * data word length is less than 9 bits long.
3218 * @param husart USART handle
3219 * @retval None
3221 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart)
3223 const HAL_USART_StateTypeDef state = husart->State;
3224 uint16_t txdatacount;
3225 uint16_t uhMask = husart->Mask;
3226 uint32_t txftie;
3228 if ((state == HAL_USART_STATE_BUSY_RX) ||
3229 (state == HAL_USART_STATE_BUSY_TX_RX))
3231 *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
3232 husart->pRxBuffPtr++;
3233 husart->RxXferCount--;
3235 if (husart->RxXferCount == 0U)
3237 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3238 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3240 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3241 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
3243 /* Clear RxISR function pointer */
3244 husart->RxISR = NULL;
3246 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3247 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3248 txdatacount = husart->TxXferCount;
3250 if (state == HAL_USART_STATE_BUSY_RX)
3252 /* Clear SPI slave underrun flag and discard transmit data */
3253 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3255 __HAL_USART_CLEAR_UDRFLAG(husart);
3256 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3259 /* Rx process is completed, restore husart->State to Ready */
3260 husart->State = HAL_USART_STATE_READY;
3262 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3263 /* Call registered Rx Complete Callback */
3264 husart->RxCpltCallback(husart);
3265 #else
3266 /* Call legacy weak Rx Complete Callback */
3267 HAL_USART_RxCpltCallback(husart);
3268 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3270 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3271 (txftie != USART_CR3_TXFTIE) &&
3272 (txdatacount == 0U))
3274 /* TxRx process is completed, restore husart->State to Ready */
3275 husart->State = HAL_USART_STATE_READY;
3277 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3278 /* Call registered Tx Rx Complete Callback */
3279 husart->TxRxCpltCallback(husart);
3280 #else
3281 /* Call legacy weak Tx Rx Complete Callback */
3282 HAL_USART_TxRxCpltCallback(husart);
3283 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3285 else
3287 /* Nothing to do */
3290 else if ((state == HAL_USART_STATE_BUSY_RX) &&
3291 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3293 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3294 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3296 else
3298 /* Nothing to do */
3304 * @brief Simplex receive an amount of data in non-blocking mode.
3305 * @note Function called under interruption only, once
3306 * interruptions have been enabled by HAL_USART_Receive_IT().
3307 * @note ISR function executed when FIFO mode is disabled and when the
3308 * data word length is 9 bits long.
3309 * @param husart USART handle
3310 * @retval None
3312 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart)
3314 const HAL_USART_StateTypeDef state = husart->State;
3315 uint16_t txdatacount;
3316 uint16_t *tmp;
3317 uint16_t uhMask = husart->Mask;
3318 uint32_t txftie;
3320 if ((state == HAL_USART_STATE_BUSY_RX) ||
3321 (state == HAL_USART_STATE_BUSY_TX_RX))
3323 tmp = (uint16_t *) husart->pRxBuffPtr;
3324 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
3325 husart->pRxBuffPtr += 2U;
3326 husart->RxXferCount--;
3328 if (husart->RxXferCount == 0U)
3330 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3331 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3333 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3334 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
3336 /* Clear RxISR function pointer */
3337 husart->RxISR = NULL;
3339 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3340 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3341 txdatacount = husart->TxXferCount;
3343 if (state == HAL_USART_STATE_BUSY_RX)
3345 /* Clear SPI slave underrun flag and discard transmit data */
3346 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3348 __HAL_USART_CLEAR_UDRFLAG(husart);
3349 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3352 /* Rx process is completed, restore husart->State to Ready */
3353 husart->State = HAL_USART_STATE_READY;
3355 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3356 /* Call registered Rx Complete Callback */
3357 husart->RxCpltCallback(husart);
3358 #else
3359 /* Call legacy weak Rx Complete Callback */
3360 HAL_USART_RxCpltCallback(husart);
3361 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3363 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3364 (txftie != USART_CR3_TXFTIE) &&
3365 (txdatacount == 0U))
3367 /* TxRx process is completed, restore husart->State to Ready */
3368 husart->State = HAL_USART_STATE_READY;
3370 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3371 /* Call registered Tx Rx Complete Callback */
3372 husart->TxRxCpltCallback(husart);
3373 #else
3374 /* Call legacy weak Tx Rx Complete Callback */
3375 HAL_USART_TxRxCpltCallback(husart);
3376 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3378 else
3380 /* Nothing to do */
3383 else if ((state == HAL_USART_STATE_BUSY_RX) &&
3384 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3386 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3387 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3389 else
3391 /* Nothing to do */
3397 * @brief Simplex receive an amount of data in non-blocking mode.
3398 * @note Function called under interruption only, once
3399 * interruptions have been enabled by HAL_USART_Receive_IT().
3400 * @note ISR function executed when FIFO mode is enabled and when the
3401 * data word length is less than 9 bits long.
3402 * @param husart USART handle
3403 * @retval None
3405 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
3407 HAL_USART_StateTypeDef state = husart->State;
3408 uint16_t txdatacount;
3409 uint16_t rxdatacount;
3410 uint16_t uhMask = husart->Mask;
3411 uint16_t nb_rx_data;
3412 uint32_t txftie;
3414 /* Check that a Rx process is ongoing */
3415 if ((state == HAL_USART_STATE_BUSY_RX) ||
3416 (state == HAL_USART_STATE_BUSY_TX_RX))
3418 for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3420 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
3422 *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
3423 husart->pRxBuffPtr++;
3424 husart->RxXferCount--;
3426 if (husart->RxXferCount == 0U)
3428 /* Disable the USART Parity Error Interrupt */
3429 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
3431 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3432 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3434 /* Clear RxISR function pointer */
3435 husart->RxISR = NULL;
3437 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3438 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3439 txdatacount = husart->TxXferCount;
3441 if (state == HAL_USART_STATE_BUSY_RX)
3443 /* Clear SPI slave underrun flag and discard transmit data */
3444 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3446 __HAL_USART_CLEAR_UDRFLAG(husart);
3447 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3450 /* Rx process is completed, restore husart->State to Ready */
3451 husart->State = HAL_USART_STATE_READY;
3452 state = HAL_USART_STATE_READY;
3454 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3455 /* Call registered Rx Complete Callback */
3456 husart->RxCpltCallback(husart);
3457 #else
3458 /* Call legacy weak Rx Complete Callback */
3459 HAL_USART_RxCpltCallback(husart);
3460 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3462 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3463 (txftie != USART_CR3_TXFTIE) &&
3464 (txdatacount == 0U))
3466 /* TxRx process is completed, restore husart->State to Ready */
3467 husart->State = HAL_USART_STATE_READY;
3468 state = HAL_USART_STATE_READY;
3470 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3471 /* Call registered Tx Rx Complete Callback */
3472 husart->TxRxCpltCallback(husart);
3473 #else
3474 /* Call legacy weak Tx Rx Complete Callback */
3475 HAL_USART_TxRxCpltCallback(husart);
3476 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3478 else
3480 /* Nothing to do */
3483 else if ((state == HAL_USART_STATE_BUSY_RX) &&
3484 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3486 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3487 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3489 else
3491 /* Nothing to do */
3496 /* When remaining number of bytes to receive is less than the RX FIFO
3497 threshold, next incoming frames are processed as if FIFO mode was
3498 disabled (i.e. one interrupt per received frame).
3500 rxdatacount = husart->RxXferCount;
3501 if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
3503 /* Disable the USART RXFT interrupt*/
3504 CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
3506 /* Update the RxISR function pointer */
3507 husart->RxISR = USART_RxISR_8BIT;
3509 /* Enable the USART Data Register Not Empty interrupt */
3510 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3512 if ((husart->TxXferCount == 0U) &&
3513 (state == HAL_USART_STATE_BUSY_TX_RX) &&
3514 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3516 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3517 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3521 else
3523 /* Clear RXNE interrupt flag */
3524 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3529 * @brief Simplex receive an amount of data in non-blocking mode.
3530 * @note Function called under interruption only, once
3531 * interruptions have been enabled by HAL_USART_Receive_IT().
3532 * @note ISR function executed when FIFO mode is enabled and when the
3533 * data word length is 9 bits long.
3534 * @param husart USART handle
3535 * @retval None
3537 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
3539 HAL_USART_StateTypeDef state = husart->State;
3540 uint16_t txdatacount;
3541 uint16_t rxdatacount;
3542 uint16_t *tmp;
3543 uint16_t uhMask = husart->Mask;
3544 uint16_t nb_rx_data;
3545 uint32_t txftie;
3547 /* Check that a Tx process is ongoing */
3548 if ((state == HAL_USART_STATE_BUSY_RX) ||
3549 (state == HAL_USART_STATE_BUSY_TX_RX))
3551 for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3553 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
3555 tmp = (uint16_t *) husart->pRxBuffPtr;
3556 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
3557 husart->pRxBuffPtr += 2U;
3558 husart->RxXferCount--;
3560 if (husart->RxXferCount == 0U)
3562 /* Disable the USART Parity Error Interrupt */
3563 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
3565 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3566 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3568 /* Clear RxISR function pointer */
3569 husart->RxISR = NULL;
3571 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3572 txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3573 txdatacount = husart->TxXferCount;
3575 if (state == HAL_USART_STATE_BUSY_RX)
3577 /* Clear SPI slave underrun flag and discard transmit data */
3578 if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3580 __HAL_USART_CLEAR_UDRFLAG(husart);
3581 __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3584 /* Rx process is completed, restore husart->State to Ready */
3585 husart->State = HAL_USART_STATE_READY;
3586 state = HAL_USART_STATE_READY;
3588 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3589 /* Call registered Rx Complete Callback */
3590 husart->RxCpltCallback(husart);
3591 #else
3592 /* Call legacy weak Rx Complete Callback */
3593 HAL_USART_RxCpltCallback(husart);
3594 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3596 else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3597 (txftie != USART_CR3_TXFTIE) &&
3598 (txdatacount == 0U))
3600 /* TxRx process is completed, restore husart->State to Ready */
3601 husart->State = HAL_USART_STATE_READY;
3602 state = HAL_USART_STATE_READY;
3604 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3605 /* Call registered Tx Rx Complete Callback */
3606 husart->TxRxCpltCallback(husart);
3607 #else
3608 /* Call legacy weak Tx Rx Complete Callback */
3609 HAL_USART_TxRxCpltCallback(husart);
3610 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3612 else
3614 /* Nothing to do */
3617 else if ((state == HAL_USART_STATE_BUSY_RX) &&
3618 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3620 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3621 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3623 else
3625 /* Nothing to do */
3630 /* When remaining number of bytes to receive is less than the RX FIFO
3631 threshold, next incoming frames are processed as if FIFO mode was
3632 disabled (i.e. one interrupt per received frame).
3634 rxdatacount = husart->RxXferCount;
3635 if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
3637 /* Disable the USART RXFT interrupt*/
3638 CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
3640 /* Update the RxISR function pointer */
3641 husart->RxISR = USART_RxISR_16BIT;
3643 /* Enable the USART Data Register Not Empty interrupt */
3644 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3646 if ((husart->TxXferCount == 0U) &&
3647 (state == HAL_USART_STATE_BUSY_TX_RX) &&
3648 (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3650 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3651 husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3655 else
3657 /* Clear RXNE interrupt flag */
3658 __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3663 * @}
3666 #endif /* HAL_USART_MODULE_ENABLED */
3668 * @}
3672 * @}
3675 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/