Subversion Repositories LedShow

Rev

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

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