F1 and F3 HAL / LL libraries
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_usart.c
blob3ffa01d45ba3984d542685be438e91f57a6a3597
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_usart.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-2017
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:
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 the USART pins (TX as alternate function pull-up, RX as alternate function Input).
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_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
32 (+++) Declare a DMA handle structure for the Tx/Rx channel.
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 channel.
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 channel.
38 (+++) Configure the priority and enable the NVIC for the transfer complete
39 interrupt on the DMA Tx/Rx channel.
40 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
41 (used for last byte sending completion detection in DMA non circular mode)
43 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
44 flow control and Mode(Receiver/Transmitter) in the husart Init structure.
46 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
47 (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
48 by calling the customized HAL_USART_MspInit(&husart) API.
50 -@@- The specific USART interrupts (Transmission complete interrupt,
51 RXNE interrupt and Error Interrupts) will be managed using the macros
52 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
54 (#) Three operation modes are available within this driver :
56 *** Polling mode IO operation ***
57 =================================
58 [..]
59 (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
60 (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
62 *** Interrupt mode IO operation ***
63 ===================================
64 [..]
65 (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
66 (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
67 add his own code by customization of function pointer HAL_USART_TxCpltCallback
68 (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
69 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
70 add his own code by customization of function pointer HAL_USART_RxCpltCallback
71 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
72 add his own code by customization of function pointer HAL_USART_ErrorCallback
74 *** DMA mode IO operation ***
75 ==============================
76 [..]
77 (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
78 (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
79 add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
80 (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
81 add his own code by customization of function pointer HAL_USART_TxCpltCallback
82 (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
83 (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
84 add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
85 (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
86 add his own code by customization of function pointer HAL_USART_RxCpltCallback
87 (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
88 add his own code by customization of function pointer HAL_USART_ErrorCallback
89 (+) Pause the DMA Transfer using HAL_USART_DMAPause()
90 (+) Resume the DMA Transfer using HAL_USART_DMAResume()
91 (+) Stop the DMA Transfer using HAL_USART_DMAStop()
93 *** USART HAL driver macros list ***
94 =============================================
95 [..]
96 Below the list of most used macros in USART HAL driver.
98 (+) __HAL_USART_ENABLE: Enable the USART peripheral
99 (+) __HAL_USART_DISABLE: Disable the USART peripheral
100 (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
101 (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
102 (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
103 (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
105 [..]
106 (@) You can refer to the USART HAL driver header file for more useful macros
108 @endverbatim
109 [..]
110 (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
111 in the data register is transmitted but is changed by the parity bit.
112 Depending on the frame length defined by the M bit (8-bits or 9-bits),
113 the possible USART frame formats are as listed in the following table:
114 +-------------------------------------------------------------+
115 | M bit | PCE bit | USART frame |
116 |---------------------|---------------------------------------|
117 | 0 | 0 | | SB | 8 bit data | STB | |
118 |---------|-----------|---------------------------------------|
119 | 0 | 1 | | SB | 7 bit data | PB | STB | |
120 |---------|-----------|---------------------------------------|
121 | 1 | 0 | | SB | 9 bit data | STB | |
122 |---------|-----------|---------------------------------------|
123 | 1 | 1 | | SB | 8 bit data | PB | STB | |
124 +-------------------------------------------------------------+
125 ******************************************************************************
126 * @attention
128 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
130 * Redistribution and use in source and binary forms, with or without modification,
131 * are permitted provided that the following conditions are met:
132 * 1. Redistributions of source code must retain the above copyright notice,
133 * this list of conditions and the following disclaimer.
134 * 2. Redistributions in binary form must reproduce the above copyright notice,
135 * this list of conditions and the following disclaimer in the documentation
136 * and/or other materials provided with the distribution.
137 * 3. Neither the name of STMicroelectronics nor the names of its contributors
138 * may be used to endorse or promote products derived from this software
139 * without specific prior written permission.
141 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
142 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
143 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
144 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
145 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
146 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
147 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
148 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
149 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
150 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152 ******************************************************************************
155 /* Includes ------------------------------------------------------------------*/
156 #include "stm32f1xx_hal.h"
158 /** @addtogroup STM32F1xx_HAL_Driver
159 * @{
162 /** @defgroup USART USART
163 * @brief HAL USART Synchronous module driver
164 * @{
166 #ifdef HAL_USART_MODULE_ENABLED
168 /* Private typedef -----------------------------------------------------------*/
169 /* Private define ------------------------------------------------------------*/
170 /** @addtogroup USART_Private_Constants
171 * @{
173 #define DUMMY_DATA 0xFFFFU
174 #define USART_TIMEOUT_VALUE 22000U
176 * @}
178 /* Private macro -------------------------------------------------------------*/
179 /* Private variables ---------------------------------------------------------*/
180 /* Private function prototypes -----------------------------------------------*/
181 /* Private functions ---------------------------------------------------------*/
182 /** @addtogroup USART_Private_Functions
183 * @{
185 static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
186 static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
187 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
188 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
189 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
190 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
191 static void USART_SetConfig (USART_HandleTypeDef *husart);
192 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
193 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
194 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
195 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
196 static void USART_DMAError(DMA_HandleTypeDef *hdma);
197 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
198 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
199 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
201 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
203 * @}
206 /* Exported functions --------------------------------------------------------*/
207 /** @defgroup USART_Exported_Functions USART Exported Functions
208 * @{
211 /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
212 * @brief Initialization and Configuration functions
214 @verbatim
215 ==============================================================================
216 ##### Initialization and Configuration functions #####
217 ==============================================================================
218 [..]
219 This subsection provides a set of functions allowing to initialize the USART
220 in asynchronous and in synchronous modes.
221 (+) For the asynchronous mode only these parameters can be configured:
222 (++) Baud Rate
223 (++) Word Length
224 (++) Stop Bit
225 (++) Parity: If the parity is enabled, then the MSB bit of the data written
226 in the data register is transmitted but is changed by the parity bit.
227 Depending on the frame length defined by the M bit (8-bits or 9-bits),
228 please refer to Reference manual for possible USART frame formats.
229 (++) USART polarity
230 (++) USART phase
231 (++) USART LastBit
232 (++) Receiver/transmitter modes
234 [..]
235 The HAL_USART_Init() function follows the USART synchronous configuration
236 procedure (details for the procedure are available in reference manuals
237 (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
239 @endverbatim
240 * @{
244 * @brief Initializes the USART mode according to the specified
245 * parameters in the USART_InitTypeDef and create the associated handle.
246 * @param husart: pointer to a USART_HandleTypeDef structure that contains
247 * the configuration information for the specified USART module.
248 * @retval HAL status
250 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
252 /* Check the USART handle allocation */
253 if(husart == NULL)
255 return HAL_ERROR;
258 /* Check the parameters */
259 assert_param(IS_USART_INSTANCE(husart->Instance));
261 if(husart->State == HAL_USART_STATE_RESET)
263 /* Allocate lock resource and initialize it */
264 husart->Lock = HAL_UNLOCKED;
266 /* Init the low level hardware */
267 HAL_USART_MspInit(husart);
270 husart->State = HAL_USART_STATE_BUSY;
272 /* Set the USART Communication parameters */
273 USART_SetConfig(husart);
275 /* In USART mode, the following bits must be kept cleared:
276 - LINEN bit in the USART_CR2 register
277 - HDSEL, SCEN and IREN bits in the USART_CR3 register */
278 CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
279 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
281 /* Enable the Peripheral */
282 __HAL_USART_ENABLE(husart);
284 /* Initialize the USART state */
285 husart->ErrorCode = HAL_USART_ERROR_NONE;
286 husart->State= HAL_USART_STATE_READY;
288 return HAL_OK;
292 * @brief DeInitializes the USART peripheral.
293 * @param husart: pointer to a USART_HandleTypeDef structure that contains
294 * the configuration information for the specified USART module.
295 * @retval HAL status
297 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
299 /* Check the USART handle allocation */
300 if(husart == NULL)
302 return HAL_ERROR;
305 /* Check the parameters */
306 assert_param(IS_USART_INSTANCE(husart->Instance));
308 husart->State = HAL_USART_STATE_BUSY;
310 /* DeInit the low level hardware */
311 HAL_USART_MspDeInit(husart);
313 husart->ErrorCode = HAL_USART_ERROR_NONE;
314 husart->State = HAL_USART_STATE_RESET;
316 /* Release Lock */
317 __HAL_UNLOCK(husart);
319 return HAL_OK;
323 * @brief USART MSP Init.
324 * @param husart: pointer to a USART_HandleTypeDef structure that contains
325 * the configuration information for the specified USART module.
326 * @retval None
328 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
330 /* Prevent unused argument(s) compilation warning */
331 UNUSED(husart);
332 /* NOTE: This function should not be modified, when the callback is needed,
333 the HAL_USART_MspInit could be implemented in the user file
338 * @brief USART MSP DeInit.
339 * @param husart: pointer to a USART_HandleTypeDef structure that contains
340 * the configuration information for the specified USART module.
341 * @retval None
343 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
345 /* Prevent unused argument(s) compilation warning */
346 UNUSED(husart);
347 /* NOTE: This function should not be modified, when the callback is needed,
348 the HAL_USART_MspDeInit could be implemented in the user file
353 * @}
356 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
357 * @brief USART Transmit and Receive functions
359 @verbatim
360 ==============================================================================
361 ##### IO operation functions #####
362 ==============================================================================
363 [..]
364 This subsection provides a set of functions allowing to manage the USART synchronous
365 data transfers.
367 [..]
368 The USART supports master mode only: it cannot receive or send data related to an input
369 clock (SCLK is always an output).
371 (#) There are two modes of transfer:
372 (++) Blocking mode: The communication is performed in polling mode.
373 The HAL status of all data processing is returned by the same function
374 after finishing transfer.
375 (++) No-Blocking mode: The communication is performed using Interrupts
376 or DMA, These API's return the HAL status.
377 The end of the data processing will be indicated through the
378 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
379 using DMA mode.
380 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
381 user callbacks
382 will be executed respectively at the end of the transmit or Receive process
383 The HAL_USART_ErrorCallback() user callback will be executed when a communication
384 error is detected
386 (#) Blocking mode APIs are :
387 (++) HAL_USART_Transmit() in simplex mode
388 (++) HAL_USART_Receive() in full duplex receive only
389 (++) HAL_USART_TransmitReceive() in full duplex mode
391 (#) Non Blocking mode APIs with Interrupt are :
392 (++) HAL_USART_Transmit_IT()in simplex mode
393 (++) HAL_USART_Receive_IT() in full duplex receive only
394 (++) HAL_USART_TransmitReceive_IT() in full duplex mode
395 (++) HAL_USART_IRQHandler()
397 (#) Non Blocking mode functions with DMA are :
398 (++) HAL_USART_Transmit_DMA()in simplex mode
399 (++) HAL_USART_Receive_DMA() in full duplex receive only
400 (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
401 (++) HAL_USART_DMAPause()
402 (++) HAL_USART_DMAResume()
403 (++) HAL_USART_DMAStop()
405 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
406 (++) HAL_USART_TxHalfCpltCallback()
407 (++) HAL_USART_TxCpltCallback()
408 (++) HAL_USART_RxHalfCpltCallback()
409 (++) HAL_USART_RxCpltCallback()
410 (++) HAL_USART_ErrorCallback()
411 (++) HAL_USART_TxRxCpltCallback()
413 @endverbatim
414 * @{
418 * @brief Simplex Send an amount of data in blocking mode.
419 * @param husart: pointer to a USART_HandleTypeDef structure that contains
420 * the configuration information for the specified USART module.
421 * @param pTxData: Pointer to data buffer
422 * @param Size: Amount of data to be sent
423 * @param Timeout: Timeout duration
424 * @retval HAL status
426 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
428 uint16_t* tmp;
429 uint32_t tickstart = 0U;
431 if(husart->State == HAL_USART_STATE_READY)
433 if((pTxData == NULL) || (Size == 0U))
435 return HAL_ERROR;
438 /* Process Locked */
439 __HAL_LOCK(husart);
441 husart->ErrorCode = HAL_USART_ERROR_NONE;
442 husart->State = HAL_USART_STATE_BUSY_TX;
444 /* Init tickstart for timeout managment */
445 tickstart = HAL_GetTick();
447 husart->TxXferSize = Size;
448 husart->TxXferCount = Size;
449 while(husart->TxXferCount > 0U)
451 husart->TxXferCount--;
452 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
454 /* Wait for TC flag in order to write data in DR */
455 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
457 return HAL_TIMEOUT;
459 tmp = (uint16_t*) pTxData;
460 WRITE_REG(husart->Instance->DR, (*tmp & (uint16_t)0x01FF));
461 if(husart->Init.Parity == USART_PARITY_NONE)
463 pTxData += 2U;
465 else
467 pTxData += 1U;
470 else
472 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
474 return HAL_TIMEOUT;
476 WRITE_REG(husart->Instance->DR, (*pTxData++ & (uint8_t)0xFF));
480 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
482 return HAL_TIMEOUT;
485 husart->State = HAL_USART_STATE_READY;
487 /* Process Unlocked */
488 __HAL_UNLOCK(husart);
490 return HAL_OK;
492 else
494 return HAL_BUSY;
499 * @brief Full-Duplex Receive an amount of data in blocking mode.
500 * @param husart: pointer to a USART_HandleTypeDef structure that contains
501 * the configuration information for the specified USART module.
502 * @param pRxData: Pointer to data buffer
503 * @param Size: Amount of data to be received
504 * @param Timeout: Timeout duration
505 * @retval HAL status
507 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
509 uint16_t* tmp;
510 uint32_t tickstart = 0U;
512 if(husart->State == HAL_USART_STATE_READY)
514 if((pRxData == NULL) || (Size == 0U))
516 return HAL_ERROR;
518 /* Process Locked */
519 __HAL_LOCK(husart);
521 husart->ErrorCode = HAL_USART_ERROR_NONE;
522 husart->State = HAL_USART_STATE_BUSY_RX;
524 /* Init tickstart for timeout managment */
525 tickstart = HAL_GetTick();
527 husart->RxXferSize = Size;
528 husart->RxXferCount = Size;
529 /* Check the remain data to be received */
530 while(husart->RxXferCount > 0U)
532 husart->RxXferCount--;
533 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
535 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
536 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
538 return HAL_TIMEOUT;
540 /* Send dummy byte in order to generate clock */
541 WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
543 /* Wait for RXNE Flag */
544 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
546 return HAL_TIMEOUT;
548 tmp = (uint16_t*) pRxData ;
549 if(husart->Init.Parity == USART_PARITY_NONE)
551 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
552 pRxData +=2U;
554 else
556 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
557 pRxData +=1U;
560 else
562 /* Wait until TXE flag is set to send dummy byte in order to generate the clock for the slave to send data */
563 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
565 return HAL_TIMEOUT;
568 /* Send Dummy Byte in order to generate clock */
569 WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x00FF));
571 /* Wait until RXNE flag is set to receive the byte */
572 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
574 return HAL_TIMEOUT;
576 if(husart->Init.Parity == USART_PARITY_NONE)
578 /* Receive data */
579 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
581 else
583 /* Receive data */
584 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
590 husart->State = HAL_USART_STATE_READY;
592 /* Process Unlocked */
593 __HAL_UNLOCK(husart);
595 return HAL_OK;
597 else
599 return HAL_BUSY;
604 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (blocking mode).
605 * @param husart: pointer to a USART_HandleTypeDef structure that contains
606 * the configuration information for the specified USART module.
607 * @param pTxData: Pointer to data transmitted buffer
608 * @param pRxData: Pointer to data received buffer
609 * @param Size: Amount of data to be sent
610 * @param Timeout: Timeout duration
611 * @retval HAL status
613 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
615 uint16_t* tmp;
616 uint32_t tickstart = 0U;
618 if(husart->State == HAL_USART_STATE_READY)
620 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
622 return HAL_ERROR;
624 /* Process Locked */
625 __HAL_LOCK(husart);
627 husart->ErrorCode = HAL_USART_ERROR_NONE;
628 husart->State = HAL_USART_STATE_BUSY_RX;
630 /* Init tickstart for timeout managment */
631 tickstart = HAL_GetTick();
633 husart->RxXferSize = Size;
634 husart->TxXferSize = Size;
635 husart->TxXferCount = Size;
636 husart->RxXferCount = Size;
638 /* Check the remain data to be received */
639 while(husart->TxXferCount > 0U)
641 husart->TxXferCount--;
642 husart->RxXferCount--;
643 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
645 /* Wait for TC flag in order to write data in DR */
646 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
648 return HAL_TIMEOUT;
650 tmp = (uint16_t*) pTxData;
651 WRITE_REG(husart->Instance->DR, (*tmp & (uint16_t)0x01FF));
652 if(husart->Init.Parity == USART_PARITY_NONE)
654 pTxData += 2U;
656 else
658 pTxData += 1U;
661 /* Wait for RXNE Flag */
662 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
664 return HAL_TIMEOUT;
666 tmp = (uint16_t*) pRxData ;
667 if(husart->Init.Parity == USART_PARITY_NONE)
669 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
670 pRxData += 2U;
672 else
674 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
675 pRxData += 1U;
678 else
680 /* Wait for TC flag in order to write data in DR */
681 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
683 return HAL_TIMEOUT;
685 WRITE_REG(husart->Instance->DR, (*pTxData++ & (uint8_t)0x00FF));
687 /* Wait for RXNE Flag */
688 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
690 return HAL_TIMEOUT;
692 if(husart->Init.Parity == USART_PARITY_NONE)
694 /* Receive data */
695 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
697 else
699 /* Receive data */
700 *pRxData++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
705 husart->State = HAL_USART_STATE_READY;
707 /* Process Unlocked */
708 __HAL_UNLOCK(husart);
710 return HAL_OK;
712 else
714 return HAL_BUSY;
719 * @brief Simplex Send an amount of data in non-blocking mode.
720 * @param husart: pointer to a USART_HandleTypeDef structure that contains
721 * the configuration information for the specified USART module.
722 * @param pTxData: Pointer to data buffer
723 * @param Size: Amount of data to be sent
724 * @retval HAL status
725 * @note The USART errors are not managed to avoid the overrun error.
727 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
729 /* Check that a Tx process is not already ongoing */
730 if(husart->State == HAL_USART_STATE_READY)
732 if((pTxData == NULL) || (Size == 0U))
734 return HAL_ERROR;
736 /* Process Locked */
737 __HAL_LOCK(husart);
739 husart->pTxBuffPtr = pTxData;
740 husart->TxXferSize = Size;
741 husart->TxXferCount = Size;
743 husart->ErrorCode = HAL_USART_ERROR_NONE;
744 husart->State = HAL_USART_STATE_BUSY_TX;
746 /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
747 are not managed by the USART transmit process to avoid the overrun interrupt
748 when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
749 to benefit for the frame error and noise interrupts the USART mode should be
750 configured only for transmit "USART_MODE_TX"
751 The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
752 Noise error interrupt */
754 /* Process Unlocked */
755 __HAL_UNLOCK(husart);
757 /* Enable the USART Transmit Data Register Empty Interrupt */
758 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
760 return HAL_OK;
762 else
764 return HAL_BUSY;
769 * @brief Simplex Receive an amount of data in non-blocking mode.
770 * @param husart: pointer to a USART_HandleTypeDef structure that contains
771 * the configuration information for the specified USART module.
772 * @param pRxData: Pointer to data buffer
773 * @param Size: Amount of data to be received
774 * @retval HAL status
776 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
778 if(husart->State == HAL_USART_STATE_READY)
780 if((pRxData == NULL) || (Size == 0U))
782 return HAL_ERROR;
784 /* Process Locked */
785 __HAL_LOCK(husart);
787 husart->pRxBuffPtr = pRxData;
788 husart->RxXferSize = Size;
789 husart->RxXferCount = Size;
791 husart->ErrorCode = HAL_USART_ERROR_NONE;
792 husart->State = HAL_USART_STATE_BUSY_RX;
794 /* Process Unlocked */
795 __HAL_UNLOCK(husart);
797 /* Enable the USART Data Register not empty Interrupt */
798 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
800 /* Enable the USART Parity Error Interrupt */
801 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
803 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
804 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
806 /* Send dummy byte in order to generate the clock for the slave to send data */
807 WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
809 return HAL_OK;
811 else
813 return HAL_BUSY;
818 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
819 * @param husart: pointer to a USART_HandleTypeDef structure that contains
820 * the configuration information for the specified USART module.
821 * @param pTxData: Pointer to data transmitted buffer
822 * @param pRxData: Pointer to data received buffer
823 * @param Size: Amount of data to be received
824 * @retval HAL status
826 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
828 if(husart->State == HAL_USART_STATE_READY)
830 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
832 return HAL_ERROR;
834 /* Process Locked */
835 __HAL_LOCK(husart);
837 husart->pRxBuffPtr = pRxData;
838 husart->RxXferSize = Size;
839 husart->RxXferCount = Size;
840 husart->pTxBuffPtr = pTxData;
841 husart->TxXferSize = Size;
842 husart->TxXferCount = Size;
844 husart->ErrorCode = HAL_USART_ERROR_NONE;
845 husart->State = HAL_USART_STATE_BUSY_TX_RX;
847 /* Process Unlocked */
848 __HAL_UNLOCK(husart);
850 /* Enable the USART Data Register not empty Interrupt */
851 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
853 /* Enable the USART Parity Error Interrupt */
854 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
856 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
857 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
859 /* Enable the USART Transmit Data Register Empty Interrupt */
860 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
862 return HAL_OK;
864 else
866 return HAL_BUSY;
871 * @brief Simplex Send an amount of data in non-blocking mode.
872 * @param husart: pointer to a USART_HandleTypeDef structure that contains
873 * the configuration information for the specified USART module.
874 * @param pTxData: Pointer to data buffer
875 * @param Size: Amount of data to be sent
876 * @retval HAL status
878 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
880 uint32_t *tmp;
882 if(husart->State == HAL_USART_STATE_READY)
884 if((pTxData == NULL) || (Size == 0U))
886 return HAL_ERROR;
888 /* Process Locked */
889 __HAL_LOCK(husart);
891 husart->pTxBuffPtr = pTxData;
892 husart->TxXferSize = Size;
893 husart->TxXferCount = Size;
895 husart->ErrorCode = HAL_USART_ERROR_NONE;
896 husart->State = HAL_USART_STATE_BUSY_TX;
898 /* Set the USART DMA transfer complete callback */
899 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
901 /* Set the USART DMA Half transfer complete callback */
902 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
904 /* Set the DMA error callback */
905 husart->hdmatx->XferErrorCallback = USART_DMAError;
907 /* Set the DMA abort callback */
908 husart->hdmatx->XferAbortCallback = NULL;
910 /* Enable the USART transmit DMA channel */
911 tmp = (uint32_t*)&pTxData;
912 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
914 /* Clear the TC flag in the SR register by writing 0 to it */
915 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
917 /* Process Unlocked */
918 __HAL_UNLOCK(husart);
920 /* Enable the DMA transfer for transmit request by setting the DMAT bit
921 in the USART CR3 register */
922 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
924 return HAL_OK;
926 else
928 return HAL_BUSY;
933 * @brief Full-Duplex Receive an amount of data in non-blocking mode.
934 * @param husart: pointer to a USART_HandleTypeDef structure that contains
935 * the configuration information for the specified USART module.
936 * @param pRxData: Pointer to data buffer
937 * @param Size: Amount of data to be received
938 * @retval HAL status
939 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
940 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
942 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
944 uint32_t *tmp;
946 if(husart->State == HAL_USART_STATE_READY)
948 if((pRxData == NULL) || (Size == 0U))
950 return HAL_ERROR;
953 /* Process Locked */
954 __HAL_LOCK(husart);
956 husart->pRxBuffPtr = pRxData;
957 husart->RxXferSize = Size;
958 husart->pTxBuffPtr = pRxData;
959 husart->TxXferSize = Size;
961 husart->ErrorCode = HAL_USART_ERROR_NONE;
962 husart->State = HAL_USART_STATE_BUSY_RX;
964 /* Set the USART DMA Rx transfer complete callback */
965 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
967 /* Set the USART DMA Half transfer complete callback */
968 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
970 /* Set the USART DMA Rx transfer error callback */
971 husart->hdmarx->XferErrorCallback = USART_DMAError;
973 /* Set the DMA abort callback */
974 husart->hdmarx->XferAbortCallback = NULL;
976 /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
977 is performed in DMA reception complete callback */
978 husart->hdmatx->XferHalfCpltCallback = NULL;
979 husart->hdmatx->XferCpltCallback = NULL;
981 /* Set the DMA error callback */
982 husart->hdmatx->XferErrorCallback = USART_DMAError;
984 /* Set the DMA AbortCpltCallback */
985 husart->hdmatx->XferAbortCallback = NULL;
987 /* Enable the USART receive DMA channel */
988 tmp = (uint32_t*)&pRxData;
989 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
991 /* Enable the USART transmit DMA channel: the transmit channel is used in order
992 to generate in the non-blocking mode the clock to the slave device,
993 this mode isn't a simplex receive mode but a full-duplex receive one */
994 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
996 /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */
997 __HAL_USART_CLEAR_OREFLAG(husart);
999 /* Process Unlocked */
1000 __HAL_UNLOCK(husart);
1002 /* Enable the USART Parity Error Interrupt */
1003 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1005 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1006 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1008 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1009 in the USART CR3 register */
1010 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1012 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1013 in the USART CR3 register */
1014 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1016 return HAL_OK;
1018 else
1020 return HAL_BUSY;
1025 * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
1026 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1027 * the configuration information for the specified USART module.
1028 * @param pTxData: Pointer to data transmitted buffer
1029 * @param pRxData: Pointer to data received buffer
1030 * @param Size: Amount of data to be received
1031 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1032 * @retval HAL status
1034 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1036 uint32_t *tmp;
1038 if(husart->State == HAL_USART_STATE_READY)
1040 if((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1042 return HAL_ERROR;
1044 /* Process Locked */
1045 __HAL_LOCK(husart);
1047 husart->pRxBuffPtr = pRxData;
1048 husart->RxXferSize = Size;
1049 husart->pTxBuffPtr = pTxData;
1050 husart->TxXferSize = Size;
1052 husart->ErrorCode = HAL_USART_ERROR_NONE;
1053 husart->State = HAL_USART_STATE_BUSY_TX_RX;
1055 /* Set the USART DMA Rx transfer complete callback */
1056 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1058 /* Set the USART DMA Half transfer complete callback */
1059 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1061 /* Set the USART DMA Tx transfer complete callback */
1062 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1064 /* Set the USART DMA Half transfer complete callback */
1065 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1067 /* Set the USART DMA Tx transfer error callback */
1068 husart->hdmatx->XferErrorCallback = USART_DMAError;
1070 /* Set the USART DMA Rx transfer error callback */
1071 husart->hdmarx->XferErrorCallback = USART_DMAError;
1073 /* Set the DMA abort callback */
1074 husart->hdmarx->XferAbortCallback = NULL;
1076 /* Enable the USART receive DMA channel */
1077 tmp = (uint32_t*)&pRxData;
1078 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t*)tmp, Size);
1080 /* Enable the USART transmit DMA channel */
1081 tmp = (uint32_t*)&pTxData;
1082 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->DR, Size);
1084 /* Clear the TC flag in the SR register by writing 0 to it */
1085 __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
1087 /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
1088 __HAL_USART_CLEAR_OREFLAG(husart);
1090 /* Process Unlocked */
1091 __HAL_UNLOCK(husart);
1093 /* Enable the USART Parity Error Interrupt */
1094 SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1096 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1097 SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1099 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1100 in the USART CR3 register */
1101 SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1103 /* Enable the DMA transfer for transmit request by setting the DMAT bit
1104 in the USART CR3 register */
1105 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1107 return HAL_OK;
1109 else
1111 return HAL_BUSY;
1116 * @brief Pauses the DMA Transfer.
1117 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1118 * the configuration information for the specified USART module.
1119 * @retval HAL status
1121 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
1123 /* Process Locked */
1124 __HAL_LOCK(husart);
1126 /* Disable the USART DMA Tx request */
1127 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1129 /* Process Unlocked */
1130 __HAL_UNLOCK(husart);
1132 return HAL_OK;
1136 * @brief Resumes the DMA Transfer.
1137 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1138 * the configuration information for the specified USART module.
1139 * @retval HAL status
1141 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
1143 /* Process Locked */
1144 __HAL_LOCK(husart);
1146 /* Enable the USART DMA Tx request */
1147 SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1149 /* Process Unlocked */
1150 __HAL_UNLOCK(husart);
1152 return HAL_OK;
1156 * @brief Stops the DMA Transfer.
1157 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1158 * the configuration information for the specified USART module.
1159 * @retval HAL status
1161 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
1163 uint32_t dmarequest = 0x00U;
1164 /* The Lock is not implemented on this API to allow the user application
1165 to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
1166 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1167 and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
1170 /* Stop USART DMA Tx request if ongoing */
1171 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
1172 if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
1174 USART_EndTxTransfer(husart);
1176 /* Abort the USART DMA Tx channel */
1177 if(husart->hdmatx != NULL)
1179 HAL_DMA_Abort(husart->hdmatx);
1182 /* Disable the USART Tx DMA request */
1183 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1186 /* Stop USART DMA Rx request if ongoing */
1187 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
1188 if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
1190 USART_EndRxTransfer(husart);
1192 /* Abort the USART DMA Rx channel */
1193 if(husart->hdmarx != NULL)
1195 HAL_DMA_Abort(husart->hdmarx);
1198 /* Disable the USART Rx DMA request */
1199 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1202 return HAL_OK;
1206 * @brief Abort ongoing transfer (blocking mode).
1207 * @param husart USART handle.
1208 * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
1209 * as described by TransferType parameter) started in Interrupt or DMA mode.
1210 * This procedure performs following operations :
1211 * - Disable PPP Interrupts (depending of transfer direction)
1212 * - Disable the DMA transfer in the peripheral register (if enabled)
1213 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1214 * - Set handle State to READY
1215 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1216 * @retval HAL status
1218 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
1220 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1221 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1222 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1224 /* Disable the USART DMA Tx request if enabled */
1225 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1227 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1229 /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
1230 if(husart->hdmatx != NULL)
1232 /* Set the USART DMA Abort callback to Null.
1233 No call back execution at end of DMA abort procedure */
1234 husart->hdmatx->XferAbortCallback = NULL;
1236 HAL_DMA_Abort(husart->hdmatx);
1240 /* Disable the USART DMA Rx request if enabled */
1241 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1243 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1245 /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
1246 if(husart->hdmarx != NULL)
1248 /* Set the USART DMA Abort callback to Null.
1249 No call back execution at end of DMA abort procedure */
1250 husart->hdmarx->XferAbortCallback = NULL;
1252 HAL_DMA_Abort(husart->hdmarx);
1256 /* Reset Tx and Rx transfer counters */
1257 husart->TxXferCount = 0x00U;
1258 husart->RxXferCount = 0x00U;
1260 /* Restore husart->State to Ready */
1261 husart->State = HAL_USART_STATE_READY;
1263 /* Reset Handle ErrorCode to No Error */
1264 husart->ErrorCode = HAL_USART_ERROR_NONE;
1266 return HAL_OK;
1270 * @brief Abort ongoing transfer (Interrupt mode).
1271 * @param husart USART handle.
1272 * @note This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
1273 * as described by TransferType parameter) started in Interrupt or DMA mode.
1274 * This procedure performs following operations :
1275 * - Disable PPP Interrupts (depending of transfer direction)
1276 * - Disable the DMA transfer in the peripheral register (if enabled)
1277 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1278 * - Set handle State to READY
1279 * - At abort completion, call user abort complete callback
1280 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1281 * considered as completed only when user abort complete callback is executed (not when exiting function).
1282 * @retval HAL status
1284 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
1286 uint32_t AbortCplt = 0x01U;
1288 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1289 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1290 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1292 /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
1293 before any call to DMA Abort functions */
1294 /* DMA Tx Handle is valid */
1295 if(husart->hdmatx != NULL)
1297 /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
1298 Otherwise, set it to NULL */
1299 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1301 husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
1303 else
1305 husart->hdmatx->XferAbortCallback = NULL;
1308 /* DMA Rx Handle is valid */
1309 if(husart->hdmarx != NULL)
1311 /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
1312 Otherwise, set it to NULL */
1313 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1315 husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
1317 else
1319 husart->hdmarx->XferAbortCallback = NULL;
1323 /* Disable the USART DMA Tx request if enabled */
1324 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1326 /* Disable DMA Tx at USART level */
1327 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1329 /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
1330 if(husart->hdmatx != NULL)
1332 /* USART Tx DMA Abort callback has already been initialised :
1333 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
1335 /* Abort DMA TX */
1336 if(HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
1338 husart->hdmatx->XferAbortCallback = NULL;
1340 else
1342 AbortCplt = 0x00U;
1347 /* Disable the USART DMA Rx request if enabled */
1348 if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1350 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1352 /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
1353 if(husart->hdmarx != NULL)
1355 /* USART Rx DMA Abort callback has already been initialised :
1356 will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
1358 /* Abort DMA RX */
1359 if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
1361 husart->hdmarx->XferAbortCallback = NULL;
1362 AbortCplt = 0x01U;
1364 else
1366 AbortCplt = 0x00U;
1371 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1372 if(AbortCplt == 0x01U)
1374 /* Reset Tx and Rx transfer counters */
1375 husart->TxXferCount = 0x00U;
1376 husart->RxXferCount = 0x00U;
1378 /* Reset errorCode */
1379 husart->ErrorCode = HAL_USART_ERROR_NONE;
1381 /* Restore husart->State to Ready */
1382 husart->State = HAL_USART_STATE_READY;
1384 /* As no DMA to be aborted, call directly user Abort complete callback */
1385 HAL_USART_AbortCpltCallback(husart);
1388 return HAL_OK;
1392 * @brief This function handles USART interrupt request.
1393 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1394 * the configuration information for the specified USART module.
1395 * @retval None
1397 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
1399 uint32_t isrflags = READ_REG(husart->Instance->SR);
1400 uint32_t cr1its = READ_REG(husart->Instance->CR1);
1401 uint32_t cr3its = READ_REG(husart->Instance->CR3);
1402 uint32_t errorflags = 0x00U;
1403 uint32_t dmarequest = 0x00U;
1405 /* If no error occurs */
1406 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1407 if(errorflags == RESET)
1409 /* USART in mode Receiver -------------------------------------------------*/
1410 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1412 if(husart->State == HAL_USART_STATE_BUSY_RX)
1414 USART_Receive_IT(husart);
1416 else
1418 USART_TransmitReceive_IT(husart);
1420 return;
1423 /* If some errors occur */
1424 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1426 /* USART parity error interrupt occurred ----------------------------------*/
1427 if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1429 husart->ErrorCode |= HAL_USART_ERROR_PE;
1432 /* USART noise error interrupt occurred --------------------------------*/
1433 if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1435 husart->ErrorCode |= HAL_USART_ERROR_NE;
1438 /* USART frame error interrupt occurred --------------------------------*/
1439 if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1441 husart->ErrorCode |= HAL_USART_ERROR_FE;
1444 /* USART Over-Run interrupt occurred -----------------------------------*/
1445 if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1447 husart->ErrorCode |= HAL_USART_ERROR_ORE;
1450 if(husart->ErrorCode != HAL_USART_ERROR_NONE)
1452 /* USART in mode Receiver -----------------------------------------------*/
1453 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1455 if(husart->State == HAL_USART_STATE_BUSY_RX)
1457 USART_Receive_IT(husart);
1459 else
1461 USART_TransmitReceive_IT(husart);
1464 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1465 consider error as blocking */
1466 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
1467 if(((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest)
1469 /* Set the USART state ready to be able to start again the process,
1470 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1471 USART_EndRxTransfer(husart);
1473 /* Disable the USART DMA Rx request if enabled */
1474 if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1476 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1478 /* Abort the USART DMA Rx channel */
1479 if(husart->hdmarx != NULL)
1481 /* Set the USART DMA Abort callback :
1482 will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
1483 husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
1485 if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
1487 /* Call Directly XferAbortCallback function in case of error */
1488 husart->hdmarx->XferAbortCallback(husart->hdmarx);
1491 else
1493 /* Call user error callback */
1494 HAL_USART_ErrorCallback(husart);
1497 else
1499 /* Call user error callback */
1500 HAL_USART_ErrorCallback(husart);
1503 else
1505 /* Call user error callback */
1506 HAL_USART_ErrorCallback(husart);
1507 husart->ErrorCode = HAL_USART_ERROR_NONE;
1510 return;
1513 /* USART in mode Transmitter -----------------------------------------------*/
1514 if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1516 if(husart->State == HAL_USART_STATE_BUSY_TX)
1518 USART_Transmit_IT(husart);
1520 else
1522 USART_TransmitReceive_IT(husart);
1524 return;
1527 /* USART in mode Transmitter (transmission end) ----------------------------*/
1528 if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1530 USART_EndTransmit_IT(husart);
1531 return;
1536 * @brief Tx Transfer completed callbacks.
1537 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1538 * the configuration information for the specified USART module.
1539 * @retval None
1541 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
1543 /* Prevent unused argument(s) compilation warning */
1544 UNUSED(husart);
1545 /* NOTE: This function should not be modified, when the callback is needed,
1546 the HAL_USART_TxCpltCallback could be implemented in the user file
1551 * @brief Tx Half Transfer completed callbacks.
1552 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1553 * the configuration information for the specified USART module.
1554 * @retval None
1556 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
1558 /* Prevent unused argument(s) compilation warning */
1559 UNUSED(husart);
1560 /* NOTE: This function should not be modified, when the callback is needed,
1561 the HAL_USART_TxHalfCpltCallback could be implemented in the user file
1566 * @brief Rx Transfer completed callbacks.
1567 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1568 * the configuration information for the specified USART module.
1569 * @retval None
1571 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
1573 /* Prevent unused argument(s) compilation warning */
1574 UNUSED(husart);
1575 /* NOTE: This function should not be modified, when the callback is needed,
1576 the HAL_USART_RxCpltCallback could be implemented in the user file
1581 * @brief Rx Half Transfer completed callbacks.
1582 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1583 * the configuration information for the specified USART module.
1584 * @retval None
1586 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
1588 /* Prevent unused argument(s) compilation warning */
1589 UNUSED(husart);
1590 /* NOTE: This function should not be modified, when the callback is needed,
1591 the HAL_USART_RxHalfCpltCallback could be implemented in the user file
1596 * @brief Tx/Rx Transfers completed callback for the non-blocking process.
1597 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1598 * the configuration information for the specified USART module.
1599 * @retval None
1601 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
1603 /* Prevent unused argument(s) compilation warning */
1604 UNUSED(husart);
1605 /* NOTE: This function should not be modified, when the callback is needed,
1606 the HAL_USART_TxRxCpltCallback could be implemented in the user file
1611 * @brief USART error callbacks.
1612 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1613 * the configuration information for the specified USART module.
1614 * @retval None
1616 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
1618 /* Prevent unused argument(s) compilation warning */
1619 UNUSED(husart);
1620 /* NOTE: This function should not be modified, when the callback is needed,
1621 the HAL_USART_ErrorCallback could be implemented in the user file
1626 * @brief USART Abort Complete callback.
1627 * @param husart USART handle.
1628 * @retval None
1630 __weak void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart)
1632 /* Prevent unused argument(s) compilation warning */
1633 UNUSED(husart);
1635 /* NOTE : This function should not be modified, when the callback is needed,
1636 the HAL_USART_AbortCpltCallback can be implemented in the user file.
1641 * @}
1644 /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
1645 * @brief USART State and Errors functions
1647 @verbatim
1648 ==============================================================================
1649 ##### Peripheral State and Errors functions #####
1650 ==============================================================================
1651 [..]
1652 This subsection provides a set of functions allowing to return the State of
1653 USART communication
1654 process, return Peripheral Errors occurred during communication process
1655 (+) HAL_USART_GetState() API can be helpful to check in run-time the state
1656 of the USART peripheral.
1657 (+) HAL_USART_GetError() check in run-time errors that could be occurred during
1658 communication.
1659 @endverbatim
1660 * @{
1664 * @brief Returns the USART state.
1665 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1666 * the configuration information for the specified USART module.
1667 * @retval HAL state
1669 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
1671 return husart->State;
1675 * @brief Return the USART error code
1676 * @param husart : pointer to a USART_HandleTypeDef structure that contains
1677 * the configuration information for the specified USART.
1678 * @retval USART Error Code
1680 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
1682 return husart->ErrorCode;
1686 * @}
1690 * @}
1693 /** @defgroup USART_Private_Functions USART Private Functions
1694 * @brief USART Private functions
1695 * @{
1698 * @brief DMA USART transmit process complete callback.
1699 * @param hdma: DMA handle
1700 * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
1701 * the configuration information for the specified DMA module.
1702 * @retval None
1704 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1706 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1707 /* DMA Normal mode */
1708 if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
1710 husart->TxXferCount = 0U;
1711 if(husart->State == HAL_USART_STATE_BUSY_TX)
1713 /* Disable the DMA transfer for transmit request by resetting the DMAT bit
1714 in the USART CR3 register */
1715 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1717 /* Enable the USART Transmit Complete Interrupt */
1718 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
1721 /* DMA Circular mode */
1722 else
1724 if(husart->State == HAL_USART_STATE_BUSY_TX)
1726 HAL_USART_TxCpltCallback(husart);
1732 * @brief DMA USART transmit process half complete callback
1733 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1734 * the configuration information for the specified DMA module.
1735 * @retval None
1737 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1739 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1741 HAL_USART_TxHalfCpltCallback(husart);
1745 * @brief DMA USART receive process complete callback.
1746 * @param hdma: DMA handle
1747 * @retval None
1749 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1751 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1752 /* DMA Normal mode */
1753 if(HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
1755 husart->RxXferCount = 0x00U;
1757 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1758 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1759 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1761 if(husart->State == HAL_USART_STATE_BUSY_RX)
1763 /* Disable the DMA transfer for the Transmit/Receiver requests by setting the DMAT/DMAR bit
1764 in the USART CR3 register */
1765 CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1767 husart->State= HAL_USART_STATE_READY;
1768 HAL_USART_RxCpltCallback(husart);
1770 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
1771 else
1773 /* Disable the DMA transfer for the Transmit/receiver requests by setting the DMAT/DMAR bit
1774 in the USART CR3 register */
1775 CLEAR_BIT(husart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
1777 husart->State= HAL_USART_STATE_READY;
1778 HAL_USART_TxRxCpltCallback(husart);
1781 /* DMA circular mode */
1782 else
1784 if(husart->State == HAL_USART_STATE_BUSY_RX)
1786 HAL_USART_RxCpltCallback(husart);
1788 /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
1789 else
1791 HAL_USART_TxRxCpltCallback(husart);
1797 * @brief DMA USART receive process half complete callback
1798 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1799 * the configuration information for the specified DMA module.
1800 * @retval None
1802 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1804 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1806 HAL_USART_RxHalfCpltCallback(husart);
1810 * @brief DMA USART communication error callback.
1811 * @param hdma: DMA handle
1812 * @retval None
1814 static void USART_DMAError(DMA_HandleTypeDef *hdma)
1816 uint32_t dmarequest = 0x00U;
1817 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1818 husart->RxXferCount = 0x00U;
1819 husart->TxXferCount = 0x00U;
1821 /* Stop USART DMA Tx request if ongoing */
1822 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
1823 if((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
1825 USART_EndTxTransfer(husart);
1828 /* Stop USART DMA Rx request if ongoing */
1829 dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
1830 if((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
1832 USART_EndRxTransfer(husart);
1835 husart->ErrorCode |= HAL_USART_ERROR_DMA;
1836 husart->State= HAL_USART_STATE_READY;
1838 HAL_USART_ErrorCallback(husart);
1842 * @brief This function handles USART Communication Timeout.
1843 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1844 * the configuration information for the specified USART module.
1845 * @param Flag: specifies the USART flag to check.
1846 * @param Status: The new Flag status (SET or RESET).
1847 * @param Tickstart: Tick start value.
1848 * @param Timeout: Timeout duration.
1849 * @retval HAL status
1851 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
1853 /* Wait until flag is set */
1854 while((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
1856 /* Check for the Timeout */
1857 if(Timeout != HAL_MAX_DELAY)
1859 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
1861 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1862 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
1863 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
1864 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
1865 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1867 husart->State= HAL_USART_STATE_READY;
1869 /* Process Unlocked */
1870 __HAL_UNLOCK(husart);
1872 return HAL_TIMEOUT;
1876 return HAL_OK;
1880 * @brief End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
1881 * @param husart: USART handle.
1882 * @retval None
1884 static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
1886 /* Disable TXEIE and TCIE interrupts */
1887 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1889 /* At end of Tx process, restore husart->State to Ready */
1890 husart->State = HAL_USART_STATE_READY;
1894 * @brief End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
1895 * @param husart: USART handle.
1896 * @retval None
1898 static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
1900 /* Disable RXNE, PE and ERR interrupts */
1901 CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1902 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1904 /* At end of Rx process, restore husart->State to Ready */
1905 husart->State = HAL_USART_STATE_READY;
1909 * @brief DMA USART communication abort callback, when initiated by HAL services on Error
1910 * (To be called at end of DMA Abort procedure following error occurrence).
1911 * @param hdma DMA handle.
1912 * @retval None
1914 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
1916 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1917 husart->RxXferCount = 0x00U;
1918 husart->TxXferCount = 0x00U;
1920 HAL_USART_ErrorCallback(husart);
1924 * @brief DMA USART Tx communication abort callback, when initiated by user
1925 * (To be called at end of DMA Tx Abort procedure following user abort request).
1926 * @note When this callback is executed, User Abort complete call back is called only if no
1927 * Abort still ongoing for Rx DMA Handle.
1928 * @param hdma DMA handle.
1929 * @retval None
1931 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
1933 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1935 husart->hdmatx->XferAbortCallback = NULL;
1937 /* Check if an Abort process is still ongoing */
1938 if(husart->hdmarx != NULL)
1940 if(husart->hdmarx->XferAbortCallback != NULL)
1942 return;
1946 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1947 husart->TxXferCount = 0x00U;
1948 husart->RxXferCount = 0x00U;
1950 /* Reset errorCode */
1951 husart->ErrorCode = HAL_USART_ERROR_NONE;
1953 /* Restore husart->State to Ready */
1954 husart->State = HAL_USART_STATE_READY;
1956 /* Call user Abort complete callback */
1957 HAL_USART_AbortCpltCallback(husart);
1961 * @brief DMA USART Rx communication abort callback, when initiated by user
1962 * (To be called at end of DMA Rx Abort procedure following user abort request).
1963 * @note When this callback is executed, User Abort complete call back is called only if no
1964 * Abort still ongoing for Tx DMA Handle.
1965 * @param hdma DMA handle.
1966 * @retval None
1968 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
1970 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1972 husart->hdmarx->XferAbortCallback = NULL;
1974 /* Check if an Abort process is still ongoing */
1975 if(husart->hdmatx != NULL)
1977 if(husart->hdmatx->XferAbortCallback != NULL)
1979 return;
1983 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
1984 husart->TxXferCount = 0x00U;
1985 husart->RxXferCount = 0x00U;
1987 /* Reset errorCode */
1988 husart->ErrorCode = HAL_USART_ERROR_NONE;
1990 /* Restore husart->State to Ready */
1991 husart->State = HAL_USART_STATE_READY;
1993 /* Call user Abort complete callback */
1994 HAL_USART_AbortCpltCallback(husart);
1998 * @brief Simplex Send an amount of data in non-blocking mode.
1999 * @param husart: pointer to a USART_HandleTypeDef structure that contains
2000 * the configuration information for the specified USART module.
2001 * @retval HAL status
2002 * @note The USART errors are not managed to avoid the overrun error.
2004 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
2006 uint16_t* tmp;
2008 if(husart->State == HAL_USART_STATE_BUSY_TX)
2010 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
2012 tmp = (uint16_t*) husart->pTxBuffPtr;
2013 WRITE_REG(husart->Instance->DR, (uint16_t)(*tmp & (uint16_t)0x01FF));
2014 if(husart->Init.Parity == USART_PARITY_NONE)
2016 husart->pTxBuffPtr += 2U;
2018 else
2020 husart->pTxBuffPtr += 1U;
2023 else
2025 WRITE_REG(husart->Instance->DR, (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF));
2028 if(--husart->TxXferCount == 0U)
2030 /* Disable the USART Transmit data register empty Interrupt */
2031 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
2033 /* Enable the USART Transmit Complete Interrupt */
2034 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
2036 return HAL_OK;
2038 else
2040 return HAL_BUSY;
2045 * @brief Wraps up transmission in non blocking mode.
2046 * @param husart: pointer to a USART_HandleTypeDef structure that contains
2047 * the configuration information for the specified USART module.
2048 * @retval HAL status
2050 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
2052 /* Disable the USART Transmit Complete Interrupt */
2053 __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
2055 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2056 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
2058 husart->State = HAL_USART_STATE_READY;
2060 HAL_USART_TxCpltCallback(husart);
2062 return HAL_OK;
2066 * @brief Simplex Receive an amount of data in non-blocking mode.
2067 * @param husart: pointer to a USART_HandleTypeDef structure that contains
2068 * the configuration information for the specified USART module.
2069 * @retval HAL status
2071 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
2073 uint16_t* tmp;
2074 if(husart->State == HAL_USART_STATE_BUSY_RX)
2076 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
2078 tmp = (uint16_t*) husart->pRxBuffPtr;
2079 if(husart->Init.Parity == USART_PARITY_NONE)
2081 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
2082 husart->pRxBuffPtr += 2U;
2084 else
2086 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
2087 husart->pRxBuffPtr += 1U;
2089 if(--husart->RxXferCount != 0x00U)
2091 /* Send dummy byte in order to generate the clock for the slave to send the next data */
2092 WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x01FF));
2095 else
2097 if(husart->Init.Parity == USART_PARITY_NONE)
2099 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
2101 else
2103 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
2106 if(--husart->RxXferCount != 0x00U)
2108 /* Send dummy byte in order to generate the clock for the slave to send the next data */
2109 WRITE_REG(husart->Instance->DR, (DUMMY_DATA & (uint16_t)0x00FF));
2113 if(husart->RxXferCount == 0U)
2115 /* Disable the USART RXNE Interrupt */
2116 CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
2118 /* Disable the USART Parity Error Interrupt */
2119 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2121 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2122 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2124 husart->State = HAL_USART_STATE_READY;
2125 HAL_USART_RxCpltCallback(husart);
2127 return HAL_OK;
2129 return HAL_OK;
2131 else
2133 return HAL_BUSY;
2138 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
2139 * @param husart: pointer to a USART_HandleTypeDef structure that contains
2140 * the configuration information for the specified USART module.
2141 * @retval HAL status
2143 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
2145 uint16_t* tmp;
2147 if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
2149 if(husart->TxXferCount != 0x00U)
2151 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
2153 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
2155 tmp = (uint16_t*) husart->pTxBuffPtr;
2156 WRITE_REG(husart->Instance->DR, (uint16_t)(*tmp & (uint16_t)0x01FF));
2157 if(husart->Init.Parity == USART_PARITY_NONE)
2159 husart->pTxBuffPtr += 2U;
2161 else
2163 husart->pTxBuffPtr += 1U;
2166 else
2168 WRITE_REG(husart->Instance->DR, (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF));
2170 husart->TxXferCount--;
2172 /* Check the latest data transmitted */
2173 if(husart->TxXferCount == 0U)
2175 CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
2180 if(husart->RxXferCount != 0x00U)
2182 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
2184 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
2186 tmp = (uint16_t*) husart->pRxBuffPtr;
2187 if(husart->Init.Parity == USART_PARITY_NONE)
2189 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
2190 husart->pRxBuffPtr += 2U;
2192 else
2194 *tmp = (uint16_t)(husart->Instance->DR & (uint16_t)0x00FF);
2195 husart->pRxBuffPtr += 1U;
2198 else
2200 if(husart->Init.Parity == USART_PARITY_NONE)
2202 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
2204 else
2206 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
2209 husart->RxXferCount--;
2213 /* Check the latest data received */
2214 if(husart->RxXferCount == 0U)
2216 /* Disable the USART RXNE Interrupt */
2217 CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
2219 /* Disable the USART Parity Error Interrupt */
2220 CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2222 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
2223 CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2225 husart->State = HAL_USART_STATE_READY;
2227 HAL_USART_TxRxCpltCallback(husart);
2229 return HAL_OK;
2232 return HAL_OK;
2234 else
2236 return HAL_BUSY;
2241 * @brief Configures the USART pferipheral.
2242 * @param husart: pointer to a USART_HandleTypeDef structure that contains
2243 * the configuration information for the specified USART module.
2244 * @retval None
2246 static void USART_SetConfig(USART_HandleTypeDef *husart)
2248 /* Check the parameters */
2249 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
2250 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
2251 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
2252 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
2253 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
2254 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
2255 assert_param(IS_USART_PARITY(husart->Init.Parity));
2256 assert_param(IS_USART_MODE(husart->Init.Mode));
2258 /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
2259 receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
2260 CLEAR_BIT(husart->Instance->CR1, ((uint32_t)(USART_CR1_TE | USART_CR1_RE)));
2262 /*---------------------------- USART CR2 Configuration ---------------------*/
2263 /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
2264 /* Set CPOL bit according to husart->Init.CLKPolarity value */
2265 /* Set CPHA bit according to husart->Init.CLKPhase value */
2266 /* Set LBCL bit according to husart->Init.CLKLastBit value */
2267 /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
2268 /* Write to USART CR2 */
2269 MODIFY_REG(husart->Instance->CR2,
2270 (uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP),
2271 ((uint32_t)(USART_CLOCK_ENABLE| husart->Init.CLKPolarity | husart->Init.CLKPhase| husart->Init.CLKLastBit | husart->Init.StopBits)));
2273 /*-------------------------- USART CR1 Configuration -----------------------*/
2274 /* Configure the USART Word Length, Parity and mode:
2275 Set the M bits according to husart->Init.WordLength value
2276 Set PCE and PS bits according to husart->Init.Parity value
2277 Set TE and RE bits according to husart->Init.Mode value */
2278 MODIFY_REG(husart->Instance->CR1,
2279 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
2280 (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode);
2282 /*-------------------------- USART CR3 Configuration -----------------------*/
2283 /* Clear CTSE and RTSE bits */
2284 CLEAR_BIT(husart->Instance->CR3, (uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
2286 /*-------------------------- USART BRR Configuration -----------------------*/
2287 if((husart->Instance == USART1))
2289 husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate);
2291 else
2293 husart->Instance->BRR = USART_BRR(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate);
2298 * @}
2301 #endif /* HAL_USART_MODULE_ENABLED */
2303 * @}
2307 * @}
2310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/