Subversion Repositories DashDisplay

Rev

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

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