Subversion Repositories dashGPS

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

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