1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_usart.c
4 * @author MCD Application Team
5 * @brief USART HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
8 * Peripheral (USART).
9 * + Initialization and de-initialization functions
10 * + IO operation functions
11 * + Peripheral Control functions
12 @verbatim
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
16 [..]
17 The USART HAL driver can be used as follows:
18
19 (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
20 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
21 (##) Enable the USARTx interface clock.
22 (##) USART pins configuration:
23 (+++) Enable the clock for the USART GPIOs.
24 (+++) Configure the USART pins as alternate function pull-up.
25 (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
26 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
27 (+++) Configure the USARTx interrupt priority.
28 (+++) Enable the NVIC USART IRQ handle.
29 (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
30 HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
31 (+++) Declare a DMA handle structure for the Tx/Rx stream.
32 (+++) Enable the DMAx interface clock.
33 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
34 (+++) Configure the DMA Tx/Rx stream.
35 (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
36 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx stream.
37 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
38 (used for last byte sending completion detection in DMA non circular mode)
39
40 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
41 flow control and Mode(Receiver/Transmitter) in the husart Init structure.
42
43 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
44 (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
45 by calling the customized HAL_USART_MspInit(&husart) API.
46
47 -@@- The specific USART interrupts (Transmission complete interrupt,
48 RXNE interrupt and Error Interrupts) will be managed using the macros
49 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
50
51 (#) Three operation modes are available within this driver :
52
53 *** Polling mode IO operation ***
54 =================================
55 [..]
56 (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
57 (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
58
59 *** Interrupt mode IO operation ***
60 ===================================
61 [..]
62 (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
63 (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
64 add his own code by customization of function pointer HAL_USART_TxCpltCallback
65 (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
66 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
67 add his own code by customization of function pointer HAL_USART_RxCpltCallback
68 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
69 add his own code by customization of function pointer HAL_USART_ErrorCallback
70
71 *** DMA mode IO operation ***
72 ==============================
73 [..]
74 (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
75 (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
76 add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
77 (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
78 add his own code by customization of function pointer HAL_USART_TxCpltCallback
79 (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
80 (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
81 add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
82 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_USART_RxCpltCallback
84 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
85 add his own code by customization of function pointer HAL_USART_ErrorCallback
86 (+) Pause the DMA Transfer using HAL_USART_DMAPause()
87 (+) Resume the DMA Transfer using HAL_USART_DMAResume()
88 (+) Stop the DMA Transfer using HAL_USART_DMAStop()
89
90 *** USART HAL driver macros list ***
91 =============================================
92 [..]
93 Below the list of most used macros in USART HAL driver.
94
95 (+) __HAL_USART_ENABLE: Enable the USART peripheral
96 (+) __HAL_USART_DISABLE: Disable the USART peripheral
97 (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
98 (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
99 (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
100 (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
101
102 [..]
103 (@) You can refer to the USART HAL driver header file for more useful macros
104
105 ##### Callback registration #####
106 ==================================
107
108 [..]
109 The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
110 allows the user to configure dynamically the driver callbacks.
111
112 [..]
113 Use Function @ref HAL_USART_RegisterCallback() to register a user callback.
114 Function @ref HAL_USART_RegisterCallback() allows to register following callbacks:
115 (+) TxHalfCpltCallback : Tx Half Complete Callback.
116 (+) TxCpltCallback : Tx Complete Callback.
117 (+) RxHalfCpltCallback : Rx Half Complete Callback.
118 (+) RxCpltCallback : Rx Complete Callback.
119 (+) TxRxCpltCallback : Tx Rx Complete Callback.
120 (+) ErrorCallback : Error Callback.
121 (+) AbortCpltCallback : Abort Complete Callback.
122 (+) MspInitCallback : USART MspInit.
123 (+) MspDeInitCallback : USART MspDeInit.
124 This function takes as parameters the HAL peripheral handle, the Callback ID
125 and a pointer to the user callback function.
126
127 [..]
128 Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default
129 weak (surcharged) function.
130 @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
131 and the Callback ID.
132 This function allows to reset following callbacks:
133 (+) TxHalfCpltCallback : Tx Half Complete Callback.
134 (+) TxCpltCallback : Tx Complete Callback.
135 (+) RxHalfCpltCallback : Rx Half Complete Callback.
136 (+) RxCpltCallback : Rx Complete Callback.
137 (+) TxRxCpltCallback : Tx Rx Complete Callback.
138 (+) ErrorCallback : Error Callback.
139 (+) AbortCpltCallback : Abort Complete Callback.
140 (+) MspInitCallback : USART MspInit.
141 (+) MspDeInitCallback : USART MspDeInit.
142
143 [..]
144 By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
145 all callbacks are set to the corresponding weak (surcharged) functions:
146 examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback().
147 Exception done for MspInit and MspDeInit functions that are respectively
148 reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init()
149 and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
150 If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit()
151 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
152
153 [..]
154 Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
155 Exception done MspInit/MspDeInit that can be registered/unregistered
156 in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
157 MspInit/DeInit callbacks can be used during the Init/DeInit.
158 In that case first register the MspInit/MspDeInit user callbacks
159 using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit()
160 or @ref HAL_USART_Init() function.
161
162 [..]
163 When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
164 not defined, the callback registration feature is not available
165 and weak (surcharged) callbacks are used.
166
167 @endverbatim
168 [..]
169 (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
170 in the data register is transmitted but is changed by the parity bit.
171 Depending on the frame length defined by the M bit (8-bits or 9-bits),
172 the possible USART frame formats are as listed in the following table:
173 +-------------------------------------------------------------+
174 | M bit | PCE bit | USART frame |
175 |---------------------|---------------------------------------|
176 | 0 | 0 | | SB | 8 bit data | STB | |
177 |---------|-----------|---------------------------------------|
178 | 0 | 1 | | SB | 7 bit data | PB | STB | |
179 |---------|-----------|---------------------------------------|
180 | 1 | 0 | | SB | 9 bit data | STB | |
181 |---------|-----------|---------------------------------------|
182 | 1 | 1 | | SB | 8 bit data | PB | STB | |
183 +-------------------------------------------------------------+
184 ******************************************************************************
185 * @attention
186 *
187 * <h2><center>© Copyright (c) 2016 STMicroelectronics.
188 * All rights reserved.</center></h2>
189 *
190 * This software component is licensed by ST under BSD 3-Clause license,
191 * the "License"; You may not use this file except in compliance with the
192 * License. You may obtain a copy of the License at:
193 * opensource.org/licenses/BSD-3-Clause
194 *
195 ******************************************************************************
196 */
197
198 /* Includes ------------------------------------------------------------------*/
199 #include "stm32f4xx_hal.h"
200
201 /** @addtogroup STM32F4xx_HAL_Driver
202 * @{
203 */
204
205 /** @defgroup USART USART
206 * @brief HAL USART Synchronous module driver
207 * @{
208 */
209 #ifdef HAL_USART_MODULE_ENABLED
210 /* Private typedef -----------------------------------------------------------*/
211 /* Private define ------------------------------------------------------------*/
212 /** @addtogroup USART_Private_Constants
213 * @{
214 */
215 #define DUMMY_DATA 0xFFFFU
216 #define USART_TIMEOUT_VALUE 22000U
217 /**
218 * @}
219 */
220 /* Private macro -------------------------------------------------------------*/
221 /* Private variables ---------------------------------------------------------*/
222 /* Private function prototypes -----------------------------------------------*/
223 /* Private functions ---------------------------------------------------------*/
224 /** @addtogroup USART_Private_Functions
225 * @{
226 */
227 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
228 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
229 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
230 static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
231 static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
232 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
233 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
234 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
235 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
236 static void USART_SetConfig(USART_HandleTypeDef *husart);
237 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
238 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
239 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
240 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
241 static void USART_DMAError(DMA_HandleTypeDef *hdma);
242 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
243 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
244 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
245
246 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
247 /**
248 * @}
249 */
250
251 /* Exported functions --------------------------------------------------------*/
252 /** @defgroup USART_Exported_Functions USART Exported Functions
253 * @{
254 */
255
256 /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
257 * @brief Initialization and Configuration functions
258 *
259 @verbatim
260 ==============================================================================
261 ##### Initialization and Configuration functions #####
262 ==============================================================================
263 [..]
264 This subsection provides a set of functions allowing to initialize the USART
265 in asynchronous and in synchronous modes.
266 (+) For the asynchronous mode only these parameters can be configured:
267 (++) Baud Rate
268 (++) Word Length
269 (++) Stop Bit
270 (++) Parity: If the parity is enabled, then the MSB bit of the data written
271 in the data register is transmitted but is changed by the parity bit.
272 Depending on the frame length defined by the M bit (8-bits or 9-bits),
273 please refer to Reference manual for possible USART frame formats.
274 (++) USART polarity
275 (++) USART phase
276 (++) USART LastBit
277 (++) Receiver/transmitter modes
278
279 [..]
280 The HAL_USART_Init() function follows the USART synchronous configuration
281 procedures (details for the procedures are available in reference manual
282 (RM0430 for STM32F4X3xx MCUs and RM0402 for STM32F412xx MCUs
283 RM0383 for STM32F411xC/E MCUs and RM0401 for STM32F410xx MCUs
284 RM0090 for STM32F4X5xx/STM32F4X7xx/STM32F429xx/STM32F439xx MCUs
285 RM0390 for STM32F446xx MCUs and RM0386 for STM32F469xx/STM32F479xx MCUs)).
286
287 @endverbatim
288 * @{
289 */
290
291 /**
292 * @brief Initialize the USART mode according to the specified
293 * parameters in the USART_InitTypeDef and initialize the associated handle.
294 * @param husart Pointer to a USART_HandleTypeDef structure that contains
295 * the configuration information for the specified USART module.
296 * @retval HAL status
297 */
HAL_USART_Init(USART_HandleTypeDef * husart)298 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
299 {
300 /* Check the USART handle allocation */
301 if (husart == NULL)
302 {
303 return HAL_ERROR;
304 }
305
306 /* Check the parameters */
307 assert_param(IS_USART_INSTANCE(husart->Instance));
308
309 if (husart->State == HAL_USART_STATE_RESET)
310 {
311 /* Allocate lock resource and initialize it */
312 husart->Lock = HAL_UNLOCKED;
313
314 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
315 USART_InitCallbacksToDefault(husart);
316
317 if (husart->MspInitCallback == NULL)
318 {
319 husart->MspInitCallback = HAL_USART_MspInit;
320 }
321
322 /* Init the low level hardware */
323 husart->MspInitCallback(husart);
324 #else
325 /* Init the low level hardware : GPIO, CLOCK */
326 HAL_USART_MspInit(husart);
327 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
328 }
329
330 husart->State = HAL_USART_STATE_BUSY;
331
332 /* Set the USART Communication parameters */
333 USART_SetConfig(husart);
334
335 /* In USART mode, the following bits must be kept cleared:
336 - LINEN bit in the USART_CR2 register
337 - HDSEL, SCEN and IREN bits in the USART_CR3 register */
338 CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
339 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
340
341 /* Enable the Peripheral */
342 __HAL_USART_ENABLE(husart);
343
344 /* Initialize the USART state */
345 husart->ErrorCode = HAL_USART_ERROR_NONE;
346 husart->State = HAL_USART_STATE_READY;
347
348 return HAL_OK;
349 }
350
351 /**
352 * @brief DeInitializes the USART peripheral.
353 * @param husart Pointer to a USART_HandleTypeDef structure that contains
354 * the configuration information for the specified USART module.
355 * @retval HAL status
356 */
HAL_USART_DeInit(USART_HandleTypeDef * husart)357 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
358 {
359 /* Check the USART handle allocation */
360 if (husart == NULL)
361 {
362 return HAL_ERROR;
363 }
364
365 /* Check the parameters */
366 assert_param(IS_USART_INSTANCE(husart->Instance));
367
368 husart->State = HAL_USART_STATE_BUSY;
369
370 /* Disable the Peripheral */
371 __HAL_USART_DISABLE(husart);
372
373 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
374 if (husart->MspDeInitCallback == NULL)
375 {
376 husart->MspDeInitCallback = HAL_USART_MspDeInit;
377 }
378 /* DeInit the low level hardware */
379 husart->MspDeInitCallback(husart);
380 #else
381 /* DeInit the low level hardware */
382 HAL_USART_MspDeInit(husart);
383 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
384
385 husart->ErrorCode = HAL_USART_ERROR_NONE;
386 husart->State = HAL_USART_STATE_RESET;
387
388 /* Release Lock */
389 __HAL_UNLOCK(husart);
390
391 return HAL_OK;
392 }
393
394 /**
395 * @brief USART MSP Init.
396 * @param husart Pointer to a USART_HandleTypeDef structure that contains
397 * the configuration information for the specified USART module.
398 * @retval None
399 */
HAL_USART_MspInit(USART_HandleTypeDef * husart)400 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
401 {
402 /* Prevent unused argument(s) compilation warning */
403 UNUSED(husart);
404 /* NOTE: This function should not be modified, when the callback is needed,
405 the HAL_USART_MspInit could be implemented in the user file
406 */
407 }
408
409 /**
410 * @brief USART MSP DeInit.
411 * @param husart Pointer to a USART_HandleTypeDef structure that contains
412 * the configuration information for the specified USART module.
413 * @retval None
414 */
HAL_USART_MspDeInit(USART_HandleTypeDef * husart)415 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
416 {
417 /* Prevent unused argument(s) compilation warning */
418 UNUSED(husart);
419 /* NOTE: This function should not be modified, when the callback is needed,
420 the HAL_USART_MspDeInit could be implemented in the user file
421 */
422 }
423
424 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
425 /**
426 * @brief Register a User USART Callback
427 * To be used instead of the weak predefined callback
428 * @param husart usart handle
429 * @param CallbackID ID of the callback to be registered
430 * This parameter can be one of the following values:
431 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
432 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
433 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
434 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
435 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
436 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
437 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
438 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
439 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
440 * @param pCallback pointer to the Callback function
441 * @retval HAL status
442 + */
HAL_USART_RegisterCallback(USART_HandleTypeDef * husart,HAL_USART_CallbackIDTypeDef CallbackID,pUSART_CallbackTypeDef pCallback)443 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback)
444 {
445 HAL_StatusTypeDef status = HAL_OK;
446
447 if (pCallback == NULL)
448 {
449 /* Update the error code */
450 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
451
452 return HAL_ERROR;
453 }
454 /* Process locked */
455 __HAL_LOCK(husart);
456
457 if (husart->State == HAL_USART_STATE_READY)
458 {
459 switch (CallbackID)
460 {
461 case HAL_USART_TX_HALFCOMPLETE_CB_ID :
462 husart->TxHalfCpltCallback = pCallback;
463 break;
464
465 case HAL_USART_TX_COMPLETE_CB_ID :
466 husart->TxCpltCallback = pCallback;
467 break;
468
469 case HAL_USART_RX_HALFCOMPLETE_CB_ID :
470 husart->RxHalfCpltCallback = pCallback;
471 break;
472
473 case HAL_USART_RX_COMPLETE_CB_ID :
474 husart->RxCpltCallback = pCallback;
475 break;
476
477 case HAL_USART_TX_RX_COMPLETE_CB_ID :
478 husart->TxRxCpltCallback = pCallback;
479 break;
480
481 case HAL_USART_ERROR_CB_ID :
482 husart->ErrorCallback = pCallback;
483 break;
484
485 case HAL_USART_ABORT_COMPLETE_CB_ID :
486 husart->AbortCpltCallback = pCallback;
487 break;
488
489 case HAL_USART_MSPINIT_CB_ID :
490 husart->MspInitCallback = pCallback;
491 break;
492
493 case HAL_USART_MSPDEINIT_CB_ID :
494 husart->MspDeInitCallback = pCallback;
495 break;
496
497 default :
498 /* Update the error code */
499 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
500
501 /* Return error status */
502 status = HAL_ERROR;
503 break;
504 }
505 }
506 else if (husart->State == HAL_USART_STATE_RESET)
507 {
508 switch (CallbackID)
509 {
510 case HAL_USART_MSPINIT_CB_ID :
511 husart->MspInitCallback = pCallback;
512 break;
513
514 case HAL_USART_MSPDEINIT_CB_ID :
515 husart->MspDeInitCallback = pCallback;
516 break;
517
518 default :
519 /* Update the error code */
520 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
521
522 /* Return error status */
523 status = HAL_ERROR;
524 break;
525 }
526 }
527 else
528 {
529 /* Update the error code */
530 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
531
532 /* Return error status */
533 status = HAL_ERROR;
534 }
535
536 /* Release Lock */
537 __HAL_UNLOCK(husart);
538
539 return status;
540 }
541
542 /**
543 * @brief Unregister an UART Callback
544 * UART callaback is redirected to the weak predefined callback
545 * @param husart uart handle
546 * @param CallbackID ID of the callback to be unregistered
547 * This parameter can be one of the following values:
548 * @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
549 * @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
550 * @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
551 * @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
552 * @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
553 * @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
554 * @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
555 * @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
556 * @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
557 * @retval HAL status
558 */
HAL_USART_UnRegisterCallback(USART_HandleTypeDef * husart,HAL_USART_CallbackIDTypeDef CallbackID)559 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
560 {
561 HAL_StatusTypeDef status = HAL_OK;
562
563 /* Process locked */
564 __HAL_LOCK(husart);
565
566 if (husart->State == HAL_USART_STATE_READY)
567 {
568 switch (CallbackID)
569 {
570 case HAL_USART_TX_HALFCOMPLETE_CB_ID :
571 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
572 break;
573
574 case HAL_USART_TX_COMPLETE_CB_ID :
575 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
576 break;
577
578 case HAL_USART_RX_HALFCOMPLETE_CB_ID :
579 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
580 break;
581
582 case HAL_USART_RX_COMPLETE_CB_ID :
583 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
584 break;
585
586 case HAL_USART_TX_RX_COMPLETE_CB_ID :
587 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
588 break;
589
590 case HAL_USART_ERROR_CB_ID :
591 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
592 break;
593
594 case HAL_USART_ABORT_COMPLETE_CB_ID :
595 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
596 break;
597
598 case HAL_USART_MSPINIT_CB_ID :
599 husart->MspInitCallback = HAL_USART_MspInit; /* Legacy weak MspInitCallback */
600 break;
601
602 case HAL_USART_MSPDEINIT_CB_ID :
603 husart->MspDeInitCallback = HAL_USART_MspDeInit; /* Legacy weak MspDeInitCallback */
604 break;
605
606 default :
607 /* Update the error code */
608 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
609
610 /* Return error status */
611 status = HAL_ERROR;
612 break;
613 }
614 }
615 else if (husart->State == HAL_USART_STATE_RESET)
616 {
617 switch (CallbackID)
618 {
619 case HAL_USART_MSPINIT_CB_ID :
620 husart->MspInitCallback = HAL_USART_MspInit;
621 break;
622
623 case HAL_USART_MSPDEINIT_CB_ID :
624 husart->MspDeInitCallback = HAL_USART_MspDeInit;
625 break;
626
627 default :
628 /* Update the error code */
629 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
630
631 /* Return error status */
632 status = HAL_ERROR;
633 break;
634 }
635 }
636 else
637 {
638 /* Update the error code */
639 husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
640
641 /* Return error status */
642 status = HAL_ERROR;
643 }
644
645 /* Release Lock */
646 __HAL_UNLOCK(husart);
647
648 return status;
649 }
650 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
651
652 /**
653 * @}
654 */
655
656 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
657 * @brief USART Transmit and Receive functions
658 *
659 @verbatim
660 ==============================================================================
661 ##### IO operation functions #####
662 ==============================================================================
663 [..]
664 This subsection provides a set of functions allowing to manage the USART synchronous
665 data transfers.
666
667 [..]
668 The USART supports master mode only: it cannot receive or send data related to an input
669 clock (SCLK is always an output).
670
671 (#) There are two modes of transfer:
672 (++) Blocking mode: The communication is performed in polling mode.
673 The HAL status of all data processing is returned by the same function
674 after finishing transfer.
675 (++) No-Blocking mode: The communication is performed using Interrupts
676 or DMA, These API's return the HAL status.
677 The end of the data processing will be indicated through the
678 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
679 using DMA mode.
680 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
681 user callbacks
682 will be executed respectively at the end of the transmit or Receive process
683 The HAL_USART_ErrorCallback() user callback will be executed when a communication
684 error is detected
685
686 (#) Blocking mode APIs are :
687 (++) HAL_USART_Transmit() in simplex mode
688 (++) HAL_USART_Receive() in full duplex receive only
689 (++) HAL_USART_TransmitReceive() in full duplex mode
690
691 (#) Non Blocking mode APIs with Interrupt are :
692 (++) HAL_USART_Transmit_IT()in simplex mode
693 (++) HAL_USART_Receive_IT() in full duplex receive only
694 (++) HAL_USART_TransmitReceive_IT() in full duplex mode
695 (++) HAL_USART_IRQHandler()
696
697 (#) Non Blocking mode functions with DMA are :
698 (++) HAL_USART_Transmit_DMA()in simplex mode
699 (++) HAL_USART_Receive_DMA() in full duplex receive only
700 (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
701 (++) HAL_USART_DMAPause()
702 (++) HAL_USART_DMAResume()
703 (++) HAL_USART_DMAStop()
704
705 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
706 (++) HAL_USART_TxHalfCpltCallback()
707 (++) HAL_USART_TxCpltCallback()
708 (++) HAL_USART_RxHalfCpltCallback()
709 (++) HAL_USART_RxCpltCallback()
710 (++) HAL_USART_ErrorCallback()
711 (++) HAL_USART_TxRxCpltCallback()
712
713 (#) Non-Blocking mode transfers could be aborted using Abort API's :
714 (++) HAL_USART_Abort()
715 (++) HAL_USART_Abort_IT()
716
717 (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
718 (++) HAL_USART_AbortCpltCallback()
719
720 (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
721 Errors are handled as follows :
722 (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
723 to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
724 Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
725 and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
726 If user wants to abort it, Abort services should be called by user.
727 (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
728 This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
729 Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
730
731 @endverbatim
732 * @{
733 */
734
735 /**
736 * @brief Simplex Send an amount of data in blocking mode.
737 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
738 * the sent data is handled as a set of u16. In this case, Size must indicate the number
739 * of u16 provided through pTxData.
740 * @param husart Pointer to a USART_HandleTypeDef structure that contains
741 * the configuration information for the specified USART module.
742 * @param pTxData Pointer to data buffer (u8 or u16 data elements).
743 * @param Size Amount of data elements (u8 or u16) to be sent.
744 * @param Timeout Timeout duration.
745 * @retval HAL status
746 */
HAL_USART_Transmit(USART_HandleTypeDef * husart,uint8_t * pTxData,uint16_t Size,uint32_t Timeout)747 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
748 {
749 uint16_t *tmp;
750 uint32_t tickstart = 0U;
751
752 if (husart->State == HAL_USART_STATE_READY)
753 {
754 if ((pTxData == NULL) || (Size == 0))
755 {
756 return HAL_ERROR;
757 }
758
759 /* Process Locked */
760 __HAL_LOCK(husart);
761
762 husart->ErrorCode = HAL_USART_ERROR_NONE;
763 husart->State = HAL_USART_STATE_BUSY_TX;
764
765 /* Init tickstart for timeout management */
766 tickstart = HAL_GetTick();
767
768 husart->TxXferSize = Size;
769 husart->TxXferCount = Size;
770 while (husart->TxXferCount > 0U)
771 {
772 husart->TxXferCount--;
773 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
774 {
775 /* Wait for TC flag in order to write data in DR */
776 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
777 {
778 return HAL_TIMEOUT;
779 }
780 tmp = (uint16_t *) pTxData;
781 husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
782 if (husart->Init.Parity == USART_PARITY_NONE)
783 {
784 pTxData += 2U;
785 }
786 else
787 {
788 pTxData += 1U;
789 }
790 }
791 else
792 {
793 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
794 {
795 return HAL_TIMEOUT;
796 }
797 husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF);
798 }
799 }
800
801 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
802 {
803 return HAL_TIMEOUT;
804 }
805
806 husart->State = HAL_USART_STATE_READY;
807
808 /* Process Unlocked */
809 __HAL_UNLOCK(husart);
810
811 return HAL_OK;
812 }
813 else
814 {
815 return HAL_BUSY;
816 }
817 }
818
819 /**
820 * @brief Full-Duplex Receive an amount of data in blocking mode.
821 * @note To receive synchronous data, dummy data are simultaneously transmitted.
822 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
823 * the received data is handled as a set of u16. In this case, Size must indicate the number
824 * of u16 available through pRxData.
825 * @param husart Pointer to a USART_HandleTypeDef structure that contains
826 * the configuration information for the specified USART module.
827 * @param pRxData Pointer to data buffer (u8 or u16 data elements).
828 * @param Size Amount of data elements (u8 or u16) to be received.
829 * @param Timeout Timeout duration.
830 * @retval HAL status
831 */
HAL_USART_Receive(USART_HandleTypeDef * husart,uint8_t * pRxData,uint16_t Size,uint32_t Timeout)832 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
833 {
834 uint16_t *tmp;
835 uint32_t tickstart = 0U;
836
837 if (husart->State == HAL_USART_STATE_READY)
838 {
839 if ((pRxData == NULL) || (Size == 0))
840 {
841 return HAL_ERROR;
842 }
843 /* Process Locked */
844 __HAL_LOCK(husart);
845
846 husart->ErrorCode = HAL_USART_ERROR_NONE;
847 husart->State = HAL_USART_STATE_BUSY_RX;
848
849 /* Init tickstart for timeout management */
850 tickstart = HAL_GetTick();
851
852 husart->RxXferSize = Size;
853 husart->RxXferCount = Size;
854 /* Check the remain data to be received */
855 while (husart->RxXferCount > 0U)
856 {
857 husart->RxXferCount--;
858 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
859 {
860 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
861 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
862 {
863 return HAL_TIMEOUT;
864 }
865 /* Send dummy byte in order to generate clock */
866 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
867
868 /* Wait for RXNE Flag */
869 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
870 {
871 return HAL_TIMEOUT;
872 }
873 tmp = (uint16_t *) pRxData ;
874 if (husart->Init.Parity == USART_PARITY_NONE)
875 {
876 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
877 pRxData += 2U;
878 }
879 else
880 {
881 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
882 pRxData += 1U;
883 }
884 }
885 else
886 {
887 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
888 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
889 {
890 return HAL_TIMEOUT;
891 }
892
893 /* Send Dummy Byte in order to generate clock */
894 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
895
896 /* Wait until RXNE flag is set to receive the byte */
897 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
898 {
899 return HAL_TIMEOUT;
900 }
901 if (husart->Init.Parity == USART_PARITY_NONE)
902 {
903 /* Receive data */
904 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
905 }
906 else
907 {
908 /* Receive data */
909 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
910 }
911
912 }
913 }
914
915 husart->State = HAL_USART_STATE_READY;
916
917 /* Process Unlocked */
918 __HAL_UNLOCK(husart);
919
920 return HAL_OK;
921 }
922 else
923 {
924 return HAL_BUSY;
925 }
926 }
927
928 /**
929 * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (blocking mode).
930 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
931 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
932 * of u16 available through pTxData and through pRxData.
933 * @param husart Pointer to a USART_HandleTypeDef structure that contains
934 * the configuration information for the specified USART module.
935 * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
936 * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
937 * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
938 * @param Timeout Timeout duration
939 * @retval HAL status
940 */
HAL_USART_TransmitReceive(USART_HandleTypeDef * husart,uint8_t * pTxData,uint8_t * pRxData,uint16_t Size,uint32_t Timeout)941 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
942 {
943 uint16_t *tmp;
944 uint32_t tickstart = 0U;
945
946 if (husart->State == HAL_USART_STATE_READY)
947 {
948 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
949 {
950 return HAL_ERROR;
951 }
952 /* Process Locked */
953 __HAL_LOCK(husart);
954
955 husart->ErrorCode = HAL_USART_ERROR_NONE;
956 husart->State = HAL_USART_STATE_BUSY_RX;
957
958 /* Init tickstart for timeout management */
959 tickstart = HAL_GetTick();
960
961 husart->RxXferSize = Size;
962 husart->TxXferSize = Size;
963 husart->TxXferCount = Size;
964 husart->RxXferCount = Size;
965
966 /* Check the remain data to be received */
967 while (husart->TxXferCount > 0U)
968 {
969 husart->TxXferCount--;
970 husart->RxXferCount--;
971 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
972 {
973 /* Wait for TC flag in order to write data in DR */
974 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
975 {
976 return HAL_TIMEOUT;
977 }
978 tmp = (uint16_t *) pTxData;
979 husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
980 if (husart->Init.Parity == USART_PARITY_NONE)
981 {
982 pTxData += 2U;
983 }
984 else
985 {
986 pTxData += 1U;
987 }
988
989 /* Wait for RXNE Flag */
990 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
991 {
992 return HAL_TIMEOUT;
993 }
994 tmp = (uint16_t *) pRxData ;
995 if (husart->Init.Parity == USART_PARITY_NONE)
996 {
997 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
998 pRxData += 2U;
999 }
1000 else
1001 {
1002 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
1003 pRxData += 1U;
1004 }
1005 }
1006 else
1007 {
1008 /* Wait for TC flag in order to write data in DR */
1009 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1010 {
1011 return HAL_TIMEOUT;
1012 }
1013 husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF);
1014
1015 /* Wait for RXNE Flag */
1016 if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1017 {
1018 return HAL_TIMEOUT;
1019 }
1020 if (husart->Init.Parity == USART_PARITY_NONE)
1021 {
1022 /* Receive data */
1023 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
1024 }
1025 else
1026 {
1027 /* Receive data */
1028 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
1029 }
1030 }
1031 }
1032
1033 husart->State = HAL_USART_STATE_READY;
1034
1035 /* Process Unlocked */
1036 __HAL_UNLOCK(husart);
1037
1038 return HAL_OK;
1039 }
1040 else
1041 {
1042 return HAL_BUSY;
1043 }
1044 }
1045
1046 /**
1047 * @brief Simplex Send an amount of data in non-blocking mode.
1048 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1049 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1050 * of u16 provided through pTxData.
1051 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1052 * the configuration information for the specified USART module.
1053 * @param pTxData Pointer to data buffer (u8 or u16 data elements).
1054 * @param Size Amount of data elements (u8 or u16) to be sent.
1055 * @retval HAL status
1056 * @note The USART errors are not managed to avoid the overrun error.
1057 */
HAL_USART_Transmit_IT(USART_HandleTypeDef * husart,uint8_t * pTxData,uint16_t Size)1058 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
1059 {
1060 if (husart->State == HAL_USART_STATE_READY)
1061 {
1062 if ((pTxData == NULL) || (Size == 0))
1063 {
1064 return HAL_ERROR;
1065 }
1066
1067 /* Process Locked */
1068 __HAL_LOCK(husart);
1069
1070 husart->pTxBuffPtr = pTxData;
1071 husart->TxXferSize = Size;
1072 husart->TxXferCount = Size;
1073
1074 husart->ErrorCode = HAL_USART_ERROR_NONE;
1075 husart->State = HAL_USART_STATE_BUSY_TX;
1076
1077 /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
1078 are not managed by the USART transmit process to avoid the overrun interrupt
1079 when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
1080 to benefit for the frame error and noise interrupts the USART mode should be
1081 configured only for transmit "USART_MODE_TX"
1082 The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
1083 Noise error interrupt */
1084
1085 /* Process Unlocked */
1086 __HAL_UNLOCK(husart);
1087
1088 /* Enable the USART Transmit Data Register Empty Interrupt */
1089 SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
1090
1091 return HAL_OK;
1092 }
1093 else
1094 {
1095 return HAL_BUSY;
1096 }
1097 }
1098
1099 /**
1100 * @brief Simplex Receive an amount of data in non-blocking mode.
1101 * @note To receive synchronous data, dummy data are simultaneously transmitted.
1102 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1103 * the received data is handled as a set of u16. In this case, Size must indicate the number
1104 * of u16 available through pRxData.
1105 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1106 * the configuration information for the specified USART module.
1107 * @param pRxData Pointer to data buffer (u8 or u16 data elements).
1108 * @param Size Amount of data elements (u8 or u16) to be received.
1109 * @retval HAL status
1110 */
HAL_USART_Receive_IT(USART_HandleTypeDef * husart,uint8_t * pRxData,uint16_t Size)1111 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1112 {
1113 if (husart->State == HAL_USART_STATE_READY)
1114 {
1115 if ((pRxData == NULL) || (Size == 0))
1116 {
1117 return HAL_ERROR;
1118 }
1119 /* Process Locked */
1120 __HAL_LOCK(husart);
1121
1122 husart->pRxBuffPtr = pRxData;
1123 husart->RxXferSize = Size;
1124 husart->RxXferCount = Size;
1125
1126 husart->ErrorCode = HAL_USART_ERROR_NONE;
1127 husart->State = HAL_USART_STATE_BUSY_RX;
1128
1129 /* Process Unlocked */
1130 __HAL_UNLOCK(husart);
1131
1132 /* Enable the USART Parity Error and Data Register not empty Interrupts */
1133 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
1134
1135 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1136 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1137
1138 /* Send dummy byte in order to generate the clock for the slave to send data */
1139 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
1140
1141 return HAL_OK;
1142 }
1143 else
1144 {
1145 return HAL_BUSY;
1146 }
1147 }
1148
1149 /**
1150 * @brief Full-Duplex Send and Receive an amount of data in full-duplex mode (non-blocking).
1151 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1152 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1153 * of u16 available through pTxData and through pRxData.
1154 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1155 * the configuration information for the specified USART module.
1156 * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
1157 * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
1158 * @param Size Amount of data elements (u8 or u16) to be sent (same amount to be received).
1159 * @retval HAL status
1160 */
HAL_USART_TransmitReceive_IT(USART_HandleTypeDef * husart,uint8_t * pTxData,uint8_t * pRxData,uint16_t Size)1161 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1162 {
1163 if (husart->State == HAL_USART_STATE_READY)
1164 {
1165 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
1166 {
1167 return HAL_ERROR;
1168 }
1169 /* Process Locked */
1170 __HAL_LOCK(husart);
1171
1172 husart->pRxBuffPtr = pRxData;
1173 husart->RxXferSize = Size;
1174 husart->RxXferCount = Size;
1175 husart->pTxBuffPtr = pTxData;
1176 husart->TxXferSize = Size;
1177 husart->TxXferCount = Size;
1178
1179 husart->ErrorCode = HAL_USART_ERROR_NONE;
1180 husart->State = HAL_USART_STATE_BUSY_TX_RX;
1181
1182 /* Process Unlocked */
1183 __HAL_UNLOCK(husart);
1184
1185 /* Enable the USART Data Register not empty Interrupt */
1186 SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
1187
1188 /* Enable the USART Parity Error Interrupt */
1189 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1190
1191 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1192 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1193
1194 /* Enable the USART Transmit Data Register Empty Interrupt */
1195 SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
1196
1197 return HAL_OK;
1198 }
1199 else
1200 {
1201 return HAL_BUSY;
1202 }
1203 }
1204
1205 /**
1206 * @brief Simplex Send an amount of data in DMA mode.
1207 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1208 * the sent data is handled as a set of u16. In this case, Size must indicate the number
1209 * of u16 provided through pTxData.
1210 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1211 * the configuration information for the specified USART module.
1212 * @param pTxData Pointer to data buffer (u8 or u16 data elements).
1213 * @param Size Amount of data elements (u8 or u16) to be sent.
1214 * @retval HAL status
1215 */
HAL_USART_Transmit_DMA(USART_HandleTypeDef * husart,uint8_t * pTxData,uint16_t Size)1216 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
1217 {
1218 uint32_t *tmp;
1219
1220 if (husart->State == HAL_USART_STATE_READY)
1221 {
1222 if ((pTxData == NULL) || (Size == 0))
1223 {
1224 return HAL_ERROR;
1225 }
1226 /* Process Locked */
1227 __HAL_LOCK(husart);
1228
1229 husart->pTxBuffPtr = pTxData;
1230 husart->TxXferSize = Size;
1231 husart->TxXferCount = Size;
1232
1233 husart->ErrorCode = HAL_USART_ERROR_NONE;
1234 husart->State = HAL_USART_STATE_BUSY_TX;
1235
1236 /* Set the USART DMA transfer complete callback */
1237 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1238
1239 /* Set the USART DMA Half transfer complete callback */
1240 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1241
1242 /* Set the DMA error callback */
1243 husart->hdmatx->XferErrorCallback = USART_DMAError;
1244
1245 /* Set the DMA abort callback */
1246 husart->hdmatx->XferAbortCallback = NULL;
1247
1248 /* Enable the USART transmit DMA stream */
1249 tmp = (uint32_t *)&pTxData;
1250 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
1251
1252 /* Clear the TC flag in the SR register by writing 0 to it */
1253 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
1254
1255 /* Process Unlocked */
1256 __HAL_UNLOCK(husart);
1257
1258 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1259 in the USART CR3 register */
1260 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1261
1262 return HAL_OK;
1263 }
1264 else
1265 {
1266 return HAL_BUSY;
1267 }
1268 }
1269
1270 /**
1271 * @brief Full-Duplex Receive an amount of data in DMA mode.
1272 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1273 * the received data is handled as a set of u16. In this case, Size must indicate the number
1274 * of u16 available through pRxData.
1275 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1276 * the configuration information for the specified USART module.
1277 * @param pRxData Pointer to data buffer (u8 or u16 data elements).
1278 * @param Size Amount of data elements (u8 or u16) to be received.
1279 * @retval HAL status
1280 * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave.
1281 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1282 */
HAL_USART_Receive_DMA(USART_HandleTypeDef * husart,uint8_t * pRxData,uint16_t Size)1283 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1284 {
1285 uint32_t *tmp;
1286
1287 if (husart->State == HAL_USART_STATE_READY)
1288 {
1289 if ((pRxData == NULL) || (Size == 0))
1290 {
1291 return HAL_ERROR;
1292 }
1293
1294 /* Process Locked */
1295 __HAL_LOCK(husart);
1296
1297 husart->pRxBuffPtr = pRxData;
1298 husart->RxXferSize = Size;
1299 husart->pTxBuffPtr = pRxData;
1300 husart->TxXferSize = Size;
1301
1302 husart->ErrorCode = HAL_USART_ERROR_NONE;
1303 husart->State = HAL_USART_STATE_BUSY_RX;
1304
1305 /* Set the USART DMA Rx transfer complete callback */
1306 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1307
1308 /* Set the USART DMA Half transfer complete callback */
1309 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1310
1311 /* Set the USART DMA Rx transfer error callback */
1312 husart->hdmarx->XferErrorCallback = USART_DMAError;
1313
1314 /* Set the DMA abort callback */
1315 husart->hdmarx->XferAbortCallback = NULL;
1316
1317 /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
1318 is performed in DMA reception complete callback */
1319 husart->hdmatx->XferHalfCpltCallback = NULL;
1320 husart->hdmatx->XferCpltCallback = NULL;
1321
1322 /* Set the DMA error callback */
1323 husart->hdmatx->XferErrorCallback = USART_DMAError;
1324
1325 /* Set the DMA AbortCpltCallback */
1326 husart->hdmatx->XferAbortCallback = NULL;
1327
1328 /* Enable the USART receive DMA stream */
1329 tmp = (uint32_t *)&pRxData;
1330 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size);
1331
1332 /* Enable the USART transmit DMA stream: the transmit stream is used in order
1333 to generate in the non-blocking mode the clock to the slave device,
1334 this mode isn't a simplex receive mode but a full-duplex receive one */
1335 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
1336
1337 /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */
1338 __HAL_USART_CLEAR_OREFLAG(husart);
1339
1340 /* Process Unlocked */
1341 __HAL_UNLOCK(husart);
1342
1343 /* Enable the USART Parity Error Interrupt */
1344 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1345
1346 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1347 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1348
1349 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1350 in the USART CR3 register */
1351 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1352
1353 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1354 in the USART CR3 register */
1355 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1356
1357 return HAL_OK;
1358 }
1359 else
1360 {
1361 return HAL_BUSY;
1362 }
1363 }
1364
1365 /**
1366 * @brief Full-Duplex Transmit Receive an amount of data in DMA mode.
1367 * @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1368 * the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1369 * of u16 available through pTxData and through pRxData.
1370 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1371 * the configuration information for the specified USART module.
1372 * @param pTxData Pointer to TX data buffer (u8 or u16 data elements).
1373 * @param pRxData Pointer to RX data buffer (u8 or u16 data elements).
1374 * @param Size Amount of data elements (u8 or u16) to be received/sent.
1375 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1376 * @retval HAL status
1377 */
HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef * husart,uint8_t * pTxData,uint8_t * pRxData,uint16_t Size)1378 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1379 {
1380 uint32_t *tmp;
1381
1382 if (husart->State == HAL_USART_STATE_READY)
1383 {
1384 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
1385 {
1386 return HAL_ERROR;
1387 }
1388 /* Process Locked */
1389 __HAL_LOCK(husart);
1390
1391 husart->pRxBuffPtr = pRxData;
1392 husart->RxXferSize = Size;
1393 husart->pTxBuffPtr = pTxData;
1394 husart->TxXferSize = Size;
1395
1396 husart->ErrorCode = HAL_USART_ERROR_NONE;
1397 husart->State = HAL_USART_STATE_BUSY_TX_RX;
1398
1399 /* Set the USART DMA Rx transfer complete callback */
1400 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1401
1402 /* Set the USART DMA Half transfer complete callback */
1403 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1404
1405 /* Set the USART DMA Tx transfer complete callback */
1406 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1407
1408 /* Set the USART DMA Half transfer complete callback */
1409 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1410
1411 /* Set the USART DMA Tx transfer error callback */
1412 husart->hdmatx->XferErrorCallback = USART_DMAError;
1413
1414 /* Set the USART DMA Rx transfer error callback */
1415 husart->hdmarx->XferErrorCallback = USART_DMAError;
1416
1417 /* Set the DMA abort callback */
1418 husart->hdmarx->XferAbortCallback = NULL;
1419
1420 /* Enable the USART receive DMA stream */
1421 tmp = (uint32_t *)&pRxData;
1422 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size);
1423
1424 /* Enable the USART transmit DMA stream */
1425 tmp = (uint32_t *)&pTxData;
1426 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
1427
1428 /* Clear the TC flag in the SR register by writing 0 to it */
1429 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
1430
1431 /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
1432 __HAL_USART_CLEAR_OREFLAG(husart);
1433
1434 /* Process Unlocked */
1435 __HAL_UNLOCK(husart);
1436
1437 /* Enable the USART Parity Error Interrupt */
1438 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1439
1440 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1441 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1442
1443 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1444 in the USART CR3 register */
1445 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1446
1447 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1448 in the USART CR3 register */
1449 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1450
1451 return HAL_OK;
1452 }
1453 else
1454 {
1455 return HAL_BUSY;
1456 }
1457 }
1458
1459 /**
1460 * @brief Pauses the DMA Transfer.
1461 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1462 * the configuration information for the specified USART module.
1463 * @retval HAL status
1464 */
HAL_USART_DMAPause(USART_HandleTypeDef * husart)1465 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
1466 {
1467 /* Process Locked */
1468 __HAL_LOCK(husart);
1469
1470 /* Disable the USART DMA Tx request */
1471 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1472
1473 /* Process Unlocked */
1474 __HAL_UNLOCK(husart);
1475
1476 return HAL_OK;
1477 }
1478
1479 /**
1480 * @brief Resumes the DMA Transfer.
1481 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1482 * the configuration information for the specified USART module.
1483 * @retval HAL status
1484 */
HAL_USART_DMAResume(USART_HandleTypeDef * husart)1485 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
1486 {
1487 /* Process Locked */
1488 __HAL_LOCK(husart);
1489
1490 /* Enable the USART DMA Tx request */
1491 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1492
1493 /* Process Unlocked */
1494 __HAL_UNLOCK(husart);
1495
1496 return HAL_OK;
1497 }
1498
1499 /**
1500 * @brief Stops the DMA Transfer.
1501 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1502 * the configuration information for the specified USART module.
1503 * @retval HAL status
1504 */
HAL_USART_DMAStop(USART_HandleTypeDef * husart)1505 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
1506 {
1507 uint32_t dmarequest = 0x00U;
1508 /* The Lock is not implemented on this API to allow the user application
1509 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
1510 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1511 and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
1512 */
1513
1514 /* Stop USART DMA Tx request if ongoing */
1515 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
1516 if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
1517 {
1518 USART_EndTxTransfer(husart);
1519
1520 /* Abort the USART DMA Tx channel */
1521 if (husart->hdmatx != NULL)
1522 {
1523 HAL_DMA_Abort(husart->hdmatx);
1524 }
1525
1526 /* Disable the USART Tx DMA request */
1527 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1528 }
1529
1530 /* Stop USART DMA Rx request if ongoing */
1531 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
1532 if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
1533 {
1534 USART_EndRxTransfer(husart);
1535
1536 /* Abort the USART DMA Rx channel */
1537 if (husart->hdmarx != NULL)
1538 {
1539 HAL_DMA_Abort(husart->hdmarx);
1540 }
1541
1542 /* Disable the USART Rx DMA request */
1543 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1544 }
1545
1546 return HAL_OK;
1547 }
1548
1549 /**
1550 * @brief Abort ongoing transfer (blocking mode).
1551 * @param husart USART handle.
1552 * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
1553 * as described by TransferType parameter) started in Interrupt or DMA mode.
1554 * This procedure performs following operations :
1555 * - Disable PPP Interrupts (depending of transfer direction)
1556 * - Disable the DMA transfer in the peripheral register (if enabled)
1557 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1558 * - Set handle State to READY
1559 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1560 * @retval HAL status
1561 */
HAL_USART_Abort(USART_HandleTypeDef * husart)1562 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
1563 {
1564 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1565 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1566 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1567
1568 /* Disable the USART DMA Tx request if enabled */
1569 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1570 {
1571 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1572
1573 /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
1574 if (husart->hdmatx != NULL)
1575 {
1576 /* Set the USART DMA Abort callback to Null.
1577 No call back execution at end of DMA abort procedure */
1578 husart->hdmatx->XferAbortCallback = NULL;
1579
1580 HAL_DMA_Abort(husart->hdmatx);
1581 }
1582 }
1583
1584 /* Disable the USART DMA Rx request if enabled */
1585 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1586 {
1587 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1588
1589 /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
1590 if (husart->hdmarx != NULL)
1591 {
1592 /* Set the USART DMA Abort callback to Null.
1593 No call back execution at end of DMA abort procedure */
1594 husart->hdmarx->XferAbortCallback = NULL;
1595
1596 HAL_DMA_Abort(husart->hdmarx);
1597 }
1598 }
1599
1600 /* Reset Tx and Rx transfer counters */
1601 husart->TxXferCount = 0x00U;
1602 husart->RxXferCount = 0x00U;
1603
1604 /* Restore husart->State to Ready */
1605 husart->State = HAL_USART_STATE_READY;
1606
1607 /* Reset Handle ErrorCode to No Error */
1608 husart->ErrorCode = HAL_USART_ERROR_NONE;
1609
1610 return HAL_OK;
1611 }
1612
1613 /**
1614 * @brief Abort ongoing transfer (Interrupt mode).
1615 * @param husart USART handle.
1616 * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
1617 * as described by TransferType parameter) started in Interrupt or DMA mode.
1618 * This procedure performs following operations :
1619 * - Disable PPP Interrupts (depending of transfer direction)
1620 * - Disable the DMA transfer in the peripheral register (if enabled)
1621 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1622 * - Set handle State to READY
1623 * - At abort completion, call user abort complete callback
1624 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1625 * considered as completed only when user abort complete callback is executed (not when exiting function).
1626 * @retval HAL status
1627 */
HAL_USART_Abort_IT(USART_HandleTypeDef * husart)1628 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
1629 {
1630 uint32_t AbortCplt = 0x01U;
1631
1632 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1633 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1634 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1635
1636 /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
1637 before any call to DMA Abort functions */
1638 /* DMA Tx Handle is valid */
1639 if (husart->hdmatx != NULL)
1640 {
1641 /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
1642 Otherwise, set it to NULL */
1643 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1644 {
1645 husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
1646 }
1647 else
1648 {
1649 husart->hdmatx->XferAbortCallback = NULL;
1650 }
1651 }
1652 /* DMA Rx Handle is valid */
1653 if (husart->hdmarx != NULL)
1654 {
1655 /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
1656 Otherwise, set it to NULL */
1657 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1658 {
1659 husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
1660 }
1661 else
1662 {
1663 husart->hdmarx->XferAbortCallback = NULL;
1664 }
1665 }
1666
1667 /* Disable the USART DMA Tx request if enabled */
1668 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1669 {
1670 /* Disable DMA Tx at USART level */
1671 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1672
1673 /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
1674 if (husart->hdmatx != NULL)
1675 {
1676 /* USART Tx DMA Abort callback has already been initialised :
1677 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
1678
1679 /* Abort DMA TX */
1680 if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
1681 {
1682 husart->hdmatx->XferAbortCallback = NULL;
1683 }
1684 else
1685 {
1686 AbortCplt = 0x00U;
1687 }
1688 }
1689 }
1690
1691 /* Disable the USART DMA Rx request if enabled */
1692 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1693 {
1694 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1695
1696 /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
1697 if (husart->hdmarx != NULL)
1698 {
1699 /* USART Rx DMA Abort callback has already been initialised :
1700 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
1701
1702 /* Abort DMA RX */
1703 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
1704 {
1705 husart->hdmarx->XferAbortCallback = NULL;
1706 AbortCplt = 0x01U;
1707 }
1708 else
1709 {
1710 AbortCplt = 0x00U;
1711 }
1712 }
1713 }
1714
1715 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1716 if (AbortCplt == 0x01U)
1717 {
1718 /* Reset Tx and Rx transfer counters */
1719 husart->TxXferCount = 0x00U;
1720 husart->RxXferCount = 0x00U;
1721
1722 /* Reset errorCode */
1723 husart->ErrorCode = HAL_USART_ERROR_NONE;
1724
1725 /* Restore husart->State to Ready */
1726 husart->State = HAL_USART_STATE_READY;
1727
1728 /* As no DMA to be aborted, call directly user Abort complete callback */
1729 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
1730 /* Call registered Abort Complete Callback */
1731 husart->AbortCpltCallback(husart);
1732 #else
1733 /* Call legacy weak Abort Complete Callback */
1734 HAL_USART_AbortCpltCallback(husart);
1735 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
1736 }
1737
1738 return HAL_OK;
1739 }
1740
1741 /**
1742 * @brief This function handles USART interrupt request.
1743 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1744 * the configuration information for the specified USART module.
1745 * @retval None
1746 */
HAL_USART_IRQHandler(USART_HandleTypeDef * husart)1747 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
1748 {
1749 uint32_t isrflags = READ_REG(husart->Instance->SR);
1750 uint32_t cr1its = READ_REG(husart->Instance->CR1);
1751 uint32_t cr3its = READ_REG(husart->Instance->CR3);
1752 uint32_t errorflags = 0x00U;
1753 uint32_t dmarequest = 0x00U;
1754
1755 /* If no error occurs */
1756 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1757 if (errorflags == RESET)
1758 {
1759 /* USART in mode Receiver -------------------------------------------------*/
1760 if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1761 {
1762 if (husart->State == HAL_USART_STATE_BUSY_RX)
1763 {
1764 USART_Receive_IT(husart);
1765 }
1766 else
1767 {
1768 USART_TransmitReceive_IT(husart);
1769 }
1770 return;
1771 }
1772 }
1773 /* If some errors occur */
1774 if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1775 {
1776 /* USART parity error interrupt occurred ----------------------------------*/
1777 if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1778 {
1779 husart->ErrorCode |= HAL_USART_ERROR_PE;
1780 }
1781
1782 /* USART noise error interrupt occurred --------------------------------*/
1783 if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1784 {
1785 husart->ErrorCode |= HAL_USART_ERROR_NE;
1786 }
1787
1788 /* USART frame error interrupt occurred --------------------------------*/
1789 if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1790 {
1791 husart->ErrorCode |= HAL_USART_ERROR_FE;
1792 }
1793
1794 /* USART Over-Run interrupt occurred -----------------------------------*/
1795 if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
1796 {
1797 husart->ErrorCode |= HAL_USART_ERROR_ORE;
1798 }
1799
1800 if (husart->ErrorCode != HAL_USART_ERROR_NONE)
1801 {
1802 /* USART in mode Receiver -----------------------------------------------*/
1803 if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1804 {
1805 if (husart->State == HAL_USART_STATE_BUSY_RX)
1806 {
1807 USART_Receive_IT(husart);
1808 }
1809 else
1810 {
1811 USART_TransmitReceive_IT(husart);
1812 }
1813 }
1814 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1815 consider error as blocking */
1816 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
1817 if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest)
1818 {
1819 /* Set the USART state ready to be able to start again the process,
1820 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1821 USART_EndRxTransfer(husart);
1822
1823 /* Disable the USART DMA Rx request if enabled */
1824 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1825 {
1826 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1827
1828 /* Abort the USART DMA Rx channel */
1829 if (husart->hdmarx != NULL)
1830 {
1831 /* Set the USART DMA Abort callback :
1832 will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
1833 husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
1834
1835 if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
1836 {
1837 /* Call Directly XferAbortCallback function in case of error */
1838 husart->hdmarx->XferAbortCallback(husart->hdmarx);
1839 }
1840 }
1841 else
1842 {
1843 /* Call user error callback */
1844 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
1845 /* Call registered Error Callback */
1846 husart->ErrorCallback(husart);
1847 #else
1848 /* Call legacy weak Error Callback */
1849 HAL_USART_ErrorCallback(husart);
1850 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
1851 }
1852 }
1853 else
1854 {
1855 /* Call user error callback */
1856 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
1857 /* Call registered Error Callback */
1858 husart->ErrorCallback(husart);
1859 #else
1860 /* Call legacy weak Error Callback */
1861 HAL_USART_ErrorCallback(husart);
1862 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
1863 }
1864 }
1865 else
1866 {
1867 /* Call user error callback */
1868 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
1869 /* Call registered Error Callback */
1870 husart->ErrorCallback(husart);
1871 #else
1872 /* Call legacy weak Error Callback */
1873 HAL_USART_ErrorCallback(husart);
1874 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
1875 husart->ErrorCode = HAL_USART_ERROR_NONE;
1876 }
1877 }
1878 return;
1879 }
1880
1881 /* USART in mode Transmitter -----------------------------------------------*/
1882 if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1883 {
1884 if (husart->State == HAL_USART_STATE_BUSY_TX)
1885 {
1886 USART_Transmit_IT(husart);
1887 }
1888 else
1889 {
1890 USART_TransmitReceive_IT(husart);
1891 }
1892 return;
1893 }
1894
1895 /* USART in mode Transmitter (transmission end) ----------------------------*/
1896 if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1897 {
1898 USART_EndTransmit_IT(husart);
1899 return;
1900 }
1901 }
1902
1903 /**
1904 * @brief Tx Transfer completed callbacks.
1905 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1906 * the configuration information for the specified USART module.
1907 * @retval None
1908 */
HAL_USART_TxCpltCallback(USART_HandleTypeDef * husart)1909 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
1910 {
1911 /* Prevent unused argument(s) compilation warning */
1912 UNUSED(husart);
1913 /* NOTE: This function should not be modified, when the callback is needed,
1914 the HAL_USART_TxCpltCallback could be implemented in the user file
1915 */
1916 }
1917
1918 /**
1919 * @brief Tx Half Transfer completed callbacks.
1920 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1921 * the configuration information for the specified USART module.
1922 * @retval None
1923 */
HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef * husart)1924 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
1925 {
1926 /* Prevent unused argument(s) compilation warning */
1927 UNUSED(husart);
1928 /* NOTE: This function should not be modified, when the callback is needed,
1929 the HAL_USART_TxHalfCpltCallback could be implemented in the user file
1930 */
1931 }
1932
1933 /**
1934 * @brief Rx Transfer completed callbacks.
1935 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1936 * the configuration information for the specified USART module.
1937 * @retval None
1938 */
HAL_USART_RxCpltCallback(USART_HandleTypeDef * husart)1939 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
1940 {
1941 /* Prevent unused argument(s) compilation warning */
1942 UNUSED(husart);
1943 /* NOTE: This function should not be modified, when the callback is needed,
1944 the HAL_USART_RxCpltCallback could be implemented in the user file
1945 */
1946 }
1947
1948 /**
1949 * @brief Rx Half Transfer completed callbacks.
1950 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1951 * the configuration information for the specified USART module.
1952 * @retval None
1953 */
HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef * husart)1954 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
1955 {
1956 /* Prevent unused argument(s) compilation warning */
1957 UNUSED(husart);
1958 /* NOTE: This function should not be modified, when the callback is needed,
1959 the HAL_USART_RxHalfCpltCallback could be implemented in the user file
1960 */
1961 }
1962
1963 /**
1964 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
1965 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1966 * the configuration information for the specified USART module.
1967 * @retval None
1968 */
HAL_USART_TxRxCpltCallback(USART_HandleTypeDef * husart)1969 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
1970 {
1971 /* Prevent unused argument(s) compilation warning */
1972 UNUSED(husart);
1973 /* NOTE: This function should not be modified, when the callback is needed,
1974 the HAL_USART_TxRxCpltCallback could be implemented in the user file
1975 */
1976 }
1977
1978 /**
1979 * @brief USART error callbacks.
1980 * @param husart Pointer to a USART_HandleTypeDef structure that contains
1981 * the configuration information for the specified USART module.
1982 * @retval None
1983 */
HAL_USART_ErrorCallback(USART_HandleTypeDef * husart)1984 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
1985 {
1986 /* Prevent unused argument(s) compilation warning */
1987 UNUSED(husart);
1988 /* NOTE: This function should not be modified, when the callback is needed,
1989 the HAL_USART_ErrorCallback could be implemented in the user file
1990 */
1991 }
1992
1993 /**
1994 * @brief USART Abort Complete callback.
1995 * @param husart USART handle.
1996 * @retval None
1997 */
HAL_USART_AbortCpltCallback(USART_HandleTypeDef * husart)1998 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
1999 {
2000 /* Prevent unused argument(s) compilation warning */
2001 UNUSED(husart);
2002
2003 /* NOTE : This function should not be modified, when the callback is needed,
2004 the HAL_USART_AbortCpltCallback can be implemented in the user file.
2005 */
2006 }
2007
2008 /**
2009 * @}
2010 */
2011
2012 /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
2013 * @brief USART State and Errors functions
2014 *
2015 @verbatim
2016 ==============================================================================
2017 ##### Peripheral State and Errors functions #####
2018 ==============================================================================
2019 [..]
2020 This subsection provides a set of functions allowing to return the State of
2021 USART communication
2022 process, return Peripheral Errors occurred during communication process
2023 (+) HAL_USART_GetState() API can be helpful to check in run-time the state
2024 of the USART peripheral.
2025 (+) HAL_USART_GetError() check in run-time errors that could be occurred during
2026 communication.
2027 @endverbatim
2028 * @{
2029 */
2030
2031 /**
2032 * @brief Returns the USART state.
2033 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2034 * the configuration information for the specified USART module.
2035 * @retval HAL state
2036 */
HAL_USART_GetState(USART_HandleTypeDef * husart)2037 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
2038 {
2039 return husart->State;
2040 }
2041
2042 /**
2043 * @brief Return the USART error code
2044 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2045 * the configuration information for the specified USART.
2046 * @retval USART Error Code
2047 */
HAL_USART_GetError(USART_HandleTypeDef * husart)2048 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
2049 {
2050 return husart->ErrorCode;
2051 }
2052
2053 /**
2054 * @}
2055 */
2056
2057 /** @defgroup USART_Private_Functions USART Private Functions
2058 * @{
2059 */
2060
2061 /**
2062 * @brief Initialize the callbacks to their default values.
2063 * @param husart USART handle.
2064 * @retval none
2065 */
2066 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
USART_InitCallbacksToDefault(USART_HandleTypeDef * husart)2067 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
2068 {
2069 /* Init the USART Callback settings */
2070 husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
2071 husart->TxCpltCallback = HAL_USART_TxCpltCallback; /* Legacy weak TxCpltCallback */
2072 husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
2073 husart->RxCpltCallback = HAL_USART_RxCpltCallback; /* Legacy weak RxCpltCallback */
2074 husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
2075 husart->ErrorCallback = HAL_USART_ErrorCallback; /* Legacy weak ErrorCallback */
2076 husart->AbortCpltCallback = HAL_USART_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
2077 }
2078 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2079
2080 /**
2081 * @brief DMA USART transmit process complete callback.
2082 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2083 * the configuration information for the specified DMA module.
2084 * @retval None
2085 */
USART_DMATransmitCplt(DMA_HandleTypeDef * hdma)2086 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2087 {
2088 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2089 /* DMA Normal mode */
2090 if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2091 {
2092 husart->TxXferCount = 0U;
2093 if (husart->State == HAL_USART_STATE_BUSY_TX)
2094 {
2095 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2096 in the USART CR3 register */
2097 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2098
2099 /* Enable the USART Transmit Complete Interrupt */
2100 SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
2101 }
2102 }
2103 /* DMA Circular mode */
2104 else
2105 {
2106 if (husart->State == HAL_USART_STATE_BUSY_TX)
2107 {
2108 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2109 /* Call registered Tx Complete Callback */
2110 husart->TxCpltCallback(husart);
2111 #else
2112 /* Call legacy weak Tx Complete Callback */
2113 HAL_USART_TxCpltCallback(husart);
2114 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2115 }
2116 }
2117 }
2118
2119 /**
2120 * @brief DMA USART transmit process half complete callback
2121 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2122 * the configuration information for the specified DMA module.
2123 * @retval None
2124 */
USART_DMATxHalfCplt(DMA_HandleTypeDef * hdma)2125 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2126 {
2127 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2128
2129 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2130 /* Call registered Tx Half Complete Callback */
2131 husart->TxHalfCpltCallback(husart);
2132 #else
2133 /* Call legacy weak Tx Half Complete Callback */
2134 HAL_USART_TxHalfCpltCallback(husart);
2135 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2136 }
2137
2138 /**
2139 * @brief DMA USART receive process complete callback.
2140 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2141 * the configuration information for the specified DMA module.
2142 * @retval None
2143 */
USART_DMAReceiveCplt(DMA_HandleTypeDef * hdma)2144 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2145 {
2146 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2147 /* DMA Normal mode */
2148 if ((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2149 {
2150 husart->RxXferCount = 0x00U;
2151
2152 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2153 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2154 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2155
2156 /* Disable the DMA transfer for the Transmit/receiver request by clearing the DMAT/DMAR bit
2157 in the USART CR3 register */
2158 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
2159 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2160
2161 husart->State = HAL_USART_STATE_READY;
2162
2163 /* The USART state is HAL_USART_STATE_BUSY_RX */
2164 if (husart->State == HAL_USART_STATE_BUSY_RX)
2165 {
2166 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2167 /* Call registered Rx Complete Callback */
2168 husart->RxCpltCallback(husart);
2169 #else
2170 /* Call legacy weak Rx Complete Callback */
2171 HAL_USART_RxCpltCallback(husart);
2172 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2173 }
2174 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2175 else
2176 {
2177 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2178 /* Call registered Tx Rx Complete Callback */
2179 husart->TxRxCpltCallback(husart);
2180 #else
2181 /* Call legacy weak Tx Rx Complete Callback */
2182 HAL_USART_TxRxCpltCallback(husart);
2183 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2184 }
2185 }
2186 /* DMA circular mode */
2187 else
2188 {
2189 if (husart->State == HAL_USART_STATE_BUSY_RX)
2190 {
2191 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2192 /* Call registered Rx Complete Callback */
2193 husart->RxCpltCallback(husart);
2194 #else
2195 /* Call legacy weak Rx Complete Callback */
2196 HAL_USART_RxCpltCallback(husart);
2197 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2198 }
2199 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2200 else
2201 {
2202 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2203 /* Call registered Tx Rx Complete Callback */
2204 husart->TxRxCpltCallback(husart);
2205 #else
2206 /* Call legacy weak Tx Rx Complete Callback */
2207 HAL_USART_TxRxCpltCallback(husart);
2208 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2209 }
2210 }
2211 }
2212
2213 /**
2214 * @brief DMA USART receive process half complete callback
2215 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2216 * the configuration information for the specified DMA module.
2217 * @retval None
2218 */
USART_DMARxHalfCplt(DMA_HandleTypeDef * hdma)2219 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2220 {
2221 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2222
2223 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2224 /* Call registered Rx Half Complete Callback */
2225 husart->RxHalfCpltCallback(husart);
2226 #else
2227 /* Call legacy weak Rx Half Complete Callback */
2228 HAL_USART_RxHalfCpltCallback(husart);
2229 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2230 }
2231
2232 /**
2233 * @brief DMA USART communication error callback.
2234 * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
2235 * the configuration information for the specified DMA module.
2236 * @retval None
2237 */
USART_DMAError(DMA_HandleTypeDef * hdma)2238 static void USART_DMAError(DMA_HandleTypeDef *hdma)
2239 {
2240 uint32_t dmarequest = 0x00U;
2241 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2242 husart->RxXferCount = 0x00U;
2243 husart->TxXferCount = 0x00U;
2244
2245 /* Stop USART DMA Tx request if ongoing */
2246 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
2247 if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
2248 {
2249 USART_EndTxTransfer(husart);
2250 }
2251
2252 /* Stop USART DMA Rx request if ongoing */
2253 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
2254 if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
2255 {
2256 USART_EndRxTransfer(husart);
2257 }
2258
2259 husart->ErrorCode |= HAL_USART_ERROR_DMA;
2260 husart->State = HAL_USART_STATE_READY;
2261
2262 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2263 /* Call registered Error Callback */
2264 husart->ErrorCallback(husart);
2265 #else
2266 /* Call legacy weak Error Callback */
2267 HAL_USART_ErrorCallback(husart);
2268 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2269 }
2270
2271 /**
2272 * @brief This function handles USART Communication Timeout.
2273 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2274 * the configuration information for the specified USART module.
2275 * @param Flag specifies the USART flag to check.
2276 * @param Status The new Flag status (SET or RESET).
2277 * @param Tickstart Tick start value.
2278 * @param Timeout Timeout duration.
2279 * @retval HAL status
2280 */
USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef * husart,uint32_t Flag,FlagStatus Status,uint32_t Tickstart,uint32_t Timeout)2281 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2282 {
2283 /* Wait until flag is set */
2284 while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
2285 {
2286 /* Check for the Timeout */
2287 if (Timeout != HAL_MAX_DELAY)
2288 {
2289 if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
2290 {
2291 /* Disable the USART Transmit Complete Interrupt */
2292 CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
2293
2294 /* Disable the USART RXNE Interrupt */
2295 CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
2296
2297 /* Disable the USART Parity Error Interrupt */
2298 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2299
2300 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2301 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2302
2303 husart->State = HAL_USART_STATE_READY;
2304
2305 /* Process Unlocked */
2306 __HAL_UNLOCK(husart);
2307
2308 return HAL_TIMEOUT;
2309 }
2310 }
2311 }
2312 return HAL_OK;
2313 }
2314
2315 /**
2316 * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
2317 * @param husart USART handle.
2318 * @retval None
2319 */
USART_EndTxTransfer(USART_HandleTypeDef * husart)2320 static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
2321 {
2322 /* Disable TXEIE and TCIE interrupts */
2323 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2324
2325 /* At end of Tx process, restore husart->State to Ready */
2326 husart->State = HAL_USART_STATE_READY;
2327 }
2328
2329 /**
2330 * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
2331 * @param husart USART handle.
2332 * @retval None
2333 */
USART_EndRxTransfer(USART_HandleTypeDef * husart)2334 static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
2335 {
2336 /* Disable RXNE, PE and ERR interrupts */
2337 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2338 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2339
2340 /* At end of Rx process, restore husart->State to Ready */
2341 husart->State = HAL_USART_STATE_READY;
2342 }
2343
2344 /**
2345 * @brief DMA USART communication abort callback, when initiated by HAL services on Error
2346 * (To be called at end of DMA Abort procedure following error occurrence).
2347 * @param hdma DMA handle.
2348 * @retval None
2349 */
USART_DMAAbortOnError(DMA_HandleTypeDef * hdma)2350 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2351 {
2352 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2353 husart->RxXferCount = 0x00U;
2354 husart->TxXferCount = 0x00U;
2355
2356 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2357 /* Call registered Error Callback */
2358 husart->ErrorCallback(husart);
2359 #else
2360 /* Call legacy weak Error Callback */
2361 HAL_USART_ErrorCallback(husart);
2362 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2363 }
2364
2365 /**
2366 * @brief DMA USART Tx communication abort callback, when initiated by user
2367 * (To be called at end of DMA Tx Abort procedure following user abort request).
2368 * @note When this callback is executed, User Abort complete call back is called only if no
2369 * Abort still ongoing for Rx DMA Handle.
2370 * @param hdma DMA handle.
2371 * @retval None
2372 */
USART_DMATxAbortCallback(DMA_HandleTypeDef * hdma)2373 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2374 {
2375 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2376
2377 husart->hdmatx->XferAbortCallback = NULL;
2378
2379 /* Check if an Abort process is still ongoing */
2380 if (husart->hdmarx != NULL)
2381 {
2382 if (husart->hdmarx->XferAbortCallback != NULL)
2383 {
2384 return;
2385 }
2386 }
2387
2388 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2389 husart->TxXferCount = 0x00U;
2390 husart->RxXferCount = 0x00U;
2391
2392 /* Reset errorCode */
2393 husart->ErrorCode = HAL_USART_ERROR_NONE;
2394
2395 /* Restore husart->State to Ready */
2396 husart->State = HAL_USART_STATE_READY;
2397
2398 /* Call user Abort complete callback */
2399 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2400 /* Call registered Abort Complete Callback */
2401 husart->AbortCpltCallback(husart);
2402 #else
2403 /* Call legacy weak Abort Complete Callback */
2404 HAL_USART_AbortCpltCallback(husart);
2405 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2406 }
2407
2408 /**
2409 * @brief DMA USART Rx communication abort callback, when initiated by user
2410 * (To be called at end of DMA Rx Abort procedure following user abort request).
2411 * @note When this callback is executed, User Abort complete call back is called only if no
2412 * Abort still ongoing for Tx DMA Handle.
2413 * @param hdma DMA handle.
2414 * @retval None
2415 */
USART_DMARxAbortCallback(DMA_HandleTypeDef * hdma)2416 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2417 {
2418 USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
2419
2420 husart->hdmarx->XferAbortCallback = NULL;
2421
2422 /* Check if an Abort process is still ongoing */
2423 if (husart->hdmatx != NULL)
2424 {
2425 if (husart->hdmatx->XferAbortCallback != NULL)
2426 {
2427 return;
2428 }
2429 }
2430
2431 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2432 husart->TxXferCount = 0x00U;
2433 husart->RxXferCount = 0x00U;
2434
2435 /* Reset errorCode */
2436 husart->ErrorCode = HAL_USART_ERROR_NONE;
2437
2438 /* Restore husart->State to Ready */
2439 husart->State = HAL_USART_STATE_READY;
2440
2441 /* Call user Abort complete callback */
2442 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2443 /* Call registered Abort Complete Callback */
2444 husart->AbortCpltCallback(husart);
2445 #else
2446 /* Call legacy weak Abort Complete Callback */
2447 HAL_USART_AbortCpltCallback(husart);
2448 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2449 }
2450
2451 /**
2452 * @brief Simplex Send an amount of data in non-blocking mode.
2453 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2454 * the configuration information for the specified USART module.
2455 * @retval HAL status
2456 * @note The USART errors are not managed to avoid the overrun error.
2457 */
USART_Transmit_IT(USART_HandleTypeDef * husart)2458 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
2459 {
2460 uint16_t *tmp;
2461
2462 if (husart->State == HAL_USART_STATE_BUSY_TX)
2463 {
2464 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
2465 {
2466 tmp = (uint16_t *) husart->pTxBuffPtr;
2467 husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
2468 if (husart->Init.Parity == USART_PARITY_NONE)
2469 {
2470 husart->pTxBuffPtr += 2U;
2471 }
2472 else
2473 {
2474 husart->pTxBuffPtr += 1U;
2475 }
2476 }
2477 else
2478 {
2479 husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
2480 }
2481
2482 if (--husart->TxXferCount == 0U)
2483 {
2484 /* Disable the USART Transmit data register empty Interrupt */
2485 CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
2486
2487 /* Enable the USART Transmit Complete Interrupt */
2488 SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
2489 }
2490 return HAL_OK;
2491 }
2492 else
2493 {
2494 return HAL_BUSY;
2495 }
2496 }
2497
2498 /**
2499 * @brief Wraps up transmission in non blocking mode.
2500 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2501 * the configuration information for the specified USART module.
2502 * @retval HAL status
2503 */
USART_EndTransmit_IT(USART_HandleTypeDef * husart)2504 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
2505 {
2506 /* Disable the USART Transmit Complete Interrupt */
2507 CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE);
2508
2509 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2510 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2511
2512 husart->State = HAL_USART_STATE_READY;
2513
2514 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2515 /* Call registered Tx Complete Callback */
2516 husart->TxCpltCallback(husart);
2517 #else
2518 /* Call legacy weak Tx Complete Callback */
2519 HAL_USART_TxCpltCallback(husart);
2520 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2521
2522 return HAL_OK;
2523 }
2524
2525 /**
2526 * @brief Simplex Receive an amount of data in non-blocking mode.
2527 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2528 * the configuration information for the specified USART module.
2529 * @retval HAL status
2530 */
USART_Receive_IT(USART_HandleTypeDef * husart)2531 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
2532 {
2533 uint16_t *tmp;
2534 if (husart->State == HAL_USART_STATE_BUSY_RX)
2535 {
2536 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
2537 {
2538 tmp = (uint16_t *) husart->pRxBuffPtr;
2539 if (husart->Init.Parity == USART_PARITY_NONE)
2540 {
2541 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
2542 husart->pRxBuffPtr += 2U;
2543 }
2544 else
2545 {
2546 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
2547 husart->pRxBuffPtr += 1U;
2548 }
2549 if (--husart->RxXferCount != 0x00U)
2550 {
2551 /* Send dummy byte in order to generate the clock for the slave to send the next data */
2552 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
2553 }
2554 }
2555 else
2556 {
2557 if (husart->Init.Parity == USART_PARITY_NONE)
2558 {
2559 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
2560 }
2561 else
2562 {
2563 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
2564 }
2565
2566 if (--husart->RxXferCount != 0x00U)
2567 {
2568 /* Send dummy byte in order to generate the clock for the slave to send the next data */
2569 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
2570 }
2571 }
2572
2573 if (husart->RxXferCount == 0U)
2574 {
2575 /* Disable the USART RXNE Interrupt */
2576 CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
2577
2578 /* Disable the USART Parity Error Interrupt */
2579 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2580
2581 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2582 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2583
2584 husart->State = HAL_USART_STATE_READY;
2585 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2586 /* Call registered Rx Complete Callback */
2587 husart->RxCpltCallback(husart);
2588 #else
2589 /* Call legacy weak Rx Complete Callback */
2590 HAL_USART_RxCpltCallback(husart);
2591 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2592
2593 return HAL_OK;
2594 }
2595 return HAL_OK;
2596 }
2597 else
2598 {
2599 return HAL_BUSY;
2600 }
2601 }
2602
2603 /**
2604 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
2605 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2606 * the configuration information for the specified USART module.
2607 * @retval HAL status
2608 */
USART_TransmitReceive_IT(USART_HandleTypeDef * husart)2609 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
2610 {
2611 uint16_t *tmp;
2612
2613 if (husart->State == HAL_USART_STATE_BUSY_TX_RX)
2614 {
2615 if (husart->TxXferCount != 0x00U)
2616 {
2617 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
2618 {
2619 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
2620 {
2621 tmp = (uint16_t *) husart->pTxBuffPtr;
2622 husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
2623 if (husart->Init.Parity == USART_PARITY_NONE)
2624 {
2625 husart->pTxBuffPtr += 2U;
2626 }
2627 else
2628 {
2629 husart->pTxBuffPtr += 1U;
2630 }
2631 }
2632 else
2633 {
2634 husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
2635 }
2636 husart->TxXferCount--;
2637
2638 /* Check the latest data transmitted */
2639 if (husart->TxXferCount == 0U)
2640 {
2641 CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
2642 }
2643 }
2644 }
2645
2646 if (husart->RxXferCount != 0x00U)
2647 {
2648 if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
2649 {
2650 if (husart->Init.WordLength == USART_WORDLENGTH_9B)
2651 {
2652 tmp = (uint16_t *) husart->pRxBuffPtr;
2653 if (husart->Init.Parity == USART_PARITY_NONE)
2654 {
2655 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
2656 husart->pRxBuffPtr += 2U;
2657 }
2658 else
2659 {
2660 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
2661 husart->pRxBuffPtr += 1U;
2662 }
2663 }
2664 else
2665 {
2666 if (husart->Init.Parity == USART_PARITY_NONE)
2667 {
2668 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
2669 }
2670 else
2671 {
2672 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
2673 }
2674 }
2675 husart->RxXferCount--;
2676 }
2677 }
2678
2679 /* Check the latest data received */
2680 if (husart->RxXferCount == 0U)
2681 {
2682 /* Disable the USART RXNE Interrupt */
2683 CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
2684
2685 /* Disable the USART Parity Error Interrupt */
2686 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2687
2688 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2689 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2690
2691 husart->State = HAL_USART_STATE_READY;
2692
2693 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2694 /* Call registered Tx Rx Complete Callback */
2695 husart->TxRxCpltCallback(husart);
2696 #else
2697 /* Call legacy weak Tx Rx Complete Callback */
2698 HAL_USART_TxRxCpltCallback(husart);
2699 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2700
2701 return HAL_OK;
2702 }
2703
2704 return HAL_OK;
2705 }
2706 else
2707 {
2708 return HAL_BUSY;
2709 }
2710 }
2711
2712 /**
2713 * @brief Configures the USART peripheral.
2714 * @param husart Pointer to a USART_HandleTypeDef structure that contains
2715 * the configuration information for the specified USART module.
2716 * @retval None
2717 */
USART_SetConfig(USART_HandleTypeDef * husart)2718 static void USART_SetConfig(USART_HandleTypeDef *husart)
2719 {
2720 uint32_t tmpreg = 0x00U;
2721 uint32_t pclk;
2722
2723 /* Check the parameters */
2724 assert_param(IS_USART_INSTANCE(husart->Instance));
2725 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
2726 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
2727 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
2728 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
2729 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
2730 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
2731 assert_param(IS_USART_PARITY(husart->Init.Parity));
2732 assert_param(IS_USART_MODE(husart->Init.Mode));
2733
2734 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
2735 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
2736 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2737
2738 /*---------------------------- USART CR2 Configuration ---------------------*/
2739 tmpreg = husart->Instance->CR2;
2740 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
2741 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP));
2742 /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
2743 /* Set CPOL bit according to husart->Init.CLKPolarity value */
2744 /* Set CPHA bit according to husart->Init.CLKPhase value */
2745 /* Set LBCL bit according to husart->Init.CLKLastBit value */
2746 /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
2747 tmpreg |= (uint32_t)(USART_CLOCK_ENABLE | husart->Init.CLKPolarity |
2748 husart->Init.CLKPhase | husart->Init.CLKLastBit | husart->Init.StopBits);
2749 /* Write to USART CR2 */
2750 WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg);
2751
2752 /*-------------------------- USART CR1 Configuration -----------------------*/
2753 tmpreg = husart->Instance->CR1;
2754
2755 /* Clear M, PCE, PS, TE, RE and OVER8 bits */
2756 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
2757 USART_CR1_RE | USART_CR1_OVER8));
2758
2759 /* Configure the USART Word Length, Parity and mode:
2760 Set the M bits according to husart->Init.WordLength value
2761 Set PCE and PS bits according to husart->Init.Parity value
2762 Set TE and RE bits according to husart->Init.Mode value
2763 Force OVER8 bit to 1 in order to reach the max USART frequencies */
2764 tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
2765
2766 /* Write to USART CR1 */
2767 WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg);
2768
2769 /*-------------------------- USART CR3 Configuration -----------------------*/
2770 /* Clear CTSE and RTSE bits */
2771 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
2772
2773 /*-------------------------- USART BRR Configuration -----------------------*/
2774 #if defined(USART6) && defined(UART9) && defined(UART10)
2775 if ((husart->Instance == USART1) || (husart->Instance == USART6) || (husart->Instance == UART9) || (husart->Instance == UART10))
2776 {
2777 pclk = HAL_RCC_GetPCLK2Freq();
2778 husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
2779 }
2780 #elif defined(USART6)
2781 if((husart->Instance == USART1) || (husart->Instance == USART6))
2782 {
2783 pclk = HAL_RCC_GetPCLK2Freq();
2784 husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
2785 }
2786 #else
2787 if(husart->Instance == USART1)
2788 {
2789 pclk = HAL_RCC_GetPCLK2Freq();
2790 husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
2791 }
2792 #endif /* USART6 || UART9 || UART10 */
2793 else
2794 {
2795 pclk = HAL_RCC_GetPCLK1Freq();
2796 husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
2797 }
2798 }
2799
2800 /**
2801 * @}
2802 */
2803
2804 /**
2805 * @}
2806 */
2807
2808 #endif /* HAL_USART_MODULE_ENABLED */
2809 /**
2810 * @}
2811 */
2812
2813 /**
2814 * @}
2815 */
2816
2817 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2818