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