2 ******************************************************************************
3 * @file stm32f3xx_hal_usart.c
4 * @author MCD Application Team
7 * @brief USART HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
12 * + Initialization and de-initialization functions
13 * + IO operation functions
14 * + Peripheral Control functions
17 ===============================================================================
18 ##### How to use this driver #####
19 ===============================================================================
21 The USART HAL driver can be used as follows:
23 (#) Declare a USART_HandleTypeDef handle structure.
24 (#) Initialize the USART low level resources by implement the HAL_USART_MspInit ()API:
25 (##) Enable the USARTx interface clock.
26 (##) USART pins configuration:
27 (+) Enable the clock for the USART GPIOs.
28 (+) Configure these USART pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
30 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
31 (+) Configure the USARTx interrupt priority.
32 (+) Enable the NVIC USART IRQ handle.
33 (@) The specific USART interrupts (Transmission complete interrupt,
34 RXNE interrupt and Error Interrupts) will be managed using the macros
35 __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
36 (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
37 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
38 (+) Declare a DMA handle structure for the Tx/Rx channel.
39 (+) Enable the DMAx interface clock.
40 (+) Configure the declared DMA handle structure with the required Tx/Rx parameters.
41 (+) Configure the DMA Tx/Rx channel.
42 (+) Associate the initilalized DMA handle to the USART DMA Tx/Rx handle.
43 (+) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
45 (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
46 flow control and Mode(Receiver/Transmitter) in the husart Init structure.
48 (#) Initialize the USART registers by calling the HAL_USART_Init() API:
49 (+) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
50 by calling the customed HAL_USART_MspInit(&husart) API.
53 ******************************************************************************
56 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
58 * Redistribution and use in source and binary forms, with or without modification,
59 * are permitted provided that the following conditions are met:
60 * 1. Redistributions of source code must retain the above copyright notice,
61 * this list of conditions and the following disclaimer.
62 * 2. Redistributions in binary form must reproduce the above copyright notice,
63 * this list of conditions and the following disclaimer in the documentation
64 * and/or other materials provided with the distribution.
65 * 3. Neither the name of STMicroelectronics nor the names of its contributors
66 * may be used to endorse or promote products derived from this software
67 * without specific prior written permission.
69 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
70 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
71 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
73 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
74 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
75 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
76 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
77 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
78 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
80 ******************************************************************************
83 /* Includes ------------------------------------------------------------------*/
84 #include "stm32f3xx_hal.h"
86 /** @addtogroup STM32F3xx_HAL_Driver
90 /** @defgroup USART HAL USART Synchronous module driver
91 * @brief HAL USART Synchronous module driver
94 #ifdef HAL_USART_MODULE_ENABLED
95 /* Private typedef -----------------------------------------------------------*/
96 /* Private define ------------------------------------------------------------*/
97 /** @defgroup UASRT_Private_Constants USART Private Constants
100 #define DUMMY_DATA ((uint16_t) 0xFFFF)
101 #define TEACK_REACK_TIMEOUT ((uint32_t) 1000)
102 #define USART_TXDMA_TIMEOUTVALUE 22000
103 #define USART_TIMEOUT_VALUE 22000
104 #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
105 USART_CR1_TE | USART_CR1_RE))
106 #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
107 USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP))
111 /* Private macro -------------------------------------------------------------*/
112 /* Private variables ---------------------------------------------------------*/
113 /* Private function prototypes -----------------------------------------------*/
114 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
115 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
116 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
117 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
118 static void USART_DMAError(DMA_HandleTypeDef *hdma);
119 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
120 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
121 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
122 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
123 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
124 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
125 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
126 /* Exported functions --------------------------------------------------------*/
129 /** @defgroup USART_Exported_Functions USART Exported Functions
133 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
134 * @brief Initialization and Configuration functions
137 ===============================================================================
138 ##### Initialization and Configuration functions #####
139 ===============================================================================
141 This subsection provides a set of functions allowing to initialize the USART
142 in asynchronous and in synchronous modes.
143 (+) For the asynchronous mode only these parameters can be configured:
147 (++) Parity: If the parity is enabled, then the MSB bit of the data written
148 in the data register is transmitted but is changed by the parity bit.
149 Depending on the frame length defined by the M bit (8-bits or 9-bits)
150 or by the M1 and M0 bits (7-bit, 8-bit or 9-bit),
151 the possible USART frame formats are as listed in the following table:
152 +---------------------------------------------------------------+
153 | M bit | PCE bit | USART frame |
154 |-----------|-----------|---------------------------------------|
155 | 0 | 0 | | SB | 8-bit data | STB | |
156 |-----------|-----------|---------------------------------------|
157 | 0 | 1 | | SB | 7-bit data | PB | STB | |
158 |-----------|-----------|---------------------------------------|
159 | 1 | 0 | | SB | 9-bit data | STB | |
160 |-----------|-----------|---------------------------------------|
161 | 1 | 1 | | SB | 8-bit data | PB | STB | |
162 +---------------------------------------------------------------+
163 | M1M0 bits | PCE bit | USART frame |
164 |-----------------------|---------------------------------------|
165 | 10 | 0 | | SB | 7-bit data | STB | |
166 |-----------|-----------|---------------------------------------|
167 | 10 | 1 | | SB | 6-bit data | PB | STB | |
168 +---------------------------------------------------------------+
172 (++) Receiver/transmitter modes
175 The HAL_USART_Init() function follows the USART synchronous configuration
176 procedure (details for the procedure are available in reference manual).
183 * @brief Initializes the USART mode according to the specified
184 * parameters in the USART_InitTypeDef and create the associated handle .
185 * @param husart: usart handle
188 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
190 /* Check the USART handle allocation */
191 if(husart == HAL_NULL)
196 /* Check the parameters */
197 assert_param(IS_USART_INSTANCE(husart->Instance));
199 if(husart->State == HAL_USART_STATE_RESET)
201 /* Init the low level hardware : GPIO, CLOCK */
202 HAL_USART_MspInit(husart);
205 husart->State = HAL_USART_STATE_BUSY;
207 /* Disable the Peripheral */
208 __HAL_USART_DISABLE(husart);
210 /* Set the Usart Communication parameters */
211 if (USART_SetConfig(husart) == HAL_ERROR)
216 /* In Synchronous mode, the following bits must be kept cleared:
217 - LINEN bit in the USART_CR2 register
218 - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
219 husart->Instance->CR2 &= ~USART_CR2_LINEN;
220 husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
222 /* Enable the Peripharal */
223 __HAL_USART_ENABLE(husart);
225 /* TEACK and/or REACK to check before moving husart->State to Ready */
226 return (USART_CheckIdleState(husart));
230 * @brief DeInitializes the USART peripheral
231 * @param husart: usart handle
234 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
236 /* Check the USART handle allocation */
237 if(husart == HAL_NULL)
242 /* Check the parameters */
243 assert_param(IS_USART_INSTANCE(husart->Instance));
245 husart->State = HAL_USART_STATE_BUSY;
247 husart->Instance->CR1 = 0x0;
248 husart->Instance->CR2 = 0x0;
249 husart->Instance->CR3 = 0x0;
251 /* DeInit the low level hardware */
252 HAL_USART_MspDeInit(husart);
254 husart->ErrorCode = HAL_USART_ERROR_NONE;
255 husart->State = HAL_USART_STATE_RESET;
258 __HAL_UNLOCK(husart);
264 * @brief USART MSP Init
265 * @param husart: usart handle
268 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
270 /* NOTE : This function should not be modified, when the callback is needed,
271 the HAL_USART_MspInit can be implemented in the user file
276 * @brief USART MSP DeInit
277 * @param husart: usart handle
280 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
282 /* NOTE : This function should not be modified, when the callback is needed,
283 the HAL_USART_MspDeInit can be implemented in the user file
291 /** @defgroup USART_Exported_Functions_Group2 Input and Output operation functions
292 * @brief USART Transmit/Receive functions
295 ===============================================================================
296 ##### I/O operation functions #####
297 ===============================================================================
298 This subsection provides a set of functions allowing to manage the USART synchronous
301 [..] The USART supports master mode only: it cannot receive or send data related to an input
302 clock (SCLK is always an output).
304 (#) There are two mode of transfer:
305 (+) Blocking mode: The communication is performed in polling mode.
306 The HAL status of all data processing is returned by the same function
307 after finishing transfer.
308 (+) No-Blocking mode: The communication is performed using Interrupts
309 or DMA, These API's return the HAL status.
310 The end of the data processing will be indicated through the
311 dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
313 The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
314 will be executed respectivelly at the end of the transmit or Receive process
315 The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
317 (#) Blocking mode API's are :
318 (+) HAL_USART_Transmit()in simplex mode
319 (+) HAL_USART_Receive() in full duplex receive only
320 (+) HAL_USART_TransmitReceive() in full duplex mode
322 (#) Non-Blocking mode API's with Interrupt are :
323 (+) HAL_USART_Transmit_IT()in simplex mode
324 (+) HAL_USART_Receive_IT() in full duplex receive only
325 (+) HAL_USART_TransmitReceive_IT()in full duplex mode
326 (+) HAL_USART_IRQHandler()
328 (#) No-Blocking mode functions with DMA are :
329 (+) HAL_USART_Transmit_DMA()in simplex mode
330 (+) HAL_USART_Receive_DMA() in full duplex receive only
331 (+) HAL_USART_TransmitReceive_DMA() in full duplex mode
332 (+) HAL_USART_DMAPause()
333 (+) HAL_USART_DMAResume()
334 (+) HAL_USART_DMAStop()
336 (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
337 (+) HAL_USART_TxCpltCallback()
338 (+) HAL_USART_RxCpltCallback()
339 (+) HAL_USART_TxHalfCpltCallback()
340 (+) HAL_USART_RxHalfCpltCallback()
341 (+) HAL_USART_ErrorCallback()
342 (+) HAL_USART_TxRxCpltCallback()
349 * @brief Simplex Send an amount of data in blocking mode
350 * @param husart: USART handle
351 * @param pTxData: pointer to data buffer
352 * @param Size: amount of data to be sent
353 * @param Timeout : Timeout duration
356 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
360 if(husart->State == HAL_USART_STATE_READY)
362 if((pTxData == HAL_NULL) || (Size == 0))
370 husart->ErrorCode = HAL_USART_ERROR_NONE;
371 husart->State = HAL_USART_STATE_BUSY_TX;
373 husart->TxXferSize = Size;
374 husart->TxXferCount = Size;
376 /* Check the remaining data to be sent */
377 while(husart->TxXferCount > 0)
379 husart->TxXferCount--;
380 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
384 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
386 tmp = (uint16_t*) pTxData;
387 husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
392 husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFF);
396 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
401 husart->State = HAL_USART_STATE_READY;
403 /* Process Unlocked */
404 __HAL_UNLOCK(husart);
415 * @brief Receive an amount of data in blocking mode
416 * To receive synchronous data, dummy data are simultaneously transmitted
417 * @param husart: USART handle
418 * @param pRxData: pointer to data buffer
419 * @param Size: amount of data to be received
420 * @param Timeout : Timeout duration
423 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
428 if(husart->State == HAL_USART_STATE_READY)
430 if((pRxData == HAL_NULL) || (Size == 0))
437 husart->ErrorCode = HAL_USART_ERROR_NONE;
438 husart->State = HAL_USART_STATE_BUSY_RX;
440 husart->RxXferSize = Size;
441 husart->RxXferCount = Size;
443 /* Computation of USART mask to apply to RDR register */
444 __HAL_USART_MASK_COMPUTATION(husart);
445 uhMask = husart->Mask;
447 /* as long as data have to be received */
448 while(husart->RxXferCount > 0)
450 husart->RxXferCount--;
452 /* Wait until TC flag is set to send dummy byte in order to generate the
453 * clock for the slave to send data.
454 * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
455 * can be written for all the cases. */
456 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
460 husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x0FF);
462 /* Wait for RXNE Flag */
463 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
468 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
470 tmp = (uint16_t*) pRxData ;
471 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
476 *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
480 husart->State = HAL_USART_STATE_READY;
482 /* Process Unlocked */
483 __HAL_UNLOCK(husart);
494 * @brief Full-Duplex Send and Receive an amount of data in blocking mode
495 * @param husart: USART handle
496 * @param pTxData: pointer to TX data buffer
497 * @param pRxData: pointer to RX data buffer
498 * @param Size: amount of data to be sent (same amount to be received)
499 * @param Timeout : Timeout duration
502 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
507 if(husart->State == HAL_USART_STATE_READY)
509 if((pTxData == HAL_NULL) || (pRxData == HAL_NULL) || (Size == 0))
516 husart->ErrorCode = HAL_USART_ERROR_NONE;
517 husart->State = HAL_USART_STATE_BUSY_RX;
519 husart->RxXferSize = Size;
520 husart->TxXferSize = Size;
521 husart->TxXferCount = Size;
522 husart->RxXferCount = Size;
524 /* Computation of USART mask to apply to RDR register */
525 __HAL_USART_MASK_COMPUTATION(husart);
526 uhMask = husart->Mask;
528 /* Check the remain data to be sent */
529 while(husart->TxXferCount > 0)
531 husart->TxXferCount--;
532 husart->RxXferCount--;
534 /* Wait until TC flag is set to send data */
535 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
539 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
541 tmp = (uint16_t*) pTxData;
542 husart->Instance->TDR = (*tmp & uhMask);
547 husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
550 /* Wait for RXNE Flag */
551 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
556 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
558 tmp = (uint16_t*) pRxData ;
559 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
564 *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
568 husart->State = HAL_USART_STATE_READY;
570 /* Process Unlocked */
571 __HAL_UNLOCK(husart);
582 * @brief Send an amount of data in interrupt mode
583 * @param husart: USART handle
584 * @param pTxData: pointer to data buffer
585 * @param Size: amount of data to be sent
588 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
590 if(husart->State == HAL_USART_STATE_READY)
592 if((pTxData == HAL_NULL) || (Size == 0))
600 husart->pTxBuffPtr = pTxData;
601 husart->TxXferSize = Size;
602 husart->TxXferCount = Size;
604 husart->ErrorCode = HAL_USART_ERROR_NONE;
605 husart->State = HAL_USART_STATE_BUSY_TX;
607 /* The USART Error Interrupts: (Frame error, noise error, overrun error)
608 are not managed by the USART Transmit Process to avoid the overrun interrupt
609 when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
610 to benefit for the frame error and noise interrupts the usart mode should be
611 configured only for transmit "USART_MODE_TX" */
613 /* Process Unlocked */
614 __HAL_UNLOCK(husart);
616 /* Enable the USART Transmit Data Register Empty Interrupt */
617 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
628 * @brief Receive an amount of data in blocking mode
629 * To receive synchronous data, dummy data are simultaneously transmitted
630 * @param husart: usart handle
631 * @param pRxData: pointer to data buffer
632 * @param Size: amount of data to be received
635 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
637 if(husart->State == HAL_USART_STATE_READY)
639 if((pRxData == HAL_NULL) || (Size == 0))
646 husart->pRxBuffPtr = pRxData;
647 husart->RxXferSize = Size;
648 husart->RxXferCount = Size;
650 __HAL_USART_MASK_COMPUTATION(husart);
652 husart->ErrorCode = HAL_USART_ERROR_NONE;
653 husart->State = HAL_USART_STATE_BUSY_RX;
655 /* Enable the USART Parity Error Interrupt */
656 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
658 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
659 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
661 /* Enable the USART Data Register not empty Interrupt */
662 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
664 /* Process Unlocked */
665 __HAL_UNLOCK(husart);
668 /* Send dummy byte in order to generate the clock for the Slave to send the next data */
669 if(husart->Init.WordLength == USART_WORDLENGTH_9B)
671 husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x01FF);
675 husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FF);
687 * @brief Full-Duplex Send and Receive an amount of data in interrupt mode
688 * @param husart: USART handle
689 * @param pTxData: pointer to TX data buffer
690 * @param pRxData: pointer to RX data buffer
691 * @param Size: amount of data to be sent (same amount to be received)
694 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
697 if(husart->State == HAL_USART_STATE_READY)
699 if((pTxData == HAL_NULL) || (pRxData == HAL_NULL) || (Size == 0))
706 husart->pRxBuffPtr = pRxData;
707 husart->RxXferSize = Size;
708 husart->RxXferCount = Size;
709 husart->pTxBuffPtr = pTxData;
710 husart->TxXferSize = Size;
711 husart->TxXferCount = Size;
713 /* Computation of USART mask to apply to RDR register */
714 __HAL_USART_MASK_COMPUTATION(husart);
716 husart->ErrorCode = HAL_USART_ERROR_NONE;
717 husart->State = HAL_USART_STATE_BUSY_TX_RX;
719 /* Enable the USART Data Register not empty Interrupt */
720 __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
722 /* Enable the USART Parity Error Interrupt */
723 __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
725 /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
726 __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
728 /* Process Unlocked */
729 __HAL_UNLOCK(husart);
731 /* Enable the USART Transmit Data Register Empty Interrupt */
732 __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
744 * @brief Send an amount of data in DMA mode
745 * @param husart: USART handle
746 * @param pTxData: pointer to data buffer
747 * @param Size: amount of data to be sent
750 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
754 if(husart->State == HAL_USART_STATE_READY)
756 if((pTxData == HAL_NULL) || (Size == 0))
763 husart->pTxBuffPtr = pTxData;
764 husart->TxXferSize = Size;
765 husart->TxXferCount = Size;
767 husart->ErrorCode = HAL_USART_ERROR_NONE;
768 husart->State = HAL_USART_STATE_BUSY_TX;
770 /* Set the USART DMA transfer complete callback */
771 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
773 /* Set the USART DMA Half transfer complete callback */
774 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
776 /* Set the DMA error callback */
777 husart->hdmatx->XferErrorCallback = USART_DMAError;
779 /* Enable the USART transmit DMA channel */
780 tmp = (uint32_t*)&pTxData;
781 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
783 /* Enable the DMA transfer for transmit request by setting the DMAT bit
784 in the USART CR3 register */
785 husart->Instance->CR3 |= USART_CR3_DMAT;
787 /* Process Unlocked */
788 __HAL_UNLOCK(husart);
799 * @brief Receive an amount of data in DMA mode
800 * @param husart: USART handle
801 * @param pRxData: pointer to data buffer
802 * @param Size: amount of data to be received
803 * @note When the USART parity is enabled (PCE = 1), the received data contain
804 * the parity bit (MSB position)
806 * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
808 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
812 if(husart->State == HAL_USART_STATE_READY)
814 if((pRxData == HAL_NULL) || (Size == 0))
822 husart->pRxBuffPtr = pRxData;
823 husart->RxXferSize = Size;
824 husart->pTxBuffPtr = pRxData;
825 husart->TxXferSize = Size;
827 husart->ErrorCode = HAL_USART_ERROR_NONE;
828 husart->State = HAL_USART_STATE_BUSY_RX;
830 /* Set the USART DMA Rx transfer complete callback */
831 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
833 /* Set the USART DMA Half transfer complete callback */
834 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
836 /* Set the USART DMA Rx transfer error callback */
837 husart->hdmarx->XferErrorCallback = USART_DMAError;
839 /* Enable the USART receive DMA channel */
840 tmp = (uint32_t*)&pRxData;
841 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
843 /* Enable the USART transmit DMA channel: the transmit channel is used in order
844 to generate in the non-blocking mode the clock to the slave device,
845 this mode isn't a simplex receive mode but a full-duplex receive mode */
846 tmp = (uint32_t*)&pRxData;
847 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
849 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
850 in the USART CR3 register */
851 husart->Instance->CR3 |= USART_CR3_DMAR;
853 /* Enable the DMA transfer for transmit request by setting the DMAT bit
854 in the USART CR3 register */
855 husart->Instance->CR3 |= USART_CR3_DMAT;
857 /* Process Unlocked */
858 __HAL_UNLOCK(husart);
869 * @brief Full-Duplex Transmit Receive an amount of data in non blocking mode
870 * @param husart: usart handle
871 * @param pTxData: pointer to TX data buffer
872 * @param pRxData: pointer to RX data buffer
873 * @param Size: amount of data to be received/sent
874 * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
877 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
881 if(husart->State == HAL_USART_STATE_READY)
883 if((pTxData == HAL_NULL) || (pRxData == HAL_NULL) || (Size == 0))
890 husart->pRxBuffPtr = pRxData;
891 husart->RxXferSize = Size;
892 husart->pTxBuffPtr = pTxData;
893 husart->TxXferSize = Size;
895 husart->ErrorCode = HAL_USART_ERROR_NONE;
896 husart->State = HAL_USART_STATE_BUSY_TX_RX;
898 /* Set the USART DMA Rx transfer complete callback */
899 husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
901 /* Set the USART DMA Half transfer complete callback */
902 husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
904 /* Set the USART DMA Tx transfer complete callback */
905 husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
907 /* Set the USART DMA Half transfer complete callback */
908 husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
910 /* Set the USART DMA Tx transfer error callback */
911 husart->hdmatx->XferErrorCallback = USART_DMAError;
913 /* Set the USART DMA Rx transfer error callback */
914 husart->hdmarx->XferErrorCallback = USART_DMAError;
916 /* Enable the USART receive DMA channel */
917 tmp = (uint32_t*)&pRxData;
918 HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
920 /* Enable the USART transmit DMA channel */
921 tmp = (uint32_t*)&pTxData;
922 HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
924 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
925 in the USART CR3 register */
926 husart->Instance->CR3 |= USART_CR3_DMAR;
928 /* Enable the DMA transfer for transmit request by setting the DMAT bit
929 in the USART CR3 register */
930 husart->Instance->CR3 |= USART_CR3_DMAT;
932 /* Process Unlocked */
933 __HAL_UNLOCK(husart);
944 * @brief Pauses the DMA Transfer.
945 * @param husart: USART handle
948 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
953 if(husart->State == HAL_USART_STATE_BUSY_TX)
955 /* Disable the USART DMA Tx request */
956 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
958 else if(husart->State == HAL_USART_STATE_BUSY_RX)
960 /* Disable the USART DMA Rx request */
961 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
963 else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
965 /* Disable the USART DMA Tx request */
966 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
967 /* Disable the USART DMA Rx request */
968 husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
971 /* Process Unlocked */
972 __HAL_UNLOCK(husart);
978 * @brief Resumes the DMA Transfer.
979 * @param husart: USART handle
982 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
987 if(husart->State == HAL_USART_STATE_BUSY_TX)
989 /* Enable the USART DMA Tx request */
990 husart->Instance->CR3 |= USART_CR3_DMAT;
992 else if(husart->State == HAL_USART_STATE_BUSY_RX)
994 /* Enable the USART DMA Rx request */
995 husart->Instance->CR3 |= USART_CR3_DMAR;
997 else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
999 /* Enable the USART DMA Rx request before the DMA Tx request */
1000 husart->Instance->CR3 |= USART_CR3_DMAR;
1001 /* Enable the USART DMA Tx request */
1002 husart->Instance->CR3 |= USART_CR3_DMAT;
1005 /* If the USART peripheral is still not enabled, enable it */
1006 if ((husart->Instance->CR1 & USART_CR1_UE) == 0)
1008 /* Enable USART peripheral */
1009 __HAL_USART_ENABLE(husart);
1012 /* Process Unlocked */
1013 __HAL_UNLOCK(husart);
1019 * @brief Stops the DMA Transfer.
1020 * @param husart: USART handle
1023 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
1025 /* Process Locked */
1028 /* Disable the USART Tx/Rx DMA requests */
1029 husart->Instance->CR3 &= ~USART_CR3_DMAT;
1030 husart->Instance->CR3 &= ~USART_CR3_DMAR;
1032 /* Abort the USART DMA tx channel */
1033 if(husart->hdmatx != HAL_NULL)
1035 HAL_DMA_Abort(husart->hdmatx);
1037 /* Abort the USART DMA rx channel */
1038 if(husart->hdmarx != HAL_NULL)
1040 HAL_DMA_Abort(husart->hdmarx);
1043 /* Disable USART peripheral */
1044 __HAL_USART_DISABLE(husart);
1046 husart->State = HAL_USART_STATE_READY;
1048 /* Process Unlocked */
1049 __HAL_UNLOCK(husart);
1055 * @brief This function handles USART interrupt request.
1056 * @param husart: USART handle
1059 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
1062 /* USART parity error interrupt occured ------------------------------------*/
1063 if((__HAL_USART_GET_IT(husart, USART_IT_PE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE) != RESET))
1065 __HAL_USART_CLEAR_IT(husart, USART_IT_PE);
1066 husart->ErrorCode |= HAL_USART_ERROR_PE;
1067 /* Set the USART state ready to be able to start again the process */
1068 husart->State = HAL_USART_STATE_READY;
1071 /* USART frame error interrupt occured -------------------------------------*/
1072 if((__HAL_USART_GET_IT(husart, USART_IT_FE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
1074 __HAL_USART_CLEAR_IT(husart, USART_IT_FE);
1075 husart->ErrorCode |= HAL_USART_ERROR_FE;
1076 /* Set the USART state ready to be able to start again the process */
1077 husart->State = HAL_USART_STATE_READY;
1080 /* USART noise error interrupt occured -------------------------------------*/
1081 if((__HAL_USART_GET_IT(husart, USART_IT_NE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
1083 __HAL_USART_CLEAR_IT(husart, USART_IT_NE);
1084 husart->ErrorCode |= HAL_USART_ERROR_NE;
1085 /* Set the USART state ready to be able to start again the process */
1086 husart->State = HAL_USART_STATE_READY;
1089 /* USART Over-Run interrupt occured ----------------------------------------*/
1090 if((__HAL_USART_GET_IT(husart, USART_IT_ORE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
1092 __HAL_USART_CLEAR_IT(husart, USART_IT_ORE);
1093 husart->ErrorCode |= HAL_USART_ERROR_ORE;
1094 /* Set the USART state ready to be able to start again the process */
1095 husart->State = HAL_USART_STATE_READY;
1098 /* Call USART Error Call back function if need be --------------------------*/
1099 if(husart->ErrorCode != HAL_USART_ERROR_NONE)
1101 HAL_USART_ErrorCallback(husart);
1104 /* USART in mode Receiver --------------------------------------------------*/
1105 if((__HAL_USART_GET_IT(husart, USART_IT_RXNE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE) != RESET))
1107 if(husart->State == HAL_USART_STATE_BUSY_RX)
1109 USART_Receive_IT(husart);
1113 USART_TransmitReceive_IT(husart);
1117 /* USART in mode Transmitter -----------------------------------------------*/
1118 if((__HAL_USART_GET_IT(husart, USART_IT_TXE) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE) != RESET))
1120 if(husart->State == HAL_USART_STATE_BUSY_TX)
1122 USART_Transmit_IT(husart);
1126 USART_TransmitReceive_IT(husart);
1130 /* USART in mode Transmitter (transmission end) -----------------------------*/
1131 if((__HAL_USART_GET_IT(husart, USART_IT_TC) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TC) != RESET))
1133 USART_EndTransmit_IT(husart);
1140 * @brief Tx Transfer completed callbacks
1141 * @param husart: usart handle
1144 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
1146 /* NOTE : This function should not be modified, when the callback is needed,
1147 the HAL_USART_TxCpltCallback can be implemented in the user file
1152 * @brief Tx Half Transfer completed callbacks.
1153 * @param husart: USART handle
1156 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
1158 /* NOTE: This function should not be modified, when the callback is needed,
1159 the HAL_USART_TxHalfCpltCallback can be implemented in the user file
1164 * @brief Rx Transfer completed callbacks.
1165 * @param husart: USART handle
1168 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
1170 /* NOTE: This function should not be modified, when the callback is needed,
1171 the HAL_USART_RxCpltCallback can be implemented in the user file
1176 * @brief Rx Half Transfer completed callbacks
1177 * @param husart: usart handle
1180 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
1182 /* NOTE : This function should not be modified, when the callback is needed,
1183 the HAL_USART_RxHalfCpltCallback can be implemented in the user file
1188 * @brief Tx/Rx Transfers completed callback for the non-blocking process
1189 * @param husart: usart handle
1192 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
1194 /* NOTE : This function should not be modified, when the callback is needed,
1195 the HAL_USART_TxRxCpltCallback can be implemented in the user file
1200 * @brief USART error callbacks
1201 * @param husart: usart handle
1204 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
1206 /* NOTE : This function should not be modified, when the callback is needed,
1207 the HAL_USART_ErrorCallback can be implemented in the user file
1215 /** @defgroup USART_Exported_Functions_Group3 Peripheral Control functions
1216 * @brief USART control functions
1219 ===============================================================================
1220 ##### Peripheral Control functions #####
1221 ===============================================================================
1223 This subsection provides a set of functions allowing to control the USART.
1224 (+) HAL_USART_GetState() API can be helpful to check in run-time the state of the USART peripheral.
1225 (+) USART_CheckIdleState() APi ensures that TEACK and/or REACK bits are set after initialization
1234 * @brief return the USART state
1235 * @param husart: USART handle
1238 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
1240 return husart->State;
1244 * @brief Return the USART error code
1245 * @param husart : pointer to a USART_HandleTypeDef structure that contains
1246 * the configuration information for the specified USART.
1247 * @retval USART Error Code
1249 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
1251 return husart->ErrorCode;
1262 /** @defgroup USART_Private_Functions USART Private Functions
1267 * @brief DMA USART transmit process complete callback
1268 * @param hdma : DMA handle
1271 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1273 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1275 husart->TxXferCount = 0;
1277 if(husart->State == HAL_USART_STATE_BUSY_TX)
1279 /* Wait for USART TC Flag */
1280 if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, USART_TXDMA_TIMEOUTVALUE) != HAL_OK)
1282 /* Timeout Occured */
1283 HAL_USART_ErrorCallback(husart);
1288 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1289 in the USART CR3 register */
1290 husart->Instance->CR3 &= ~(USART_CR3_DMAT);
1291 husart->State= HAL_USART_STATE_READY;
1294 /* the usart state is HAL_USART_STATE_BUSY_TX_RX*/
1297 husart->State= HAL_USART_STATE_BUSY_RX;
1298 HAL_USART_TxCpltCallback(husart);
1304 * @brief DMA USART transmit process half complete callback
1305 * @param hdma : DMA handle
1308 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1310 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1312 HAL_USART_TxHalfCpltCallback(husart);
1316 * @brief DMA USART receive process complete callback
1317 * @param hdma : DMA handle
1320 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1322 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1324 husart->RxXferCount = 0;
1326 /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
1327 in USART CR3 register */
1328 husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
1329 /* similarly, disable the DMA TX transfer that was started to provide the
1330 clock to the slave device */
1331 husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
1333 husart->State= HAL_USART_STATE_READY;
1335 HAL_USART_RxCpltCallback(husart);
1339 * @brief DMA USART receive process half complete callback
1340 * @param hdma : DMA handle
1343 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1345 USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1347 HAL_USART_RxHalfCpltCallback(husart);
1351 * @brief DMA USART communication error callback
1352 * @param hdma : DMA handle
1355 static void USART_DMAError(DMA_HandleTypeDef *hdma)
1357 USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1359 husart->RxXferCount = 0;
1360 husart->TxXferCount = 0;
1361 husart->ErrorCode |= HAL_USART_ERROR_DMA;
1362 husart->State= HAL_USART_STATE_READY;
1364 HAL_USART_ErrorCallback(husart);
1368 * @brief This function handles USART Communication Timeout.
1369 * @param husart: USART handle
1370 * @param Flag: specifies the USART flag to check.
1371 * @param Status: The new Flag status (SET or RESET).
1372 * @param Timeout: Timeout duration
1373 * @retval HAL status
1375 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1377 uint32_t tickstart = HAL_GetTick();
1379 /* Wait until flag is set */
1382 while(__HAL_USART_GET_FLAG(husart, Flag) == RESET)
1384 /* Check for the Timeout */
1385 if(Timeout != HAL_MAX_DELAY)
1387 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1389 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1390 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
1391 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
1392 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
1393 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1395 husart->State= HAL_USART_STATE_TIMEOUT;
1397 /* Process Unlocked */
1398 __HAL_UNLOCK(husart);
1407 while(__HAL_USART_GET_FLAG(husart, Flag) != RESET)
1409 /* Check for the Timeout */
1410 if(Timeout != HAL_MAX_DELAY)
1412 if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
1414 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1415 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
1416 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
1417 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
1418 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1420 husart->State= HAL_USART_STATE_TIMEOUT;
1422 /* Process Unlocked */
1423 __HAL_UNLOCK(husart);
1434 * @brief Configure the USART peripheral
1435 * @param husart: USART handle
1438 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
1440 uint32_t tmpreg = 0x0;
1441 USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
1442 HAL_StatusTypeDef ret = HAL_OK;
1444 /* Check the parameters */
1445 assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
1446 assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
1447 assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
1448 assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
1449 assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
1450 assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
1451 assert_param(IS_USART_PARITY(husart->Init.Parity));
1452 assert_param(IS_USART_MODE(husart->Init.Mode));
1455 /*-------------------------- USART CR1 Configuration -----------------------*/
1456 /* Clear M, PCE, PS, TE and RE bits and configure
1457 * the USART Word Length, Parity and Mode:
1458 * set the M bits according to husart->Init.WordLength value
1459 * set PCE and PS bits according to husart->Init.Parity value
1460 * set TE and RE bits according to husart->Init.Mode value */
1461 tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode;
1462 MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
1464 /*---------------------------- USART CR2 Configuration ---------------------*/
1465 /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
1466 * set CPOL bit according to husart->Init.CLKPolarity value
1467 * set CPHA bit according to husart->Init.CLKPhase value
1468 * set LBCL bit according to husart->Init.CLKLastBit value
1469 * set STOP[13:12] bits according to husart->Init.StopBits value */
1470 tmpreg = (uint32_t)(USART_CLOCK_ENABLED);
1471 tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
1472 tmpreg |= ((uint32_t)husart->Init.CLKLastBit | (uint32_t)husart->Init.StopBits);
1473 MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
1475 /*-------------------------- USART CR3 Configuration -----------------------*/
1476 /* no CR3 register configuration */
1478 /*-------------------------- USART BRR Configuration -----------------------*/
1479 __HAL_USART_GETCLOCKSOURCE(husart, clocksource);
1480 switch (clocksource)
1482 case USART_CLOCKSOURCE_PCLK1:
1483 husart->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK1Freq() / husart->Init.BaudRate);
1485 case USART_CLOCKSOURCE_PCLK2:
1486 husart->Instance->BRR = (uint16_t)(HAL_RCC_GetPCLK2Freq() / husart->Init.BaudRate);
1488 case USART_CLOCKSOURCE_HSI:
1489 husart->Instance->BRR = (uint16_t)(HSI_VALUE / husart->Init.BaudRate);
1491 case USART_CLOCKSOURCE_SYSCLK:
1492 husart->Instance->BRR = (uint16_t)(HAL_RCC_GetSysClockFreq() / husart->Init.BaudRate);
1494 case USART_CLOCKSOURCE_LSE:
1495 husart->Instance->BRR = (uint16_t)(LSE_VALUE / husart->Init.BaudRate);
1497 case USART_CLOCKSOURCE_UNDEFINED:
1509 * @brief Check the USART Idle State
1510 * @param husart: USART handle
1511 * @retval HAL status
1513 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
1515 /* Initialize the USART ErrorCode */
1516 husart->ErrorCode = HAL_USART_ERROR_NONE;
1518 /* Check if the Transmitter is enabled */
1519 if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
1521 /* Wait until TEACK flag is set */
1522 if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1524 /* Timeout Occured */
1528 /* Check if the Receiver is enabled */
1529 if((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
1531 /* Wait until REACK flag is set */
1532 if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
1534 /* Timeout Occured */
1539 /* Initialize the USART state*/
1540 husart->State= HAL_USART_STATE_READY;
1542 /* Process Unlocked */
1543 __HAL_UNLOCK(husart);
1549 * @brief Simplex Send an amount of data in non-blocking mode.
1550 * Function called under interruption only, once
1551 * interruptions have been enabled by HAL_USART_Transmit_IT()
1552 * @param husart: USART handle
1553 * @retval HAL status
1554 * @note The USART errors are not managed to avoid the overrun error.
1556 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
1560 if (husart->State == HAL_USART_STATE_BUSY_TX)
1563 if(husart->TxXferCount == 0)
1565 /* Disable the USART Transmit Complete Interrupt */
1566 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
1568 /* Enable the USART Transmit Complete Interrupt */
1569 __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
1575 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1577 tmp = (uint16_t*) husart->pTxBuffPtr;
1578 husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
1579 husart->pTxBuffPtr += 2;
1583 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
1586 husart->TxXferCount--;
1599 * @brief Wraps up transmission in non blocking mode.
1600 * @param husart: pointer to a USART_HandleTypeDef structure that contains
1601 * the configuration information for the specified USART module.
1602 * @retval HAL status
1604 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
1606 /* Disable the USART Transmit Complete Interrupt */
1607 __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
1609 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1610 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1612 husart->State = HAL_USART_STATE_READY;
1614 HAL_USART_TxCpltCallback(husart);
1621 * @brief Simplex Receive an amount of data in non-blocking mode.
1622 * Function called under interruption only, once
1623 * interruptions have been enabled by HAL_USART_Receive_IT()
1624 * @param husart: USART handle
1625 * @retval HAL status
1627 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
1630 uint16_t uhMask = husart->Mask;
1632 if(husart->State == HAL_USART_STATE_BUSY_RX)
1635 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1637 tmp = (uint16_t*) husart->pRxBuffPtr;
1638 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
1639 husart->pRxBuffPtr += 2;
1643 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
1645 /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
1646 husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FF);
1648 if(--husart->RxXferCount == 0)
1650 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
1652 /* Disable the USART Parity Error Interrupt */
1653 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
1655 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1656 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1658 husart->State = HAL_USART_STATE_READY;
1660 HAL_USART_RxCpltCallback(husart);
1674 * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
1675 * Function called under interruption only, once
1676 * interruptions have been enabled by HAL_USART_TransmitReceive_IT()
1677 * @param husart: USART handle
1678 * @retval HAL status
1680 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
1683 uint16_t uhMask = husart->Mask;
1685 if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
1688 if(husart->TxXferCount != 0x00)
1690 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
1692 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1694 tmp = (uint16_t*) husart->pTxBuffPtr;
1695 husart->Instance->TDR = (uint16_t)(*tmp & uhMask);
1696 husart->pTxBuffPtr += 2;
1700 husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)uhMask);
1702 husart->TxXferCount--;
1704 /* Check the latest data transmitted */
1705 if(husart->TxXferCount == 0)
1707 __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
1712 if(husart->RxXferCount != 0x00)
1714 if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
1716 if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1718 tmp = (uint16_t*) husart->pRxBuffPtr;
1719 *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
1720 husart->pRxBuffPtr += 2;
1724 *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
1726 husart->RxXferCount--;
1730 /* Check the latest data received */
1731 if(husart->RxXferCount == 0)
1733 __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
1735 /* Disable the USART Parity Error Interrupt */
1736 __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
1738 /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1739 __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
1741 husart->State = HAL_USART_STATE_READY;
1743 HAL_USART_TxRxCpltCallback(husart);
1760 #endif /* HAL_USART_MODULE_ENABLED */
1769 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/