38
|
1 /**
|
|
2 ******************************************************************************
|
|
3 * @file stm32f4xx_hal_usart.c
|
|
4 * @author MCD Application Team
|
|
5 * @version V1.2.0
|
|
6 * @date 26-December-2014
|
|
7 * @brief USART HAL module driver.
|
|
8 * This file provides firmware functions to manage the following
|
|
9 * functionalities of the Universal Synchronous Asynchronous Receiver Transmitter (USART) peripheral:
|
|
10 * + Initialization and de-initialization functions
|
|
11 * + IO operation functions
|
|
12 * + Peripheral Control functions
|
|
13 @verbatim
|
|
14 ==============================================================================
|
|
15 ##### How to use this driver #####
|
|
16 ==============================================================================
|
|
17 [..]
|
|
18 The USART HAL driver can be used as follows:
|
|
19
|
|
20 (#) Declare a USART_HandleTypeDef handle structure.
|
|
21 (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit () API:
|
|
22 (##) Enable the USARTx interface clock.
|
|
23 (##) USART pins configuration:
|
|
24 (+++) Enable the clock for the USART GPIOs.
|
|
25 (+++) Configure these USART pins as alternate function pull-up.
|
|
26 (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
|
|
27 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
|
|
28 (+++) Configure the USARTx interrupt priority.
|
|
29 (+++) Enable the NVIC USART IRQ handle.
|
|
30 (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
|
|
31 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
|
|
32 (+++) Declare a DMA handle structure for the Tx/Rx stream.
|
|
33 (+++) Enable the DMAx interface clock.
|
|
34 (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
|
|
35 (+++) Configure the DMA Tx/Rx Stream.
|
|
36 (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
|
|
37 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
|
|
38
|
|
39 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
|
|
40 flow control and Mode(Receiver/Transmitter) in the husart Init structure.
|
|
41
|
|
42 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
|
|
43 (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
|
|
44 by calling the customized HAL_USART_MspInit(&husart) API.
|
|
45
|
|
46 -@@- The specific USART interrupts (Transmission complete interrupt,
|
|
47 RXNE interrupt and Error Interrupts) will be managed using the macros
|
|
48 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
|
|
49
|
|
50 (#) Three operation modes are available within this driver :
|
|
51
|
|
52 *** Polling mode IO operation ***
|
|
53 =================================
|
|
54 [..]
|
|
55 (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
|
|
56 (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
|
|
57
|
|
58 *** Interrupt mode IO operation ***
|
|
59 ===================================
|
|
60 [..]
|
|
61 (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
|
|
62 (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
|
|
63 add his own code by customization of function pointer HAL_USART_TxCpltCallback
|
|
64 (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
|
|
65 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
|
|
66 add his own code by customization of function pointer HAL_UART_RxCpltCallback
|
|
67 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
|
|
68 add his own code by customization of function pointer HAL_USART_ErrorCallback
|
|
69
|
|
70 *** DMA mode IO operation ***
|
|
71 ==============================
|
|
72 [..]
|
|
73 (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
|
|
74 (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
|
|
75 add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
|
|
76 (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
|
|
77 add his own code by customization of function pointer HAL_USART_TxCpltCallback
|
|
78 (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
|
|
79 (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
|
|
80 add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
|
|
81 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
|
|
82 add his own code by customization of function pointer HAL_USART_RxCpltCallback
|
|
83 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
|
|
84 add his own code by customization of function pointer HAL_USART_ErrorCallback
|
|
85 (+) Pause the DMA Transfer using HAL_USART_DMAPause()
|
|
86 (+) Resume the DMA Transfer using HAL_USART_DMAResume()
|
|
87 (+) Stop the DMA Transfer using HAL_USART_DMAStop()
|
|
88
|
|
89 *** USART HAL driver macros list ***
|
|
90 =============================================
|
|
91 [..]
|
|
92 Below the list of most used macros in USART HAL driver.
|
|
93
|
|
94 (+) __HAL_USART_ENABLE: Enable the USART peripheral
|
|
95 (+) __HAL_USART_DISABLE: Disable the USART peripheral
|
|
96 (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
|
|
97 (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
|
|
98 (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
|
|
99 (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
|
|
100
|
|
101 [..]
|
|
102 (@) You can refer to the USART HAL driver header file for more useful macros
|
|
103
|
|
104 @endverbatim
|
|
105 ******************************************************************************
|
|
106 * @attention
|
|
107 *
|
|
108 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
|
|
109 *
|
|
110 * Redistribution and use in source and binary forms, with or without modification,
|
|
111 * are permitted provided that the following conditions are met:
|
|
112 * 1. Redistributions of source code must retain the above copyright notice,
|
|
113 * this list of conditions and the following disclaimer.
|
|
114 * 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
115 * this list of conditions and the following disclaimer in the documentation
|
|
116 * and/or other materials provided with the distribution.
|
|
117 * 3. Neither the name of STMicroelectronics nor the names of its contributors
|
|
118 * may be used to endorse or promote products derived from this software
|
|
119 * without specific prior written permission.
|
|
120 *
|
|
121 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
122 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
123 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
124 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
125 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
126 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
127 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
128 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
129 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
130 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
131 *
|
|
132 ******************************************************************************
|
|
133 */
|
|
134
|
|
135 /* Includes ------------------------------------------------------------------*/
|
|
136 #include "stm32f4xx_hal.h"
|
|
137
|
|
138 /** @addtogroup STM32F4xx_HAL_Driver
|
|
139 * @{
|
|
140 */
|
|
141
|
|
142 /** @defgroup USART USART
|
|
143 * @brief HAL USART Synchronous module driver
|
|
144 * @{
|
|
145 */
|
|
146 #ifdef HAL_USART_MODULE_ENABLED
|
|
147 /* Private typedef -----------------------------------------------------------*/
|
|
148 /* Private define ------------------------------------------------------------*/
|
|
149 /** @addtogroup USART_Private_Constants
|
|
150 * @{
|
|
151 */
|
|
152 #define DUMMY_DATA 0xFFFF
|
|
153 #define USART_TIMEOUT_VALUE 22000
|
|
154 /**
|
|
155 * @}
|
|
156 */
|
|
157 /* Private macro -------------------------------------------------------------*/
|
|
158 /* Private variables ---------------------------------------------------------*/
|
|
159 /* Private function prototypes -----------------------------------------------*/
|
|
160 /* Private functions ---------------------------------------------------------*/
|
|
161 /** @addtogroup USART_Private_Functions
|
|
162 * @{
|
|
163 */
|
|
164 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
|
|
165 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
|
|
166 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
|
|
167 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
|
|
168 static void USART_SetConfig (USART_HandleTypeDef *husart);
|
|
169 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
|
|
170 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
|
|
171 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
|
|
172 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
|
|
173 static void USART_DMAError(DMA_HandleTypeDef *hdma);
|
|
174 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
|
|
175 /**
|
|
176 * @}
|
|
177 */
|
|
178
|
|
179 /* Exported functions --------------------------------------------------------*/
|
|
180 /** @defgroup USART_Exported_Functions USART Exported Functions
|
|
181 * @{
|
|
182 */
|
|
183
|
|
184 /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
|
|
185 * @brief Initialization and Configuration functions
|
|
186 *
|
|
187 @verbatim
|
|
188 ==============================================================================
|
|
189 ##### Initialization and Configuration functions #####
|
|
190 ==============================================================================
|
|
191 [..]
|
|
192 This subsection provides a set of functions allowing to initialize the USART
|
|
193 in asynchronous and in synchronous modes.
|
|
194 (+) For the asynchronous mode only these parameters can be configured:
|
|
195 (++) Baud Rate
|
|
196 (++) Word Length
|
|
197 (++) Stop Bit
|
|
198 (++) Parity: If the parity is enabled, then the MSB bit of the data written
|
|
199 in the data register is transmitted but is changed by the parity bit.
|
|
200 Depending on the frame length defined by the M bit (8-bits or 9-bits),
|
|
201 please refer to Reference manual for possible USART frame formats.
|
|
202 (++) USART polarity
|
|
203 (++) USART phase
|
|
204 (++) USART LastBit
|
|
205 (++) Receiver/transmitter modes
|
|
206
|
|
207 [..]
|
|
208 The HAL_USART_Init() function follows the USART synchronous configuration
|
|
209 procedure (details for the procedure are available in reference manual (RM0329)).
|
|
210
|
|
211 @endverbatim
|
|
212 * @{
|
|
213 */
|
|
214
|
|
215 /**
|
|
216 * @brief Initializes the USART mode according to the specified
|
|
217 * parameters in the USART_InitTypeDef and create the associated handle.
|
|
218 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
219 * the configuration information for the specified USART module.
|
|
220 * @retval HAL status
|
|
221 */
|
|
222 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
|
|
223 {
|
|
224 /* Check the USART handle allocation */
|
|
225 if(husart == NULL)
|
|
226 {
|
|
227 return HAL_ERROR;
|
|
228 }
|
|
229
|
|
230 /* Check the parameters */
|
|
231 assert_param(IS_USART_INSTANCE(husart->Instance));
|
|
232
|
|
233 if(husart->State == HAL_USART_STATE_RESET)
|
|
234 {
|
|
235 /* Init the low level hardware */
|
|
236 HAL_USART_MspInit(husart);
|
|
237 }
|
|
238
|
|
239 husart->State = HAL_USART_STATE_BUSY;
|
|
240
|
|
241 /* Set the USART Communication parameters */
|
|
242 USART_SetConfig(husart);
|
|
243
|
|
244 /* In USART mode, the following bits must be kept cleared:
|
|
245 - LINEN bit in the USART_CR2 register
|
|
246 - HDSEL, SCEN and IREN bits in the USART_CR3 register */
|
|
247 husart->Instance->CR2 &= ~USART_CR2_LINEN;
|
|
248 husart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN | USART_CR3_HDSEL);
|
|
249
|
|
250 /* Enable the Peripheral */
|
|
251 __HAL_USART_ENABLE(husart);
|
|
252
|
|
253 /* Initialize the USART state */
|
|
254 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
255 husart->State= HAL_USART_STATE_READY;
|
|
256
|
|
257 return HAL_OK;
|
|
258 }
|
|
259
|
|
260 /**
|
|
261 * @brief DeInitializes the USART peripheral.
|
|
262 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
263 * the configuration information for the specified USART module.
|
|
264 * @retval HAL status
|
|
265 */
|
|
266 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
|
|
267 {
|
|
268 /* Check the USART handle allocation */
|
|
269 if(husart == NULL)
|
|
270 {
|
|
271 return HAL_ERROR;
|
|
272 }
|
|
273
|
|
274 /* Check the parameters */
|
|
275 assert_param(IS_USART_INSTANCE(husart->Instance));
|
|
276
|
|
277 husart->State = HAL_USART_STATE_BUSY;
|
|
278
|
|
279 /* Disable the Peripheral */
|
|
280 __HAL_USART_DISABLE(husart);
|
|
281
|
|
282 /* DeInit the low level hardware */
|
|
283 HAL_USART_MspDeInit(husart);
|
|
284
|
|
285 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
286 husart->State = HAL_USART_STATE_RESET;
|
|
287
|
|
288 /* Release Lock */
|
|
289 __HAL_UNLOCK(husart);
|
|
290
|
|
291 return HAL_OK;
|
|
292 }
|
|
293
|
|
294 /**
|
|
295 * @brief USART MSP Init.
|
|
296 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
297 * the configuration information for the specified USART module.
|
|
298 * @retval None
|
|
299 */
|
|
300 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
|
|
301 {
|
|
302 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
303 the HAL_USART_MspInit could be implemented in the user file
|
|
304 */
|
|
305 }
|
|
306
|
|
307 /**
|
|
308 * @brief USART MSP DeInit.
|
|
309 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
310 * the configuration information for the specified USART module.
|
|
311 * @retval None
|
|
312 */
|
|
313 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
|
|
314 {
|
|
315 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
316 the HAL_USART_MspDeInit could be implemented in the user file
|
|
317 */
|
|
318 }
|
|
319
|
|
320 /**
|
|
321 * @}
|
|
322 */
|
|
323
|
|
324 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
|
|
325 * @brief USART Transmit and Receive functions
|
|
326 *
|
|
327 @verbatim
|
|
328 ==============================================================================
|
|
329 ##### IO operation functions #####
|
|
330 ==============================================================================
|
|
331 [..]
|
|
332 This subsection provides a set of functions allowing to manage the USART synchronous
|
|
333 data transfers.
|
|
334
|
|
335 [..]
|
|
336 The USART supports master mode only: it cannot receive or send data related to an input
|
|
337 clock (SCLK is always an output).
|
|
338
|
|
339 (#) There are two modes of transfer:
|
|
340 (++) Blocking mode: The communication is performed in polling mode.
|
|
341 The HAL status of all data processing is returned by the same function
|
|
342 after finishing transfer.
|
|
343 (++) No-Blocking mode: The communication is performed using Interrupts
|
|
344 or DMA, These API's return the HAL status.
|
|
345 The end of the data processing will be indicated through the
|
|
346 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
|
|
347 using DMA mode.
|
|
348 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
|
|
349 user callbacks
|
|
350 will be executed respectively at the end of the transmit or Receive process
|
|
351 The HAL_USART_ErrorCallback() user callback will be executed when a communication
|
|
352 error is detected
|
|
353
|
|
354 (#) Blocking mode APIs are :
|
|
355 (++) HAL_USART_Transmit() in simplex mode
|
|
356 (++) HAL_USART_Receive() in full duplex receive only
|
|
357 (++) HAL_USART_TransmitReceive() in full duplex mode
|
|
358
|
|
359 (#) Non Blocking mode APIs with Interrupt are :
|
|
360 (++) HAL_USART_Transmit_IT()in simplex mode
|
|
361 (++) HAL_USART_Receive_IT() in full duplex receive only
|
|
362 (++) HAL_USART_TransmitReceive_IT() in full duplex mode
|
|
363 (++) HAL_USART_IRQHandler()
|
|
364
|
|
365 (#) Non Blocking mode functions with DMA are :
|
|
366 (++) HAL_USART_Transmit_DMA()in simplex mode
|
|
367 (++) HAL_USART_Receive_DMA() in full duplex receive only
|
|
368 (++) HAL_USART_TransmitReceie_DMA() in full duplex mode
|
|
369 (++) HAL_USART_DMAPause()
|
|
370 (++) HAL_USART_DMAResume()
|
|
371 (++) HAL_USART_DMAStop()
|
|
372
|
|
373 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
|
|
374 (++) HAL_USART_TxHalfCpltCallback()
|
|
375 (++) HAL_USART_TxCpltCallback()
|
|
376 (++) HAL_USART_RxHalfCpltCallback()
|
|
377 (++) HAL_USART_RxCpltCallback()
|
|
378 (++) HAL_USART_ErrorCallback()
|
|
379 (++) HAL_USART_TxRxCpltCallback()
|
|
380
|
|
381 @endverbatim
|
|
382 * @{
|
|
383 */
|
|
384
|
|
385 /**
|
|
386 * @brief Simplex Send an amount of data in blocking mode.
|
|
387 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
388 * the configuration information for the specified USART module.
|
|
389 * @param pTxData: Pointer to data buffer
|
|
390 * @param Size: Amount of data to be sent
|
|
391 * @param Timeout: Timeout duration
|
|
392 * @retval HAL status
|
|
393 */
|
|
394 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
|
|
395 {
|
|
396 uint16_t* tmp;
|
|
397
|
|
398 if(husart->State == HAL_USART_STATE_READY)
|
|
399 {
|
|
400 if((pTxData == NULL) || (Size == 0))
|
|
401 {
|
|
402 return HAL_ERROR;
|
|
403 }
|
|
404
|
|
405 /* Process Locked */
|
|
406 __HAL_LOCK(husart);
|
|
407
|
|
408 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
409 husart->State = HAL_USART_STATE_BUSY_TX;
|
|
410
|
|
411 husart->TxXferSize = Size;
|
|
412 husart->TxXferCount = Size;
|
|
413 while(husart->TxXferCount > 0)
|
|
414 {
|
|
415 husart->TxXferCount--;
|
|
416 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
417 {
|
|
418 /* Wait for TC flag in order to write data in DR */
|
|
419 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
420 {
|
|
421 return HAL_TIMEOUT;
|
|
422 }
|
|
423 tmp = (uint16_t*) pTxData;
|
|
424 husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
|
|
425 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
426 {
|
|
427 pTxData += 2;
|
|
428 }
|
|
429 else
|
|
430 {
|
|
431 pTxData += 1;
|
|
432 }
|
|
433 }
|
|
434 else
|
|
435 {
|
|
436 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
437 {
|
|
438 return HAL_TIMEOUT;
|
|
439 }
|
|
440 husart->Instance->DR = (*pTxData++ & (uint8_t)0xFF);
|
|
441 }
|
|
442 }
|
|
443
|
|
444 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
|
|
445 {
|
|
446 return HAL_TIMEOUT;
|
|
447 }
|
|
448
|
|
449 husart->State = HAL_USART_STATE_READY;
|
|
450
|
|
451 /* Process Unlocked */
|
|
452 __HAL_UNLOCK(husart);
|
|
453
|
|
454 return HAL_OK;
|
|
455 }
|
|
456 else
|
|
457 {
|
|
458 return HAL_BUSY;
|
|
459 }
|
|
460 }
|
|
461
|
|
462 /**
|
|
463 * @brief Full-Duplex Receive an amount of data in blocking mode.
|
|
464 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
465 * the configuration information for the specified USART module.
|
|
466 * @param pRxData: Pointer to data buffer
|
|
467 * @param Size: Amount of data to be received
|
|
468 * @param Timeout: Timeout duration
|
|
469 * @retval HAL status
|
|
470 */
|
|
471 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
|
|
472 {
|
|
473 uint16_t* tmp;
|
|
474
|
|
475 if(husart->State == HAL_USART_STATE_READY)
|
|
476 {
|
|
477 if((pRxData == NULL) || (Size == 0))
|
|
478 {
|
|
479 return HAL_ERROR;
|
|
480 }
|
|
481 /* Process Locked */
|
|
482 __HAL_LOCK(husart);
|
|
483
|
|
484 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
485 husart->State = HAL_USART_STATE_BUSY_RX;
|
|
486
|
|
487 husart->RxXferSize = Size;
|
|
488 husart->RxXferCount = Size;
|
|
489 /* Check the remain data to be received */
|
|
490 while(husart->RxXferCount > 0)
|
|
491 {
|
|
492 husart->RxXferCount--;
|
|
493 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
494 {
|
|
495 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
|
|
496 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
497 {
|
|
498 return HAL_TIMEOUT;
|
|
499 }
|
|
500 /* Send dummy byte in order to generate clock */
|
|
501 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
|
|
502
|
|
503 /* Wait for RXNE Flag */
|
|
504 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
505 {
|
|
506 return HAL_TIMEOUT;
|
|
507 }
|
|
508 tmp = (uint16_t*) pRxData ;
|
|
509 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
510 {
|
|
511 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
|
|
512 pRxData +=2;
|
|
513 }
|
|
514 else
|
|
515 {
|
|
516 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
|
|
517 pRxData +=1;
|
|
518 }
|
|
519 }
|
|
520 else
|
|
521 {
|
|
522 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
|
|
523 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
524 {
|
|
525 return HAL_TIMEOUT;
|
|
526 }
|
|
527
|
|
528 /* Send Dummy Byte in order to generate clock */
|
|
529 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
|
|
530
|
|
531 /* Wait until RXNE flag is set to receive the byte */
|
|
532 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
533 {
|
|
534 return HAL_TIMEOUT;
|
|
535 }
|
|
536 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
537 {
|
|
538 /* Receive data */
|
|
539 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
|
|
540 }
|
|
541 else
|
|
542 {
|
|
543 /* Receive data */
|
|
544 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
|
|
545 }
|
|
546
|
|
547 }
|
|
548 }
|
|
549
|
|
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 Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode).
|
|
565 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
566 * the configuration information for the specified USART module.
|
|
567 * @param pTxData: Pointer to data transmitted buffer
|
|
568 * @param pRxData: Pointer to data received buffer
|
|
569 * @param Size: Amount of data to be sent
|
|
570 * @param Timeout: Timeout duration
|
|
571 * @retval HAL status
|
|
572 */
|
|
573 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
|
|
574 {
|
|
575 uint16_t* tmp;
|
|
576
|
|
577 if(husart->State == HAL_USART_STATE_READY)
|
|
578 {
|
|
579 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
|
|
580 {
|
|
581 return HAL_ERROR;
|
|
582 }
|
|
583 /* Process Locked */
|
|
584 __HAL_LOCK(husart);
|
|
585
|
|
586 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
587 husart->State = HAL_USART_STATE_BUSY_RX;
|
|
588
|
|
589 husart->RxXferSize = Size;
|
|
590 husart->TxXferSize = Size;
|
|
591 husart->TxXferCount = Size;
|
|
592 husart->RxXferCount = Size;
|
|
593
|
|
594 /* Check the remain data to be received */
|
|
595 while(husart->TxXferCount > 0)
|
|
596 {
|
|
597 husart->TxXferCount--;
|
|
598 husart->RxXferCount--;
|
|
599 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
600 {
|
|
601 /* Wait for TC flag in order to write data in DR */
|
|
602 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
603 {
|
|
604 return HAL_TIMEOUT;
|
|
605 }
|
|
606 tmp = (uint16_t*) pTxData;
|
|
607 husart->Instance->DR = (*tmp & (uint16_t)0x01FF);
|
|
608 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
609 {
|
|
610 pTxData += 2;
|
|
611 }
|
|
612 else
|
|
613 {
|
|
614 pTxData += 1;
|
|
615 }
|
|
616
|
|
617 /* Wait for RXNE Flag */
|
|
618 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
619 {
|
|
620 return HAL_TIMEOUT;
|
|
621 }
|
|
622 tmp = (uint16_t*) pRxData ;
|
|
623 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
624 {
|
|
625 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
|
|
626 pRxData += 2;
|
|
627 }
|
|
628 else
|
|
629 {
|
|
630 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
|
|
631 pRxData += 1;
|
|
632 }
|
|
633 }
|
|
634 else
|
|
635 {
|
|
636 /* Wait for TC flag in order to write data in DR */
|
|
637 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, Timeout) != HAL_OK)
|
|
638 {
|
|
639 return HAL_TIMEOUT;
|
|
640 }
|
|
641 husart->Instance->DR = (*pTxData++ & (uint8_t)0x00FF);
|
|
642
|
|
643 /* Wait for RXNE Flag */
|
|
644 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
|
|
645 {
|
|
646 return HAL_TIMEOUT;
|
|
647 }
|
|
648 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
649 {
|
|
650 /* Receive data */
|
|
651 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
|
|
652 }
|
|
653 else
|
|
654 {
|
|
655 /* Receive data */
|
|
656 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
|
|
657 }
|
|
658 }
|
|
659 }
|
|
660
|
|
661 husart->State = HAL_USART_STATE_READY;
|
|
662
|
|
663 /* Process Unlocked */
|
|
664 __HAL_UNLOCK(husart);
|
|
665
|
|
666 return HAL_OK;
|
|
667 }
|
|
668 else
|
|
669 {
|
|
670 return HAL_BUSY;
|
|
671 }
|
|
672 }
|
|
673
|
|
674 /**
|
|
675 * @brief Simplex Send an amount of data in non-blocking mode.
|
|
676 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
677 * the configuration information for the specified USART module.
|
|
678 * @param pTxData: Pointer to data buffer
|
|
679 * @param Size: Amount of data to be sent
|
|
680 * @retval HAL status
|
|
681 * @note The USART errors are not managed to avoid the overrun error.
|
|
682 */
|
|
683 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
|
|
684 {
|
|
685 if(husart->State == HAL_USART_STATE_READY)
|
|
686 {
|
|
687 if((pTxData == NULL) || (Size == 0))
|
|
688 {
|
|
689 return HAL_ERROR;
|
|
690 }
|
|
691
|
|
692 /* Process Locked */
|
|
693 __HAL_LOCK(husart);
|
|
694
|
|
695 husart->pTxBuffPtr = pTxData;
|
|
696 husart->TxXferSize = Size;
|
|
697 husart->TxXferCount = Size;
|
|
698
|
|
699 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
700 husart->State = HAL_USART_STATE_BUSY_TX;
|
|
701
|
|
702 /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
|
|
703 are not managed by the USART transmit process to avoid the overrun interrupt
|
|
704 when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
|
|
705 to benefit for the frame error and noise interrupts the USART mode should be
|
|
706 configured only for transmit "USART_MODE_TX"
|
|
707 The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
|
|
708 Noise error interrupt */
|
|
709
|
|
710 /* Process Unlocked */
|
|
711 __HAL_UNLOCK(husart);
|
|
712
|
|
713 /* Enable the USART Transmit Data Register Empty Interrupt */
|
|
714 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
|
|
715
|
|
716 return HAL_OK;
|
|
717 }
|
|
718 else
|
|
719 {
|
|
720 return HAL_BUSY;
|
|
721 }
|
|
722 }
|
|
723
|
|
724 /**
|
|
725 * @brief Simplex Receive an amount of data in non-blocking mode.
|
|
726 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
727 * the configuration information for the specified USART module.
|
|
728 * @param pRxData: Pointer to data buffer
|
|
729 * @param Size: Amount of data to be received
|
|
730 * @retval HAL status
|
|
731 */
|
|
732 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
|
|
733 {
|
|
734 if(husart->State == HAL_USART_STATE_READY)
|
|
735 {
|
|
736 if((pRxData == NULL) || (Size == 0))
|
|
737 {
|
|
738 return HAL_ERROR;
|
|
739 }
|
|
740 /* Process Locked */
|
|
741 __HAL_LOCK(husart);
|
|
742
|
|
743 husart->pRxBuffPtr = pRxData;
|
|
744 husart->RxXferSize = Size;
|
|
745 husart->RxXferCount = Size;
|
|
746
|
|
747 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
748 husart->State = HAL_USART_STATE_BUSY_RX;
|
|
749
|
|
750 /* Process Unlocked */
|
|
751 __HAL_UNLOCK(husart);
|
|
752
|
|
753 /* Enable the USART Data Register not empty Interrupt */
|
|
754 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
|
|
755
|
|
756 /* Enable the USART Parity Error Interrupt */
|
|
757 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
|
|
758
|
|
759 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
760 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
|
|
761
|
|
762 /* Send dummy byte in order to generate the clock for the slave to send data */
|
|
763 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
|
|
764
|
|
765 return HAL_OK;
|
|
766 }
|
|
767 else
|
|
768 {
|
|
769 return HAL_BUSY;
|
|
770 }
|
|
771 }
|
|
772
|
|
773 /**
|
|
774 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
|
|
775 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
776 * the configuration information for the specified USART module.
|
|
777 * @param pTxData: Pointer to data transmitted buffer
|
|
778 * @param pRxData: Pointer to data received buffer
|
|
779 * @param Size: Amount of data to be received
|
|
780 * @retval HAL status
|
|
781 */
|
|
782 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
|
|
783 {
|
|
784 if(husart->State == HAL_USART_STATE_READY)
|
|
785 {
|
|
786 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
|
|
787 {
|
|
788 return HAL_ERROR;
|
|
789 }
|
|
790 /* Process Locked */
|
|
791 __HAL_LOCK(husart);
|
|
792
|
|
793 husart->pRxBuffPtr = pRxData;
|
|
794 husart->RxXferSize = Size;
|
|
795 husart->RxXferCount = Size;
|
|
796 husart->pTxBuffPtr = pTxData;
|
|
797 husart->TxXferSize = Size;
|
|
798 husart->TxXferCount = Size;
|
|
799
|
|
800 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
801 husart->State = HAL_USART_STATE_BUSY_TX_RX;
|
|
802
|
|
803 /* Process Unlocked */
|
|
804 __HAL_UNLOCK(husart);
|
|
805
|
|
806 /* Enable the USART Data Register not empty Interrupt */
|
|
807 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
|
|
808
|
|
809 /* Enable the USART Parity Error Interrupt */
|
|
810 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
|
|
811
|
|
812 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
813 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
|
|
814
|
|
815 /* Enable the USART Transmit Data Register Empty Interrupt */
|
|
816 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
|
|
817
|
|
818 return HAL_OK;
|
|
819 }
|
|
820 else
|
|
821 {
|
|
822 return HAL_BUSY;
|
|
823 }
|
|
824 }
|
|
825
|
|
826 /**
|
|
827 * @brief Simplex Send an amount of data in non-blocking mode.
|
|
828 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
829 * the configuration information for the specified USART module.
|
|
830 * @param pTxData: Pointer to data buffer
|
|
831 * @param Size: Amount of data to be sent
|
|
832 * @retval HAL status
|
|
833 */
|
|
834 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
|
|
835 {
|
|
836 uint32_t *tmp;
|
|
837
|
|
838 if(husart->State == HAL_USART_STATE_READY)
|
|
839 {
|
|
840 if((pTxData == NULL) || (Size == 0))
|
|
841 {
|
|
842 return HAL_ERROR;
|
|
843 }
|
|
844 /* Process Locked */
|
|
845 __HAL_LOCK(husart);
|
|
846
|
|
847 husart->pTxBuffPtr = pTxData;
|
|
848 husart->TxXferSize = Size;
|
|
849 husart->TxXferCount = Size;
|
|
850
|
|
851 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
852 husart->State = HAL_USART_STATE_BUSY_TX;
|
|
853
|
|
854 /* Set the USART DMA transfer complete callback */
|
|
855 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
|
|
856
|
|
857 /* Set the USART DMA Half transfer complete callback */
|
|
858 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
|
|
859
|
|
860 /* Set the DMA error callback */
|
|
861 husart->hdmatx->XferErrorCallback = USART_DMAError;
|
|
862
|
|
863 /* Enable the USART transmit DMA Stream */
|
|
864 tmp = (uint32_t*)&pTxData;
|
|
865 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
|
|
866
|
|
867 /* Clear the TC flag in the SR register by writing 0 to it */
|
|
868 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
|
|
869
|
|
870 /* Enable the DMA transfer for transmit request by setting the DMAT bit
|
|
871 in the USART CR3 register */
|
|
872 husart->Instance->CR3 |= USART_CR3_DMAT;
|
|
873
|
|
874 /* Process Unlocked */
|
|
875 __HAL_UNLOCK(husart);
|
|
876
|
|
877 return HAL_OK;
|
|
878 }
|
|
879 else
|
|
880 {
|
|
881 return HAL_BUSY;
|
|
882 }
|
|
883 }
|
|
884
|
|
885 /**
|
|
886 * @brief Full-Duplex Receive an amount of data in non-blocking mode.
|
|
887 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
888 * the configuration information for the specified USART module.
|
|
889 * @param pRxData: Pointer to data buffer
|
|
890 * @param Size: Amount of data to be received
|
|
891 * @retval HAL status
|
|
892 * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave.
|
|
893 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
|
|
894 */
|
|
895 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
|
|
896 {
|
|
897 uint32_t *tmp;
|
|
898
|
|
899 if(husart->State == HAL_USART_STATE_READY)
|
|
900 {
|
|
901 if((pRxData == NULL) || (Size == 0))
|
|
902 {
|
|
903 return HAL_ERROR;
|
|
904 }
|
|
905
|
|
906 /* Process Locked */
|
|
907 __HAL_LOCK(husart);
|
|
908
|
|
909 husart->pRxBuffPtr = pRxData;
|
|
910 husart->RxXferSize = Size;
|
|
911 husart->pTxBuffPtr = pRxData;
|
|
912 husart->TxXferSize = Size;
|
|
913
|
|
914 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
915 husart->State = HAL_USART_STATE_BUSY_RX;
|
|
916
|
|
917 /* Set the USART DMA Rx transfer complete callback */
|
|
918 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
|
|
919
|
|
920 /* Set the USART DMA Half transfer complete callback */
|
|
921 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
|
|
922
|
|
923 /* Set the USART DMA Rx transfer error callback */
|
|
924 husart->hdmarx->XferErrorCallback = USART_DMAError;
|
|
925
|
|
926 /* Enable the USART receive DMA Stream */
|
|
927 tmp = (uint32_t*)&pRxData;
|
|
928 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
|
|
929
|
|
930 /* Enable the USART transmit DMA Stream: the transmit stream is used in order
|
|
931 to generate in the non-blocking mode the clock to the slave device,
|
|
932 this mode isn't a simplex receive mode but a full-duplex receive one */
|
|
933 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
|
|
934
|
|
935 /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer
|
|
936 when using the USART in circular mode */
|
|
937 __HAL_USART_CLEAR_OREFLAG(husart);
|
|
938
|
|
939 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
|
|
940 in the USART CR3 register */
|
|
941 husart->Instance->CR3 |= USART_CR3_DMAR;
|
|
942
|
|
943 /* Enable the DMA transfer for transmit request by setting the DMAT bit
|
|
944 in the USART CR3 register */
|
|
945 husart->Instance->CR3 |= USART_CR3_DMAT;
|
|
946
|
|
947 /* Process Unlocked */
|
|
948 __HAL_UNLOCK(husart);
|
|
949
|
|
950 return HAL_OK;
|
|
951 }
|
|
952 else
|
|
953 {
|
|
954 return HAL_BUSY;
|
|
955 }
|
|
956 }
|
|
957
|
|
958 /**
|
|
959 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
|
|
960 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
961 * the configuration information for the specified USART module.
|
|
962 * @param pTxData: Pointer to data transmitted buffer
|
|
963 * @param pRxData: Pointer to data received buffer
|
|
964 * @param Size: Amount of data to be received
|
|
965 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
|
|
966 * @retval HAL status
|
|
967 */
|
|
968 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
|
|
969 {
|
|
970 uint32_t *tmp;
|
|
971
|
|
972 if(husart->State == HAL_USART_STATE_READY)
|
|
973 {
|
|
974 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
|
|
975 {
|
|
976 return HAL_ERROR;
|
|
977 }
|
|
978 /* Process Locked */
|
|
979 __HAL_LOCK(husart);
|
|
980
|
|
981 husart->pRxBuffPtr = pRxData;
|
|
982 husart->RxXferSize = Size;
|
|
983 husart->pTxBuffPtr = pTxData;
|
|
984 husart->TxXferSize = Size;
|
|
985
|
|
986 husart->ErrorCode = HAL_USART_ERROR_NONE;
|
|
987 husart->State = HAL_USART_STATE_BUSY_TX_RX;
|
|
988
|
|
989 /* Set the USART DMA Rx transfer complete callback */
|
|
990 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
|
|
991
|
|
992 /* Set the USART DMA Half transfer complete callback */
|
|
993 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
|
|
994
|
|
995 /* Set the USART DMA Tx transfer complete callback */
|
|
996 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
|
|
997
|
|
998 /* Set the USART DMA Half transfer complete callback */
|
|
999 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
|
|
1000
|
|
1001 /* Set the USART DMA Tx transfer error callback */
|
|
1002 husart->hdmatx->XferErrorCallback = USART_DMAError;
|
|
1003
|
|
1004 /* Set the USART DMA Rx transfer error callback */
|
|
1005 husart->hdmarx->XferErrorCallback = USART_DMAError;
|
|
1006
|
|
1007 /* Enable the USART receive DMA Stream */
|
|
1008 tmp = (uint32_t*)&pRxData;
|
|
1009 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
|
|
1010
|
|
1011 /* Enable the USART transmit DMA Stream */
|
|
1012 tmp = (uint32_t*)&pTxData;
|
|
1013 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
|
|
1014
|
|
1015 /* Clear the TC flag in the SR register by writing 0 to it */
|
|
1016 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
|
|
1017
|
|
1018 /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
|
|
1019 __HAL_USART_CLEAR_OREFLAG(husart);
|
|
1020
|
|
1021 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
|
|
1022 in the USART CR3 register */
|
|
1023 husart->Instance->CR3 |= USART_CR3_DMAR;
|
|
1024
|
|
1025 /* Enable the DMA transfer for transmit request by setting the DMAT bit
|
|
1026 in the USART CR3 register */
|
|
1027 husart->Instance->CR3 |= USART_CR3_DMAT;
|
|
1028
|
|
1029 /* Process Unlocked */
|
|
1030 __HAL_UNLOCK(husart);
|
|
1031
|
|
1032 return HAL_OK;
|
|
1033 }
|
|
1034 else
|
|
1035 {
|
|
1036 return HAL_BUSY;
|
|
1037 }
|
|
1038 }
|
|
1039
|
|
1040 /**
|
|
1041 * @brief Pauses the DMA Transfer.
|
|
1042 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1043 * the configuration information for the specified USART module.
|
|
1044 * @retval HAL status
|
|
1045 */
|
|
1046 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
|
|
1047 {
|
|
1048 /* Process Locked */
|
|
1049 __HAL_LOCK(husart);
|
|
1050
|
|
1051 /* Disable the USART DMA Tx request */
|
|
1052 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
|
|
1053
|
|
1054 /* Process Unlocked */
|
|
1055 __HAL_UNLOCK(husart);
|
|
1056
|
|
1057 return HAL_OK;
|
|
1058 }
|
|
1059
|
|
1060 /**
|
|
1061 * @brief Resumes the DMA Transfer.
|
|
1062 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1063 * the configuration information for the specified USART module.
|
|
1064 * @retval HAL status
|
|
1065 */
|
|
1066 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
|
|
1067 {
|
|
1068 /* Process Locked */
|
|
1069 __HAL_LOCK(husart);
|
|
1070
|
|
1071 /* Enable the USART DMA Tx request */
|
|
1072 husart->Instance->CR3 |= USART_CR3_DMAT;
|
|
1073
|
|
1074 /* Process Unlocked */
|
|
1075 __HAL_UNLOCK(husart);
|
|
1076
|
|
1077 return HAL_OK;
|
|
1078 }
|
|
1079
|
|
1080 /**
|
|
1081 * @brief Stops the DMA Transfer.
|
|
1082 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1083 * the configuration information for the specified USART module.
|
|
1084 * @retval HAL status
|
|
1085 */
|
|
1086 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
|
|
1087 {
|
|
1088 /* The Lock is not implemented on this API to allow the user application
|
|
1089 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
|
|
1090 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
|
|
1091 and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
|
|
1092 */
|
|
1093
|
|
1094 /* Abort the USART DMA Tx Stream */
|
|
1095 if(husart->hdmatx != NULL)
|
|
1096 {
|
|
1097 HAL_DMA_Abort(husart->hdmatx);
|
|
1098 }
|
|
1099 /* Abort the USART DMA Rx Stream */
|
|
1100 if(husart->hdmarx != NULL)
|
|
1101 {
|
|
1102 HAL_DMA_Abort(husart->hdmarx);
|
|
1103 }
|
|
1104
|
|
1105 /* Disable the USART Tx/Rx DMA requests */
|
|
1106 husart->Instance->CR3 &= ~USART_CR3_DMAT;
|
|
1107 husart->Instance->CR3 &= ~USART_CR3_DMAR;
|
|
1108
|
|
1109 husart->State = HAL_USART_STATE_READY;
|
|
1110
|
|
1111 return HAL_OK;
|
|
1112 }
|
|
1113
|
|
1114 /**
|
|
1115 * @brief This function handles USART interrupt request.
|
|
1116 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1117 * the configuration information for the specified USART module.
|
|
1118 * @retval None
|
|
1119 */
|
|
1120 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
|
|
1121 {
|
|
1122 uint32_t tmp1 = 0, tmp2 = 0;
|
|
1123
|
|
1124 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_PE);
|
|
1125 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE);
|
|
1126 /* USART parity error interrupt occurred -----------------------------------*/
|
|
1127 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1128 {
|
|
1129 __HAL_USART_CLEAR_PEFLAG(husart);
|
|
1130 husart->ErrorCode |= HAL_USART_ERROR_PE;
|
|
1131 }
|
|
1132
|
|
1133 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_FE);
|
|
1134 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR);
|
|
1135 /* USART frame error interrupt occurred ------------------------------------*/
|
|
1136 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1137 {
|
|
1138 __HAL_USART_CLEAR_FEFLAG(husart);
|
|
1139 husart->ErrorCode |= HAL_USART_ERROR_FE;
|
|
1140 }
|
|
1141
|
|
1142 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_NE);
|
|
1143 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR);
|
|
1144 /* USART noise error interrupt occurred ------------------------------------*/
|
|
1145 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1146 {
|
|
1147 __HAL_USART_CLEAR_NEFLAG(husart);
|
|
1148 husart->ErrorCode |= HAL_USART_ERROR_NE;
|
|
1149 }
|
|
1150
|
|
1151 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_ORE);
|
|
1152 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR);
|
|
1153 /* USART Over-Run interrupt occurred ---------------------------------------*/
|
|
1154 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1155 {
|
|
1156 __HAL_USART_CLEAR_OREFLAG(husart);
|
|
1157 husart->ErrorCode |= HAL_USART_ERROR_ORE;
|
|
1158 }
|
|
1159
|
|
1160 if(husart->ErrorCode != HAL_USART_ERROR_NONE)
|
|
1161 {
|
|
1162 /* Set the USART state ready to be able to start again the process */
|
|
1163 husart->State = HAL_USART_STATE_READY;
|
|
1164
|
|
1165 HAL_USART_ErrorCallback(husart);
|
|
1166 }
|
|
1167
|
|
1168 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE);
|
|
1169 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE);
|
|
1170 /* USART in mode Receiver --------------------------------------------------*/
|
|
1171 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1172 {
|
|
1173 if(husart->State == HAL_USART_STATE_BUSY_RX)
|
|
1174 {
|
|
1175 USART_Receive_IT(husart);
|
|
1176 }
|
|
1177 else
|
|
1178 {
|
|
1179 USART_TransmitReceive_IT(husart);
|
|
1180 }
|
|
1181 }
|
|
1182
|
|
1183 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_TXE);
|
|
1184 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE);
|
|
1185 /* USART in mode Transmitter -----------------------------------------------*/
|
|
1186 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1187 {
|
|
1188 if(husart->State == HAL_USART_STATE_BUSY_TX)
|
|
1189 {
|
|
1190 USART_Transmit_IT(husart);
|
|
1191 }
|
|
1192 else
|
|
1193 {
|
|
1194 USART_TransmitReceive_IT(husart);
|
|
1195 }
|
|
1196 }
|
|
1197
|
|
1198 tmp1 = __HAL_USART_GET_FLAG(husart, USART_FLAG_TC);
|
|
1199 tmp2 = __HAL_USART_GET_IT_SOURCE(husart, USART_IT_TC);
|
|
1200 /* USART in mode Transmitter (transmission end) ----------------------------*/
|
|
1201 if((tmp1 != RESET) && (tmp2 != RESET))
|
|
1202 {
|
|
1203 USART_EndTransmit_IT(husart);
|
|
1204 }
|
|
1205 }
|
|
1206
|
|
1207 /**
|
|
1208 * @brief Tx Transfer completed callbacks.
|
|
1209 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1210 * the configuration information for the specified USART module.
|
|
1211 * @retval None
|
|
1212 */
|
|
1213 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
|
|
1214 {
|
|
1215 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1216 the HAL_USART_TxCpltCallback could be implemented in the user file
|
|
1217 */
|
|
1218 }
|
|
1219
|
|
1220 /**
|
|
1221 * @brief Tx Half Transfer completed callbacks.
|
|
1222 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1223 * the configuration information for the specified USART module.
|
|
1224 * @retval None
|
|
1225 */
|
|
1226 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
|
|
1227 {
|
|
1228 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1229 the HAL_USART_TxCpltCallback could be implemented in the user file
|
|
1230 */
|
|
1231 }
|
|
1232
|
|
1233 /**
|
|
1234 * @brief Rx Transfer completed callbacks.
|
|
1235 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1236 * the configuration information for the specified USART module.
|
|
1237 * @retval None
|
|
1238 */
|
|
1239 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
|
|
1240 {
|
|
1241 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1242 the HAL_USART_TxCpltCallback could be implemented in the user file
|
|
1243 */
|
|
1244 }
|
|
1245
|
|
1246 /**
|
|
1247 * @brief Rx Half Transfer completed callbacks.
|
|
1248 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1249 * the configuration information for the specified USART module.
|
|
1250 * @retval None
|
|
1251 */
|
|
1252 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
|
|
1253 {
|
|
1254 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1255 the HAL_USART_TxCpltCallback could be implemented in the user file
|
|
1256 */
|
|
1257 }
|
|
1258
|
|
1259 /**
|
|
1260 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
|
|
1261 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1262 * the configuration information for the specified USART module.
|
|
1263 * @retval None
|
|
1264 */
|
|
1265 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
|
|
1266 {
|
|
1267 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1268 the HAL_USART_TxCpltCallback could be implemented in the user file
|
|
1269 */
|
|
1270 }
|
|
1271
|
|
1272 /**
|
|
1273 * @brief USART error callbacks.
|
|
1274 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1275 * the configuration information for the specified USART module.
|
|
1276 * @retval None
|
|
1277 */
|
|
1278 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
|
|
1279 {
|
|
1280 /* NOTE: This function Should not be modified, when the callback is needed,
|
|
1281 the HAL_USART_ErrorCallback could be implemented in the user file
|
|
1282 */
|
|
1283 }
|
|
1284
|
|
1285 /**
|
|
1286 * @}
|
|
1287 */
|
|
1288
|
|
1289 /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
|
|
1290 * @brief USART State and Errors functions
|
|
1291 *
|
|
1292 @verbatim
|
|
1293 ==============================================================================
|
|
1294 ##### Peripheral State and Errors functions #####
|
|
1295 ==============================================================================
|
|
1296 [..]
|
|
1297 This subsection provides a set of functions allowing to return the State of
|
|
1298 USART communication
|
|
1299 process, return Peripheral Errors occurred during communication process
|
|
1300 (+) HAL_USART_GetState() API can be helpful to check in run-time the state
|
|
1301 of the USART peripheral.
|
|
1302 (+) HAL_USART_GetError() check in run-time errors that could be occurred during
|
|
1303 communication.
|
|
1304 @endverbatim
|
|
1305 * @{
|
|
1306 */
|
|
1307
|
|
1308 /**
|
|
1309 * @brief Returns the USART state.
|
|
1310 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1311 * the configuration information for the specified USART module.
|
|
1312 * @retval HAL state
|
|
1313 */
|
|
1314 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
|
|
1315 {
|
|
1316 return husart->State;
|
|
1317 }
|
|
1318
|
|
1319 /**
|
|
1320 * @brief Return the USART error code
|
|
1321 * @param husart : pointer to a USART_HandleTypeDef structure that contains
|
|
1322 * the configuration information for the specified USART.
|
|
1323 * @retval USART Error Code
|
|
1324 */
|
|
1325 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
|
|
1326 {
|
|
1327 return husart->ErrorCode;
|
|
1328 }
|
|
1329
|
|
1330 /**
|
|
1331 * @}
|
|
1332 */
|
|
1333
|
|
1334
|
|
1335 /**
|
|
1336 * @brief DMA USART transmit process complete callback.
|
|
1337 * @param hdma: DMA handle
|
|
1338 * @retval None
|
|
1339 */
|
|
1340 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
|
|
1341 {
|
|
1342 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1343 /* DMA Normal mode */
|
|
1344 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
|
|
1345 {
|
|
1346 husart->TxXferCount = 0;
|
|
1347 if(husart->State == HAL_USART_STATE_BUSY_TX)
|
|
1348 {
|
|
1349 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
|
|
1350 in the USART CR3 register */
|
|
1351 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
|
|
1352
|
|
1353 /* Enable the USART Transmit Complete Interrupt */
|
|
1354 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
|
|
1355 }
|
|
1356 }
|
|
1357 /* DMA Circular mode */
|
|
1358 else
|
|
1359 {
|
|
1360 if(husart->State == HAL_USART_STATE_BUSY_TX)
|
|
1361 {
|
|
1362 HAL_USART_TxCpltCallback(husart);
|
|
1363 }
|
|
1364 }
|
|
1365 }
|
|
1366
|
|
1367 /**
|
|
1368 * @brief DMA USART transmit process half complete callback
|
|
1369 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
1370 * the configuration information for the specified DMA module.
|
|
1371 * @retval None
|
|
1372 */
|
|
1373 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
|
|
1374 {
|
|
1375 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
|
|
1376
|
|
1377 HAL_USART_TxHalfCpltCallback(husart);
|
|
1378 }
|
|
1379
|
|
1380 /**
|
|
1381 * @brief DMA USART receive process complete callback.
|
|
1382 * @param hdma: DMA handle
|
|
1383 * @retval None
|
|
1384 */
|
|
1385 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
|
|
1386 {
|
|
1387 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1388 /* DMA Normal mode */
|
|
1389 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
|
|
1390 {
|
|
1391 husart->RxXferCount = 0;
|
|
1392 if(husart->State == HAL_USART_STATE_BUSY_RX)
|
|
1393 {
|
|
1394 /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit
|
|
1395 in the USART CR3 register */
|
|
1396 husart->Instance->CR3 &= ~(USART_CR3_DMAR);
|
|
1397
|
|
1398 husart->State= HAL_USART_STATE_READY;
|
|
1399 HAL_USART_RxCpltCallback(husart);
|
|
1400 }
|
|
1401 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
|
|
1402 else
|
|
1403 {
|
|
1404 /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit
|
|
1405 in the USART CR3 register */
|
|
1406 husart->Instance->CR3 &= ~(USART_CR3_DMAR);
|
|
1407 husart->Instance->CR3 &= ~(USART_CR3_DMAT);
|
|
1408
|
|
1409 husart->State= HAL_USART_STATE_READY;
|
|
1410 HAL_USART_TxRxCpltCallback(husart);
|
|
1411 }
|
|
1412 }
|
|
1413 /* DMA circular mode */
|
|
1414 else
|
|
1415 {
|
|
1416 if(husart->State == HAL_USART_STATE_BUSY_RX)
|
|
1417 {
|
|
1418 HAL_USART_RxCpltCallback(husart);
|
|
1419 }
|
|
1420 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
|
|
1421 else
|
|
1422 {
|
|
1423 HAL_USART_TxRxCpltCallback(husart);
|
|
1424 }
|
|
1425 }
|
|
1426 }
|
|
1427
|
|
1428 /**
|
|
1429 * @brief DMA USART receive process half complete callback
|
|
1430 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
|
|
1431 * the configuration information for the specified DMA module.
|
|
1432 * @retval None
|
|
1433 */
|
|
1434 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
|
|
1435 {
|
|
1436 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
|
|
1437
|
|
1438 HAL_USART_RxHalfCpltCallback(husart);
|
|
1439 }
|
|
1440
|
|
1441 /**
|
|
1442 * @brief DMA USART communication error callback.
|
|
1443 * @param hdma: DMA handle
|
|
1444 * @retval None
|
|
1445 */
|
|
1446 static void USART_DMAError(DMA_HandleTypeDef *hdma)
|
|
1447 {
|
|
1448 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
|
|
1449
|
|
1450 husart->RxXferCount = 0;
|
|
1451 husart->TxXferCount = 0;
|
|
1452 husart->ErrorCode |= HAL_USART_ERROR_DMA;
|
|
1453 husart->State= HAL_USART_STATE_READY;
|
|
1454
|
|
1455 HAL_USART_ErrorCallback(husart);
|
|
1456 }
|
|
1457
|
|
1458 /**
|
|
1459 * @brief This function handles USART Communication Timeout.
|
|
1460 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1461 * the configuration information for the specified USART module.
|
|
1462 * @param Flag: specifies the USART flag to check.
|
|
1463 * @param Status: The new Flag status (SET or RESET).
|
|
1464 * @param Timeout: Timeout duration
|
|
1465 * @retval HAL status
|
|
1466 */
|
|
1467 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
|
|
1468 {
|
|
1469 uint32_t tickstart = 0;
|
|
1470
|
|
1471 /* Get tick */
|
|
1472 tickstart = HAL_GetTick();
|
|
1473
|
|
1474 /* Wait until flag is set */
|
|
1475 if(Status == RESET)
|
|
1476 {
|
|
1477 while(__HAL_USART_GET_FLAG(husart, Flag) == RESET)
|
|
1478 {
|
|
1479 /* Check for the Timeout */
|
|
1480 if(Timeout != HAL_MAX_DELAY)
|
|
1481 {
|
|
1482 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
|
1483 {
|
|
1484 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
|
|
1485 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
|
|
1486 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
|
|
1487 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
|
|
1488 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
|
|
1489
|
|
1490 husart->State= HAL_USART_STATE_READY;
|
|
1491
|
|
1492 /* Process Unlocked */
|
|
1493 __HAL_UNLOCK(husart);
|
|
1494
|
|
1495 return HAL_TIMEOUT;
|
|
1496 }
|
|
1497 }
|
|
1498 }
|
|
1499 }
|
|
1500 else
|
|
1501 {
|
|
1502 while(__HAL_USART_GET_FLAG(husart, Flag) != RESET)
|
|
1503 {
|
|
1504 /* Check for the Timeout */
|
|
1505 if(Timeout != HAL_MAX_DELAY)
|
|
1506 {
|
|
1507 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
|
|
1508 {
|
|
1509 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
|
|
1510 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
|
|
1511 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
|
|
1512 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
|
|
1513 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
|
|
1514
|
|
1515 husart->State= HAL_USART_STATE_READY;
|
|
1516
|
|
1517 /* Process Unlocked */
|
|
1518 __HAL_UNLOCK(husart);
|
|
1519
|
|
1520 return HAL_TIMEOUT;
|
|
1521 }
|
|
1522 }
|
|
1523 }
|
|
1524 }
|
|
1525 return HAL_OK;
|
|
1526 }
|
|
1527
|
|
1528
|
|
1529 /**
|
|
1530 * @brief Simplex Send an amount of data in non-blocking mode.
|
|
1531 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1532 * the configuration information for the specified USART module.
|
|
1533 * @retval HAL status
|
|
1534 * @note The USART errors are not managed to avoid the overrun error.
|
|
1535 */
|
|
1536 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
|
|
1537 {
|
|
1538 uint16_t* tmp;
|
|
1539
|
|
1540 if(husart->State == HAL_USART_STATE_BUSY_TX)
|
|
1541 {
|
|
1542 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
1543 {
|
|
1544 tmp = (uint16_t*) husart->pTxBuffPtr;
|
|
1545 husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
|
|
1546 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1547 {
|
|
1548 husart->pTxBuffPtr += 2;
|
|
1549 }
|
|
1550 else
|
|
1551 {
|
|
1552 husart->pTxBuffPtr += 1;
|
|
1553 }
|
|
1554 }
|
|
1555 else
|
|
1556 {
|
|
1557 husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
|
|
1558 }
|
|
1559
|
|
1560 if(--husart->TxXferCount == 0)
|
|
1561 {
|
|
1562 /* Disable the USART Transmit data register empty Interrupt */
|
|
1563 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
|
|
1564
|
|
1565 /* Enable the USART Transmit Complete Interrupt */
|
|
1566 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
|
|
1567 }
|
|
1568 return HAL_OK;
|
|
1569 }
|
|
1570 else
|
|
1571 {
|
|
1572 return HAL_BUSY;
|
|
1573 }
|
|
1574 }
|
|
1575
|
|
1576 /**
|
|
1577 * @brief Wraps up transmission in non blocking mode.
|
|
1578 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1579 * the configuration information for the specified USART module.
|
|
1580 * @retval HAL status
|
|
1581 */
|
|
1582 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
|
|
1583 {
|
|
1584 /* Disable the USART Transmit Complete Interrupt */
|
|
1585 __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
|
|
1586
|
|
1587 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
1588 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
|
|
1589
|
|
1590 husart->State = HAL_USART_STATE_READY;
|
|
1591
|
|
1592 HAL_USART_TxCpltCallback(husart);
|
|
1593
|
|
1594 return HAL_OK;
|
|
1595 }
|
|
1596
|
|
1597 /**
|
|
1598 * @brief Simplex Receive an amount of data in non-blocking mode.
|
|
1599 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1600 * the configuration information for the specified USART module.
|
|
1601 * @retval HAL status
|
|
1602 */
|
|
1603 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
|
|
1604 {
|
|
1605 uint16_t* tmp;
|
|
1606 if(husart->State == HAL_USART_STATE_BUSY_RX)
|
|
1607 {
|
|
1608 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
1609 {
|
|
1610 tmp = (uint16_t*) husart->pRxBuffPtr;
|
|
1611 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1612 {
|
|
1613 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
|
|
1614 husart->pRxBuffPtr += 2;
|
|
1615 }
|
|
1616 else
|
|
1617 {
|
|
1618 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
|
|
1619 husart->pRxBuffPtr += 1;
|
|
1620 }
|
|
1621 if(--husart->RxXferCount != 0x00)
|
|
1622 {
|
|
1623 /* Send dummy byte in order to generate the clock for the slave to send the next data */
|
|
1624 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
|
|
1625 }
|
|
1626 }
|
|
1627 else
|
|
1628 {
|
|
1629 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1630 {
|
|
1631 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
|
|
1632 }
|
|
1633 else
|
|
1634 {
|
|
1635 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
|
|
1636 }
|
|
1637
|
|
1638 if(--husart->RxXferCount != 0x00)
|
|
1639 {
|
|
1640 /* Send dummy byte in order to generate the clock for the slave to send the next data */
|
|
1641 husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x00FF);
|
|
1642 }
|
|
1643 }
|
|
1644
|
|
1645 if(husart->RxXferCount == 0)
|
|
1646 {
|
|
1647 /* Disable the USART RXNE Interrupt */
|
|
1648 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
|
|
1649
|
|
1650 /* Disable the USART Parity Error Interrupt */
|
|
1651 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
|
|
1652
|
|
1653 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
1654 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
|
|
1655
|
|
1656 husart->State = HAL_USART_STATE_READY;
|
|
1657 HAL_USART_RxCpltCallback(husart);
|
|
1658
|
|
1659 return HAL_OK;
|
|
1660 }
|
|
1661 return HAL_OK;
|
|
1662 }
|
|
1663 else
|
|
1664 {
|
|
1665 return HAL_BUSY;
|
|
1666 }
|
|
1667 }
|
|
1668
|
|
1669 /**
|
|
1670 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
|
|
1671 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1672 * the configuration information for the specified USART module.
|
|
1673 * @retval HAL status
|
|
1674 */
|
|
1675 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
|
|
1676 {
|
|
1677 uint16_t* tmp;
|
|
1678
|
|
1679 if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
|
|
1680 {
|
|
1681 if(husart->TxXferCount != 0x00)
|
|
1682 {
|
|
1683 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
|
|
1684 {
|
|
1685 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
1686 {
|
|
1687 tmp = (uint16_t*) husart->pTxBuffPtr;
|
|
1688 husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
|
|
1689 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1690 {
|
|
1691 husart->pTxBuffPtr += 2;
|
|
1692 }
|
|
1693 else
|
|
1694 {
|
|
1695 husart->pTxBuffPtr += 1;
|
|
1696 }
|
|
1697 }
|
|
1698 else
|
|
1699 {
|
|
1700 husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
|
|
1701 }
|
|
1702 husart->TxXferCount--;
|
|
1703
|
|
1704 /* Check the latest data transmitted */
|
|
1705 if(husart->TxXferCount == 0)
|
|
1706 {
|
|
1707 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
|
|
1708 }
|
|
1709 }
|
|
1710 }
|
|
1711
|
|
1712 if(husart->RxXferCount != 0x00)
|
|
1713 {
|
|
1714 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
|
|
1715 {
|
|
1716 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
|
|
1717 {
|
|
1718 tmp = (uint16_t*) husart->pRxBuffPtr;
|
|
1719 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1720 {
|
|
1721 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
|
|
1722 husart->pRxBuffPtr += 2;
|
|
1723 }
|
|
1724 else
|
|
1725 {
|
|
1726 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
|
|
1727 husart->pRxBuffPtr += 1;
|
|
1728 }
|
|
1729 }
|
|
1730 else
|
|
1731 {
|
|
1732 if(husart->Init.Parity == USART_PARITY_NONE)
|
|
1733 {
|
|
1734 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
|
|
1735 }
|
|
1736 else
|
|
1737 {
|
|
1738 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
|
|
1739 }
|
|
1740 }
|
|
1741 husart->RxXferCount--;
|
|
1742 }
|
|
1743 }
|
|
1744
|
|
1745 /* Check the latest data received */
|
|
1746 if(husart->RxXferCount == 0)
|
|
1747 {
|
|
1748 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
|
|
1749
|
|
1750 /* Disable the USART Parity Error Interrupt */
|
|
1751 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
|
|
1752
|
|
1753 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
|
|
1754 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
|
|
1755
|
|
1756 husart->State = HAL_USART_STATE_READY;
|
|
1757
|
|
1758 HAL_USART_TxRxCpltCallback(husart);
|
|
1759
|
|
1760 return HAL_OK;
|
|
1761 }
|
|
1762
|
|
1763 return HAL_OK;
|
|
1764 }
|
|
1765 else
|
|
1766 {
|
|
1767 return HAL_BUSY;
|
|
1768 }
|
|
1769 }
|
|
1770
|
|
1771 /**
|
|
1772 * @brief Configures the USART peripheral.
|
|
1773 * @param husart: pointer to a USART_HandleTypeDef structure that contains
|
|
1774 * the configuration information for the specified USART module.
|
|
1775 * @retval None
|
|
1776 */
|
|
1777 static void USART_SetConfig(USART_HandleTypeDef *husart)
|
|
1778 {
|
|
1779 uint32_t tmpreg = 0x00;
|
|
1780
|
|
1781 /* Check the parameters */
|
|
1782 assert_param(IS_USART_INSTANCE(husart->Instance));
|
|
1783 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
|
|
1784 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
|
|
1785 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
|
|
1786 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
|
|
1787 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
|
|
1788 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
|
|
1789 assert_param(IS_USART_PARITY(husart->Init.Parity));
|
|
1790 assert_param(IS_USART_MODE(husart->Init.Mode));
|
|
1791
|
|
1792 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
|
|
1793 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
|
|
1794 husart->Instance->CR1 &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
|
|
1795
|
|
1796 /*---------------------------- USART CR2 Configuration ---------------------*/
|
|
1797 tmpreg = husart->Instance->CR2;
|
|
1798 /* Clear CLKEN, CPOL, CPHA and LBCL bits */
|
|
1799 tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP));
|
|
1800 /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
|
|
1801 /* Set CPOL bit according to husart->Init.CLKPolarity value */
|
|
1802 /* Set CPHA bit according to husart->Init.CLKPhase value */
|
|
1803 /* Set LBCL bit according to husart->Init.CLKLastBit value */
|
|
1804 /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
|
|
1805 tmpreg |= (uint32_t)(USART_CLOCK_ENABLE| husart->Init.CLKPolarity |
|
|
1806 husart->Init.CLKPhase| husart->Init.CLKLastBit | husart->Init.StopBits);
|
|
1807 /* Write to USART CR2 */
|
|
1808 husart->Instance->CR2 = (uint32_t)tmpreg;
|
|
1809
|
|
1810 /*-------------------------- USART CR1 Configuration -----------------------*/
|
|
1811 tmpreg = husart->Instance->CR1;
|
|
1812
|
|
1813 /* Clear M, PCE, PS, TE, RE and OVER8 bits */
|
|
1814 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
|
|
1815 USART_CR1_RE | USART_CR1_OVER8));
|
|
1816
|
|
1817 /* Configure the USART Word Length, Parity and mode:
|
|
1818 Set the M bits according to husart->Init.WordLength value
|
|
1819 Set PCE and PS bits according to husart->Init.Parity value
|
|
1820 Set TE and RE bits according to husart->Init.Mode value
|
|
1821 Force OVER8 bit to 1 in order to reach the max USART frequencies */
|
|
1822 tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
|
|
1823
|
|
1824 /* Write to USART CR1 */
|
|
1825 husart->Instance->CR1 = (uint32_t)tmpreg;
|
|
1826
|
|
1827 /*-------------------------- USART CR3 Configuration -----------------------*/
|
|
1828 /* Clear CTSE and RTSE bits */
|
|
1829 husart->Instance->CR3 &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
|
|
1830
|
|
1831 /*-------------------------- USART BRR Configuration -----------------------*/
|
|
1832 if((husart->Instance == USART1) || (husart->Instance == USART6))
|
|
1833 {
|
|
1834 husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate);
|
|
1835 }
|
|
1836 else
|
|
1837 {
|
|
1838 husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate);
|
|
1839 }
|
|
1840 }
|
|
1841
|
|
1842 /**
|
|
1843 * @}
|
|
1844 */
|
|
1845
|
|
1846 #endif /* HAL_USART_MODULE_ENABLED */
|
|
1847 /**
|
|
1848 * @}
|
|
1849 */
|
|
1850
|
|
1851 /**
|
|
1852 * @}
|
|
1853 */
|
|
1854
|
|
1855 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|