2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_uart.c
\r
4 * @author MCD Application Team
\r
5 * @brief UART HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
\r
8 * + Initialization and de-initialization functions
\r
9 * + IO operation functions
\r
10 * + Peripheral Control functions
\r
14 ===============================================================================
\r
15 ##### How to use this driver #####
\r
16 ===============================================================================
\r
18 The UART HAL driver can be used as follows:
\r
20 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
\r
21 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
\r
22 (++) Enable the USARTx interface clock.
\r
23 (++) UART pins configuration:
\r
24 (+++) Enable the clock for the UART GPIOs.
\r
25 (+++) Configure these UART pins as alternate function pull-up.
\r
26 (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
\r
27 and HAL_UART_Receive_IT() APIs):
\r
28 (+++) Configure the USARTx interrupt priority.
\r
29 (+++) Enable the NVIC USART IRQ handle.
\r
30 (++) UART interrupts handling:
\r
31 -@@- The specific UART interrupts (Transmission complete interrupt,
\r
32 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
\r
33 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
\r
34 inside the transmit and receive processes.
\r
35 (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
\r
36 and HAL_UART_Receive_DMA() APIs):
\r
37 (+++) Declare a DMA handle structure for the Tx/Rx channel.
\r
38 (+++) Enable the DMAx interface clock.
\r
39 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
\r
40 (+++) Configure the DMA Tx/Rx channel.
\r
41 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
\r
42 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
\r
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
\r
45 flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
\r
47 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
\r
48 in the huart handle AdvancedInit structure.
\r
50 (#) For the UART asynchronous mode, initialize the UART registers by calling
\r
51 the HAL_UART_Init() API.
\r
53 (#) For the UART Half duplex mode, initialize the UART registers by calling
\r
54 the HAL_HalfDuplex_Init() API.
\r
56 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
\r
57 by calling the HAL_LIN_Init() API.
\r
59 (#) For the UART Multiprocessor mode, initialize the UART registers
\r
60 by calling the HAL_MultiProcessor_Init() API.
\r
62 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
\r
63 by calling the HAL_RS485Ex_Init() API.
\r
66 (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
\r
67 also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
\r
68 calling the customized HAL_UART_MspInit() API.
\r
70 ##### Callback registration #####
\r
71 ==================================
\r
74 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
\r
75 allows the user to configure dynamically the driver callbacks.
\r
78 Use Function @ref HAL_UART_RegisterCallback() to register a user callback.
\r
79 Function @ref HAL_UART_RegisterCallback() allows to register following callbacks:
\r
80 (+) TxHalfCpltCallback : Tx Half Complete Callback.
\r
81 (+) TxCpltCallback : Tx Complete Callback.
\r
82 (+) RxHalfCpltCallback : Rx Half Complete Callback.
\r
83 (+) RxCpltCallback : Rx Complete Callback.
\r
84 (+) ErrorCallback : Error Callback.
\r
85 (+) AbortCpltCallback : Abort Complete Callback.
\r
86 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
\r
87 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
\r
88 (+) WakeupCallback : Wakeup Callback.
\r
89 (+) RxFifoFullCallback : Rx Fifo Full Callback.
\r
90 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
\r
91 (+) MspInitCallback : UART MspInit.
\r
92 (+) MspDeInitCallback : UART MspDeInit.
\r
93 This function takes as parameters the HAL peripheral handle, the Callback ID
\r
94 and a pointer to the user callback function.
\r
97 Use function @ref HAL_UART_UnRegisterCallback() to reset a callback to the default
\r
98 weak (surcharged) function.
\r
99 @ref HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
\r
100 and the Callback ID.
\r
101 This function allows to reset following callbacks:
\r
102 (+) TxHalfCpltCallback : Tx Half Complete Callback.
\r
103 (+) TxCpltCallback : Tx Complete Callback.
\r
104 (+) RxHalfCpltCallback : Rx Half Complete Callback.
\r
105 (+) RxCpltCallback : Rx Complete Callback.
\r
106 (+) ErrorCallback : Error Callback.
\r
107 (+) AbortCpltCallback : Abort Complete Callback.
\r
108 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
\r
109 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
\r
110 (+) WakeupCallback : Wakeup Callback.
\r
111 (+) RxFifoFullCallback : Rx Fifo Full Callback.
\r
112 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
\r
113 (+) MspInitCallback : UART MspInit.
\r
114 (+) MspDeInitCallback : UART MspDeInit.
\r
117 By default, after the @ref HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
\r
118 all callbacks are set to the corresponding weak (surcharged) functions:
\r
119 examples @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback().
\r
120 Exception done for MspInit and MspDeInit functions that are respectively
\r
121 reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init()
\r
122 and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
\r
123 If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref HAL_UART_DeInit()
\r
124 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
\r
127 Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
\r
128 Exception done MspInit/MspDeInit that can be registered/unregistered
\r
129 in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
\r
130 MspInit/DeInit callbacks can be used during the Init/DeInit.
\r
131 In that case first register the MspInit/MspDeInit user callbacks
\r
132 using @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit()
\r
133 or @ref HAL_UART_Init() function.
\r
136 When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
\r
137 not defined, the callback registration feature is not available
\r
138 and weak (surcharged) callbacks are used.
\r
142 ******************************************************************************
\r
145 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
146 * All rights reserved.</center></h2>
\r
148 * This software component is licensed by ST under BSD 3-Clause license,
\r
149 * the "License"; You may not use this file except in compliance with the
\r
150 * License. You may obtain a copy of the License at:
\r
151 * opensource.org/licenses/BSD-3-Clause
\r
153 ******************************************************************************
\r
156 /* Includes ------------------------------------------------------------------*/
\r
157 #include "stm32l4xx_hal.h"
\r
159 /** @addtogroup STM32L4xx_HAL_Driver
\r
163 /** @defgroup UART UART
\r
164 * @brief HAL UART module driver
\r
168 #ifdef HAL_UART_MODULE_ENABLED
\r
170 /* Private typedef -----------------------------------------------------------*/
\r
171 /* Private define ------------------------------------------------------------*/
\r
172 /** @defgroup UART_Private_Constants UART Private Constants
\r
175 #if defined(USART_CR1_FIFOEN)
\r
176 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
\r
177 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
\r
178 USART_CR1_FIFOEN )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
\r
180 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
\r
181 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
\r
182 #endif /* USART_CR1_FIFOEN */
\r
184 #if defined(USART_CR1_FIFOEN)
\r
185 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT| \
\r
186 USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
\r
188 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
\r
189 #endif /* USART_CR1_FIFOEN */
\r
191 #define LPUART_BRR_MIN 0x00000300U /* LPUART BRR minimum authorized value */
\r
192 #define LPUART_BRR_MAX 0x000FFFFFU /* LPUART BRR maximum authorized value */
\r
194 #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
\r
195 #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
\r
201 /* Private macros ------------------------------------------------------------*/
\r
202 /* Private variables ---------------------------------------------------------*/
\r
203 /* Private function prototypes -----------------------------------------------*/
\r
204 /** @addtogroup UART_Private_Functions
\r
207 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
\r
208 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
\r
209 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
\r
210 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
\r
211 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
\r
212 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
\r
213 static void UART_DMAError(DMA_HandleTypeDef *hdma);
\r
214 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
\r
215 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
\r
216 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
\r
217 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
\r
218 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
\r
219 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
\r
220 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
\r
221 #if defined(USART_CR1_FIFOEN)
\r
222 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
\r
223 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
\r
224 #endif /* USART_CR1_FIFOEN */
\r
225 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
\r
226 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
\r
227 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
\r
228 #if defined(USART_CR1_FIFOEN)
\r
229 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
\r
230 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
\r
231 #endif /* USART_CR1_FIFOEN */
\r
236 /* Exported functions --------------------------------------------------------*/
\r
238 /** @defgroup UART_Exported_Functions UART Exported Functions
\r
242 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
\r
243 * @brief Initialization and Configuration functions
\r
246 ===============================================================================
\r
247 ##### Initialization and Configuration functions #####
\r
248 ===============================================================================
\r
250 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
\r
251 in asynchronous mode.
\r
252 (+) For the asynchronous mode the parameters below can be configured:
\r
256 (++) Parity: If the parity is enabled, then the MSB bit of the data written
\r
257 in the data register is transmitted but is changed by the parity bit.
\r
258 (++) Hardware flow control
\r
259 (++) Receiver/transmitter modes
\r
260 (++) Over Sampling Method
\r
261 (++) One-Bit Sampling Method
\r
262 (+) For the asynchronous mode, the following advanced features can be configured as well:
\r
263 (++) TX and/or RX pin level inversion
\r
264 (++) data logical level inversion
\r
265 (++) RX and TX pins swap
\r
266 (++) RX overrun detection disabling
\r
267 (++) DMA disabling on RX error
\r
268 (++) MSB first on communication line
\r
269 (++) auto Baud rate detection
\r
271 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
\r
272 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
\r
273 and UART multiprocessor mode configuration procedures (details for the procedures
\r
274 are available in reference manual).
\r
278 Depending on the frame length defined by the M1 and M0 bits (7-bit,
\r
279 8-bit or 9-bit), the possible UART formats are listed in the
\r
282 Table 1. UART frame format.
\r
283 +-----------------------------------------------------------------------+
\r
284 | M1 bit | M0 bit | PCE bit | UART frame |
\r
285 |---------|---------|-----------|---------------------------------------|
\r
286 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
\r
287 |---------|---------|-----------|---------------------------------------|
\r
288 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
\r
289 |---------|---------|-----------|---------------------------------------|
\r
290 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
\r
291 |---------|---------|-----------|---------------------------------------|
\r
292 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
\r
293 |---------|---------|-----------|---------------------------------------|
\r
294 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
\r
295 |---------|---------|-----------|---------------------------------------|
\r
296 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
\r
297 +-----------------------------------------------------------------------+
\r
303 * @brief Initialize the UART mode according to the specified
\r
304 * parameters in the UART_InitTypeDef and initialize the associated handle.
\r
305 * @param huart UART handle.
\r
306 * @retval HAL status
\r
308 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
\r
310 /* Check the UART handle allocation */
\r
316 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
\r
318 /* Check the parameters */
\r
319 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
\r
323 /* Check the parameters */
\r
324 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
\r
327 if (huart->gState == HAL_UART_STATE_RESET)
\r
329 /* Allocate lock resource and initialize it */
\r
330 huart->Lock = HAL_UNLOCKED;
\r
332 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
333 UART_InitCallbacksToDefault(huart);
\r
335 if (huart->MspInitCallback == NULL)
\r
337 huart->MspInitCallback = HAL_UART_MspInit;
\r
340 /* Init the low level hardware */
\r
341 huart->MspInitCallback(huart);
\r
343 /* Init the low level hardware : GPIO, CLOCK */
\r
344 HAL_UART_MspInit(huart);
\r
345 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
348 huart->gState = HAL_UART_STATE_BUSY;
\r
350 __HAL_UART_DISABLE(huart);
\r
352 /* Set the UART Communication parameters */
\r
353 if (UART_SetConfig(huart) == HAL_ERROR)
\r
358 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
360 UART_AdvFeatureConfig(huart);
\r
363 /* In asynchronous mode, the following bits must be kept cleared:
\r
364 - LINEN and CLKEN bits in the USART_CR2 register,
\r
365 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
\r
366 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
\r
367 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
\r
369 __HAL_UART_ENABLE(huart);
\r
371 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
372 return (UART_CheckIdleState(huart));
\r
376 * @brief Initialize the half-duplex mode according to the specified
\r
377 * parameters in the UART_InitTypeDef and creates the associated handle.
\r
378 * @param huart UART handle.
\r
379 * @retval HAL status
\r
381 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
\r
383 /* Check the UART handle allocation */
\r
389 /* Check UART instance */
\r
390 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
\r
392 if (huart->gState == HAL_UART_STATE_RESET)
\r
394 /* Allocate lock resource and initialize it */
\r
395 huart->Lock = HAL_UNLOCKED;
\r
397 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
398 UART_InitCallbacksToDefault(huart);
\r
400 if (huart->MspInitCallback == NULL)
\r
402 huart->MspInitCallback = HAL_UART_MspInit;
\r
405 /* Init the low level hardware */
\r
406 huart->MspInitCallback(huart);
\r
408 /* Init the low level hardware : GPIO, CLOCK */
\r
409 HAL_UART_MspInit(huart);
\r
410 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
413 huart->gState = HAL_UART_STATE_BUSY;
\r
415 __HAL_UART_DISABLE(huart);
\r
417 /* Set the UART Communication parameters */
\r
418 if (UART_SetConfig(huart) == HAL_ERROR)
\r
423 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
425 UART_AdvFeatureConfig(huart);
\r
428 /* In half-duplex mode, the following bits must be kept cleared:
\r
429 - LINEN and CLKEN bits in the USART_CR2 register,
\r
430 - SCEN and IREN bits in the USART_CR3 register.*/
\r
431 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
\r
432 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
\r
434 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
\r
435 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
\r
437 __HAL_UART_ENABLE(huart);
\r
439 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
440 return (UART_CheckIdleState(huart));
\r
445 * @brief Initialize the LIN mode according to the specified
\r
446 * parameters in the UART_InitTypeDef and creates the associated handle.
\r
447 * @param huart UART handle.
\r
448 * @param BreakDetectLength Specifies the LIN break detection length.
\r
449 * This parameter can be one of the following values:
\r
450 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
\r
451 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
\r
452 * @retval HAL status
\r
454 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
\r
456 /* Check the UART handle allocation */
\r
462 /* Check the LIN UART instance */
\r
463 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
\r
464 /* Check the Break detection length parameter */
\r
465 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
\r
467 /* LIN mode limited to 16-bit oversampling only */
\r
468 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
\r
472 /* LIN mode limited to 8-bit data length */
\r
473 if (huart->Init.WordLength != UART_WORDLENGTH_8B)
\r
478 if (huart->gState == HAL_UART_STATE_RESET)
\r
480 /* Allocate lock resource and initialize it */
\r
481 huart->Lock = HAL_UNLOCKED;
\r
483 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
484 UART_InitCallbacksToDefault(huart);
\r
486 if (huart->MspInitCallback == NULL)
\r
488 huart->MspInitCallback = HAL_UART_MspInit;
\r
491 /* Init the low level hardware */
\r
492 huart->MspInitCallback(huart);
\r
494 /* Init the low level hardware : GPIO, CLOCK */
\r
495 HAL_UART_MspInit(huart);
\r
496 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
499 huart->gState = HAL_UART_STATE_BUSY;
\r
501 __HAL_UART_DISABLE(huart);
\r
503 /* Set the UART Communication parameters */
\r
504 if (UART_SetConfig(huart) == HAL_ERROR)
\r
509 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
511 UART_AdvFeatureConfig(huart);
\r
514 /* In LIN mode, the following bits must be kept cleared:
\r
515 - LINEN and CLKEN bits in the USART_CR2 register,
\r
516 - SCEN and IREN bits in the USART_CR3 register.*/
\r
517 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
\r
518 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
\r
520 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
\r
521 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
\r
523 /* Set the USART LIN Break detection length. */
\r
524 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
\r
526 __HAL_UART_ENABLE(huart);
\r
528 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
529 return (UART_CheckIdleState(huart));
\r
534 * @brief Initialize the multiprocessor mode according to the specified
\r
535 * parameters in the UART_InitTypeDef and initialize the associated handle.
\r
536 * @param huart UART handle.
\r
537 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
\r
538 * @param WakeUpMethod Specifies the UART wakeup method.
\r
539 * This parameter can be one of the following values:
\r
540 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
\r
541 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
\r
542 * @note If the user resorts to idle line detection wake up, the Address parameter
\r
543 * is useless and ignored by the initialization function.
\r
544 * @note If the user resorts to address mark wake up, the address length detection
\r
545 * is configured by default to 4 bits only. For the UART to be able to
\r
546 * manage 6-, 7- or 8-bit long addresses detection, the API
\r
547 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
\r
548 * HAL_MultiProcessor_Init().
\r
549 * @retval HAL status
\r
551 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
\r
553 /* Check the UART handle allocation */
\r
559 /* Check the wake up method parameter */
\r
560 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
\r
562 if (huart->gState == HAL_UART_STATE_RESET)
\r
564 /* Allocate lock resource and initialize it */
\r
565 huart->Lock = HAL_UNLOCKED;
\r
567 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
568 UART_InitCallbacksToDefault(huart);
\r
570 if (huart->MspInitCallback == NULL)
\r
572 huart->MspInitCallback = HAL_UART_MspInit;
\r
575 /* Init the low level hardware */
\r
576 huart->MspInitCallback(huart);
\r
578 /* Init the low level hardware : GPIO, CLOCK */
\r
579 HAL_UART_MspInit(huart);
\r
580 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
583 huart->gState = HAL_UART_STATE_BUSY;
\r
585 __HAL_UART_DISABLE(huart);
\r
587 /* Set the UART Communication parameters */
\r
588 if (UART_SetConfig(huart) == HAL_ERROR)
\r
593 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
\r
595 UART_AdvFeatureConfig(huart);
\r
598 /* In multiprocessor mode, the following bits must be kept cleared:
\r
599 - LINEN and CLKEN bits in the USART_CR2 register,
\r
600 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
\r
601 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
\r
602 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
\r
604 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
\r
606 /* If address mark wake up method is chosen, set the USART address node */
\r
607 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
\r
610 /* Set the wake up method by setting the WAKE bit in the CR1 register */
\r
611 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
\r
613 __HAL_UART_ENABLE(huart);
\r
615 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
\r
616 return (UART_CheckIdleState(huart));
\r
621 * @brief DeInitialize the UART peripheral.
\r
622 * @param huart UART handle.
\r
623 * @retval HAL status
\r
625 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
\r
627 /* Check the UART handle allocation */
\r
633 /* Check the parameters */
\r
634 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
\r
636 huart->gState = HAL_UART_STATE_BUSY;
\r
638 __HAL_UART_DISABLE(huart);
\r
640 huart->Instance->CR1 = 0x0U;
\r
641 huart->Instance->CR2 = 0x0U;
\r
642 huart->Instance->CR3 = 0x0U;
\r
644 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
645 if (huart->MspDeInitCallback == NULL)
\r
647 huart->MspDeInitCallback = HAL_UART_MspDeInit;
\r
649 /* DeInit the low level hardware */
\r
650 huart->MspDeInitCallback(huart);
\r
652 /* DeInit the low level hardware */
\r
653 HAL_UART_MspDeInit(huart);
\r
654 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
\r
656 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
657 huart->gState = HAL_UART_STATE_RESET;
\r
658 huart->RxState = HAL_UART_STATE_RESET;
\r
660 __HAL_UNLOCK(huart);
\r
666 * @brief Initialize the UART MSP.
\r
667 * @param huart UART handle.
\r
670 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
\r
672 /* Prevent unused argument(s) compilation warning */
\r
675 /* NOTE : This function should not be modified, when the callback is needed,
\r
676 the HAL_UART_MspInit can be implemented in the user file
\r
681 * @brief DeInitialize the UART MSP.
\r
682 * @param huart UART handle.
\r
685 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
\r
687 /* Prevent unused argument(s) compilation warning */
\r
690 /* NOTE : This function should not be modified, when the callback is needed,
\r
691 the HAL_UART_MspDeInit can be implemented in the user file
\r
695 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
697 * @brief Register a User UART Callback
\r
698 * To be used instead of the weak predefined callback
\r
699 * @param huart uart handle
\r
700 * @param CallbackID ID of the callback to be registered
\r
701 * This parameter can be one of the following values:
\r
702 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
\r
703 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
\r
704 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
\r
705 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
\r
706 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
\r
707 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
\r
708 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
\r
709 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
\r
710 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
\r
711 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
\r
712 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
\r
713 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
\r
714 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
\r
715 * @param pCallback pointer to the Callback function
\r
716 * @retval HAL status
\r
718 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
\r
719 pUART_CallbackTypeDef pCallback)
\r
721 HAL_StatusTypeDef status = HAL_OK;
\r
723 if (pCallback == NULL)
\r
725 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
732 if (huart->gState == HAL_UART_STATE_READY)
\r
734 switch (CallbackID)
\r
736 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
\r
737 huart->TxHalfCpltCallback = pCallback;
\r
740 case HAL_UART_TX_COMPLETE_CB_ID :
\r
741 huart->TxCpltCallback = pCallback;
\r
744 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
\r
745 huart->RxHalfCpltCallback = pCallback;
\r
748 case HAL_UART_RX_COMPLETE_CB_ID :
\r
749 huart->RxCpltCallback = pCallback;
\r
752 case HAL_UART_ERROR_CB_ID :
\r
753 huart->ErrorCallback = pCallback;
\r
756 case HAL_UART_ABORT_COMPLETE_CB_ID :
\r
757 huart->AbortCpltCallback = pCallback;
\r
760 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
\r
761 huart->AbortTransmitCpltCallback = pCallback;
\r
764 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
\r
765 huart->AbortReceiveCpltCallback = pCallback;
\r
768 case HAL_UART_WAKEUP_CB_ID :
\r
769 huart->WakeupCallback = pCallback;
\r
772 #if defined(USART_CR1_FIFOEN)
\r
773 case HAL_UART_RX_FIFO_FULL_CB_ID :
\r
774 huart->RxFifoFullCallback = pCallback;
\r
777 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
\r
778 huart->TxFifoEmptyCallback = pCallback;
\r
780 #endif /* USART_CR1_FIFOEN */
\r
782 case HAL_UART_MSPINIT_CB_ID :
\r
783 huart->MspInitCallback = pCallback;
\r
786 case HAL_UART_MSPDEINIT_CB_ID :
\r
787 huart->MspDeInitCallback = pCallback;
\r
791 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
793 status = HAL_ERROR;
\r
797 else if (huart->gState == HAL_UART_STATE_RESET)
\r
799 switch (CallbackID)
\r
801 case HAL_UART_MSPINIT_CB_ID :
\r
802 huart->MspInitCallback = pCallback;
\r
805 case HAL_UART_MSPDEINIT_CB_ID :
\r
806 huart->MspDeInitCallback = pCallback;
\r
810 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
812 status = HAL_ERROR;
\r
818 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
820 status = HAL_ERROR;
\r
823 __HAL_UNLOCK(huart);
\r
829 * @brief Unregister an UART Callback
\r
830 * UART callaback is redirected to the weak predefined callback
\r
831 * @param huart uart handle
\r
832 * @param CallbackID ID of the callback to be unregistered
\r
833 * This parameter can be one of the following values:
\r
834 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
\r
835 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
\r
836 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
\r
837 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
\r
838 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
\r
839 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
\r
840 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
\r
841 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
\r
842 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
\r
843 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
\r
844 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
\r
845 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
\r
846 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
\r
847 * @retval HAL status
\r
849 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
\r
851 HAL_StatusTypeDef status = HAL_OK;
\r
855 if (HAL_UART_STATE_READY == huart->gState)
\r
857 switch (CallbackID)
\r
859 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
\r
860 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
\r
863 case HAL_UART_TX_COMPLETE_CB_ID :
\r
864 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
\r
867 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
\r
868 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
\r
871 case HAL_UART_RX_COMPLETE_CB_ID :
\r
872 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
\r
875 case HAL_UART_ERROR_CB_ID :
\r
876 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
\r
879 case HAL_UART_ABORT_COMPLETE_CB_ID :
\r
880 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
883 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
\r
884 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
\r
887 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
\r
888 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
\r
891 case HAL_UART_WAKEUP_CB_ID :
\r
892 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
\r
895 #if defined(USART_CR1_FIFOEN)
\r
896 case HAL_UART_RX_FIFO_FULL_CB_ID :
\r
897 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
\r
900 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
\r
901 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
\r
904 #endif /* USART_CR1_FIFOEN */
\r
905 case HAL_UART_MSPINIT_CB_ID :
\r
906 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
\r
909 case HAL_UART_MSPDEINIT_CB_ID :
\r
910 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
\r
914 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
916 status = HAL_ERROR;
\r
920 else if (HAL_UART_STATE_RESET == huart->gState)
\r
922 switch (CallbackID)
\r
924 case HAL_UART_MSPINIT_CB_ID :
\r
925 huart->MspInitCallback = HAL_UART_MspInit;
\r
928 case HAL_UART_MSPDEINIT_CB_ID :
\r
929 huart->MspDeInitCallback = HAL_UART_MspDeInit;
\r
933 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
935 status = HAL_ERROR;
\r
941 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
\r
943 status = HAL_ERROR;
\r
946 __HAL_UNLOCK(huart);
\r
950 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
956 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
\r
957 * @brief UART Transmit/Receive functions
\r
960 ===============================================================================
\r
961 ##### IO operation functions #####
\r
962 ===============================================================================
\r
963 This subsection provides a set of functions allowing to manage the UART asynchronous
\r
964 and Half duplex data transfers.
\r
966 (#) There are two mode of transfer:
\r
967 (+) Blocking mode: The communication is performed in polling mode.
\r
968 The HAL status of all data processing is returned by the same function
\r
969 after finishing transfer.
\r
970 (+) Non-Blocking mode: The communication is performed using Interrupts
\r
971 or DMA, These API's return the HAL status.
\r
972 The end of the data processing will be indicated through the
\r
973 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
\r
975 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
\r
976 will be executed respectively at the end of the transmit or Receive process
\r
977 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
\r
979 (#) Blocking mode API's are :
\r
980 (+) HAL_UART_Transmit()
\r
981 (+) HAL_UART_Receive()
\r
983 (#) Non-Blocking mode API's with Interrupt are :
\r
984 (+) HAL_UART_Transmit_IT()
\r
985 (+) HAL_UART_Receive_IT()
\r
986 (+) HAL_UART_IRQHandler()
\r
988 (#) Non-Blocking mode API's with DMA are :
\r
989 (+) HAL_UART_Transmit_DMA()
\r
990 (+) HAL_UART_Receive_DMA()
\r
991 (+) HAL_UART_DMAPause()
\r
992 (+) HAL_UART_DMAResume()
\r
993 (+) HAL_UART_DMAStop()
\r
995 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
\r
996 (+) HAL_UART_TxHalfCpltCallback()
\r
997 (+) HAL_UART_TxCpltCallback()
\r
998 (+) HAL_UART_RxHalfCpltCallback()
\r
999 (+) HAL_UART_RxCpltCallback()
\r
1000 (+) HAL_UART_ErrorCallback()
\r
1002 (#) Non-Blocking mode transfers could be aborted using Abort API's :
\r
1003 (+) HAL_UART_Abort()
\r
1004 (+) HAL_UART_AbortTransmit()
\r
1005 (+) HAL_UART_AbortReceive()
\r
1006 (+) HAL_UART_Abort_IT()
\r
1007 (+) HAL_UART_AbortTransmit_IT()
\r
1008 (+) HAL_UART_AbortReceive_IT()
\r
1010 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
\r
1011 (+) HAL_UART_AbortCpltCallback()
\r
1012 (+) HAL_UART_AbortTransmitCpltCallback()
\r
1013 (+) HAL_UART_AbortReceiveCpltCallback()
\r
1015 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
\r
1016 Errors are handled as follows :
\r
1017 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
\r
1018 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
\r
1019 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
\r
1020 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
\r
1021 If user wants to abort it, Abort services should be called by user.
\r
1022 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
\r
1023 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
\r
1024 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
\r
1026 -@- In the Half duplex communication, it is forbidden to run the transmit
\r
1027 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
\r
1034 * @brief Send an amount of data in blocking mode.
\r
1035 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
\r
1036 * data to the TXFIFO. Write operations to the TDR register are performed
\r
1037 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
\r
1038 * TXE are mapped on the same bit-field.
\r
1039 * @param huart UART handle.
\r
1040 * @param pData Pointer to data buffer.
\r
1041 * @param Size Amount of data to be sent.
\r
1042 * @param Timeout Timeout duration.
\r
1043 * @retval HAL status
\r
1045 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1047 uint8_t *pdata8bits;
\r
1048 uint16_t *pdata16bits;
\r
1049 uint32_t tickstart;
\r
1051 /* Check that a Tx process is not already ongoing */
\r
1052 if (huart->gState == HAL_UART_STATE_READY)
\r
1054 if ((pData == NULL) || (Size == 0U))
\r
1059 __HAL_LOCK(huart);
\r
1061 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1062 huart->gState = HAL_UART_STATE_BUSY_TX;
\r
1064 /* Init tickstart for timeout managment*/
\r
1065 tickstart = HAL_GetTick();
\r
1067 huart->TxXferSize = Size;
\r
1068 huart->TxXferCount = Size;
\r
1070 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
\r
1071 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1073 pdata8bits = NULL;
\r
1074 pdata16bits = (uint16_t *) pData;
\r
1078 pdata8bits = pData;
\r
1079 pdata16bits = NULL;
\r
1082 while (huart->TxXferCount > 0U)
\r
1084 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
\r
1086 return HAL_TIMEOUT;
\r
1088 if (pdata8bits == NULL)
\r
1090 huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
\r
1095 huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
\r
1098 huart->TxXferCount--;
\r
1101 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
\r
1103 return HAL_TIMEOUT;
\r
1106 /* At end of Tx process, restore huart->gState to Ready */
\r
1107 huart->gState = HAL_UART_STATE_READY;
\r
1109 __HAL_UNLOCK(huart);
\r
1120 * @brief Receive an amount of data in blocking mode.
\r
1121 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
\r
1122 * is not empty. Read operations from the RDR register are performed when
\r
1123 * RXFNE flag is set. From hardware perspective, RXFNE flag and
\r
1124 * RXNE are mapped on the same bit-field.
\r
1125 * @param huart UART handle.
\r
1126 * @param pData Pointer to data buffer.
\r
1127 * @param Size Amount of data to be received.
\r
1128 * @param Timeout Timeout duration.
\r
1129 * @retval HAL status
\r
1131 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
1133 uint8_t *pdata8bits;
\r
1134 uint16_t *pdata16bits;
\r
1136 uint32_t tickstart;
\r
1138 /* Check that a Rx process is not already ongoing */
\r
1139 if (huart->RxState == HAL_UART_STATE_READY)
\r
1141 if ((pData == NULL) || (Size == 0U))
\r
1146 __HAL_LOCK(huart);
\r
1148 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1149 huart->RxState = HAL_UART_STATE_BUSY_RX;
\r
1151 /* Init tickstart for timeout managment*/
\r
1152 tickstart = HAL_GetTick();
\r
1154 huart->RxXferSize = Size;
\r
1155 huart->RxXferCount = Size;
\r
1157 /* Computation of UART mask to apply to RDR register */
\r
1158 UART_MASK_COMPUTATION(huart);
\r
1159 uhMask = huart->Mask;
\r
1161 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
\r
1162 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1164 pdata8bits = NULL;
\r
1165 pdata16bits = (uint16_t *) pData;
\r
1169 pdata8bits = pData;
\r
1170 pdata16bits = NULL;
\r
1173 /* as long as data have to be received */
\r
1174 while (huart->RxXferCount > 0U)
\r
1176 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
\r
1178 return HAL_TIMEOUT;
\r
1180 if (pdata8bits == NULL)
\r
1182 *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
\r
1187 *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
\r
1190 huart->RxXferCount--;
\r
1193 /* At end of Rx process, restore huart->RxState to Ready */
\r
1194 huart->RxState = HAL_UART_STATE_READY;
\r
1196 __HAL_UNLOCK(huart);
\r
1207 * @brief Send an amount of data in interrupt mode.
\r
1208 * @param huart UART handle.
\r
1209 * @param pData Pointer to data buffer.
\r
1210 * @param Size Amount of data to be sent.
\r
1211 * @retval HAL status
\r
1213 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
\r
1215 /* Check that a Tx process is not already ongoing */
\r
1216 if (huart->gState == HAL_UART_STATE_READY)
\r
1218 if ((pData == NULL) || (Size == 0U))
\r
1223 __HAL_LOCK(huart);
\r
1225 huart->pTxBuffPtr = pData;
\r
1226 huart->TxXferSize = Size;
\r
1227 huart->TxXferCount = Size;
\r
1228 huart->TxISR = NULL;
\r
1230 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1231 huart->gState = HAL_UART_STATE_BUSY_TX;
\r
1233 #if defined(USART_CR1_FIFOEN)
\r
1234 /* Configure Tx interrupt processing */
\r
1235 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
1237 /* Set the Tx ISR function pointer according to the data word length */
\r
1238 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1240 huart->TxISR = UART_TxISR_16BIT_FIFOEN;
\r
1244 huart->TxISR = UART_TxISR_8BIT_FIFOEN;
\r
1247 __HAL_UNLOCK(huart);
\r
1249 /* Enable the TX FIFO threshold interrupt */
\r
1250 SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
\r
1254 /* Set the Tx ISR function pointer according to the data word length */
\r
1255 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1257 huart->TxISR = UART_TxISR_16BIT;
\r
1261 huart->TxISR = UART_TxISR_8BIT;
\r
1264 __HAL_UNLOCK(huart);
\r
1266 /* Enable the Transmit Data Register Empty interrupt */
\r
1267 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
\r
1270 /* Set the Tx ISR function pointer according to the data word length */
\r
1271 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1273 huart->TxISR = UART_TxISR_16BIT;
\r
1277 huart->TxISR = UART_TxISR_8BIT;
\r
1280 __HAL_UNLOCK(huart);
\r
1282 /* Enable the Transmit Data Register Empty interrupt */
\r
1283 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
\r
1284 #endif /* USART_CR1_FIFOEN */
\r
1295 * @brief Receive an amount of data in interrupt mode.
\r
1296 * @param huart UART handle.
\r
1297 * @param pData Pointer to data buffer.
\r
1298 * @param Size Amount of data to be received.
\r
1299 * @retval HAL status
\r
1301 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
\r
1303 /* Check that a Rx process is not already ongoing */
\r
1304 if (huart->RxState == HAL_UART_STATE_READY)
\r
1306 if ((pData == NULL) || (Size == 0U))
\r
1311 __HAL_LOCK(huart);
\r
1313 huart->pRxBuffPtr = pData;
\r
1314 huart->RxXferSize = Size;
\r
1315 huart->RxXferCount = Size;
\r
1316 huart->RxISR = NULL;
\r
1318 /* Computation of UART mask to apply to RDR register */
\r
1319 UART_MASK_COMPUTATION(huart);
\r
1321 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1322 huart->RxState = HAL_UART_STATE_BUSY_RX;
\r
1324 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
\r
1325 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1327 #if defined(USART_CR1_FIFOEN)
\r
1328 /* Configure Rx interrupt processing*/
\r
1329 if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
\r
1331 /* Set the Rx ISR function pointer according to the data word length */
\r
1332 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1334 huart->RxISR = UART_RxISR_16BIT_FIFOEN;
\r
1338 huart->RxISR = UART_RxISR_8BIT_FIFOEN;
\r
1341 __HAL_UNLOCK(huart);
\r
1343 /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
\r
1344 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
1345 SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
\r
1349 /* Set the Rx ISR function pointer according to the data word length */
\r
1350 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1352 huart->RxISR = UART_RxISR_16BIT;
\r
1356 huart->RxISR = UART_RxISR_8BIT;
\r
1359 __HAL_UNLOCK(huart);
\r
1361 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
\r
1362 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
\r
1365 /* Set the Rx ISR function pointer according to the data word length */
\r
1366 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
\r
1368 huart->RxISR = UART_RxISR_16BIT;
\r
1372 huart->RxISR = UART_RxISR_8BIT;
\r
1375 __HAL_UNLOCK(huart);
\r
1377 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
\r
1378 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
\r
1379 #endif /* USART_CR1_FIFOEN */
\r
1390 * @brief Send an amount of data in DMA mode.
\r
1391 * @param huart UART handle.
\r
1392 * @param pData Pointer to data buffer.
\r
1393 * @param Size Amount of data to be sent.
\r
1394 * @retval HAL status
\r
1396 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
\r
1398 /* Check that a Tx process is not already ongoing */
\r
1399 if (huart->gState == HAL_UART_STATE_READY)
\r
1401 if ((pData == NULL) || (Size == 0U))
\r
1406 __HAL_LOCK(huart);
\r
1408 huart->pTxBuffPtr = pData;
\r
1409 huart->TxXferSize = Size;
\r
1410 huart->TxXferCount = Size;
\r
1412 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1413 huart->gState = HAL_UART_STATE_BUSY_TX;
\r
1415 if (huart->hdmatx != NULL)
\r
1417 /* Set the UART DMA transfer complete callback */
\r
1418 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
\r
1420 /* Set the UART DMA Half transfer complete callback */
\r
1421 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
\r
1423 /* Set the DMA error callback */
\r
1424 huart->hdmatx->XferErrorCallback = UART_DMAError;
\r
1426 /* Set the DMA abort callback */
\r
1427 huart->hdmatx->XferAbortCallback = NULL;
\r
1429 /* Enable the UART transmit DMA channel */
\r
1430 if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
\r
1432 /* Set error code to DMA */
\r
1433 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1435 __HAL_UNLOCK(huart);
\r
1437 /* Restore huart->gState to ready */
\r
1438 huart->gState = HAL_UART_STATE_READY;
\r
1443 /* Clear the TC flag in the ICR register */
\r
1444 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
\r
1446 __HAL_UNLOCK(huart);
\r
1448 /* Enable the DMA transfer for transmit request by setting the DMAT bit
\r
1449 in the UART CR3 register */
\r
1450 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1461 * @brief Receive an amount of data in DMA mode.
\r
1462 * @note When the UART parity is enabled (PCE = 1), the received data contain
\r
1463 * the parity bit (MSB position).
\r
1464 * @param huart UART handle.
\r
1465 * @param pData Pointer to data buffer.
\r
1466 * @param Size Amount of data to be received.
\r
1467 * @retval HAL status
\r
1469 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
\r
1471 /* Check that a Rx process is not already ongoing */
\r
1472 if (huart->RxState == HAL_UART_STATE_READY)
\r
1474 if ((pData == NULL) || (Size == 0U))
\r
1479 __HAL_LOCK(huart);
\r
1481 huart->pRxBuffPtr = pData;
\r
1482 huart->RxXferSize = Size;
\r
1484 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1485 huart->RxState = HAL_UART_STATE_BUSY_RX;
\r
1487 if (huart->hdmarx != NULL)
\r
1489 /* Set the UART DMA transfer complete callback */
\r
1490 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
\r
1492 /* Set the UART DMA Half transfer complete callback */
\r
1493 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
\r
1495 /* Set the DMA error callback */
\r
1496 huart->hdmarx->XferErrorCallback = UART_DMAError;
\r
1498 /* Set the DMA abort callback */
\r
1499 huart->hdmarx->XferAbortCallback = NULL;
\r
1501 /* Enable the DMA channel */
\r
1502 if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
\r
1504 /* Set error code to DMA */
\r
1505 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1507 __HAL_UNLOCK(huart);
\r
1509 /* Restore huart->gState to ready */
\r
1510 huart->gState = HAL_UART_STATE_READY;
\r
1515 __HAL_UNLOCK(huart);
\r
1517 /* Enable the UART Parity Error Interrupt */
\r
1518 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
1520 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
\r
1521 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1523 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
\r
1524 in the UART CR3 register */
\r
1525 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1536 * @brief Pause the DMA Transfer.
\r
1537 * @param huart UART handle.
\r
1538 * @retval HAL status
\r
1540 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
\r
1542 const HAL_UART_StateTypeDef gstate = huart->gState;
\r
1543 const HAL_UART_StateTypeDef rxstate = huart->RxState;
\r
1545 __HAL_LOCK(huart);
\r
1547 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
\r
1548 (gstate == HAL_UART_STATE_BUSY_TX))
\r
1550 /* Disable the UART DMA Tx request */
\r
1551 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1553 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
\r
1554 (rxstate == HAL_UART_STATE_BUSY_RX))
\r
1556 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
1557 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
1558 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1560 /* Disable the UART DMA Rx request */
\r
1561 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1564 __HAL_UNLOCK(huart);
\r
1570 * @brief Resume the DMA Transfer.
\r
1571 * @param huart UART handle.
\r
1572 * @retval HAL status
\r
1574 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
\r
1576 __HAL_LOCK(huart);
\r
1578 if (huart->gState == HAL_UART_STATE_BUSY_TX)
\r
1580 /* Enable the UART DMA Tx request */
\r
1581 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1583 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
\r
1585 /* Clear the Overrun flag before resuming the Rx transfer */
\r
1586 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
\r
1588 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
1589 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
1590 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1592 /* Enable the UART DMA Rx request */
\r
1593 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1596 __HAL_UNLOCK(huart);
\r
1602 * @brief Stop the DMA Transfer.
\r
1603 * @param huart UART handle.
\r
1604 * @retval HAL status
\r
1606 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
\r
1608 /* The Lock is not implemented on this API to allow the user application
\r
1609 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
\r
1610 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
\r
1611 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
\r
1612 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
\r
1613 the stream and the corresponding call back is executed. */
\r
1615 const HAL_UART_StateTypeDef gstate = huart->gState;
\r
1616 const HAL_UART_StateTypeDef rxstate = huart->RxState;
\r
1618 /* Stop UART DMA Tx request if ongoing */
\r
1619 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
\r
1620 (gstate == HAL_UART_STATE_BUSY_TX))
\r
1622 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1624 /* Abort the UART DMA Tx channel */
\r
1625 if (huart->hdmatx != NULL)
\r
1627 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
\r
1629 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
\r
1631 /* Set error code to DMA */
\r
1632 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1634 return HAL_TIMEOUT;
\r
1639 UART_EndTxTransfer(huart);
\r
1642 /* Stop UART DMA Rx request if ongoing */
\r
1643 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
\r
1644 (rxstate == HAL_UART_STATE_BUSY_RX))
\r
1646 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1648 /* Abort the UART DMA Rx channel */
\r
1649 if (huart->hdmarx != NULL)
\r
1651 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
\r
1653 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
\r
1655 /* Set error code to DMA */
\r
1656 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1658 return HAL_TIMEOUT;
\r
1663 UART_EndRxTransfer(huart);
\r
1670 * @brief Abort ongoing transfers (blocking mode).
\r
1671 * @param huart UART handle.
\r
1672 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
\r
1673 * This procedure performs following operations :
\r
1674 * - Disable UART Interrupts (Tx and Rx)
\r
1675 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
1676 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
\r
1677 * - Set handle State to READY
\r
1678 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
\r
1679 * @retval HAL status
\r
1681 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
\r
1683 #if defined(USART_CR1_FIFOEN)
\r
1684 /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
\r
1685 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
\r
1686 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
\r
1688 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
1689 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
\r
1690 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1691 #endif /* USART_CR1_FIFOEN */
\r
1693 /* Disable the UART DMA Tx request if enabled */
\r
1694 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
\r
1696 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1698 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
\r
1699 if (huart->hdmatx != NULL)
\r
1701 /* Set the UART DMA Abort callback to Null.
\r
1702 No call back execution at end of DMA abort procedure */
\r
1703 huart->hdmatx->XferAbortCallback = NULL;
\r
1705 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
\r
1707 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
\r
1709 /* Set error code to DMA */
\r
1710 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1712 return HAL_TIMEOUT;
\r
1718 /* Disable the UART DMA Rx request if enabled */
\r
1719 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
1721 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1723 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
\r
1724 if (huart->hdmarx != NULL)
\r
1726 /* Set the UART DMA Abort callback to Null.
\r
1727 No call back execution at end of DMA abort procedure */
\r
1728 huart->hdmarx->XferAbortCallback = NULL;
\r
1730 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
\r
1732 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
\r
1734 /* Set error code to DMA */
\r
1735 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1737 return HAL_TIMEOUT;
\r
1743 /* Reset Tx and Rx transfer counters */
\r
1744 huart->TxXferCount = 0U;
\r
1745 huart->RxXferCount = 0U;
\r
1747 /* Clear the Error flags in the ICR register */
\r
1748 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
1750 #if defined(USART_CR1_FIFOEN)
\r
1751 /* Flush the whole TX FIFO (if needed) */
\r
1752 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
1754 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
1756 #endif /* USART_CR1_FIFOEN */
\r
1758 /* Discard the received data */
\r
1759 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
1761 /* Restore huart->gState and huart->RxState to Ready */
\r
1762 huart->gState = HAL_UART_STATE_READY;
\r
1763 huart->RxState = HAL_UART_STATE_READY;
\r
1765 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
1771 * @brief Abort ongoing Transmit transfer (blocking mode).
\r
1772 * @param huart UART handle.
\r
1773 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
\r
1774 * This procedure performs following operations :
\r
1775 * - Disable UART Interrupts (Tx)
\r
1776 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
1777 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
\r
1778 * - Set handle State to READY
\r
1779 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
\r
1780 * @retval HAL status
\r
1782 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
\r
1784 #if defined(USART_CR1_FIFOEN)
\r
1785 /* Disable TCIE, TXEIE and TXFTIE interrupts */
\r
1786 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
\r
1787 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
\r
1789 /* Disable TXEIE and TCIE interrupts */
\r
1790 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
\r
1791 #endif /* USART_CR1_FIFOEN */
\r
1793 /* Disable the UART DMA Tx request if enabled */
\r
1794 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
\r
1796 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1798 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
\r
1799 if (huart->hdmatx != NULL)
\r
1801 /* Set the UART DMA Abort callback to Null.
\r
1802 No call back execution at end of DMA abort procedure */
\r
1803 huart->hdmatx->XferAbortCallback = NULL;
\r
1805 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
\r
1807 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
\r
1809 /* Set error code to DMA */
\r
1810 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1812 return HAL_TIMEOUT;
\r
1818 /* Reset Tx transfer counter */
\r
1819 huart->TxXferCount = 0U;
\r
1821 #if defined(USART_CR1_FIFOEN)
\r
1822 /* Flush the whole TX FIFO (if needed) */
\r
1823 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
1825 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
1827 #endif /* USART_CR1_FIFOEN */
\r
1829 /* Restore huart->gState to Ready */
\r
1830 huart->gState = HAL_UART_STATE_READY;
\r
1836 * @brief Abort ongoing Receive transfer (blocking mode).
\r
1837 * @param huart UART handle.
\r
1838 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
\r
1839 * This procedure performs following operations :
\r
1840 * - Disable UART Interrupts (Rx)
\r
1841 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
1842 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
\r
1843 * - Set handle State to READY
\r
1844 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
\r
1845 * @retval HAL status
\r
1847 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
\r
1849 #if defined(USART_CR1_FIFOEN)
\r
1850 /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
\r
1851 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
\r
1852 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
\r
1854 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
1855 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
\r
1856 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1857 #endif /* USART_CR1_FIFOEN */
\r
1859 /* Disable the UART DMA Rx request if enabled */
\r
1860 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
1862 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1864 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
\r
1865 if (huart->hdmarx != NULL)
\r
1867 /* Set the UART DMA Abort callback to Null.
\r
1868 No call back execution at end of DMA abort procedure */
\r
1869 huart->hdmarx->XferAbortCallback = NULL;
\r
1871 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
\r
1873 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
\r
1875 /* Set error code to DMA */
\r
1876 huart->ErrorCode = HAL_UART_ERROR_DMA;
\r
1878 return HAL_TIMEOUT;
\r
1884 /* Reset Rx transfer counter */
\r
1885 huart->RxXferCount = 0U;
\r
1887 /* Clear the Error flags in the ICR register */
\r
1888 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
1890 /* Discard the received data */
\r
1891 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
1893 /* Restore huart->RxState to Ready */
\r
1894 huart->RxState = HAL_UART_STATE_READY;
\r
1900 * @brief Abort ongoing transfers (Interrupt mode).
\r
1901 * @param huart UART handle.
\r
1902 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
\r
1903 * This procedure performs following operations :
\r
1904 * - Disable UART Interrupts (Tx and Rx)
\r
1905 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
1906 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
\r
1907 * - Set handle State to READY
\r
1908 * - At abort completion, call user abort complete callback
\r
1909 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
\r
1910 * considered as completed only when user abort complete callback is executed (not when exiting function).
\r
1911 * @retval HAL status
\r
1913 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
\r
1915 uint32_t abortcplt = 1U;
\r
1917 /* Disable interrupts */
\r
1918 #if defined(USART_CR1_FIFOEN)
\r
1919 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE | USART_CR1_TXEIE_TXFNFIE));
\r
1920 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
\r
1922 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
\r
1923 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
1924 #endif /* USART_CR1_FIFOEN */
\r
1926 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
\r
1927 before any call to DMA Abort functions */
\r
1928 /* DMA Tx Handle is valid */
\r
1929 if (huart->hdmatx != NULL)
\r
1931 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
\r
1932 Otherwise, set it to NULL */
\r
1933 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
\r
1935 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
\r
1939 huart->hdmatx->XferAbortCallback = NULL;
\r
1942 /* DMA Rx Handle is valid */
\r
1943 if (huart->hdmarx != NULL)
\r
1945 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
\r
1946 Otherwise, set it to NULL */
\r
1947 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
1949 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
\r
1953 huart->hdmarx->XferAbortCallback = NULL;
\r
1957 /* Disable the UART DMA Tx request if enabled */
\r
1958 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
\r
1960 /* Disable DMA Tx at UART level */
\r
1961 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
1963 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
\r
1964 if (huart->hdmatx != NULL)
\r
1966 /* UART Tx DMA Abort callback has already been initialised :
\r
1967 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
\r
1969 /* Abort DMA TX */
\r
1970 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
\r
1972 huart->hdmatx->XferAbortCallback = NULL;
\r
1981 /* Disable the UART DMA Rx request if enabled */
\r
1982 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
1984 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
1986 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
\r
1987 if (huart->hdmarx != NULL)
\r
1989 /* UART Rx DMA Abort callback has already been initialised :
\r
1990 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
\r
1992 /* Abort DMA RX */
\r
1993 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
\r
1995 huart->hdmarx->XferAbortCallback = NULL;
\r
2005 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
\r
2006 if (abortcplt == 1U)
\r
2008 /* Reset Tx and Rx transfer counters */
\r
2009 huart->TxXferCount = 0U;
\r
2010 huart->RxXferCount = 0U;
\r
2012 /* Clear ISR function pointers */
\r
2013 huart->RxISR = NULL;
\r
2014 huart->TxISR = NULL;
\r
2016 /* Reset errorCode */
\r
2017 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
2019 /* Clear the Error flags in the ICR register */
\r
2020 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
2022 #if defined(USART_CR1_FIFOEN)
\r
2023 /* Flush the whole TX FIFO (if needed) */
\r
2024 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
2026 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
2028 #endif /* USART_CR1_FIFOEN */
\r
2030 /* Discard the received data */
\r
2031 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
2033 /* Restore huart->gState and huart->RxState to Ready */
\r
2034 huart->gState = HAL_UART_STATE_READY;
\r
2035 huart->RxState = HAL_UART_STATE_READY;
\r
2037 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2038 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2039 /* Call registered Abort complete callback */
\r
2040 huart->AbortCpltCallback(huart);
\r
2042 /* Call legacy weak Abort complete callback */
\r
2043 HAL_UART_AbortCpltCallback(huart);
\r
2044 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2051 * @brief Abort ongoing Transmit transfer (Interrupt mode).
\r
2052 * @param huart UART handle.
\r
2053 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
\r
2054 * This procedure performs following operations :
\r
2055 * - Disable UART Interrupts (Tx)
\r
2056 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
2057 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
\r
2058 * - Set handle State to READY
\r
2059 * - At abort completion, call user abort complete callback
\r
2060 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
\r
2061 * considered as completed only when user abort complete callback is executed (not when exiting function).
\r
2062 * @retval HAL status
\r
2064 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
\r
2066 /* Disable interrupts */
\r
2067 #if defined(USART_CR1_FIFOEN)
\r
2068 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
\r
2069 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
\r
2071 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
\r
2072 #endif /* USART_CR1_FIFOEN */
\r
2074 /* Disable the UART DMA Tx request if enabled */
\r
2075 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
\r
2077 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
2079 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
\r
2080 if (huart->hdmatx != NULL)
\r
2082 /* Set the UART DMA Abort callback :
\r
2083 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
\r
2084 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
\r
2086 /* Abort DMA TX */
\r
2087 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
\r
2089 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
\r
2090 huart->hdmatx->XferAbortCallback(huart->hdmatx);
\r
2095 /* Reset Tx transfer counter */
\r
2096 huart->TxXferCount = 0U;
\r
2098 /* Clear TxISR function pointers */
\r
2099 huart->TxISR = NULL;
\r
2101 /* Restore huart->gState to Ready */
\r
2102 huart->gState = HAL_UART_STATE_READY;
\r
2104 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2105 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2106 /* Call registered Abort Transmit Complete Callback */
\r
2107 huart->AbortTransmitCpltCallback(huart);
\r
2109 /* Call legacy weak Abort Transmit Complete Callback */
\r
2110 HAL_UART_AbortTransmitCpltCallback(huart);
\r
2111 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2116 /* Reset Tx transfer counter */
\r
2117 huart->TxXferCount = 0U;
\r
2119 /* Clear TxISR function pointers */
\r
2120 huart->TxISR = NULL;
\r
2122 #if defined(USART_CR1_FIFOEN)
\r
2123 /* Flush the whole TX FIFO (if needed) */
\r
2124 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
2126 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
2128 #endif /* USART_CR1_FIFOEN */
\r
2130 /* Restore huart->gState to Ready */
\r
2131 huart->gState = HAL_UART_STATE_READY;
\r
2133 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2134 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2135 /* Call registered Abort Transmit Complete Callback */
\r
2136 huart->AbortTransmitCpltCallback(huart);
\r
2138 /* Call legacy weak Abort Transmit Complete Callback */
\r
2139 HAL_UART_AbortTransmitCpltCallback(huart);
\r
2140 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2147 * @brief Abort ongoing Receive transfer (Interrupt mode).
\r
2148 * @param huart UART handle.
\r
2149 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
\r
2150 * This procedure performs following operations :
\r
2151 * - Disable UART Interrupts (Rx)
\r
2152 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
2153 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
\r
2154 * - Set handle State to READY
\r
2155 * - At abort completion, call user abort complete callback
\r
2156 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
\r
2157 * considered as completed only when user abort complete callback is executed (not when exiting function).
\r
2158 * @retval HAL status
\r
2160 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
\r
2162 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
2163 #if defined(USART_CR1_FIFOEN)
\r
2164 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
\r
2165 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
\r
2167 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
\r
2168 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
2169 #endif /* USART_CR1_FIFOEN */
\r
2171 /* Disable the UART DMA Rx request if enabled */
\r
2172 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
2174 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
2176 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
\r
2177 if (huart->hdmarx != NULL)
\r
2179 /* Set the UART DMA Abort callback :
\r
2180 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
\r
2181 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
\r
2183 /* Abort DMA RX */
\r
2184 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
\r
2186 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
\r
2187 huart->hdmarx->XferAbortCallback(huart->hdmarx);
\r
2192 /* Reset Rx transfer counter */
\r
2193 huart->RxXferCount = 0U;
\r
2195 /* Clear RxISR function pointer */
\r
2196 huart->pRxBuffPtr = NULL;
\r
2198 /* Clear the Error flags in the ICR register */
\r
2199 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
2201 /* Discard the received data */
\r
2202 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
2204 /* Restore huart->RxState to Ready */
\r
2205 huart->RxState = HAL_UART_STATE_READY;
\r
2207 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2208 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2209 /* Call registered Abort Receive Complete Callback */
\r
2210 huart->AbortReceiveCpltCallback(huart);
\r
2212 /* Call legacy weak Abort Receive Complete Callback */
\r
2213 HAL_UART_AbortReceiveCpltCallback(huart);
\r
2214 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2219 /* Reset Rx transfer counter */
\r
2220 huart->RxXferCount = 0U;
\r
2222 /* Clear RxISR function pointer */
\r
2223 huart->pRxBuffPtr = NULL;
\r
2225 /* Clear the Error flags in the ICR register */
\r
2226 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
2228 /* Restore huart->RxState to Ready */
\r
2229 huart->RxState = HAL_UART_STATE_READY;
\r
2231 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2232 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2233 /* Call registered Abort Receive Complete Callback */
\r
2234 huart->AbortReceiveCpltCallback(huart);
\r
2236 /* Call legacy weak Abort Receive Complete Callback */
\r
2237 HAL_UART_AbortReceiveCpltCallback(huart);
\r
2238 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2245 * @brief Handle UART interrupt request.
\r
2246 * @param huart UART handle.
\r
2249 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
\r
2251 uint32_t isrflags = READ_REG(huart->Instance->ISR);
\r
2252 uint32_t cr1its = READ_REG(huart->Instance->CR1);
\r
2253 uint32_t cr3its = READ_REG(huart->Instance->CR3);
\r
2255 uint32_t errorflags;
\r
2256 uint32_t errorcode;
\r
2258 /* If no error occurs */
\r
2259 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
\r
2260 if (errorflags == 0U)
\r
2262 /* UART in mode Receiver ---------------------------------------------------*/
\r
2263 #if defined(USART_CR1_FIFOEN)
\r
2264 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
\r
2265 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
\r
2266 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
\r
2268 if (((isrflags & USART_ISR_RXNE) != 0U)
\r
2269 && ((cr1its & USART_CR1_RXNEIE) != 0U))
\r
2270 #endif /* USART_CR1_FIFOEN */
\r
2272 if (huart->RxISR != NULL)
\r
2274 huart->RxISR(huart);
\r
2280 /* If some errors occur */
\r
2281 #if defined(USART_CR1_FIFOEN)
\r
2282 if ((errorflags != 0U)
\r
2283 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
\r
2284 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U))))
\r
2286 if ((errorflags != 0U)
\r
2287 && (((cr3its & USART_CR3_EIE) != 0U)
\r
2288 || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
\r
2289 #endif /* USART_CR1_FIFOEN */
\r
2291 /* UART parity error interrupt occurred -------------------------------------*/
\r
2292 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
\r
2294 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
\r
2296 huart->ErrorCode |= HAL_UART_ERROR_PE;
\r
2299 /* UART frame error interrupt occurred --------------------------------------*/
\r
2300 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
\r
2302 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
\r
2304 huart->ErrorCode |= HAL_UART_ERROR_FE;
\r
2307 /* UART noise error interrupt occurred --------------------------------------*/
\r
2308 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
\r
2310 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
\r
2312 huart->ErrorCode |= HAL_UART_ERROR_NE;
\r
2315 /* UART Over-Run interrupt occurred -----------------------------------------*/
\r
2316 #if defined(USART_CR1_FIFOEN)
\r
2317 if (((isrflags & USART_ISR_ORE) != 0U)
\r
2318 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
\r
2319 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
\r
2321 if (((isrflags & USART_ISR_ORE) != 0U)
\r
2322 && (((cr1its & USART_CR1_RXNEIE) != 0U) ||
\r
2323 ((cr3its & USART_CR3_EIE) != 0U)))
\r
2324 #endif /* USART_CR1_FIFOEN */
\r
2326 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
\r
2328 huart->ErrorCode |= HAL_UART_ERROR_ORE;
\r
2331 /* Call UART Error Call back function if need be --------------------------*/
\r
2332 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
\r
2334 /* UART in mode Receiver ---------------------------------------------------*/
\r
2335 #if defined(USART_CR1_FIFOEN)
\r
2336 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
\r
2337 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
\r
2338 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
\r
2340 if (((isrflags & USART_ISR_RXNE) != 0U)
\r
2341 && ((cr1its & USART_CR1_RXNEIE) != 0U))
\r
2342 #endif /* USART_CR1_FIFOEN */
\r
2344 if (huart->RxISR != NULL)
\r
2346 huart->RxISR(huart);
\r
2350 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
\r
2351 consider error as blocking */
\r
2352 errorcode = huart->ErrorCode;
\r
2353 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
\r
2354 ((errorcode & HAL_UART_ERROR_ORE) != 0U))
\r
2356 /* Blocking error : transfer is aborted
\r
2357 Set the UART state ready to be able to start again the process,
\r
2358 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
\r
2359 UART_EndRxTransfer(huart);
\r
2361 /* Disable the UART DMA Rx request if enabled */
\r
2362 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
\r
2364 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
2366 /* Abort the UART DMA Rx channel */
\r
2367 if (huart->hdmarx != NULL)
\r
2369 /* Set the UART DMA Abort callback :
\r
2370 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
\r
2371 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
\r
2373 /* Abort DMA RX */
\r
2374 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
\r
2376 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
\r
2377 huart->hdmarx->XferAbortCallback(huart->hdmarx);
\r
2382 /* Call user error callback */
\r
2383 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2384 /*Call registered error callback*/
\r
2385 huart->ErrorCallback(huart);
\r
2387 /*Call legacy weak error callback*/
\r
2388 HAL_UART_ErrorCallback(huart);
\r
2389 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2395 /* Call user error callback */
\r
2396 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2397 /*Call registered error callback*/
\r
2398 huart->ErrorCallback(huart);
\r
2400 /*Call legacy weak error callback*/
\r
2401 HAL_UART_ErrorCallback(huart);
\r
2402 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2407 /* Non Blocking error : transfer could go on.
\r
2408 Error is notified to user through user error callback */
\r
2409 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2410 /*Call registered error callback*/
\r
2411 huart->ErrorCallback(huart);
\r
2413 /*Call legacy weak error callback*/
\r
2414 HAL_UART_ErrorCallback(huart);
\r
2415 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2416 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
2421 } /* End if some error occurs */
\r
2423 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
\r
2424 if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
\r
2426 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
\r
2428 /* UART Rx state is not reset as a reception process might be ongoing.
\r
2429 If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
\r
2431 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2432 /* Call registered Wakeup Callback */
\r
2433 huart->WakeupCallback(huart);
\r
2435 /* Call legacy weak Wakeup Callback */
\r
2436 HAL_UARTEx_WakeupCallback(huart);
\r
2437 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2441 /* UART in mode Transmitter ------------------------------------------------*/
\r
2442 #if defined(USART_CR1_FIFOEN)
\r
2443 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
\r
2444 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
\r
2445 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
\r
2447 if (((isrflags & USART_ISR_TXE) != 0U)
\r
2448 && ((cr1its & USART_CR1_TXEIE) != 0U))
\r
2449 #endif /* USART_CR1_FIFOEN */
\r
2451 if (huart->TxISR != NULL)
\r
2453 huart->TxISR(huart);
\r
2458 /* UART in mode Transmitter (transmission end) -----------------------------*/
\r
2459 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
\r
2461 UART_EndTransmit_IT(huart);
\r
2465 #if defined(USART_CR1_FIFOEN)
\r
2466 /* UART TX Fifo Empty occurred ----------------------------------------------*/
\r
2467 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
\r
2469 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2470 /* Call registered Tx Fifo Empty Callback */
\r
2471 huart->TxFifoEmptyCallback(huart);
\r
2473 /* Call legacy weak Tx Fifo Empty Callback */
\r
2474 HAL_UARTEx_TxFifoEmptyCallback(huart);
\r
2475 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2479 /* UART RX Fifo Full occurred ----------------------------------------------*/
\r
2480 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
\r
2482 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2483 /* Call registered Rx Fifo Full Callback */
\r
2484 huart->RxFifoFullCallback(huart);
\r
2486 /* Call legacy weak Rx Fifo Full Callback */
\r
2487 HAL_UARTEx_RxFifoFullCallback(huart);
\r
2488 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2491 #endif /* USART_CR1_FIFOEN */
\r
2495 * @brief Tx Transfer completed callback.
\r
2496 * @param huart UART handle.
\r
2499 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
\r
2501 /* Prevent unused argument(s) compilation warning */
\r
2504 /* NOTE : This function should not be modified, when the callback is needed,
\r
2505 the HAL_UART_TxCpltCallback can be implemented in the user file.
\r
2510 * @brief Tx Half Transfer completed callback.
\r
2511 * @param huart UART handle.
\r
2514 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
\r
2516 /* Prevent unused argument(s) compilation warning */
\r
2519 /* NOTE: This function should not be modified, when the callback is needed,
\r
2520 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
\r
2525 * @brief Rx Transfer completed callback.
\r
2526 * @param huart UART handle.
\r
2529 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
\r
2531 /* Prevent unused argument(s) compilation warning */
\r
2534 /* NOTE : This function should not be modified, when the callback is needed,
\r
2535 the HAL_UART_RxCpltCallback can be implemented in the user file.
\r
2540 * @brief Rx Half Transfer completed callback.
\r
2541 * @param huart UART handle.
\r
2544 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
\r
2546 /* Prevent unused argument(s) compilation warning */
\r
2549 /* NOTE: This function should not be modified, when the callback is needed,
\r
2550 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
\r
2555 * @brief UART error callback.
\r
2556 * @param huart UART handle.
\r
2559 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
\r
2561 /* Prevent unused argument(s) compilation warning */
\r
2564 /* NOTE : This function should not be modified, when the callback is needed,
\r
2565 the HAL_UART_ErrorCallback can be implemented in the user file.
\r
2570 * @brief UART Abort Complete callback.
\r
2571 * @param huart UART handle.
\r
2574 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
\r
2576 /* Prevent unused argument(s) compilation warning */
\r
2579 /* NOTE : This function should not be modified, when the callback is needed,
\r
2580 the HAL_UART_AbortCpltCallback can be implemented in the user file.
\r
2585 * @brief UART Abort Complete callback.
\r
2586 * @param huart UART handle.
\r
2589 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
\r
2591 /* Prevent unused argument(s) compilation warning */
\r
2594 /* NOTE : This function should not be modified, when the callback is needed,
\r
2595 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
\r
2600 * @brief UART Abort Receive Complete callback.
\r
2601 * @param huart UART handle.
\r
2604 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
\r
2606 /* Prevent unused argument(s) compilation warning */
\r
2609 /* NOTE : This function should not be modified, when the callback is needed,
\r
2610 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
\r
2618 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
\r
2619 * @brief UART control functions
\r
2622 ===============================================================================
\r
2623 ##### Peripheral Control functions #####
\r
2624 ===============================================================================
\r
2626 This subsection provides a set of functions allowing to control the UART.
\r
2627 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
\r
2628 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
\r
2629 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
\r
2630 (+) UART_SetConfig() API configures the UART peripheral
\r
2631 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
\r
2632 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
\r
2633 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
\r
2634 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
\r
2635 (+) HAL_LIN_SendBreak() API transmits the break characters
\r
2641 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
\r
2642 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
\r
2643 * @param huart UART handle.
\r
2644 * @retval HAL status
\r
2646 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
\r
2648 __HAL_LOCK(huart);
\r
2650 huart->gState = HAL_UART_STATE_BUSY;
\r
2652 /* Enable USART mute mode by setting the MME bit in the CR1 register */
\r
2653 SET_BIT(huart->Instance->CR1, USART_CR1_MME);
\r
2655 huart->gState = HAL_UART_STATE_READY;
\r
2657 return (UART_CheckIdleState(huart));
\r
2661 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
\r
2662 * as it may not have been in mute mode at this very moment).
\r
2663 * @param huart UART handle.
\r
2664 * @retval HAL status
\r
2666 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
\r
2668 __HAL_LOCK(huart);
\r
2670 huart->gState = HAL_UART_STATE_BUSY;
\r
2672 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
\r
2673 CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
\r
2675 huart->gState = HAL_UART_STATE_READY;
\r
2677 return (UART_CheckIdleState(huart));
\r
2681 * @brief Enter UART mute mode (means UART actually enters mute mode).
\r
2682 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
\r
2683 * @param huart UART handle.
\r
2686 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
\r
2688 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
\r
2692 * @brief Enable the UART transmitter and disable the UART receiver.
\r
2693 * @param huart UART handle.
\r
2694 * @retval HAL status
\r
2696 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
\r
2698 __HAL_LOCK(huart);
\r
2699 huart->gState = HAL_UART_STATE_BUSY;
\r
2701 /* Clear TE and RE bits */
\r
2702 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
\r
2704 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
\r
2705 SET_BIT(huart->Instance->CR1, USART_CR1_TE);
\r
2707 huart->gState = HAL_UART_STATE_READY;
\r
2709 __HAL_UNLOCK(huart);
\r
2715 * @brief Enable the UART receiver and disable the UART transmitter.
\r
2716 * @param huart UART handle.
\r
2717 * @retval HAL status.
\r
2719 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
\r
2721 __HAL_LOCK(huart);
\r
2722 huart->gState = HAL_UART_STATE_BUSY;
\r
2724 /* Clear TE and RE bits */
\r
2725 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
\r
2727 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
\r
2728 SET_BIT(huart->Instance->CR1, USART_CR1_RE);
\r
2730 huart->gState = HAL_UART_STATE_READY;
\r
2732 __HAL_UNLOCK(huart);
\r
2739 * @brief Transmit break characters.
\r
2740 * @param huart UART handle.
\r
2741 * @retval HAL status
\r
2743 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
\r
2745 /* Check the parameters */
\r
2746 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
\r
2748 __HAL_LOCK(huart);
\r
2750 huart->gState = HAL_UART_STATE_BUSY;
\r
2752 /* Send break characters */
\r
2753 __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
\r
2755 huart->gState = HAL_UART_STATE_READY;
\r
2757 __HAL_UNLOCK(huart);
\r
2766 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
\r
2767 * @brief UART Peripheral State functions
\r
2770 ==============================================================================
\r
2771 ##### Peripheral State and Error functions #####
\r
2772 ==============================================================================
\r
2774 This subsection provides functions allowing to :
\r
2775 (+) Return the UART handle state.
\r
2776 (+) Return the UART handle error code
\r
2783 * @brief Return the UART handle state.
\r
2784 * @param huart Pointer to a UART_HandleTypeDef structure that contains
\r
2785 * the configuration information for the specified UART.
\r
2786 * @retval HAL state
\r
2788 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
\r
2792 temp1 = huart->gState;
\r
2793 temp2 = huart->RxState;
\r
2795 return (HAL_UART_StateTypeDef)(temp1 | temp2);
\r
2799 * @brief Return the UART handle error code.
\r
2800 * @param huart Pointer to a UART_HandleTypeDef structure that contains
\r
2801 * the configuration information for the specified UART.
\r
2802 * @retval UART Error Code
\r
2804 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
\r
2806 return huart->ErrorCode;
\r
2816 /** @defgroup UART_Private_Functions UART Private Functions
\r
2821 * @brief Initialize the callbacks to their default values.
\r
2822 * @param huart UART handle.
\r
2825 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
2826 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
\r
2828 /* Init the UART Callback settings */
\r
2829 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
\r
2830 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
\r
2831 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
\r
2832 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
\r
2833 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
\r
2834 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
2835 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
\r
2836 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
\r
2837 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
\r
2838 #if defined(USART_CR1_FIFOEN)
\r
2839 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
\r
2840 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
\r
2841 #endif /* USART_CR1_FIFOEN */
\r
2844 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
2847 * @brief Configure the UART peripheral.
\r
2848 * @param huart UART handle.
\r
2849 * @retval HAL status
\r
2851 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
\r
2855 UART_ClockSourceTypeDef clocksource;
\r
2856 uint32_t usartdiv = 0x00000000U;
\r
2857 HAL_StatusTypeDef ret = HAL_OK;
\r
2858 uint32_t lpuart_ker_ck_pres = 0x00000000U;
\r
2860 /* Check the parameters */
\r
2861 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
\r
2862 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
\r
2863 if (UART_INSTANCE_LOWPOWER(huart))
\r
2865 assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
\r
2869 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
\r
2870 assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
\r
2873 assert_param(IS_UART_PARITY(huart->Init.Parity));
\r
2874 assert_param(IS_UART_MODE(huart->Init.Mode));
\r
2875 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
\r
2876 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
\r
2877 #if defined(USART_PRESC_PRESCALER)
\r
2878 assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
\r
2879 #endif /* USART_PRESC_PRESCALER */
\r
2881 /*-------------------------- USART CR1 Configuration -----------------------*/
\r
2882 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
\r
2883 * the UART Word Length, Parity, Mode and oversampling:
\r
2884 * set the M bits according to huart->Init.WordLength value
\r
2885 * set PCE and PS bits according to huart->Init.Parity value
\r
2886 * set TE and RE bits according to huart->Init.Mode value
\r
2887 * set OVER8 bit according to huart->Init.OverSampling value */
\r
2888 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
\r
2889 #if defined(USART_CR1_FIFOEN)
\r
2890 tmpreg |= (uint32_t)huart->FifoMode;
\r
2891 #endif /* USART_CR1_FIFOEN */
\r
2892 MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
\r
2894 /*-------------------------- USART CR2 Configuration -----------------------*/
\r
2895 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
\r
2896 * to huart->Init.StopBits value */
\r
2897 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
\r
2899 /*-------------------------- USART CR3 Configuration -----------------------*/
\r
2901 * - UART HardWare Flow Control: set CTSE and RTSE bits according
\r
2902 * to huart->Init.HwFlowCtl value
\r
2903 * - one-bit sampling method versus three samples' majority rule according
\r
2904 * to huart->Init.OneBitSampling (not applicable to LPUART) */
\r
2905 tmpreg = (uint32_t)huart->Init.HwFlowCtl;
\r
2907 if (!(UART_INSTANCE_LOWPOWER(huart)))
\r
2909 tmpreg |= huart->Init.OneBitSampling;
\r
2911 MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
\r
2913 #if defined(USART_PRESC_PRESCALER)
\r
2914 /*-------------------------- USART PRESC Configuration -----------------------*/
\r
2916 * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
\r
2917 MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
\r
2918 #endif /* USART_PRESC_PRESCALER */
\r
2920 /*-------------------------- USART BRR Configuration -----------------------*/
\r
2921 UART_GETCLOCKSOURCE(huart, clocksource);
\r
2923 /* Check LPUART instance */
\r
2924 if (UART_INSTANCE_LOWPOWER(huart))
\r
2926 /* Retrieve frequency clock */
\r
2927 switch (clocksource)
\r
2929 case UART_CLOCKSOURCE_PCLK1:
\r
2930 #if defined(USART_PRESC_PRESCALER)
\r
2931 lpuart_ker_ck_pres = (HAL_RCC_GetPCLK1Freq() / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
\r
2933 lpuart_ker_ck_pres = HAL_RCC_GetPCLK1Freq();
\r
2934 #endif /* USART_PRESC_PRESCALER */
\r
2936 case UART_CLOCKSOURCE_HSI:
\r
2937 #if defined(USART_PRESC_PRESCALER)
\r
2938 lpuart_ker_ck_pres = ((uint32_t)HSI_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
\r
2940 lpuart_ker_ck_pres = (uint32_t)HSI_VALUE;
\r
2941 #endif /* USART_PRESC_PRESCALER */
\r
2943 case UART_CLOCKSOURCE_SYSCLK:
\r
2944 #if defined(USART_PRESC_PRESCALER)
\r
2945 lpuart_ker_ck_pres = (HAL_RCC_GetSysClockFreq() / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
\r
2947 lpuart_ker_ck_pres = HAL_RCC_GetSysClockFreq();
\r
2948 #endif /* USART_PRESC_PRESCALER */
\r
2950 case UART_CLOCKSOURCE_LSE:
\r
2951 #if defined(USART_PRESC_PRESCALER)
\r
2952 lpuart_ker_ck_pres = ((uint32_t)LSE_VALUE / UART_GET_DIV_FACTOR(huart->Init.ClockPrescaler));
\r
2954 lpuart_ker_ck_pres = (uint32_t)LSE_VALUE;
\r
2955 #endif /* USART_PRESC_PRESCALER */
\r
2957 case UART_CLOCKSOURCE_UNDEFINED:
\r
2963 /* if proper clock source reported */
\r
2964 if (lpuart_ker_ck_pres != 0U)
\r
2966 /* ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
\r
2967 if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
\r
2968 (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
\r
2974 switch (clocksource)
\r
2976 case UART_CLOCKSOURCE_PCLK1:
\r
2977 #if defined(USART_PRESC_PRESCALER)
\r
2978 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
2980 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
\r
2981 #endif /* USART_PRESC_PRESCALER */
\r
2983 case UART_CLOCKSOURCE_HSI:
\r
2984 #if defined(USART_PRESC_PRESCALER)
\r
2985 usartdiv = (uint32_t)(UART_DIV_LPUART(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
2987 usartdiv = (uint32_t)(UART_DIV_LPUART(HSI_VALUE, huart->Init.BaudRate));
\r
2988 #endif /* USART_PRESC_PRESCALER */
\r
2990 case UART_CLOCKSOURCE_SYSCLK:
\r
2991 #if defined(USART_PRESC_PRESCALER)
\r
2992 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
2994 usartdiv = (uint32_t)(UART_DIV_LPUART(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
\r
2995 #endif /* USART_PRESC_PRESCALER */
\r
2997 case UART_CLOCKSOURCE_LSE:
\r
2998 #if defined(USART_PRESC_PRESCALER)
\r
2999 usartdiv = (uint32_t)(UART_DIV_LPUART(LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3001 usartdiv = (uint32_t)(UART_DIV_LPUART(LSE_VALUE, huart->Init.BaudRate));
\r
3002 #endif /* USART_PRESC_PRESCALER */
\r
3004 case UART_CLOCKSOURCE_UNDEFINED:
\r
3010 /* It is forbidden to write values lower than 0x300 in the LPUART_BRR register */
\r
3011 if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
\r
3013 huart->Instance->BRR = usartdiv;
\r
3019 } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) || (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
\r
3020 } /* if (lpuart_ker_ck_pres != 0) */
\r
3022 /* Check UART Over Sampling to set Baud Rate Register */
\r
3023 else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
\r
3025 switch (clocksource)
\r
3027 case UART_CLOCKSOURCE_PCLK1:
\r
3028 #if defined(USART_PRESC_PRESCALER)
\r
3029 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3031 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
\r
3032 #endif /* USART_PRESC_PRESCALER */
\r
3034 case UART_CLOCKSOURCE_PCLK2:
\r
3035 #if defined(USART_PRESC_PRESCALER)
\r
3036 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3038 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
\r
3039 #endif /* USART_PRESC_PRESCALER */
\r
3041 case UART_CLOCKSOURCE_HSI:
\r
3042 #if defined(USART_PRESC_PRESCALER)
\r
3043 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3045 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
\r
3046 #endif /* USART_PRESC_PRESCALER */
\r
3048 case UART_CLOCKSOURCE_SYSCLK:
\r
3049 #if defined(USART_PRESC_PRESCALER)
\r
3050 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3052 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
\r
3053 #endif /* USART_PRESC_PRESCALER */
\r
3055 case UART_CLOCKSOURCE_LSE:
\r
3056 #if defined(USART_PRESC_PRESCALER)
\r
3057 usartdiv = (uint16_t)(UART_DIV_SAMPLING8((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3059 usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
\r
3060 #endif /* USART_PRESC_PRESCALER */
\r
3062 case UART_CLOCKSOURCE_UNDEFINED:
\r
3068 /* USARTDIV must be greater than or equal to 0d16 */
\r
3069 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
\r
3071 brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
\r
3072 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
\r
3073 huart->Instance->BRR = brrtemp;
\r
3082 switch (clocksource)
\r
3084 case UART_CLOCKSOURCE_PCLK1:
\r
3085 #if defined(USART_PRESC_PRESCALER)
\r
3086 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3088 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate));
\r
3089 #endif /* USART_PRESC_PRESCALER */
\r
3091 case UART_CLOCKSOURCE_PCLK2:
\r
3092 #if defined(USART_PRESC_PRESCALER)
\r
3093 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3095 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate));
\r
3096 #endif /* USART_PRESC_PRESCALER */
\r
3098 case UART_CLOCKSOURCE_HSI:
\r
3099 #if defined(USART_PRESC_PRESCALER)
\r
3100 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3102 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
\r
3103 #endif /* USART_PRESC_PRESCALER */
\r
3105 case UART_CLOCKSOURCE_SYSCLK:
\r
3106 #if defined(USART_PRESC_PRESCALER)
\r
3107 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3109 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart->Init.BaudRate));
\r
3110 #endif /* USART_PRESC_PRESCALER */
\r
3112 case UART_CLOCKSOURCE_LSE:
\r
3113 #if defined(USART_PRESC_PRESCALER)
\r
3114 usartdiv = (uint16_t)(UART_DIV_SAMPLING16((uint32_t)LSE_VALUE, huart->Init.BaudRate, huart->Init.ClockPrescaler));
\r
3116 usartdiv = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
\r
3117 #endif /* USART_PRESC_PRESCALER */
\r
3119 case UART_CLOCKSOURCE_UNDEFINED:
\r
3125 /* USARTDIV must be greater than or equal to 0d16 */
\r
3126 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
\r
3128 huart->Instance->BRR = usartdiv;
\r
3136 #if defined(USART_CR1_FIFOEN)
\r
3137 /* Initialize the number of data to process during RX/TX ISR execution */
\r
3138 huart->NbTxDataToProcess = 1;
\r
3139 huart->NbRxDataToProcess = 1;
\r
3140 #endif /* USART_CR1_FIFOEN */
\r
3142 /* Clear ISR function pointers */
\r
3143 huart->RxISR = NULL;
\r
3144 huart->TxISR = NULL;
\r
3150 * @brief Configure the UART peripheral advanced features.
\r
3151 * @param huart UART handle.
\r
3154 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
\r
3156 /* Check whether the set of advanced features to configure is properly set */
\r
3157 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
\r
3159 /* if required, configure TX pin active level inversion */
\r
3160 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
\r
3162 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
\r
3163 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
\r
3166 /* if required, configure RX pin active level inversion */
\r
3167 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
\r
3169 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
\r
3170 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
\r
3173 /* if required, configure data inversion */
\r
3174 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
\r
3176 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
\r
3177 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
\r
3180 /* if required, configure RX/TX pins swap */
\r
3181 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
\r
3183 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
\r
3184 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
\r
3187 /* if required, configure RX overrun detection disabling */
\r
3188 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
\r
3190 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
\r
3191 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
\r
3194 /* if required, configure DMA disabling on reception error */
\r
3195 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
\r
3197 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
\r
3198 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
\r
3201 /* if required, configure auto Baud rate detection scheme */
\r
3202 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
\r
3204 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
\r
3205 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
\r
3206 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
\r
3207 /* set auto Baudrate detection parameters if detection is enabled */
\r
3208 if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
\r
3210 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
\r
3211 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
\r
3215 /* if required, configure MSB first on communication line */
\r
3216 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
\r
3218 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
\r
3219 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
\r
3224 * @brief Check the UART Idle State.
\r
3225 * @param huart UART handle.
\r
3226 * @retval HAL status
\r
3228 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
\r
3230 uint32_t tickstart;
\r
3232 /* Initialize the UART ErrorCode */
\r
3233 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
3235 /* Init tickstart for timeout managment*/
\r
3236 tickstart = HAL_GetTick();
\r
3238 /* Check if the Transmitter is enabled */
\r
3239 if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
\r
3241 /* Wait until TEACK flag is set */
\r
3242 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
\r
3244 /* Timeout occurred */
\r
3245 return HAL_TIMEOUT;
\r
3249 /* Check if the Receiver is enabled */
\r
3250 if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
\r
3252 /* Wait until REACK flag is set */
\r
3253 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
\r
3255 /* Timeout occurred */
\r
3256 return HAL_TIMEOUT;
\r
3260 /* Initialize the UART State */
\r
3261 huart->gState = HAL_UART_STATE_READY;
\r
3262 huart->RxState = HAL_UART_STATE_READY;
\r
3264 __HAL_UNLOCK(huart);
\r
3270 * @brief Handle UART Communication Timeout.
\r
3271 * @param huart UART handle.
\r
3272 * @param Flag Specifies the UART flag to check
\r
3273 * @param Status Flag status (SET or RESET)
\r
3274 * @param Tickstart Tick start value
\r
3275 * @param Timeout Timeout duration
\r
3276 * @retval HAL status
\r
3278 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
\r
3279 uint32_t Tickstart, uint32_t Timeout)
\r
3281 /* Wait until flag is set */
\r
3282 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
\r
3284 /* Check for the Timeout */
\r
3285 if (Timeout != HAL_MAX_DELAY)
\r
3287 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
\r
3289 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
\r
3290 #if defined(USART_CR1_FIFOEN)
\r
3291 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
\r
3293 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
\r
3294 #endif /* USART_CR1_FIFOEN */
\r
3295 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
3297 huart->gState = HAL_UART_STATE_READY;
\r
3298 huart->RxState = HAL_UART_STATE_READY;
\r
3300 __HAL_UNLOCK(huart);
\r
3302 return HAL_TIMEOUT;
\r
3311 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
\r
3312 * @param huart UART handle.
\r
3315 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
\r
3317 #if defined(USART_CR1_FIFOEN)
\r
3318 /* Disable TXEIE, TCIE, TXFT interrupts */
\r
3319 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
\r
3320 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
\r
3322 /* Disable TXEIE and TCIE interrupts */
\r
3323 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
\r
3324 #endif /* USART_CR1_FIFOEN */
\r
3326 /* At end of Tx process, restore huart->gState to Ready */
\r
3327 huart->gState = HAL_UART_STATE_READY;
\r
3332 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
\r
3333 * @param huart UART handle.
\r
3336 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
\r
3338 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
3339 #if defined(USART_CR1_FIFOEN)
\r
3340 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
\r
3341 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
\r
3343 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
\r
3344 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
3345 #endif /* USART_CR1_FIFOEN */
\r
3347 /* At end of Rx process, restore huart->RxState to Ready */
\r
3348 huart->RxState = HAL_UART_STATE_READY;
\r
3350 /* Reset RxIsr function pointer */
\r
3351 huart->RxISR = NULL;
\r
3356 * @brief DMA UART transmit process complete callback.
\r
3357 * @param hdma DMA handle.
\r
3360 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
\r
3362 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3364 /* DMA Normal mode */
\r
3365 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
\r
3367 huart->TxXferCount = 0U;
\r
3369 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
\r
3370 in the UART CR3 register */
\r
3371 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
\r
3373 /* Enable the UART Transmit Complete Interrupt */
\r
3374 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3376 /* DMA Circular mode */
\r
3379 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3380 /*Call registered Tx complete callback*/
\r
3381 huart->TxCpltCallback(huart);
\r
3383 /*Call legacy weak Tx complete callback*/
\r
3384 HAL_UART_TxCpltCallback(huart);
\r
3385 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3390 * @brief DMA UART transmit process half complete callback.
\r
3391 * @param hdma DMA handle.
\r
3394 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
\r
3396 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3398 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3399 /*Call registered Tx Half complete callback*/
\r
3400 huart->TxHalfCpltCallback(huart);
\r
3402 /*Call legacy weak Tx Half complete callback*/
\r
3403 HAL_UART_TxHalfCpltCallback(huart);
\r
3404 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3408 * @brief DMA UART receive process complete callback.
\r
3409 * @param hdma DMA handle.
\r
3412 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
\r
3414 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3416 /* DMA Normal mode */
\r
3417 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
\r
3419 huart->RxXferCount = 0U;
\r
3421 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
\r
3422 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
3423 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
3425 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
\r
3426 in the UART CR3 register */
\r
3427 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
\r
3429 /* At end of Rx process, restore huart->RxState to Ready */
\r
3430 huart->RxState = HAL_UART_STATE_READY;
\r
3433 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3434 /*Call registered Rx complete callback*/
\r
3435 huart->RxCpltCallback(huart);
\r
3437 /*Call legacy weak Rx complete callback*/
\r
3438 HAL_UART_RxCpltCallback(huart);
\r
3439 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3443 * @brief DMA UART receive process half complete callback.
\r
3444 * @param hdma DMA handle.
\r
3447 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
\r
3449 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3451 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3452 /*Call registered Rx Half complete callback*/
\r
3453 huart->RxHalfCpltCallback(huart);
\r
3455 /*Call legacy weak Rx Half complete callback*/
\r
3456 HAL_UART_RxHalfCpltCallback(huart);
\r
3457 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3461 * @brief DMA UART communication error callback.
\r
3462 * @param hdma DMA handle.
\r
3465 static void UART_DMAError(DMA_HandleTypeDef *hdma)
\r
3467 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3469 const HAL_UART_StateTypeDef gstate = huart->gState;
\r
3470 const HAL_UART_StateTypeDef rxstate = huart->RxState;
\r
3472 /* Stop UART DMA Tx request if ongoing */
\r
3473 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
\r
3474 (gstate == HAL_UART_STATE_BUSY_TX))
\r
3476 huart->TxXferCount = 0U;
\r
3477 UART_EndTxTransfer(huart);
\r
3480 /* Stop UART DMA Rx request if ongoing */
\r
3481 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
\r
3482 (rxstate == HAL_UART_STATE_BUSY_RX))
\r
3484 huart->RxXferCount = 0U;
\r
3485 UART_EndRxTransfer(huart);
\r
3488 huart->ErrorCode |= HAL_UART_ERROR_DMA;
\r
3490 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3491 /*Call registered error callback*/
\r
3492 huart->ErrorCallback(huart);
\r
3494 /*Call legacy weak error callback*/
\r
3495 HAL_UART_ErrorCallback(huart);
\r
3496 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3500 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
\r
3501 * (To be called at end of DMA Abort procedure following error occurrence).
\r
3502 * @param hdma DMA handle.
\r
3505 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
\r
3507 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3508 huart->RxXferCount = 0U;
\r
3509 huart->TxXferCount = 0U;
\r
3511 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3512 /*Call registered error callback*/
\r
3513 huart->ErrorCallback(huart);
\r
3515 /*Call legacy weak error callback*/
\r
3516 HAL_UART_ErrorCallback(huart);
\r
3517 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3521 * @brief DMA UART Tx communication abort callback, when initiated by user
\r
3522 * (To be called at end of DMA Tx Abort procedure following user abort request).
\r
3523 * @note When this callback is executed, User Abort complete call back is called only if no
\r
3524 * Abort still ongoing for Rx DMA Handle.
\r
3525 * @param hdma DMA handle.
\r
3528 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
\r
3530 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3532 huart->hdmatx->XferAbortCallback = NULL;
\r
3534 /* Check if an Abort process is still ongoing */
\r
3535 if (huart->hdmarx != NULL)
\r
3537 if (huart->hdmarx->XferAbortCallback != NULL)
\r
3543 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
\r
3544 huart->TxXferCount = 0U;
\r
3545 huart->RxXferCount = 0U;
\r
3547 /* Reset errorCode */
\r
3548 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
3550 /* Clear the Error flags in the ICR register */
\r
3551 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
3553 #if defined(USART_CR1_FIFOEN)
\r
3554 /* Flush the whole TX FIFO (if needed) */
\r
3555 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
3557 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
3559 #endif /* USART_CR1_FIFOEN */
\r
3561 /* Restore huart->gState and huart->RxState to Ready */
\r
3562 huart->gState = HAL_UART_STATE_READY;
\r
3563 huart->RxState = HAL_UART_STATE_READY;
\r
3565 /* Call user Abort complete callback */
\r
3566 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3567 /* Call registered Abort complete callback */
\r
3568 huart->AbortCpltCallback(huart);
\r
3570 /* Call legacy weak Abort complete callback */
\r
3571 HAL_UART_AbortCpltCallback(huart);
\r
3572 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3577 * @brief DMA UART Rx communication abort callback, when initiated by user
\r
3578 * (To be called at end of DMA Rx Abort procedure following user abort request).
\r
3579 * @note When this callback is executed, User Abort complete call back is called only if no
\r
3580 * Abort still ongoing for Tx DMA Handle.
\r
3581 * @param hdma DMA handle.
\r
3584 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
\r
3586 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3588 huart->hdmarx->XferAbortCallback = NULL;
\r
3590 /* Check if an Abort process is still ongoing */
\r
3591 if (huart->hdmatx != NULL)
\r
3593 if (huart->hdmatx->XferAbortCallback != NULL)
\r
3599 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
\r
3600 huart->TxXferCount = 0U;
\r
3601 huart->RxXferCount = 0U;
\r
3603 /* Reset errorCode */
\r
3604 huart->ErrorCode = HAL_UART_ERROR_NONE;
\r
3606 /* Clear the Error flags in the ICR register */
\r
3607 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
3609 /* Discard the received data */
\r
3610 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
3612 /* Restore huart->gState and huart->RxState to Ready */
\r
3613 huart->gState = HAL_UART_STATE_READY;
\r
3614 huart->RxState = HAL_UART_STATE_READY;
\r
3616 /* Call user Abort complete callback */
\r
3617 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3618 /* Call registered Abort complete callback */
\r
3619 huart->AbortCpltCallback(huart);
\r
3621 /* Call legacy weak Abort complete callback */
\r
3622 HAL_UART_AbortCpltCallback(huart);
\r
3623 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3628 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
\r
3629 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
\r
3630 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
\r
3631 * and leads to user Tx Abort Complete callback execution).
\r
3632 * @param hdma DMA handle.
\r
3635 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
\r
3637 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
\r
3639 huart->TxXferCount = 0U;
\r
3641 #if defined(USART_CR1_FIFOEN)
\r
3642 /* Flush the whole TX FIFO (if needed) */
\r
3643 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
\r
3645 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
\r
3647 #endif /* USART_CR1_FIFOEN */
\r
3649 /* Restore huart->gState to Ready */
\r
3650 huart->gState = HAL_UART_STATE_READY;
\r
3652 /* Call user Abort complete callback */
\r
3653 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3654 /* Call registered Abort Transmit Complete Callback */
\r
3655 huart->AbortTransmitCpltCallback(huart);
\r
3657 /* Call legacy weak Abort Transmit Complete Callback */
\r
3658 HAL_UART_AbortTransmitCpltCallback(huart);
\r
3659 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3663 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
\r
3664 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
\r
3665 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
\r
3666 * and leads to user Rx Abort Complete callback execution).
\r
3667 * @param hdma DMA handle.
\r
3670 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
\r
3672 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
\r
3674 huart->RxXferCount = 0U;
\r
3676 /* Clear the Error flags in the ICR register */
\r
3677 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
\r
3679 /* Discard the received data */
\r
3680 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
3682 /* Restore huart->RxState to Ready */
\r
3683 huart->RxState = HAL_UART_STATE_READY;
\r
3685 /* Call user Abort complete callback */
\r
3686 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3687 /* Call registered Abort Receive Complete Callback */
\r
3688 huart->AbortReceiveCpltCallback(huart);
\r
3690 /* Call legacy weak Abort Receive Complete Callback */
\r
3691 HAL_UART_AbortReceiveCpltCallback(huart);
\r
3692 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3696 * @brief TX interrrupt handler for 7 or 8 bits data word length .
\r
3697 * @note Function is called under interruption only, once
\r
3698 * interruptions have been enabled by HAL_UART_Transmit_IT().
\r
3699 * @param huart UART handle.
\r
3702 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
\r
3704 /* Check that a Tx process is ongoing */
\r
3705 if (huart->gState == HAL_UART_STATE_BUSY_TX)
\r
3707 if (huart->TxXferCount == 0U)
\r
3709 /* Disable the UART Transmit Data Register Empty Interrupt */
\r
3710 #if defined(USART_CR1_FIFOEN)
\r
3711 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
\r
3713 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
\r
3714 #endif /* USART_CR1_FIFOEN */
\r
3716 /* Enable the UART Transmit Complete Interrupt */
\r
3717 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3721 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
\r
3722 huart->pTxBuffPtr++;
\r
3723 huart->TxXferCount--;
\r
3729 * @brief TX interrrupt handler for 9 bits data word length.
\r
3730 * @note Function is called under interruption only, once
\r
3731 * interruptions have been enabled by HAL_UART_Transmit_IT().
\r
3732 * @param huart UART handle.
\r
3735 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
\r
3739 /* Check that a Tx process is ongoing */
\r
3740 if (huart->gState == HAL_UART_STATE_BUSY_TX)
\r
3742 if (huart->TxXferCount == 0U)
\r
3744 /* Disable the UART Transmit Data Register Empty Interrupt */
\r
3745 #if defined(USART_CR1_FIFOEN)
\r
3746 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
\r
3748 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
\r
3749 #endif /* USART_CR1_FIFOEN */
\r
3751 /* Enable the UART Transmit Complete Interrupt */
\r
3752 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3756 tmp = (uint16_t *) huart->pTxBuffPtr;
\r
3757 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
\r
3758 huart->pTxBuffPtr += 2U;
\r
3759 huart->TxXferCount--;
\r
3764 #if defined(USART_CR1_FIFOEN)
\r
3766 * @brief TX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
\r
3767 * @note Function is called under interruption only, once
\r
3768 * interruptions have been enabled by HAL_UART_Transmit_IT().
\r
3769 * @param huart UART handle.
\r
3772 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
\r
3774 uint16_t nb_tx_data;
\r
3776 /* Check that a Tx process is ongoing */
\r
3777 if (huart->gState == HAL_UART_STATE_BUSY_TX)
\r
3779 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
\r
3781 if (huart->TxXferCount == 0U)
\r
3783 /* Disable the TX FIFO threshold interrupt */
\r
3784 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
\r
3786 /* Enable the UART Transmit Complete Interrupt */
\r
3787 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3789 break; /* force exit loop */
\r
3791 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
\r
3793 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
\r
3794 huart->pTxBuffPtr++;
\r
3795 huart->TxXferCount--;
\r
3799 /* Nothing to do */
\r
3806 * @brief TX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
\r
3807 * @note Function is called under interruption only, once
\r
3808 * interruptions have been enabled by HAL_UART_Transmit_IT().
\r
3809 * @param huart UART handle.
\r
3812 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
\r
3815 uint16_t nb_tx_data;
\r
3817 /* Check that a Tx process is ongoing */
\r
3818 if (huart->gState == HAL_UART_STATE_BUSY_TX)
\r
3820 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
\r
3822 if (huart->TxXferCount == 0U)
\r
3824 /* Disable the TX FIFO threshold interrupt */
\r
3825 CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
\r
3827 /* Enable the UART Transmit Complete Interrupt */
\r
3828 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3830 break; /* force exit loop */
\r
3832 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
\r
3834 tmp = (uint16_t *) huart->pTxBuffPtr;
\r
3835 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
\r
3836 huart->pTxBuffPtr += 2U;
\r
3837 huart->TxXferCount--;
\r
3841 /* Nothing to do */
\r
3846 #endif /* USART_CR1_FIFOEN */
\r
3849 * @brief Wrap up transmission in non-blocking mode.
\r
3850 * @param huart pointer to a UART_HandleTypeDef structure that contains
\r
3851 * the configuration information for the specified UART module.
\r
3854 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
\r
3856 /* Disable the UART Transmit Complete Interrupt */
\r
3857 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
\r
3859 /* Tx process is ended, restore huart->gState to Ready */
\r
3860 huart->gState = HAL_UART_STATE_READY;
\r
3862 /* Cleat TxISR function pointer */
\r
3863 huart->TxISR = NULL;
\r
3865 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3866 /*Call registered Tx complete callback*/
\r
3867 huart->TxCpltCallback(huart);
\r
3869 /*Call legacy weak Tx complete callback*/
\r
3870 HAL_UART_TxCpltCallback(huart);
\r
3871 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3875 * @brief RX interrrupt handler for 7 or 8 bits data word length .
\r
3876 * @param huart UART handle.
\r
3879 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
\r
3881 uint16_t uhMask = huart->Mask;
\r
3884 /* Check that a Rx process is ongoing */
\r
3885 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
\r
3887 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
\r
3888 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
\r
3889 huart->pRxBuffPtr++;
\r
3890 huart->RxXferCount--;
\r
3892 if (huart->RxXferCount == 0U)
\r
3894 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
\r
3895 #if defined(USART_CR1_FIFOEN)
\r
3896 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
\r
3898 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
\r
3899 #endif /* USART_CR1_FIFOEN */
\r
3901 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
\r
3902 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
3904 /* Rx process is completed, restore huart->RxState to Ready */
\r
3905 huart->RxState = HAL_UART_STATE_READY;
\r
3907 /* Clear RxISR function pointer */
\r
3908 huart->RxISR = NULL;
\r
3910 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3911 /*Call registered Rx complete callback*/
\r
3912 huart->RxCpltCallback(huart);
\r
3914 /*Call legacy weak Rx complete callback*/
\r
3915 HAL_UART_RxCpltCallback(huart);
\r
3916 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3921 /* Clear RXNE interrupt flag */
\r
3922 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
3927 * @brief RX interrrupt handler for 9 bits data word length .
\r
3928 * @note Function is called under interruption only, once
\r
3929 * interruptions have been enabled by HAL_UART_Receive_IT()
\r
3930 * @param huart UART handle.
\r
3933 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
\r
3936 uint16_t uhMask = huart->Mask;
\r
3939 /* Check that a Rx process is ongoing */
\r
3940 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
\r
3942 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
\r
3943 tmp = (uint16_t *) huart->pRxBuffPtr ;
\r
3944 *tmp = (uint16_t)(uhdata & uhMask);
\r
3945 huart->pRxBuffPtr += 2U;
\r
3946 huart->RxXferCount--;
\r
3948 if (huart->RxXferCount == 0U)
\r
3950 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
\r
3951 #if defined(USART_CR1_FIFOEN)
\r
3952 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
\r
3954 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
\r
3955 #endif /* USART_CR1_FIFOEN */
\r
3957 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
\r
3958 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
\r
3960 /* Rx process is completed, restore huart->RxState to Ready */
\r
3961 huart->RxState = HAL_UART_STATE_READY;
\r
3963 /* Clear RxISR function pointer */
\r
3964 huart->RxISR = NULL;
\r
3966 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
3967 /*Call registered Rx complete callback*/
\r
3968 huart->RxCpltCallback(huart);
\r
3970 /*Call legacy weak Rx complete callback*/
\r
3971 HAL_UART_RxCpltCallback(huart);
\r
3972 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
3977 /* Clear RXNE interrupt flag */
\r
3978 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
3982 #if defined(USART_CR1_FIFOEN)
\r
3984 * @brief RX interrrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
\r
3985 * @note Function is called under interruption only, once
\r
3986 * interruptions have been enabled by HAL_UART_Receive_IT()
\r
3987 * @param huart UART handle.
\r
3990 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
\r
3992 uint16_t uhMask = huart->Mask;
\r
3994 uint16_t nb_rx_data;
\r
3995 uint16_t rxdatacount;
\r
3997 /* Check that a Rx process is ongoing */
\r
3998 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
\r
4000 for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
\r
4002 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
\r
4003 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
\r
4004 huart->pRxBuffPtr++;
\r
4005 huart->RxXferCount--;
\r
4007 if (huart->RxXferCount == 0U)
\r
4009 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
\r
4010 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
4012 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
\r
4013 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
\r
4015 /* Rx process is completed, restore huart->RxState to Ready */
\r
4016 huart->RxState = HAL_UART_STATE_READY;
\r
4018 /* Clear RxISR function pointer */
\r
4019 huart->RxISR = NULL;
\r
4021 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
4022 /*Call registered Rx complete callback*/
\r
4023 huart->RxCpltCallback(huart);
\r
4025 /*Call legacy weak Rx complete callback*/
\r
4026 HAL_UART_RxCpltCallback(huart);
\r
4027 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
4031 /* When remaining number of bytes to receive is less than the RX FIFO
\r
4032 threshold, next incoming frames are processed as if FIFO mode was
\r
4033 disabled (i.e. one interrupt per received frame).
\r
4035 rxdatacount = huart->RxXferCount;
\r
4036 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
\r
4038 /* Disable the UART RXFT interrupt*/
\r
4039 CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
\r
4041 /* Update the RxISR function pointer */
\r
4042 huart->RxISR = UART_RxISR_8BIT;
\r
4044 /* Enable the UART Data Register Not Empty interrupt */
\r
4045 SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
\r
4050 /* Clear RXNE interrupt flag */
\r
4051 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
4056 * @brief RX interrrupt handler for 9 bits data word length and FIFO mode is enabled.
\r
4057 * @note Function is called under interruption only, once
\r
4058 * interruptions have been enabled by HAL_UART_Receive_IT()
\r
4059 * @param huart UART handle.
\r
4062 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
\r
4065 uint16_t uhMask = huart->Mask;
\r
4067 uint16_t nb_rx_data;
\r
4068 uint16_t rxdatacount;
\r
4070 /* Check that a Rx process is ongoing */
\r
4071 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
\r
4073 for (nb_rx_data = huart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
\r
4075 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
\r
4076 tmp = (uint16_t *) huart->pRxBuffPtr ;
\r
4077 *tmp = (uint16_t)(uhdata & uhMask);
\r
4078 huart->pRxBuffPtr += 2U;
\r
4079 huart->RxXferCount--;
\r
4081 if (huart->RxXferCount == 0U)
\r
4083 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
\r
4084 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
\r
4086 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
\r
4087 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
\r
4089 /* Rx process is completed, restore huart->RxState to Ready */
\r
4090 huart->RxState = HAL_UART_STATE_READY;
\r
4092 /* Clear RxISR function pointer */
\r
4093 huart->RxISR = NULL;
\r
4095 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
\r
4096 /*Call registered Rx complete callback*/
\r
4097 huart->RxCpltCallback(huart);
\r
4099 /*Call legacy weak Rx complete callback*/
\r
4100 HAL_UART_RxCpltCallback(huart);
\r
4101 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
\r
4105 /* When remaining number of bytes to receive is less than the RX FIFO
\r
4106 threshold, next incoming frames are processed as if FIFO mode was
\r
4107 disabled (i.e. one interrupt per received frame).
\r
4109 rxdatacount = huart->RxXferCount;
\r
4110 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
\r
4112 /* Disable the UART RXFT interrupt*/
\r
4113 CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
\r
4115 /* Update the RxISR function pointer */
\r
4116 huart->RxISR = UART_RxISR_16BIT;
\r
4118 /* Enable the UART Data Register Not Empty interrupt */
\r
4119 SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
\r
4124 /* Clear RXNE interrupt flag */
\r
4125 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
\r
4128 #endif /* USART_CR1_FIFOEN */
\r
4134 #endif /* HAL_UART_MODULE_ENABLED */
\r
4143 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r