2 ******************************************************************************
3 * @file stm32f4xx_hal_uart.c
4 * @author MCD Application Team
7 * @brief UART HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Errors functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The UART HAL driver can be used as follows:
22 (#) Declare a UART_HandleTypeDef handle structure.
24 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
25 (##) Enable the USARTx interface clock.
26 (##) UART pins configuration:
27 (+++) Enable the clock for the UART GPIOs.
28 (+++) Configure these UART pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
30 and HAL_UART_Receive_IT() APIs):
31 (+++) Configure the USARTx interrupt priority.
32 (+++) Enable the NVIC USART IRQ handle.
33 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
34 and HAL_UART_Receive_DMA() APIs):
35 (+++) Declare a DMA handle structure for the Tx/Rx stream.
36 (+++) Enable the DMAx interface clock.
37 (+++) Configure the declared DMA handle structure with the required
39 (+++) Configure the DMA Tx/Rx Stream.
40 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
41 (+++) Configure the priority and enable the NVIC for the transfer complete
42 interrupt on the DMA Tx/Rx Stream.
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
45 flow control and Mode(Receiver/Transmitter) in the Init structure.
47 (#) For the UART asynchronous mode, initialize the UART registers by calling
48 the HAL_UART_Init() API.
50 (#) For the UART Half duplex mode, initialize the UART registers by calling
51 the HAL_HalfDuplex_Init() API.
53 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
55 (#) For the Multi-Processor mode, initialize the UART registers by calling
56 the HAL_MultiProcessor_Init() API.
59 (@) The specific UART interrupts (Transmission complete interrupt,
60 RXNE interrupt and Error Interrupts) will be managed using the macros
61 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
65 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
66 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed
67 HAL_UART_MspInit() API.
70 Three operation modes are available within this driver :
72 *** Polling mode IO operation ***
73 =================================
75 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
76 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
78 *** Interrupt mode IO operation ***
79 ===================================
81 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
82 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
84 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
85 add his own code by customization of function pointer HAL_UART_TxCpltCallback
86 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
87 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
88 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
89 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
90 add his own code by customization of function pointer HAL_UART_RxCpltCallback
91 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
92 add his own code by customization of function pointer HAL_UART_ErrorCallback
94 *** DMA mode IO operation ***
95 ==============================
97 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
98 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
99 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
100 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
101 add his own code by customization of function pointer HAL_UART_TxCpltCallback
102 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
103 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
104 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
105 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
106 add his own code by customization of function pointer HAL_UART_RxCpltCallback
107 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
108 add his own code by customization of function pointer HAL_UART_ErrorCallback
109 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
110 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
111 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
113 *** UART HAL driver macros list ***
114 =============================================
116 Below the list of most used macros in UART HAL driver.
118 (+) __HAL_UART_ENABLE: Enable the UART peripheral
119 (+) __HAL_UART_DISABLE: Disable the UART peripheral
120 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
121 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
122 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
123 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
126 (@) You can refer to the UART HAL driver header file for more useful macros
129 ******************************************************************************
132 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
134 * Redistribution and use in source and binary forms, with or without modification,
135 * are permitted provided that the following conditions are met:
136 * 1. Redistributions of source code must retain the above copyright notice,
137 * this list of conditions and the following disclaimer.
138 * 2. Redistributions in binary form must reproduce the above copyright notice,
139 * this list of conditions and the following disclaimer in the documentation
140 * and/or other materials provided with the distribution.
141 * 3. Neither the name of STMicroelectronics nor the names of its contributors
142 * may be used to endorse or promote products derived from this software
143 * without specific prior written permission.
145 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
146 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
147 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
148 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
149 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
150 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
151 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
152 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
153 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
154 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
156 ******************************************************************************
159 /* Includes ------------------------------------------------------------------*/
160 #include "stm32f4xx_hal.h"
162 /** @addtogroup STM32F4xx_HAL_Driver
167 * @brief HAL UART module driver
170 #ifdef HAL_UART_MODULE_ENABLED
172 /* Private typedef -----------------------------------------------------------*/
173 /* Private define ------------------------------------------------------------*/
174 #define UART_TIMEOUT_VALUE 22000
175 /* Private macro -------------------------------------------------------------*/
176 /* Private variables ---------------------------------------------------------*/
177 /* Private function prototypes -----------------------------------------------*/
178 static void UART_SetConfig (UART_HandleTypeDef *huart);
179 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
180 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
181 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
182 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
183 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
184 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
185 static void UART_DMAError(DMA_HandleTypeDef *hdma);
186 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
188 /* Private functions ---------------------------------------------------------*/
190 /** @defgroup UART_Private_Functions
194 /** @defgroup UART_Group1 Initialization and de-initialization functions
195 * @brief Initialization and Configuration functions
198 ===============================================================================
199 ##### Initialization and Configuration functions #####
200 ===============================================================================
202 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
203 in asynchronous mode.
204 (+) For the asynchronous mode only these parameters can be configured:
208 (++) Parity: If the parity is enabled, then the MSB bit of the data written
209 in the data register is transmitted but is changed by the parity bit.
210 Depending on the frame length defined by the M bit (8-bits or 9-bits),
211 please refer to Reference manual for possible UART frame formats.
212 (++) Hardware flow control
213 (++) Receiver/transmitter modes
214 (++) Over Sampling Methode
216 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
217 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
218 configuration procedures (details for the procedures are available in reference manual (RM0329)).
225 * @brief Initializes the UART mode according to the specified parameters in
226 * the UART_InitTypeDef and create the associated handle.
227 * @param huart: pointer to a UART_HandleTypeDef structure that contains
228 * the configuration information for the specified UART module.
231 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
233 /* Check the UART handle allocation */
234 if(huart == HAL_NULL)
239 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
241 /* Check the parameters */
242 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
246 /* Check the parameters */
247 assert_param(IS_UART_INSTANCE(huart->Instance));
250 if(huart->State == HAL_UART_STATE_RESET)
252 /* Init the low level hardware */
253 HAL_UART_MspInit(huart);
256 huart->State = HAL_UART_STATE_BUSY;
258 /* Disable the peripheral */
259 __HAL_UART_DISABLE(huart);
261 /* Set the UART Communication parameters */
262 UART_SetConfig(huart);
264 /* In asynchronous mode, the following bits must be kept cleared:
265 - LINEN and CLKEN bits in the USART_CR2 register,
266 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
267 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
268 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
270 /* Enable the peripheral */
271 __HAL_UART_ENABLE(huart);
273 /* Initialize the UART state */
274 huart->ErrorCode = HAL_UART_ERROR_NONE;
275 huart->State= HAL_UART_STATE_READY;
281 * @brief Initializes the half-duplex mode according to the specified
282 * parameters in the UART_InitTypeDef and create the associated handle.
283 * @param huart: pointer to a UART_HandleTypeDef structure that contains
284 * the configuration information for the specified UART module.
287 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
289 /* Check the UART handle allocation */
290 if(huart == HAL_NULL)
295 if(huart->State == HAL_UART_STATE_RESET)
297 /* Init the low level hardware */
298 HAL_UART_MspInit(huart);
301 huart->State = HAL_UART_STATE_BUSY;
303 /* Disable the peripheral */
304 __HAL_UART_DISABLE(huart);
306 /* Set the UART Communication parameters */
307 UART_SetConfig(huart);
309 /* In half-duplex mode, the following bits must be kept cleared:
310 - LINEN and CLKEN bits in the USART_CR2 register,
311 - SCEN and IREN bits in the USART_CR3 register.*/
312 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
313 huart->Instance->CR3 &= ~(USART_CR3_IREN | USART_CR3_SCEN);
315 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
316 huart->Instance->CR3 |= USART_CR3_HDSEL;
318 /* Enable the peripheral */
319 __HAL_UART_ENABLE(huart);
321 /* Initialize the UART state*/
322 huart->ErrorCode = HAL_UART_ERROR_NONE;
323 huart->State= HAL_UART_STATE_READY;
329 * @brief Initializes the LIN mode according to the specified
330 * parameters in the UART_InitTypeDef and create the associated handle.
331 * @param huart: pointer to a UART_HandleTypeDef structure that contains
332 * the configuration information for the specified UART module.
333 * @param BreakDetectLength: Specifies the LIN break detection length.
334 * This parameter can be one of the following values:
335 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
336 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
339 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
341 /* Check the UART handle allocation */
342 if(huart == HAL_NULL)
346 /* Check the Break detection length parameter */
347 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
349 if(huart->State == HAL_UART_STATE_RESET)
351 /* Init the low level hardware */
352 HAL_UART_MspInit(huart);
355 huart->State = HAL_UART_STATE_BUSY;
357 /* Disable the peripheral */
358 __HAL_UART_DISABLE(huart);
360 /* Set the UART Communication parameters */
361 UART_SetConfig(huart);
363 /* In LIN mode, the following bits must be kept cleared:
364 - LINEN and CLKEN bits in the USART_CR2 register,
365 - SCEN and IREN bits in the USART_CR3 register.*/
366 huart->Instance->CR2 &= ~(USART_CR2_CLKEN);
367 huart->Instance->CR3 &= ~(USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN);
369 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
370 huart->Instance->CR2 |= USART_CR2_LINEN;
372 /* Set the USART LIN Break detection length. */
373 huart->Instance->CR2 &= ~(USART_CR2_LBDL);
374 huart->Instance->CR2 |= BreakDetectLength;
376 /* Enable the peripheral */
377 __HAL_UART_ENABLE(huart);
379 /* Initialize the UART state*/
380 huart->ErrorCode = HAL_UART_ERROR_NONE;
381 huart->State= HAL_UART_STATE_READY;
387 * @brief Initializes the Multi-Processor mode according to the specified
388 * parameters in the UART_InitTypeDef and create the associated handle.
389 * @param huart: pointer to a UART_HandleTypeDef structure that contains
390 * the configuration information for the specified UART module.
391 * @param Address: USART address
392 * @param WakeUpMethode: specifies the USART wakeup method.
393 * This parameter can be one of the following values:
394 * @arg UART_WAKEUPMETHODE_IDLELINE: Wakeup by an idle line detection
395 * @arg UART_WAKEUPMETHODE_ADDRESSMARK: Wakeup by an address mark
398 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethode)
400 /* Check the UART handle allocation */
401 if(huart == HAL_NULL)
406 /* Check the Address & wake up method parameters */
407 assert_param(IS_UART_WAKEUPMETHODE(WakeUpMethode));
408 assert_param(IS_UART_ADDRESS(Address));
410 if(huart->State == HAL_UART_STATE_RESET)
412 /* Init the low level hardware */
413 HAL_UART_MspInit(huart);
416 huart->State = HAL_UART_STATE_BUSY;
418 /* Disable the peripheral */
419 __HAL_UART_DISABLE(huart);
421 /* Set the UART Communication parameters */
422 UART_SetConfig(huart);
424 /* In Multi-Processor mode, the following bits must be kept cleared:
425 - LINEN and CLKEN bits in the USART_CR2 register,
426 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
427 huart->Instance->CR2 &= ~(USART_CR2_LINEN | USART_CR2_CLKEN);
428 huart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
430 /* Clear the USART address */
431 huart->Instance->CR2 &= ~(USART_CR2_ADD);
432 /* Set the USART address node */
433 huart->Instance->CR2 |= Address;
435 /* Set the wake up methode by setting the WAKE bit in the CR1 register */
436 huart->Instance->CR1 &= ~(USART_CR1_WAKE);
437 huart->Instance->CR1 |= WakeUpMethode;
439 /* Enable the peripheral */
440 __HAL_UART_ENABLE(huart);
442 /* Initialize the UART state */
443 huart->ErrorCode = HAL_UART_ERROR_NONE;
444 huart->State= HAL_UART_STATE_READY;
450 * @brief DeInitializes the UART peripheral.
451 * @param huart: pointer to a UART_HandleTypeDef structure that contains
452 * the configuration information for the specified UART module.
455 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
457 /* Check the UART handle allocation */
458 if(huart == HAL_NULL)
463 /* Check the parameters */
464 assert_param(IS_UART_INSTANCE(huart->Instance));
466 huart->State = HAL_UART_STATE_BUSY;
468 /* DeInit the low level hardware */
469 HAL_UART_MspDeInit(huart);
471 huart->ErrorCode = HAL_UART_ERROR_NONE;
472 huart->State = HAL_UART_STATE_RESET;
481 * @brief UART MSP Init.
482 * @param huart: pointer to a UART_HandleTypeDef structure that contains
483 * the configuration information for the specified UART module.
486 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
488 /* NOTE: This function Should not be modified, when the callback is needed,
489 the HAL_UART_MspInit could be implemented in the user file
494 * @brief UART MSP DeInit.
495 * @param huart: pointer to a UART_HandleTypeDef structure that contains
496 * the configuration information for the specified UART module.
499 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
501 /* NOTE: This function Should not be modified, when the callback is needed,
502 the HAL_UART_MspDeInit could be implemented in the user file
510 /** @defgroup UART_Group2 IO operation functions
511 * @brief UART Transmit and Receive functions
514 ==============================================================================
515 ##### IO operation functions #####
516 ==============================================================================
518 This subsection provides a set of functions allowing to manage the UART asynchronous
519 and Half duplex data transfers.
521 (#) There are two modes of transfer:
522 (++) Blocking mode: The communication is performed in polling mode.
523 The HAL status of all data processing is returned by the same function
524 after finishing transfer.
525 (++) Non blocking mode: The communication is performed using Interrupts
526 or DMA, these APIs return the HAL status.
527 The end of the data processing will be indicated through the
528 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
530 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
531 will be executed respectivelly at the end of the transmit or receive process.
532 The HAL_UART_ErrorCallback() user callback will be executed when
533 a communication error is detected.
535 (#) Blocking mode APIs are:
536 (++) HAL_UART_Transmit()
537 (++) HAL_UART_Receive()
539 (#) Non Blocking mode APIs with Interrupt are:
540 (++) HAL_UART_Transmit_IT()
541 (++) HAL_UART_Receive_IT()
542 (++) HAL_UART_IRQHandler()
544 (#) Non Blocking mode functions with DMA are:
545 (++) HAL_UART_Transmit_DMA()
546 (++) HAL_UART_Receive_DMA()
548 (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
549 (++) HAL_UART_TxCpltCallback()
550 (++) HAL_UART_RxCpltCallback()
551 (++) HAL_UART_ErrorCallback()
554 (@) In the Half duplex communication, it is forbidden to run the transmit
555 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
563 * @brief Sends an amount of data in blocking mode.
564 * @param huart: pointer to a UART_HandleTypeDef structure that contains
565 * the configuration information for the specified UART module.
566 * @param pData: Pointer to data buffer
567 * @param Size: Amount of data to be sent
568 * @param Timeout: Timeout duration
571 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
577 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
579 if((pData == HAL_NULL ) || (Size == 0))
587 huart->ErrorCode = HAL_UART_ERROR_NONE;
588 /* Check if a non-blocking receive process is ongoing or not */
589 if(huart->State == HAL_UART_STATE_BUSY_RX)
591 huart->State = HAL_UART_STATE_BUSY_TX_RX;
595 huart->State = HAL_UART_STATE_BUSY_TX;
598 huart->TxXferSize = Size;
599 huart->TxXferCount = Size;
600 while(huart->TxXferCount > 0)
602 huart->TxXferCount--;
603 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
605 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
609 tmp = (uint16_t*) pData;
610 huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
611 if(huart->Init.Parity == UART_PARITY_NONE)
622 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
626 huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
630 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
635 /* Check if a non-blocking receive process is ongoing or not */
636 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
638 huart->State = HAL_UART_STATE_BUSY_RX;
642 huart->State = HAL_UART_STATE_READY;
645 /* Process Unlocked */
657 * @brief Receives an amount of data in blocking mode.
658 * @param huart: pointer to a UART_HandleTypeDef structure that contains
659 * the configuration information for the specified UART module.
660 * @param pData: Pointer to data buffer
661 * @param Size: Amount of data to be received
662 * @param Timeout: Timeout duration
665 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
671 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
673 if((pData == HAL_NULL ) || (Size == 0))
681 huart->ErrorCode = HAL_UART_ERROR_NONE;
682 /* Check if a non-blocking transmit process is ongoing or not */
683 if(huart->State == HAL_UART_STATE_BUSY_TX)
685 huart->State = HAL_UART_STATE_BUSY_TX_RX;
689 huart->State = HAL_UART_STATE_BUSY_RX;
692 huart->RxXferSize = Size;
693 huart->RxXferCount = Size;
695 /* Check the remain data to be received */
696 while(huart->RxXferCount > 0)
698 huart->RxXferCount--;
699 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
701 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
705 tmp = (uint16_t*) pData ;
706 if(huart->Init.Parity == UART_PARITY_NONE)
708 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
713 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
720 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
724 if(huart->Init.Parity == UART_PARITY_NONE)
726 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
730 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
736 /* Check if a non-blocking transmit process is ongoing or not */
737 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
739 huart->State = HAL_UART_STATE_BUSY_TX;
743 huart->State = HAL_UART_STATE_READY;
745 /* Process Unlocked */
757 * @brief Sends an amount of data in non blocking mode.
758 * @param huart: pointer to a UART_HandleTypeDef structure that contains
759 * the configuration information for the specified UART module.
760 * @param pData: Pointer to data buffer
761 * @param Size: Amount of data to be sent
764 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
769 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_RX))
771 if((pData == HAL_NULL ) || (Size == 0))
779 huart->pTxBuffPtr = pData;
780 huart->TxXferSize = Size;
781 huart->TxXferCount = Size;
783 huart->ErrorCode = HAL_UART_ERROR_NONE;
784 /* Check if a receive process is ongoing or not */
785 if(huart->State == HAL_UART_STATE_BUSY_RX)
787 huart->State = HAL_UART_STATE_BUSY_TX_RX;
791 huart->State = HAL_UART_STATE_BUSY_TX;
794 /* Enable the UART Parity Error Interrupt */
795 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
797 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
798 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
800 /* Process Unlocked */
803 /* Enable the UART Transmit data register empty Interrupt */
804 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
815 * @brief Receives an amount of data in non blocking mode
816 * @param huart: pointer to a UART_HandleTypeDef structure that contains
817 * the configuration information for the specified UART module.
818 * @param pData: Pointer to data buffer
819 * @param Size: Amount of data to be received
822 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
827 if((tmp == HAL_UART_STATE_READY) || (tmp == HAL_UART_STATE_BUSY_TX))
829 if((pData == HAL_NULL ) || (Size == 0))
837 huart->pRxBuffPtr = pData;
838 huart->RxXferSize = Size;
839 huart->RxXferCount = Size;
841 huart->ErrorCode = HAL_UART_ERROR_NONE;
842 /* Check if a transmit process is ongoing or not */
843 if(huart->State == HAL_UART_STATE_BUSY_TX)
845 huart->State = HAL_UART_STATE_BUSY_TX_RX;
849 huart->State = HAL_UART_STATE_BUSY_RX;
852 /* Enable the UART Parity Error Interrupt */
853 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
855 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
856 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
858 /* Process Unlocked */
861 /* Enable the UART Data Register not empty Interrupt */
862 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
873 * @brief Sends an amount of data in non blocking mode.
874 * @param huart: pointer to a UART_HandleTypeDef structure that contains
875 * the configuration information for the specified UART module.
876 * @param pData: Pointer to data buffer
877 * @param Size: Amount of data to be sent
880 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
886 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_RX))
888 if((pData == HAL_NULL ) || (Size == 0))
896 huart->pTxBuffPtr = pData;
897 huart->TxXferSize = Size;
898 huart->TxXferCount = Size;
900 huart->ErrorCode = HAL_UART_ERROR_NONE;
901 /* Check if a receive process is ongoing or not */
902 if(huart->State == HAL_UART_STATE_BUSY_RX)
904 huart->State = HAL_UART_STATE_BUSY_TX_RX;
908 huart->State = HAL_UART_STATE_BUSY_TX;
911 /* Set the UART DMA transfer complete callback */
912 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
914 /* Set the UART DMA Half transfer complete callback */
915 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
917 /* Set the DMA error callback */
918 huart->hdmatx->XferErrorCallback = UART_DMAError;
920 /* Enable the UART transmit DMA Stream */
921 tmp = (uint32_t*)&pData;
922 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
924 /* Enable the DMA transfer for transmit request by setting the DMAT bit
925 in the UART CR3 register */
926 huart->Instance->CR3 |= USART_CR3_DMAT;
928 /* Process Unlocked */
940 * @brief Receives an amount of data in non blocking mode.
941 * @param huart: pointer to a UART_HandleTypeDef structure that contains
942 * the configuration information for the specified UART module.
943 * @param pData: Pointer to data buffer
944 * @param Size: Amount of data to be received
945 * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
948 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
954 if((tmp1 == HAL_UART_STATE_READY) || (tmp1 == HAL_UART_STATE_BUSY_TX))
956 if((pData == HAL_NULL ) || (Size == 0))
964 huart->pRxBuffPtr = pData;
965 huart->RxXferSize = Size;
967 huart->ErrorCode = HAL_UART_ERROR_NONE;
968 /* Check if a transmit process is ongoing or not */
969 if(huart->State == HAL_UART_STATE_BUSY_TX)
971 huart->State = HAL_UART_STATE_BUSY_TX_RX;
975 huart->State = HAL_UART_STATE_BUSY_RX;
978 /* Set the UART DMA transfer complete callback */
979 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
981 /* Set the UART DMA Half transfer complete callback */
982 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
984 /* Set the DMA error callback */
985 huart->hdmarx->XferErrorCallback = UART_DMAError;
987 /* Enable the DMA Stream */
988 tmp = (uint32_t*)&pData;
989 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
991 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
992 in the UART CR3 register */
993 huart->Instance->CR3 |= USART_CR3_DMAR;
995 /* Process Unlocked */
1007 * @brief Pauses the DMA Transfer.
1008 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1009 * the configuration information for the specified UART module.
1010 * @retval HAL status
1012 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1014 /* Process Locked */
1017 if(huart->State == HAL_UART_STATE_BUSY_TX)
1019 /* Disable the UART DMA Tx request */
1020 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
1022 else if(huart->State == HAL_UART_STATE_BUSY_RX)
1024 /* Disable the UART DMA Rx request */
1025 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
1027 else if (huart->State == HAL_UART_STATE_BUSY_TX_RX)
1029 /* Disable the UART DMA Tx & Rx requests */
1030 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
1031 huart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
1034 /* Process Unlocked */
1035 __HAL_UNLOCK(huart);
1041 * @brief Resumes the DMA Transfer.
1042 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1043 * the configuration information for the specified UART module.
1044 * @retval HAL status
1046 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1048 /* Process Locked */
1051 if(huart->State == HAL_UART_STATE_BUSY_TX)
1053 /* Enable the UART DMA Tx request */
1054 huart->Instance->CR3 |= USART_CR3_DMAT;
1056 else if(huart->State == HAL_UART_STATE_BUSY_RX)
1058 /* Clear the Overrun flag before resumming the Rx transfer*/
1059 __HAL_UART_CLEAR_OREFLAG(huart);
1060 /* Enable the UART DMA Rx request */
1061 huart->Instance->CR3 |= USART_CR3_DMAR;
1063 else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1065 /* Clear the Overrun flag before resumming the Rx transfer*/
1066 __HAL_UART_CLEAR_OREFLAG(huart);
1067 /* Enable the UART DMA Tx & Rx request */
1068 huart->Instance->CR3 |= USART_CR3_DMAT;
1069 huart->Instance->CR3 |= USART_CR3_DMAR;
1072 /* Process Unlocked */
1073 __HAL_UNLOCK(huart);
1079 * @brief Stops the DMA Transfer.
1080 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1081 * the configuration information for the specified UART module.
1082 * @retval HAL status
1084 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1086 /* The Lock is not implemented on this API to allow the user application
1087 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
1088 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1089 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
1092 /* Disable the UART Tx/Rx DMA requests */
1093 huart->Instance->CR3 &= ~USART_CR3_DMAT;
1094 huart->Instance->CR3 &= ~USART_CR3_DMAR;
1096 /* Abort the UART DMA tx Stream */
1097 if(huart->hdmatx != HAL_NULL)
1099 HAL_DMA_Abort(huart->hdmatx);
1101 /* Abort the UART DMA rx Stream */
1102 if(huart->hdmarx != HAL_NULL)
1104 HAL_DMA_Abort(huart->hdmarx);
1107 huart->State = HAL_UART_STATE_READY;
1113 * @brief This function handles UART interrupt request.
1114 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1115 * the configuration information for the specified UART module.
1118 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
1120 uint32_t tmp1 = 0, tmp2 = 0;
1122 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
1123 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);
1124 /* UART parity error interrupt occurred ------------------------------------*/
1125 if((tmp1 != RESET) && (tmp2 != RESET))
1127 __HAL_UART_CLEAR_PEFLAG(huart);
1129 huart->ErrorCode |= HAL_UART_ERROR_PE;
1132 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE);
1133 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1134 /* UART frame error interrupt occurred -------------------------------------*/
1135 if((tmp1 != RESET) && (tmp2 != RESET))
1137 __HAL_UART_CLEAR_FEFLAG(huart);
1139 huart->ErrorCode |= HAL_UART_ERROR_FE;
1142 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE);
1143 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1144 /* UART noise error interrupt occurred -------------------------------------*/
1145 if((tmp1 != RESET) && (tmp2 != RESET))
1147 __HAL_UART_CLEAR_NEFLAG(huart);
1149 huart->ErrorCode |= HAL_UART_ERROR_NE;
1152 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE);
1153 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
1154 /* UART Over-Run interrupt occurred ----------------------------------------*/
1155 if((tmp1 != RESET) && (tmp2 != RESET))
1157 __HAL_UART_CLEAR_OREFLAG(huart);
1159 huart->ErrorCode |= HAL_UART_ERROR_ORE;
1162 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE);
1163 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE);
1164 /* UART in mode Receiver ---------------------------------------------------*/
1165 if((tmp1 != RESET) && (tmp2 != RESET))
1167 UART_Receive_IT(huart);
1170 tmp1 = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
1171 tmp2 = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
1172 /* UART in mode Transmitter ------------------------------------------------*/
1173 if((tmp1 != RESET) && (tmp2 != RESET))
1175 UART_Transmit_IT(huart);
1178 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1180 /* Set the UART state ready to be able to start again the process */
1181 huart->State = HAL_UART_STATE_READY;
1183 HAL_UART_ErrorCallback(huart);
1188 * @brief Tx Transfer completed callbacks.
1189 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1190 * the configuration information for the specified UART module.
1193 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1195 /* NOTE: This function Should not be modified, when the callback is needed,
1196 the HAL_UART_TxCpltCallback could be implemented in the user file
1201 * @brief Tx Half Transfer completed callbacks.
1202 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1203 * the configuration information for the specified UART module.
1206 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
1208 /* NOTE: This function Should not be modified, when the callback is needed,
1209 the HAL_UART_TxCpltCallback could be implemented in the user file
1214 * @brief Rx Transfer completed callbacks.
1215 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1216 * the configuration information for the specified UART module.
1219 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1221 /* NOTE: This function Should not be modified, when the callback is needed,
1222 the HAL_UART_TxCpltCallback could be implemented in the user file
1227 * @brief Rx Half Transfer completed callbacks.
1228 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1229 * the configuration information for the specified UART module.
1232 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
1234 /* NOTE: This function Should not be modified, when the callback is needed,
1235 the HAL_UART_TxCpltCallback could be implemented in the user file
1240 * @brief UART error callbacks.
1241 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1242 * the configuration information for the specified UART module.
1245 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1247 /* NOTE: This function Should not be modified, when the callback is needed,
1248 the HAL_UART_ErrorCallback could be implemented in the user file
1256 /** @defgroup UART_Group3 Peripheral Control functions
1257 * @brief UART control functions
1260 ==============================================================================
1261 ##### Peripheral Control functions #####
1262 ==============================================================================
1264 This subsection provides a set of functions allowing to control the UART:
1265 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
1266 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
1267 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
1274 * @brief Transmits break characters.
1275 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1276 * the configuration information for the specified UART module.
1277 * @retval HAL status
1279 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
1281 /* Check the parameters */
1282 assert_param(IS_UART_INSTANCE(huart->Instance));
1284 /* Process Locked */
1287 huart->State = HAL_UART_STATE_BUSY;
1289 /* Send break characters */
1290 huart->Instance->CR1 |= USART_CR1_SBK;
1292 huart->State = HAL_UART_STATE_READY;
1294 /* Process Unlocked */
1295 __HAL_UNLOCK(huart);
1301 * @brief Enters the UART in mute mode.
1302 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1303 * the configuration information for the specified UART module.
1304 * @retval HAL status
1306 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
1308 /* Check the parameters */
1309 assert_param(IS_UART_INSTANCE(huart->Instance));
1311 /* Process Locked */
1314 huart->State = HAL_UART_STATE_BUSY;
1316 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
1317 huart->Instance->CR1 |= USART_CR1_RWU;
1319 huart->State = HAL_UART_STATE_READY;
1321 /* Process Unlocked */
1322 __HAL_UNLOCK(huart);
1328 * @brief Exits the UART mute mode: wake up software.
1329 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1330 * the configuration information for the specified UART module.
1331 * @retval HAL status
1333 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
1335 /* Check the parameters */
1336 assert_param(IS_UART_INSTANCE(huart->Instance));
1338 /* Process Locked */
1341 huart->State = HAL_UART_STATE_BUSY;
1343 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
1344 huart->Instance->CR1 &= (uint32_t)~((uint32_t)USART_CR1_RWU);
1346 huart->State = HAL_UART_STATE_READY;
1348 /* Process Unlocked */
1349 __HAL_UNLOCK(huart);
1355 * @brief Enables the UART transmitter and disables the UART receiver.
1356 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1357 * the configuration information for the specified UART module.
1358 * @retval HAL status
1360 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
1362 uint32_t tmpreg = 0x00;
1364 /* Process Locked */
1367 huart->State = HAL_UART_STATE_BUSY;
1369 /*-------------------------- USART CR1 Configuration -----------------------*/
1370 tmpreg = huart->Instance->CR1;
1372 /* Clear TE and RE bits */
1373 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1375 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
1376 tmpreg |= (uint32_t)USART_CR1_TE;
1378 /* Write to USART CR1 */
1379 huart->Instance->CR1 = (uint32_t)tmpreg;
1381 huart->State = HAL_UART_STATE_READY;
1383 /* Process Unlocked */
1384 __HAL_UNLOCK(huart);
1390 * @brief Enables the UART receiver and disables the UART transmitter.
1391 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1392 * the configuration information for the specified UART module.
1393 * @retval HAL status
1395 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
1397 uint32_t tmpreg = 0x00;
1399 /* Process Locked */
1402 huart->State = HAL_UART_STATE_BUSY;
1404 /*-------------------------- USART CR1 Configuration -----------------------*/
1405 tmpreg = huart->Instance->CR1;
1407 /* Clear TE and RE bits */
1408 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1410 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
1411 tmpreg |= (uint32_t)USART_CR1_RE;
1413 /* Write to USART CR1 */
1414 huart->Instance->CR1 = (uint32_t)tmpreg;
1416 huart->State = HAL_UART_STATE_READY;
1418 /* Process Unlocked */
1419 __HAL_UNLOCK(huart);
1428 /** @defgroup UART_Group4 Peripheral State and Errors functions
1429 * @brief UART State and Errors functions
1432 ==============================================================================
1433 ##### Peripheral State and Errors functions #####
1434 ==============================================================================
1436 This subsection provides a set of functions allowing to return the State of
1437 UART communication process, return Peripheral Errors occurred during communication
1439 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
1440 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
1447 * @brief Returns the UART state.
1448 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1449 * the configuration information for the specified UART module.
1452 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
1454 return huart->State;
1458 * @brief Return the UART error code
1459 * @param huart : pointer to a UART_HandleTypeDef structure that contains
1460 * the configuration information for the specified UART.
1461 * @retval UART Error Code
1463 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
1465 return huart->ErrorCode;
1473 * @brief DMA UART transmit process complete callback.
1474 * @param hdma: DMA handle
1477 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1479 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1480 /* DMA Normal mode*/
1481 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
1483 huart->TxXferCount = 0;
1485 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1486 in the UART CR3 register */
1487 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
1489 /* Wait for UART TC Flag */
1490 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, UART_TIMEOUT_VALUE) != HAL_OK)
1492 /* Timeout occurred */
1493 huart->State = HAL_UART_STATE_TIMEOUT;
1494 HAL_UART_ErrorCallback(huart);
1499 /* Check if a receive process is ongoing or not */
1500 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1502 huart->State = HAL_UART_STATE_BUSY_RX;
1506 huart->State = HAL_UART_STATE_READY;
1508 HAL_UART_TxCpltCallback(huart);
1511 /* DMA Circular mode */
1514 HAL_UART_TxCpltCallback(huart);
1519 * @brief DMA UART transmit process half complete callback
1520 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1521 * the configuration information for the specified DMA module.
1524 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
1526 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1528 HAL_UART_TxHalfCpltCallback(huart);
1532 * @brief DMA UART receive process complete callback.
1533 * @param hdma: DMA handle
1536 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
1538 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1539 /* DMA Normal mode*/
1540 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
1542 huart->RxXferCount = 0;
1544 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1545 in the UART CR3 register */
1546 huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
1548 /* Check if a transmit process is ongoing or not */
1549 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1551 huart->State = HAL_UART_STATE_BUSY_TX;
1555 huart->State = HAL_UART_STATE_READY;
1558 HAL_UART_RxCpltCallback(huart);
1562 * @brief DMA UART receive process half complete callback
1563 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1564 * the configuration information for the specified DMA module.
1567 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
1569 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
1571 HAL_UART_RxHalfCpltCallback(huart);
1575 * @brief DMA UART communication error callback.
1576 * @param hdma: DMA handle
1579 static void UART_DMAError(DMA_HandleTypeDef *hdma)
1581 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1582 huart->RxXferCount = 0;
1583 huart->TxXferCount = 0;
1584 huart->State= HAL_UART_STATE_READY;
1585 huart->ErrorCode |= HAL_UART_ERROR_DMA;
1586 HAL_UART_ErrorCallback(huart);
1590 * @brief This function handles UART Communication Timeout.
1591 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1592 * the configuration information for the specified UART module.
1593 * @param Flag: specifies the UART flag to check.
1594 * @param Status: The new Flag status (SET or RESET).
1595 * @param Timeout: Timeout duration
1596 * @retval HAL status
1598 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
1600 uint32_t tickstart = 0;
1603 tickstart = HAL_GetTick();
1605 /* Wait until flag is set */
1608 while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
1610 /* Check for the Timeout */
1611 if(Timeout != HAL_MAX_DELAY)
1613 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1615 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1616 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1617 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1618 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1619 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1621 huart->State= HAL_UART_STATE_READY;
1623 /* Process Unlocked */
1624 __HAL_UNLOCK(huart);
1633 while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
1635 /* Check for the Timeout */
1636 if(Timeout != HAL_MAX_DELAY)
1638 if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
1640 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1641 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1642 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1643 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1644 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1646 huart->State= HAL_UART_STATE_READY;
1648 /* Process Unlocked */
1649 __HAL_UNLOCK(huart);
1660 * @brief Sends an amount of data in non blocking mode.
1661 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1662 * the configuration information for the specified UART module.
1663 * @retval HAL status
1665 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
1670 tmp1 = huart->State;
1671 if((tmp1 == HAL_UART_STATE_BUSY_TX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
1673 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
1675 tmp = (uint16_t*) huart->pTxBuffPtr;
1676 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
1677 if(huart->Init.Parity == UART_PARITY_NONE)
1679 huart->pTxBuffPtr += 2;
1683 huart->pTxBuffPtr += 1;
1688 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
1691 if(--huart->TxXferCount == 0)
1693 /* Disable the UART Transmit Complete Interrupt */
1694 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
1696 /* Check if a receive process is ongoing or not */
1697 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1699 huart->State = HAL_UART_STATE_BUSY_RX;
1703 /* Disable the UART Parity Error Interrupt */
1704 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1706 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1707 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1709 huart->State = HAL_UART_STATE_READY;
1712 /* Wait on TC flag to be able to start a second transfer */
1713 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, UART_TIMEOUT_VALUE) != HAL_OK)
1718 HAL_UART_TxCpltCallback(huart);
1731 * @brief Receives an amount of data in non blocking mode
1732 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1733 * the configuration information for the specified UART module.
1734 * @retval HAL status
1736 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
1741 tmp1 = huart->State;
1742 if((tmp1 == HAL_UART_STATE_BUSY_RX) || (tmp1 == HAL_UART_STATE_BUSY_TX_RX))
1744 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
1746 tmp = (uint16_t*) huart->pRxBuffPtr;
1747 if(huart->Init.Parity == UART_PARITY_NONE)
1749 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
1750 huart->pRxBuffPtr += 2;
1754 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
1755 huart->pRxBuffPtr += 1;
1760 if(huart->Init.Parity == UART_PARITY_NONE)
1762 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
1766 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
1770 if(--huart->RxXferCount == 0)
1772 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
1774 /* Check if a transmit process is ongoing or not */
1775 if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
1777 huart->State = HAL_UART_STATE_BUSY_TX;
1781 /* Disable the UART Parity Error Interrupt */
1782 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
1784 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1785 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
1787 huart->State = HAL_UART_STATE_READY;
1789 HAL_UART_RxCpltCallback(huart);
1802 * @brief Configures the UART peripheral.
1803 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1804 * the configuration information for the specified UART module.
1807 static void UART_SetConfig(UART_HandleTypeDef *huart)
1809 uint32_t tmpreg = 0x00;
1811 /* Check the parameters */
1812 assert_param(IS_UART_INSTANCE(huart->Instance));
1813 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
1814 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
1815 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
1816 assert_param(IS_UART_PARITY(huart->Init.Parity));
1817 assert_param(IS_UART_MODE(huart->Init.Mode));
1818 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
1820 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
1821 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
1823 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
1826 /*-------------------------- USART CR2 Configuration -----------------------*/
1827 tmpreg = huart->Instance->CR2;
1829 /* Clear STOP[13:12] bits */
1830 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
1832 /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */
1833 tmpreg |= (uint32_t)huart->Init.StopBits;
1835 /* Write to USART CR2 */
1836 huart->Instance->CR2 = (uint32_t)tmpreg;
1838 /*-------------------------- USART CR1 Configuration -----------------------*/
1839 tmpreg = huart->Instance->CR1;
1841 /* Clear M, PCE, PS, TE and RE bits */
1842 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
1843 USART_CR1_RE | USART_CR1_OVER8));
1845 /* Configure the UART Word Length, Parity and mode:
1846 Set the M bits according to huart->Init.WordLength value
1847 Set PCE and PS bits according to huart->Init.Parity value
1848 Set TE and RE bits according to huart->Init.Mode value
1849 Set OVER8 bit according to huart->Init.OverSampling value */
1850 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
1852 /* Write to USART CR1 */
1853 huart->Instance->CR1 = (uint32_t)tmpreg;
1855 /*-------------------------- USART CR3 Configuration -----------------------*/
1856 tmpreg = huart->Instance->CR3;
1858 /* Clear CTSE and RTSE bits */
1859 tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
1861 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
1862 tmpreg |= huart->Init.HwFlowCtl;
1864 /* Write to USART CR3 */
1865 huart->Instance->CR3 = (uint32_t)tmpreg;
1867 /* Check the Over Sampling */
1868 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
1870 /*-------------------------- USART BRR Configuration ---------------------*/
1871 if((huart->Instance == USART1) || (huart->Instance == USART6))
1873 huart->Instance->BRR = __UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
1877 huart->Instance->BRR = __UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
1882 /*-------------------------- USART BRR Configuration ---------------------*/
1883 if((huart->Instance == USART1) || (huart->Instance == USART6))
1885 huart->Instance->BRR = __UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
1889 huart->Instance->BRR = __UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
1898 #endif /* HAL_UART_MODULE_ENABLED */
1907 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/