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