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