Subversion Repositories LedShow

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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