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