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