Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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