Subversion Repositories LedShow

Rev

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

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