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