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