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