source: S-port/trunk/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c

Last change on this file was 1, checked in by AlexLir, 3 years ago
File size: 131.9 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_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 * + Peripheral State and Errors functions
12 @verbatim
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
16 [..]
17 The UART HAL driver can be used as follows:
18
19 (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
20 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
21 (##) Enable the USARTx interface clock.
22 (##) UART pins configuration:
23 (+++) Enable the clock for the UART GPIOs.
24 (+++) Configure these UART pins (TX as alternate function pull-up, RX as alternate function Input).
25 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
26 and HAL_UART_Receive_IT() APIs):
27 (+++) Configure the USARTx interrupt priority.
28 (+++) Enable the NVIC USART IRQ handle.
29 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
30 and HAL_UART_Receive_DMA() APIs):
31 (+++) Declare a DMA handle structure for the Tx/Rx stream.
32 (+++) Enable the DMAx interface clock.
33 (+++) Configure the declared DMA handle structure with the required
34 Tx/Rx parameters.
35 (+++) Configure the DMA Tx/Rx stream.
36 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
37 (+++) Configure the priority and enable the NVIC for the transfer complete
38 interrupt on the DMA Tx/Rx stream.
39 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
40 (used for last byte sending completion detection in DMA non circular mode)
41
42 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
43 flow control and Mode(Receiver/Transmitter) in the huart Init structure.
44
45 (#) For the UART asynchronous mode, initialize the UART registers by calling
46 the HAL_UART_Init() API.
47
48 (#) For the UART Half duplex mode, initialize the UART registers by calling
49 the HAL_HalfDuplex_Init() API.
50
51 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
52
53 (#) For the Multi-Processor mode, initialize the UART registers by calling
54 the HAL_MultiProcessor_Init() API.
55
56 [..]
57 (@) The specific UART interrupts (Transmission complete interrupt,
58 RXNE interrupt and Error Interrupts) will be managed using the macros
59 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
60 and receive process.
61
62 [..]
63 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
64 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
65 HAL_UART_MspInit() API.
66
67 ##### Callback registration #####
68 ==================================
69
70 [..]
71 The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
72 allows the user to configure dynamically the driver callbacks.
73
74 [..]
75 Use Function @ref HAL_UART_RegisterCallback() to register a user callback.
76 Function @ref HAL_UART_RegisterCallback() allows to register following callbacks:
77 (+) TxHalfCpltCallback : Tx Half Complete Callback.
78 (+) TxCpltCallback : Tx Complete Callback.
79 (+) RxHalfCpltCallback : Rx Half Complete Callback.
80 (+) RxCpltCallback : Rx Complete Callback.
81 (+) ErrorCallback : Error Callback.
82 (+) AbortCpltCallback : Abort Complete Callback.
83 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
84 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
85 (+) MspInitCallback : UART MspInit.
86 (+) MspDeInitCallback : UART MspDeInit.
87 This function takes as parameters the HAL peripheral handle, the Callback ID
88 and a pointer to the user callback function.
89
90 [..]
91 Use function @ref HAL_UART_UnRegisterCallback() to reset a callback to the default
92 weak (surcharged) function.
93 @ref HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
94 and the Callback ID.
95 This function allows to reset following callbacks:
96 (+) TxHalfCpltCallback : Tx Half Complete Callback.
97 (+) TxCpltCallback : Tx Complete Callback.
98 (+) RxHalfCpltCallback : Rx Half Complete Callback.
99 (+) RxCpltCallback : Rx Complete Callback.
100 (+) ErrorCallback : Error Callback.
101 (+) AbortCpltCallback : Abort Complete Callback.
102 (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
103 (+) AbortReceiveCpltCallback : Abort Receive Complete Callback.
104 (+) MspInitCallback : UART MspInit.
105 (+) MspDeInitCallback : UART MspDeInit.
106
107 [..]
108 For specific callback RxEventCallback, use dedicated registration/reset functions:
109 respectively @ref HAL_UART_RegisterRxEventCallback() , @ref HAL_UART_UnRegisterRxEventCallback().
110
111 [..]
112 By default, after the @ref HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
113 all callbacks are set to the corresponding weak (surcharged) functions:
114 examples @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback().
115 Exception done for MspInit and MspDeInit functions that are respectively
116 reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init()
117 and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
118 If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref HAL_UART_DeInit()
119 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
120
121 [..]
122 Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
123 Exception done MspInit/MspDeInit that can be registered/unregistered
124 in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
125 MspInit/DeInit callbacks can be used during the Init/DeInit.
126 In that case first register the MspInit/MspDeInit user callbacks
127 using @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit()
128 or @ref HAL_UART_Init() function.
129
130 [..]
131 When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
132 not defined, the callback registration feature is not available
133 and weak (surcharged) callbacks are used.
134
135 [..]
136 Three operation modes are available within this driver :
137
138 *** Polling mode IO operation ***
139 =================================
140 [..]
141 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
142 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
143
144 *** Interrupt mode IO operation ***
145 ===================================
146 [..]
147 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
148 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
149 add his own code by customization of function pointer HAL_UART_TxCpltCallback
150 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
151 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
152 add his own code by customization of function pointer HAL_UART_RxCpltCallback
153 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
154 add his own code by customization of function pointer HAL_UART_ErrorCallback
155
156 *** DMA mode IO operation ***
157 ==============================
158 [..]
159 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
160 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
161 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
162 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
163 add his own code by customization of function pointer HAL_UART_TxCpltCallback
164 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
165 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
166 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
167 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
168 add his own code by customization of function pointer HAL_UART_RxCpltCallback
169 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
170 add his own code by customization of function pointer HAL_UART_ErrorCallback
171 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
172 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
173 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
174
175
176 [..] This subsection also provides a set of additional functions providing enhanced reception
177 services to user. (For example, these functions allow application to handle use cases
178 where number of data to be received is unknown).
179
180 (#) Compared to standard reception services which only consider number of received
181 data elements as reception completion criteria, these functions also consider additional events
182 as triggers for updating reception status to caller :
183 (+) Detection of inactivity period (RX line has not been active for a given period).
184 (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
185 for 1 frame time, after last received byte.
186
187 (#) There are two mode of transfer:
188 (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
189 or till IDLE event occurs. Reception is handled only during function execution.
190 When function exits, no data reception could occur. HAL status and number of actually received data elements,
191 are returned by function after finishing transfer.
192 (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
193 These API's return the HAL status.
194 The end of the data processing will be indicated through the
195 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
196 The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
197 The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.
198
199 (#) Blocking mode API:
200 (+) HAL_UARTEx_ReceiveToIdle()
201
202 (#) Non-Blocking mode API with Interrupt:
203 (+) HAL_UARTEx_ReceiveToIdle_IT()
204
205 (#) Non-Blocking mode API with DMA:
206 (+) HAL_UARTEx_ReceiveToIdle_DMA()
207
208
209 *** UART HAL driver macros list ***
210 =============================================
211 [..]
212 Below the list of most used macros in UART HAL driver.
213
214 (+) __HAL_UART_ENABLE: Enable the UART peripheral
215 (+) __HAL_UART_DISABLE: Disable the UART peripheral
216 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
217 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
218 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
219 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
220 (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
221
222 [..]
223 (@) You can refer to the UART HAL driver header file for more useful macros
224
225 @endverbatim
226 [..]
227 (@) Additional remark: If the parity is enabled, then the MSB bit of the data written
228 in the data register is transmitted but is changed by the parity bit.
229 Depending on the frame length defined by the M bit (8-bits or 9-bits),
230 the possible UART frame formats are as listed in the following table:
231 +-------------------------------------------------------------+
232 | M bit | PCE bit | UART frame |
233 |---------------------|---------------------------------------|
234 | 0 | 0 | | SB | 8 bit data | STB | |
235 |---------|-----------|---------------------------------------|
236 | 0 | 1 | | SB | 7 bit data | PB | STB | |
237 |---------|-----------|---------------------------------------|
238 | 1 | 0 | | SB | 9 bit data | STB | |
239 |---------|-----------|---------------------------------------|
240 | 1 | 1 | | SB | 8 bit data | PB | STB | |
241 +-------------------------------------------------------------+
242 ******************************************************************************
243 * @attention
244 *
245 * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
246 * All rights reserved.</center></h2>
247 *
248 * This software component is licensed by ST under BSD 3-Clause license,
249 * the "License"; You may not use this file except in compliance with the
250 * License. You may obtain a copy of the License at:
251 * opensource.org/licenses/BSD-3-Clause
252 *
253 ******************************************************************************
254 */
255
256/* Includes ------------------------------------------------------------------*/
257#include "stm32f4xx_hal.h"
258
259/** @addtogroup STM32F4xx_HAL_Driver
260 * @{
261 */
262
263/** @defgroup UART UART
264 * @brief HAL UART module driver
265 * @{
266 */
267#ifdef HAL_UART_MODULE_ENABLED
268
269/* Private typedef -----------------------------------------------------------*/
270/* Private define ------------------------------------------------------------*/
271/** @addtogroup UART_Private_Constants
272 * @{
273 */
274/**
275 * @}
276 */
277/* Private macro -------------------------------------------------------------*/
278/* Private variables ---------------------------------------------------------*/
279/* Private function prototypes -----------------------------------------------*/
280/** @addtogroup UART_Private_Functions UART Private Functions
281 * @{
282 */
283
284#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
285void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart);
286#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
287static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
288static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
289static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
290static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
291static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
292static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
293static void UART_DMAError(DMA_HandleTypeDef *hdma);
294static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
295static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
296static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
297static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
298static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
299static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
300static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
301static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
302static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
303static void UART_SetConfig(UART_HandleTypeDef *huart);
304
305/**
306 * @}
307 */
308
309/* Exported functions ---------------------------------------------------------*/
310/** @defgroup UART_Exported_Functions UART Exported Functions
311 * @{
312 */
313
314/** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
315 * @brief Initialization and Configuration functions
316 *
317@verbatim
318 ===============================================================================
319 ##### Initialization and Configuration functions #####
320 ===============================================================================
321 [..]
322 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
323 in asynchronous mode.
324 (+) For the asynchronous mode only these parameters can be configured:
325 (++) Baud Rate
326 (++) Word Length
327 (++) Stop Bit
328 (++) Parity: If the parity is enabled, then the MSB bit of the data written
329 in the data register is transmitted but is changed by the parity bit.
330 Depending on the frame length defined by the M bit (8-bits or 9-bits),
331 please refer to Reference manual for possible UART frame formats.
332 (++) Hardware flow control
333 (++) Receiver/transmitter modes
334 (++) Over Sampling Method
335 [..]
336 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
337 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor configuration
338 procedures (details for the procedures are available in reference manual
339 (RM0430 for STM32F4X3xx MCUs and RM0402 for STM32F412xx MCUs
340 RM0383 for STM32F411xC/E MCUs and RM0401 for STM32F410xx MCUs
341 RM0090 for STM32F4X5xx/STM32F4X7xx/STM32F429xx/STM32F439xx MCUs
342 RM0390 for STM32F446xx MCUs and RM0386 for STM32F469xx/STM32F479xx MCUs)).
343
344@endverbatim
345 * @{
346 */
347
348/**
349 * @brief Initializes the UART mode according to the specified parameters in
350 * the UART_InitTypeDef and create the associated handle.
351 * @param huart Pointer to a UART_HandleTypeDef structure that contains
352 * the configuration information for the specified UART module.
353 * @retval HAL status
354 */
355HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
356{
357 /* Check the UART handle allocation */
358 if (huart == NULL)
359 {
360 return HAL_ERROR;
361 }
362
363 /* Check the parameters */
364 if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
365 {
366 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6.
367 Except for STM32F446xx devices, that is available for USART1, USART2, USART3, USART6, UART4 and UART5.
368 */
369 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
370 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
371 }
372 else
373 {
374 assert_param(IS_UART_INSTANCE(huart->Instance));
375 }
376 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
377 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
378
379 if (huart->gState == HAL_UART_STATE_RESET)
380 {
381 /* Allocate lock resource and initialize it */
382 huart->Lock = HAL_UNLOCKED;
383
384#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
385 UART_InitCallbacksToDefault(huart);
386
387 if (huart->MspInitCallback == NULL)
388 {
389 huart->MspInitCallback = HAL_UART_MspInit;
390 }
391
392 /* Init the low level hardware */
393 huart->MspInitCallback(huart);
394#else
395 /* Init the low level hardware : GPIO, CLOCK */
396 HAL_UART_MspInit(huart);
397#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
398 }
399
400 huart->gState = HAL_UART_STATE_BUSY;
401
402 /* Disable the peripheral */
403 __HAL_UART_DISABLE(huart);
404
405 /* Set the UART Communication parameters */
406 UART_SetConfig(huart);
407
408 /* In asynchronous mode, the following bits must be kept cleared:
409 - LINEN and CLKEN bits in the USART_CR2 register,
410 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
411 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
412 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
413
414 /* Enable the peripheral */
415 __HAL_UART_ENABLE(huart);
416
417 /* Initialize the UART state */
418 huart->ErrorCode = HAL_UART_ERROR_NONE;
419 huart->gState = HAL_UART_STATE_READY;
420 huart->RxState = HAL_UART_STATE_READY;
421
422 return HAL_OK;
423}
424
425/**
426 * @brief Initializes the half-duplex mode according to the specified
427 * parameters in the UART_InitTypeDef and create the associated handle.
428 * @param huart Pointer to a UART_HandleTypeDef structure that contains
429 * the configuration information for the specified UART module.
430 * @retval HAL status
431 */
432HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
433{
434 /* Check the UART handle allocation */
435 if (huart == NULL)
436 {
437 return HAL_ERROR;
438 }
439
440 /* Check the parameters */
441 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
442 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
443 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
444
445 if (huart->gState == HAL_UART_STATE_RESET)
446 {
447 /* Allocate lock resource and initialize it */
448 huart->Lock = HAL_UNLOCKED;
449
450#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
451 UART_InitCallbacksToDefault(huart);
452
453 if (huart->MspInitCallback == NULL)
454 {
455 huart->MspInitCallback = HAL_UART_MspInit;
456 }
457
458 /* Init the low level hardware */
459 huart->MspInitCallback(huart);
460#else
461 /* Init the low level hardware : GPIO, CLOCK */
462 HAL_UART_MspInit(huart);
463#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
464 }
465
466 huart->gState = HAL_UART_STATE_BUSY;
467
468 /* Disable the peripheral */
469 __HAL_UART_DISABLE(huart);
470
471 /* Set the UART Communication parameters */
472 UART_SetConfig(huart);
473
474 /* In half-duplex mode, the following bits must be kept cleared:
475 - LINEN and CLKEN bits in the USART_CR2 register,
476 - SCEN and IREN bits in the USART_CR3 register.*/
477 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
478 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
479
480 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
481 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
482
483 /* Enable the peripheral */
484 __HAL_UART_ENABLE(huart);
485
486 /* Initialize the UART state*/
487 huart->ErrorCode = HAL_UART_ERROR_NONE;
488 huart->gState = HAL_UART_STATE_READY;
489 huart->RxState = HAL_UART_STATE_READY;
490
491 return HAL_OK;
492}
493
494/**
495 * @brief Initializes the LIN mode according to the specified
496 * parameters in the UART_InitTypeDef and create the associated handle.
497 * @param huart Pointer to a UART_HandleTypeDef structure that contains
498 * the configuration information for the specified UART module.
499 * @param BreakDetectLength Specifies the LIN break detection length.
500 * This parameter can be one of the following values:
501 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
502 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
503 * @retval HAL status
504 */
505HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
506{
507 /* Check the UART handle allocation */
508 if (huart == NULL)
509 {
510 return HAL_ERROR;
511 }
512
513 /* Check the LIN UART instance */
514 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
515
516 /* Check the Break detection length parameter */
517 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
518 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
519 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
520
521 if (huart->gState == HAL_UART_STATE_RESET)
522 {
523 /* Allocate lock resource and initialize it */
524 huart->Lock = HAL_UNLOCKED;
525
526#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
527 UART_InitCallbacksToDefault(huart);
528
529 if (huart->MspInitCallback == NULL)
530 {
531 huart->MspInitCallback = HAL_UART_MspInit;
532 }
533
534 /* Init the low level hardware */
535 huart->MspInitCallback(huart);
536#else
537 /* Init the low level hardware : GPIO, CLOCK */
538 HAL_UART_MspInit(huart);
539#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
540 }
541
542 huart->gState = HAL_UART_STATE_BUSY;
543
544 /* Disable the peripheral */
545 __HAL_UART_DISABLE(huart);
546
547 /* Set the UART Communication parameters */
548 UART_SetConfig(huart);
549
550 /* In LIN mode, the following bits must be kept cleared:
551 - CLKEN bits in the USART_CR2 register,
552 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
553 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_CLKEN));
554 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
555
556 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
557 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
558
559 /* Set the USART LIN Break detection length. */
560 CLEAR_BIT(huart->Instance->CR2, USART_CR2_LBDL);
561 SET_BIT(huart->Instance->CR2, BreakDetectLength);
562
563 /* Enable the peripheral */
564 __HAL_UART_ENABLE(huart);
565
566 /* Initialize the UART state*/
567 huart->ErrorCode = HAL_UART_ERROR_NONE;
568 huart->gState = HAL_UART_STATE_READY;
569 huart->RxState = HAL_UART_STATE_READY;
570
571 return HAL_OK;
572}
573
574/**
575 * @brief Initializes the Multi-Processor mode according to the specified
576 * parameters in the UART_InitTypeDef and create the associated handle.
577 * @param huart Pointer to a UART_HandleTypeDef structure that contains
578 * the configuration information for the specified UART module.
579 * @param Address USART address
580 * @param WakeUpMethod specifies the USART wake-up method.
581 * This parameter can be one of the following values:
582 * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection
583 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark
584 * @retval HAL status
585 */
586HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
587{
588 /* Check the UART handle allocation */
589 if (huart == NULL)
590 {
591 return HAL_ERROR;
592 }
593
594 /* Check the parameters */
595 assert_param(IS_UART_INSTANCE(huart->Instance));
596
597 /* Check the Address & wake up method parameters */
598 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
599 assert_param(IS_UART_ADDRESS(Address));
600 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
601 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
602
603 if (huart->gState == HAL_UART_STATE_RESET)
604 {
605 /* Allocate lock resource and initialize it */
606 huart->Lock = HAL_UNLOCKED;
607
608#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
609 UART_InitCallbacksToDefault(huart);
610
611 if (huart->MspInitCallback == NULL)
612 {
613 huart->MspInitCallback = HAL_UART_MspInit;
614 }
615
616 /* Init the low level hardware */
617 huart->MspInitCallback(huart);
618#else
619 /* Init the low level hardware : GPIO, CLOCK */
620 HAL_UART_MspInit(huart);
621#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
622 }
623
624 huart->gState = HAL_UART_STATE_BUSY;
625
626 /* Disable the peripheral */
627 __HAL_UART_DISABLE(huart);
628
629 /* Set the UART Communication parameters */
630 UART_SetConfig(huart);
631
632 /* In Multi-Processor mode, the following bits must be kept cleared:
633 - LINEN and CLKEN bits in the USART_CR2 register,
634 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
635 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
636 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
637
638 /* Set the USART address node */
639 CLEAR_BIT(huart->Instance->CR2, USART_CR2_ADD);
640 SET_BIT(huart->Instance->CR2, Address);
641
642 /* Set the wake up method by setting the WAKE bit in the CR1 register */
643 CLEAR_BIT(huart->Instance->CR1, USART_CR1_WAKE);
644 SET_BIT(huart->Instance->CR1, WakeUpMethod);
645
646 /* Enable the peripheral */
647 __HAL_UART_ENABLE(huart);
648
649 /* Initialize the UART state */
650 huart->ErrorCode = HAL_UART_ERROR_NONE;
651 huart->gState = HAL_UART_STATE_READY;
652 huart->RxState = HAL_UART_STATE_READY;
653
654 return HAL_OK;
655}
656
657/**
658 * @brief DeInitializes the UART peripheral.
659 * @param huart Pointer to a UART_HandleTypeDef structure that contains
660 * the configuration information for the specified UART module.
661 * @retval HAL status
662 */
663HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
664{
665 /* Check the UART handle allocation */
666 if (huart == NULL)
667 {
668 return HAL_ERROR;
669 }
670
671 /* Check the parameters */
672 assert_param(IS_UART_INSTANCE(huart->Instance));
673
674 huart->gState = HAL_UART_STATE_BUSY;
675
676 /* Disable the Peripheral */
677 __HAL_UART_DISABLE(huart);
678
679#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
680 if (huart->MspDeInitCallback == NULL)
681 {
682 huart->MspDeInitCallback = HAL_UART_MspDeInit;
683 }
684 /* DeInit the low level hardware */
685 huart->MspDeInitCallback(huart);
686#else
687 /* DeInit the low level hardware */
688 HAL_UART_MspDeInit(huart);
689#endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
690
691 huart->ErrorCode = HAL_UART_ERROR_NONE;
692 huart->gState = HAL_UART_STATE_RESET;
693 huart->RxState = HAL_UART_STATE_RESET;
694 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
695
696 /* Process Unlock */
697 __HAL_UNLOCK(huart);
698
699 return HAL_OK;
700}
701
702/**
703 * @brief UART MSP Init.
704 * @param huart Pointer to a UART_HandleTypeDef structure that contains
705 * the configuration information for the specified UART module.
706 * @retval None
707 */
708__weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
709{
710 /* Prevent unused argument(s) compilation warning */
711 UNUSED(huart);
712 /* NOTE: This function should not be modified, when the callback is needed,
713 the HAL_UART_MspInit could be implemented in the user file
714 */
715}
716
717/**
718 * @brief UART MSP DeInit.
719 * @param huart Pointer to a UART_HandleTypeDef structure that contains
720 * the configuration information for the specified UART module.
721 * @retval None
722 */
723__weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
724{
725 /* Prevent unused argument(s) compilation warning */
726 UNUSED(huart);
727 /* NOTE: This function should not be modified, when the callback is needed,
728 the HAL_UART_MspDeInit could be implemented in the user file
729 */
730}
731
732#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
733/**
734 * @brief Register a User UART Callback
735 * To be used instead of the weak predefined callback
736 * @param huart uart handle
737 * @param CallbackID ID of the callback to be registered
738 * This parameter can be one of the following values:
739 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
740 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
741 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
742 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
743 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
744 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
745 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
746 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
747 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
748 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
749 * @param pCallback pointer to the Callback function
750 * @retval HAL status
751 */
752HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID, pUART_CallbackTypeDef pCallback)
753{
754 HAL_StatusTypeDef status = HAL_OK;
755
756 if (pCallback == NULL)
757 {
758 /* Update the error code */
759 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
760
761 return HAL_ERROR;
762 }
763 /* Process locked */
764 __HAL_LOCK(huart);
765
766 if (huart->gState == HAL_UART_STATE_READY)
767 {
768 switch (CallbackID)
769 {
770 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
771 huart->TxHalfCpltCallback = pCallback;
772 break;
773
774 case HAL_UART_TX_COMPLETE_CB_ID :
775 huart->TxCpltCallback = pCallback;
776 break;
777
778 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
779 huart->RxHalfCpltCallback = pCallback;
780 break;
781
782 case HAL_UART_RX_COMPLETE_CB_ID :
783 huart->RxCpltCallback = pCallback;
784 break;
785
786 case HAL_UART_ERROR_CB_ID :
787 huart->ErrorCallback = pCallback;
788 break;
789
790 case HAL_UART_ABORT_COMPLETE_CB_ID :
791 huart->AbortCpltCallback = pCallback;
792 break;
793
794 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
795 huart->AbortTransmitCpltCallback = pCallback;
796 break;
797
798 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
799 huart->AbortReceiveCpltCallback = pCallback;
800 break;
801
802 case HAL_UART_MSPINIT_CB_ID :
803 huart->MspInitCallback = pCallback;
804 break;
805
806 case HAL_UART_MSPDEINIT_CB_ID :
807 huart->MspDeInitCallback = pCallback;
808 break;
809
810 default :
811 /* Update the error code */
812 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
813
814 /* Return error status */
815 status = HAL_ERROR;
816 break;
817 }
818 }
819 else if (huart->gState == HAL_UART_STATE_RESET)
820 {
821 switch (CallbackID)
822 {
823 case HAL_UART_MSPINIT_CB_ID :
824 huart->MspInitCallback = pCallback;
825 break;
826
827 case HAL_UART_MSPDEINIT_CB_ID :
828 huart->MspDeInitCallback = pCallback;
829 break;
830
831 default :
832 /* Update the error code */
833 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
834
835 /* Return error status */
836 status = HAL_ERROR;
837 break;
838 }
839 }
840 else
841 {
842 /* Update the error code */
843 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
844
845 /* Return error status */
846 status = HAL_ERROR;
847 }
848
849 /* Release Lock */
850 __HAL_UNLOCK(huart);
851
852 return status;
853}
854
855/**
856 * @brief Unregister an UART Callback
857 * UART callaback is redirected to the weak predefined callback
858 * @param huart uart handle
859 * @param CallbackID ID of the callback to be unregistered
860 * This parameter can be one of the following values:
861 * @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
862 * @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
863 * @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
864 * @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
865 * @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
866 * @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
867 * @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
868 * @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
869 * @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
870 * @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
871 * @retval HAL status
872 */
873HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
874{
875 HAL_StatusTypeDef status = HAL_OK;
876
877 /* Process locked */
878 __HAL_LOCK(huart);
879
880 if (HAL_UART_STATE_READY == huart->gState)
881 {
882 switch (CallbackID)
883 {
884 case HAL_UART_TX_HALFCOMPLETE_CB_ID :
885 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
886 break;
887
888 case HAL_UART_TX_COMPLETE_CB_ID :
889 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
890 break;
891
892 case HAL_UART_RX_HALFCOMPLETE_CB_ID :
893 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
894 break;
895
896 case HAL_UART_RX_COMPLETE_CB_ID :
897 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
898 break;
899
900 case HAL_UART_ERROR_CB_ID :
901 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
902 break;
903
904 case HAL_UART_ABORT_COMPLETE_CB_ID :
905 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
906 break;
907
908 case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
909 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
910 break;
911
912 case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
913 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
914 break;
915
916 case HAL_UART_MSPINIT_CB_ID :
917 huart->MspInitCallback = HAL_UART_MspInit; /* Legacy weak MspInitCallback */
918 break;
919
920 case HAL_UART_MSPDEINIT_CB_ID :
921 huart->MspDeInitCallback = HAL_UART_MspDeInit; /* Legacy weak MspDeInitCallback */
922 break;
923
924 default :
925 /* Update the error code */
926 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
927
928 /* Return error status */
929 status = HAL_ERROR;
930 break;
931 }
932 }
933 else if (HAL_UART_STATE_RESET == huart->gState)
934 {
935 switch (CallbackID)
936 {
937 case HAL_UART_MSPINIT_CB_ID :
938 huart->MspInitCallback = HAL_UART_MspInit;
939 break;
940
941 case HAL_UART_MSPDEINIT_CB_ID :
942 huart->MspDeInitCallback = HAL_UART_MspDeInit;
943 break;
944
945 default :
946 /* Update the error code */
947 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
948
949 /* Return error status */
950 status = HAL_ERROR;
951 break;
952 }
953 }
954 else
955 {
956 /* Update the error code */
957 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
958
959 /* Return error status */
960 status = HAL_ERROR;
961 }
962
963 /* Release Lock */
964 __HAL_UNLOCK(huart);
965
966 return status;
967}
968
969/**
970 * @brief Register a User UART Rx Event Callback
971 * To be used instead of the weak predefined callback
972 * @param huart Uart handle
973 * @param pCallback Pointer to the Rx Event Callback function
974 * @retval HAL status
975 */
976HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback)
977{
978 HAL_StatusTypeDef status = HAL_OK;
979
980 if (pCallback == NULL)
981 {
982 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
983
984 return HAL_ERROR;
985 }
986
987 /* Process locked */
988 __HAL_LOCK(huart);
989
990 if (huart->gState == HAL_UART_STATE_READY)
991 {
992 huart->RxEventCallback = pCallback;
993 }
994 else
995 {
996 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
997
998 status = HAL_ERROR;
999 }
1000
1001 /* Release Lock */
1002 __HAL_UNLOCK(huart);
1003
1004 return status;
1005}
1006
1007/**
1008 * @brief UnRegister the UART Rx Event Callback
1009 * UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback
1010 * @param huart Uart handle
1011 * @retval HAL status
1012 */
1013HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart)
1014{
1015 HAL_StatusTypeDef status = HAL_OK;
1016
1017 /* Process locked */
1018 __HAL_LOCK(huart);
1019
1020 if (huart->gState == HAL_UART_STATE_READY)
1021 {
1022 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback */
1023 }
1024 else
1025 {
1026 huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
1027
1028 status = HAL_ERROR;
1029 }
1030
1031 /* Release Lock */
1032 __HAL_UNLOCK(huart);
1033 return status;
1034}
1035#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1036
1037/**
1038 * @}
1039 */
1040
1041/** @defgroup UART_Exported_Functions_Group2 IO operation functions
1042 * @brief UART Transmit and Receive functions
1043 *
1044@verbatim
1045 ===============================================================================
1046 ##### IO operation functions #####
1047 ===============================================================================
1048 This subsection provides a set of functions allowing to manage the UART asynchronous
1049 and Half duplex data transfers.
1050
1051 (#) There are two modes of transfer:
1052 (+) Blocking mode: The communication is performed in polling mode.
1053 The HAL status of all data processing is returned by the same function
1054 after finishing transfer.
1055 (+) Non-Blocking mode: The communication is performed using Interrupts
1056 or DMA, these API's return the HAL status.
1057 The end of the data processing will be indicated through the
1058 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
1059 using DMA mode.
1060 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
1061 will be executed respectively at the end of the transmit or receive process
1062 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected.
1063
1064 (#) Blocking mode API's are :
1065 (+) HAL_UART_Transmit()
1066 (+) HAL_UART_Receive()
1067
1068 (#) Non-Blocking mode API's with Interrupt are :
1069 (+) HAL_UART_Transmit_IT()
1070 (+) HAL_UART_Receive_IT()
1071 (+) HAL_UART_IRQHandler()
1072
1073 (#) Non-Blocking mode API's with DMA are :
1074 (+) HAL_UART_Transmit_DMA()
1075 (+) HAL_UART_Receive_DMA()
1076 (+) HAL_UART_DMAPause()
1077 (+) HAL_UART_DMAResume()
1078 (+) HAL_UART_DMAStop()
1079
1080 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
1081 (+) HAL_UART_TxHalfCpltCallback()
1082 (+) HAL_UART_TxCpltCallback()
1083 (+) HAL_UART_RxHalfCpltCallback()
1084 (+) HAL_UART_RxCpltCallback()
1085 (+) HAL_UART_ErrorCallback()
1086
1087 (#) Non-Blocking mode transfers could be aborted using Abort API's :
1088 (+) HAL_UART_Abort()
1089 (+) HAL_UART_AbortTransmit()
1090 (+) HAL_UART_AbortReceive()
1091 (+) HAL_UART_Abort_IT()
1092 (+) HAL_UART_AbortTransmit_IT()
1093 (+) HAL_UART_AbortReceive_IT()
1094
1095 (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
1096 (+) HAL_UART_AbortCpltCallback()
1097 (+) HAL_UART_AbortTransmitCpltCallback()
1098 (+) HAL_UART_AbortReceiveCpltCallback()
1099
1100 (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced reception services:
1101 (+) HAL_UARTEx_RxEventCallback()
1102
1103 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
1104 Errors are handled as follows :
1105 (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1106 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
1107 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
1108 and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
1109 If user wants to abort it, Abort services should be called by user.
1110 (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1111 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1112 Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1113
1114 -@- In the Half duplex communication, it is forbidden to run the transmit
1115 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1116
1117@endverbatim
1118 * @{
1119 */
1120
1121/**
1122 * @brief Sends an amount of data in blocking mode.
1123 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1124 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1125 * of u16 provided through pData.
1126 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1127 * the configuration information for the specified UART module.
1128 * @param pData Pointer to data buffer (u8 or u16 data elements).
1129 * @param Size Amount of data elements (u8 or u16) to be sent
1130 * @param Timeout Timeout duration
1131 * @retval HAL status
1132 */
1133HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1134{
1135 uint8_t *pdata8bits;
1136 uint16_t *pdata16bits;
1137 uint32_t tickstart = 0U;
1138
1139 /* Check that a Tx process is not already ongoing */
1140 if (huart->gState == HAL_UART_STATE_READY)
1141 {
1142 if ((pData == NULL) || (Size == 0U))
1143 {
1144 return HAL_ERROR;
1145 }
1146
1147 /* Process Locked */
1148 __HAL_LOCK(huart);
1149
1150 huart->ErrorCode = HAL_UART_ERROR_NONE;
1151 huart->gState = HAL_UART_STATE_BUSY_TX;
1152
1153 /* Init tickstart for timeout management */
1154 tickstart = HAL_GetTick();
1155
1156 huart->TxXferSize = Size;
1157 huart->TxXferCount = Size;
1158
1159 /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1160 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1161 {
1162 pdata8bits = NULL;
1163 pdata16bits = (uint16_t *) pData;
1164 }
1165 else
1166 {
1167 pdata8bits = pData;
1168 pdata16bits = NULL;
1169 }
1170
1171 /* Process Unlocked */
1172 __HAL_UNLOCK(huart);
1173
1174 while (huart->TxXferCount > 0U)
1175 {
1176 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1177 {
1178 return HAL_TIMEOUT;
1179 }
1180 if (pdata8bits == NULL)
1181 {
1182 huart->Instance->DR = (uint16_t)(*pdata16bits & 0x01FFU);
1183 pdata16bits++;
1184 }
1185 else
1186 {
1187 huart->Instance->DR = (uint8_t)(*pdata8bits & 0xFFU);
1188 pdata8bits++;
1189 }
1190 huart->TxXferCount--;
1191 }
1192
1193 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
1194 {
1195 return HAL_TIMEOUT;
1196 }
1197
1198 /* At end of Tx process, restore huart->gState to Ready */
1199 huart->gState = HAL_UART_STATE_READY;
1200
1201 return HAL_OK;
1202 }
1203 else
1204 {
1205 return HAL_BUSY;
1206 }
1207}
1208
1209/**
1210 * @brief Receives an amount of data in blocking mode.
1211 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1212 * the received data is handled as a set of u16. In this case, Size must indicate the number
1213 * of u16 available through pData.
1214 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1215 * the configuration information for the specified UART module.
1216 * @param pData Pointer to data buffer (u8 or u16 data elements).
1217 * @param Size Amount of data elements (u8 or u16) to be received.
1218 * @param Timeout Timeout duration
1219 * @retval HAL status
1220 */
1221HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1222{
1223 uint8_t *pdata8bits;
1224 uint16_t *pdata16bits;
1225 uint32_t tickstart = 0U;
1226
1227 /* Check that a Rx process is not already ongoing */
1228 if (huart->RxState == HAL_UART_STATE_READY)
1229 {
1230 if ((pData == NULL) || (Size == 0U))
1231 {
1232 return HAL_ERROR;
1233 }
1234
1235 /* Process Locked */
1236 __HAL_LOCK(huart);
1237
1238 huart->ErrorCode = HAL_UART_ERROR_NONE;
1239 huart->RxState = HAL_UART_STATE_BUSY_RX;
1240 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1241
1242 /* Init tickstart for timeout management */
1243 tickstart = HAL_GetTick();
1244
1245 huart->RxXferSize = Size;
1246 huart->RxXferCount = Size;
1247
1248 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1249 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1250 {
1251 pdata8bits = NULL;
1252 pdata16bits = (uint16_t *) pData;
1253 }
1254 else
1255 {
1256 pdata8bits = pData;
1257 pdata16bits = NULL;
1258 }
1259
1260 /* Process Unlocked */
1261 __HAL_UNLOCK(huart);
1262
1263 /* Check the remain data to be received */
1264 while (huart->RxXferCount > 0U)
1265 {
1266 if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1267 {
1268 return HAL_TIMEOUT;
1269 }
1270 if (pdata8bits == NULL)
1271 {
1272 *pdata16bits = (uint16_t)(huart->Instance->DR & 0x01FF);
1273 pdata16bits++;
1274 }
1275 else
1276 {
1277 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) || ((huart->Init.WordLength == UART_WORDLENGTH_8B) && (huart->Init.Parity == UART_PARITY_NONE)))
1278 {
1279 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
1280 }
1281 else
1282 {
1283 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
1284 }
1285 pdata8bits++;
1286 }
1287 huart->RxXferCount--;
1288 }
1289
1290 /* At end of Rx process, restore huart->RxState to Ready */
1291 huart->RxState = HAL_UART_STATE_READY;
1292
1293 return HAL_OK;
1294 }
1295 else
1296 {
1297 return HAL_BUSY;
1298 }
1299}
1300
1301/**
1302 * @brief Sends an amount of data in non blocking mode.
1303 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1304 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1305 * of u16 provided through pData.
1306 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1307 * the configuration information for the specified UART module.
1308 * @param pData Pointer to data buffer (u8 or u16 data elements).
1309 * @param Size Amount of data elements (u8 or u16) to be sent
1310 * @retval HAL status
1311 */
1312HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1313{
1314 /* Check that a Tx process is not already ongoing */
1315 if (huart->gState == HAL_UART_STATE_READY)
1316 {
1317 if ((pData == NULL) || (Size == 0U))
1318 {
1319 return HAL_ERROR;
1320 }
1321
1322 /* Process Locked */
1323 __HAL_LOCK(huart);
1324
1325 huart->pTxBuffPtr = pData;
1326 huart->TxXferSize = Size;
1327 huart->TxXferCount = Size;
1328
1329 huart->ErrorCode = HAL_UART_ERROR_NONE;
1330 huart->gState = HAL_UART_STATE_BUSY_TX;
1331
1332 /* Process Unlocked */
1333 __HAL_UNLOCK(huart);
1334
1335 /* Enable the UART Transmit data register empty Interrupt */
1336 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
1337
1338 return HAL_OK;
1339 }
1340 else
1341 {
1342 return HAL_BUSY;
1343 }
1344}
1345
1346/**
1347 * @brief Receives an amount of data in non blocking mode.
1348 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1349 * the received data is handled as a set of u16. In this case, Size must indicate the number
1350 * of u16 available through pData.
1351 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1352 * the configuration information for the specified UART module.
1353 * @param pData Pointer to data buffer (u8 or u16 data elements).
1354 * @param Size Amount of data elements (u8 or u16) to be received.
1355 * @retval HAL status
1356 */
1357HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1358{
1359 /* Check that a Rx process is not already ongoing */
1360 if (huart->RxState == HAL_UART_STATE_READY)
1361 {
1362 if ((pData == NULL) || (Size == 0U))
1363 {
1364 return HAL_ERROR;
1365 }
1366
1367 /* Process Locked */
1368 __HAL_LOCK(huart);
1369
1370 /* Set Reception type to Standard reception */
1371 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1372
1373 return(UART_Start_Receive_IT(huart, pData, Size));
1374 }
1375 else
1376 {
1377 return HAL_BUSY;
1378 }
1379}
1380
1381/**
1382 * @brief Sends an amount of data in DMA mode.
1383 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1384 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1385 * of u16 provided through pData.
1386 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1387 * the configuration information for the specified UART module.
1388 * @param pData Pointer to data buffer (u8 or u16 data elements).
1389 * @param Size Amount of data elements (u8 or u16) to be sent
1390 * @retval HAL status
1391 */
1392HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1393{
1394 uint32_t *tmp;
1395
1396 /* Check that a Tx process is not already ongoing */
1397 if (huart->gState == HAL_UART_STATE_READY)
1398 {
1399 if ((pData == NULL) || (Size == 0U))
1400 {
1401 return HAL_ERROR;
1402 }
1403
1404 /* Process Locked */
1405 __HAL_LOCK(huart);
1406
1407 huart->pTxBuffPtr = pData;
1408 huart->TxXferSize = Size;
1409 huart->TxXferCount = Size;
1410
1411 huart->ErrorCode = HAL_UART_ERROR_NONE;
1412 huart->gState = HAL_UART_STATE_BUSY_TX;
1413
1414 /* Set the UART DMA transfer complete callback */
1415 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
1416
1417 /* Set the UART DMA Half transfer complete callback */
1418 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
1419
1420 /* Set the DMA error callback */
1421 huart->hdmatx->XferErrorCallback = UART_DMAError;
1422
1423 /* Set the DMA abort callback */
1424 huart->hdmatx->XferAbortCallback = NULL;
1425
1426 /* Enable the UART transmit DMA stream */
1427 tmp = (uint32_t *)&pData;
1428 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t *)tmp, (uint32_t)&huart->Instance->DR, Size);
1429
1430 /* Clear the TC flag in the SR register by writing 0 to it */
1431 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
1432
1433 /* Process Unlocked */
1434 __HAL_UNLOCK(huart);
1435
1436 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1437 in the UART CR3 register */
1438 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1439
1440 return HAL_OK;
1441 }
1442 else
1443 {
1444 return HAL_BUSY;
1445 }
1446}
1447
1448/**
1449 * @brief Receives an amount of data in DMA mode.
1450 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1451 * the received data is handled as a set of u16. In this case, Size must indicate the number
1452 * of u16 available through pData.
1453 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1454 * the configuration information for the specified UART module.
1455 * @param pData Pointer to data buffer (u8 or u16 data elements).
1456 * @param Size Amount of data elements (u8 or u16) to be received.
1457 * @note When the UART parity is enabled (PCE = 1) the received data contains the parity bit.
1458 * @retval HAL status
1459 */
1460HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1461{
1462 /* Check that a Rx process is not already ongoing */
1463 if (huart->RxState == HAL_UART_STATE_READY)
1464 {
1465 if ((pData == NULL) || (Size == 0U))
1466 {
1467 return HAL_ERROR;
1468 }
1469
1470 /* Process Locked */
1471 __HAL_LOCK(huart);
1472
1473 /* Set Reception type to Standard reception */
1474 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1475
1476 return(UART_Start_Receive_DMA(huart, pData, Size));
1477 }
1478 else
1479 {
1480 return HAL_BUSY;
1481 }
1482}
1483
1484/**
1485 * @brief Pauses the DMA Transfer.
1486 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1487 * the configuration information for the specified UART module.
1488 * @retval HAL status
1489 */
1490HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1491{
1492 uint32_t dmarequest = 0x00U;
1493
1494 /* Process Locked */
1495 __HAL_LOCK(huart);
1496
1497 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1498 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1499 {
1500 /* Disable the UART DMA Tx request */
1501 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1502 }
1503
1504 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1505 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1506 {
1507 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1508 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1509 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1510
1511 /* Disable the UART DMA Rx request */
1512 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1513 }
1514
1515 /* Process Unlocked */
1516 __HAL_UNLOCK(huart);
1517
1518 return HAL_OK;
1519}
1520
1521/**
1522 * @brief Resumes the DMA Transfer.
1523 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1524 * the configuration information for the specified UART module.
1525 * @retval HAL status
1526 */
1527HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1528{
1529 /* Process Locked */
1530 __HAL_LOCK(huart);
1531
1532 if (huart->gState == HAL_UART_STATE_BUSY_TX)
1533 {
1534 /* Enable the UART DMA Tx request */
1535 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1536 }
1537
1538 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
1539 {
1540 /* Clear the Overrun flag before resuming the Rx transfer*/
1541 __HAL_UART_CLEAR_OREFLAG(huart);
1542
1543 /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1544 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1545 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1546
1547 /* Enable the UART DMA Rx request */
1548 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1549 }
1550
1551 /* Process Unlocked */
1552 __HAL_UNLOCK(huart);
1553
1554 return HAL_OK;
1555}
1556
1557/**
1558 * @brief Stops the DMA Transfer.
1559 * @param huart Pointer to a UART_HandleTypeDef structure that contains
1560 * the configuration information for the specified UART module.
1561 * @retval HAL status
1562 */
1563HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1564{
1565 uint32_t dmarequest = 0x00U;
1566 /* The Lock is not implemented on this API to allow the user application
1567 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
1568 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1569 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
1570 */
1571
1572 /* Stop UART DMA Tx request if ongoing */
1573 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1574 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1575 {
1576 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1577
1578 /* Abort the UART DMA Tx stream */
1579 if (huart->hdmatx != NULL)
1580 {
1581 HAL_DMA_Abort(huart->hdmatx);
1582 }
1583 UART_EndTxTransfer(huart);
1584 }
1585
1586 /* Stop UART DMA Rx request if ongoing */
1587 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1588 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1589 {
1590 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1591
1592 /* Abort the UART DMA Rx stream */
1593 if (huart->hdmarx != NULL)
1594 {
1595 HAL_DMA_Abort(huart->hdmarx);
1596 }
1597 UART_EndRxTransfer(huart);
1598 }
1599
1600 return HAL_OK;
1601}
1602
1603/**
1604 * @brief Receive an amount of data in blocking mode till either the expected number of data is received or an IDLE event occurs.
1605 * @note HAL_OK is returned if reception is completed (expected number of data has been received)
1606 * or if reception is stopped after IDLE event (less than the expected number of data has been received)
1607 * In this case, RxLen output parameter indicates number of data available in reception buffer.
1608 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M = 01),
1609 * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
1610 * of uint16_t available through pData.
1611 * @param huart UART handle.
1612 * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
1613 * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
1614 * @param RxLen Number of data elements finally received (could be lower than Size, in case reception ends on IDLE event)
1615 * @param Timeout Timeout duration expressed in ms (covers the whole reception sequence).
1616 * @retval HAL status
1617 */
1618HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen, uint32_t Timeout)
1619{
1620 uint8_t *pdata8bits;
1621 uint16_t *pdata16bits;
1622 uint32_t tickstart;
1623
1624 /* Check that a Rx process is not already ongoing */
1625 if (huart->RxState == HAL_UART_STATE_READY)
1626 {
1627 if ((pData == NULL) || (Size == 0U))
1628 {
1629 return HAL_ERROR;
1630 }
1631
1632 __HAL_LOCK(huart);
1633
1634 huart->ErrorCode = HAL_UART_ERROR_NONE;
1635 huart->RxState = HAL_UART_STATE_BUSY_RX;
1636 huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
1637
1638 /* Init tickstart for timeout management */
1639 tickstart = HAL_GetTick();
1640
1641 huart->RxXferSize = Size;
1642 huart->RxXferCount = Size;
1643
1644 /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1645 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1646 {
1647 pdata8bits = NULL;
1648 pdata16bits = (uint16_t *) pData;
1649 }
1650 else
1651 {
1652 pdata8bits = pData;
1653 pdata16bits = NULL;
1654 }
1655
1656 __HAL_UNLOCK(huart);
1657
1658 /* Initialize output number of received elements */
1659 *RxLen = 0U;
1660
1661 /* as long as data have to be received */
1662 while (huart->RxXferCount > 0U)
1663 {
1664 /* Check if IDLE flag is set */
1665 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
1666 {
1667 /* Clear IDLE flag in ISR */
1668 __HAL_UART_CLEAR_IDLEFLAG(huart);
1669
1670 /* If Set, but no data ever received, clear flag without exiting loop */
1671 /* If Set, and data has already been received, this means Idle Event is valid : End reception */
1672 if (*RxLen > 0U)
1673 {
1674 huart->RxState = HAL_UART_STATE_READY;
1675
1676 return HAL_OK;
1677 }
1678 }
1679
1680 /* Check if RXNE flag is set */
1681 if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE))
1682 {
1683 if (pdata8bits == NULL)
1684 {
1685 *pdata16bits = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
1686 pdata16bits++;
1687 }
1688 else
1689 {
1690 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) || ((huart->Init.WordLength == UART_WORDLENGTH_8B) && (huart->Init.Parity == UART_PARITY_NONE)))
1691 {
1692 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
1693 }
1694 else
1695 {
1696 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
1697 }
1698
1699 pdata8bits++;
1700 }
1701 /* Increment number of received elements */
1702 *RxLen += 1U;
1703 huart->RxXferCount--;
1704 }
1705
1706 /* Check for the Timeout */
1707 if (Timeout != HAL_MAX_DELAY)
1708 {
1709 if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1710 {
1711 huart->RxState = HAL_UART_STATE_READY;
1712
1713 return HAL_TIMEOUT;
1714 }
1715 }
1716 }
1717
1718 /* Set number of received elements in output parameter : RxLen */
1719 *RxLen = huart->RxXferSize - huart->RxXferCount;
1720 /* At end of Rx process, restore huart->RxState to Ready */
1721 huart->RxState = HAL_UART_STATE_READY;
1722
1723 return HAL_OK;
1724 }
1725 else
1726 {
1727 return HAL_BUSY;
1728 }
1729}
1730
1731/**
1732 * @brief Receive an amount of data in interrupt mode till either the expected number of data is received or an IDLE event occurs.
1733 * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
1734 * to UART interrupts raised by RXNE and IDLE events. Callback is called at end of reception indicating
1735 * number of received data elements.
1736 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M = 01),
1737 * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
1738 * of uint16_t available through pData.
1739 * @param huart UART handle.
1740 * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
1741 * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
1742 * @retval HAL status
1743 */
1744HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1745{
1746 HAL_StatusTypeDef status;
1747
1748 /* Check that a Rx process is not already ongoing */
1749 if (huart->RxState == HAL_UART_STATE_READY)
1750 {
1751 if ((pData == NULL) || (Size == 0U))
1752 {
1753 return HAL_ERROR;
1754 }
1755
1756 __HAL_LOCK(huart);
1757
1758 /* Set Reception type to reception till IDLE Event*/
1759 huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
1760
1761 status = UART_Start_Receive_IT(huart, pData, Size);
1762
1763 /* Check Rx process has been successfully started */
1764 if (status == HAL_OK)
1765 {
1766 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1767 {
1768 __HAL_UART_CLEAR_IDLEFLAG(huart);
1769 SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
1770 }
1771 else
1772 {
1773 /* In case of errors already pending when reception is started,
1774 Interrupts may have already been raised and lead to reception abortion.
1775 (Overrun error for instance).
1776 In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
1777 status = HAL_ERROR;
1778 }
1779 }
1780
1781 return status;
1782 }
1783 else
1784 {
1785 return HAL_BUSY;
1786 }
1787}
1788
1789/**
1790 * @brief Receive an amount of data in DMA mode till either the expected number of data is received or an IDLE event occurs.
1791 * @note Reception is initiated by this function call. Further progress of reception is achieved thanks
1792 * to DMA services, transferring automatically received data elements in user reception buffer and
1793 * calling registered callbacks at half/end of reception. UART IDLE events are also used to consider
1794 * reception phase as ended. In all cases, callback execution will indicate number of received data elements.
1795 * @note When the UART parity is enabled (PCE = 1), the received data contain
1796 * the parity bit (MSB position).
1797 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M = 01),
1798 * the received data is handled as a set of uint16_t. In this case, Size must indicate the number
1799 * of uint16_t available through pData.
1800 * @param huart UART handle.
1801 * @param pData Pointer to data buffer (uint8_t or uint16_t data elements).
1802 * @param Size Amount of data elements (uint8_t or uint16_t) to be received.
1803 * @retval HAL status
1804 */
1805HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1806{
1807 HAL_StatusTypeDef status;
1808
1809 /* Check that a Rx process is not already ongoing */
1810 if (huart->RxState == HAL_UART_STATE_READY)
1811 {
1812 if ((pData == NULL) || (Size == 0U))
1813 {
1814 return HAL_ERROR;
1815 }
1816
1817 __HAL_LOCK(huart);
1818
1819 /* Set Reception type to reception till IDLE Event*/
1820 huart->ReceptionType = HAL_UART_RECEPTION_TOIDLE;
1821
1822 status = UART_Start_Receive_DMA(huart, pData, Size);
1823
1824 /* Check Rx process has been successfully started */
1825 if (status == HAL_OK)
1826 {
1827 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1828 {
1829 __HAL_UART_CLEAR_IDLEFLAG(huart);
1830 SET_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
1831 }
1832 else
1833 {
1834 /* In case of errors already pending when reception is started,
1835 Interrupts may have already been raised and lead to reception abortion.
1836 (Overrun error for instance).
1837 In such case Reception Type has been reset to HAL_UART_RECEPTION_STANDARD. */
1838 status = HAL_ERROR;
1839 }
1840 }
1841
1842 return status;
1843 }
1844 else
1845 {
1846 return HAL_BUSY;
1847 }
1848}
1849
1850/**
1851 * @brief Abort ongoing transfers (blocking mode).
1852 * @param huart UART handle.
1853 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1854 * This procedure performs following operations :
1855 * - Disable UART Interrupts (Tx and Rx)
1856 * - Disable the DMA transfer in the peripheral register (if enabled)
1857 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1858 * - Set handle State to READY
1859 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1860 * @retval HAL status
1861*/
1862HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1863{
1864 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1865 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1866 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1867
1868 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1869 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1870 {
1871 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1872 }
1873
1874 /* Disable the UART DMA Tx request if enabled */
1875 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1876 {
1877 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1878
1879 /* Abort the UART DMA Tx stream: use blocking DMA Abort API (no callback) */
1880 if (huart->hdmatx != NULL)
1881 {
1882 /* Set the UART DMA Abort callback to Null.
1883 No call back execution at end of DMA abort procedure */
1884 huart->hdmatx->XferAbortCallback = NULL;
1885
1886 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1887 {
1888 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1889 {
1890 /* Set error code to DMA */
1891 huart->ErrorCode = HAL_UART_ERROR_DMA;
1892
1893 return HAL_TIMEOUT;
1894 }
1895 }
1896 }
1897 }
1898
1899 /* Disable the UART DMA Rx request if enabled */
1900 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1901 {
1902 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1903
1904 /* Abort the UART DMA Rx stream: use blocking DMA Abort API (no callback) */
1905 if (huart->hdmarx != NULL)
1906 {
1907 /* Set the UART DMA Abort callback to Null.
1908 No call back execution at end of DMA abort procedure */
1909 huart->hdmarx->XferAbortCallback = NULL;
1910
1911 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1912 {
1913 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1914 {
1915 /* Set error code to DMA */
1916 huart->ErrorCode = HAL_UART_ERROR_DMA;
1917
1918 return HAL_TIMEOUT;
1919 }
1920 }
1921 }
1922 }
1923
1924 /* Reset Tx and Rx transfer counters */
1925 huart->TxXferCount = 0x00U;
1926 huart->RxXferCount = 0x00U;
1927
1928 /* Reset ErrorCode */
1929 huart->ErrorCode = HAL_UART_ERROR_NONE;
1930
1931 /* Restore huart->RxState and huart->gState to Ready */
1932 huart->RxState = HAL_UART_STATE_READY;
1933 huart->gState = HAL_UART_STATE_READY;
1934 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1935
1936 return HAL_OK;
1937}
1938
1939/**
1940 * @brief Abort ongoing Transmit transfer (blocking mode).
1941 * @param huart UART handle.
1942 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1943 * This procedure performs following operations :
1944 * - Disable UART Interrupts (Tx)
1945 * - Disable the DMA transfer in the peripheral register (if enabled)
1946 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1947 * - Set handle State to READY
1948 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1949 * @retval HAL status
1950*/
1951HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1952{
1953 /* Disable TXEIE and TCIE interrupts */
1954 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1955
1956 /* Disable the UART DMA Tx request if enabled */
1957 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1958 {
1959 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1960
1961 /* Abort the UART DMA Tx stream : use blocking DMA Abort API (no callback) */
1962 if (huart->hdmatx != NULL)
1963 {
1964 /* Set the UART DMA Abort callback to Null.
1965 No call back execution at end of DMA abort procedure */
1966 huart->hdmatx->XferAbortCallback = NULL;
1967
1968 if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1969 {
1970 if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1971 {
1972 /* Set error code to DMA */
1973 huart->ErrorCode = HAL_UART_ERROR_DMA;
1974
1975 return HAL_TIMEOUT;
1976 }
1977 }
1978 }
1979 }
1980
1981 /* Reset Tx transfer counter */
1982 huart->TxXferCount = 0x00U;
1983
1984 /* Restore huart->gState to Ready */
1985 huart->gState = HAL_UART_STATE_READY;
1986
1987 return HAL_OK;
1988}
1989
1990/**
1991 * @brief Abort ongoing Receive transfer (blocking mode).
1992 * @param huart UART handle.
1993 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1994 * This procedure performs following operations :
1995 * - Disable UART Interrupts (Rx)
1996 * - Disable the DMA transfer in the peripheral register (if enabled)
1997 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1998 * - Set handle State to READY
1999 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
2000 * @retval HAL status
2001*/
2002HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
2003{
2004 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2005 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2006 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2007
2008 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2009 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2010 {
2011 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2012 }
2013
2014 /* Disable the UART DMA Rx request if enabled */
2015 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2016 {
2017 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2018
2019 /* Abort the UART DMA Rx stream : use blocking DMA Abort API (no callback) */
2020 if (huart->hdmarx != NULL)
2021 {
2022 /* Set the UART DMA Abort callback to Null.
2023 No call back execution at end of DMA abort procedure */
2024 huart->hdmarx->XferAbortCallback = NULL;
2025
2026 if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
2027 {
2028 if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
2029 {
2030 /* Set error code to DMA */
2031 huart->ErrorCode = HAL_UART_ERROR_DMA;
2032
2033 return HAL_TIMEOUT;
2034 }
2035 }
2036 }
2037 }
2038
2039 /* Reset Rx transfer counter */
2040 huart->RxXferCount = 0x00U;
2041
2042 /* Restore huart->RxState to Ready */
2043 huart->RxState = HAL_UART_STATE_READY;
2044 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2045
2046 return HAL_OK;
2047}
2048
2049/**
2050 * @brief Abort ongoing transfers (Interrupt mode).
2051 * @param huart UART handle.
2052 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
2053 * This procedure performs following operations :
2054 * - Disable UART Interrupts (Tx and Rx)
2055 * - Disable the DMA transfer in the peripheral register (if enabled)
2056 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2057 * - Set handle State to READY
2058 * - At abort completion, call user abort complete callback
2059 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2060 * considered as completed only when user abort complete callback is executed (not when exiting function).
2061 * @retval HAL status
2062*/
2063HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
2064{
2065 uint32_t AbortCplt = 0x01U;
2066
2067 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2068 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
2069 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2070
2071 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2072 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2073 {
2074 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2075 }
2076
2077 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
2078 before any call to DMA Abort functions */
2079 /* DMA Tx Handle is valid */
2080 if (huart->hdmatx != NULL)
2081 {
2082 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
2083 Otherwise, set it to NULL */
2084 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2085 {
2086 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
2087 }
2088 else
2089 {
2090 huart->hdmatx->XferAbortCallback = NULL;
2091 }
2092 }
2093 /* DMA Rx Handle is valid */
2094 if (huart->hdmarx != NULL)
2095 {
2096 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
2097 Otherwise, set it to NULL */
2098 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2099 {
2100 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
2101 }
2102 else
2103 {
2104 huart->hdmarx->XferAbortCallback = NULL;
2105 }
2106 }
2107
2108 /* Disable the UART DMA Tx request if enabled */
2109 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2110 {
2111 /* Disable DMA Tx at UART level */
2112 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2113
2114 /* Abort the UART DMA Tx stream : use non blocking DMA Abort API (callback) */
2115 if (huart->hdmatx != NULL)
2116 {
2117 /* UART Tx DMA Abort callback has already been initialised :
2118 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2119
2120 /* Abort DMA TX */
2121 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2122 {
2123 huart->hdmatx->XferAbortCallback = NULL;
2124 }
2125 else
2126 {
2127 AbortCplt = 0x00U;
2128 }
2129 }
2130 }
2131
2132 /* Disable the UART DMA Rx request if enabled */
2133 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2134 {
2135 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2136
2137 /* Abort the UART DMA Rx stream : use non blocking DMA Abort API (callback) */
2138 if (huart->hdmarx != NULL)
2139 {
2140 /* UART Rx DMA Abort callback has already been initialised :
2141 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2142
2143 /* Abort DMA RX */
2144 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2145 {
2146 huart->hdmarx->XferAbortCallback = NULL;
2147 AbortCplt = 0x01U;
2148 }
2149 else
2150 {
2151 AbortCplt = 0x00U;
2152 }
2153 }
2154 }
2155
2156 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
2157 if (AbortCplt == 0x01U)
2158 {
2159 /* Reset Tx and Rx transfer counters */
2160 huart->TxXferCount = 0x00U;
2161 huart->RxXferCount = 0x00U;
2162
2163 /* Reset ErrorCode */
2164 huart->ErrorCode = HAL_UART_ERROR_NONE;
2165
2166 /* Restore huart->gState and huart->RxState to Ready */
2167 huart->gState = HAL_UART_STATE_READY;
2168 huart->RxState = HAL_UART_STATE_READY;
2169 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2170
2171 /* As no DMA to be aborted, call directly user Abort complete callback */
2172#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2173 /* Call registered Abort complete callback */
2174 huart->AbortCpltCallback(huart);
2175#else
2176 /* Call legacy weak Abort complete callback */
2177 HAL_UART_AbortCpltCallback(huart);
2178#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2179 }
2180
2181 return HAL_OK;
2182}
2183
2184/**
2185 * @brief Abort ongoing Transmit transfer (Interrupt mode).
2186 * @param huart UART handle.
2187 * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
2188 * This procedure performs following operations :
2189 * - Disable UART Interrupts (Tx)
2190 * - Disable the DMA transfer in the peripheral register (if enabled)
2191 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2192 * - Set handle State to READY
2193 * - At abort completion, call user abort complete callback
2194 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2195 * considered as completed only when user abort complete callback is executed (not when exiting function).
2196 * @retval HAL status
2197*/
2198HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
2199{
2200 /* Disable TXEIE and TCIE interrupts */
2201 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2202
2203 /* Disable the UART DMA Tx request if enabled */
2204 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2205 {
2206 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2207
2208 /* Abort the UART DMA Tx stream : use blocking DMA Abort API (no callback) */
2209 if (huart->hdmatx != NULL)
2210 {
2211 /* Set the UART DMA Abort callback :
2212 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2213 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
2214
2215 /* Abort DMA TX */
2216 if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2217 {
2218 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2219 huart->hdmatx->XferAbortCallback(huart->hdmatx);
2220 }
2221 }
2222 else
2223 {
2224 /* Reset Tx transfer counter */
2225 huart->TxXferCount = 0x00U;
2226
2227 /* Restore huart->gState to Ready */
2228 huart->gState = HAL_UART_STATE_READY;
2229
2230 /* As no DMA to be aborted, call directly user Abort complete callback */
2231#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2232 /* Call registered Abort Transmit Complete Callback */
2233 huart->AbortTransmitCpltCallback(huart);
2234#else
2235 /* Call legacy weak Abort Transmit Complete Callback */
2236 HAL_UART_AbortTransmitCpltCallback(huart);
2237#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2238 }
2239 }
2240 else
2241 {
2242 /* Reset Tx transfer counter */
2243 huart->TxXferCount = 0x00U;
2244
2245 /* Restore huart->gState to Ready */
2246 huart->gState = HAL_UART_STATE_READY;
2247
2248 /* As no DMA to be aborted, call directly user Abort complete callback */
2249#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2250 /* Call registered Abort Transmit Complete Callback */
2251 huart->AbortTransmitCpltCallback(huart);
2252#else
2253 /* Call legacy weak Abort Transmit Complete Callback */
2254 HAL_UART_AbortTransmitCpltCallback(huart);
2255#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2256 }
2257
2258 return HAL_OK;
2259}
2260
2261/**
2262 * @brief Abort ongoing Receive transfer (Interrupt mode).
2263 * @param huart UART handle.
2264 * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2265 * This procedure performs following operations :
2266 * - Disable UART Interrupts (Rx)
2267 * - Disable the DMA transfer in the peripheral register (if enabled)
2268 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2269 * - Set handle State to READY
2270 * - At abort completion, call user abort complete callback
2271 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2272 * considered as completed only when user abort complete callback is executed (not when exiting function).
2273 * @retval HAL status
2274*/
2275HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
2276{
2277 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2278 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2279 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2280
2281 /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2282 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2283 {
2284 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2285 }
2286
2287 /* Disable the UART DMA Rx request if enabled */
2288 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2289 {
2290 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2291
2292 /* Abort the UART DMA Rx stream : use blocking DMA Abort API (no callback) */
2293 if (huart->hdmarx != NULL)
2294 {
2295 /* Set the UART DMA Abort callback :
2296 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2297 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
2298
2299 /* Abort DMA RX */
2300 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2301 {
2302 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2303 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2304 }
2305 }
2306 else
2307 {
2308 /* Reset Rx transfer counter */
2309 huart->RxXferCount = 0x00U;
2310
2311 /* Restore huart->RxState to Ready */
2312 huart->RxState = HAL_UART_STATE_READY;
2313 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2314
2315 /* As no DMA to be aborted, call directly user Abort complete callback */
2316#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2317 /* Call registered Abort Receive Complete Callback */
2318 huart->AbortReceiveCpltCallback(huart);
2319#else
2320 /* Call legacy weak Abort Receive Complete Callback */
2321 HAL_UART_AbortReceiveCpltCallback(huart);
2322#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2323 }
2324 }
2325 else
2326 {
2327 /* Reset Rx transfer counter */
2328 huart->RxXferCount = 0x00U;
2329
2330 /* Restore huart->RxState to Ready */
2331 huart->RxState = HAL_UART_STATE_READY;
2332 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2333
2334 /* As no DMA to be aborted, call directly user Abort complete callback */
2335#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2336 /* Call registered Abort Receive Complete Callback */
2337 huart->AbortReceiveCpltCallback(huart);
2338#else
2339 /* Call legacy weak Abort Receive Complete Callback */
2340 HAL_UART_AbortReceiveCpltCallback(huart);
2341#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2342 }
2343
2344 return HAL_OK;
2345}
2346
2347/**
2348 * @brief This function handles UART interrupt request.
2349 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2350 * the configuration information for the specified UART module.
2351 * @retval None
2352 */
2353void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
2354{
2355 uint32_t isrflags = READ_REG(huart->Instance->SR);
2356 uint32_t cr1its = READ_REG(huart->Instance->CR1);
2357 uint32_t cr3its = READ_REG(huart->Instance->CR3);
2358 uint32_t errorflags = 0x00U;
2359 uint32_t dmarequest = 0x00U;
2360
2361 /* If no error occurs */
2362 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
2363 if (errorflags == RESET)
2364 {
2365 /* UART in mode Receiver -------------------------------------------------*/
2366 if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
2367 {
2368 UART_Receive_IT(huart);
2369 return;
2370 }
2371 }
2372
2373 /* If some errors occur */
2374 if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
2375 {
2376 /* UART parity error interrupt occurred ----------------------------------*/
2377 if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
2378 {
2379 huart->ErrorCode |= HAL_UART_ERROR_PE;
2380 }
2381
2382 /* UART noise error interrupt occurred -----------------------------------*/
2383 if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
2384 {
2385 huart->ErrorCode |= HAL_UART_ERROR_NE;
2386 }
2387
2388 /* UART frame error interrupt occurred -----------------------------------*/
2389 if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
2390 {
2391 huart->ErrorCode |= HAL_UART_ERROR_FE;
2392 }
2393
2394 /* UART Over-Run interrupt occurred --------------------------------------*/
2395 if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
2396 {
2397 huart->ErrorCode |= HAL_UART_ERROR_ORE;
2398 }
2399
2400 /* Call UART Error Call back function if need be --------------------------*/
2401 if (huart->ErrorCode != HAL_UART_ERROR_NONE)
2402 {
2403 /* UART in mode Receiver -----------------------------------------------*/
2404 if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
2405 {
2406 UART_Receive_IT(huart);
2407 }
2408
2409 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2410 consider error as blocking */
2411 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
2412 if (((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest)
2413 {
2414 /* Blocking error : transfer is aborted
2415 Set the UART state ready to be able to start again the process,
2416 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2417 UART_EndRxTransfer(huart);
2418
2419 /* Disable the UART DMA Rx request if enabled */
2420 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2421 {
2422 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2423
2424 /* Abort the UART DMA Rx stream */
2425 if (huart->hdmarx != NULL)
2426 {
2427 /* Set the UART DMA Abort callback :
2428 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2429 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
2430 if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2431 {
2432 /* Call Directly XferAbortCallback function in case of error */
2433 huart->hdmarx->XferAbortCallback(huart->hdmarx);
2434 }
2435 }
2436 else
2437 {
2438 /* Call user error callback */
2439#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2440 /*Call registered error callback*/
2441 huart->ErrorCallback(huart);
2442#else
2443 /*Call legacy weak error callback*/
2444 HAL_UART_ErrorCallback(huart);
2445#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2446 }
2447 }
2448 else
2449 {
2450 /* Call user error callback */
2451#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2452 /*Call registered error callback*/
2453 huart->ErrorCallback(huart);
2454#else
2455 /*Call legacy weak error callback*/
2456 HAL_UART_ErrorCallback(huart);
2457#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2458 }
2459 }
2460 else
2461 {
2462 /* Non Blocking error : transfer could go on.
2463 Error is notified to user through user error callback */
2464#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2465 /*Call registered error callback*/
2466 huart->ErrorCallback(huart);
2467#else
2468 /*Call legacy weak error callback*/
2469 HAL_UART_ErrorCallback(huart);
2470#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2471
2472 huart->ErrorCode = HAL_UART_ERROR_NONE;
2473 }
2474 }
2475 return;
2476 } /* End if some error occurs */
2477
2478 /* Check current reception Mode :
2479 If Reception till IDLE event has been selected : */
2480 if ( (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2481 &&((isrflags & USART_SR_IDLE) != 0U)
2482 &&((cr1its & USART_SR_IDLE) != 0U))
2483 {
2484 __HAL_UART_CLEAR_IDLEFLAG(huart);
2485
2486 /* Check if DMA mode is enabled in UART */
2487 if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2488 {
2489 /* DMA mode enabled */
2490 /* Check received length : If all expected data are received, do nothing,
2491 (DMA cplt callback will be called).
2492 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2493 uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx);
2494 if ( (nb_remaining_rx_data > 0U)
2495 &&(nb_remaining_rx_data < huart->RxXferSize))
2496 {
2497 /* Reception is not complete */
2498 huart->RxXferCount = nb_remaining_rx_data;
2499
2500 /* In Normal mode, end DMA xfer and HAL UART Rx process*/
2501 if (huart->hdmarx->Init.Mode != DMA_CIRCULAR)
2502 {
2503 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2504 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2505 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2506
2507 /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2508 in the UART CR3 register */
2509 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2510
2511 /* At end of Rx process, restore huart->RxState to Ready */
2512 huart->RxState = HAL_UART_STATE_READY;
2513 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2514
2515 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2516
2517 /* Last bytes received, so no need as the abort is immediate */
2518 (void)HAL_DMA_Abort(huart->hdmarx);
2519 }
2520#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2521 /*Call registered Rx Event callback*/
2522 huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2523#else
2524 /*Call legacy weak Rx Event callback*/
2525 HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2526#endif
2527 }
2528 return;
2529 }
2530 else
2531 {
2532 /* DMA mode not enabled */
2533 /* Check received length : If all expected data are received, do nothing.
2534 Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2535 uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount;
2536 if ( (huart->RxXferCount > 0U)
2537 &&(nb_rx_data > 0U) )
2538 {
2539 /* Disable the UART Parity Error Interrupt and RXNE interrupts */
2540 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2541
2542 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
2543 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2544
2545 /* Rx process is completed, restore huart->RxState to Ready */
2546 huart->RxState = HAL_UART_STATE_READY;
2547 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2548
2549 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2550#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2551 /*Call registered Rx complete callback*/
2552 huart->RxEventCallback(huart, nb_rx_data);
2553#else
2554 /*Call legacy weak Rx Event callback*/
2555 HAL_UARTEx_RxEventCallback(huart, nb_rx_data);
2556#endif
2557 }
2558 return;
2559 }
2560 }
2561
2562 /* UART in mode Transmitter ------------------------------------------------*/
2563 if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
2564 {
2565 UART_Transmit_IT(huart);
2566 return;
2567 }
2568
2569 /* UART in mode Transmitter end --------------------------------------------*/
2570 if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
2571 {
2572 UART_EndTransmit_IT(huart);
2573 return;
2574 }
2575}
2576
2577/**
2578 * @brief Tx Transfer completed callbacks.
2579 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2580 * the configuration information for the specified UART module.
2581 * @retval None
2582 */
2583__weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
2584{
2585 /* Prevent unused argument(s) compilation warning */
2586 UNUSED(huart);
2587 /* NOTE: This function should not be modified, when the callback is needed,
2588 the HAL_UART_TxCpltCallback could be implemented in the user file
2589 */
2590}
2591
2592/**
2593 * @brief Tx Half Transfer completed callbacks.
2594 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2595 * the configuration information for the specified UART module.
2596 * @retval None
2597 */
2598__weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
2599{
2600 /* Prevent unused argument(s) compilation warning */
2601 UNUSED(huart);
2602 /* NOTE: This function should not be modified, when the callback is needed,
2603 the HAL_UART_TxHalfCpltCallback could be implemented in the user file
2604 */
2605}
2606
2607/**
2608 * @brief Rx Transfer completed callbacks.
2609 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2610 * the configuration information for the specified UART module.
2611 * @retval None
2612 */
2613__weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
2614{
2615 /* Prevent unused argument(s) compilation warning */
2616 UNUSED(huart);
2617 /* NOTE: This function should not be modified, when the callback is needed,
2618 the HAL_UART_RxCpltCallback could be implemented in the user file
2619 */
2620}
2621
2622/**
2623 * @brief Rx Half Transfer completed callbacks.
2624 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2625 * the configuration information for the specified UART module.
2626 * @retval None
2627 */
2628__weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
2629{
2630 /* Prevent unused argument(s) compilation warning */
2631 UNUSED(huart);
2632 /* NOTE: This function should not be modified, when the callback is needed,
2633 the HAL_UART_RxHalfCpltCallback could be implemented in the user file
2634 */
2635}
2636
2637/**
2638 * @brief UART error callbacks.
2639 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2640 * the configuration information for the specified UART module.
2641 * @retval None
2642 */
2643__weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
2644{
2645 /* Prevent unused argument(s) compilation warning */
2646 UNUSED(huart);
2647 /* NOTE: This function should not be modified, when the callback is needed,
2648 the HAL_UART_ErrorCallback could be implemented in the user file
2649 */
2650}
2651
2652/**
2653 * @brief UART Abort Complete callback.
2654 * @param huart UART handle.
2655 * @retval None
2656 */
2657__weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
2658{
2659 /* Prevent unused argument(s) compilation warning */
2660 UNUSED(huart);
2661
2662 /* NOTE : This function should not be modified, when the callback is needed,
2663 the HAL_UART_AbortCpltCallback can be implemented in the user file.
2664 */
2665}
2666
2667/**
2668 * @brief UART Abort Complete callback.
2669 * @param huart UART handle.
2670 * @retval None
2671 */
2672__weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
2673{
2674 /* Prevent unused argument(s) compilation warning */
2675 UNUSED(huart);
2676
2677 /* NOTE : This function should not be modified, when the callback is needed,
2678 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2679 */
2680}
2681
2682/**
2683 * @brief UART Abort Receive Complete callback.
2684 * @param huart UART handle.
2685 * @retval None
2686 */
2687__weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
2688{
2689 /* Prevent unused argument(s) compilation warning */
2690 UNUSED(huart);
2691
2692 /* NOTE : This function should not be modified, when the callback is needed,
2693 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2694 */
2695}
2696
2697/**
2698 * @brief Reception Event Callback (Rx event notification called after use of advanced reception service).
2699 * @param huart UART handle
2700 * @param Size Number of data available in application reception buffer (indicates a position in
2701 * reception buffer until which, data are available)
2702 * @retval None
2703 */
2704__weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
2705{
2706 /* Prevent unused argument(s) compilation warning */
2707 UNUSED(huart);
2708 UNUSED(Size);
2709
2710 /* NOTE : This function should not be modified, when the callback is needed,
2711 the HAL_UARTEx_RxEventCallback can be implemented in the user file.
2712 */
2713}
2714
2715/**
2716 * @}
2717 */
2718
2719/** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2720 * @brief UART control functions
2721 *
2722@verbatim
2723 ==============================================================================
2724 ##### Peripheral Control functions #####
2725 ==============================================================================
2726 [..]
2727 This subsection provides a set of functions allowing to control the UART:
2728 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
2729 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
2730 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
2731 (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode
2732 (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode
2733
2734@endverbatim
2735 * @{
2736 */
2737
2738/**
2739 * @brief Transmits break characters.
2740 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2741 * the configuration information for the specified UART module.
2742 * @retval HAL status
2743 */
2744HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2745{
2746 /* Check the parameters */
2747 assert_param(IS_UART_INSTANCE(huart->Instance));
2748
2749 /* Process Locked */
2750 __HAL_LOCK(huart);
2751
2752 huart->gState = HAL_UART_STATE_BUSY;
2753
2754 /* Send break characters */
2755 SET_BIT(huart->Instance->CR1, USART_CR1_SBK);
2756
2757 huart->gState = HAL_UART_STATE_READY;
2758
2759 /* Process Unlocked */
2760 __HAL_UNLOCK(huart);
2761
2762 return HAL_OK;
2763}
2764
2765/**
2766 * @brief Enters the UART in mute mode.
2767 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2768 * the configuration information for the specified UART module.
2769 * @retval HAL status
2770 */
2771HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
2772{
2773 /* Check the parameters */
2774 assert_param(IS_UART_INSTANCE(huart->Instance));
2775
2776 /* Process Locked */
2777 __HAL_LOCK(huart);
2778
2779 huart->gState = HAL_UART_STATE_BUSY;
2780
2781 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
2782 SET_BIT(huart->Instance->CR1, USART_CR1_RWU);
2783
2784 huart->gState = HAL_UART_STATE_READY;
2785
2786 /* Process Unlocked */
2787 __HAL_UNLOCK(huart);
2788
2789 return HAL_OK;
2790}
2791
2792/**
2793 * @brief Exits the UART mute mode: wake up software.
2794 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2795 * the configuration information for the specified UART module.
2796 * @retval HAL status
2797 */
2798HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
2799{
2800 /* Check the parameters */
2801 assert_param(IS_UART_INSTANCE(huart->Instance));
2802
2803 /* Process Locked */
2804 __HAL_LOCK(huart);
2805
2806 huart->gState = HAL_UART_STATE_BUSY;
2807
2808 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
2809 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU);
2810
2811 huart->gState = HAL_UART_STATE_READY;
2812
2813 /* Process Unlocked */
2814 __HAL_UNLOCK(huart);
2815
2816 return HAL_OK;
2817}
2818
2819/**
2820 * @brief Enables the UART transmitter and disables the UART receiver.
2821 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2822 * the configuration information for the specified UART module.
2823 * @retval HAL status
2824 */
2825HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2826{
2827 uint32_t tmpreg = 0x00U;
2828
2829 /* Process Locked */
2830 __HAL_LOCK(huart);
2831
2832 huart->gState = HAL_UART_STATE_BUSY;
2833
2834 /*-------------------------- USART CR1 Configuration -----------------------*/
2835 tmpreg = huart->Instance->CR1;
2836
2837 /* Clear TE and RE bits */
2838 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
2839
2840 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2841 tmpreg |= (uint32_t)USART_CR1_TE;
2842
2843 /* Write to USART CR1 */
2844 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
2845
2846 huart->gState = HAL_UART_STATE_READY;
2847
2848 /* Process Unlocked */
2849 __HAL_UNLOCK(huart);
2850
2851 return HAL_OK;
2852}
2853
2854/**
2855 * @brief Enables the UART receiver and disables the UART transmitter.
2856 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2857 * the configuration information for the specified UART module.
2858 * @retval HAL status
2859 */
2860HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2861{
2862 uint32_t tmpreg = 0x00U;
2863
2864 /* Process Locked */
2865 __HAL_LOCK(huart);
2866
2867 huart->gState = HAL_UART_STATE_BUSY;
2868
2869 /*-------------------------- USART CR1 Configuration -----------------------*/
2870 tmpreg = huart->Instance->CR1;
2871
2872 /* Clear TE and RE bits */
2873 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
2874
2875 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2876 tmpreg |= (uint32_t)USART_CR1_RE;
2877
2878 /* Write to USART CR1 */
2879 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
2880
2881 huart->gState = HAL_UART_STATE_READY;
2882
2883 /* Process Unlocked */
2884 __HAL_UNLOCK(huart);
2885
2886 return HAL_OK;
2887}
2888
2889/**
2890 * @}
2891 */
2892
2893/** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
2894 * @brief UART State and Errors functions
2895 *
2896@verbatim
2897 ==============================================================================
2898 ##### Peripheral State and Errors functions #####
2899 ==============================================================================
2900 [..]
2901 This subsection provides a set of functions allowing to return the State of
2902 UART communication process, return Peripheral Errors occurred during communication
2903 process
2904 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
2905 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
2906
2907@endverbatim
2908 * @{
2909 */
2910
2911/**
2912 * @brief Returns the UART state.
2913 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2914 * the configuration information for the specified UART module.
2915 * @retval HAL state
2916 */
2917HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
2918{
2919 uint32_t temp1 = 0x00U, temp2 = 0x00U;
2920 temp1 = huart->gState;
2921 temp2 = huart->RxState;
2922
2923 return (HAL_UART_StateTypeDef)(temp1 | temp2);
2924}
2925
2926/**
2927 * @brief Return the UART error code
2928 * @param huart Pointer to a UART_HandleTypeDef structure that contains
2929 * the configuration information for the specified UART.
2930 * @retval UART Error Code
2931 */
2932uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
2933{
2934 return huart->ErrorCode;
2935}
2936
2937/**
2938 * @}
2939 */
2940
2941/**
2942 * @}
2943 */
2944
2945/** @defgroup UART_Private_Functions UART Private Functions
2946 * @{
2947 */
2948
2949/**
2950 * @brief Initialize the callbacks to their default values.
2951 * @param huart UART handle.
2952 * @retval none
2953 */
2954#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2955void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
2956{
2957 /* Init the UART Callback settings */
2958 huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
2959 huart->TxCpltCallback = HAL_UART_TxCpltCallback; /* Legacy weak TxCpltCallback */
2960 huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
2961 huart->RxCpltCallback = HAL_UART_RxCpltCallback; /* Legacy weak RxCpltCallback */
2962 huart->ErrorCallback = HAL_UART_ErrorCallback; /* Legacy weak ErrorCallback */
2963 huart->AbortCpltCallback = HAL_UART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2964 huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
2965 huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback; /* Legacy weak AbortReceiveCpltCallback */
2966 huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak RxEventCallback */
2967
2968}
2969#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2970
2971/**
2972 * @brief DMA UART transmit process complete callback.
2973 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2974 * the configuration information for the specified DMA module.
2975 * @retval None
2976 */
2977static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2978{
2979 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2980 /* DMA Normal mode*/
2981 if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2982 {
2983 huart->TxXferCount = 0x00U;
2984
2985 /* Disable the DMA transfer for transmit request by setting the DMAT bit
2986 in the UART CR3 register */
2987 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2988
2989 /* Enable the UART Transmit Complete Interrupt */
2990 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
2991
2992 }
2993 /* DMA Circular mode */
2994 else
2995 {
2996#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2997 /*Call registered Tx complete callback*/
2998 huart->TxCpltCallback(huart);
2999#else
3000 /*Call legacy weak Tx complete callback*/
3001 HAL_UART_TxCpltCallback(huart);
3002#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3003 }
3004}
3005
3006/**
3007 * @brief DMA UART transmit process half complete callback
3008 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3009 * the configuration information for the specified DMA module.
3010 * @retval None
3011 */
3012static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
3013{
3014 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3015
3016#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3017 /*Call registered Tx complete callback*/
3018 huart->TxHalfCpltCallback(huart);
3019#else
3020 /*Call legacy weak Tx complete callback*/
3021 HAL_UART_TxHalfCpltCallback(huart);
3022#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3023}
3024
3025/**
3026 * @brief DMA UART receive process complete callback.
3027 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3028 * the configuration information for the specified DMA module.
3029 * @retval None
3030 */
3031static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3032{
3033 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3034 /* DMA Normal mode*/
3035 if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
3036 {
3037 huart->RxXferCount = 0U;
3038
3039 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3040 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3041 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3042
3043 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
3044 in the UART CR3 register */
3045 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3046
3047 /* At end of Rx process, restore huart->RxState to Ready */
3048 huart->RxState = HAL_UART_STATE_READY;
3049
3050 /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */
3051 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3052 {
3053 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3054 }
3055 }
3056
3057 /* Check current reception Mode :
3058 If Reception till IDLE event has been selected : use Rx Event callback */
3059 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3060 {
3061#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3062 /*Call registered Rx Event callback*/
3063 huart->RxEventCallback(huart, huart->RxXferSize);
3064#else
3065 /*Call legacy weak Rx Event callback*/
3066 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3067#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3068 }
3069 else
3070 {
3071 /* In other cases : use Rx Complete callback */
3072#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3073 /*Call registered Rx complete callback*/
3074 huart->RxCpltCallback(huart);
3075#else
3076 /*Call legacy weak Rx complete callback*/
3077 HAL_UART_RxCpltCallback(huart);
3078#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3079 }
3080}
3081
3082/**
3083 * @brief DMA UART receive process half complete callback
3084 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3085 * the configuration information for the specified DMA module.
3086 * @retval None
3087 */
3088static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
3089{
3090 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3091
3092 /* Check current reception Mode :
3093 If Reception till IDLE event has been selected : use Rx Event callback */
3094 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3095 {
3096#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3097 /*Call registered Rx Event callback*/
3098 huart->RxEventCallback(huart, huart->RxXferSize/2U);
3099#else
3100 /*Call legacy weak Rx Event callback*/
3101 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize/2U);
3102#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3103 }
3104 else
3105 {
3106 /* In other cases : use Rx Half Complete callback */
3107#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3108 /*Call registered Rx Half complete callback*/
3109 huart->RxHalfCpltCallback(huart);
3110#else
3111 /*Call legacy weak Rx Half complete callback*/
3112 HAL_UART_RxHalfCpltCallback(huart);
3113#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3114 }
3115}
3116
3117/**
3118 * @brief DMA UART communication error callback.
3119 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3120 * the configuration information for the specified DMA module.
3121 * @retval None
3122 */
3123static void UART_DMAError(DMA_HandleTypeDef *hdma)
3124{
3125 uint32_t dmarequest = 0x00U;
3126 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3127
3128 /* Stop UART DMA Tx request if ongoing */
3129 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
3130 if ((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
3131 {
3132 huart->TxXferCount = 0x00U;
3133 UART_EndTxTransfer(huart);
3134 }
3135
3136 /* Stop UART DMA Rx request if ongoing */
3137 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
3138 if ((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
3139 {
3140 huart->RxXferCount = 0x00U;
3141 UART_EndRxTransfer(huart);
3142 }
3143
3144 huart->ErrorCode |= HAL_UART_ERROR_DMA;
3145#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3146 /*Call registered error callback*/
3147 huart->ErrorCallback(huart);
3148#else
3149 /*Call legacy weak error callback*/
3150 HAL_UART_ErrorCallback(huart);
3151#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3152}
3153
3154/**
3155 * @brief This function handles UART Communication Timeout.
3156 * @param huart Pointer to a UART_HandleTypeDef structure that contains
3157 * the configuration information for the specified UART module.
3158 * @param Flag specifies the UART flag to check.
3159 * @param Status The new Flag status (SET or RESET).
3160 * @param Tickstart Tick start value
3161 * @param Timeout Timeout duration
3162 * @retval HAL status
3163 */
3164static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
3165{
3166 /* Wait until flag is set */
3167 while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
3168 {
3169 /* Check for the Timeout */
3170 if (Timeout != HAL_MAX_DELAY)
3171 {
3172 if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
3173 {
3174 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
3175 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
3176 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3177
3178 huart->gState = HAL_UART_STATE_READY;
3179 huart->RxState = HAL_UART_STATE_READY;
3180
3181 /* Process Unlocked */
3182 __HAL_UNLOCK(huart);
3183
3184 return HAL_TIMEOUT;
3185 }
3186 }
3187 }
3188 return HAL_OK;
3189}
3190
3191/**
3192 * @brief Start Receive operation in interrupt mode.
3193 * @note This function could be called by all HAL UART API providing reception in Interrupt mode.
3194 * @note When calling this function, parameters validity is considered as already checked,
3195 * i.e. Rx State, buffer address, ...
3196 * UART Handle is assumed as Locked.
3197 * @param huart UART handle.
3198 * @param pData Pointer to data buffer (u8 or u16 data elements).
3199 * @param Size Amount of data elements (u8 or u16) to be received.
3200 * @retval HAL status
3201 */
3202HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3203{
3204 huart->pRxBuffPtr = pData;
3205 huart->RxXferSize = Size;
3206 huart->RxXferCount = Size;
3207
3208 huart->ErrorCode = HAL_UART_ERROR_NONE;
3209 huart->RxState = HAL_UART_STATE_BUSY_RX;
3210
3211 /* Process Unlocked */
3212 __HAL_UNLOCK(huart);
3213
3214 /* Enable the UART Parity Error Interrupt */
3215 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
3216
3217 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3218 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
3219
3220 /* Enable the UART Data Register not empty Interrupt */
3221 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
3222
3223 return HAL_OK;
3224}
3225
3226/**
3227 * @brief Start Receive operation in DMA mode.
3228 * @note This function could be called by all HAL UART API providing reception in DMA mode.
3229 * @note When calling this function, parameters validity is considered as already checked,
3230 * i.e. Rx State, buffer address, ...
3231 * UART Handle is assumed as Locked.
3232 * @param huart UART handle.
3233 * @param pData Pointer to data buffer (u8 or u16 data elements).
3234 * @param Size Amount of data elements (u8 or u16) to be received.
3235 * @retval HAL status
3236 */
3237HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3238{
3239 uint32_t *tmp;
3240
3241 huart->pRxBuffPtr = pData;
3242 huart->RxXferSize = Size;
3243
3244 huart->ErrorCode = HAL_UART_ERROR_NONE;
3245 huart->RxState = HAL_UART_STATE_BUSY_RX;
3246
3247 /* Set the UART DMA transfer complete callback */
3248 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
3249
3250 /* Set the UART DMA Half transfer complete callback */
3251 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
3252
3253 /* Set the DMA error callback */
3254 huart->hdmarx->XferErrorCallback = UART_DMAError;
3255
3256 /* Set the DMA abort callback */
3257 huart->hdmarx->XferAbortCallback = NULL;
3258
3259 /* Enable the DMA stream */
3260 tmp = (uint32_t *)&pData;
3261 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t *)tmp, Size);
3262
3263 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
3264 __HAL_UART_CLEAR_OREFLAG(huart);
3265
3266 /* Process Unlocked */
3267 __HAL_UNLOCK(huart);
3268
3269 /* Enable the UART Parity Error Interrupt */
3270 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3271
3272 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3273 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3274
3275 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
3276 in the UART CR3 register */
3277 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3278
3279 return HAL_OK;
3280}
3281
3282/**
3283 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3284 * @param huart UART handle.
3285 * @retval None
3286 */
3287static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
3288{
3289 /* Disable TXEIE and TCIE interrupts */
3290 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
3291
3292 /* At end of Tx process, restore huart->gState to Ready */
3293 huart->gState = HAL_UART_STATE_READY;
3294}
3295
3296/**
3297 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3298 * @param huart UART handle.
3299 * @retval None
3300 */
3301static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
3302{
3303 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3304 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
3305 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3306
3307 /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */
3308 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3309 {
3310 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3311 }
3312
3313 /* At end of Rx process, restore huart->RxState to Ready */
3314 huart->RxState = HAL_UART_STATE_READY;
3315 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3316}
3317
3318/**
3319 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
3320 * (To be called at end of DMA Abort procedure following error occurrence).
3321 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3322 * the configuration information for the specified DMA module.
3323 * @retval None
3324 */
3325static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3326{
3327 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3328 huart->RxXferCount = 0x00U;
3329 huart->TxXferCount = 0x00U;
3330
3331#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3332 /*Call registered error callback*/
3333 huart->ErrorCallback(huart);
3334#else
3335 /*Call legacy weak error callback*/
3336 HAL_UART_ErrorCallback(huart);
3337#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3338}
3339
3340/**
3341 * @brief DMA UART Tx communication abort callback, when initiated by user
3342 * (To be called at end of DMA Tx Abort procedure following user abort request).
3343 * @note When this callback is executed, User Abort complete call back is called only if no
3344 * Abort still ongoing for Rx DMA Handle.
3345 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3346 * the configuration information for the specified DMA module.
3347 * @retval None
3348 */
3349static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3350{
3351 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3352
3353 huart->hdmatx->XferAbortCallback = NULL;
3354
3355 /* Check if an Abort process is still ongoing */
3356 if (huart->hdmarx != NULL)
3357 {
3358 if (huart->hdmarx->XferAbortCallback != NULL)
3359 {
3360 return;
3361 }
3362 }
3363
3364 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3365 huart->TxXferCount = 0x00U;
3366 huart->RxXferCount = 0x00U;
3367
3368 /* Reset ErrorCode */
3369 huart->ErrorCode = HAL_UART_ERROR_NONE;
3370
3371 /* Restore huart->gState and huart->RxState to Ready */
3372 huart->gState = HAL_UART_STATE_READY;
3373 huart->RxState = HAL_UART_STATE_READY;
3374 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3375
3376 /* Call user Abort complete callback */
3377#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3378 /* Call registered Abort complete callback */
3379 huart->AbortCpltCallback(huart);
3380#else
3381 /* Call legacy weak Abort complete callback */
3382 HAL_UART_AbortCpltCallback(huart);
3383#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3384}
3385
3386/**
3387 * @brief DMA UART Rx communication abort callback, when initiated by user
3388 * (To be called at end of DMA Rx Abort procedure following user abort request).
3389 * @note When this callback is executed, User Abort complete call back is called only if no
3390 * Abort still ongoing for Tx DMA Handle.
3391 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3392 * the configuration information for the specified DMA module.
3393 * @retval None
3394 */
3395static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3396{
3397 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3398
3399 huart->hdmarx->XferAbortCallback = NULL;
3400
3401 /* Check if an Abort process is still ongoing */
3402 if (huart->hdmatx != NULL)
3403 {
3404 if (huart->hdmatx->XferAbortCallback != NULL)
3405 {
3406 return;
3407 }
3408 }
3409
3410 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3411 huart->TxXferCount = 0x00U;
3412 huart->RxXferCount = 0x00U;
3413
3414 /* Reset ErrorCode */
3415 huart->ErrorCode = HAL_UART_ERROR_NONE;
3416
3417 /* Restore huart->gState and huart->RxState to Ready */
3418 huart->gState = HAL_UART_STATE_READY;
3419 huart->RxState = HAL_UART_STATE_READY;
3420 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3421
3422 /* Call user Abort complete callback */
3423#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3424 /* Call registered Abort complete callback */
3425 huart->AbortCpltCallback(huart);
3426#else
3427 /* Call legacy weak Abort complete callback */
3428 HAL_UART_AbortCpltCallback(huart);
3429#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3430}
3431
3432/**
3433 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
3434 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3435 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3436 * and leads to user Tx Abort Complete callback execution).
3437 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3438 * the configuration information for the specified DMA module.
3439 * @retval None
3440 */
3441static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3442{
3443 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3444
3445 huart->TxXferCount = 0x00U;
3446
3447 /* Restore huart->gState to Ready */
3448 huart->gState = HAL_UART_STATE_READY;
3449
3450 /* Call user Abort complete callback */
3451#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3452 /* Call registered Abort Transmit Complete Callback */
3453 huart->AbortTransmitCpltCallback(huart);
3454#else
3455 /* Call legacy weak Abort Transmit Complete Callback */
3456 HAL_UART_AbortTransmitCpltCallback(huart);
3457#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3458}
3459
3460/**
3461 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
3462 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
3463 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
3464 * and leads to user Rx Abort Complete callback execution).
3465 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
3466 * the configuration information for the specified DMA module.
3467 * @retval None
3468 */
3469static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3470{
3471 UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3472
3473 huart->RxXferCount = 0x00U;
3474
3475 /* Restore huart->RxState to Ready */
3476 huart->RxState = HAL_UART_STATE_READY;
3477 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3478
3479 /* Call user Abort complete callback */
3480#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3481 /* Call registered Abort Receive Complete Callback */
3482 huart->AbortReceiveCpltCallback(huart);
3483#else
3484 /* Call legacy weak Abort Receive Complete Callback */
3485 HAL_UART_AbortReceiveCpltCallback(huart);
3486#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3487}
3488
3489/**
3490 * @brief Sends an amount of data in non blocking mode.
3491 * @param huart Pointer to a UART_HandleTypeDef structure that contains
3492 * the configuration information for the specified UART module.
3493 * @retval HAL status
3494 */
3495static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
3496{
3497 uint16_t *tmp;
3498
3499 /* Check that a Tx process is ongoing */
3500 if (huart->gState == HAL_UART_STATE_BUSY_TX)
3501 {
3502 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3503 {
3504 tmp = (uint16_t *) huart->pTxBuffPtr;
3505 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
3506 huart->pTxBuffPtr += 2U;
3507 }
3508 else
3509 {
3510 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
3511 }
3512
3513 if (--huart->TxXferCount == 0U)
3514 {
3515 /* Disable the UART Transmit Complete Interrupt */
3516 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
3517
3518 /* Enable the UART Transmit Complete Interrupt */
3519 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
3520 }
3521 return HAL_OK;
3522 }
3523 else
3524 {
3525 return HAL_BUSY;
3526 }
3527}
3528
3529/**
3530 * @brief Wraps up transmission in non blocking mode.
3531 * @param huart Pointer to a UART_HandleTypeDef structure that contains
3532 * the configuration information for the specified UART module.
3533 * @retval HAL status
3534 */
3535static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
3536{
3537 /* Disable the UART Transmit Complete Interrupt */
3538 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
3539
3540 /* Tx process is ended, restore huart->gState to Ready */
3541 huart->gState = HAL_UART_STATE_READY;
3542
3543#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3544 /*Call registered Tx complete callback*/
3545 huart->TxCpltCallback(huart);
3546#else
3547 /*Call legacy weak Tx complete callback*/
3548 HAL_UART_TxCpltCallback(huart);
3549#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3550
3551 return HAL_OK;
3552}
3553
3554/**
3555 * @brief Receives an amount of data in non blocking mode
3556 * @param huart Pointer to a UART_HandleTypeDef structure that contains
3557 * the configuration information for the specified UART module.
3558 * @retval HAL status
3559 */
3560static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
3561{
3562 uint8_t *pdata8bits;
3563 uint16_t *pdata16bits;
3564
3565 /* Check that a Rx process is ongoing */
3566 if (huart->RxState == HAL_UART_STATE_BUSY_RX)
3567 {
3568 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3569 {
3570 pdata8bits = NULL;
3571 pdata16bits = (uint16_t *) huart->pRxBuffPtr;
3572 *pdata16bits = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
3573 huart->pRxBuffPtr += 2U;
3574 }
3575 else
3576 {
3577 pdata8bits = (uint8_t *) huart->pRxBuffPtr;
3578 pdata16bits = NULL;
3579
3580 if ((huart->Init.WordLength == UART_WORDLENGTH_9B) || ((huart->Init.WordLength == UART_WORDLENGTH_8B) && (huart->Init.Parity == UART_PARITY_NONE)))
3581 {
3582 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
3583 }
3584 else
3585 {
3586 *pdata8bits = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
3587 }
3588 huart->pRxBuffPtr += 1U;
3589 }
3590
3591 if (--huart->RxXferCount == 0U)
3592 {
3593 /* Disable the UART Data Register not empty Interrupt */
3594 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
3595
3596 /* Disable the UART Parity Error Interrupt */
3597 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
3598
3599 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3600 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
3601
3602 /* Rx process is completed, restore huart->RxState to Ready */
3603 huart->RxState = HAL_UART_STATE_READY;
3604
3605 /* Check current reception Mode :
3606 If Reception till IDLE event has been selected : */
3607 if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3608 {
3609 /* Disable IDLE interrupt */
3610 CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3611
3612#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3613 /*Call registered Rx Event callback*/
3614 huart->RxEventCallback(huart, huart->RxXferSize);
3615#else
3616 /*Call legacy weak Rx Event callback*/
3617 HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3618#endif
3619 }
3620 else
3621 {
3622 /* Standard reception API called */
3623#if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3624 /*Call registered Rx complete callback*/
3625 huart->RxCpltCallback(huart);
3626#else
3627 /*Call legacy weak Rx complete callback*/
3628 HAL_UART_RxCpltCallback(huart);
3629#endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3630 }
3631 huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3632 return HAL_OK;
3633 }
3634 return HAL_OK;
3635 }
3636 else
3637 {
3638 return HAL_BUSY;
3639 }
3640}
3641
3642/**
3643 * @brief Configures the UART peripheral.
3644 * @param huart Pointer to a UART_HandleTypeDef structure that contains
3645 * the configuration information for the specified UART module.
3646 * @retval None
3647 */
3648static void UART_SetConfig(UART_HandleTypeDef *huart)
3649{
3650 uint32_t tmpreg;
3651 uint32_t pclk;
3652
3653 /* Check the parameters */
3654 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
3655 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
3656 assert_param(IS_UART_PARITY(huart->Init.Parity));
3657 assert_param(IS_UART_MODE(huart->Init.Mode));
3658
3659 /*-------------------------- USART CR2 Configuration -----------------------*/
3660 /* Configure the UART Stop Bits: Set STOP[13:12] bits
3661 according to huart->Init.StopBits value */
3662 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
3663
3664 /*-------------------------- USART CR1 Configuration -----------------------*/
3665 /* Configure the UART Word Length, Parity and mode:
3666 Set the M bits according to huart->Init.WordLength value
3667 Set PCE and PS bits according to huart->Init.Parity value
3668 Set TE and RE bits according to huart->Init.Mode value
3669 Set OVER8 bit according to huart->Init.OverSampling value */
3670
3671 tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
3672 MODIFY_REG(huart->Instance->CR1,
3673 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
3674 tmpreg);
3675
3676 /*-------------------------- USART CR3 Configuration -----------------------*/
3677 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
3678 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
3679
3680
3681#if defined(USART6) && defined(UART9) && defined(UART10)
3682 if ((huart->Instance == USART1) || (huart->Instance == USART6) || (huart->Instance == UART9) || (huart->Instance == UART10))
3683 {
3684 pclk = HAL_RCC_GetPCLK2Freq();
3685 }
3686#elif defined(USART6)
3687 if ((huart->Instance == USART1) || (huart->Instance == USART6))
3688 {
3689 pclk = HAL_RCC_GetPCLK2Freq();
3690 }
3691#else
3692 if (huart->Instance == USART1)
3693 {
3694 pclk = HAL_RCC_GetPCLK2Freq();
3695 }
3696#endif /* USART6 */
3697 else
3698 {
3699 pclk = HAL_RCC_GetPCLK1Freq();
3700 }
3701 /*-------------------------- USART BRR Configuration ---------------------*/
3702 if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
3703 {
3704 huart->Instance->BRR = UART_BRR_SAMPLING8(pclk, huart->Init.BaudRate);
3705 }
3706 else
3707 {
3708 huart->Instance->BRR = UART_BRR_SAMPLING16(pclk, huart->Init.BaudRate);
3709 }
3710}
3711
3712/**
3713 * @}
3714 */
3715
3716#endif /* HAL_UART_MODULE_ENABLED */
3717/**
3718 * @}
3719 */
3720
3721/**
3722 * @}
3723 */
3724
3725/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.