Subversion Repositories canSerial

Rev

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

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