Subversion Repositories AFRtranscoder

Rev

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