1 /**
2 ******************************************************************************
3 * @file stm32g4xx_hal_uart.c
4 * @author MCD Application Team
5 * @brief UART HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 *
12 *
13 ******************************************************************************
14 * @attention
15 *
16 * Copyright (c) 2019 STMicroelectronics.
17 * All rights reserved.
18 *
19 * This software is licensed under terms that can be found in the LICENSE file
20 * in the root directory of this software component.
21 * If no LICENSE file comes with this software, it is provided AS-IS.
22 *
23 ******************************************************************************
24 @verbatim
25 ===============================================================================
26 ##### How to use this driver #####
27 ===============================================================================
28 [..]
29 The UART HAL driver can be used as follows:
30
31 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
32 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
33 (++) Enable the USARTx interface clock.
34 (++) UART pins configuration:
35 (+++) Enable the clock for the UART GPIOs.
36 (+++) Configure these UART pins as alternate function pull-up.
37 (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
38 and HAL_UART_Receive_IT() APIs):
39 (+++) Configure the USARTx interrupt priority.
40 (+++) Enable the NVIC USART IRQ handle.
41 (++) UART interrupts handling:
42 -@@- The specific UART interrupts (Transmission complete interrupt,
43 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
44 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
45 inside the transmit and receive processes.
46 (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
47 and HAL_UART_Receive_DMA() APIs):
48 (+++) Declare a DMA handle structure for the Tx/Rx channel.
49 (+++) Enable the DMAx interface clock.
50 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
51 (+++) Configure the DMA Tx/Rx channel.
52 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
53 (+++) Configure the priority and enable the NVIC for the transfer complete
54 interrupt on the DMA Tx/Rx channel.
55
56 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
57 flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
58
59 (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
60 in the huart handle AdvancedInit structure.
61
62 (#) For the UART asynchronous mode, initialize the UART registers by calling
63 the HAL_UART_Init() API.
64
65 (#) For the UART Half duplex mode, initialize the UART registers by calling
66 the HAL_HalfDuplex_Init() API.
67
68 (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
69 by calling the HAL_LIN_Init() API.
70
71 (#) For the UART Multiprocessor mode, initialize the UART registers
72 by calling the HAL_MultiProcessor_Init() API.
73
74 (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
75 by calling the HAL_RS485Ex_Init() API.
76
77 [..]
78 (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
79 also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
80 calling the customized HAL_UART_MspInit() API.
81
82 ##### Callback registration #####
83 ==================================
84
85 [..]
86 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
87 allows the user to configure dynamically the driver callbacks.
88
89 [..]
90 Use Function HAL_UART_RegisterCallback() to register a user callback.
91 Function HAL_UART_RegisterCallback() allows to register following callbacks:
92 (+) TxHalfCpltCallback : Tx Half Complete Callback.
93 (+) TxCpltCallback : Tx Complete Callback.
94 (+) RxHalfCpltCallback : Rx Half Complete Callback.
95 (+) RxCpltCallback : Rx Complete Callback.
96 (+) ErrorCallback : Error Callback.
97 (+) AbortCpltCallback : Abort Complete Callback.
98 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
99 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
100 (+) WakeupCallback : Wakeup Callback.
101 (+) RxFifoFullCallback : Rx Fifo Full Callback.
102 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
103 (+) MspInitCallback : UART MspInit.
104 (+) MspDeInitCallback : UART MspDeInit.
105 This function takes as parameters the HAL peripheral handle, the Callback ID
106 and a pointer to the user callback function.
107
108 [..]
109 Use function HAL_UART_UnRegisterCallback() to reset a callback to the default
110 weak (surcharged) function.
111 HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
112 and the Callback ID.
113 This function allows to reset following callbacks:
114 (+) TxHalfCpltCallback : Tx Half Complete Callback.
115 (+) TxCpltCallback : Tx Complete Callback.
116 (+) RxHalfCpltCallback : Rx Half Complete Callback.
117 (+) RxCpltCallback : Rx Complete Callback.
118 (+) ErrorCallback : Error Callback.
119 (+) AbortCpltCallback : Abort Complete Callback.
120 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
121 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
122 (+) WakeupCallback : Wakeup Callback.
123 (+) RxFifoFullCallback : Rx Fifo Full Callback.
124 (+) TxFifoEmptyCallback : Tx Fifo Empty Callback.
125 (+) MspInitCallback : UART MspInit.
126 (+) MspDeInitCallback : UART MspDeInit.
127
128 [..]
129 For specific callback RxEventCallback, use dedicated registration/reset functions:
130 respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback().
131
132 [..]
133 By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
134 all callbacks are set to the corresponding weak (surcharged) functions:
135 examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback().
136 Exception done for MspInit and MspDeInit functions that are respectively
137 reset to the legacy weak (surcharged) functions in the HAL_UART_Init()
138 and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
139 If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit()
140 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
141
142 [..]
143 Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
144 Exception done MspInit/MspDeInit that can be registered/unregistered
145 in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
146 MspInit/DeInit callbacks can be used during the Init/DeInit.
147 In that case first register the MspInit/MspDeInit user callbacks
148 using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit()
149 or HAL_UART_Init() function.
150
151 [..]
152 When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
153 not defined, the callback registration feature is not available
154 and weak (surcharged) callbacks are used.
155
156
157 @endverbatim
158 ******************************************************************************
159 */
160
161 /* Includes ------------------------------------------------------------------*/
162 #include "stm32g4xx_hal.h"
163
164 /** @addtogroup STM32G4xx_HAL_Driver
165 * @{
166 */
167
168 /** @defgroup UART UART
169 * @brief HAL UART module driver
170 * @{
171 */
172
173 #ifdef HAL_UART_MODULE_ENABLED
174
175 /* Private typedef -----------------------------------------------------------*/
176 /* Private define ------------------------------------------------------------*/
177 /** @defgroup UART_Private_Constants UART Private Constants
178 * @{
179 */
180 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | \
181 USART_CR1_OVER8 | USART_CR1_FIFOEN)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
182
183 #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT | USART_CR3_TXFTCFG | \
184 USART_CR3_RXFTCFG)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
185
186 #define LPUART_BRR_MIN 0x00000300U /* LPUART BRR minimum authorized value */
187 #define LPUART_BRR_MAX 0x000FFFFFU /* LPUART BRR maximum authorized value */
188
189 #define UART_BRR_MIN 0x10U /* UART BRR minimum authorized value */
190 #define UART_BRR_MAX 0x0000FFFFU /* UART BRR maximum authorized value */
191 /**
192 * @}
193 */
194
195 /* Private macros ------------------------------------------------------------*/
196 /* Private function prototypes -----------------------------------------------*/
197 /** @addtogroup UART_Private_Functions
198 * @{
199 */
200 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
201 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
202 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
203 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
204 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
205 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
206 static void UART_DMAError(DMA_HandleTypeDef *hdma);
207 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
208 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
209 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
210 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
211 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
212 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
213 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
214 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
215 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
216 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
217 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
218 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
219 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
220 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
221 /**
222 * @}
223 */
224
225 /* Private variables ---------------------------------------------------------*/
226 /** @addtogroup UART_Private_variables
227 * @{
228 */
229 const uint16_t UARTPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
230 /**
231 * @}
232 */
233
234 /* Exported Constants --------------------------------------------------------*/
235 /* Exported functions --------------------------------------------------------*/
236
237 /** @defgroup UART_Exported_Functions UART Exported Functions
238 * @{
239 */
240
241 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
242 * @brief Initialization and Configuration functions
243 *
244 @verbatim
245 ===============================================================================
246 ##### Initialization and Configuration functions #####
247 ===============================================================================
248 [..]
249 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
250 in asynchronous mode.
251 (+) For the asynchronous mode the parameters below can be configured:
252 (++) Baud Rate
253 (++) Word Length
254 (++) Stop Bit
255 (++) Parity: If the parity is enabled, then the MSB bit of the data written
256 in the data register is transmitted but is changed by the parity bit.
257 (++) Hardware flow control
258 (++) Receiver/transmitter modes
259 (++) Over Sampling Method
260 (++) One-Bit Sampling Method
261 (+) For the asynchronous mode, the following advanced features can be configured as well:
262 (++) TX and/or RX pin level inversion
263 (++) data logical level inversion
264 (++) RX and TX pins swap
265 (++) RX overrun detection disabling
266 (++) DMA disabling on RX error
267 (++) MSB first on communication line
268 (++) auto Baud rate detection
269 [..]
270 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
271 follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
272 and UART multiprocessor mode configuration procedures (details for the procedures
273 are available in reference manual).
274
275 @endverbatim
276
277 Depending on the frame length defined by the M1 and M0 bits (7-bit,
278 8-bit or 9-bit), the possible UART formats are listed in the
279 following table.
280
281 Table 1. UART frame format.
282 +-----------------------------------------------------------------------+
283 | M1 bit | M0 bit | PCE bit | UART frame |
284 |---------|---------|-----------|---------------------------------------|
285 | 0 | 0 | 0 | | SB | 8 bit data | STB | |
286 |---------|---------|-----------|---------------------------------------|
287 | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
288 |---------|---------|-----------|---------------------------------------|
289 | 0 | 1 | 0 | | SB | 9 bit data | STB | |
290 |---------|---------|-----------|---------------------------------------|
291 | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
292 |---------|---------|-----------|---------------------------------------|
293 | 1 | 0 | 0 | | SB | 7 bit data | STB | |
294 |---------|---------|-----------|---------------------------------------|
295 | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
296 +-----------------------------------------------------------------------+
297
298 * @{
299 */
300
301 /**
302 * @brief Initialize the UART mode according to the specified
303 * parameters in the UART_InitTypeDef and initialize the associated handle.
304 * @param huart UART handle.
305 * @retval HAL status
306 */
HAL_UART_Init(UART_HandleTypeDef * huart)307 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
308 {
309 /* Check the UART handle allocation */
310 if (huart == NULL)
311 {
312 return HAL_ERROR;
313 }
314
315 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
316 {
317 /* Check the parameters */
318 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
319 }
320 else
321 {
322 /* Check the parameters */
323 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
324 }
325
326 if (huart->gState == HAL_UART_STATE_RESET)
327 {
328 /* Allocate lock resource and initialize it */
329 huart->Lock = HAL_UNLOCKED;
330
331 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
332 UART_InitCallbacksToDefault(huart);
333
334 if (huart->MspInitCallback == NULL)
335 {
336 huart->MspInitCallback = HAL_UART_MspInit;
337 }
338
339 /* Init the low level hardware */
340 huart->MspInitCallback(huart);
341 #else
342 /* Init the low level hardware : GPIO, CLOCK */
343 HAL_UART_MspInit(huart);
344 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
345 }
346
347 huart->gState = HAL_UART_STATE_BUSY;
348
349 __HAL_UART_DISABLE(huart);
350
351 /* Set the UART Communication parameters */
352 if (UART_SetConfig(huart) == HAL_ERROR)
353 {
354 return HAL_ERROR;
355 }
356
357 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
358 {
359 UART_AdvFeatureConfig(huart);
360 }
361
362 /* In asynchronous mode, the following bits must be kept cleared:
363 - LINEN and CLKEN bits in the USART_CR2 register,
364 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
365 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
366 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
367
368 __HAL_UART_ENABLE(huart);
369
370 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
371 return (UART_CheckIdleState(huart));
372 }
373
374 /**
375 * @brief Initialize the half-duplex mode according to the specified
376 * parameters in the UART_InitTypeDef and creates the associated handle.
377 * @param huart UART handle.
378 * @retval HAL status
379 */
HAL_HalfDuplex_Init(UART_HandleTypeDef * huart)380 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
381 {
382 /* Check the UART handle allocation */
383 if (huart == NULL)
384 {
385 return HAL_ERROR;
386 }
387
388 /* Check UART instance */
389 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
390
391 if (huart->gState == HAL_UART_STATE_RESET)
392 {
393 /* Allocate lock resource and initialize it */
394 huart->Lock = HAL_UNLOCKED;
395
396 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
397 UART_InitCallbacksToDefault(huart);
398
399 if (huart->MspInitCallback == NULL)
400 {
401 huart->MspInitCallback = HAL_UART_MspInit;
402 }
403
404 /* Init the low level hardware */
405 huart->MspInitCallback(huart);
406 #else
407 /* Init the low level hardware : GPIO, CLOCK */
408 HAL_UART_MspInit(huart);
409 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
410 }
411
412 huart->gState = HAL_UART_STATE_BUSY;
413
414 __HAL_UART_DISABLE(huart);
415
416 /* Set the UART Communication parameters */
417 if (UART_SetConfig(huart) == HAL_ERROR)
418 {
419 return HAL_ERROR;
420 }
421
422 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
423 {
424 UART_AdvFeatureConfig(huart);
425 }
426
427 /* In half-duplex mode, the following bits must be kept cleared:
428 - LINEN and CLKEN bits in the USART_CR2 register,
429 - SCEN and IREN bits in the USART_CR3 register.*/
430 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
431 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
432
433 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
434 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
435
436 __HAL_UART_ENABLE(huart);
437
438 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
439 return (UART_CheckIdleState(huart));
440 }
441
442
443 /**
444 * @brief Initialize the LIN mode according to the specified
445 * parameters in the UART_InitTypeDef and creates the associated handle.
446 * @param huart UART handle.
447 * @param BreakDetectLength Specifies the LIN break detection length.
448 * This parameter can be one of the following values:
449 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
450 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
451 * @retval HAL status
452 */
HAL_LIN_Init(UART_HandleTypeDef * huart,uint32_t BreakDetectLength)453 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
454 {
455 /* Check the UART handle allocation */
456 if (huart == NULL)
457 {
458 return HAL_ERROR;
459 }
460
461 /* Check the LIN UART instance */
462 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
463 /* Check the Break detection length parameter */
464 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
465
466 /* LIN mode limited to 16-bit oversampling only */
467 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
468 {
469 return HAL_ERROR;
470 }
471 /* LIN mode limited to 8-bit data length */
472 if (huart->Init.WordLength != UART_WORDLENGTH_8B)
473 {
474 return HAL_ERROR;
475 }
476
477 if (huart->gState == HAL_UART_STATE_RESET)
478 {
479 /* Allocate lock resource and initialize it */
480 huart->Lock = HAL_UNLOCKED;
481
482 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
483 UART_InitCallbacksToDefault(huart);
484
485 if (huart->MspInitCallback == NULL)
486 {
487 huart->MspInitCallback = HAL_UART_MspInit;
488 }
489
490 /* Init the low level hardware */
491 huart->MspInitCallback(huart);
492 #else
493 /* Init the low level hardware : GPIO, CLOCK */
494 HAL_UART_MspInit(huart);
495 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
496 }
497
498 huart->gState = HAL_UART_STATE_BUSY;
499
500 __HAL_UART_DISABLE(huart);
501
502 /* Set the UART Communication parameters */
503 if (UART_SetConfig(huart) == HAL_ERROR)
504 {
505 return HAL_ERROR;
506 }
507
508 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
509 {
510 UART_AdvFeatureConfig(huart);
511 }
512
513 /* In LIN mode, the following bits must be kept cleared:
514 - LINEN and CLKEN bits in the USART_CR2 register,
515 - SCEN and IREN bits in the USART_CR3 register.*/
516 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
517 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
518
519 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
520 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
521
522 /* Set the USART LIN Break detection length. */
523 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
524
525 __HAL_UART_ENABLE(huart);
526
527 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
528 return (UART_CheckIdleState(huart));
529 }
530
531
532 /**
533 * @brief Initialize the multiprocessor mode according to the specified
534 * parameters in the UART_InitTypeDef and initialize the associated handle.
535 * @param huart UART handle.
536 * @param Address UART node address (4-, 6-, 7- or 8-bit long).
537 * @param WakeUpMethod Specifies the UART wakeup method.
538 * This parameter can be one of the following values:
539 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
540 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
541 * @note If the user resorts to idle line detection wake up, the Address parameter
542 * is useless and ignored by the initialization function.
543 * @note If the user resorts to address mark wake up, the address length detection
544 * is configured by default to 4 bits only. For the UART to be able to
545 * manage 6-, 7- or 8-bit long addresses detection, the API
546 * HAL_MultiProcessorEx_AddressLength_Set() must be called after
547 * HAL_MultiProcessor_Init().
548 * @retval HAL status
549 */
HAL_MultiProcessor_Init(UART_HandleTypeDef * huart,uint8_t Address,uint32_t WakeUpMethod)550 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
551 {
552 /* Check the UART handle allocation */
553 if (huart == NULL)
554 {
555 return HAL_ERROR;
556 }
557
558 /* Check the wake up method parameter */
559 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
560
561 if (huart->gState == HAL_UART_STATE_RESET)
562 {
563 /* Allocate lock resource and initialize it */
564 huart->Lock = HAL_UNLOCKED;
565
566 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
567 UART_InitCallbacksToDefault(huart);
568
569 if (huart->MspInitCallback == NULL)
570 {
571 huart->MspInitCallback = HAL_UART_MspInit;
572 }
573
574 /* Init the low level hardware */
575 huart->MspInitCallback(huart);
576 #else
577 /* Init the low level hardware : GPIO, CLOCK */
578 HAL_UART_MspInit(huart);
579 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
580 }
581
582 huart->gState = HAL_UART_STATE_BUSY;
583
584 __HAL_UART_DISABLE(huart);
585
586 /* Set the UART Communication parameters */
587 if (UART_SetConfig(huart) == HAL_ERROR)
588 {
589 return HAL_ERROR;
590 }
591
592 if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
593 {
594 UART_AdvFeatureConfig(huart);
595 }
596
597 /* In multiprocessor mode, the following bits must be kept cleared:
598 - LINEN and CLKEN bits in the USART_CR2 register,
599 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
600 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
601 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
602
603 if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
604 {
605 /* If address mark wake up method is chosen, set the USART address node */
606 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
607 }
608
609 /* Set the wake up method by setting the WAKE bit in the CR1 register */
610 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
611
612 __HAL_UART_ENABLE(huart);
613
614 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
615 return (UART_CheckIdleState(huart));
616 }
617
618
619 /**
620 * @brief DeInitialize the UART peripheral.
621 * @param huart UART handle.
622 * @retval HAL status
623 */
HAL_UART_DeInit(UART_HandleTypeDef * huart)624 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
625 {
626 /* Check the UART handle allocation */
627 if (huart == NULL)
628 {
629 return HAL_ERROR;
630 }
631
632 /* Check the parameters */
633 assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
634
635 huart->gState = HAL_UART_STATE_BUSY;
636
637 __HAL_UART_DISABLE(huart);
638
639 huart->Instance->CR1 = 0x0U;
640 huart->Instance->CR2 = 0x0U;
641 huart->Instance->CR3 = 0x0U;
642
643 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
644 if (huart->MspDeInitCallback == NULL)
645 {
646 huart->MspDeInitCallback = HAL_UART_MspDeInit;
647 }
648 /* DeInit the low level hardware */
649 huart->MspDeInitCallback(huart);
650 #else
651 /* DeInit the low level hardware */
652 HAL_UART_MspDeInit(huart);
653 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
654
655 huart->ErrorCode = HAL_UART_ERROR_NONE;
656 huart->gState = HAL_UART_STATE_RESET;
657 huart->RxState = HAL_UART_STATE_RESET;
658 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
659
660 __HAL_UNLOCK(huart);
661
662 return HAL_OK;
663 }
664
665 /**
666 * @brief Initialize the UART MSP.
667 * @param huart UART handle.
668 * @retval None
669 */
HAL_UART_MspInit(UART_HandleTypeDef * huart)670 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
671 {
672 /* Prevent unused argument(s) compilation warning */
673 UNUSED(huart);
674
675 /* NOTE : This function should not be modified, when the callback is needed,
676 the HAL_UART_MspInit can be implemented in the user file
677 */
678 }
679
680 /**
681 * @brief DeInitialize the UART MSP.
682 * @param huart UART handle.
683 * @retval None
684 */
HAL_UART_MspDeInit(UART_HandleTypeDef * huart)685 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
686 {
687 /* Prevent unused argument(s) compilation warning */
688 UNUSED(huart);
689
690 /* NOTE : This function should not be modified, when the callback is needed,
691 the HAL_UART_MspDeInit can be implemented in the user file
692 */
693 }
694
695 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
696 /**
697 * @brief Register a User UART Callback
698 * To be used instead of the weak predefined callback
699 * @param huart uart handle
700 * @param CallbackID ID of the callback to be registered
701 * This parameter can be one of the following values:
702 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
703 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
704 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
705 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
706 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
707 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
708 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
709 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
710 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
711 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
712 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
713 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
714 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
715 * @param pCallback pointer to the Callback function
716 * @retval HAL status
717 */
HAL_UART_RegisterCallback(UART_HandleTypeDef * huart,HAL_UART_CallbackIDTypeDef CallbackID,pUART_CallbackTypeDef pCallback)718 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
719 pUART_CallbackTypeDef pCallback)
720 {
721 HAL_StatusTypeDef status = HAL_OK;
722
723 if (pCallback == NULL)
724 {
725 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
726
727 return HAL_ERROR;
728 }
729
730 __HAL_LOCK(huart);
731
732 if (huart->gState == HAL_UART_STATE_READY)
733 {
734 switch (CallbackID)
735 {
736 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
737 huart->TxHalfCpltCallback = pCallback;
738 break;
739
740 case HAL_UART_TX_COMPLETE_CB_ID :
741 huart->TxCpltCallback = pCallback;
742 break;
743
744 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
745 huart->RxHalfCpltCallback = pCallback;
746 break;
747
748 case HAL_UART_RX_COMPLETE_CB_ID :
749 huart->RxCpltCallback = pCallback;
750 break;
751
752 case HAL_UART_ERROR_CB_ID :
753 huart->ErrorCallback = pCallback;
754 break;
755
756 case HAL_UART_ABORT_COMPLETE_CB_ID :
757 huart->AbortCpltCallback = pCallback;
758 break;
759
760 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
761 huart->AbortTransmitCpltCallback = pCallback;
762 break;
763
764 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
765 huart->AbortReceiveCpltCallback = pCallback;
766 break;
767
768 case HAL_UART_WAKEUP_CB_ID :
769 huart->WakeupCallback = pCallback;
770 break;
771
772 case HAL_UART_RX_FIFO_FULL_CB_ID :
773 huart->RxFifoFullCallback = pCallback;
774 break;
775
776 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
777 huart->TxFifoEmptyCallback = pCallback;
778 break;
779
780 case HAL_UART_MSPINIT_CB_ID :
781 huart->MspInitCallback = pCallback;
782 break;
783
784 case HAL_UART_MSPDEINIT_CB_ID :
785 huart->MspDeInitCallback = pCallback;
786 break;
787
788 default :
789 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
790
791 status = HAL_ERROR;
792 break;
793 }
794 }
795 else if (huart->gState == HAL_UART_STATE_RESET)
796 {
797 switch (CallbackID)
798 {
799 case HAL_UART_MSPINIT_CB_ID :
800 huart->MspInitCallback = pCallback;
801 break;
802
803 case HAL_UART_MSPDEINIT_CB_ID :
804 huart->MspDeInitCallback = pCallback;
805 break;
806
807 default :
808 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
809
810 status = HAL_ERROR;
811 break;
812 }
813 }
814 else
815 {
816 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
817
818 status = HAL_ERROR;
819 }
820
821 __HAL_UNLOCK(huart);
822
823 return status;
824 }
825
826 /**
827 * @brief Unregister an UART Callback
828 * UART callaback is redirected to the weak predefined callback
829 * @param huart uart handle
830 * @param CallbackID ID of the callback to be unregistered
831 * This parameter can be one of the following values:
832 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
833 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
834 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
835 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
836 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
837 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
838 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
839 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
840 * @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
841 * @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
842 * @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
843 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
844 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
845 * @retval HAL status
846 */
HAL_UART_UnRegisterCallback(UART_HandleTypeDef * huart,HAL_UART_CallbackIDTypeDef CallbackID)847 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
848 {
849 HAL_StatusTypeDef status = HAL_OK;
850
851 __HAL_LOCK(huart);
852
853 if (HAL_UART_STATE_READY == huart->gState)
854 {
855 switch (CallbackID)
856 {
857 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
858 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
859 break;
860
861 case HAL_UART_TX_COMPLETE_CB_ID :
862 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
863 break;
864
865 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
866 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
867 break;
868
869 case HAL_UART_RX_COMPLETE_CB_ID :
870 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
871 break;
872
873 case HAL_UART_ERROR_CB_ID :
874 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
875 break;
876
877 case HAL_UART_ABORT_COMPLETE_CB_ID :
878 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
879 break;
880
881 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
882 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak
883 AbortTransmitCpltCallback */
884 break;
885
886 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
887 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak
888 AbortReceiveCpltCallback */
889 break;
890
891 case HAL_UART_WAKEUP_CB_ID :
892 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
893 break;
894
895 case HAL_UART_RX_FIFO_FULL_CB_ID :
896 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
897 break;
898
899 case HAL_UART_TX_FIFO_EMPTY_CB_ID :
900 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
901 break;
902
903 case HAL_UART_MSPINIT_CB_ID :
904 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
905 break;
906
907 case HAL_UART_MSPDEINIT_CB_ID :
908 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
909 break;
910
911 default :
912 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
913
914 status = HAL_ERROR;
915 break;
916 }
917 }
918 else if (HAL_UART_STATE_RESET == huart->gState)
919 {
920 switch (CallbackID)
921 {
922 case HAL_UART_MSPINIT_CB_ID :
923 huart->MspInitCallback = HAL_UART_MspInit;
924 break;
925
926 case HAL_UART_MSPDEINIT_CB_ID :
927 huart->MspDeInitCallback = HAL_UART_MspDeInit;
928 break;
929
930 default :
931 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
932
933 status = HAL_ERROR;
934 break;
935 }
936 }
937 else
938 {
939 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
940
941 status = HAL_ERROR;
942 }
943
944 __HAL_UNLOCK(huart);
945
946 return status;
947 }
948
949 /**
950 * @brief Register a User UART Rx Event Callback
951 * To be used instead of the weak predefined callback
952 * @param huart Uart handle
953 * @param pCallback Pointer to the Rx Event Callback function
954 * @retval HAL status
955 */
HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef * huart,pUART_RxEventCallbackTypeDef pCallback)956 HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback)
957 {
958 HAL_StatusTypeDef status = HAL_OK;
959
960 if (pCallback == NULL)
961 {
962 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
963
964 return HAL_ERROR;
965 }
966
967 /* Process locked */
968 __HAL_LOCK(huart);
969
970 if (huart->gState == HAL_UART_STATE_READY)
971 {
972 huart->RxEventCallback = pCallback;
973 }
974 else
975 {
976 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
977
978 status = HAL_ERROR;
979 }
980
981 /* Release Lock */
982 __HAL_UNLOCK(huart);
983
984 return status;
985 }
986
987 /**
988 * @brief UnRegister the UART Rx Event Callback
989 * UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback
990 * @param huart Uart handle
991 * @retval HAL status
992 */
HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef * huart)993 HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart)
994 {
995 HAL_StatusTypeDef status = HAL_OK;
996
997 /* Process locked */
998 __HAL_LOCK(huart);
999
1000 if (huart->gState == HAL_UART_STATE_READY)
1001 {
1002 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback */
1003 }
1004 else
1005 {
1006 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
1007
1008 status = HAL_ERROR;
1009 }
1010
1011 /* Release Lock */
1012 __HAL_UNLOCK(huart);
1013 return status;
1014 }
1015
1016 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1017
1018 /**
1019 * @}
1020 */
1021
1022 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
1023 * @brief UART Transmit/Receive functions
1024 *
1025 @verbatim
1026 ===============================================================================
1027 ##### IO operation functions #####
1028 ===============================================================================
1029 This subsection provides a set of functions allowing to manage the UART asynchronous
1030 and Half duplex data transfers.
1031
1032 (#) There are two mode of transfer:
1033 (+) Blocking mode: The communication is performed in polling mode.
1034 The HAL status of all data processing is returned by the same function
1035 after finishing transfer.
1036 (+) Non-Blocking mode: The communication is performed using Interrupts
1037 or DMA, These API's return the HAL status.
1038 The end of the data processing will be indicated through the
1039 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
1040 using DMA mode.
1041 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
1042 will be executed respectively at the end of the transmit or Receive process
1043 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
1044
1045 (#) Blocking mode API's are :
1046 (+) HAL_UART_Transmit()
1047 (+) HAL_UART_Receive()
1048
1049 (#) Non-Blocking mode API's with Interrupt are :
1050 (+) HAL_UART_Transmit_IT()
1051 (+) HAL_UART_Receive_IT()
1052 (+) HAL_UART_IRQHandler()
1053
1054 (#) Non-Blocking mode API's with DMA are :
1055 (+) HAL_UART_Transmit_DMA()
1056 (+) HAL_UART_Receive_DMA()
1057 (+) HAL_UART_DMAPause()
1058 (+) HAL_UART_DMAResume()
1059 (+) HAL_UART_DMAStop()
1060
1061 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
1062 (+) HAL_UART_TxHalfCpltCallback()
1063 (+) HAL_UART_TxCpltCallback()
1064 (+) HAL_UART_RxHalfCpltCallback()
1065 (+) HAL_UART_RxCpltCallback()
1066 (+) HAL_UART_ErrorCallback()
1067
1068 (#) Non-Blocking mode transfers could be aborted using Abort API's :
1069 (+) HAL_UART_Abort()
1070 (+) HAL_UART_AbortTransmit()
1071 (+) HAL_UART_AbortReceive()
1072 (+) HAL_UART_Abort_IT()
1073 (+) HAL_UART_AbortTransmit_IT()
1074 (+) HAL_UART_AbortReceive_IT()
1075
1076 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
1077 (+) HAL_UART_AbortCpltCallback()
1078 (+) HAL_UART_AbortTransmitCpltCallback()
1079 (+) HAL_UART_AbortReceiveCpltCallback()
1080
1081 (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced
1082 reception services:
1083 (+) HAL_UARTEx_RxEventCallback()
1084
1085 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
1086 Errors are handled as follows :
1087 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1088 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
1089 in Interrupt mode reception .
1090 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
1091 to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1092 Transfer is kept ongoing on UART side.
1093 If user wants to abort it, Abort services should be called by user.
1094 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1095 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1096 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback()
1097 user callback is executed.
1098
1099 -@- In the Half duplex communication, it is forbidden to run the transmit
1100 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1101
1102 @endverbatim
1103 * @{
1104 */
1105
1106 /**
1107 * @brief Send an amount of data in blocking mode.
1108 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1109 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1110 * of u16 provided through pData.
1111 * @note When FIFO mode is enabled, writing a data in the TDR register adds one
1112 * data to the TXFIFO. Write operations to the TDR register are performed
1113 * when TXFNF flag is set. From hardware perspective, TXFNF flag and
1114 * TXE are mapped on the same bit-field.
1115 * @param huart UART handle.
1116 * @param pData Pointer to data buffer (u8 or u16 data elements).
1117 * @param Size Amount of data elements (u8 or u16) to be sent.
1118 * @param Timeout Timeout duration.
1119 * @retval HAL status
1120 */
HAL_UART_Transmit(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size,uint32_t Timeout)1121 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
1122 {
1123 const uint8_t *pdata8bits;
1124 const uint16_t *pdata16bits;
1125 uint32_t tickstart;
1126
1127 /* Check that a Tx process is not already ongoing */
1128 if (huart->gState == HAL_UART_STATE_READY)
1129 {
1130 if ((pData == NULL) || (Size == 0U))
1131 {
1132 return HAL_ERROR;
1133 }
1134
1135 __HAL_LOCK(huart);
1136
1137 huart->ErrorCode = HAL_UART_ERROR_NONE;
1138 huart->gState = HAL_UART_STATE_BUSY_TX;
1139
1140 /* Init tickstart for timeout management */
1141 tickstart = HAL_GetTick();
1142
1143 huart->TxXferSize = Size;
1144 huart->TxXferCount = Size;
1145
1146 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1147 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1148 {
1149 pdata8bits = NULL;
1150 pdata16bits = (const uint16_t *) pData;
1151 }
1152 else
1153 {
1154 pdata8bits = pData;
1155 pdata16bits = NULL;
1156 }
1157
1158 __HAL_UNLOCK(huart);
1159
1160 while (huart->TxXferCount > 0U)
1161 {
1162 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1163 {
1164 return HAL_TIMEOUT;
1165 }
1166 if (pdata8bits == NULL)
1167 {
1168 huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
1169 pdata16bits++;
1170 }
1171 else
1172 {
1173 huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
1174 pdata8bits++;
1175 }
1176 huart->TxXferCount--;
1177 }
1178
1179 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
1180 {
1181 return HAL_TIMEOUT;
1182 }
1183
1184 /* At end of Tx process, restore huart->gState to Ready */
1185 huart->gState = HAL_UART_STATE_READY;
1186
1187 return HAL_OK;
1188 }
1189 else
1190 {
1191 return HAL_BUSY;
1192 }
1193 }
1194
1195 /**
1196 * @brief Receive an amount of data in blocking mode.
1197 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1198 * the received data is handled as a set of u16. In this case, Size must indicate the number
1199 * of u16 available through pData.
1200 * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
1201 * is not empty. Read operations from the RDR register are performed when
1202 * RXFNE flag is set. From hardware perspective, RXFNE flag and
1203 * RXNE are mapped on the same bit-field.
1204 * @param huart UART handle.
1205 * @param pData Pointer to data buffer (u8 or u16 data elements).
1206 * @param Size Amount of data elements (u8 or u16) to be received.
1207 * @param Timeout Timeout duration.
1208 * @retval HAL status
1209 */
HAL_UART_Receive(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size,uint32_t Timeout)1210 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1211 {
1212 uint8_t *pdata8bits;
1213 uint16_t *pdata16bits;
1214 uint16_t uhMask;
1215 uint32_t tickstart;
1216
1217 /* Check that a Rx process is not already ongoing */
1218 if (huart->RxState == HAL_UART_STATE_READY)
1219 {
1220 if ((pData == NULL) || (Size == 0U))
1221 {
1222 return HAL_ERROR;
1223 }
1224
1225 __HAL_LOCK(huart);
1226
1227 huart->ErrorCode = HAL_UART_ERROR_NONE;
1228 huart->RxState = HAL_UART_STATE_BUSY_RX;
1229 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1230
1231 /* Init tickstart for timeout management */
1232 tickstart = HAL_GetTick();
1233
1234 huart->RxXferSize = Size;
1235 huart->RxXferCount = Size;
1236
1237 /* Computation of UART mask to apply to RDR register */
1238 UART_MASK_COMPUTATION(huart);
1239 uhMask = huart->Mask;
1240
1241 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1242 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1243 {
1244 pdata8bits = NULL;
1245 pdata16bits = (uint16_t *) pData;
1246 }
1247 else
1248 {
1249 pdata8bits = pData;
1250 pdata16bits = NULL;
1251 }
1252
1253 __HAL_UNLOCK(huart);
1254
1255 /* as long as data have to be received */
1256 while (huart->RxXferCount > 0U)
1257 {
1258 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1259 {
1260 return HAL_TIMEOUT;
1261 }
1262 if (pdata8bits == NULL)
1263 {
1264 *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
1265 pdata16bits++;
1266 }
1267 else
1268 {
1269 *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1270 pdata8bits++;
1271 }
1272 huart->RxXferCount--;
1273 }
1274
1275 /* At end of Rx process, restore huart->RxState to Ready */
1276 huart->RxState = HAL_UART_STATE_READY;
1277
1278 return HAL_OK;
1279 }
1280 else
1281 {
1282 return HAL_BUSY;
1283 }
1284 }
1285
1286 /**
1287 * @brief Send an amount of data in interrupt mode.
1288 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1289 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1290 * of u16 provided through pData.
1291 * @param huart UART handle.
1292 * @param pData Pointer to data buffer (u8 or u16 data elements).
1293 * @param Size Amount of data elements (u8 or u16) to be sent.
1294 * @retval HAL status
1295 */
HAL_UART_Transmit_IT(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size)1296 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1297 {
1298 /* Check that a Tx process is not already ongoing */
1299 if (huart->gState == HAL_UART_STATE_READY)
1300 {
1301 if ((pData == NULL) || (Size == 0U))
1302 {
1303 return HAL_ERROR;
1304 }
1305
1306 __HAL_LOCK(huart);
1307
1308 huart->pTxBuffPtr = pData;
1309 huart->TxXferSize = Size;
1310 huart->TxXferCount = Size;
1311 huart->TxISR = NULL;
1312
1313 huart->ErrorCode = HAL_UART_ERROR_NONE;
1314 huart->gState = HAL_UART_STATE_BUSY_TX;
1315
1316 /* Configure Tx interrupt processing */
1317 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1318 {
1319 /* Set the Tx ISR function pointer according to the data word length */
1320 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1321 {
1322 huart->TxISR = UART_TxISR_16BIT_FIFOEN;
1323 }
1324 else
1325 {
1326 huart->TxISR = UART_TxISR_8BIT_FIFOEN;
1327 }
1328
1329 __HAL_UNLOCK(huart);
1330
1331 /* Enable the TX FIFO threshold interrupt */
1332 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1333 }
1334 else
1335 {
1336 /* Set the Tx ISR function pointer according to the data word length */
1337 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1338 {
1339 huart->TxISR = UART_TxISR_16BIT;
1340 }
1341 else
1342 {
1343 huart->TxISR = UART_TxISR_8BIT;
1344 }
1345
1346 __HAL_UNLOCK(huart);
1347
1348 /* Enable the Transmit Data Register Empty interrupt */
1349 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1350 }
1351
1352 return HAL_OK;
1353 }
1354 else
1355 {
1356 return HAL_BUSY;
1357 }
1358 }
1359
1360 /**
1361 * @brief Receive an amount of data in interrupt mode.
1362 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1363 * the received data is handled as a set of u16. In this case, Size must indicate the number
1364 * of u16 available through pData.
1365 * @param huart UART handle.
1366 * @param pData Pointer to data buffer (u8 or u16 data elements).
1367 * @param Size Amount of data elements (u8 or u16) to be received.
1368 * @retval HAL status
1369 */
HAL_UART_Receive_IT(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)1370 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1371 {
1372 /* Check that a Rx process is not already ongoing */
1373 if (huart->RxState == HAL_UART_STATE_READY)
1374 {
1375 if ((pData == NULL) || (Size == 0U))
1376 {
1377 return HAL_ERROR;
1378 }
1379
1380 __HAL_LOCK(huart);
1381
1382 /* Set Reception type to Standard reception */
1383 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1384
1385 if (!(IS_LPUART_INSTANCE(huart->Instance)))
1386 {
1387 /* Check that USART RTOEN bit is set */
1388 if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1389 {
1390 /* Enable the UART Receiver Timeout Interrupt */
1391 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1392 }
1393 }
1394
1395 return (UART_Start_Receive_IT(huart, pData, Size));
1396 }
1397 else
1398 {
1399 return HAL_BUSY;
1400 }
1401 }
1402
1403 /**
1404 * @brief Send an amount of data in DMA mode.
1405 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1406 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1407 * of u16 provided through pData.
1408 * @param huart UART handle.
1409 * @param pData Pointer to data buffer (u8 or u16 data elements).
1410 * @param Size Amount of data elements (u8 or u16) to be sent.
1411 * @retval HAL status
1412 */
HAL_UART_Transmit_DMA(UART_HandleTypeDef * huart,const uint8_t * pData,uint16_t Size)1413 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1414 {
1415 /* Check that a Tx process is not already ongoing */
1416 if (huart->gState == HAL_UART_STATE_READY)
1417 {
1418 if ((pData == NULL) || (Size == 0U))
1419 {
1420 return HAL_ERROR;
1421 }
1422
1423 __HAL_LOCK(huart);
1424
1425 huart->pTxBuffPtr = pData;
1426 huart->TxXferSize = Size;
1427 huart->TxXferCount = Size;
1428
1429 huart->ErrorCode = HAL_UART_ERROR_NONE;
1430 huart->gState = HAL_UART_STATE_BUSY_TX;
1431
1432 if (huart->hdmatx != NULL)
1433 {
1434 /* Set the UART DMA transfer complete callback */
1435 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
1436
1437 /* Set the UART DMA Half transfer complete callback */
1438 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
1439
1440 /* Set the DMA error callback */
1441 huart->hdmatx->XferErrorCallback = UART_DMAError;
1442
1443 /* Set the DMA abort callback */
1444 huart->hdmatx->XferAbortCallback = NULL;
1445
1446 /* Enable the UART transmit DMA channel */
1447 if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
1448 {
1449 /* Set error code to DMA */
1450 huart->ErrorCode = HAL_UART_ERROR_DMA;
1451
1452 __HAL_UNLOCK(huart);
1453
1454 /* Restore huart->gState to ready */
1455 huart->gState = HAL_UART_STATE_READY;
1456
1457 return HAL_ERROR;
1458 }
1459 }
1460 /* Clear the TC flag in the ICR register */
1461 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
1462
1463 __HAL_UNLOCK(huart);
1464
1465 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1466 in the UART CR3 register */
1467 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1468
1469 return HAL_OK;
1470 }
1471 else
1472 {
1473 return HAL_BUSY;
1474 }
1475 }
1476
1477 /**
1478 * @brief Receive an amount of data in DMA mode.
1479 * @note When the UART parity is enabled (PCE = 1), the received data contain
1480 * the parity bit (MSB position).
1481 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1482 * the received data is handled as a set of u16. In this case, Size must indicate the number
1483 * of u16 available through pData.
1484 * @param huart UART handle.
1485 * @param pData Pointer to data buffer (u8 or u16 data elements).
1486 * @param Size Amount of data elements (u8 or u16) to be received.
1487 * @retval HAL status
1488 */
HAL_UART_Receive_DMA(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)1489 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1490 {
1491 /* Check that a Rx process is not already ongoing */
1492 if (huart->RxState == HAL_UART_STATE_READY)
1493 {
1494 if ((pData == NULL) || (Size == 0U))
1495 {
1496 return HAL_ERROR;
1497 }
1498
1499 __HAL_LOCK(huart);
1500
1501 /* Set Reception type to Standard reception */
1502 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1503
1504 if (!(IS_LPUART_INSTANCE(huart->Instance)))
1505 {
1506 /* Check that USART RTOEN bit is set */
1507 if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1508 {
1509 /* Enable the UART Receiver Timeout Interrupt */
1510 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1511 }
1512 }
1513
1514 return (UART_Start_Receive_DMA(huart, pData, Size));
1515 }
1516 else
1517 {
1518 return HAL_BUSY;
1519 }
1520 }
1521
1522 /**
1523 * @brief Pause the DMA Transfer.
1524 * @param huart UART handle.
1525 * @retval HAL status
1526 */
HAL_UART_DMAPause(UART_HandleTypeDef * huart)1527 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1528 {
1529 const HAL_UART_StateTypeDef gstate = huart->gState;
1530 const HAL_UART_StateTypeDef rxstate = huart->RxState;
1531
1532 __HAL_LOCK(huart);
1533
1534 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1535 (gstate == HAL_UART_STATE_BUSY_TX))
1536 {
1537 /* Disable the UART DMA Tx request */
1538 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1539 }
1540 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1541 (rxstate == HAL_UART_STATE_BUSY_RX))
1542 {
1543 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1544 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1545 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1546
1547 /* Disable the UART DMA Rx request */
1548 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1549 }
1550
1551 __HAL_UNLOCK(huart);
1552
1553 return HAL_OK;
1554 }
1555
1556 /**
1557 * @brief Resume the DMA Transfer.
1558 * @param huart UART handle.
1559 * @retval HAL status
1560 */
HAL_UART_DMAResume(UART_HandleTypeDef * huart)1561 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1562 {
1563 __HAL_LOCK(huart);
1564
1565 if (huart->gState == HAL_UART_STATE_BUSY_TX)
1566 {
1567 /* Enable the UART DMA Tx request */
1568 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1569 }
1570 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
1571 {
1572 /* Clear the Overrun flag before resuming the Rx transfer */
1573 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
1574
1575 /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1576 if (huart->Init.Parity != UART_PARITY_NONE)
1577 {
1578 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1579 }
1580 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1581
1582 /* Enable the UART DMA Rx request */
1583 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1584 }
1585
1586 __HAL_UNLOCK(huart);
1587
1588 return HAL_OK;
1589 }
1590
1591 /**
1592 * @brief Stop the DMA Transfer.
1593 * @param huart UART handle.
1594 * @retval HAL status
1595 */
HAL_UART_DMAStop(UART_HandleTypeDef * huart)1596 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1597 {
1598 /* The Lock is not implemented on this API to allow the user application
1599 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1600 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1601 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1602 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1603 the stream and the corresponding call back is executed. */
1604
1605 const HAL_UART_StateTypeDef gstate = huart->gState;
1606 const HAL_UART_StateTypeDef rxstate = huart->RxState;
1607
1608 /* Stop UART DMA Tx request if ongoing */
1609 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1610 (gstate == HAL_UART_STATE_BUSY_TX))
1611 {
1612 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1613
1614 /* Abort the UART DMA Tx channel */
1615 if (huart->hdmatx != NULL)
1616 {
1617 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1618 {
1619 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1620 {
1621 /* Set error code to DMA */
1622 huart->ErrorCode = HAL_UART_ERROR_DMA;
1623
1624 return HAL_TIMEOUT;
1625 }
1626 }
1627 }
1628
1629 UART_EndTxTransfer(huart);
1630 }
1631
1632 /* Stop UART DMA Rx request if ongoing */
1633 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1634 (rxstate == HAL_UART_STATE_BUSY_RX))
1635 {
1636 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1637
1638 /* Abort the UART DMA Rx channel */
1639 if (huart->hdmarx != NULL)
1640 {
1641 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1642 {
1643 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1644 {
1645 /* Set error code to DMA */
1646 huart->ErrorCode = HAL_UART_ERROR_DMA;
1647
1648 return HAL_TIMEOUT;
1649 }
1650 }
1651 }
1652
1653 UART_EndRxTransfer(huart);
1654 }
1655
1656 return HAL_OK;
1657 }
1658
1659 /**
1660 * @brief Abort ongoing transfers (blocking mode).
1661 * @param huart UART handle.
1662 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1663 * This procedure performs following operations :
1664 * - Disable UART Interrupts (Tx and Rx)
1665 * - Disable the DMA transfer in the peripheral register (if enabled)
1666 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1667 * - Set handle State to READY
1668 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1669 * @retval HAL status
1670 */
HAL_UART_Abort(UART_HandleTypeDef * huart)1671 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1672 {
1673 /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1674 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE |
1675 USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1676 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
1677
1678 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1679 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1680 {
1681 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1682 }
1683
1684 /* Abort the UART DMA Tx channel if enabled */
1685 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1686 {
1687 /* Disable the UART DMA Tx request if enabled */
1688 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1689
1690 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1691 if (huart->hdmatx != NULL)
1692 {
1693 /* Set the UART DMA Abort callback to Null.
1694 No call back execution at end of DMA abort procedure */
1695 huart->hdmatx->XferAbortCallback = NULL;
1696
1697 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1698 {
1699 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1700 {
1701 /* Set error code to DMA */
1702 huart->ErrorCode = HAL_UART_ERROR_DMA;
1703
1704 return HAL_TIMEOUT;
1705 }
1706 }
1707 }
1708 }
1709
1710 /* Abort the UART DMA Rx channel if enabled */
1711 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1712 {
1713 /* Disable the UART DMA Rx request if enabled */
1714 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1715
1716 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1717 if (huart->hdmarx != NULL)
1718 {
1719 /* Set the UART DMA Abort callback to Null.
1720 No call back execution at end of DMA abort procedure */
1721 huart->hdmarx->XferAbortCallback = NULL;
1722
1723 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1724 {
1725 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1726 {
1727 /* Set error code to DMA */
1728 huart->ErrorCode = HAL_UART_ERROR_DMA;
1729
1730 return HAL_TIMEOUT;
1731 }
1732 }
1733 }
1734 }
1735
1736 /* Reset Tx and Rx transfer counters */
1737 huart->TxXferCount = 0U;
1738 huart->RxXferCount = 0U;
1739
1740 /* Clear the Error flags in the ICR register */
1741 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1742
1743 /* Flush the whole TX FIFO (if needed) */
1744 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1745 {
1746 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1747 }
1748
1749 /* Discard the received data */
1750 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1751
1752 /* Restore huart->gState and huart->RxState to Ready */
1753 huart->gState = HAL_UART_STATE_READY;
1754 huart->RxState = HAL_UART_STATE_READY;
1755 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1756
1757 huart->ErrorCode = HAL_UART_ERROR_NONE;
1758
1759 return HAL_OK;
1760 }
1761
1762 /**
1763 * @brief Abort ongoing Transmit transfer (blocking mode).
1764 * @param huart UART handle.
1765 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1766 * This procedure performs following operations :
1767 * - Disable UART Interrupts (Tx)
1768 * - Disable the DMA transfer in the peripheral register (if enabled)
1769 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1770 * - Set handle State to READY
1771 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1772 * @retval HAL status
1773 */
HAL_UART_AbortTransmit(UART_HandleTypeDef * huart)1774 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1775 {
1776 /* Disable TCIE, TXEIE and TXFTIE interrupts */
1777 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
1778 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1779
1780 /* Abort the UART DMA Tx channel if enabled */
1781 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1782 {
1783 /* Disable the UART DMA Tx request if enabled */
1784 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1785
1786 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1787 if (huart->hdmatx != NULL)
1788 {
1789 /* Set the UART DMA Abort callback to Null.
1790 No call back execution at end of DMA abort procedure */
1791 huart->hdmatx->XferAbortCallback = NULL;
1792
1793 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1794 {
1795 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1796 {
1797 /* Set error code to DMA */
1798 huart->ErrorCode = HAL_UART_ERROR_DMA;
1799
1800 return HAL_TIMEOUT;
1801 }
1802 }
1803 }
1804 }
1805
1806 /* Reset Tx transfer counter */
1807 huart->TxXferCount = 0U;
1808
1809 /* Flush the whole TX FIFO (if needed) */
1810 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1811 {
1812 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1813 }
1814
1815 /* Restore huart->gState to Ready */
1816 huart->gState = HAL_UART_STATE_READY;
1817
1818 return HAL_OK;
1819 }
1820
1821 /**
1822 * @brief Abort ongoing Receive transfer (blocking mode).
1823 * @param huart UART handle.
1824 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1825 * This procedure performs following operations :
1826 * - Disable UART Interrupts (Rx)
1827 * - Disable the DMA transfer in the peripheral register (if enabled)
1828 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1829 * - Set handle State to READY
1830 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1831 * @retval HAL status
1832 */
HAL_UART_AbortReceive(UART_HandleTypeDef * huart)1833 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1834 {
1835 /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
1836 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
1837 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
1838
1839 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1840 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1841 {
1842 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1843 }
1844
1845 /* Abort the UART DMA Rx channel if enabled */
1846 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1847 {
1848 /* Disable the UART DMA Rx request if enabled */
1849 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1850
1851 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1852 if (huart->hdmarx != NULL)
1853 {
1854 /* Set the UART DMA Abort callback to Null.
1855 No call back execution at end of DMA abort procedure */
1856 huart->hdmarx->XferAbortCallback = NULL;
1857
1858 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1859 {
1860 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1861 {
1862 /* Set error code to DMA */
1863 huart->ErrorCode = HAL_UART_ERROR_DMA;
1864
1865 return HAL_TIMEOUT;
1866 }
1867 }
1868 }
1869 }
1870
1871 /* Reset Rx transfer counter */
1872 huart->RxXferCount = 0U;
1873
1874 /* Clear the Error flags in the ICR register */
1875 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1876
1877 /* Discard the received data */
1878 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1879
1880 /* Restore huart->RxState to Ready */
1881 huart->RxState = HAL_UART_STATE_READY;
1882 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1883
1884 return HAL_OK;
1885 }
1886
1887 /**
1888 * @brief Abort ongoing transfers (Interrupt mode).
1889 * @param huart UART handle.
1890 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1891 * This procedure performs following operations :
1892 * - Disable UART Interrupts (Tx and Rx)
1893 * - Disable the DMA transfer in the peripheral register (if enabled)
1894 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1895 * - Set handle State to READY
1896 * - At abort completion, call user abort complete callback
1897 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1898 * considered as completed only when user abort complete callback is executed (not when exiting function).
1899 * @retval HAL status
1900 */
HAL_UART_Abort_IT(UART_HandleTypeDef * huart)1901 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1902 {
1903 uint32_t abortcplt = 1U;
1904
1905 /* Disable interrupts */
1906 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE |
1907 USART_CR1_TXEIE_TXFNFIE));
1908 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1909
1910 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1911 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1912 {
1913 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1914 }
1915
1916 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1917 before any call to DMA Abort functions */
1918 /* DMA Tx Handle is valid */
1919 if (huart->hdmatx != NULL)
1920 {
1921 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1922 Otherwise, set it to NULL */
1923 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1924 {
1925 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
1926 }
1927 else
1928 {
1929 huart->hdmatx->XferAbortCallback = NULL;
1930 }
1931 }
1932 /* DMA Rx Handle is valid */
1933 if (huart->hdmarx != NULL)
1934 {
1935 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1936 Otherwise, set it to NULL */
1937 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1938 {
1939 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
1940 }
1941 else
1942 {
1943 huart->hdmarx->XferAbortCallback = NULL;
1944 }
1945 }
1946
1947 /* Abort the UART DMA Tx channel if enabled */
1948 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1949 {
1950 /* Disable DMA Tx at UART level */
1951 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1952
1953 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1954 if (huart->hdmatx != NULL)
1955 {
1956 /* UART Tx DMA Abort callback has already been initialised :
1957 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1958
1959 /* Abort DMA TX */
1960 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1961 {
1962 huart->hdmatx->XferAbortCallback = NULL;
1963 }
1964 else
1965 {
1966 abortcplt = 0U;
1967 }
1968 }
1969 }
1970
1971 /* Abort the UART DMA Rx channel if enabled */
1972 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1973 {
1974 /* Disable the UART DMA Rx request if enabled */
1975 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1976
1977 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1978 if (huart->hdmarx != NULL)
1979 {
1980 /* UART Rx DMA Abort callback has already been initialised :
1981 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1982
1983 /* Abort DMA RX */
1984 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1985 {
1986 huart->hdmarx->XferAbortCallback = NULL;
1987 abortcplt = 1U;
1988 }
1989 else
1990 {
1991 abortcplt = 0U;
1992 }
1993 }
1994 }
1995
1996 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1997 if (abortcplt == 1U)
1998 {
1999 /* Reset Tx and Rx transfer counters */
2000 huart->TxXferCount = 0U;
2001 huart->RxXferCount = 0U;
2002
2003 /* Clear ISR function pointers */
2004 huart->RxISR = NULL;
2005 huart->TxISR = NULL;
2006
2007 /* Reset errorCode */
2008 huart->ErrorCode = HAL_UART_ERROR_NONE;
2009
2010 /* Clear the Error flags in the ICR register */
2011 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2012
2013 /* Flush the whole TX FIFO (if needed) */
2014 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2015 {
2016 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2017 }
2018
2019 /* Discard the received data */
2020 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2021
2022 /* Restore huart->gState and huart->RxState to Ready */
2023 huart->gState = HAL_UART_STATE_READY;
2024 huart->RxState = HAL_UART_STATE_READY;
2025 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2026
2027 /* As no DMA to be aborted, call directly user Abort complete callback */
2028 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2029 /* Call registered Abort complete callback */
2030 huart->AbortCpltCallback(huart);
2031 #else
2032 /* Call legacy weak Abort complete callback */
2033 HAL_UART_AbortCpltCallback(huart);
2034 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2035 }
2036
2037 return HAL_OK;
2038 }
2039
2040 /**
2041 * @brief Abort ongoing Transmit transfer (Interrupt mode).
2042 * @param huart UART handle.
2043 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
2044 * This procedure performs following operations :
2045 * - Disable UART Interrupts (Tx)
2046 * - Disable the DMA transfer in the peripheral register (if enabled)
2047 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2048 * - Set handle State to READY
2049 * - At abort completion, call user abort complete callback
2050 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2051 * considered as completed only when user abort complete callback is executed (not when exiting function).
2052 * @retval HAL status
2053 */
HAL_UART_AbortTransmit_IT(UART_HandleTypeDef * huart)2054 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
2055 {
2056 /* Disable interrupts */
2057 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
2058 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
2059
2060 /* Abort the UART DMA Tx channel if enabled */
2061 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2062 {
2063 /* Disable the UART DMA Tx request if enabled */
2064 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2065
2066 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
2067 if (huart->hdmatx != NULL)
2068 {
2069 /* Set the UART DMA Abort callback :
2070 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2071 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
2072
2073 /* Abort DMA TX */
2074 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2075 {
2076 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2077 huart->hdmatx->XferAbortCallback(huart->hdmatx);
2078 }
2079 }
2080 else
2081 {
2082 /* Reset Tx transfer counter */
2083 huart->TxXferCount = 0U;
2084
2085 /* Clear TxISR function pointers */
2086 huart->TxISR = NULL;
2087
2088 /* Restore huart->gState to Ready */
2089 huart->gState = HAL_UART_STATE_READY;
2090
2091 /* As no DMA to be aborted, call directly user Abort complete callback */
2092 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2093 /* Call registered Abort Transmit Complete Callback */
2094 huart->AbortTransmitCpltCallback(huart);
2095 #else
2096 /* Call legacy weak Abort Transmit Complete Callback */
2097 HAL_UART_AbortTransmitCpltCallback(huart);
2098 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2099 }
2100 }
2101 else
2102 {
2103 /* Reset Tx transfer counter */
2104 huart->TxXferCount = 0U;
2105
2106 /* Clear TxISR function pointers */
2107 huart->TxISR = NULL;
2108
2109 /* Flush the whole TX FIFO (if needed) */
2110 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2111 {
2112 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2113 }
2114
2115 /* Restore huart->gState to Ready */
2116 huart->gState = HAL_UART_STATE_READY;
2117
2118 /* As no DMA to be aborted, call directly user Abort complete callback */
2119 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2120 /* Call registered Abort Transmit Complete Callback */
2121 huart->AbortTransmitCpltCallback(huart);
2122 #else
2123 /* Call legacy weak Abort Transmit Complete Callback */
2124 HAL_UART_AbortTransmitCpltCallback(huart);
2125 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2126 }
2127
2128 return HAL_OK;
2129 }
2130
2131 /**
2132 * @brief Abort ongoing Receive transfer (Interrupt mode).
2133 * @param huart UART handle.
2134 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2135 * This procedure performs following operations :
2136 * - Disable UART Interrupts (Rx)
2137 * - Disable the DMA transfer in the peripheral register (if enabled)
2138 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2139 * - Set handle State to READY
2140 * - At abort completion, call user abort complete callback
2141 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2142 * considered as completed only when user abort complete callback is executed (not when exiting function).
2143 * @retval HAL status
2144 */
HAL_UART_AbortReceive_IT(UART_HandleTypeDef * huart)2145 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
2146 {
2147 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2148 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
2149 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2150
2151 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2152 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2153 {
2154 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2155 }
2156
2157 /* Abort the UART DMA Rx channel if enabled */
2158 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2159 {
2160 /* Disable the UART DMA Rx request if enabled */
2161 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2162
2163 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2164 if (huart->hdmarx != NULL)
2165 {
2166 /* Set the UART DMA Abort callback :
2167 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2168 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
2169
2170 /* Abort DMA RX */
2171 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2172 {
2173 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2174 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2175 }
2176 }
2177 else
2178 {
2179 /* Reset Rx transfer counter */
2180 huart->RxXferCount = 0U;
2181
2182 /* Clear RxISR function pointer */
2183 huart->pRxBuffPtr = NULL;
2184
2185 /* Clear the Error flags in the ICR register */
2186 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2187
2188 /* Discard the received data */
2189 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2190
2191 /* Restore huart->RxState to Ready */
2192 huart->RxState = HAL_UART_STATE_READY;
2193 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2194
2195 /* As no DMA to be aborted, call directly user Abort complete callback */
2196 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2197 /* Call registered Abort Receive Complete Callback */
2198 huart->AbortReceiveCpltCallback(huart);
2199 #else
2200 /* Call legacy weak Abort Receive Complete Callback */
2201 HAL_UART_AbortReceiveCpltCallback(huart);
2202 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2203 }
2204 }
2205 else
2206 {
2207 /* Reset Rx transfer counter */
2208 huart->RxXferCount = 0U;
2209
2210 /* Clear RxISR function pointer */
2211 huart->pRxBuffPtr = NULL;
2212
2213 /* Clear the Error flags in the ICR register */
2214 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2215
2216 /* Restore huart->RxState to Ready */
2217 huart->RxState = HAL_UART_STATE_READY;
2218 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2219
2220 /* As no DMA to be aborted, call directly user Abort complete callback */
2221 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2222 /* Call registered Abort Receive Complete Callback */
2223 huart->AbortReceiveCpltCallback(huart);
2224 #else
2225 /* Call legacy weak Abort Receive Complete Callback */
2226 HAL_UART_AbortReceiveCpltCallback(huart);
2227 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2228 }
2229
2230 return HAL_OK;
2231 }
2232
2233 /**
2234 * @brief Handle UART interrupt request.
2235 * @param huart UART handle.
2236 * @retval None
2237 */
HAL_UART_IRQHandler(UART_HandleTypeDef * huart)2238 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
2239 {
2240 uint32_t isrflags = READ_REG(huart->Instance->ISR);
2241 uint32_t cr1its = READ_REG(huart->Instance->CR1);
2242 uint32_t cr3its = READ_REG(huart->Instance->CR3);
2243
2244 uint32_t errorflags;
2245 uint32_t errorcode;
2246
2247 /* If no error occurs */
2248 errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
2249 if (errorflags == 0U)
2250 {
2251 /* UART in mode Receiver ---------------------------------------------------*/
2252 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2253 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2254 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2255 {
2256 if (huart->RxISR != NULL)
2257 {
2258 huart->RxISR(huart);
2259 }
2260 return;
2261 }
2262 }
2263
2264 /* If some errors occur */
2265 if ((errorflags != 0U)
2266 && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2267 || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U))))
2268 {
2269 /* UART parity error interrupt occurred -------------------------------------*/
2270 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2271 {
2272 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
2273
2274 huart->ErrorCode |= HAL_UART_ERROR_PE;
2275 }
2276
2277 /* UART frame error interrupt occurred --------------------------------------*/
2278 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2279 {
2280 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
2281
2282 huart->ErrorCode |= HAL_UART_ERROR_FE;
2283 }
2284
2285 /* UART noise error interrupt occurred --------------------------------------*/
2286 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2287 {
2288 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
2289
2290 huart->ErrorCode |= HAL_UART_ERROR_NE;
2291 }
2292
2293 /* UART Over-Run interrupt occurred -----------------------------------------*/
2294 if (((isrflags & USART_ISR_ORE) != 0U)
2295 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2296 ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2297 {
2298 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
2299
2300 huart->ErrorCode |= HAL_UART_ERROR_ORE;
2301 }
2302
2303 /* UART Receiver Timeout interrupt occurred ---------------------------------*/
2304 if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
2305 {
2306 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
2307
2308 huart->ErrorCode |= HAL_UART_ERROR_RTO;
2309 }
2310
2311 /* Call UART Error Call back function if need be ----------------------------*/
2312 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
2313 {
2314 /* UART in mode Receiver --------------------------------------------------*/
2315 if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2316 && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2317 || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2318 {
2319 if (huart->RxISR != NULL)
2320 {
2321 huart->RxISR(huart);
2322 }
2323 }
2324
2325 /* If Error is to be considered as blocking :
2326 - Receiver Timeout error in Reception
2327 - Overrun error in Reception
2328 - any error occurs in DMA mode reception
2329 */
2330 errorcode = huart->ErrorCode;
2331 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
2332 ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
2333 {
2334 /* Blocking error : transfer is aborted
2335 Set the UART state ready to be able to start again the process,
2336 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2337 UART_EndRxTransfer(huart);
2338
2339 /* Abort the UART DMA Rx channel if enabled */
2340 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2341 {
2342 /* Disable the UART DMA Rx request if enabled */
2343 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2344
2345 /* Abort the UART DMA Rx channel */
2346 if (huart->hdmarx != NULL)
2347 {
2348 /* Set the UART DMA Abort callback :
2349 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2350 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
2351
2352 /* Abort DMA RX */
2353 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2354 {
2355 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2356 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2357 }
2358 }
2359 else
2360 {
2361 /* Call user error callback */
2362 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2363 /*Call registered error callback*/
2364 huart->ErrorCallback(huart);
2365 #else
2366 /*Call legacy weak error callback*/
2367 HAL_UART_ErrorCallback(huart);
2368 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2369
2370 }
2371 }
2372 else
2373 {
2374 /* Call user error callback */
2375 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2376 /*Call registered error callback*/
2377 huart->ErrorCallback(huart);
2378 #else
2379 /*Call legacy weak error callback*/
2380 HAL_UART_ErrorCallback(huart);
2381 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2382 }
2383 }
2384 else
2385 {
2386 /* Non Blocking error : transfer could go on.
2387 Error is notified to user through user error callback */
2388 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2389 /*Call registered error callback*/
2390 huart->ErrorCallback(huart);
2391 #else
2392 /*Call legacy weak error callback*/
2393 HAL_UART_ErrorCallback(huart);
2394 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2395 huart->ErrorCode = HAL_UART_ERROR_NONE;
2396 }
2397 }
2398 return;
2399
2400 } /* End if some error occurs */
2401
2402 /* Check current reception Mode :
2403 If Reception till IDLE event has been selected : */
2404 if ((huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2405 && ((isrflags & USART_ISR_IDLE) != 0U)
2406 && ((cr1its & USART_ISR_IDLE) != 0U))
2407 {
2408 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
2409
2410 /* Check if DMA mode is enabled in UART */
2411 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2412 {
2413 /* DMA mode enabled */
2414 /* Check received length : If all expected data are received, do nothing,
2415 (DMA cplt callback will be called).
2416 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2417 uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx);
2418 if ((nb_remaining_rx_data > 0U)
2419 && (nb_remaining_rx_data < huart->RxXferSize))
2420 {
2421 /* Reception is not complete */
2422 huart->RxXferCount = nb_remaining_rx_data;
2423
2424 /* In Normal mode, end DMA xfer and HAL UART Rx process*/
2425 if (HAL_IS_BIT_CLR(huart->hdmarx->Instance->CCR, DMA_CCR_CIRC))
2426 {
2427 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2428 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2429 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2430
2431 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2432 in the UART CR3 register */
2433 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2434
2435 /* At end of Rx process, restore huart->RxState to Ready */
2436 huart->RxState = HAL_UART_STATE_READY;
2437 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2438
2439 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2440
2441 /* Last bytes received, so no need as the abort is immediate */
2442 (void)HAL_DMA_Abort(huart->hdmarx);
2443 }
2444 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2445 /*Call registered Rx Event callback*/
2446 huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2447 #else
2448 /*Call legacy weak Rx Event callback*/
2449 HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2450 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2451 }
2452 return;
2453 }
2454 else
2455 {
2456 /* DMA mode not enabled */
2457 /* Check received length : If all expected data are received, do nothing.
2458 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2459 uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount;
2460 if ((huart->RxXferCount > 0U)
2461 && (nb_rx_data > 0U))
2462 {
2463 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
2464 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2465
2466 /* Disable the UART Error Interrupt:(Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
2467 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2468
2469 /* Rx process is completed, restore huart->RxState to Ready */
2470 huart->RxState = HAL_UART_STATE_READY;
2471 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2472
2473 /* Clear RxISR function pointer */
2474 huart->RxISR = NULL;
2475
2476 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2477 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2478 /*Call registered Rx complete callback*/
2479 huart->RxEventCallback(huart, nb_rx_data);
2480 #else
2481 /*Call legacy weak Rx Event callback*/
2482 HAL_UARTEx_RxEventCallback(huart, nb_rx_data);
2483 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2484 }
2485 return;
2486 }
2487 }
2488
2489 /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
2490 if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
2491 {
2492 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
2493
2494 /* UART Rx state is not reset as a reception process might be ongoing.
2495 If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
2496
2497 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2498 /* Call registered Wakeup Callback */
2499 huart->WakeupCallback(huart);
2500 #else
2501 /* Call legacy weak Wakeup Callback */
2502 HAL_UARTEx_WakeupCallback(huart);
2503 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2504 return;
2505 }
2506
2507 /* UART in mode Transmitter ------------------------------------------------*/
2508 if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2509 && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2510 || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2511 {
2512 if (huart->TxISR != NULL)
2513 {
2514 huart->TxISR(huart);
2515 }
2516 return;
2517 }
2518
2519 /* UART in mode Transmitter (transmission end) -----------------------------*/
2520 if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2521 {
2522 UART_EndTransmit_IT(huart);
2523 return;
2524 }
2525
2526 /* UART TX Fifo Empty occurred ----------------------------------------------*/
2527 if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2528 {
2529 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2530 /* Call registered Tx Fifo Empty Callback */
2531 huart->TxFifoEmptyCallback(huart);
2532 #else
2533 /* Call legacy weak Tx Fifo Empty Callback */
2534 HAL_UARTEx_TxFifoEmptyCallback(huart);
2535 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2536 return;
2537 }
2538
2539 /* UART RX Fifo Full occurred ----------------------------------------------*/
2540 if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2541 {
2542 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2543 /* Call registered Rx Fifo Full Callback */
2544 huart->RxFifoFullCallback(huart);
2545 #else
2546 /* Call legacy weak Rx Fifo Full Callback */
2547 HAL_UARTEx_RxFifoFullCallback(huart);
2548 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2549 return;
2550 }
2551 }
2552
2553 /**
2554 * @brief Tx Transfer completed callback.
2555 * @param huart UART handle.
2556 * @retval None
2557 */
HAL_UART_TxCpltCallback(UART_HandleTypeDef * huart)2558 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
2559 {
2560 /* Prevent unused argument(s) compilation warning */
2561 UNUSED(huart);
2562
2563 /* NOTE : This function should not be modified, when the callback is needed,
2564 the HAL_UART_TxCpltCallback can be implemented in the user file.
2565 */
2566 }
2567
2568 /**
2569 * @brief Tx Half Transfer completed callback.
2570 * @param huart UART handle.
2571 * @retval None
2572 */
HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef * huart)2573 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
2574 {
2575 /* Prevent unused argument(s) compilation warning */
2576 UNUSED(huart);
2577
2578 /* NOTE: This function should not be modified, when the callback is needed,
2579 the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2580 */
2581 }
2582
2583 /**
2584 * @brief Rx Transfer completed callback.
2585 * @param huart UART handle.
2586 * @retval None
2587 */
HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart)2588 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
2589 {
2590 /* Prevent unused argument(s) compilation warning */
2591 UNUSED(huart);
2592
2593 /* NOTE : This function should not be modified, when the callback is needed,
2594 the HAL_UART_RxCpltCallback can be implemented in the user file.
2595 */
2596 }
2597
2598 /**
2599 * @brief Rx Half Transfer completed callback.
2600 * @param huart UART handle.
2601 * @retval None
2602 */
HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef * huart)2603 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
2604 {
2605 /* Prevent unused argument(s) compilation warning */
2606 UNUSED(huart);
2607
2608 /* NOTE: This function should not be modified, when the callback is needed,
2609 the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2610 */
2611 }
2612
2613 /**
2614 * @brief UART error callback.
2615 * @param huart UART handle.
2616 * @retval None
2617 */
HAL_UART_ErrorCallback(UART_HandleTypeDef * huart)2618 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
2619 {
2620 /* Prevent unused argument(s) compilation warning */
2621 UNUSED(huart);
2622
2623 /* NOTE : This function should not be modified, when the callback is needed,
2624 the HAL_UART_ErrorCallback can be implemented in the user file.
2625 */
2626 }
2627
2628 /**
2629 * @brief UART Abort Complete callback.
2630 * @param huart UART handle.
2631 * @retval None
2632 */
HAL_UART_AbortCpltCallback(UART_HandleTypeDef * huart)2633 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
2634 {
2635 /* Prevent unused argument(s) compilation warning */
2636 UNUSED(huart);
2637
2638 /* NOTE : This function should not be modified, when the callback is needed,
2639 the HAL_UART_AbortCpltCallback can be implemented in the user file.
2640 */
2641 }
2642
2643 /**
2644 * @brief UART Abort Complete callback.
2645 * @param huart UART handle.
2646 * @retval None
2647 */
HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef * huart)2648 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
2649 {
2650 /* Prevent unused argument(s) compilation warning */
2651 UNUSED(huart);
2652
2653 /* NOTE : This function should not be modified, when the callback is needed,
2654 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2655 */
2656 }
2657
2658 /**
2659 * @brief UART Abort Receive Complete callback.
2660 * @param huart UART handle.
2661 * @retval None
2662 */
HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef * huart)2663 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
2664 {
2665 /* Prevent unused argument(s) compilation warning */
2666 UNUSED(huart);
2667
2668 /* NOTE : This function should not be modified, when the callback is needed,
2669 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2670 */
2671 }
2672
2673 /**
2674 * @brief Reception Event Callback (Rx event notification called after use of advanced reception service).
2675 * @param huart UART handle
2676 * @param Size Number of data available in application reception buffer (indicates a position in
2677 * reception buffer until which, data are available)
2678 * @retval None
2679 */
HAL_UARTEx_RxEventCallback(UART_HandleTypeDef * huart,uint16_t Size)2680 __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
2681 {
2682 /* Prevent unused argument(s) compilation warning */
2683 UNUSED(huart);
2684 UNUSED(Size);
2685
2686 /* NOTE : This function should not be modified, when the callback is needed,
2687 the HAL_UARTEx_RxEventCallback can be implemented in the user file.
2688 */
2689 }
2690
2691 /**
2692 * @}
2693 */
2694
2695 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2696 * @brief UART control functions
2697 *
2698 @verbatim
2699 ===============================================================================
2700 ##### Peripheral Control functions #####
2701 ===============================================================================
2702 [..]
2703 This subsection provides a set of functions allowing to control the UART.
2704 (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
2705 (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
2706 (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
2707 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2708 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2709 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2710 (+) UART_SetConfig() API configures the UART peripheral
2711 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2712 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2713 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2714 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2715 (+) HAL_LIN_SendBreak() API transmits the break characters
2716 @endverbatim
2717 * @{
2718 */
2719
2720 /**
2721 * @brief Update on the fly the receiver timeout value in RTOR register.
2722 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2723 * the configuration information for the specified UART module.
2724 * @param TimeoutValue receiver timeout value in number of baud blocks. The timeout
2725 * value must be less or equal to 0x0FFFFFFFF.
2726 * @retval None
2727 */
HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef * huart,uint32_t TimeoutValue)2728 void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
2729 {
2730 if (!(IS_LPUART_INSTANCE(huart->Instance)))
2731 {
2732 assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
2733 MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
2734 }
2735 }
2736
2737 /**
2738 * @brief Enable the UART receiver timeout feature.
2739 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2740 * the configuration information for the specified UART module.
2741 * @retval HAL status
2742 */
HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef * huart)2743 HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
2744 {
2745 if (!(IS_LPUART_INSTANCE(huart->Instance)))
2746 {
2747 if (huart->gState == HAL_UART_STATE_READY)
2748 {
2749 /* Process Locked */
2750 __HAL_LOCK(huart);
2751
2752 huart->gState = HAL_UART_STATE_BUSY;
2753
2754 /* Set the USART RTOEN bit */
2755 SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2756
2757 huart->gState = HAL_UART_STATE_READY;
2758
2759 /* Process Unlocked */
2760 __HAL_UNLOCK(huart);
2761
2762 return HAL_OK;
2763 }
2764 else
2765 {
2766 return HAL_BUSY;
2767 }
2768 }
2769 else
2770 {
2771 return HAL_ERROR;
2772 }
2773 }
2774
2775 /**
2776 * @brief Disable the UART receiver timeout feature.
2777 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2778 * the configuration information for the specified UART module.
2779 * @retval HAL status
2780 */
HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef * huart)2781 HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
2782 {
2783 if (!(IS_LPUART_INSTANCE(huart->Instance)))
2784 {
2785 if (huart->gState == HAL_UART_STATE_READY)
2786 {
2787 /* Process Locked */
2788 __HAL_LOCK(huart);
2789
2790 huart->gState = HAL_UART_STATE_BUSY;
2791
2792 /* Clear the USART RTOEN bit */
2793 CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2794
2795 huart->gState = HAL_UART_STATE_READY;
2796
2797 /* Process Unlocked */
2798 __HAL_UNLOCK(huart);
2799
2800 return HAL_OK;
2801 }
2802 else
2803 {
2804 return HAL_BUSY;
2805 }
2806 }
2807 else
2808 {
2809 return HAL_ERROR;
2810 }
2811 }
2812
2813 /**
2814 * @brief Enable UART in mute mode (does not mean UART enters mute mode;
2815 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2816 * @param huart UART handle.
2817 * @retval HAL status
2818 */
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef * huart)2819 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
2820 {
2821 __HAL_LOCK(huart);
2822
2823 huart->gState = HAL_UART_STATE_BUSY;
2824
2825 /* Enable USART mute mode by setting the MME bit in the CR1 register */
2826 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_MME);
2827
2828 huart->gState = HAL_UART_STATE_READY;
2829
2830 return (UART_CheckIdleState(huart));
2831 }
2832
2833 /**
2834 * @brief Disable UART mute mode (does not mean the UART actually exits mute mode
2835 * as it may not have been in mute mode at this very moment).
2836 * @param huart UART handle.
2837 * @retval HAL status
2838 */
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef * huart)2839 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
2840 {
2841 __HAL_LOCK(huart);
2842
2843 huart->gState = HAL_UART_STATE_BUSY;
2844
2845 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2846 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
2847
2848 huart->gState = HAL_UART_STATE_READY;
2849
2850 return (UART_CheckIdleState(huart));
2851 }
2852
2853 /**
2854 * @brief Enter UART mute mode (means UART actually enters mute mode).
2855 * @note To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2856 * @param huart UART handle.
2857 * @retval None
2858 */
HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef * huart)2859 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
2860 {
2861 __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
2862 }
2863
2864 /**
2865 * @brief Enable the UART transmitter and disable the UART receiver.
2866 * @param huart UART handle.
2867 * @retval HAL status
2868 */
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef * huart)2869 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2870 {
2871 __HAL_LOCK(huart);
2872 huart->gState = HAL_UART_STATE_BUSY;
2873
2874 /* Clear TE and RE bits */
2875 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2876
2877 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2878 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2879
2880 huart->gState = HAL_UART_STATE_READY;
2881
2882 __HAL_UNLOCK(huart);
2883
2884 return HAL_OK;
2885 }
2886
2887 /**
2888 * @brief Enable the UART receiver and disable the UART transmitter.
2889 * @param huart UART handle.
2890 * @retval HAL status.
2891 */
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef * huart)2892 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2893 {
2894 __HAL_LOCK(huart);
2895 huart->gState = HAL_UART_STATE_BUSY;
2896
2897 /* Clear TE and RE bits */
2898 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2899
2900 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2901 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2902
2903 huart->gState = HAL_UART_STATE_READY;
2904
2905 __HAL_UNLOCK(huart);
2906
2907 return HAL_OK;
2908 }
2909
2910
2911 /**
2912 * @brief Transmit break characters.
2913 * @param huart UART handle.
2914 * @retval HAL status
2915 */
HAL_LIN_SendBreak(UART_HandleTypeDef * huart)2916 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2917 {
2918 /* Check the parameters */
2919 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
2920
2921 __HAL_LOCK(huart);
2922
2923 huart->gState = HAL_UART_STATE_BUSY;
2924
2925 /* Send break characters */
2926 __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
2927
2928 huart->gState = HAL_UART_STATE_READY;
2929
2930 __HAL_UNLOCK(huart);
2931
2932 return HAL_OK;
2933 }
2934
2935 /**
2936 * @}
2937 */
2938
2939 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2940 * @brief UART Peripheral State functions
2941 *
2942 @verbatim
2943 ==============================================================================
2944 ##### Peripheral State and Error functions #####
2945 ==============================================================================
2946 [..]
2947 This subsection provides functions allowing to :
2948 (+) Return the UART handle state.
2949 (+) Return the UART handle error code
2950
2951 @endverbatim
2952 * @{
2953 */
2954
2955 /**
2956 * @brief Return the UART handle state.
2957 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2958 * the configuration information for the specified UART.
2959 * @retval HAL state
2960 */
HAL_UART_GetState(UART_HandleTypeDef * huart)2961 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
2962 {
2963 uint32_t temp1;
2964 uint32_t temp2;
2965 temp1 = huart->gState;
2966 temp2 = huart->RxState;
2967
2968 return (HAL_UART_StateTypeDef)(temp1 | temp2);
2969 }
2970
2971 /**
2972 * @brief Return the UART handle error code.
2973 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2974 * the configuration information for the specified UART.
2975 * @retval UART Error Code
2976 */
HAL_UART_GetError(UART_HandleTypeDef * huart)2977 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
2978 {
2979 return huart->ErrorCode;
2980 }
2981 /**
2982 * @}
2983 */
2984
2985 /**
2986 * @}
2987 */
2988
2989 /** @defgroup UART_Private_Functions UART Private Functions
2990 * @{
2991 */
2992
2993 /**
2994 * @brief Initialize the callbacks to their default values.
2995 * @param huart UART handle.
2996 * @retval none
2997 */
2998 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
UART_InitCallbacksToDefault(UART_HandleTypeDef * huart)2999 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
3000 {
3001 /* Init the UART Callback settings */
3002 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
3003 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
3004 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
3005 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
3006 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
3007 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
3008 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
3009 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
3010 huart->WakeupCallback = HAL_UARTEx_WakeupCallback; /* Legacy weak WakeupCallback */
3011 huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback; /* Legacy weak RxFifoFullCallback */
3012 huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback; /* Legacy weak TxFifoEmptyCallback */
3013 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak RxEventCallback */
3014
3015 }
3016 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3017
3018 /**
3019 * @brief Configure the UART peripheral.
3020 * @param huart UART handle.
3021 * @retval HAL status
3022 */
UART_SetConfig(UART_HandleTypeDef * huart)3023 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
3024 {
3025 uint32_t tmpreg;
3026 uint16_t brrtemp;
3027 UART_ClockSourceTypeDef clocksource;
3028 uint32_t usartdiv;
3029 HAL_StatusTypeDef ret = HAL_OK;
3030 uint32_t lpuart_ker_ck_pres;
3031 uint32_t pclk;
3032
3033 /* Check the parameters */
3034 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
3035 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
3036 if (UART_INSTANCE_LOWPOWER(huart))
3037 {
3038 assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
3039 }
3040 else
3041 {
3042 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
3043 assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
3044 }
3045
3046 assert_param(IS_UART_PARITY(huart->Init.Parity));
3047 assert_param(IS_UART_MODE(huart->Init.Mode));
3048 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
3049 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
3050 assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
3051
3052 /*-------------------------- USART CR1 Configuration -----------------------*/
3053 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
3054 * the UART Word Length, Parity, Mode and oversampling:
3055 * set the M bits according to huart->Init.WordLength value
3056 * set PCE and PS bits according to huart->Init.Parity value
3057 * set TE and RE bits according to huart->Init.Mode value
3058 * set OVER8 bit according to huart->Init.OverSampling value */
3059 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
3060 MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
3061
3062 /*-------------------------- USART CR2 Configuration -----------------------*/
3063 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
3064 * to huart->Init.StopBits value */
3065 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
3066
3067 /*-------------------------- USART CR3 Configuration -----------------------*/
3068 /* Configure
3069 * - UART HardWare Flow Control: set CTSE and RTSE bits according
3070 * to huart->Init.HwFlowCtl value
3071 * - one-bit sampling method versus three samples' majority rule according
3072 * to huart->Init.OneBitSampling (not applicable to LPUART) */
3073 tmpreg = (uint32_t)huart->Init.HwFlowCtl;
3074
3075 if (!(UART_INSTANCE_LOWPOWER(huart)))
3076 {
3077 tmpreg |= huart->Init.OneBitSampling;
3078 }
3079 MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
3080
3081 /*-------------------------- USART PRESC Configuration -----------------------*/
3082 /* Configure
3083 * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
3084 MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
3085
3086 /*-------------------------- USART BRR Configuration -----------------------*/
3087 UART_GETCLOCKSOURCE(huart, clocksource);
3088
3089 /* Check LPUART instance */
3090 if (UART_INSTANCE_LOWPOWER(huart))
3091 {
3092 /* Retrieve frequency clock */
3093 switch (clocksource)
3094 {
3095 case UART_CLOCKSOURCE_PCLK1:
3096 pclk = HAL_RCC_GetPCLK1Freq();
3097 break;
3098 case UART_CLOCKSOURCE_HSI:
3099 pclk = (uint32_t) HSI_VALUE;
3100 break;
3101 case UART_CLOCKSOURCE_SYSCLK:
3102 pclk = HAL_RCC_GetSysClockFreq();
3103 break;
3104 case UART_CLOCKSOURCE_LSE:
3105 pclk = (uint32_t) LSE_VALUE;
3106 break;
3107 default:
3108 pclk = 0U;
3109 ret = HAL_ERROR;
3110 break;
3111 }
3112
3113 /* If proper clock source reported */
3114 if (pclk != 0U)
3115 {
3116 /* Compute clock after Prescaler */
3117 lpuart_ker_ck_pres = (pclk / UARTPrescTable[huart->Init.ClockPrescaler]);
3118
3119 /* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
3120 if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
3121 (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
3122 {
3123 ret = HAL_ERROR;
3124 }
3125 else
3126 {
3127 /* Check computed UsartDiv value is in allocated range
3128 (it is forbidden to write values lower than 0x300 in the LPUART_BRR register) */
3129 usartdiv = (uint32_t)(UART_DIV_LPUART(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3130 if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
3131 {
3132 huart->Instance->BRR = usartdiv;
3133 }
3134 else
3135 {
3136 ret = HAL_ERROR;
3137 }
3138 } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) ||
3139 (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
3140 } /* if (pclk != 0) */
3141 }
3142 /* Check UART Over Sampling to set Baud Rate Register */
3143 else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
3144 {
3145 switch (clocksource)
3146 {
3147 case UART_CLOCKSOURCE_PCLK1:
3148 pclk = HAL_RCC_GetPCLK1Freq();
3149 break;
3150 case UART_CLOCKSOURCE_PCLK2:
3151 pclk = HAL_RCC_GetPCLK2Freq();
3152 break;
3153 case UART_CLOCKSOURCE_HSI:
3154 pclk = (uint32_t) HSI_VALUE;
3155 break;
3156 case UART_CLOCKSOURCE_SYSCLK:
3157 pclk = HAL_RCC_GetSysClockFreq();
3158 break;
3159 case UART_CLOCKSOURCE_LSE:
3160 pclk = (uint32_t) LSE_VALUE;
3161 break;
3162 default:
3163 pclk = 0U;
3164 ret = HAL_ERROR;
3165 break;
3166 }
3167
3168 /* USARTDIV must be greater than or equal to 0d16 */
3169 if (pclk != 0U)
3170 {
3171 usartdiv = (uint32_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3172 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3173 {
3174 brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
3175 brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
3176 huart->Instance->BRR = brrtemp;
3177 }
3178 else
3179 {
3180 ret = HAL_ERROR;
3181 }
3182 }
3183 }
3184 else
3185 {
3186 switch (clocksource)
3187 {
3188 case UART_CLOCKSOURCE_PCLK1:
3189 pclk = HAL_RCC_GetPCLK1Freq();
3190 break;
3191 case UART_CLOCKSOURCE_PCLK2:
3192 pclk = HAL_RCC_GetPCLK2Freq();
3193 break;
3194 case UART_CLOCKSOURCE_HSI:
3195 pclk = (uint32_t) HSI_VALUE;
3196 break;
3197 case UART_CLOCKSOURCE_SYSCLK:
3198 pclk = HAL_RCC_GetSysClockFreq();
3199 break;
3200 case UART_CLOCKSOURCE_LSE:
3201 pclk = (uint32_t) LSE_VALUE;
3202 break;
3203 default:
3204 pclk = 0U;
3205 ret = HAL_ERROR;
3206 break;
3207 }
3208
3209 if (pclk != 0U)
3210 {
3211 /* USARTDIV must be greater than or equal to 0d16 */
3212 usartdiv = (uint32_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3213 if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3214 {
3215 huart->Instance->BRR = (uint16_t)usartdiv;
3216 }
3217 else
3218 {
3219 ret = HAL_ERROR;
3220 }
3221 }
3222 }
3223
3224 /* Initialize the number of data to process during RX/TX ISR execution */
3225 huart->NbTxDataToProcess = 1;
3226 huart->NbRxDataToProcess = 1;
3227
3228 /* Clear ISR function pointers */
3229 huart->RxISR = NULL;
3230 huart->TxISR = NULL;
3231
3232 return ret;
3233 }
3234
3235 /**
3236 * @brief Configure the UART peripheral advanced features.
3237 * @param huart UART handle.
3238 * @retval None
3239 */
UART_AdvFeatureConfig(UART_HandleTypeDef * huart)3240 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
3241 {
3242 /* Check whether the set of advanced features to configure is properly set */
3243 assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
3244
3245 /* if required, configure TX pin active level inversion */
3246 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
3247 {
3248 assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
3249 MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
3250 }
3251
3252 /* if required, configure RX pin active level inversion */
3253 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
3254 {
3255 assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
3256 MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
3257 }
3258
3259 /* if required, configure data inversion */
3260 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
3261 {
3262 assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
3263 MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
3264 }
3265
3266 /* if required, configure RX/TX pins swap */
3267 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
3268 {
3269 assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
3270 MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
3271 }
3272
3273 /* if required, configure RX overrun detection disabling */
3274 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
3275 {
3276 assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
3277 MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
3278 }
3279
3280 /* if required, configure DMA disabling on reception error */
3281 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
3282 {
3283 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
3284 MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
3285 }
3286
3287 /* if required, configure auto Baud rate detection scheme */
3288 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
3289 {
3290 assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
3291 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
3292 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
3293 /* set auto Baudrate detection parameters if detection is enabled */
3294 if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
3295 {
3296 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
3297 MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
3298 }
3299 }
3300
3301 /* if required, configure MSB first on communication line */
3302 if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
3303 {
3304 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
3305 MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
3306 }
3307 }
3308
3309 /**
3310 * @brief Check the UART Idle State.
3311 * @param huart UART handle.
3312 * @retval HAL status
3313 */
UART_CheckIdleState(UART_HandleTypeDef * huart)3314 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
3315 {
3316 uint32_t tickstart;
3317
3318 /* Initialize the UART ErrorCode */
3319 huart->ErrorCode = HAL_UART_ERROR_NONE;
3320
3321 /* Init tickstart for timeout management */
3322 tickstart = HAL_GetTick();
3323
3324 /* Check if the Transmitter is enabled */
3325 if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
3326 {
3327 /* Wait until TEACK flag is set */
3328 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3329 {
3330 /* Timeout occurred */
3331 return HAL_TIMEOUT;
3332 }
3333 }
3334
3335 /* Check if the Receiver is enabled */
3336 if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
3337 {
3338 /* Wait until REACK flag is set */
3339 if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3340 {
3341 /* Timeout occurred */
3342 return HAL_TIMEOUT;
3343 }
3344 }
3345
3346 /* Initialize the UART State */
3347 huart->gState = HAL_UART_STATE_READY;
3348 huart->RxState = HAL_UART_STATE_READY;
3349 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3350
3351 __HAL_UNLOCK(huart);
3352
3353 return HAL_OK;
3354 }
3355
3356 /**
3357 * @brief This function handles UART Communication Timeout. It waits
3358 * until a flag is no longer in the specified status.
3359 * @param huart UART handle.
3360 * @param Flag Specifies the UART flag to check
3361 * @param Status The actual Flag status (SET or RESET)
3362 * @param Tickstart Tick start value
3363 * @param Timeout Timeout duration
3364 * @retval HAL status
3365 */
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef * huart,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)3366 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
3367 uint32_t Tickstart, uint32_t Timeout)
3368 {
3369 /* Wait until flag is set */
3370 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
3371 {
3372 /* Check for the Timeout */
3373 if (Timeout != HAL_MAX_DELAY)
3374 {
3375 if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
3376 {
3377 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
3378 interrupts for the interrupt process */
3379 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE |
3380 USART_CR1_TXEIE_TXFNFIE));
3381 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3382
3383 huart->gState = HAL_UART_STATE_READY;
3384 huart->RxState = HAL_UART_STATE_READY;
3385
3386 __HAL_UNLOCK(huart);
3387
3388 return HAL_TIMEOUT;
3389 }
3390
3391 if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U)
3392 {
3393 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
3394 {
3395 /* Clear Receiver Timeout flag*/
3396 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
3397
3398 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error)
3399 interrupts for the interrupt process */
3400 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE |
3401 USART_CR1_TXEIE_TXFNFIE));
3402 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3403
3404 huart->gState = HAL_UART_STATE_READY;
3405 huart->RxState = HAL_UART_STATE_READY;
3406 huart->ErrorCode = HAL_UART_ERROR_RTO;
3407
3408 /* Process Unlocked */
3409 __HAL_UNLOCK(huart);
3410
3411 return HAL_TIMEOUT;
3412 }
3413 }
3414 }
3415 }
3416 return HAL_OK;
3417 }
3418
3419 /**
3420 * @brief Start Receive operation in interrupt mode.
3421 * @note This function could be called by all HAL UART API providing reception in Interrupt mode.
3422 * @note When calling this function, parameters validity is considered as already checked,
3423 * i.e. Rx State, buffer address, ...
3424 * UART Handle is assumed as Locked.
3425 * @param huart UART handle.
3426 * @param pData Pointer to data buffer (u8 or u16 data elements).
3427 * @param Size Amount of data elements (u8 or u16) to be received.
3428 * @retval HAL status
3429 */
UART_Start_Receive_IT(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)3430 HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3431 {
3432 huart->pRxBuffPtr = pData;
3433 huart->RxXferSize = Size;
3434 huart->RxXferCount = Size;
3435 huart->RxISR = NULL;
3436
3437 /* Computation of UART mask to apply to RDR register */
3438 UART_MASK_COMPUTATION(huart);
3439
3440 huart->ErrorCode = HAL_UART_ERROR_NONE;
3441 huart->RxState = HAL_UART_STATE_BUSY_RX;
3442
3443 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3444 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3445
3446 /* Configure Rx interrupt processing */
3447 if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
3448 {
3449 /* Set the Rx ISR function pointer according to the data word length */
3450 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3451 {
3452 huart->RxISR = UART_RxISR_16BIT_FIFOEN;
3453 }
3454 else
3455 {
3456 huart->RxISR = UART_RxISR_8BIT_FIFOEN;
3457 }
3458
3459 __HAL_UNLOCK(huart);
3460
3461 /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
3462 if (huart->Init.Parity != UART_PARITY_NONE)
3463 {
3464 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3465 }
3466 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
3467 }
3468 else
3469 {
3470 /* Set the Rx ISR function pointer according to the data word length */
3471 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3472 {
3473 huart->RxISR = UART_RxISR_16BIT;
3474 }
3475 else
3476 {
3477 huart->RxISR = UART_RxISR_8BIT;
3478 }
3479
3480 __HAL_UNLOCK(huart);
3481
3482 /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
3483 if (huart->Init.Parity != UART_PARITY_NONE)
3484 {
3485 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
3486 }
3487 else
3488 {
3489 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3490 }
3491 }
3492 return HAL_OK;
3493 }
3494
3495 /**
3496 * @brief Start Receive operation in DMA mode.
3497 * @note This function could be called by all HAL UART API providing reception in DMA mode.
3498 * @note When calling this function, parameters validity is considered as already checked,
3499 * i.e. Rx State, buffer address, ...
3500 * UART Handle is assumed as Locked.
3501 * @param huart UART handle.
3502 * @param pData Pointer to data buffer (u8 or u16 data elements).
3503 * @param Size Amount of data elements (u8 or u16) to be received.
3504 * @retval HAL status
3505 */
UART_Start_Receive_DMA(UART_HandleTypeDef * huart,uint8_t * pData,uint16_t Size)3506 HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3507 {
3508 huart->pRxBuffPtr = pData;
3509 huart->RxXferSize = Size;
3510
3511 huart->ErrorCode = HAL_UART_ERROR_NONE;
3512 huart->RxState = HAL_UART_STATE_BUSY_RX;
3513
3514 if (huart->hdmarx != NULL)
3515 {
3516 /* Set the UART DMA transfer complete callback */
3517 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
3518
3519 /* Set the UART DMA Half transfer complete callback */
3520 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
3521
3522 /* Set the DMA error callback */
3523 huart->hdmarx->XferErrorCallback = UART_DMAError;
3524
3525 /* Set the DMA abort callback */
3526 huart->hdmarx->XferAbortCallback = NULL;
3527
3528 /* Enable the DMA channel */
3529 if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
3530 {
3531 /* Set error code to DMA */
3532 huart->ErrorCode = HAL_UART_ERROR_DMA;
3533
3534 __HAL_UNLOCK(huart);
3535
3536 /* Restore huart->RxState to ready */
3537 huart->RxState = HAL_UART_STATE_READY;
3538
3539 return HAL_ERROR;
3540 }
3541 }
3542 __HAL_UNLOCK(huart);
3543
3544 /* Enable the UART Parity Error Interrupt */
3545 if (huart->Init.Parity != UART_PARITY_NONE)
3546 {
3547 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3548 }
3549
3550 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3551 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3552
3553 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
3554 in the UART CR3 register */
3555 ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3556
3557 return HAL_OK;
3558 }
3559
3560
3561 /**
3562 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3563 * @param huart UART handle.
3564 * @retval None
3565 */
UART_EndTxTransfer(UART_HandleTypeDef * huart)3566 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
3567 {
3568 /* Disable TXEIE, TCIE, TXFT interrupts */
3569 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
3570 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
3571
3572 /* At end of Tx process, restore huart->gState to Ready */
3573 huart->gState = HAL_UART_STATE_READY;
3574 }
3575
3576
3577 /**
3578 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3579 * @param huart UART handle.
3580 * @retval None
3581 */
UART_EndRxTransfer(UART_HandleTypeDef * huart)3582 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
3583 {
3584 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3585 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3586 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3587
3588 /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */
3589 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3590 {
3591 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3592 }
3593
3594 /* At end of Rx process, restore huart->RxState to Ready */
3595 huart->RxState = HAL_UART_STATE_READY;
3596 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3597
3598 /* Reset RxIsr function pointer */
3599 huart->RxISR = NULL;
3600 }
3601
3602
3603 /**
3604 * @brief DMA UART transmit process complete callback.
3605 * @param hdma DMA handle.
3606 * @retval None
3607 */
UART_DMATransmitCplt(DMA_HandleTypeDef * hdma)3608 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3609 {
3610 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3611
3612 /* DMA Normal mode */
3613 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
3614 {
3615 huart->TxXferCount = 0U;
3616
3617 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
3618 in the UART CR3 register */
3619 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
3620
3621 /* Enable the UART Transmit Complete Interrupt */
3622 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3623 }
3624 /* DMA Circular mode */
3625 else
3626 {
3627 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3628 /*Call registered Tx complete callback*/
3629 huart->TxCpltCallback(huart);
3630 #else
3631 /*Call legacy weak Tx complete callback*/
3632 HAL_UART_TxCpltCallback(huart);
3633 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3634 }
3635 }
3636
3637 /**
3638 * @brief DMA UART transmit process half complete callback.
3639 * @param hdma DMA handle.
3640 * @retval None
3641 */
UART_DMATxHalfCplt(DMA_HandleTypeDef * hdma)3642 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
3643 {
3644 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3645
3646 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3647 /*Call registered Tx Half complete callback*/
3648 huart->TxHalfCpltCallback(huart);
3649 #else
3650 /*Call legacy weak Tx Half complete callback*/
3651 HAL_UART_TxHalfCpltCallback(huart);
3652 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3653 }
3654
3655 /**
3656 * @brief DMA UART receive process complete callback.
3657 * @param hdma DMA handle.
3658 * @retval None
3659 */
UART_DMAReceiveCplt(DMA_HandleTypeDef * hdma)3660 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3661 {
3662 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3663
3664 /* DMA Normal mode */
3665 if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
3666 {
3667 huart->RxXferCount = 0U;
3668
3669 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3670 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3671 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3672
3673 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3674 in the UART CR3 register */
3675 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3676
3677 /* At end of Rx process, restore huart->RxState to Ready */
3678 huart->RxState = HAL_UART_STATE_READY;
3679
3680 /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */
3681 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3682 {
3683 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3684 }
3685 }
3686
3687 /* Check current reception Mode :
3688 If Reception till IDLE event has been selected : use Rx Event callback */
3689 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3690 {
3691 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3692 /*Call registered Rx Event callback*/
3693 huart->RxEventCallback(huart, huart->RxXferSize);
3694 #else
3695 /*Call legacy weak Rx Event callback*/
3696 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3697 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3698 }
3699 else
3700 {
3701 /* In other cases : use Rx Complete callback */
3702 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3703 /*Call registered Rx complete callback*/
3704 huart->RxCpltCallback(huart);
3705 #else
3706 /*Call legacy weak Rx complete callback*/
3707 HAL_UART_RxCpltCallback(huart);
3708 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3709 }
3710 }
3711
3712 /**
3713 * @brief DMA UART receive process half complete callback.
3714 * @param hdma DMA handle.
3715 * @retval None
3716 */
UART_DMARxHalfCplt(DMA_HandleTypeDef * hdma)3717 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
3718 {
3719 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3720
3721 /* Check current reception Mode :
3722 If Reception till IDLE event has been selected : use Rx Event callback */
3723 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3724 {
3725 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3726 /*Call registered Rx Event callback*/
3727 huart->RxEventCallback(huart, huart->RxXferSize / 2U);
3728 #else
3729 /*Call legacy weak Rx Event callback*/
3730 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U);
3731 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3732 }
3733 else
3734 {
3735 /* In other cases : use Rx Half Complete callback */
3736 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3737 /*Call registered Rx Half complete callback*/
3738 huart->RxHalfCpltCallback(huart);
3739 #else
3740 /*Call legacy weak Rx Half complete callback*/
3741 HAL_UART_RxHalfCpltCallback(huart);
3742 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3743 }
3744 }
3745
3746 /**
3747 * @brief DMA UART communication error callback.
3748 * @param hdma DMA handle.
3749 * @retval None
3750 */
UART_DMAError(DMA_HandleTypeDef * hdma)3751 static void UART_DMAError(DMA_HandleTypeDef *hdma)
3752 {
3753 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3754
3755 const HAL_UART_StateTypeDef gstate = huart->gState;
3756 const HAL_UART_StateTypeDef rxstate = huart->RxState;
3757
3758 /* Stop UART DMA Tx request if ongoing */
3759 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
3760 (gstate == HAL_UART_STATE_BUSY_TX))
3761 {
3762 huart->TxXferCount = 0U;
3763 UART_EndTxTransfer(huart);
3764 }
3765
3766 /* Stop UART DMA Rx request if ongoing */
3767 if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
3768 (rxstate == HAL_UART_STATE_BUSY_RX))
3769 {
3770 huart->RxXferCount = 0U;
3771 UART_EndRxTransfer(huart);
3772 }
3773
3774 huart->ErrorCode |= HAL_UART_ERROR_DMA;
3775
3776 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3777 /*Call registered error callback*/
3778 huart->ErrorCallback(huart);
3779 #else
3780 /*Call legacy weak error callback*/
3781 HAL_UART_ErrorCallback(huart);
3782 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3783 }
3784
3785 /**
3786 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
3787 * (To be called at end of DMA Abort procedure following error occurrence).
3788 * @param hdma DMA handle.
3789 * @retval None
3790 */
UART_DMAAbortOnError(DMA_HandleTypeDef * hdma)3791 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3792 {
3793 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3794 huart->RxXferCount = 0U;
3795 huart->TxXferCount = 0U;
3796
3797 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3798 /*Call registered error callback*/
3799 huart->ErrorCallback(huart);
3800 #else
3801 /*Call legacy weak error callback*/
3802 HAL_UART_ErrorCallback(huart);
3803 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3804 }
3805
3806 /**
3807 * @brief DMA UART Tx communication abort callback, when initiated by user
3808 * (To be called at end of DMA Tx Abort procedure following user abort request).
3809 * @note When this callback is executed, User Abort complete call back is called only if no
3810 * Abort still ongoing for Rx DMA Handle.
3811 * @param hdma DMA handle.
3812 * @retval None
3813 */
UART_DMATxAbortCallback(DMA_HandleTypeDef * hdma)3814 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3815 {
3816 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3817
3818 huart->hdmatx->XferAbortCallback = NULL;
3819
3820 /* Check if an Abort process is still ongoing */
3821 if (huart->hdmarx != NULL)
3822 {
3823 if (huart->hdmarx->XferAbortCallback != NULL)
3824 {
3825 return;
3826 }
3827 }
3828
3829 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3830 huart->TxXferCount = 0U;
3831 huart->RxXferCount = 0U;
3832
3833 /* Reset errorCode */
3834 huart->ErrorCode = HAL_UART_ERROR_NONE;
3835
3836 /* Clear the Error flags in the ICR register */
3837 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3838
3839 /* Flush the whole TX FIFO (if needed) */
3840 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3841 {
3842 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3843 }
3844
3845 /* Restore huart->gState and huart->RxState to Ready */
3846 huart->gState = HAL_UART_STATE_READY;
3847 huart->RxState = HAL_UART_STATE_READY;
3848 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3849
3850 /* Call user Abort complete callback */
3851 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3852 /* Call registered Abort complete callback */
3853 huart->AbortCpltCallback(huart);
3854 #else
3855 /* Call legacy weak Abort complete callback */
3856 HAL_UART_AbortCpltCallback(huart);
3857 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3858 }
3859
3860
3861 /**
3862 * @brief DMA UART Rx communication abort callback, when initiated by user
3863 * (To be called at end of DMA Rx Abort procedure following user abort request).
3864 * @note When this callback is executed, User Abort complete call back is called only if no
3865 * Abort still ongoing for Tx DMA Handle.
3866 * @param hdma DMA handle.
3867 * @retval None
3868 */
UART_DMARxAbortCallback(DMA_HandleTypeDef * hdma)3869 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3870 {
3871 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3872
3873 huart->hdmarx->XferAbortCallback = NULL;
3874
3875 /* Check if an Abort process is still ongoing */
3876 if (huart->hdmatx != NULL)
3877 {
3878 if (huart->hdmatx->XferAbortCallback != NULL)
3879 {
3880 return;
3881 }
3882 }
3883
3884 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3885 huart->TxXferCount = 0U;
3886 huart->RxXferCount = 0U;
3887
3888 /* Reset errorCode */
3889 huart->ErrorCode = HAL_UART_ERROR_NONE;
3890
3891 /* Clear the Error flags in the ICR register */
3892 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3893
3894 /* Discard the received data */
3895 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3896
3897 /* Restore huart->gState and huart->RxState to Ready */
3898 huart->gState = HAL_UART_STATE_READY;
3899 huart->RxState = HAL_UART_STATE_READY;
3900 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3901
3902 /* Call user Abort complete callback */
3903 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3904 /* Call registered Abort complete callback */
3905 huart->AbortCpltCallback(huart);
3906 #else
3907 /* Call legacy weak Abort complete callback */
3908 HAL_UART_AbortCpltCallback(huart);
3909 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3910 }
3911
3912
3913 /**
3914 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
3915 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3916 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3917 * and leads to user Tx Abort Complete callback execution).
3918 * @param hdma DMA handle.
3919 * @retval None
3920 */
UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef * hdma)3921 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3922 {
3923 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3924
3925 huart->TxXferCount = 0U;
3926
3927 /* Flush the whole TX FIFO (if needed) */
3928 if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3929 {
3930 __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3931 }
3932
3933 /* Restore huart->gState to Ready */
3934 huart->gState = HAL_UART_STATE_READY;
3935
3936 /* Call user Abort complete callback */
3937 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3938 /* Call registered Abort Transmit Complete Callback */
3939 huart->AbortTransmitCpltCallback(huart);
3940 #else
3941 /* Call legacy weak Abort Transmit Complete Callback */
3942 HAL_UART_AbortTransmitCpltCallback(huart);
3943 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3944 }
3945
3946 /**
3947 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
3948 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
3949 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3950 * and leads to user Rx Abort Complete callback execution).
3951 * @param hdma DMA handle.
3952 * @retval None
3953 */
UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef * hdma)3954 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3955 {
3956 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3957
3958 huart->RxXferCount = 0U;
3959
3960 /* Clear the Error flags in the ICR register */
3961 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3962
3963 /* Discard the received data */
3964 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3965
3966 /* Restore huart->RxState to Ready */
3967 huart->RxState = HAL_UART_STATE_READY;
3968 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3969
3970 /* Call user Abort complete callback */
3971 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3972 /* Call registered Abort Receive Complete Callback */
3973 huart->AbortReceiveCpltCallback(huart);
3974 #else
3975 /* Call legacy weak Abort Receive Complete Callback */
3976 HAL_UART_AbortReceiveCpltCallback(huart);
3977 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3978 }
3979
3980 /**
3981 * @brief TX interrupt handler for 7 or 8 bits data word length .
3982 * @note Function is called under interruption only, once
3983 * interruptions have been enabled by HAL_UART_Transmit_IT().
3984 * @param huart UART handle.
3985 * @retval None
3986 */
UART_TxISR_8BIT(UART_HandleTypeDef * huart)3987 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
3988 {
3989 /* Check that a Tx process is ongoing */
3990 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3991 {
3992 if (huart->TxXferCount == 0U)
3993 {
3994 /* Disable the UART Transmit Data Register Empty Interrupt */
3995 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
3996
3997 /* Enable the UART Transmit Complete Interrupt */
3998 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3999 }
4000 else
4001 {
4002 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4003 huart->pTxBuffPtr++;
4004 huart->TxXferCount--;
4005 }
4006 }
4007 }
4008
4009 /**
4010 * @brief TX interrupt handler for 9 bits data word length.
4011 * @note Function is called under interruption only, once
4012 * interruptions have been enabled by HAL_UART_Transmit_IT().
4013 * @param huart UART handle.
4014 * @retval None
4015 */
UART_TxISR_16BIT(UART_HandleTypeDef * huart)4016 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
4017 {
4018 const uint16_t *tmp;
4019
4020 /* Check that a Tx process is ongoing */
4021 if (huart->gState == HAL_UART_STATE_BUSY_TX)
4022 {
4023 if (huart->TxXferCount == 0U)
4024 {
4025 /* Disable the UART Transmit Data Register Empty Interrupt */
4026 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
4027
4028 /* Enable the UART Transmit Complete Interrupt */
4029 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4030 }
4031 else
4032 {
4033 tmp = (const uint16_t *) huart->pTxBuffPtr;
4034 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4035 huart->pTxBuffPtr += 2U;
4036 huart->TxXferCount--;
4037 }
4038 }
4039 }
4040
4041 /**
4042 * @brief TX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
4043 * @note Function is called under interruption only, once
4044 * interruptions have been enabled by HAL_UART_Transmit_IT().
4045 * @param huart UART handle.
4046 * @retval None
4047 */
UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef * huart)4048 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4049 {
4050 uint16_t nb_tx_data;
4051
4052 /* Check that a Tx process is ongoing */
4053 if (huart->gState == HAL_UART_STATE_BUSY_TX)
4054 {
4055 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4056 {
4057 if (huart->TxXferCount == 0U)
4058 {
4059 /* Disable the TX FIFO threshold interrupt */
4060 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4061
4062 /* Enable the UART Transmit Complete Interrupt */
4063 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4064
4065 break; /* force exit loop */
4066 }
4067 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4068 {
4069 huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4070 huart->pTxBuffPtr++;
4071 huart->TxXferCount--;
4072 }
4073 else
4074 {
4075 /* Nothing to do */
4076 }
4077 }
4078 }
4079 }
4080
4081 /**
4082 * @brief TX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4083 * @note Function is called under interruption only, once
4084 * interruptions have been enabled by HAL_UART_Transmit_IT().
4085 * @param huart UART handle.
4086 * @retval None
4087 */
UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef * huart)4088 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4089 {
4090 const uint16_t *tmp;
4091 uint16_t nb_tx_data;
4092
4093 /* Check that a Tx process is ongoing */
4094 if (huart->gState == HAL_UART_STATE_BUSY_TX)
4095 {
4096 for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4097 {
4098 if (huart->TxXferCount == 0U)
4099 {
4100 /* Disable the TX FIFO threshold interrupt */
4101 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4102
4103 /* Enable the UART Transmit Complete Interrupt */
4104 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4105
4106 break; /* force exit loop */
4107 }
4108 else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4109 {
4110 tmp = (const uint16_t *) huart->pTxBuffPtr;
4111 huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4112 huart->pTxBuffPtr += 2U;
4113 huart->TxXferCount--;
4114 }
4115 else
4116 {
4117 /* Nothing to do */
4118 }
4119 }
4120 }
4121 }
4122
4123 /**
4124 * @brief Wrap up transmission in non-blocking mode.
4125 * @param huart pointer to a UART_HandleTypeDef structure that contains
4126 * the configuration information for the specified UART module.
4127 * @retval None
4128 */
UART_EndTransmit_IT(UART_HandleTypeDef * huart)4129 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
4130 {
4131 /* Disable the UART Transmit Complete Interrupt */
4132 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4133
4134 /* Tx process is ended, restore huart->gState to Ready */
4135 huart->gState = HAL_UART_STATE_READY;
4136
4137 /* Cleat TxISR function pointer */
4138 huart->TxISR = NULL;
4139
4140 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4141 /*Call registered Tx complete callback*/
4142 huart->TxCpltCallback(huart);
4143 #else
4144 /*Call legacy weak Tx complete callback*/
4145 HAL_UART_TxCpltCallback(huart);
4146 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4147 }
4148
4149 /**
4150 * @brief RX interrupt handler for 7 or 8 bits data word length .
4151 * @param huart UART handle.
4152 * @retval None
4153 */
UART_RxISR_8BIT(UART_HandleTypeDef * huart)4154 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
4155 {
4156 uint16_t uhMask = huart->Mask;
4157 uint16_t uhdata;
4158
4159 /* Check that a Rx process is ongoing */
4160 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4161 {
4162 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4163 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4164 huart->pRxBuffPtr++;
4165 huart->RxXferCount--;
4166
4167 if (huart->RxXferCount == 0U)
4168 {
4169 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
4170 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4171
4172 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4173 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4174
4175 /* Rx process is completed, restore huart->RxState to Ready */
4176 huart->RxState = HAL_UART_STATE_READY;
4177
4178 /* Clear RxISR function pointer */
4179 huart->RxISR = NULL;
4180
4181 /* Check current reception Mode :
4182 If Reception till IDLE event has been selected : */
4183 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4184 {
4185 /* Set reception type to Standard */
4186 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4187
4188 /* Disable IDLE interrupt */
4189 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4190
4191 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4192 {
4193 /* Clear IDLE Flag */
4194 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4195 }
4196 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4197 /*Call registered Rx Event callback*/
4198 huart->RxEventCallback(huart, huart->RxXferSize);
4199 #else
4200 /*Call legacy weak Rx Event callback*/
4201 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4202 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4203 }
4204 else
4205 {
4206 /* Standard reception API called */
4207 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4208 /*Call registered Rx complete callback*/
4209 huart->RxCpltCallback(huart);
4210 #else
4211 /*Call legacy weak Rx complete callback*/
4212 HAL_UART_RxCpltCallback(huart);
4213 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4214 }
4215 }
4216 }
4217 else
4218 {
4219 /* Clear RXNE interrupt flag */
4220 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4221 }
4222 }
4223
4224 /**
4225 * @brief RX interrupt handler for 9 bits data word length .
4226 * @note Function is called under interruption only, once
4227 * interruptions have been enabled by HAL_UART_Receive_IT()
4228 * @param huart UART handle.
4229 * @retval None
4230 */
UART_RxISR_16BIT(UART_HandleTypeDef * huart)4231 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
4232 {
4233 uint16_t *tmp;
4234 uint16_t uhMask = huart->Mask;
4235 uint16_t uhdata;
4236
4237 /* Check that a Rx process is ongoing */
4238 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4239 {
4240 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4241 tmp = (uint16_t *) huart->pRxBuffPtr ;
4242 *tmp = (uint16_t)(uhdata & uhMask);
4243 huart->pRxBuffPtr += 2U;
4244 huart->RxXferCount--;
4245
4246 if (huart->RxXferCount == 0U)
4247 {
4248 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
4249 ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4250
4251 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4252 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4253
4254 /* Rx process is completed, restore huart->RxState to Ready */
4255 huart->RxState = HAL_UART_STATE_READY;
4256
4257 /* Clear RxISR function pointer */
4258 huart->RxISR = NULL;
4259
4260 /* Check current reception Mode :
4261 If Reception till IDLE event has been selected : */
4262 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4263 {
4264 /* Set reception type to Standard */
4265 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4266
4267 /* Disable IDLE interrupt */
4268 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4269
4270 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4271 {
4272 /* Clear IDLE Flag */
4273 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4274 }
4275 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4276 /*Call registered Rx Event callback*/
4277 huart->RxEventCallback(huart, huart->RxXferSize);
4278 #else
4279 /*Call legacy weak Rx Event callback*/
4280 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4281 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4282 }
4283 else
4284 {
4285 /* Standard reception API called */
4286 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4287 /*Call registered Rx complete callback*/
4288 huart->RxCpltCallback(huart);
4289 #else
4290 /*Call legacy weak Rx complete callback*/
4291 HAL_UART_RxCpltCallback(huart);
4292 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4293 }
4294 }
4295 }
4296 else
4297 {
4298 /* Clear RXNE interrupt flag */
4299 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4300 }
4301 }
4302
4303 /**
4304 * @brief RX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
4305 * @note Function is called under interruption only, once
4306 * interruptions have been enabled by HAL_UART_Receive_IT()
4307 * @param huart UART handle.
4308 * @retval None
4309 */
UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef * huart)4310 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4311 {
4312 uint16_t uhMask = huart->Mask;
4313 uint16_t uhdata;
4314 uint16_t nb_rx_data;
4315 uint16_t rxdatacount;
4316 uint32_t isrflags = READ_REG(huart->Instance->ISR);
4317 uint32_t cr1its = READ_REG(huart->Instance->CR1);
4318 uint32_t cr3its = READ_REG(huart->Instance->CR3);
4319
4320 /* Check that a Rx process is ongoing */
4321 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4322 {
4323 nb_rx_data = huart->NbRxDataToProcess;
4324 while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4325 {
4326 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4327 *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4328 huart->pRxBuffPtr++;
4329 huart->RxXferCount--;
4330 isrflags = READ_REG(huart->Instance->ISR);
4331
4332 /* If some non blocking errors occurred */
4333 if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4334 {
4335 /* UART parity error interrupt occurred -------------------------------------*/
4336 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4337 {
4338 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4339
4340 huart->ErrorCode |= HAL_UART_ERROR_PE;
4341 }
4342
4343 /* UART frame error interrupt occurred --------------------------------------*/
4344 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4345 {
4346 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4347
4348 huart->ErrorCode |= HAL_UART_ERROR_FE;
4349 }
4350
4351 /* UART noise error interrupt occurred --------------------------------------*/
4352 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4353 {
4354 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4355
4356 huart->ErrorCode |= HAL_UART_ERROR_NE;
4357 }
4358
4359 /* Call UART Error Call back function if need be ----------------------------*/
4360 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4361 {
4362 /* Non Blocking error : transfer could go on.
4363 Error is notified to user through user error callback */
4364 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4365 /*Call registered error callback*/
4366 huart->ErrorCallback(huart);
4367 #else
4368 /*Call legacy weak error callback*/
4369 HAL_UART_ErrorCallback(huart);
4370 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4371 huart->ErrorCode = HAL_UART_ERROR_NONE;
4372 }
4373 }
4374
4375 if (huart->RxXferCount == 0U)
4376 {
4377 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4378 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4379
4380 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4381 and RX FIFO Threshold interrupt */
4382 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4383
4384 /* Rx process is completed, restore huart->RxState to Ready */
4385 huart->RxState = HAL_UART_STATE_READY;
4386
4387 /* Clear RxISR function pointer */
4388 huart->RxISR = NULL;
4389
4390 /* Check current reception Mode :
4391 If Reception till IDLE event has been selected : */
4392 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4393 {
4394 /* Set reception type to Standard */
4395 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4396
4397 /* Disable IDLE interrupt */
4398 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4399
4400 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4401 {
4402 /* Clear IDLE Flag */
4403 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4404 }
4405 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4406 /*Call registered Rx Event callback*/
4407 huart->RxEventCallback(huart, huart->RxXferSize);
4408 #else
4409 /*Call legacy weak Rx Event callback*/
4410 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4411 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4412 }
4413 else
4414 {
4415 /* Standard reception API called */
4416 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4417 /*Call registered Rx complete callback*/
4418 huart->RxCpltCallback(huart);
4419 #else
4420 /*Call legacy weak Rx complete callback*/
4421 HAL_UART_RxCpltCallback(huart);
4422 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4423 }
4424 }
4425 }
4426
4427 /* When remaining number of bytes to receive is less than the RX FIFO
4428 threshold, next incoming frames are processed as if FIFO mode was
4429 disabled (i.e. one interrupt per received frame).
4430 */
4431 rxdatacount = huart->RxXferCount;
4432 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4433 {
4434 /* Disable the UART RXFT interrupt*/
4435 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4436
4437 /* Update the RxISR function pointer */
4438 huart->RxISR = UART_RxISR_8BIT;
4439
4440 /* Enable the UART Data Register Not Empty interrupt */
4441 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4442 }
4443 }
4444 else
4445 {
4446 /* Clear RXNE interrupt flag */
4447 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4448 }
4449 }
4450
4451 /**
4452 * @brief RX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4453 * @note Function is called under interruption only, once
4454 * interruptions have been enabled by HAL_UART_Receive_IT()
4455 * @param huart UART handle.
4456 * @retval None
4457 */
UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef * huart)4458 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4459 {
4460 uint16_t *tmp;
4461 uint16_t uhMask = huart->Mask;
4462 uint16_t uhdata;
4463 uint16_t nb_rx_data;
4464 uint16_t rxdatacount;
4465 uint32_t isrflags = READ_REG(huart->Instance->ISR);
4466 uint32_t cr1its = READ_REG(huart->Instance->CR1);
4467 uint32_t cr3its = READ_REG(huart->Instance->CR3);
4468
4469 /* Check that a Rx process is ongoing */
4470 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4471 {
4472 nb_rx_data = huart->NbRxDataToProcess;
4473 while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4474 {
4475 uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4476 tmp = (uint16_t *) huart->pRxBuffPtr ;
4477 *tmp = (uint16_t)(uhdata & uhMask);
4478 huart->pRxBuffPtr += 2U;
4479 huart->RxXferCount--;
4480 isrflags = READ_REG(huart->Instance->ISR);
4481
4482 /* If some non blocking errors occurred */
4483 if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4484 {
4485 /* UART parity error interrupt occurred -------------------------------------*/
4486 if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4487 {
4488 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4489
4490 huart->ErrorCode |= HAL_UART_ERROR_PE;
4491 }
4492
4493 /* UART frame error interrupt occurred --------------------------------------*/
4494 if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4495 {
4496 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4497
4498 huart->ErrorCode |= HAL_UART_ERROR_FE;
4499 }
4500
4501 /* UART noise error interrupt occurred --------------------------------------*/
4502 if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4503 {
4504 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4505
4506 huart->ErrorCode |= HAL_UART_ERROR_NE;
4507 }
4508
4509 /* Call UART Error Call back function if need be ----------------------------*/
4510 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4511 {
4512 /* Non Blocking error : transfer could go on.
4513 Error is notified to user through user error callback */
4514 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4515 /*Call registered error callback*/
4516 huart->ErrorCallback(huart);
4517 #else
4518 /*Call legacy weak error callback*/
4519 HAL_UART_ErrorCallback(huart);
4520 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4521 huart->ErrorCode = HAL_UART_ERROR_NONE;
4522 }
4523 }
4524
4525 if (huart->RxXferCount == 0U)
4526 {
4527 /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4528 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4529
4530 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4531 and RX FIFO Threshold interrupt */
4532 ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4533
4534 /* Rx process is completed, restore huart->RxState to Ready */
4535 huart->RxState = HAL_UART_STATE_READY;
4536
4537 /* Clear RxISR function pointer */
4538 huart->RxISR = NULL;
4539
4540 /* Check current reception Mode :
4541 If Reception till IDLE event has been selected : */
4542 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4543 {
4544 /* Set reception type to Standard */
4545 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4546
4547 /* Disable IDLE interrupt */
4548 ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4549
4550 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4551 {
4552 /* Clear IDLE Flag */
4553 __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4554 }
4555 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4556 /*Call registered Rx Event callback*/
4557 huart->RxEventCallback(huart, huart->RxXferSize);
4558 #else
4559 /*Call legacy weak Rx Event callback*/
4560 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4561 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4562 }
4563 else
4564 {
4565 /* Standard reception API called */
4566 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4567 /*Call registered Rx complete callback*/
4568 huart->RxCpltCallback(huart);
4569 #else
4570 /*Call legacy weak Rx complete callback*/
4571 HAL_UART_RxCpltCallback(huart);
4572 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4573 }
4574 }
4575 }
4576
4577 /* When remaining number of bytes to receive is less than the RX FIFO
4578 threshold, next incoming frames are processed as if FIFO mode was
4579 disabled (i.e. one interrupt per received frame).
4580 */
4581 rxdatacount = huart->RxXferCount;
4582 if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4583 {
4584 /* Disable the UART RXFT interrupt*/
4585 ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4586
4587 /* Update the RxISR function pointer */
4588 huart->RxISR = UART_RxISR_16BIT;
4589
4590 /* Enable the UART Data Register Not Empty interrupt */
4591 ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4592 }
4593 }
4594 else
4595 {
4596 /* Clear RXNE interrupt flag */
4597 __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4598 }
4599 }
4600
4601 /**
4602 * @}
4603 */
4604
4605 #endif /* HAL_UART_MODULE_ENABLED */
4606 /**
4607 * @}
4608 */
4609
4610 /**
4611 * @}
4612 */
4613
4614