2 ******************************************************************************
3 * @file stm32g4xx_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
9 * + Initialization and de-initialization functions
10 * + IO operation functions
11 * + Peripheral Control functions
12 * + Peripheral State and Error functions
15 ===============================================================================
16 ##### How to use this driver #####
17 ===============================================================================
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.
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 ==================================
60 The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
61 allows the user to configure dynamically the driver callbacks.
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.
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,
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.
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).
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.
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.
124 ******************************************************************************
127 * <h2><center>© 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 "stm32g4xx_hal.h"
141 /** @addtogroup STM32G4xx_HAL_Driver
145 /** @defgroup USART USART
146 * @brief HAL USART Synchronous module driver
150 #ifdef HAL_USART_MODULE_ENABLED
152 /* Private typedef -----------------------------------------------------------*/
153 /* Private define ------------------------------------------------------------*/
154 /** @defgroup USART_Private_Constants USART Private Constants
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 */
175 /* Private macros ------------------------------------------------------------*/
176 /* Private variables ---------------------------------------------------------*/
177 /* Private function prototypes -----------------------------------------------*/
178 /** @addtogroup USART_Private_Functions
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
);
212 /* Exported functions --------------------------------------------------------*/
214 /** @defgroup USART_Exported_Functions USART Exported Functions
218 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
219 * @brief Initialization and Configuration functions
222 ===============================================================================
223 ##### Initialization and Configuration functions #####
224 ===============================================================================
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:
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.
237 (++) Receiver/transmitter modes
240 The HAL_USART_Init() function follows the USART synchronous configuration
241 procedure (details for the procedure are available in reference manual).
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
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 +-----------------------------------------------------------------------+
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.
275 HAL_StatusTypeDef
HAL_USART_Init(USART_HandleTypeDef
*husart
)
277 /* Check the USART handle allocation */
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
);
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
)
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.
336 HAL_StatusTypeDef
HAL_USART_DeInit(USART_HandleTypeDef
*husart
)
338 /* Check the USART handle allocation */
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
);
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
;
369 __HAL_UNLOCK(husart
);
375 * @brief Initialize the USART MSP.
376 * @param husart USART handle.
379 __weak
void HAL_USART_MspInit(USART_HandleTypeDef
*husart
)
381 /* Prevent unused argument(s) compilation warning */
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.
394 __weak
void HAL_USART_MspDeInit(USART_HandleTypeDef
*husart
)
396 /* Prevent unused argument(s) compilation warning */
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
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
;
440 if (husart
->State
== HAL_USART_STATE_READY
)
444 case HAL_USART_TX_HALFCOMPLETE_CB_ID
:
445 husart
->TxHalfCpltCallback
= pCallback
;
448 case HAL_USART_TX_COMPLETE_CB_ID
:
449 husart
->TxCpltCallback
= pCallback
;
452 case HAL_USART_RX_HALFCOMPLETE_CB_ID
:
453 husart
->RxHalfCpltCallback
= pCallback
;
456 case HAL_USART_RX_COMPLETE_CB_ID
:
457 husart
->RxCpltCallback
= pCallback
;
460 case HAL_USART_TX_RX_COMPLETE_CB_ID
:
461 husart
->TxRxCpltCallback
= pCallback
;
464 case HAL_USART_ERROR_CB_ID
:
465 husart
->ErrorCallback
= pCallback
;
468 case HAL_USART_ABORT_COMPLETE_CB_ID
:
469 husart
->AbortCpltCallback
= pCallback
;
472 case HAL_USART_RX_FIFO_FULL_CB_ID
:
473 husart
->RxFifoFullCallback
= pCallback
;
476 case HAL_USART_TX_FIFO_EMPTY_CB_ID
:
477 husart
->TxFifoEmptyCallback
= pCallback
;
480 case HAL_USART_MSPINIT_CB_ID
:
481 husart
->MspInitCallback
= pCallback
;
484 case HAL_USART_MSPDEINIT_CB_ID
:
485 husart
->MspDeInitCallback
= pCallback
;
489 /* Update the error code */
490 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
492 /* Return error status */
497 else if (husart
->State
== HAL_USART_STATE_RESET
)
501 case HAL_USART_MSPINIT_CB_ID
:
502 husart
->MspInitCallback
= pCallback
;
505 case HAL_USART_MSPDEINIT_CB_ID
:
506 husart
->MspDeInitCallback
= pCallback
;
510 /* Update the error code */
511 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
513 /* Return error status */
520 /* Update the error code */
521 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
523 /* Return error status */
528 __HAL_UNLOCK(husart
);
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
552 HAL_StatusTypeDef
HAL_USART_UnRegisterCallback(USART_HandleTypeDef
*husart
, HAL_USART_CallbackIDTypeDef CallbackID
)
554 HAL_StatusTypeDef status
= HAL_OK
;
559 if (HAL_USART_STATE_READY
== husart
->State
)
563 case HAL_USART_TX_HALFCOMPLETE_CB_ID
:
564 husart
->TxHalfCpltCallback
= HAL_USART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
567 case HAL_USART_TX_COMPLETE_CB_ID
:
568 husart
->TxCpltCallback
= HAL_USART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
571 case HAL_USART_RX_HALFCOMPLETE_CB_ID
:
572 husart
->RxHalfCpltCallback
= HAL_USART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
575 case HAL_USART_RX_COMPLETE_CB_ID
:
576 husart
->RxCpltCallback
= HAL_USART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
579 case HAL_USART_TX_RX_COMPLETE_CB_ID
:
580 husart
->TxRxCpltCallback
= HAL_USART_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
583 case HAL_USART_ERROR_CB_ID
:
584 husart
->ErrorCallback
= HAL_USART_ErrorCallback
; /* Legacy weak ErrorCallback */
587 case HAL_USART_ABORT_COMPLETE_CB_ID
:
588 husart
->AbortCpltCallback
= HAL_USART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
591 case HAL_USART_RX_FIFO_FULL_CB_ID
:
592 husart
->RxFifoFullCallback
= HAL_USARTEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
595 case HAL_USART_TX_FIFO_EMPTY_CB_ID
:
596 husart
->TxFifoEmptyCallback
= HAL_USARTEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
599 case HAL_USART_MSPINIT_CB_ID
:
600 husart
->MspInitCallback
= HAL_USART_MspInit
; /* Legacy weak MspInitCallback */
603 case HAL_USART_MSPDEINIT_CB_ID
:
604 husart
->MspDeInitCallback
= HAL_USART_MspDeInit
; /* Legacy weak MspDeInitCallback */
608 /* Update the error code */
609 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
611 /* Return error status */
616 else if (HAL_USART_STATE_RESET
== husart
->State
)
620 case HAL_USART_MSPINIT_CB_ID
:
621 husart
->MspInitCallback
= HAL_USART_MspInit
;
624 case HAL_USART_MSPDEINIT_CB_ID
:
625 husart
->MspDeInitCallback
= HAL_USART_MspDeInit
;
629 /* Update the error code */
630 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
632 /* Return error status */
639 /* Update the error code */
640 husart
->ErrorCode
|= HAL_USART_ERROR_INVALID_CALLBACK
;
642 /* Return error status */
647 __HAL_UNLOCK(husart
);
651 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
658 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
659 * @brief USART Transmit and Receive functions
662 ===============================================================================
663 ##### IO operation functions #####
664 ===============================================================================
665 [..] This subsection provides a set of functions allowing to manage the USART synchronous
668 [..] The USART supports master mode only: it cannot receive or send data related to an input
669 clock (SCLK is always an output).
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
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.
736 * @brief Simplex send an amount of data in blocking mode.
737 * @param husart USART handle.
738 * @param pTxData Pointer to data buffer.
739 * @param Size Amount of data to be sent.
740 * @param Timeout Timeout duration.
743 HAL_StatusTypeDef
HAL_USART_Transmit(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint16_t Size
, uint32_t Timeout
)
745 uint8_t *ptxdata8bits
;
746 uint16_t *ptxdata16bits
;
749 if (husart
->State
== HAL_USART_STATE_READY
)
751 if ((pTxData
== NULL
) || (Size
== 0U))
759 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
760 husart
->State
= HAL_USART_STATE_BUSY_TX
;
762 /* Init tickstart for timeout managment*/
763 tickstart
= HAL_GetTick();
765 husart
->TxXferSize
= Size
;
766 husart
->TxXferCount
= Size
;
768 /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
769 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
772 ptxdata16bits
= (uint16_t *) pTxData
;
776 ptxdata8bits
= pTxData
;
777 ptxdata16bits
= NULL
;
780 /* Check the remaining data to be sent */
781 while (husart
->TxXferCount
> 0U)
783 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
787 if (ptxdata8bits
== NULL
)
789 husart
->Instance
->TDR
= (uint16_t)(*ptxdata16bits
& 0x01FFU
);
794 husart
->Instance
->TDR
= (uint8_t)(*ptxdata8bits
& 0xFFU
);
798 husart
->TxXferCount
--;
801 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
806 /* Clear Transmission Complete Flag */
807 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_TCF
);
809 /* Clear overrun flag and discard the received data */
810 __HAL_USART_CLEAR_OREFLAG(husart
);
811 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
812 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
814 /* At end of Tx process, restore husart->State to Ready */
815 husart
->State
= HAL_USART_STATE_READY
;
817 /* Process Unlocked */
818 __HAL_UNLOCK(husart
);
829 * @brief Receive an amount of data in blocking mode.
830 * @note To receive synchronous data, dummy data are simultaneously transmitted.
831 * @param husart USART handle.
832 * @param pRxData Pointer to data buffer.
833 * @param Size Amount of data to be received.
834 * @param Timeout Timeout duration.
837 HAL_StatusTypeDef
HAL_USART_Receive(USART_HandleTypeDef
*husart
, uint8_t *pRxData
, uint16_t Size
, uint32_t Timeout
)
839 uint8_t *prxdata8bits
;
840 uint16_t *prxdata16bits
;
844 if (husart
->State
== HAL_USART_STATE_READY
)
846 if ((pRxData
== NULL
) || (Size
== 0U))
854 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
855 husart
->State
= HAL_USART_STATE_BUSY_RX
;
857 /* Init tickstart for timeout managment*/
858 tickstart
= HAL_GetTick();
860 husart
->RxXferSize
= Size
;
861 husart
->RxXferCount
= Size
;
863 /* Computation of USART mask to apply to RDR register */
864 USART_MASK_COMPUTATION(husart
);
865 uhMask
= husart
->Mask
;
867 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
868 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
871 prxdata16bits
= (uint16_t *) pRxData
;
875 prxdata8bits
= pRxData
;
876 prxdata16bits
= NULL
;
879 /* as long as data have to be received */
880 while (husart
->RxXferCount
> 0U)
882 if (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
)
884 /* Wait until TXE flag is set to send dummy byte in order to generate the
885 * clock for the slave to send data.
886 * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
887 * can be written for all the cases. */
888 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
892 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x0FF);
895 /* Wait for RXNE Flag */
896 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
901 if (prxdata8bits
== NULL
)
903 *prxdata16bits
= (uint16_t)(husart
->Instance
->RDR
& uhMask
);
908 *prxdata8bits
= (uint8_t)(husart
->Instance
->RDR
& (uint8_t)(uhMask
& 0xFFU
));
912 husart
->RxXferCount
--;
916 /* Clear SPI slave underrun flag and discard transmit data */
917 if (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
)
919 __HAL_USART_CLEAR_UDRFLAG(husart
);
920 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
923 /* At end of Rx process, restore husart->State to Ready */
924 husart
->State
= HAL_USART_STATE_READY
;
926 /* Process Unlocked */
927 __HAL_UNLOCK(husart
);
938 * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
939 * @param husart USART handle.
940 * @param pTxData pointer to TX data buffer.
941 * @param pRxData pointer to RX data buffer.
942 * @param Size amount of data to be sent (same amount to be received).
943 * @param Timeout Timeout duration.
946 HAL_StatusTypeDef
HAL_USART_TransmitReceive(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint8_t *pRxData
,
947 uint16_t Size
, uint32_t Timeout
)
949 uint8_t *prxdata8bits
;
950 uint16_t *prxdata16bits
;
951 uint8_t *ptxdata8bits
;
952 uint16_t *ptxdata16bits
;
954 uint16_t rxdatacount
;
957 if (husart
->State
== HAL_USART_STATE_READY
)
959 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
967 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
968 husart
->State
= HAL_USART_STATE_BUSY_RX
;
970 /* Init tickstart for timeout managment*/
971 tickstart
= HAL_GetTick();
973 husart
->RxXferSize
= Size
;
974 husart
->TxXferSize
= Size
;
975 husart
->TxXferCount
= Size
;
976 husart
->RxXferCount
= Size
;
978 /* Computation of USART mask to apply to RDR register */
979 USART_MASK_COMPUTATION(husart
);
980 uhMask
= husart
->Mask
;
982 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
983 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
987 ptxdata16bits
= (uint16_t *) pTxData
;
988 prxdata16bits
= (uint16_t *) pRxData
;
992 prxdata8bits
= pRxData
;
993 ptxdata8bits
= pTxData
;
994 ptxdata16bits
= NULL
;
995 prxdata16bits
= NULL
;
998 if ((husart
->TxXferCount
== 0x01U
) || (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
))
1000 /* Wait until TXE flag is set to send data */
1001 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1005 if (ptxdata8bits
== NULL
)
1007 husart
->Instance
->TDR
= (uint16_t)(*ptxdata16bits
& uhMask
);
1012 husart
->Instance
->TDR
= (uint8_t)(*ptxdata8bits
& (uint8_t)(uhMask
& 0xFFU
));
1016 husart
->TxXferCount
--;
1019 /* Check the remain data to be sent */
1020 /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
1021 rxdatacount
= husart
->RxXferCount
;
1022 while ((husart
->TxXferCount
> 0U) || (rxdatacount
> 0U))
1024 if (husart
->TxXferCount
> 0U)
1026 /* Wait until TXE flag is set to send data */
1027 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1031 if (ptxdata8bits
== NULL
)
1033 husart
->Instance
->TDR
= (uint16_t)(*ptxdata16bits
& uhMask
);
1038 husart
->Instance
->TDR
= (uint8_t)(*ptxdata8bits
& (uint8_t)(uhMask
& 0xFFU
));
1042 husart
->TxXferCount
--;
1045 if (husart
->RxXferCount
> 0U)
1047 /* Wait for RXNE Flag */
1048 if (USART_WaitOnFlagUntilTimeout(husart
, USART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
1053 if (prxdata8bits
== NULL
)
1055 *prxdata16bits
= (uint16_t)(husart
->Instance
->RDR
& uhMask
);
1060 *prxdata8bits
= (uint8_t)(husart
->Instance
->RDR
& (uint8_t)(uhMask
& 0xFFU
));
1064 husart
->RxXferCount
--;
1066 rxdatacount
= husart
->RxXferCount
;
1069 /* At end of TxRx process, restore husart->State to Ready */
1070 husart
->State
= HAL_USART_STATE_READY
;
1072 /* Process Unlocked */
1073 __HAL_UNLOCK(husart
);
1084 * @brief Send an amount of data in interrupt mode.
1085 * @param husart USART handle.
1086 * @param pTxData pointer to data buffer.
1087 * @param Size amount of data to be sent.
1088 * @retval HAL status
1090 HAL_StatusTypeDef
HAL_USART_Transmit_IT(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint16_t Size
)
1092 if (husart
->State
== HAL_USART_STATE_READY
)
1094 if ((pTxData
== NULL
) || (Size
== 0U))
1099 /* Process Locked */
1102 husart
->pTxBuffPtr
= pTxData
;
1103 husart
->TxXferSize
= Size
;
1104 husart
->TxXferCount
= Size
;
1105 husart
->TxISR
= NULL
;
1107 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1108 husart
->State
= HAL_USART_STATE_BUSY_TX
;
1110 /* The USART Error Interrupts: (Frame error, noise error, overrun error)
1111 are not managed by the USART Transmit Process to avoid the overrun interrupt
1112 when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
1113 to benefit for the frame error and noise interrupts the usart mode should be
1114 configured only for transmit "USART_MODE_TX" */
1116 /* Configure Tx interrupt processing */
1117 if (husart
->FifoMode
== USART_FIFOMODE_ENABLE
)
1119 /* Set the Tx ISR function pointer according to the data word length */
1120 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1122 husart
->TxISR
= USART_TxISR_16BIT_FIFOEN
;
1126 husart
->TxISR
= USART_TxISR_8BIT_FIFOEN
;
1129 /* Process Unlocked */
1130 __HAL_UNLOCK(husart
);
1132 /* Enable the TX FIFO threshold interrupt */
1133 __HAL_USART_ENABLE_IT(husart
, USART_IT_TXFT
);
1137 /* Set the Tx ISR function pointer according to the data word length */
1138 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1140 husart
->TxISR
= USART_TxISR_16BIT
;
1144 husart
->TxISR
= USART_TxISR_8BIT
;
1147 /* Process Unlocked */
1148 __HAL_UNLOCK(husart
);
1150 /* Enable the USART Transmit Data Register Empty Interrupt */
1151 __HAL_USART_ENABLE_IT(husart
, USART_IT_TXE
);
1163 * @brief Receive an amount of data in interrupt mode.
1164 * @note To receive synchronous data, dummy data are simultaneously transmitted.
1165 * @param husart USART handle.
1166 * @param pRxData pointer to data buffer.
1167 * @param Size amount of data to be received.
1168 * @retval HAL status
1170 HAL_StatusTypeDef
HAL_USART_Receive_IT(USART_HandleTypeDef
*husart
, uint8_t *pRxData
, uint16_t Size
)
1172 uint16_t nb_dummy_data
;
1174 if (husart
->State
== HAL_USART_STATE_READY
)
1176 if ((pRxData
== NULL
) || (Size
== 0U))
1181 /* Process Locked */
1184 husart
->pRxBuffPtr
= pRxData
;
1185 husart
->RxXferSize
= Size
;
1186 husart
->RxXferCount
= Size
;
1187 husart
->RxISR
= NULL
;
1189 USART_MASK_COMPUTATION(husart
);
1191 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1192 husart
->State
= HAL_USART_STATE_BUSY_RX
;
1194 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1195 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1197 /* Configure Rx interrupt processing */
1198 if ((husart
->FifoMode
== USART_FIFOMODE_ENABLE
) && (Size
>= husart
->NbRxDataToProcess
))
1200 /* Set the Rx ISR function pointer according to the data word length */
1201 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1203 husart
->RxISR
= USART_RxISR_16BIT_FIFOEN
;
1207 husart
->RxISR
= USART_RxISR_8BIT_FIFOEN
;
1210 /* Process Unlocked */
1211 __HAL_UNLOCK(husart
);
1213 /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */
1214 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1215 SET_BIT(husart
->Instance
->CR3
, USART_CR3_RXFTIE
);
1219 /* Set the Rx ISR function pointer according to the data word length */
1220 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1222 husart
->RxISR
= USART_RxISR_16BIT
;
1226 husart
->RxISR
= USART_RxISR_8BIT
;
1229 /* Process Unlocked */
1230 __HAL_UNLOCK(husart
);
1232 /* Enable the USART Parity Error and Data Register not empty Interrupts */
1233 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1236 if (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
)
1238 /* Send dummy data in order to generate the clock for the Slave to send the next data.
1239 When FIFO mode is disabled only one data must be transferred.
1240 When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold.
1242 if ((husart
->FifoMode
== USART_FIFOMODE_ENABLE
) && (Size
>= husart
->NbRxDataToProcess
))
1244 for (nb_dummy_data
= husart
->NbRxDataToProcess
; nb_dummy_data
> 0U ; nb_dummy_data
--)
1246 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
1251 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
1264 * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
1265 * @param husart USART handle.
1266 * @param pTxData pointer to TX data buffer.
1267 * @param pRxData pointer to RX data buffer.
1268 * @param Size amount of data to be sent (same amount to be received).
1269 * @retval HAL status
1271 HAL_StatusTypeDef
HAL_USART_TransmitReceive_IT(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint8_t *pRxData
,
1275 if (husart
->State
== HAL_USART_STATE_READY
)
1277 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1282 /* Process Locked */
1285 husart
->pRxBuffPtr
= pRxData
;
1286 husart
->RxXferSize
= Size
;
1287 husart
->RxXferCount
= Size
;
1288 husart
->pTxBuffPtr
= pTxData
;
1289 husart
->TxXferSize
= Size
;
1290 husart
->TxXferCount
= Size
;
1292 /* Computation of USART mask to apply to RDR register */
1293 USART_MASK_COMPUTATION(husart
);
1295 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1296 husart
->State
= HAL_USART_STATE_BUSY_TX_RX
;
1298 /* Configure TxRx interrupt processing */
1299 if ((husart
->FifoMode
== USART_FIFOMODE_ENABLE
) && (Size
>= husart
->NbRxDataToProcess
))
1301 /* Set the Rx ISR function pointer according to the data word length */
1302 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1304 husart
->TxISR
= USART_TxISR_16BIT_FIFOEN
;
1305 husart
->RxISR
= USART_RxISR_16BIT_FIFOEN
;
1309 husart
->TxISR
= USART_TxISR_8BIT_FIFOEN
;
1310 husart
->RxISR
= USART_RxISR_8BIT_FIFOEN
;
1313 /* Process Locked */
1314 __HAL_UNLOCK(husart
);
1316 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1317 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1319 /* Enable the USART Parity Error interrupt */
1320 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1322 /* Enable the TX and RX FIFO Threshold interrupts */
1323 SET_BIT(husart
->Instance
->CR3
, (USART_CR3_TXFTIE
| USART_CR3_RXFTIE
));
1327 if ((husart
->Init
.WordLength
== USART_WORDLENGTH_9B
) && (husart
->Init
.Parity
== USART_PARITY_NONE
))
1329 husart
->TxISR
= USART_TxISR_16BIT
;
1330 husart
->RxISR
= USART_RxISR_16BIT
;
1334 husart
->TxISR
= USART_TxISR_8BIT
;
1335 husart
->RxISR
= USART_RxISR_8BIT
;
1338 /* Process Locked */
1339 __HAL_UNLOCK(husart
);
1341 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1342 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1344 /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
1345 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
| USART_CR1_RXNEIE_RXFNEIE
);
1347 /* Enable the USART Transmit Data Register Empty Interrupt */
1348 SET_BIT(husart
->Instance
->CR1
, USART_CR1_TXEIE_TXFNFIE
);
1360 * @brief Send an amount of data in DMA mode.
1361 * @param husart USART handle.
1362 * @param pTxData pointer to data buffer.
1363 * @param Size amount of data to be sent.
1364 * @retval HAL status
1366 HAL_StatusTypeDef
HAL_USART_Transmit_DMA(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint16_t Size
)
1368 HAL_StatusTypeDef status
= HAL_OK
;
1371 if (husart
->State
== HAL_USART_STATE_READY
)
1373 if ((pTxData
== NULL
) || (Size
== 0U))
1378 /* Process Locked */
1381 husart
->pTxBuffPtr
= pTxData
;
1382 husart
->TxXferSize
= Size
;
1383 husart
->TxXferCount
= Size
;
1385 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1386 husart
->State
= HAL_USART_STATE_BUSY_TX
;
1388 if (husart
->hdmatx
!= NULL
)
1390 /* Set the USART DMA transfer complete callback */
1391 husart
->hdmatx
->XferCpltCallback
= USART_DMATransmitCplt
;
1393 /* Set the USART DMA Half transfer complete callback */
1394 husart
->hdmatx
->XferHalfCpltCallback
= USART_DMATxHalfCplt
;
1396 /* Set the DMA error callback */
1397 husart
->hdmatx
->XferErrorCallback
= USART_DMAError
;
1399 /* Enable the USART transmit DMA channel */
1400 tmp
= (uint32_t *)&pTxData
;
1401 status
= HAL_DMA_Start_IT(husart
->hdmatx
, *(uint32_t *)tmp
, (uint32_t)&husart
->Instance
->TDR
, Size
);
1404 if (status
== HAL_OK
)
1406 /* Clear the TC flag in the ICR register */
1407 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_TCF
);
1409 /* Process Unlocked */
1410 __HAL_UNLOCK(husart
);
1412 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1413 in the USART CR3 register */
1414 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1420 /* Set error code to DMA */
1421 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1423 /* Process Unlocked */
1424 __HAL_UNLOCK(husart
);
1426 /* Restore husart->State to ready */
1427 husart
->State
= HAL_USART_STATE_READY
;
1439 * @brief Receive an amount of data in DMA mode.
1440 * @note When the USART parity is enabled (PCE = 1), the received data contain
1441 * the parity bit (MSB position).
1442 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
1443 * @param husart USART handle.
1444 * @param pRxData pointer to data buffer.
1445 * @param Size amount of data to be received.
1446 * @retval HAL status
1448 HAL_StatusTypeDef
HAL_USART_Receive_DMA(USART_HandleTypeDef
*husart
, uint8_t *pRxData
, uint16_t Size
)
1450 HAL_StatusTypeDef status
= HAL_OK
;
1451 uint32_t *tmp
= (uint32_t *)&pRxData
;
1453 /* Check that a Rx process is not already ongoing */
1454 if (husart
->State
== HAL_USART_STATE_READY
)
1456 if ((pRxData
== NULL
) || (Size
== 0U))
1461 /* Process Locked */
1464 husart
->pRxBuffPtr
= pRxData
;
1465 husart
->RxXferSize
= Size
;
1466 husart
->pTxBuffPtr
= pRxData
;
1467 husart
->TxXferSize
= Size
;
1469 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1470 husart
->State
= HAL_USART_STATE_BUSY_RX
;
1472 if (husart
->hdmarx
!= NULL
)
1474 /* Set the USART DMA Rx transfer complete callback */
1475 husart
->hdmarx
->XferCpltCallback
= USART_DMAReceiveCplt
;
1477 /* Set the USART DMA Half transfer complete callback */
1478 husart
->hdmarx
->XferHalfCpltCallback
= USART_DMARxHalfCplt
;
1480 /* Set the USART DMA Rx transfer error callback */
1481 husart
->hdmarx
->XferErrorCallback
= USART_DMAError
;
1483 /* Enable the USART receive DMA channel */
1484 status
= HAL_DMA_Start_IT(husart
->hdmarx
, (uint32_t)&husart
->Instance
->RDR
, *(uint32_t *)tmp
, Size
);
1487 if ((status
== HAL_OK
) &&
1488 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
1490 /* Enable the USART transmit DMA channel: the transmit channel is used in order
1491 to generate in the non-blocking mode the clock to the slave device,
1492 this mode isn't a simplex receive mode but a full-duplex receive mode */
1494 /* Set the USART DMA Tx Complete and Error callback to Null */
1495 if (husart
->hdmatx
!= NULL
)
1497 husart
->hdmatx
->XferErrorCallback
= NULL
;
1498 husart
->hdmatx
->XferHalfCpltCallback
= NULL
;
1499 husart
->hdmatx
->XferCpltCallback
= NULL
;
1500 status
= HAL_DMA_Start_IT(husart
->hdmatx
, *(uint32_t *)tmp
, (uint32_t)&husart
->Instance
->TDR
, Size
);
1504 if (status
== HAL_OK
)
1506 /* Process Unlocked */
1507 __HAL_UNLOCK(husart
);
1509 /* Enable the USART Parity Error Interrupt */
1510 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1512 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1513 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1515 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1516 in the USART CR3 register */
1517 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1519 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1520 in the USART CR3 register */
1521 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1527 if (husart
->hdmarx
!= NULL
)
1529 status
= HAL_DMA_Abort(husart
->hdmarx
);
1532 /* No need to check on error code */
1535 /* Set error code to DMA */
1536 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1538 /* Process Unlocked */
1539 __HAL_UNLOCK(husart
);
1541 /* Restore husart->State to ready */
1542 husart
->State
= HAL_USART_STATE_READY
;
1554 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
1555 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1556 * @param husart USART handle.
1557 * @param pTxData pointer to TX data buffer.
1558 * @param pRxData pointer to RX data buffer.
1559 * @param Size amount of data to be received/sent.
1560 * @retval HAL status
1562 HAL_StatusTypeDef
HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef
*husart
, uint8_t *pTxData
, uint8_t *pRxData
,
1565 HAL_StatusTypeDef status
;
1568 if (husart
->State
== HAL_USART_STATE_READY
)
1570 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1575 /* Process Locked */
1578 husart
->pRxBuffPtr
= pRxData
;
1579 husart
->RxXferSize
= Size
;
1580 husart
->pTxBuffPtr
= pTxData
;
1581 husart
->TxXferSize
= Size
;
1583 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1584 husart
->State
= HAL_USART_STATE_BUSY_TX_RX
;
1586 if ((husart
->hdmarx
!= NULL
) && (husart
->hdmatx
!= NULL
))
1588 /* Set the USART DMA Rx transfer complete callback */
1589 husart
->hdmarx
->XferCpltCallback
= USART_DMAReceiveCplt
;
1591 /* Set the USART DMA Half transfer complete callback */
1592 husart
->hdmarx
->XferHalfCpltCallback
= USART_DMARxHalfCplt
;
1594 /* Set the USART DMA Tx transfer complete callback */
1595 husart
->hdmatx
->XferCpltCallback
= USART_DMATransmitCplt
;
1597 /* Set the USART DMA Half transfer complete callback */
1598 husart
->hdmatx
->XferHalfCpltCallback
= USART_DMATxHalfCplt
;
1600 /* Set the USART DMA Tx transfer error callback */
1601 husart
->hdmatx
->XferErrorCallback
= USART_DMAError
;
1603 /* Set the USART DMA Rx transfer error callback */
1604 husart
->hdmarx
->XferErrorCallback
= USART_DMAError
;
1606 /* Enable the USART receive DMA channel */
1607 tmp
= (uint32_t *)&pRxData
;
1608 status
= HAL_DMA_Start_IT(husart
->hdmarx
, (uint32_t)&husart
->Instance
->RDR
, *(uint32_t *)tmp
, Size
);
1610 /* Enable the USART transmit DMA channel */
1611 if (status
== HAL_OK
)
1613 tmp
= (uint32_t *)&pTxData
;
1614 status
= HAL_DMA_Start_IT(husart
->hdmatx
, *(uint32_t *)tmp
, (uint32_t)&husart
->Instance
->TDR
, Size
);
1622 if (status
== HAL_OK
)
1624 /* Process Unlocked */
1625 __HAL_UNLOCK(husart
);
1627 /* Enable the USART Parity Error Interrupt */
1628 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1630 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1631 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1633 /* Clear the TC flag in the ICR register */
1634 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_TCF
);
1636 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1637 in the USART CR3 register */
1638 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1640 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1641 in the USART CR3 register */
1642 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1648 if (husart
->hdmarx
!= NULL
)
1650 status
= HAL_DMA_Abort(husart
->hdmarx
);
1653 /* No need to check on error code */
1656 /* Set error code to DMA */
1657 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1659 /* Process Unlocked */
1660 __HAL_UNLOCK(husart
);
1662 /* Restore husart->State to ready */
1663 husart
->State
= HAL_USART_STATE_READY
;
1675 * @brief Pause the DMA Transfer.
1676 * @param husart USART handle.
1677 * @retval HAL status
1679 HAL_StatusTypeDef
HAL_USART_DMAPause(USART_HandleTypeDef
*husart
)
1681 const HAL_USART_StateTypeDef state
= husart
->State
;
1683 /* Process Locked */
1686 if ((HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAT
)) &&
1687 (state
== HAL_USART_STATE_BUSY_TX
))
1689 /* Disable the USART DMA Tx request */
1690 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1692 else if ((state
== HAL_USART_STATE_BUSY_RX
) ||
1693 (state
== HAL_USART_STATE_BUSY_TX_RX
))
1695 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAT
))
1697 /* Disable the USART DMA Tx request */
1698 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1700 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
))
1702 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1703 CLEAR_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1704 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1706 /* Disable the USART DMA Rx request */
1707 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1715 /* Process Unlocked */
1716 __HAL_UNLOCK(husart
);
1722 * @brief Resume the DMA Transfer.
1723 * @param husart USART handle.
1724 * @retval HAL status
1726 HAL_StatusTypeDef
HAL_USART_DMAResume(USART_HandleTypeDef
*husart
)
1728 const HAL_USART_StateTypeDef state
= husart
->State
;
1730 /* Process Locked */
1733 if (state
== HAL_USART_STATE_BUSY_TX
)
1735 /* Enable the USART DMA Tx request */
1736 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1738 else if ((state
== HAL_USART_STATE_BUSY_RX
) ||
1739 (state
== HAL_USART_STATE_BUSY_TX_RX
))
1741 /* Clear the Overrun flag before resuming the Rx transfer*/
1742 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_OREF
);
1744 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1745 SET_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
1746 SET_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
1748 /* Enable the USART DMA Rx request before the DMA Tx request */
1749 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1751 /* Enable the USART DMA Tx request */
1752 SET_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1759 /* Process Unlocked */
1760 __HAL_UNLOCK(husart
);
1766 * @brief Stop the DMA Transfer.
1767 * @param husart USART handle.
1768 * @retval HAL status
1770 HAL_StatusTypeDef
HAL_USART_DMAStop(USART_HandleTypeDef
*husart
)
1772 /* The Lock is not implemented on this API to allow the user application
1773 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
1774 HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
1775 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1776 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1777 the stream and the corresponding call back is executed. */
1779 /* Disable the USART Tx/Rx DMA requests */
1780 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1781 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1783 /* Abort the USART DMA tx channel */
1784 if (husart
->hdmatx
!= NULL
)
1786 if (HAL_DMA_Abort(husart
->hdmatx
) != HAL_OK
)
1788 if (HAL_DMA_GetError(husart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1790 /* Set error code to DMA */
1791 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1797 /* Abort the USART DMA rx channel */
1798 if (husart
->hdmarx
!= NULL
)
1800 if (HAL_DMA_Abort(husart
->hdmarx
) != HAL_OK
)
1802 if (HAL_DMA_GetError(husart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1804 /* Set error code to DMA */
1805 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1812 USART_EndTransfer(husart
);
1813 husart
->State
= HAL_USART_STATE_READY
;
1819 * @brief Abort ongoing transfers (blocking mode).
1820 * @param husart USART handle.
1821 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1822 * This procedure performs following operations :
1823 * - Disable USART Interrupts (Tx and Rx)
1824 * - Disable the DMA transfer in the peripheral register (if enabled)
1825 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1826 * - Set handle State to READY
1827 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1828 * @retval HAL status
1830 HAL_StatusTypeDef
HAL_USART_Abort(USART_HandleTypeDef
*husart
)
1832 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1833 CLEAR_BIT(husart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
|
1835 CLEAR_BIT(husart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
1837 /* Disable the USART DMA Tx request if enabled */
1838 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAT
))
1840 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1842 /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
1843 if (husart
->hdmatx
!= NULL
)
1845 /* Set the USART DMA Abort callback to Null.
1846 No call back execution at end of DMA abort procedure */
1847 husart
->hdmatx
->XferAbortCallback
= NULL
;
1849 if (HAL_DMA_Abort(husart
->hdmatx
) != HAL_OK
)
1851 if (HAL_DMA_GetError(husart
->hdmatx
) == HAL_DMA_ERROR_TIMEOUT
)
1853 /* Set error code to DMA */
1854 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1862 /* Disable the USART DMA Rx request if enabled */
1863 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
))
1865 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1867 /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
1868 if (husart
->hdmarx
!= NULL
)
1870 /* Set the USART DMA Abort callback to Null.
1871 No call back execution at end of DMA abort procedure */
1872 husart
->hdmarx
->XferAbortCallback
= NULL
;
1874 if (HAL_DMA_Abort(husart
->hdmarx
) != HAL_OK
)
1876 if (HAL_DMA_GetError(husart
->hdmarx
) == HAL_DMA_ERROR_TIMEOUT
)
1878 /* Set error code to DMA */
1879 husart
->ErrorCode
= HAL_USART_ERROR_DMA
;
1887 /* Reset Tx and Rx transfer counters */
1888 husart
->TxXferCount
= 0U;
1889 husart
->RxXferCount
= 0U;
1891 /* Clear the Error flags in the ICR register */
1892 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_OREF
| USART_CLEAR_NEF
| USART_CLEAR_PEF
| USART_CLEAR_FEF
);
1894 /* Flush the whole TX FIFO (if needed) */
1895 if (husart
->FifoMode
== USART_FIFOMODE_ENABLE
)
1897 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
1900 /* Discard the received data */
1901 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
1903 /* Restore husart->State to Ready */
1904 husart
->State
= HAL_USART_STATE_READY
;
1906 /* Reset Handle ErrorCode to No Error */
1907 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
1913 * @brief Abort ongoing transfers (Interrupt mode).
1914 * @param husart USART handle.
1915 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1916 * This procedure performs following operations :
1917 * - Disable USART Interrupts (Tx and Rx)
1918 * - Disable the DMA transfer in the peripheral register (if enabled)
1919 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1920 * - Set handle State to READY
1921 * - At abort completion, call user abort complete callback
1922 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1923 * considered as completed only when user abort complete callback is executed (not when exiting function).
1924 * @retval HAL status
1926 HAL_StatusTypeDef
HAL_USART_Abort_IT(USART_HandleTypeDef
*husart
)
1928 uint32_t abortcplt
= 1U;
1930 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1931 CLEAR_BIT(husart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
|
1933 CLEAR_BIT(husart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
1935 /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
1936 before any call to DMA Abort functions */
1937 /* DMA Tx Handle is valid */
1938 if (husart
->hdmatx
!= NULL
)
1940 /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
1941 Otherwise, set it to NULL */
1942 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAT
))
1944 husart
->hdmatx
->XferAbortCallback
= USART_DMATxAbortCallback
;
1948 husart
->hdmatx
->XferAbortCallback
= NULL
;
1951 /* DMA Rx Handle is valid */
1952 if (husart
->hdmarx
!= NULL
)
1954 /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
1955 Otherwise, set it to NULL */
1956 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
))
1958 husart
->hdmarx
->XferAbortCallback
= USART_DMARxAbortCallback
;
1962 husart
->hdmarx
->XferAbortCallback
= NULL
;
1966 /* Disable the USART DMA Tx request if enabled */
1967 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAT
))
1969 /* Disable DMA Tx at USART level */
1970 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
1972 /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
1973 if (husart
->hdmatx
!= NULL
)
1975 /* USART Tx DMA Abort callback has already been initialised :
1976 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
1979 if (HAL_DMA_Abort_IT(husart
->hdmatx
) != HAL_OK
)
1981 husart
->hdmatx
->XferAbortCallback
= NULL
;
1990 /* Disable the USART DMA Rx request if enabled */
1991 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
))
1993 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
1995 /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
1996 if (husart
->hdmarx
!= NULL
)
1998 /* USART Rx DMA Abort callback has already been initialised :
1999 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
2002 if (HAL_DMA_Abort_IT(husart
->hdmarx
) != HAL_OK
)
2004 husart
->hdmarx
->XferAbortCallback
= NULL
;
2014 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
2015 if (abortcplt
== 1U)
2017 /* Reset Tx and Rx transfer counters */
2018 husart
->TxXferCount
= 0U;
2019 husart
->RxXferCount
= 0U;
2021 /* Reset errorCode */
2022 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
2024 /* Clear the Error flags in the ICR register */
2025 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_OREF
| USART_CLEAR_NEF
| USART_CLEAR_PEF
| USART_CLEAR_FEF
);
2027 /* Flush the whole TX FIFO (if needed) */
2028 if (husart
->FifoMode
== USART_FIFOMODE_ENABLE
)
2030 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
2033 /* Discard the received data */
2034 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
2036 /* Restore husart->State to Ready */
2037 husart
->State
= HAL_USART_STATE_READY
;
2039 /* As no DMA to be aborted, call directly user Abort complete callback */
2040 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2041 /* Call registered Abort Complete Callback */
2042 husart
->AbortCpltCallback(husart
);
2044 /* Call legacy weak Abort Complete Callback */
2045 HAL_USART_AbortCpltCallback(husart
);
2046 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2053 * @brief Handle USART interrupt request.
2054 * @param husart USART handle.
2057 void HAL_USART_IRQHandler(USART_HandleTypeDef
*husart
)
2059 uint32_t isrflags
= READ_REG(husart
->Instance
->ISR
);
2060 uint32_t cr1its
= READ_REG(husart
->Instance
->CR1
);
2061 uint32_t cr3its
= READ_REG(husart
->Instance
->CR3
);
2063 uint32_t errorflags
;
2066 /* If no error occurs */
2067 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
| USART_ISR_UDR
));
2068 if (errorflags
== 0U)
2070 /* USART in mode Receiver ---------------------------------------------------*/
2071 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
2072 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
2073 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
2075 if (husart
->RxISR
!= NULL
)
2077 husart
->RxISR(husart
);
2083 /* If some errors occur */
2084 if ((errorflags
!= 0U)
2085 && (((cr3its
& (USART_CR3_RXFTIE
| USART_CR3_EIE
)) != 0U)
2086 || ((cr1its
& (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
)) != 0U)))
2088 /* USART parity error interrupt occurred -------------------------------------*/
2089 if (((isrflags
& USART_ISR_PE
) != 0U) && ((cr1its
& USART_CR1_PEIE
) != 0U))
2091 __HAL_USART_CLEAR_IT(husart
, USART_CLEAR_PEF
);
2093 husart
->ErrorCode
|= HAL_USART_ERROR_PE
;
2096 /* USART frame error interrupt occurred --------------------------------------*/
2097 if (((isrflags
& USART_ISR_FE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2099 __HAL_USART_CLEAR_IT(husart
, USART_CLEAR_FEF
);
2101 husart
->ErrorCode
|= HAL_USART_ERROR_FE
;
2104 /* USART noise error interrupt occurred --------------------------------------*/
2105 if (((isrflags
& USART_ISR_NE
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2107 __HAL_USART_CLEAR_IT(husart
, USART_CLEAR_NEF
);
2109 husart
->ErrorCode
|= HAL_USART_ERROR_NE
;
2112 /* USART Over-Run interrupt occurred -----------------------------------------*/
2113 if (((isrflags
& USART_ISR_ORE
) != 0U)
2114 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U) ||
2115 ((cr3its
& (USART_CR3_RXFTIE
| USART_CR3_EIE
)) != 0U)))
2117 __HAL_USART_CLEAR_IT(husart
, USART_CLEAR_OREF
);
2119 husart
->ErrorCode
|= HAL_USART_ERROR_ORE
;
2122 /* USART SPI slave underrun error interrupt occurred -------------------------*/
2123 if (((isrflags
& USART_ISR_UDR
) != 0U) && ((cr3its
& USART_CR3_EIE
) != 0U))
2125 /* Ignore SPI slave underrun errors when reception is going on */
2126 if (husart
->State
== HAL_USART_STATE_BUSY_RX
)
2128 __HAL_USART_CLEAR_UDRFLAG(husart
);
2133 __HAL_USART_CLEAR_UDRFLAG(husart
);
2134 husart
->ErrorCode
|= HAL_USART_ERROR_UDR
;
2138 /* Call USART Error Call back function if need be --------------------------*/
2139 if (husart
->ErrorCode
!= HAL_USART_ERROR_NONE
)
2141 /* USART in mode Receiver ---------------------------------------------------*/
2142 if (((isrflags
& USART_ISR_RXNE_RXFNE
) != 0U)
2143 && (((cr1its
& USART_CR1_RXNEIE_RXFNEIE
) != 0U)
2144 || ((cr3its
& USART_CR3_RXFTIE
) != 0U)))
2146 if (husart
->RxISR
!= NULL
)
2148 husart
->RxISR(husart
);
2152 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2153 consider error as blocking */
2154 errorcode
= husart
->ErrorCode
& HAL_USART_ERROR_ORE
;
2155 if ((HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
)) ||
2158 /* Blocking error : transfer is aborted
2159 Set the USART state ready to be able to start again the process,
2160 Disable Interrupts, and disable DMA requests, if ongoing */
2161 USART_EndTransfer(husart
);
2163 /* Disable the USART DMA Rx request if enabled */
2164 if (HAL_IS_BIT_SET(husart
->Instance
->CR3
, USART_CR3_DMAR
))
2166 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
| USART_CR3_DMAR
);
2168 /* Abort the USART DMA Tx channel */
2169 if (husart
->hdmatx
!= NULL
)
2171 /* Set the USART Tx DMA Abort callback to NULL : no callback
2172 executed at end of DMA abort procedure */
2173 husart
->hdmatx
->XferAbortCallback
= NULL
;
2176 (void)HAL_DMA_Abort_IT(husart
->hdmatx
);
2179 /* Abort the USART DMA Rx channel */
2180 if (husart
->hdmarx
!= NULL
)
2182 /* Set the USART Rx DMA Abort callback :
2183 will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
2184 husart
->hdmarx
->XferAbortCallback
= USART_DMAAbortOnError
;
2187 if (HAL_DMA_Abort_IT(husart
->hdmarx
) != HAL_OK
)
2189 /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
2190 husart
->hdmarx
->XferAbortCallback(husart
->hdmarx
);
2195 /* Call user error callback */
2196 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2197 /* Call registered Error Callback */
2198 husart
->ErrorCallback(husart
);
2200 /* Call legacy weak Error Callback */
2201 HAL_USART_ErrorCallback(husart
);
2202 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2207 /* Call user error callback */
2208 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2209 /* Call registered Error Callback */
2210 husart
->ErrorCallback(husart
);
2212 /* Call legacy weak Error Callback */
2213 HAL_USART_ErrorCallback(husart
);
2214 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2219 /* Non Blocking error : transfer could go on.
2220 Error is notified to user through user error callback */
2221 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2222 /* Call registered Error Callback */
2223 husart
->ErrorCallback(husart
);
2225 /* Call legacy weak Error Callback */
2226 HAL_USART_ErrorCallback(husart
);
2227 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2228 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
2233 } /* End if some error occurs */
2236 /* USART in mode Transmitter ------------------------------------------------*/
2237 if (((isrflags
& USART_ISR_TXE_TXFNF
) != 0U)
2238 && (((cr1its
& USART_CR1_TXEIE_TXFNFIE
) != 0U)
2239 || ((cr3its
& USART_CR3_TXFTIE
) != 0U)))
2241 if (husart
->TxISR
!= NULL
)
2243 husart
->TxISR(husart
);
2248 /* USART in mode Transmitter (transmission end) -----------------------------*/
2249 if (((isrflags
& USART_ISR_TC
) != 0U) && ((cr1its
& USART_CR1_TCIE
) != 0U))
2251 USART_EndTransmit_IT(husart
);
2255 /* USART TX Fifo Empty occurred ----------------------------------------------*/
2256 if (((isrflags
& USART_ISR_TXFE
) != 0U) && ((cr1its
& USART_CR1_TXFEIE
) != 0U))
2258 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2259 /* Call registered Tx Fifo Empty Callback */
2260 husart
->TxFifoEmptyCallback(husart
);
2262 /* Call legacy weak Tx Fifo Empty Callback */
2263 HAL_USARTEx_TxFifoEmptyCallback(husart
);
2264 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2268 /* USART RX Fifo Full occurred ----------------------------------------------*/
2269 if (((isrflags
& USART_ISR_RXFF
) != 0U) && ((cr1its
& USART_CR1_RXFFIE
) != 0U))
2271 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2272 /* Call registered Rx Fifo Full Callback */
2273 husart
->RxFifoFullCallback(husart
);
2275 /* Call legacy weak Rx Fifo Full Callback */
2276 HAL_USARTEx_RxFifoFullCallback(husart
);
2277 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2283 * @brief Tx Transfer completed callback.
2284 * @param husart USART handle.
2287 __weak
void HAL_USART_TxCpltCallback(USART_HandleTypeDef
*husart
)
2289 /* Prevent unused argument(s) compilation warning */
2292 /* NOTE : This function should not be modified, when the callback is needed,
2293 the HAL_USART_TxCpltCallback can be implemented in the user file.
2298 * @brief Tx Half Transfer completed callback.
2299 * @param husart USART handle.
2302 __weak
void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef
*husart
)
2304 /* Prevent unused argument(s) compilation warning */
2307 /* NOTE: This function should not be modified, when the callback is needed,
2308 the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
2313 * @brief Rx Transfer completed callback.
2314 * @param husart USART handle.
2317 __weak
void HAL_USART_RxCpltCallback(USART_HandleTypeDef
*husart
)
2319 /* Prevent unused argument(s) compilation warning */
2322 /* NOTE: This function should not be modified, when the callback is needed,
2323 the HAL_USART_RxCpltCallback can be implemented in the user file.
2328 * @brief Rx Half Transfer completed callback.
2329 * @param husart USART handle.
2332 __weak
void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef
*husart
)
2334 /* Prevent unused argument(s) compilation warning */
2337 /* NOTE : This function should not be modified, when the callback is needed,
2338 the HAL_USART_RxHalfCpltCallback can be implemented in the user file
2343 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
2344 * @param husart USART handle.
2347 __weak
void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef
*husart
)
2349 /* Prevent unused argument(s) compilation warning */
2352 /* NOTE : This function should not be modified, when the callback is needed,
2353 the HAL_USART_TxRxCpltCallback can be implemented in the user file
2358 * @brief USART error callback.
2359 * @param husart USART handle.
2362 __weak
void HAL_USART_ErrorCallback(USART_HandleTypeDef
*husart
)
2364 /* Prevent unused argument(s) compilation warning */
2367 /* NOTE : This function should not be modified, when the callback is needed,
2368 the HAL_USART_ErrorCallback can be implemented in the user file.
2373 * @brief USART Abort Complete callback.
2374 * @param husart USART handle.
2377 __weak
void HAL_USART_AbortCpltCallback(USART_HandleTypeDef
*husart
)
2379 /* Prevent unused argument(s) compilation warning */
2382 /* NOTE : This function should not be modified, when the callback is needed,
2383 the HAL_USART_AbortCpltCallback can be implemented in the user file.
2391 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
2392 * @brief USART Peripheral State and Error functions
2395 ==============================================================================
2396 ##### Peripheral State and Error functions #####
2397 ==============================================================================
2399 This subsection provides functions allowing to :
2400 (+) Return the USART handle state
2401 (+) Return the USART handle error code
2409 * @brief Return the USART handle state.
2410 * @param husart pointer to a USART_HandleTypeDef structure that contains
2411 * the configuration information for the specified USART.
2412 * @retval USART handle state
2414 HAL_USART_StateTypeDef
HAL_USART_GetState(USART_HandleTypeDef
*husart
)
2416 return husart
->State
;
2420 * @brief Return the USART error code.
2421 * @param husart pointer to a USART_HandleTypeDef structure that contains
2422 * the configuration information for the specified USART.
2423 * @retval USART handle Error Code
2425 uint32_t HAL_USART_GetError(USART_HandleTypeDef
*husart
)
2427 return husart
->ErrorCode
;
2438 /** @defgroup USART_Private_Functions USART Private Functions
2443 * @brief Initialize the callbacks to their default values.
2444 * @param husart USART handle.
2447 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2448 void USART_InitCallbacksToDefault(USART_HandleTypeDef
*husart
)
2450 /* Init the USART Callback settings */
2451 husart
->TxHalfCpltCallback
= HAL_USART_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
2452 husart
->TxCpltCallback
= HAL_USART_TxCpltCallback
; /* Legacy weak TxCpltCallback */
2453 husart
->RxHalfCpltCallback
= HAL_USART_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
2454 husart
->RxCpltCallback
= HAL_USART_RxCpltCallback
; /* Legacy weak RxCpltCallback */
2455 husart
->TxRxCpltCallback
= HAL_USART_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
2456 husart
->ErrorCallback
= HAL_USART_ErrorCallback
; /* Legacy weak ErrorCallback */
2457 husart
->AbortCpltCallback
= HAL_USART_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
2458 husart
->RxFifoFullCallback
= HAL_USARTEx_RxFifoFullCallback
; /* Legacy weak RxFifoFullCallback */
2459 husart
->TxFifoEmptyCallback
= HAL_USARTEx_TxFifoEmptyCallback
; /* Legacy weak TxFifoEmptyCallback */
2461 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2464 * @brief End ongoing transfer on USART peripheral (following error detection or Transfer completion).
2465 * @param husart USART handle.
2468 static void USART_EndTransfer(USART_HandleTypeDef
*husart
)
2470 /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
2471 CLEAR_BIT(husart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE_TXFNFIE
|
2473 CLEAR_BIT(husart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
| USART_CR3_TXFTIE
));
2475 /* At end of process, restore husart->State to Ready */
2476 husart
->State
= HAL_USART_STATE_READY
;
2480 * @brief DMA USART transmit process complete callback.
2481 * @param hdma DMA handle.
2484 static void USART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2486 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2488 /* DMA Normal mode */
2489 if (HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
))
2491 husart
->TxXferCount
= 0U;
2493 if (husart
->State
== HAL_USART_STATE_BUSY_TX
)
2495 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2496 in the USART CR3 register */
2497 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
2499 /* Enable the USART Transmit Complete Interrupt */
2500 __HAL_USART_ENABLE_IT(husart
, USART_IT_TC
);
2503 /* DMA Circular mode */
2506 if (husart
->State
== HAL_USART_STATE_BUSY_TX
)
2508 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2509 /* Call registered Tx Complete Callback */
2510 husart
->TxCpltCallback(husart
);
2512 /* Call legacy weak Tx Complete Callback */
2513 HAL_USART_TxCpltCallback(husart
);
2514 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2520 * @brief DMA USART transmit process half complete callback.
2521 * @param hdma DMA handle.
2524 static void USART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
2526 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2528 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2529 /* Call registered Tx Half Complete Callback */
2530 husart
->TxHalfCpltCallback(husart
);
2532 /* Call legacy weak Tx Half Complete Callback */
2533 HAL_USART_TxHalfCpltCallback(husart
);
2534 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2538 * @brief DMA USART receive process complete callback.
2539 * @param hdma DMA handle.
2542 static void USART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2544 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2546 /* DMA Normal mode */
2547 if (HAL_IS_BIT_CLR(hdma
->Instance
->CCR
, DMA_CCR_CIRC
))
2549 husart
->RxXferCount
= 0U;
2551 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2552 CLEAR_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
2553 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
2555 /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
2556 in USART CR3 register */
2557 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAR
);
2558 /* similarly, disable the DMA TX transfer that was started to provide the
2559 clock to the slave device */
2560 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_DMAT
);
2562 if (husart
->State
== HAL_USART_STATE_BUSY_RX
)
2564 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2565 /* Call registered Rx Complete Callback */
2566 husart
->RxCpltCallback(husart
);
2568 /* Call legacy weak Rx Complete Callback */
2569 HAL_USART_RxCpltCallback(husart
);
2570 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2572 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2575 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2576 /* Call registered Tx Rx Complete Callback */
2577 husart
->TxRxCpltCallback(husart
);
2579 /* Call legacy weak Tx Rx Complete Callback */
2580 HAL_USART_TxRxCpltCallback(husart
);
2581 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2583 husart
->State
= HAL_USART_STATE_READY
;
2585 /* DMA circular mode */
2588 if (husart
->State
== HAL_USART_STATE_BUSY_RX
)
2590 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2591 /* Call registered Rx Complete Callback */
2592 husart
->RxCpltCallback(husart
);
2594 /* Call legacy weak Rx Complete Callback */
2595 HAL_USART_RxCpltCallback(husart
);
2596 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2598 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2601 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2602 /* Call registered Tx Rx Complete Callback */
2603 husart
->TxRxCpltCallback(husart
);
2605 /* Call legacy weak Tx Rx Complete Callback */
2606 HAL_USART_TxRxCpltCallback(husart
);
2607 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2613 * @brief DMA USART receive process half complete callback.
2614 * @param hdma DMA handle.
2617 static void USART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
2619 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2621 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2622 /* Call registered Rx Half Complete Callback */
2623 husart
->RxHalfCpltCallback(husart
);
2625 /* Call legacy weak Rx Half Complete Callback */
2626 HAL_USART_RxHalfCpltCallback(husart
);
2627 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2631 * @brief DMA USART communication error callback.
2632 * @param hdma DMA handle.
2635 static void USART_DMAError(DMA_HandleTypeDef
*hdma
)
2637 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2639 husart
->RxXferCount
= 0U;
2640 husart
->TxXferCount
= 0U;
2641 USART_EndTransfer(husart
);
2643 husart
->ErrorCode
|= HAL_USART_ERROR_DMA
;
2644 husart
->State
= HAL_USART_STATE_READY
;
2646 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2647 /* Call registered Error Callback */
2648 husart
->ErrorCallback(husart
);
2650 /* Call legacy weak Error Callback */
2651 HAL_USART_ErrorCallback(husart
);
2652 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2656 * @brief DMA USART communication abort callback, when initiated by HAL services on Error
2657 * (To be called at end of DMA Abort procedure following error occurrence).
2658 * @param hdma DMA handle.
2661 static void USART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
2663 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2664 husart
->RxXferCount
= 0U;
2665 husart
->TxXferCount
= 0U;
2667 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2668 /* Call registered Error Callback */
2669 husart
->ErrorCallback(husart
);
2671 /* Call legacy weak Error Callback */
2672 HAL_USART_ErrorCallback(husart
);
2673 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2677 * @brief DMA USART Tx communication abort callback, when initiated by user
2678 * (To be called at end of DMA Tx Abort procedure following user abort request).
2679 * @note When this callback is executed, User Abort complete call back is called only if no
2680 * Abort still ongoing for Rx DMA Handle.
2681 * @param hdma DMA handle.
2684 static void USART_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
2686 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2688 husart
->hdmatx
->XferAbortCallback
= NULL
;
2690 /* Check if an Abort process is still ongoing */
2691 if (husart
->hdmarx
!= NULL
)
2693 if (husart
->hdmarx
->XferAbortCallback
!= NULL
)
2699 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2700 husart
->TxXferCount
= 0U;
2701 husart
->RxXferCount
= 0U;
2703 /* Reset errorCode */
2704 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
2706 /* Clear the Error flags in the ICR register */
2707 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_OREF
| USART_CLEAR_NEF
| USART_CLEAR_PEF
| USART_CLEAR_FEF
);
2709 /* Restore husart->State to Ready */
2710 husart
->State
= HAL_USART_STATE_READY
;
2712 /* Call user Abort complete callback */
2713 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2714 /* Call registered Abort Complete Callback */
2715 husart
->AbortCpltCallback(husart
);
2717 /* Call legacy weak Abort Complete Callback */
2718 HAL_USART_AbortCpltCallback(husart
);
2719 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2725 * @brief DMA USART Rx communication abort callback, when initiated by user
2726 * (To be called at end of DMA Rx Abort procedure following user abort request).
2727 * @note When this callback is executed, User Abort complete call back is called only if no
2728 * Abort still ongoing for Tx DMA Handle.
2729 * @param hdma DMA handle.
2732 static void USART_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
2734 USART_HandleTypeDef
*husart
= (USART_HandleTypeDef
*)(hdma
->Parent
);
2736 husart
->hdmarx
->XferAbortCallback
= NULL
;
2738 /* Check if an Abort process is still ongoing */
2739 if (husart
->hdmatx
!= NULL
)
2741 if (husart
->hdmatx
->XferAbortCallback
!= NULL
)
2747 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2748 husart
->TxXferCount
= 0U;
2749 husart
->RxXferCount
= 0U;
2751 /* Reset errorCode */
2752 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
2754 /* Clear the Error flags in the ICR register */
2755 __HAL_USART_CLEAR_FLAG(husart
, USART_CLEAR_OREF
| USART_CLEAR_NEF
| USART_CLEAR_PEF
| USART_CLEAR_FEF
);
2757 /* Restore husart->State to Ready */
2758 husart
->State
= HAL_USART_STATE_READY
;
2760 /* Call user Abort complete callback */
2761 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2762 /* Call registered Abort Complete Callback */
2763 husart
->AbortCpltCallback(husart
);
2765 /* Call legacy weak Abort Complete Callback */
2766 HAL_USART_AbortCpltCallback(husart
);
2767 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2772 * @brief Handle USART Communication Timeout.
2773 * @param husart USART handle.
2774 * @param Flag Specifies the USART flag to check.
2775 * @param Status the Flag status (SET or RESET).
2776 * @param Tickstart Tick start value
2777 * @param Timeout timeout duration.
2778 * @retval HAL status
2780 static HAL_StatusTypeDef
USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef
*husart
, uint32_t Flag
, FlagStatus Status
,
2781 uint32_t Tickstart
, uint32_t Timeout
)
2783 /* Wait until flag is set */
2784 while ((__HAL_USART_GET_FLAG(husart
, Flag
) ? SET
: RESET
) == Status
)
2786 /* Check for the Timeout */
2787 if (Timeout
!= HAL_MAX_DELAY
)
2789 if (((HAL_GetTick() - Tickstart
) > Timeout
) || (Timeout
== 0U))
2791 husart
->State
= HAL_USART_STATE_READY
;
2793 /* Process Unlocked */
2794 __HAL_UNLOCK(husart
);
2804 * @brief Configure the USART peripheral.
2805 * @param husart USART handle.
2806 * @retval HAL status
2808 static HAL_StatusTypeDef
USART_SetConfig(USART_HandleTypeDef
*husart
)
2811 USART_ClockSourceTypeDef clocksource
;
2812 HAL_StatusTypeDef ret
= HAL_OK
;
2814 uint32_t usartdiv
= 0x00000000;
2816 /* Check the parameters */
2817 assert_param(IS_USART_POLARITY(husart
->Init
.CLKPolarity
));
2818 assert_param(IS_USART_PHASE(husart
->Init
.CLKPhase
));
2819 assert_param(IS_USART_LASTBIT(husart
->Init
.CLKLastBit
));
2820 assert_param(IS_USART_BAUDRATE(husart
->Init
.BaudRate
));
2821 assert_param(IS_USART_WORD_LENGTH(husart
->Init
.WordLength
));
2822 assert_param(IS_USART_STOPBITS(husart
->Init
.StopBits
));
2823 assert_param(IS_USART_PARITY(husart
->Init
.Parity
));
2824 assert_param(IS_USART_MODE(husart
->Init
.Mode
));
2825 assert_param(IS_USART_PRESCALER(husart
->Init
.ClockPrescaler
));
2827 /*-------------------------- USART CR1 Configuration -----------------------*/
2828 /* Clear M, PCE, PS, TE and RE bits and configure
2829 * the USART Word Length, Parity and Mode:
2830 * set the M bits according to husart->Init.WordLength value
2831 * set PCE and PS bits according to husart->Init.Parity value
2832 * set TE and RE bits according to husart->Init.Mode value
2833 * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
2834 tmpreg
= (uint32_t)husart
->Init
.WordLength
| husart
->Init
.Parity
| husart
->Init
.Mode
| USART_CR1_OVER8
;
2835 MODIFY_REG(husart
->Instance
->CR1
, USART_CR1_FIELDS
, tmpreg
);
2837 /*---------------------------- USART CR2 Configuration ---------------------*/
2838 /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits:
2839 * set CPOL bit according to husart->Init.CLKPolarity value
2840 * set CPHA bit according to husart->Init.CLKPhase value
2841 * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
2842 * set STOP[13:12] bits according to husart->Init.StopBits value */
2843 tmpreg
= (uint32_t)(USART_CLOCK_ENABLE
);
2844 tmpreg
|= (uint32_t)husart
->Init
.CLKLastBit
;
2845 tmpreg
|= ((uint32_t)husart
->Init
.CLKPolarity
| (uint32_t)husart
->Init
.CLKPhase
);
2846 tmpreg
|= (uint32_t)husart
->Init
.StopBits
;
2847 MODIFY_REG(husart
->Instance
->CR2
, USART_CR2_FIELDS
, tmpreg
);
2849 /*-------------------------- USART PRESC Configuration -----------------------*/
2851 * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */
2852 MODIFY_REG(husart
->Instance
->PRESC
, USART_PRESC_PRESCALER
, husart
->Init
.ClockPrescaler
);
2854 /*-------------------------- USART BRR Configuration -----------------------*/
2855 /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
2856 USART_GETCLOCKSOURCE(husart
, clocksource
);
2858 switch (clocksource
)
2860 case USART_CLOCKSOURCE_PCLK1
:
2861 usartdiv
= (uint32_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), husart
->Init
.BaudRate
, husart
->Init
.ClockPrescaler
));
2863 case USART_CLOCKSOURCE_PCLK2
:
2864 usartdiv
= (uint32_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), husart
->Init
.BaudRate
, husart
->Init
.ClockPrescaler
));
2866 case USART_CLOCKSOURCE_HSI
:
2867 usartdiv
= (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE
, husart
->Init
.BaudRate
, husart
->Init
.ClockPrescaler
));
2869 case USART_CLOCKSOURCE_SYSCLK
:
2870 usartdiv
= (uint32_t)(USART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), husart
->Init
.BaudRate
, husart
->Init
.ClockPrescaler
));
2872 case USART_CLOCKSOURCE_LSE
:
2873 usartdiv
= (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE
, husart
->Init
.BaudRate
, husart
->Init
.ClockPrescaler
));
2880 /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */
2881 if ((usartdiv
>= USART_BRR_MIN
) && (usartdiv
<= USART_BRR_MAX
))
2883 brrtemp
= (uint16_t)(usartdiv
& 0xFFF0U
);
2884 brrtemp
|= (uint16_t)((usartdiv
& (uint16_t)0x000FU
) >> 1U);
2885 husart
->Instance
->BRR
= brrtemp
;
2892 /* Initialize the number of data to process during RX/TX ISR execution */
2893 husart
->NbTxDataToProcess
= 1U;
2894 husart
->NbRxDataToProcess
= 1U;
2896 /* Clear ISR function pointers */
2897 husart
->RxISR
= NULL
;
2898 husart
->TxISR
= NULL
;
2904 * @brief Check the USART Idle State.
2905 * @param husart USART handle.
2906 * @retval HAL status
2908 static HAL_StatusTypeDef
USART_CheckIdleState(USART_HandleTypeDef
*husart
)
2912 /* Initialize the USART ErrorCode */
2913 husart
->ErrorCode
= HAL_USART_ERROR_NONE
;
2915 /* Init tickstart for timeout managment*/
2916 tickstart
= HAL_GetTick();
2918 /* Check if the Transmitter is enabled */
2919 if ((husart
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2921 /* Wait until TEACK flag is set */
2922 if (USART_WaitOnFlagUntilTimeout(husart
, USART_ISR_TEACK
, RESET
, tickstart
, USART_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2924 /* Timeout occurred */
2928 /* Check if the Receiver is enabled */
2929 if ((husart
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2931 /* Wait until REACK flag is set */
2932 if (USART_WaitOnFlagUntilTimeout(husart
, USART_ISR_REACK
, RESET
, tickstart
, USART_TEACK_REACK_TIMEOUT
) != HAL_OK
)
2934 /* Timeout occurred */
2939 /* Initialize the USART state*/
2940 husart
->State
= HAL_USART_STATE_READY
;
2942 /* Process Unlocked */
2943 __HAL_UNLOCK(husart
);
2949 * @brief Simplex send an amount of data in non-blocking mode.
2950 * @note Function called under interruption only, once
2951 * interruptions have been enabled by HAL_USART_Transmit_IT().
2952 * @note The USART errors are not managed to avoid the overrun error.
2953 * @note ISR function executed when FIFO mode is disabled and when the
2954 * data word length is less than 9 bits long.
2955 * @param husart USART handle.
2958 static void USART_TxISR_8BIT(USART_HandleTypeDef
*husart
)
2960 const HAL_USART_StateTypeDef state
= husart
->State
;
2962 /* Check that a Tx process is ongoing */
2963 if ((state
== HAL_USART_STATE_BUSY_TX
) ||
2964 (state
== HAL_USART_STATE_BUSY_TX_RX
))
2966 if (husart
->TxXferCount
== 0U)
2968 /* Disable the USART Transmit data register empty interrupt */
2969 __HAL_USART_DISABLE_IT(husart
, USART_IT_TXE
);
2971 /* Enable the USART Transmit Complete Interrupt */
2972 __HAL_USART_ENABLE_IT(husart
, USART_IT_TC
);
2976 husart
->Instance
->TDR
= (uint8_t)(*husart
->pTxBuffPtr
& (uint8_t)0xFF);
2977 husart
->pTxBuffPtr
++;
2978 husart
->TxXferCount
--;
2984 * @brief Simplex send an amount of data in non-blocking mode.
2985 * @note Function called under interruption only, once
2986 * interruptions have been enabled by HAL_USART_Transmit_IT().
2987 * @note The USART errors are not managed to avoid the overrun error.
2988 * @note ISR function executed when FIFO mode is disabled and when the
2989 * data word length is 9 bits long.
2990 * @param husart USART handle.
2993 static void USART_TxISR_16BIT(USART_HandleTypeDef
*husart
)
2995 const HAL_USART_StateTypeDef state
= husart
->State
;
2998 if ((state
== HAL_USART_STATE_BUSY_TX
) ||
2999 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3001 if (husart
->TxXferCount
== 0U)
3003 /* Disable the USART Transmit data register empty interrupt */
3004 __HAL_USART_DISABLE_IT(husart
, USART_IT_TXE
);
3006 /* Enable the USART Transmit Complete Interrupt */
3007 __HAL_USART_ENABLE_IT(husart
, USART_IT_TC
);
3011 tmp
= (uint16_t *) husart
->pTxBuffPtr
;
3012 husart
->Instance
->TDR
= (uint16_t)(*tmp
& 0x01FFU
);
3013 husart
->pTxBuffPtr
+= 2U;
3014 husart
->TxXferCount
--;
3020 * @brief Simplex send an amount of data in non-blocking mode.
3021 * @note Function called under interruption only, once
3022 * interruptions have been enabled by HAL_USART_Transmit_IT().
3023 * @note The USART errors are not managed to avoid the overrun error.
3024 * @note ISR function executed when FIFO mode is enabled and when the
3025 * data word length is less than 9 bits long.
3026 * @param husart USART handle.
3029 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef
*husart
)
3031 const HAL_USART_StateTypeDef state
= husart
->State
;
3032 uint16_t nb_tx_data
;
3034 /* Check that a Tx process is ongoing */
3035 if ((state
== HAL_USART_STATE_BUSY_TX
) ||
3036 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3038 for (nb_tx_data
= husart
->NbTxDataToProcess
; nb_tx_data
> 0U ; nb_tx_data
--)
3040 if (husart
->TxXferCount
== 0U)
3042 /* Disable the TX FIFO threshold interrupt */
3043 __HAL_USART_DISABLE_IT(husart
, USART_IT_TXFT
);
3045 /* Enable the USART Transmit Complete Interrupt */
3046 __HAL_USART_ENABLE_IT(husart
, USART_IT_TC
);
3048 break; /* force exit loop */
3050 else if (__HAL_USART_GET_FLAG(husart
, USART_FLAG_TXFNF
) == SET
)
3052 husart
->Instance
->TDR
= (uint8_t)(*husart
->pTxBuffPtr
& (uint8_t)0xFF);
3053 husart
->pTxBuffPtr
++;
3054 husart
->TxXferCount
--;
3065 * @brief Simplex send an amount of data in non-blocking mode.
3066 * @note Function called under interruption only, once
3067 * interruptions have been enabled by HAL_USART_Transmit_IT().
3068 * @note The USART errors are not managed to avoid the overrun error.
3069 * @note ISR function executed when FIFO mode is enabled and when the
3070 * data word length is 9 bits long.
3071 * @param husart USART handle.
3074 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef
*husart
)
3076 const HAL_USART_StateTypeDef state
= husart
->State
;
3078 uint16_t nb_tx_data
;
3080 /* Check that a Tx process is ongoing */
3081 if ((state
== HAL_USART_STATE_BUSY_TX
) ||
3082 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3084 for (nb_tx_data
= husart
->NbTxDataToProcess
; nb_tx_data
> 0U ; nb_tx_data
--)
3086 if (husart
->TxXferCount
== 0U)
3088 /* Disable the TX FIFO threshold interrupt */
3089 __HAL_USART_DISABLE_IT(husart
, USART_IT_TXFT
);
3091 /* Enable the USART Transmit Complete Interrupt */
3092 __HAL_USART_ENABLE_IT(husart
, USART_IT_TC
);
3094 break; /* force exit loop */
3096 else if (__HAL_USART_GET_FLAG(husart
, USART_FLAG_TXFNF
) == SET
)
3098 tmp
= (uint16_t *) husart
->pTxBuffPtr
;
3099 husart
->Instance
->TDR
= (uint16_t)(*tmp
& 0x01FFU
);
3100 husart
->pTxBuffPtr
+= 2U;
3101 husart
->TxXferCount
--;
3112 * @brief Wraps up transmission in non-blocking mode.
3113 * @param husart Pointer to a USART_HandleTypeDef structure that contains
3114 * the configuration information for the specified USART module.
3117 static void USART_EndTransmit_IT(USART_HandleTypeDef
*husart
)
3119 /* Disable the USART Transmit Complete Interrupt */
3120 __HAL_USART_DISABLE_IT(husart
, USART_IT_TC
);
3122 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3123 __HAL_USART_DISABLE_IT(husart
, USART_IT_ERR
);
3125 /* Clear TxISR function pointer */
3126 husart
->TxISR
= NULL
;
3128 if (husart
->State
== HAL_USART_STATE_BUSY_TX
)
3130 /* Clear overrun flag and discard the received data */
3131 __HAL_USART_CLEAR_OREFLAG(husart
);
3132 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
3134 /* Tx process is completed, restore husart->State to Ready */
3135 husart
->State
= HAL_USART_STATE_READY
;
3137 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3138 /* Call registered Tx Complete Callback */
3139 husart
->TxCpltCallback(husart
);
3141 /* Call legacy weak Tx Complete Callback */
3142 HAL_USART_TxCpltCallback(husart
);
3143 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3145 else if (husart
->RxXferCount
== 0U)
3147 /* TxRx process is completed, restore husart->State to Ready */
3148 husart
->State
= HAL_USART_STATE_READY
;
3150 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3151 /* Call registered Tx Rx Complete Callback */
3152 husart
->TxRxCpltCallback(husart
);
3154 /* Call legacy weak Tx Rx Complete Callback */
3155 HAL_USART_TxRxCpltCallback(husart
);
3156 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3166 * @brief Simplex receive an amount of data in non-blocking mode.
3167 * @note Function called under interruption only, once
3168 * interruptions have been enabled by HAL_USART_Receive_IT().
3169 * @note ISR function executed when FIFO mode is disabled and when the
3170 * data word length is less than 9 bits long.
3171 * @param husart USART handle
3174 static void USART_RxISR_8BIT(USART_HandleTypeDef
*husart
)
3176 const HAL_USART_StateTypeDef state
= husart
->State
;
3177 uint16_t txdatacount
;
3178 uint16_t uhMask
= husart
->Mask
;
3181 if ((state
== HAL_USART_STATE_BUSY_RX
) ||
3182 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3184 *husart
->pRxBuffPtr
= (uint8_t)(husart
->Instance
->RDR
& (uint8_t)uhMask
);
3185 husart
->pRxBuffPtr
++;
3186 husart
->RxXferCount
--;
3188 if (husart
->RxXferCount
== 0U)
3190 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3191 CLEAR_BIT(husart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
3193 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3194 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
3196 /* Clear RxISR function pointer */
3197 husart
->RxISR
= NULL
;
3199 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3200 txftie
= READ_BIT(husart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3201 txdatacount
= husart
->TxXferCount
;
3203 if (state
== HAL_USART_STATE_BUSY_RX
)
3205 /* Clear SPI slave underrun flag and discard transmit data */
3206 if (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
)
3208 __HAL_USART_CLEAR_UDRFLAG(husart
);
3209 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
3212 /* Rx process is completed, restore husart->State to Ready */
3213 husart
->State
= HAL_USART_STATE_READY
;
3215 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3216 /* Call registered Rx Complete Callback */
3217 husart
->RxCpltCallback(husart
);
3219 /* Call legacy weak Rx Complete Callback */
3220 HAL_USART_RxCpltCallback(husart
);
3221 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3223 else if ((READ_BIT(husart
->Instance
->CR1
, USART_CR1_TCIE
) != USART_CR1_TCIE
) &&
3224 (txftie
!= USART_CR3_TXFTIE
) &&
3225 (txdatacount
== 0U))
3227 /* TxRx process is completed, restore husart->State to Ready */
3228 husart
->State
= HAL_USART_STATE_READY
;
3230 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3231 /* Call registered Tx Rx Complete Callback */
3232 husart
->TxRxCpltCallback(husart
);
3234 /* Call legacy weak Tx Rx Complete Callback */
3235 HAL_USART_TxRxCpltCallback(husart
);
3236 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3243 else if ((state
== HAL_USART_STATE_BUSY_RX
) &&
3244 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3246 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3247 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3257 * @brief Simplex receive an amount of data in non-blocking mode.
3258 * @note Function called under interruption only, once
3259 * interruptions have been enabled by HAL_USART_Receive_IT().
3260 * @note ISR function executed when FIFO mode is disabled and when the
3261 * data word length is 9 bits long.
3262 * @param husart USART handle
3265 static void USART_RxISR_16BIT(USART_HandleTypeDef
*husart
)
3267 const HAL_USART_StateTypeDef state
= husart
->State
;
3268 uint16_t txdatacount
;
3270 uint16_t uhMask
= husart
->Mask
;
3273 if ((state
== HAL_USART_STATE_BUSY_RX
) ||
3274 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3276 tmp
= (uint16_t *) husart
->pRxBuffPtr
;
3277 *tmp
= (uint16_t)(husart
->Instance
->RDR
& uhMask
);
3278 husart
->pRxBuffPtr
+= 2U;
3279 husart
->RxXferCount
--;
3281 if (husart
->RxXferCount
== 0U)
3283 /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3284 CLEAR_BIT(husart
->Instance
->CR1
, (USART_CR1_RXNEIE_RXFNEIE
| USART_CR1_PEIE
));
3286 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3287 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_EIE
);
3289 /* Clear RxISR function pointer */
3290 husart
->RxISR
= NULL
;
3292 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3293 txftie
= READ_BIT(husart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3294 txdatacount
= husart
->TxXferCount
;
3296 if (state
== HAL_USART_STATE_BUSY_RX
)
3298 /* Clear SPI slave underrun flag and discard transmit data */
3299 if (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
)
3301 __HAL_USART_CLEAR_UDRFLAG(husart
);
3302 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
3305 /* Rx process is completed, restore husart->State to Ready */
3306 husart
->State
= HAL_USART_STATE_READY
;
3308 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3309 /* Call registered Rx Complete Callback */
3310 husart
->RxCpltCallback(husart
);
3312 /* Call legacy weak Rx Complete Callback */
3313 HAL_USART_RxCpltCallback(husart
);
3314 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3316 else if ((READ_BIT(husart
->Instance
->CR1
, USART_CR1_TCIE
) != USART_CR1_TCIE
) &&
3317 (txftie
!= USART_CR3_TXFTIE
) &&
3318 (txdatacount
== 0U))
3320 /* TxRx process is completed, restore husart->State to Ready */
3321 husart
->State
= HAL_USART_STATE_READY
;
3323 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3324 /* Call registered Tx Rx Complete Callback */
3325 husart
->TxRxCpltCallback(husart
);
3327 /* Call legacy weak Tx Rx Complete Callback */
3328 HAL_USART_TxRxCpltCallback(husart
);
3329 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3336 else if ((state
== HAL_USART_STATE_BUSY_RX
) &&
3337 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3339 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3340 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3350 * @brief Simplex receive an amount of data in non-blocking mode.
3351 * @note Function called under interruption only, once
3352 * interruptions have been enabled by HAL_USART_Receive_IT().
3353 * @note ISR function executed when FIFO mode is enabled and when the
3354 * data word length is less than 9 bits long.
3355 * @param husart USART handle
3358 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef
*husart
)
3360 HAL_USART_StateTypeDef state
= husart
->State
;
3361 uint16_t txdatacount
;
3362 uint16_t rxdatacount
;
3363 uint16_t uhMask
= husart
->Mask
;
3364 uint16_t nb_rx_data
;
3367 /* Check that a Rx process is ongoing */
3368 if ((state
== HAL_USART_STATE_BUSY_RX
) ||
3369 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3371 for (nb_rx_data
= husart
->NbRxDataToProcess
; nb_rx_data
> 0U ; nb_rx_data
--)
3373 if (__HAL_USART_GET_FLAG(husart
, USART_FLAG_RXFNE
) == SET
)
3375 *husart
->pRxBuffPtr
= (uint8_t)(husart
->Instance
->RDR
& (uint8_t)(uhMask
& 0xFFU
));
3376 husart
->pRxBuffPtr
++;
3377 husart
->RxXferCount
--;
3379 if (husart
->RxXferCount
== 0U)
3381 /* Disable the USART Parity Error Interrupt */
3382 CLEAR_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
3384 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3385 CLEAR_BIT(husart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
3387 /* Clear RxISR function pointer */
3388 husart
->RxISR
= NULL
;
3390 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3391 txftie
= READ_BIT(husart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3392 txdatacount
= husart
->TxXferCount
;
3394 if (state
== HAL_USART_STATE_BUSY_RX
)
3396 /* Clear SPI slave underrun flag and discard transmit data */
3397 if (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
)
3399 __HAL_USART_CLEAR_UDRFLAG(husart
);
3400 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
3403 /* Rx process is completed, restore husart->State to Ready */
3404 husart
->State
= HAL_USART_STATE_READY
;
3405 state
= HAL_USART_STATE_READY
;
3407 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3408 /* Call registered Rx Complete Callback */
3409 husart
->RxCpltCallback(husart
);
3411 /* Call legacy weak Rx Complete Callback */
3412 HAL_USART_RxCpltCallback(husart
);
3413 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3415 else if ((READ_BIT(husart
->Instance
->CR1
, USART_CR1_TCIE
) != USART_CR1_TCIE
) &&
3416 (txftie
!= USART_CR3_TXFTIE
) &&
3417 (txdatacount
== 0U))
3419 /* TxRx process is completed, restore husart->State to Ready */
3420 husart
->State
= HAL_USART_STATE_READY
;
3421 state
= HAL_USART_STATE_READY
;
3423 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3424 /* Call registered Tx Rx Complete Callback */
3425 husart
->TxRxCpltCallback(husart
);
3427 /* Call legacy weak Tx Rx Complete Callback */
3428 HAL_USART_TxRxCpltCallback(husart
);
3429 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3436 else if ((state
== HAL_USART_STATE_BUSY_RX
) &&
3437 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3439 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3440 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3449 /* When remaining number of bytes to receive is less than the RX FIFO
3450 threshold, next incoming frames are processed as if FIFO mode was
3451 disabled (i.e. one interrupt per received frame).
3453 rxdatacount
= husart
->RxXferCount
;
3454 if (((rxdatacount
!= 0U)) && (rxdatacount
< husart
->NbRxDataToProcess
))
3456 /* Disable the USART RXFT interrupt*/
3457 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_RXFTIE
);
3459 /* Update the RxISR function pointer */
3460 husart
->RxISR
= USART_RxISR_8BIT
;
3462 /* Enable the USART Data Register Not Empty interrupt */
3463 SET_BIT(husart
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
3465 if ((husart
->TxXferCount
== 0U) &&
3466 (state
== HAL_USART_STATE_BUSY_TX_RX
) &&
3467 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3469 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3470 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3476 /* Clear RXNE interrupt flag */
3477 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
3482 * @brief Simplex receive an amount of data in non-blocking mode.
3483 * @note Function called under interruption only, once
3484 * interruptions have been enabled by HAL_USART_Receive_IT().
3485 * @note ISR function executed when FIFO mode is enabled and when the
3486 * data word length is 9 bits long.
3487 * @param husart USART handle
3490 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef
*husart
)
3492 HAL_USART_StateTypeDef state
= husart
->State
;
3493 uint16_t txdatacount
;
3494 uint16_t rxdatacount
;
3496 uint16_t uhMask
= husart
->Mask
;
3497 uint16_t nb_rx_data
;
3500 /* Check that a Tx process is ongoing */
3501 if ((state
== HAL_USART_STATE_BUSY_RX
) ||
3502 (state
== HAL_USART_STATE_BUSY_TX_RX
))
3504 for (nb_rx_data
= husart
->NbRxDataToProcess
; nb_rx_data
> 0U ; nb_rx_data
--)
3506 if (__HAL_USART_GET_FLAG(husart
, USART_FLAG_RXFNE
) == SET
)
3508 tmp
= (uint16_t *) husart
->pRxBuffPtr
;
3509 *tmp
= (uint16_t)(husart
->Instance
->RDR
& uhMask
);
3510 husart
->pRxBuffPtr
+= 2U;
3511 husart
->RxXferCount
--;
3513 if (husart
->RxXferCount
== 0U)
3515 /* Disable the USART Parity Error Interrupt */
3516 CLEAR_BIT(husart
->Instance
->CR1
, USART_CR1_PEIE
);
3518 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
3519 CLEAR_BIT(husart
->Instance
->CR3
, (USART_CR3_EIE
| USART_CR3_RXFTIE
));
3521 /* Clear RxISR function pointer */
3522 husart
->RxISR
= NULL
;
3524 /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3525 txftie
= READ_BIT(husart
->Instance
->CR3
, USART_CR3_TXFTIE
);
3526 txdatacount
= husart
->TxXferCount
;
3528 if (state
== HAL_USART_STATE_BUSY_RX
)
3530 /* Clear SPI slave underrun flag and discard transmit data */
3531 if (husart
->SlaveMode
== USART_SLAVEMODE_ENABLE
)
3533 __HAL_USART_CLEAR_UDRFLAG(husart
);
3534 __HAL_USART_SEND_REQ(husart
, USART_TXDATA_FLUSH_REQUEST
);
3537 /* Rx process is completed, restore husart->State to Ready */
3538 husart
->State
= HAL_USART_STATE_READY
;
3539 state
= HAL_USART_STATE_READY
;
3541 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3542 /* Call registered Rx Complete Callback */
3543 husart
->RxCpltCallback(husart
);
3545 /* Call legacy weak Rx Complete Callback */
3546 HAL_USART_RxCpltCallback(husart
);
3547 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3549 else if ((READ_BIT(husart
->Instance
->CR1
, USART_CR1_TCIE
) != USART_CR1_TCIE
) &&
3550 (txftie
!= USART_CR3_TXFTIE
) &&
3551 (txdatacount
== 0U))
3553 /* TxRx process is completed, restore husart->State to Ready */
3554 husart
->State
= HAL_USART_STATE_READY
;
3555 state
= HAL_USART_STATE_READY
;
3557 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3558 /* Call registered Tx Rx Complete Callback */
3559 husart
->TxRxCpltCallback(husart
);
3561 /* Call legacy weak Tx Rx Complete Callback */
3562 HAL_USART_TxRxCpltCallback(husart
);
3563 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3570 else if ((state
== HAL_USART_STATE_BUSY_RX
) &&
3571 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3573 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3574 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3583 /* When remaining number of bytes to receive is less than the RX FIFO
3584 threshold, next incoming frames are processed as if FIFO mode was
3585 disabled (i.e. one interrupt per received frame).
3587 rxdatacount
= husart
->RxXferCount
;
3588 if (((rxdatacount
!= 0U)) && (rxdatacount
< husart
->NbRxDataToProcess
))
3590 /* Disable the USART RXFT interrupt*/
3591 CLEAR_BIT(husart
->Instance
->CR3
, USART_CR3_RXFTIE
);
3593 /* Update the RxISR function pointer */
3594 husart
->RxISR
= USART_RxISR_16BIT
;
3596 /* Enable the USART Data Register Not Empty interrupt */
3597 SET_BIT(husart
->Instance
->CR1
, USART_CR1_RXNEIE_RXFNEIE
);
3599 if ((husart
->TxXferCount
== 0U) &&
3600 (state
== HAL_USART_STATE_BUSY_TX_RX
) &&
3601 (husart
->SlaveMode
== USART_SLAVEMODE_DISABLE
))
3603 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3604 husart
->Instance
->TDR
= (USART_DUMMY_DATA
& (uint16_t)0x00FF);
3610 /* Clear RXNE interrupt flag */
3611 __HAL_USART_SEND_REQ(husart
, USART_RXDATA_FLUSH_REQUEST
);
3619 #endif /* HAL_USART_MODULE_ENABLED */
3628 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/