Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal_uart.c
blob40f28ea8e21d4f7667b37d7ec0d0dee558928272
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_uart.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
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
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
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
38 Tx/Rx parameters.
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.
58 [..]
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
62 and receive process.
64 [..]
65 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
66 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
67 HAL_UART_MspInit() API.
69 [..]
70 Three operation modes are available within this driver :
72 *** Polling mode IO operation ***
73 =================================
74 [..]
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 ===================================
80 [..]
81 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
82 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
83 add his own code by customization of function pointer HAL_UART_TxCpltCallback
84 (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
85 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
86 add his own code by customization of function pointer HAL_UART_RxCpltCallback
87 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
88 add his own code by customization of function pointer HAL_UART_ErrorCallback
90 *** DMA mode IO operation ***
91 ==============================
92 [..]
93 (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
94 (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
95 add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
96 (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
97 add his own code by customization of function pointer HAL_UART_TxCpltCallback
98 (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
99 (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
100 add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
101 (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
102 add his own code by customization of function pointer HAL_UART_RxCpltCallback
103 (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
104 add his own code by customization of function pointer HAL_UART_ErrorCallback
105 (+) Pause the DMA Transfer using HAL_UART_DMAPause()
106 (+) Resume the DMA Transfer using HAL_UART_DMAResume()
107 (+) Stop the DMA Transfer using HAL_UART_DMAStop()
109 *** UART HAL driver macros list ***
110 =============================================
111 [..]
112 Below the list of most used macros in UART HAL driver.
114 (+) __HAL_UART_ENABLE: Enable the UART peripheral
115 (+) __HAL_UART_DISABLE: Disable the UART peripheral
116 (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
117 (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
118 (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
119 (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
120 (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
122 [..]
123 (@) You can refer to the UART HAL driver header file for more useful macros
125 @endverbatim
126 ******************************************************************************
127 * @attention
129 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
131 * Redistribution and use in source and binary forms, with or without modification,
132 * are permitted provided that the following conditions are met:
133 * 1. Redistributions of source code must retain the above copyright notice,
134 * this list of conditions and the following disclaimer.
135 * 2. Redistributions in binary form must reproduce the above copyright notice,
136 * this list of conditions and the following disclaimer in the documentation
137 * and/or other materials provided with the distribution.
138 * 3. Neither the name of STMicroelectronics nor the names of its contributors
139 * may be used to endorse or promote products derived from this software
140 * without specific prior written permission.
142 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
143 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
144 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
145 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
146 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
147 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
148 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
149 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
150 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
151 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
153 ******************************************************************************
156 /* Includes ------------------------------------------------------------------*/
157 #include "stm32f4xx_hal.h"
159 /** @addtogroup STM32F4xx_HAL_Driver
160 * @{
163 /** @defgroup UART UART
164 * @brief HAL UART module driver
165 * @{
167 #ifdef HAL_UART_MODULE_ENABLED
169 /* Private typedef -----------------------------------------------------------*/
170 /* Private define ------------------------------------------------------------*/
171 /** @addtogroup UART_Private_Constants
172 * @{
175 * @}
177 /* Private macro -------------------------------------------------------------*/
178 /* Private variables ---------------------------------------------------------*/
179 /* Private function prototypes -----------------------------------------------*/
180 /** @addtogroup UART_Private_Functions UART Private Functions
181 * @{
183 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
184 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
185 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
186 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
187 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
188 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
189 static void UART_DMAError(DMA_HandleTypeDef *hdma);
190 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
191 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
192 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
193 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
194 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
195 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
196 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
197 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
198 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
199 static void UART_SetConfig (UART_HandleTypeDef *huart);
201 * @}
204 /* Exported functions ---------------------------------------------------------*/
205 /** @defgroup UART_Exported_Functions UART Exported Functions
206 * @{
209 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
210 * @brief Initialization and Configuration functions
212 @verbatim
213 ===============================================================================
214 ##### Initialization and Configuration functions #####
215 ===============================================================================
216 [..]
217 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
218 in asynchronous mode.
219 (+) For the asynchronous mode only these parameters can be configured:
220 (++) Baud Rate
221 (++) Word Length
222 (++) Stop Bit
223 (++) Parity: If the parity is enabled, then the MSB bit of the data written
224 in the data register is transmitted but is changed by the parity bit.
225 Depending on the frame length defined by the M bit (8-bits or 9-bits),
226 please refer to Reference manual for possible UART frame formats.
227 (++) Hardware flow control
228 (++) Receiver/transmitter modes
229 (++) Over Sampling Method
230 [..]
231 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
232 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
233 configuration procedures (details for the procedures are available in reference manual (RM0329)).
235 @endverbatim
236 * @{
240 * @brief Initializes the UART mode according to the specified parameters in
241 * the UART_InitTypeDef and create the associated handle.
242 * @param huart: pointer to a UART_HandleTypeDef structure that contains
243 * the configuration information for the specified UART module.
244 * @retval HAL status
246 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
248 /* Check the UART handle allocation */
249 if(huart == NULL)
251 return HAL_ERROR;
254 /* Check the parameters */
255 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
257 /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
258 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
259 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
261 else
263 assert_param(IS_UART_INSTANCE(huart->Instance));
265 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
266 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
268 if(huart->gState == HAL_UART_STATE_RESET)
270 /* Allocate lock resource and initialize it */
271 huart->Lock = HAL_UNLOCKED;
272 /* Init the low level hardware */
273 HAL_UART_MspInit(huart);
276 huart->gState = HAL_UART_STATE_BUSY;
278 /* Disable the peripheral */
279 __HAL_UART_DISABLE(huart);
281 /* Set the UART Communication parameters */
282 UART_SetConfig(huart);
284 /* In asynchronous mode, the following bits must be kept cleared:
285 - LINEN and CLKEN bits in the USART_CR2 register,
286 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
287 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
288 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
290 /* Enable the peripheral */
291 __HAL_UART_ENABLE(huart);
293 /* Initialize the UART state */
294 huart->ErrorCode = HAL_UART_ERROR_NONE;
295 huart->gState= HAL_UART_STATE_READY;
296 huart->RxState= HAL_UART_STATE_READY;
298 return HAL_OK;
302 * @brief Initializes the half-duplex mode according to the specified
303 * parameters in the UART_InitTypeDef and create the associated handle.
304 * @param huart: pointer to a UART_HandleTypeDef structure that contains
305 * the configuration information for the specified UART module.
306 * @retval HAL status
308 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
310 /* Check the UART handle allocation */
311 if(huart == NULL)
313 return HAL_ERROR;
316 /* Check the parameters */
317 assert_param(IS_UART_INSTANCE(huart->Instance));
318 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
319 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
321 if(huart->gState == HAL_UART_STATE_RESET)
323 /* Allocate lock resource and initialize it */
324 huart->Lock = HAL_UNLOCKED;
325 /* Init the low level hardware */
326 HAL_UART_MspInit(huart);
329 huart->gState = HAL_UART_STATE_BUSY;
331 /* Disable the peripheral */
332 __HAL_UART_DISABLE(huart);
334 /* Set the UART Communication parameters */
335 UART_SetConfig(huart);
337 /* In half-duplex mode, the following bits must be kept cleared:
338 - LINEN and CLKEN bits in the USART_CR2 register,
339 - SCEN and IREN bits in the USART_CR3 register.*/
340 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
341 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
343 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
344 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
346 /* Enable the peripheral */
347 __HAL_UART_ENABLE(huart);
349 /* Initialize the UART state*/
350 huart->ErrorCode = HAL_UART_ERROR_NONE;
351 huart->gState= HAL_UART_STATE_READY;
352 huart->RxState= HAL_UART_STATE_READY;
354 return HAL_OK;
358 * @brief Initializes the LIN mode according to the specified
359 * parameters in the UART_InitTypeDef and create the associated handle.
360 * @param huart: pointer to a UART_HandleTypeDef structure that contains
361 * the configuration information for the specified UART module.
362 * @param BreakDetectLength: Specifies the LIN break detection length.
363 * This parameter can be one of the following values:
364 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
365 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
366 * @retval HAL status
368 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
370 /* Check the UART handle allocation */
371 if(huart == NULL)
373 return HAL_ERROR;
376 /* Check the parameters */
377 assert_param(IS_UART_INSTANCE(huart->Instance));
378 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
379 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
380 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
382 if(huart->gState == HAL_UART_STATE_RESET)
384 /* Allocate lock resource and initialize it */
385 huart->Lock = HAL_UNLOCKED;
386 /* Init the low level hardware */
387 HAL_UART_MspInit(huart);
390 huart->gState = HAL_UART_STATE_BUSY;
392 /* Disable the peripheral */
393 __HAL_UART_DISABLE(huart);
395 /* Set the UART Communication parameters */
396 UART_SetConfig(huart);
398 /* In LIN mode, the following bits must be kept cleared:
399 - LINEN and CLKEN bits in the USART_CR2 register,
400 - SCEN and IREN bits in the USART_CR3 register.*/
401 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
402 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
404 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
405 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
407 /* Set the USART LIN Break detection length. */
408 CLEAR_BIT(huart->Instance->CR2, USART_CR2_LBDL);
409 SET_BIT(huart->Instance->CR2, BreakDetectLength);
411 /* Enable the peripheral */
412 __HAL_UART_ENABLE(huart);
414 /* Initialize the UART state*/
415 huart->ErrorCode = HAL_UART_ERROR_NONE;
416 huart->gState= HAL_UART_STATE_READY;
417 huart->RxState= HAL_UART_STATE_READY;
419 return HAL_OK;
423 * @brief Initializes the Multi-Processor mode according to the specified
424 * parameters in the UART_InitTypeDef and create the associated handle.
425 * @param huart: pointer to a UART_HandleTypeDef structure that contains
426 * the configuration information for the specified UART module.
427 * @param Address: USART address
428 * @param WakeUpMethod: specifies the USART wake-up method.
429 * This parameter can be one of the following values:
430 * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection
431 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark
432 * @retval HAL status
434 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
436 /* Check the UART handle allocation */
437 if(huart == NULL)
439 return HAL_ERROR;
442 /* Check the parameters */
443 assert_param(IS_UART_INSTANCE(huart->Instance));
444 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
445 assert_param(IS_UART_ADDRESS(Address));
446 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
447 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
449 if(huart->gState == HAL_UART_STATE_RESET)
451 /* Allocate lock resource and initialize it */
452 huart->Lock = HAL_UNLOCKED;
453 /* Init the low level hardware */
454 HAL_UART_MspInit(huart);
457 huart->gState = HAL_UART_STATE_BUSY;
459 /* Disable the peripheral */
460 __HAL_UART_DISABLE(huart);
462 /* Set the UART Communication parameters */
463 UART_SetConfig(huart);
465 /* In Multi-Processor mode, the following bits must be kept cleared:
466 - LINEN and CLKEN bits in the USART_CR2 register,
467 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
468 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
469 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
471 /* Clear the USART address */
472 CLEAR_BIT(huart->Instance->CR2, USART_CR2_ADD);
473 /* Set the USART address node */
474 SET_BIT(huart->Instance->CR2, Address);
476 /* Set the wake up method by setting the WAKE bit in the CR1 register */
477 CLEAR_BIT(huart->Instance->CR1, USART_CR1_WAKE);
478 SET_BIT(huart->Instance->CR1, WakeUpMethod);
480 /* Enable the peripheral */
481 __HAL_UART_ENABLE(huart);
483 /* Initialize the UART state */
484 huart->ErrorCode = HAL_UART_ERROR_NONE;
485 huart->gState= HAL_UART_STATE_READY;
486 huart->RxState= HAL_UART_STATE_READY;
488 return HAL_OK;
492 * @brief DeInitializes the UART peripheral.
493 * @param huart: pointer to a UART_HandleTypeDef structure that contains
494 * the configuration information for the specified UART module.
495 * @retval HAL status
497 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
499 /* Check the UART handle allocation */
500 if(huart == NULL)
502 return HAL_ERROR;
505 /* Check the parameters */
506 assert_param(IS_UART_INSTANCE(huart->Instance));
508 huart->gState = HAL_UART_STATE_BUSY;
510 /* DeInit the low level hardware */
511 HAL_UART_MspDeInit(huart);
513 huart->ErrorCode = HAL_UART_ERROR_NONE;
514 huart->gState = HAL_UART_STATE_RESET;
515 huart->RxState = HAL_UART_STATE_RESET;
517 /* Process Lock */
518 __HAL_UNLOCK(huart);
520 return HAL_OK;
524 * @brief UART MSP Init.
525 * @param huart: pointer to a UART_HandleTypeDef structure that contains
526 * the configuration information for the specified UART module.
527 * @retval None
529 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
531 /* Prevent unused argument(s) compilation warning */
532 UNUSED(huart);
533 /* NOTE: This function Should not be modified, when the callback is needed,
534 the HAL_UART_MspInit could be implemented in the user file
539 * @brief UART MSP DeInit.
540 * @param huart: pointer to a UART_HandleTypeDef structure that contains
541 * the configuration information for the specified UART module.
542 * @retval None
544 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
546 /* Prevent unused argument(s) compilation warning */
547 UNUSED(huart);
548 /* NOTE: This function Should not be modified, when the callback is needed,
549 the HAL_UART_MspDeInit could be implemented in the user file
554 * @}
557 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
558 * @brief UART Transmit and Receive functions
560 @verbatim
561 ==============================================================================
562 ##### IO operation functions #####
563 ==============================================================================
564 [..]
565 This subsection provides a set of functions allowing to manage the UART asynchronous
566 and Half duplex data transfers.
568 (#) There are two modes of transfer:
569 (++) Blocking mode: The communication is performed in polling mode.
570 The HAL status of all data processing is returned by the same function
571 after finishing transfer.
572 (++) Non blocking mode: The communication is performed using Interrupts
573 or DMA, these APIs return the HAL status.
574 The end of the data processing will be indicated through the
575 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
576 using DMA mode.
577 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
578 will be executed respectively at the end of the transmit or receive process.
579 The HAL_UART_ErrorCallback() user callback will be executed when
580 a communication error is detected.
582 (#) Blocking mode APIs are:
583 (++) HAL_UART_Transmit()
584 (++) HAL_UART_Receive()
586 (#) Non Blocking mode APIs with Interrupt are:
587 (++) HAL_UART_Transmit_IT()
588 (++) HAL_UART_Receive_IT()
589 (++) HAL_UART_IRQHandler()
591 (#) Non Blocking mode functions with DMA are:
592 (++) HAL_UART_Transmit_DMA()
593 (++) HAL_UART_Receive_DMA()
595 (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
596 (++) HAL_UART_TxCpltCallback()
597 (++) HAL_UART_RxCpltCallback()
598 (++) HAL_UART_ErrorCallback()
600 [..]
601 (@) In the Half duplex communication, it is forbidden to run the transmit
602 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
603 can't be useful.
605 @endverbatim
606 * @{
610 * @brief Sends an amount of data in blocking mode.
611 * @param huart: pointer to a UART_HandleTypeDef structure that contains
612 * the configuration information for the specified UART module.
613 * @param pData: Pointer to data buffer
614 * @param Size: Amount of data to be sent
615 * @param Timeout: Timeout duration
616 * @retval HAL status
618 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
620 uint16_t* tmp;
621 uint32_t tickstart = 0U;
623 /* Check that a Tx process is not already ongoing */
624 if(huart->gState == HAL_UART_STATE_READY)
626 if((pData == NULL ) || (Size == 0))
628 return HAL_ERROR;
631 /* Process Locked */
632 __HAL_LOCK(huart);
634 huart->ErrorCode = HAL_UART_ERROR_NONE;
635 huart->gState = HAL_UART_STATE_BUSY_TX;
637 /* Init tickstart for timeout managment */
638 tickstart = HAL_GetTick();
640 huart->TxXferSize = Size;
641 huart->TxXferCount = Size;
642 while(huart->TxXferCount > 0U)
644 huart->TxXferCount--;
645 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
647 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
649 return HAL_TIMEOUT;
651 tmp = (uint16_t*) pData;
652 huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
653 if(huart->Init.Parity == UART_PARITY_NONE)
655 pData +=2U;
657 else
659 pData +=1U;
662 else
664 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
666 return HAL_TIMEOUT;
668 huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
672 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
674 return HAL_TIMEOUT;
677 /* At end of Tx process, restore huart->gState to Ready */
678 huart->gState = HAL_UART_STATE_READY;
680 /* Process Unlocked */
681 __HAL_UNLOCK(huart);
683 return HAL_OK;
685 else
687 return HAL_BUSY;
692 * @brief Receives an amount of data in blocking mode.
693 * @param huart: pointer to a UART_HandleTypeDef structure that contains
694 * the configuration information for the specified UART module.
695 * @param pData: Pointer to data buffer
696 * @param Size: Amount of data to be received
697 * @param Timeout: Timeout duration
698 * @retval HAL status
700 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
702 uint16_t* tmp;
703 uint32_t tickstart = 0U;
705 /* Check that a Rx process is not already ongoing */
706 if(huart->RxState == HAL_UART_STATE_READY)
708 if((pData == NULL ) || (Size == 0))
710 return HAL_ERROR;
713 /* Process Locked */
714 __HAL_LOCK(huart);
716 huart->ErrorCode = HAL_UART_ERROR_NONE;
717 huart->RxState = HAL_UART_STATE_BUSY_RX;
719 /* Init tickstart for timeout managment */
720 tickstart = HAL_GetTick();
722 huart->RxXferSize = Size;
723 huart->RxXferCount = Size;
725 /* Check the remain data to be received */
726 while(huart->RxXferCount > 0U)
728 huart->RxXferCount--;
729 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
731 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
733 return HAL_TIMEOUT;
735 tmp = (uint16_t*) pData;
736 if(huart->Init.Parity == UART_PARITY_NONE)
738 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
739 pData +=2U;
741 else
743 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
744 pData +=1U;
748 else
750 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
752 return HAL_TIMEOUT;
754 if(huart->Init.Parity == UART_PARITY_NONE)
756 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
758 else
760 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
766 /* At end of Rx process, restore huart->RxState to Ready */
767 huart->RxState = HAL_UART_STATE_READY;
769 /* Process Unlocked */
770 __HAL_UNLOCK(huart);
772 return HAL_OK;
774 else
776 return HAL_BUSY;
781 * @brief Sends an amount of data in non blocking mode.
782 * @param huart: pointer to a UART_HandleTypeDef structure that contains
783 * the configuration information for the specified UART module.
784 * @param pData: Pointer to data buffer
785 * @param Size: Amount of data to be sent
786 * @retval HAL status
788 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
790 /* Check that a Tx process is not already ongoing */
791 if(huart->gState == HAL_UART_STATE_READY)
793 if((pData == NULL ) || (Size == 0))
795 return HAL_ERROR;
798 /* Process Locked */
799 __HAL_LOCK(huart);
801 huart->pTxBuffPtr = pData;
802 huart->TxXferSize = Size;
803 huart->TxXferCount = Size;
805 huart->ErrorCode = HAL_UART_ERROR_NONE;
806 huart->gState = HAL_UART_STATE_BUSY_TX;
808 /* Process Unlocked */
809 __HAL_UNLOCK(huart);
811 /* Enable the UART Transmit data register empty Interrupt */
812 SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
814 return HAL_OK;
816 else
818 return HAL_BUSY;
823 * @brief Receives an amount of data in non blocking mode
824 * @param huart: pointer to a UART_HandleTypeDef structure that contains
825 * the configuration information for the specified UART module.
826 * @param pData: Pointer to data buffer
827 * @param Size: Amount of data to be received
828 * @retval HAL status
830 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
832 /* Check that a Rx process is not already ongoing */
833 if(huart->RxState == HAL_UART_STATE_READY)
835 if((pData == NULL ) || (Size == 0))
837 return HAL_ERROR;
840 /* Process Locked */
841 __HAL_LOCK(huart);
843 huart->pRxBuffPtr = pData;
844 huart->RxXferSize = Size;
845 huart->RxXferCount = Size;
847 huart->ErrorCode = HAL_UART_ERROR_NONE;
848 huart->RxState = HAL_UART_STATE_BUSY_RX;
850 /* Process Unlocked */
851 __HAL_UNLOCK(huart);
853 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
854 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
856 /* Enable the UART Parity Error and Data Register not empty Interrupts */
857 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
859 return HAL_OK;
861 else
863 return HAL_BUSY;
868 * @brief Sends an amount of data in non blocking mode.
869 * @param huart: pointer to a UART_HandleTypeDef structure that contains
870 * the configuration information for the specified UART module.
871 * @param pData: Pointer to data buffer
872 * @param Size: Amount of data to be sent
873 * @retval HAL status
875 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
877 uint32_t *tmp;
879 /* Check that a Tx process is not already ongoing */
880 if(huart->gState == HAL_UART_STATE_READY)
882 if((pData == NULL ) || (Size == 0))
884 return HAL_ERROR;
887 /* Process Locked */
888 __HAL_LOCK(huart);
890 huart->pTxBuffPtr = pData;
891 huart->TxXferSize = Size;
892 huart->TxXferCount = Size;
894 huart->ErrorCode = HAL_UART_ERROR_NONE;
895 huart->gState = HAL_UART_STATE_BUSY_TX;
897 /* Set the UART DMA transfer complete callback */
898 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
900 /* Set the UART DMA Half transfer complete callback */
901 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
903 /* Set the DMA error callback */
904 huart->hdmatx->XferErrorCallback = UART_DMAError;
906 /* Set the DMA abort callback */
907 huart->hdmatx->XferAbortCallback = NULL;
909 /* Enable the UART transmit DMA Stream */
910 tmp = (uint32_t*)&pData;
911 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
913 /* Clear the TC flag in the SR register by writing 0 to it */
914 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
916 /* Process Unlocked */
917 __HAL_UNLOCK(huart);
919 /* Enable the DMA transfer for transmit request by setting the DMAT bit
920 in the UART CR3 register */
921 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
923 return HAL_OK;
925 else
927 return HAL_BUSY;
932 * @brief Receives an amount of data in non blocking mode.
933 * @param huart: pointer to a UART_HandleTypeDef structure that contains
934 * the configuration information for the specified UART module.
935 * @param pData: Pointer to data buffer
936 * @param Size: Amount of data to be received
937 * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
938 * @retval HAL status
940 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
942 uint32_t *tmp;
944 /* Check that a Rx process is not already ongoing */
945 if(huart->RxState == HAL_UART_STATE_READY)
947 if((pData == NULL ) || (Size == 0))
949 return HAL_ERROR;
952 /* Process Locked */
953 __HAL_LOCK(huart);
955 huart->pRxBuffPtr = pData;
956 huart->RxXferSize = Size;
958 huart->ErrorCode = HAL_UART_ERROR_NONE;
959 huart->RxState = HAL_UART_STATE_BUSY_RX;
961 /* Set the UART DMA transfer complete callback */
962 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
964 /* Set the UART DMA Half transfer complete callback */
965 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
967 /* Set the DMA error callback */
968 huart->hdmarx->XferErrorCallback = UART_DMAError;
970 /* Set the DMA abort callback */
971 huart->hdmarx->XferAbortCallback = NULL;
973 /* Enable the DMA Stream */
974 tmp = (uint32_t*)&pData;
975 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
977 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
978 __HAL_UART_CLEAR_OREFLAG(huart);
980 /* Process Unlocked */
981 __HAL_UNLOCK(huart);
983 /* Enable the UART Parity Error Interrupt */
984 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
986 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
987 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
989 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
990 in the UART CR3 register */
991 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
993 return HAL_OK;
995 else
997 return HAL_BUSY;
1002 * @brief Pauses the DMA Transfer.
1003 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1004 * the configuration information for the specified UART module.
1005 * @retval HAL status
1007 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1009 uint32_t dmarequest = 0x00U;
1011 /* Process Locked */
1012 __HAL_LOCK(huart);
1013 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1014 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1016 /* Disable the UART DMA Tx request */
1017 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1019 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1020 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1022 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1023 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1024 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1026 /* Disable the UART DMA Rx request */
1027 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1030 /* Process Unlocked */
1031 __HAL_UNLOCK(huart);
1033 return HAL_OK;
1037 * @brief Resumes the DMA Transfer.
1038 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1039 * the configuration information for the specified UART module.
1040 * @retval HAL status
1042 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1044 /* Process Locked */
1045 __HAL_LOCK(huart);
1047 if(huart->gState == HAL_UART_STATE_BUSY_TX)
1049 /* Enable the UART DMA Tx request */
1050 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1052 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1054 /* Clear the Overrun flag before resuming the Rx transfer*/
1055 __HAL_UART_CLEAR_OREFLAG(huart);
1057 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1058 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1059 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1061 /* Enable the UART DMA Rx request */
1062 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1065 /* Process Unlocked */
1066 __HAL_UNLOCK(huart);
1068 return HAL_OK;
1072 * @brief Stops the DMA Transfer.
1073 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1074 * the configuration information for the specified UART module.
1075 * @retval HAL status
1077 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1079 uint32_t dmarequest = 0x00U;
1080 /* The Lock is not implemented on this API to allow the user application
1081 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
1082 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1083 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
1086 /* Stop UART DMA Tx request if ongoing */
1087 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1088 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1090 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1092 /* Abort the UART DMA Tx channel */
1093 if(huart->hdmatx != NULL)
1095 HAL_DMA_Abort(huart->hdmatx);
1097 UART_EndTxTransfer(huart);
1100 /* Stop UART DMA Rx request if ongoing */
1101 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1102 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1104 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1106 /* Abort the UART DMA Rx channel */
1107 if(huart->hdmarx != NULL)
1109 HAL_DMA_Abort(huart->hdmarx);
1111 UART_EndRxTransfer(huart);
1114 return HAL_OK;
1118 * @brief Abort ongoing transfers (blocking mode).
1119 * @param huart UART handle.
1120 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1121 * This procedure performs following operations :
1122 * - Disable PPP Interrupts
1123 * - Disable the DMA transfer in the peripheral register (if enabled)
1124 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1125 * - Set handle State to READY
1126 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1127 * @retval HAL status
1129 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1131 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1132 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1133 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1135 /* Disable the UART DMA Tx request if enabled */
1136 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1138 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1140 /* Abort the UART DMA Tx channel: use blocking DMA Abort API (no callback) */
1141 if(huart->hdmatx != NULL)
1143 /* Set the UART DMA Abort callback to Null.
1144 No call back execution at end of DMA abort procedure */
1145 huart->hdmatx->XferAbortCallback = NULL;
1147 HAL_DMA_Abort(huart->hdmatx);
1151 /* Disable the UART DMA Rx request if enabled */
1152 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1154 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1156 /* Abort the UART DMA Rx channel: use blocking DMA Abort API (no callback) */
1157 if(huart->hdmarx != NULL)
1159 /* Set the UART DMA Abort callback to Null.
1160 No call back execution at end of DMA abort procedure */
1161 huart->hdmarx->XferAbortCallback = NULL;
1163 HAL_DMA_Abort(huart->hdmarx);
1167 /* Reset Tx and Rx transfer counters */
1168 huart->TxXferCount = 0x00U;
1169 huart->RxXferCount = 0x00U;
1171 /* Reset ErrorCode */
1172 huart->ErrorCode = HAL_UART_ERROR_NONE;
1174 /* Restore huart->RxState and huart->gState to Ready */
1175 huart->RxState = HAL_UART_STATE_READY;
1176 huart->gState = HAL_UART_STATE_READY;
1178 return HAL_OK;
1182 * @brief Abort ongoing Transmit transfer (blocking mode).
1183 * @param huart UART handle.
1184 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1185 * This procedure performs following operations :
1186 * - Disable PPP Interrupts
1187 * - Disable the DMA transfer in the peripheral register (if enabled)
1188 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1189 * - Set handle State to READY
1190 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1191 * @retval HAL status
1193 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1195 /* Disable TXEIE and TCIE interrupts */
1196 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1198 /* Disable the UART DMA Tx request if enabled */
1199 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1201 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1203 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1204 if(huart->hdmatx != NULL)
1206 /* Set the UART DMA Abort callback to Null.
1207 No call back execution at end of DMA abort procedure */
1208 huart->hdmatx->XferAbortCallback = NULL;
1210 HAL_DMA_Abort(huart->hdmatx);
1214 /* Reset Tx transfer counter */
1215 huart->TxXferCount = 0x00U;
1217 /* Restore huart->gState to Ready */
1218 huart->gState = HAL_UART_STATE_READY;
1220 return HAL_OK;
1224 * @brief Abort ongoing Receive transfer (blocking mode).
1225 * @param huart UART handle.
1226 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1227 * This procedure performs following operations :
1228 * - Disable PPP Interrupts
1229 * - Disable the DMA transfer in the peripheral register (if enabled)
1230 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1231 * - Set handle State to READY
1232 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1233 * @retval HAL status
1235 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1237 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1238 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1239 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1241 /* Disable the UART DMA Rx request if enabled */
1242 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1244 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1246 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1247 if(huart->hdmarx != NULL)
1249 /* Set the UART DMA Abort callback to Null.
1250 No call back execution at end of DMA abort procedure */
1251 huart->hdmarx->XferAbortCallback = NULL;
1253 HAL_DMA_Abort(huart->hdmarx);
1257 /* Reset Rx transfer counter */
1258 huart->RxXferCount = 0x00U;
1260 /* Restore huart->RxState to Ready */
1261 huart->RxState = HAL_UART_STATE_READY;
1263 return HAL_OK;
1267 * @brief Abort ongoing transfers (Interrupt mode).
1268 * @param huart UART handle.
1269 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1270 * This procedure performs following operations :
1271 * - Disable PPP Interrupts
1272 * - Disable the DMA transfer in the peripheral register (if enabled)
1273 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1274 * - Set handle State to READY
1275 * - At abort completion, call user abort complete callback
1276 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1277 * considered as completed only when user abort complete callback is executed (not when exiting function).
1278 * @retval HAL status
1280 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1282 uint32_t AbortCplt = 0x01U;
1284 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1285 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1286 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1288 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1289 before any call to DMA Abort functions */
1290 /* DMA Tx Handle is valid */
1291 if(huart->hdmatx != NULL)
1293 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1294 Otherwise, set it to NULL */
1295 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1297 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
1299 else
1301 huart->hdmatx->XferAbortCallback = NULL;
1304 /* DMA Rx Handle is valid */
1305 if(huart->hdmarx != NULL)
1307 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1308 Otherwise, set it to NULL */
1309 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1311 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
1313 else
1315 huart->hdmarx->XferAbortCallback = NULL;
1319 /* Disable the UART DMA Tx request if enabled */
1320 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1322 /* Disable DMA Tx at UART level */
1323 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1325 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1326 if(huart->hdmatx != NULL)
1328 /* UART Tx DMA Abort callback has already been initialised :
1329 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1331 /* Abort DMA TX */
1332 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1334 huart->hdmatx->XferAbortCallback = NULL;
1336 else
1338 AbortCplt = 0x00U;
1343 /* Disable the UART DMA Rx request if enabled */
1344 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1346 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1348 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1349 if(huart->hdmarx != NULL)
1351 /* UART Rx DMA Abort callback has already been initialised :
1352 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1354 /* Abort DMA RX */
1355 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1357 huart->hdmarx->XferAbortCallback = NULL;
1358 AbortCplt = 0x01U;
1360 else
1362 AbortCplt = 0x00U;
1367 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1368 if(AbortCplt == 0x01U)
1370 /* Reset Tx and Rx transfer counters */
1371 huart->TxXferCount = 0x00U;
1372 huart->RxXferCount = 0x00U;
1374 /* Reset ErrorCode */
1375 huart->ErrorCode = HAL_UART_ERROR_NONE;
1377 /* Restore huart->gState and huart->RxState to Ready */
1378 huart->gState = HAL_UART_STATE_READY;
1379 huart->RxState = HAL_UART_STATE_READY;
1381 /* As no DMA to be aborted, call directly user Abort complete callback */
1382 HAL_UART_AbortCpltCallback(huart);
1385 return HAL_OK;
1389 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1390 * @param huart UART handle.
1391 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1392 * This procedure performs following operations :
1393 * - Disable PPP Interrupts
1394 * - Disable the DMA transfer in the peripheral register (if enabled)
1395 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1396 * - Set handle State to READY
1397 * - At abort completion, call user abort complete callback
1398 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1399 * considered as completed only when user abort complete callback is executed (not when exiting function).
1400 * @retval HAL status
1402 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
1404 /* Disable TXEIE and TCIE interrupts */
1405 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1407 /* Disable the UART DMA Tx request if enabled */
1408 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1410 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1412 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1413 if(huart->hdmatx != NULL)
1415 /* Set the UART DMA Abort callback :
1416 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1417 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
1419 /* Abort DMA TX */
1420 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1422 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
1423 huart->hdmatx->XferAbortCallback(huart->hdmatx);
1426 else
1428 /* Reset Tx transfer counter */
1429 huart->TxXferCount = 0x00U;
1431 /* Restore huart->gState to Ready */
1432 huart->gState = HAL_UART_STATE_READY;
1434 /* As no DMA to be aborted, call directly user Abort complete callback */
1435 HAL_UART_AbortTransmitCpltCallback(huart);
1438 else
1440 /* Reset Tx transfer counter */
1441 huart->TxXferCount = 0x00U;
1443 /* Restore huart->gState to Ready */
1444 huart->gState = HAL_UART_STATE_READY;
1446 /* As no DMA to be aborted, call directly user Abort complete callback */
1447 HAL_UART_AbortTransmitCpltCallback(huart);
1450 return HAL_OK;
1454 * @brief Abort ongoing Receive transfer (Interrupt mode).
1455 * @param huart UART handle.
1456 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1457 * This procedure performs following operations :
1458 * - Disable PPP Interrupts
1459 * - Disable the DMA transfer in the peripheral register (if enabled)
1460 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1461 * - Set handle State to READY
1462 * - At abort completion, call user abort complete callback
1463 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1464 * considered as completed only when user abort complete callback is executed (not when exiting function).
1465 * @retval HAL status
1467 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
1469 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1470 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1471 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1473 /* Disable the UART DMA Rx request if enabled */
1474 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1476 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1478 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1479 if(huart->hdmarx != NULL)
1481 /* Set the UART DMA Abort callback :
1482 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1483 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
1485 /* Abort DMA RX */
1486 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1488 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1489 huart->hdmarx->XferAbortCallback(huart->hdmarx);
1492 else
1494 /* Reset Rx transfer counter */
1495 huart->RxXferCount = 0x00U;
1497 /* Restore huart->RxState to Ready */
1498 huart->RxState = HAL_UART_STATE_READY;
1500 /* As no DMA to be aborted, call directly user Abort complete callback */
1501 HAL_UART_AbortReceiveCpltCallback(huart);
1504 else
1506 /* Reset Rx transfer counter */
1507 huart->RxXferCount = 0x00U;
1509 /* Restore huart->RxState to Ready */
1510 huart->RxState = HAL_UART_STATE_READY;
1512 /* As no DMA to be aborted, call directly user Abort complete callback */
1513 HAL_UART_AbortReceiveCpltCallback(huart);
1516 return HAL_OK;
1520 * @brief This function handles UART interrupt request.
1521 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1522 * the configuration information for the specified UART module.
1523 * @retval None
1525 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
1527 uint32_t isrflags = READ_REG(huart->Instance->SR);
1528 uint32_t cr1its = READ_REG(huart->Instance->CR1);
1529 uint32_t cr3its = READ_REG(huart->Instance->CR3);
1530 uint32_t errorflags = 0x00U;
1531 uint32_t dmarequest = 0x00U;
1533 /* If no error occurs */
1534 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1535 if(errorflags == RESET)
1537 /* UART in mode Receiver -------------------------------------------------*/
1538 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1540 UART_Receive_IT(huart);
1541 return;
1545 /* If some errors occur */
1546 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1548 /* UART parity error interrupt occurred ----------------------------------*/
1549 if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1551 huart->ErrorCode |= HAL_UART_ERROR_PE;
1554 /* UART noise error interrupt occurred -----------------------------------*/
1555 if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1557 huart->ErrorCode |= HAL_UART_ERROR_NE;
1560 /* UART frame error interrupt occurred -----------------------------------*/
1561 if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1563 huart->ErrorCode |= HAL_UART_ERROR_FE;
1566 /* UART Over-Run interrupt occurred --------------------------------------*/
1567 if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1569 huart->ErrorCode |= HAL_UART_ERROR_ORE;
1572 /* Call UART Error Call back function if need be --------------------------*/
1573 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1575 /* UART in mode Receiver -----------------------------------------------*/
1576 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1578 UART_Receive_IT(huart);
1581 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1582 consider error as blocking */
1583 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1584 if(((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest)
1586 /* Blocking error : transfer is aborted
1587 Set the UART state ready to be able to start again the process,
1588 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1589 UART_EndRxTransfer(huart);
1591 /* Disable the UART DMA Rx request if enabled */
1592 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1594 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1596 /* Abort the UART DMA Rx channel */
1597 if(huart->hdmarx != NULL)
1599 /* Set the UART DMA Abort callback :
1600 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1601 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
1602 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1604 /* Call Directly XferAbortCallback function in case of error */
1605 huart->hdmarx->XferAbortCallback(huart->hdmarx);
1608 else
1610 /* Call user error callback */
1611 HAL_UART_ErrorCallback(huart);
1614 else
1616 /* Call user error callback */
1617 HAL_UART_ErrorCallback(huart);
1620 else
1622 /* Non Blocking error : transfer could go on.
1623 Error is notified to user through user error callback */
1624 HAL_UART_ErrorCallback(huart);
1625 huart->ErrorCode = HAL_UART_ERROR_NONE;
1628 return;
1629 } /* End if some error occurs */
1631 /* UART in mode Transmitter ------------------------------------------------*/
1632 if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1634 UART_Transmit_IT(huart);
1635 return;
1638 /* UART in mode Transmitter end --------------------------------------------*/
1639 if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1641 UART_EndTransmit_IT(huart);
1642 return;
1647 * @brief Tx Transfer completed callbacks.
1648 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1649 * the configuration information for the specified UART module.
1650 * @retval None
1652 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1654 /* Prevent unused argument(s) compilation warning */
1655 UNUSED(huart);
1656 /* NOTE: This function Should not be modified, when the callback is needed,
1657 the HAL_UART_TxCpltCallback could be implemented in the user file
1662 * @brief Tx Half Transfer completed callbacks.
1663 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1664 * the configuration information for the specified UART module.
1665 * @retval None
1667 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
1669 /* Prevent unused argument(s) compilation warning */
1670 UNUSED(huart);
1671 /* NOTE: This function Should not be modified, when the callback is needed,
1672 the HAL_UART_TxCpltCallback could be implemented in the user file
1677 * @brief Rx Transfer completed callbacks.
1678 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1679 * the configuration information for the specified UART module.
1680 * @retval None
1682 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1684 /* Prevent unused argument(s) compilation warning */
1685 UNUSED(huart);
1686 /* NOTE: This function Should not be modified, when the callback is needed,
1687 the HAL_UART_TxCpltCallback could be implemented in the user file
1692 * @brief Rx Half Transfer completed callbacks.
1693 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1694 * the configuration information for the specified UART module.
1695 * @retval None
1697 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
1699 /* Prevent unused argument(s) compilation warning */
1700 UNUSED(huart);
1701 /* NOTE: This function Should not be modified, when the callback is needed,
1702 the HAL_UART_TxCpltCallback could be implemented in the user file
1707 * @brief UART error callbacks.
1708 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1709 * the configuration information for the specified UART module.
1710 * @retval None
1712 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1714 /* Prevent unused argument(s) compilation warning */
1715 UNUSED(huart);
1716 /* NOTE: This function Should not be modified, when the callback is needed,
1717 the HAL_UART_ErrorCallback could be implemented in the user file
1722 * @brief UART Abort Complete callback.
1723 * @param huart UART handle.
1724 * @retval None
1726 __weak void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart)
1728 /* Prevent unused argument(s) compilation warning */
1729 UNUSED(huart);
1731 /* NOTE : This function should not be modified, when the callback is needed,
1732 the HAL_UART_AbortCpltCallback can be implemented in the user file.
1736 * @brief UART Abort Complete callback.
1737 * @param huart UART handle.
1738 * @retval None
1740 __weak void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart)
1742 /* Prevent unused argument(s) compilation warning */
1743 UNUSED(huart);
1745 /* NOTE : This function should not be modified, when the callback is needed,
1746 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
1751 * @brief UART Abort Receive Complete callback.
1752 * @param huart UART handle.
1753 * @retval None
1755 __weak void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart)
1757 /* Prevent unused argument(s) compilation warning */
1758 UNUSED(huart);
1760 /* NOTE : This function should not be modified, when the callback is needed,
1761 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
1766 * @}
1769 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1770 * @brief UART control functions
1772 @verbatim
1773 ==============================================================================
1774 ##### Peripheral Control functions #####
1775 ==============================================================================
1776 [..]
1777 This subsection provides a set of functions allowing to control the UART:
1778 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
1779 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
1780 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
1782 @endverbatim
1783 * @{
1787 * @brief Transmits break characters.
1788 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1789 * the configuration information for the specified UART module.
1790 * @retval HAL status
1792 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
1794 /* Check the parameters */
1795 assert_param(IS_UART_INSTANCE(huart->Instance));
1797 /* Process Locked */
1798 __HAL_LOCK(huart);
1800 huart->gState = HAL_UART_STATE_BUSY;
1802 /* Send break characters */
1803 SET_BIT(huart->Instance->CR1, USART_CR1_SBK);
1805 huart->gState = HAL_UART_STATE_READY;
1807 /* Process Unlocked */
1808 __HAL_UNLOCK(huart);
1810 return HAL_OK;
1814 * @brief Enters the UART in mute mode.
1815 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1816 * the configuration information for the specified UART module.
1817 * @retval HAL status
1819 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
1821 /* Check the parameters */
1822 assert_param(IS_UART_INSTANCE(huart->Instance));
1824 /* Process Locked */
1825 __HAL_LOCK(huart);
1827 huart->gState = HAL_UART_STATE_BUSY;
1829 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
1830 SET_BIT(huart->Instance->CR1, USART_CR1_RWU);
1832 huart->gState = HAL_UART_STATE_READY;
1834 /* Process Unlocked */
1835 __HAL_UNLOCK(huart);
1837 return HAL_OK;
1841 * @brief Exits the UART mute mode: wake up software.
1842 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1843 * the configuration information for the specified UART module.
1844 * @retval HAL status
1846 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
1848 /* Check the parameters */
1849 assert_param(IS_UART_INSTANCE(huart->Instance));
1851 /* Process Locked */
1852 __HAL_LOCK(huart);
1854 huart->gState = HAL_UART_STATE_BUSY;
1856 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
1857 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU);
1859 huart->gState = HAL_UART_STATE_READY;
1861 /* Process Unlocked */
1862 __HAL_UNLOCK(huart);
1864 return HAL_OK;
1868 * @brief Enables the UART transmitter and disables the UART receiver.
1869 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1870 * the configuration information for the specified UART module.
1871 * @retval HAL status
1873 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
1875 uint32_t tmpreg = 0x00U;
1877 /* Process Locked */
1878 __HAL_LOCK(huart);
1880 huart->gState = HAL_UART_STATE_BUSY;
1882 /*-------------------------- USART CR1 Configuration -----------------------*/
1883 tmpreg = huart->Instance->CR1;
1885 /* Clear TE and RE bits */
1886 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1888 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
1889 tmpreg |= (uint32_t)USART_CR1_TE;
1891 /* Write to USART CR1 */
1892 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
1894 huart->gState = HAL_UART_STATE_READY;
1896 /* Process Unlocked */
1897 __HAL_UNLOCK(huart);
1899 return HAL_OK;
1903 * @brief Enables the UART receiver and disables the UART transmitter.
1904 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1905 * the configuration information for the specified UART module.
1906 * @retval HAL status
1908 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
1910 uint32_t tmpreg = 0x00U;
1912 /* Process Locked */
1913 __HAL_LOCK(huart);
1915 huart->gState = HAL_UART_STATE_BUSY;
1917 /*-------------------------- USART CR1 Configuration -----------------------*/
1918 tmpreg = huart->Instance->CR1;
1920 /* Clear TE and RE bits */
1921 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1923 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
1924 tmpreg |= (uint32_t)USART_CR1_RE;
1926 /* Write to USART CR1 */
1927 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
1929 huart->gState = HAL_UART_STATE_READY;
1931 /* Process Unlocked */
1932 __HAL_UNLOCK(huart);
1934 return HAL_OK;
1938 * @}
1941 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
1942 * @brief UART State and Errors functions
1944 @verbatim
1945 ==============================================================================
1946 ##### Peripheral State and Errors functions #####
1947 ==============================================================================
1948 [..]
1949 This subsection provides a set of functions allowing to return the State of
1950 UART communication process, return Peripheral Errors occurred during communication
1951 process
1952 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
1953 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
1955 @endverbatim
1956 * @{
1960 * @brief Returns the UART state.
1961 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1962 * the configuration information for the specified UART module.
1963 * @retval HAL state
1965 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
1967 uint32_t temp1= 0x00U, temp2 = 0x00U;
1968 temp1 = huart->gState;
1969 temp2 = huart->RxState;
1971 return (HAL_UART_StateTypeDef)(temp1 | temp2);
1975 * @brief Return the UART error code
1976 * @param huart : pointer to a UART_HandleTypeDef structure that contains
1977 * the configuration information for the specified UART.
1978 * @retval UART Error Code
1980 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
1982 return huart->ErrorCode;
1986 * @}
1990 * @brief DMA UART transmit process complete callback.
1991 * @param hdma: DMA handle
1992 * @retval None
1994 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
1996 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1997 /* DMA Normal mode*/
1998 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2000 huart->TxXferCount = 0U;
2002 /* Disable the DMA transfer for transmit request by setting the DMAT bit
2003 in the UART CR3 register */
2004 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2006 /* Enable the UART Transmit Complete Interrupt */
2007 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
2010 /* DMA Circular mode */
2011 else
2013 HAL_UART_TxCpltCallback(huart);
2018 * @brief DMA UART transmit process half complete callback
2019 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2020 * the configuration information for the specified DMA module.
2021 * @retval None
2023 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2025 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2027 HAL_UART_TxHalfCpltCallback(huart);
2031 * @brief DMA UART receive process complete callback.
2032 * @param hdma: DMA handle
2033 * @retval None
2035 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2037 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2038 /* DMA Normal mode*/
2039 if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
2041 huart->RxXferCount = 0U;
2043 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2044 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2045 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2047 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
2048 in the UART CR3 register */
2049 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2051 /* At end of Rx process, restore huart->RxState to Ready */
2052 huart->RxState = HAL_UART_STATE_READY;
2054 HAL_UART_RxCpltCallback(huart);
2058 * @brief DMA UART receive process half complete callback
2059 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2060 * the configuration information for the specified DMA module.
2061 * @retval None
2063 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2065 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2067 HAL_UART_RxHalfCpltCallback(huart);
2071 * @brief DMA UART communication error callback.
2072 * @param hdma: DMA handle
2073 * @retval None
2075 static void UART_DMAError(DMA_HandleTypeDef *hdma)
2077 uint32_t dmarequest = 0x00U;
2078 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2080 /* Stop UART DMA Tx request if ongoing */
2081 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
2082 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
2084 huart->TxXferCount = 0U;
2085 UART_EndTxTransfer(huart);
2088 /* Stop UART DMA Rx request if ongoing */
2089 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
2090 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
2092 huart->RxXferCount = 0U;
2093 UART_EndRxTransfer(huart);
2096 huart->ErrorCode |= HAL_UART_ERROR_DMA;
2097 HAL_UART_ErrorCallback(huart);
2101 * @brief This function handles UART Communication Timeout.
2102 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2103 * the configuration information for the specified UART module.
2104 * @param Flag: specifies the UART flag to check.
2105 * @param Status: The new Flag status (SET or RESET).
2106 * @param Tickstart Tick start value
2107 * @param Timeout: Timeout duration
2108 * @retval HAL status
2110 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2112 /* Wait until flag is set */
2113 while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
2115 /* Check for the Timeout */
2116 if(Timeout != HAL_MAX_DELAY)
2118 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
2120 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2121 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
2122 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2124 huart->gState = HAL_UART_STATE_READY;
2125 huart->RxState = HAL_UART_STATE_READY;
2127 /* Process Unlocked */
2128 __HAL_UNLOCK(huart);
2130 return HAL_TIMEOUT;
2135 return HAL_OK;
2139 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
2140 * @param huart: UART handle.
2141 * @retval None
2143 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
2145 /* Disable TXEIE and TCIE interrupts */
2146 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2148 /* At end of Tx process, restore huart->gState to Ready */
2149 huart->gState = HAL_UART_STATE_READY;
2153 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2154 * @param huart: UART handle.
2155 * @retval None
2157 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
2159 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2160 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2161 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2163 /* At end of Rx process, restore huart->RxState to Ready */
2164 huart->RxState = HAL_UART_STATE_READY;
2168 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
2169 * (To be called at end of DMA Abort procedure following error occurrence).
2170 * @param hdma DMA handle.
2171 * @retval None
2173 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2175 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2176 huart->RxXferCount = 0U;
2177 huart->TxXferCount = 0U;
2179 HAL_UART_ErrorCallback(huart);
2183 * @brief DMA UART Tx communication abort callback, when initiated by user
2184 * (To be called at end of DMA Tx Abort procedure following user abort request).
2185 * @note When this callback is executed, User Abort complete call back is called only if no
2186 * Abort still ongoing for Rx DMA Handle.
2187 * @param hdma DMA handle.
2188 * @retval None
2190 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2192 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2194 huart->hdmatx->XferAbortCallback = NULL;
2196 /* Check if an Abort process is still ongoing */
2197 if(huart->hdmarx != NULL)
2199 if(huart->hdmarx->XferAbortCallback != NULL)
2201 return;
2205 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2206 huart->TxXferCount = 0x00U;
2207 huart->RxXferCount = 0x00U;
2209 /* Reset ErrorCode */
2210 huart->ErrorCode = HAL_UART_ERROR_NONE;
2212 /* Restore huart->gState and huart->RxState to Ready */
2213 huart->gState = HAL_UART_STATE_READY;
2214 huart->RxState = HAL_UART_STATE_READY;
2216 /* Call user Abort complete callback */
2217 HAL_UART_AbortCpltCallback(huart);
2221 * @brief DMA UART Rx communication abort callback, when initiated by user
2222 * (To be called at end of DMA Rx Abort procedure following user abort request).
2223 * @note When this callback is executed, User Abort complete call back is called only if no
2224 * Abort still ongoing for Tx DMA Handle.
2225 * @param hdma DMA handle.
2226 * @retval None
2228 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2230 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2232 huart->hdmarx->XferAbortCallback = NULL;
2234 /* Check if an Abort process is still ongoing */
2235 if(huart->hdmatx != NULL)
2237 if(huart->hdmatx->XferAbortCallback != NULL)
2239 return;
2243 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2244 huart->TxXferCount = 0x00U;
2245 huart->RxXferCount = 0x00U;
2247 /* Reset ErrorCode */
2248 huart->ErrorCode = HAL_UART_ERROR_NONE;
2250 /* Restore huart->gState and huart->RxState to Ready */
2251 huart->gState = HAL_UART_STATE_READY;
2252 huart->RxState = HAL_UART_STATE_READY;
2254 /* Call user Abort complete callback */
2255 HAL_UART_AbortCpltCallback(huart);
2259 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
2260 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
2261 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2262 * and leads to user Tx Abort Complete callback execution).
2263 * @param hdma DMA handle.
2264 * @retval None
2266 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2268 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2270 huart->TxXferCount = 0x00U;
2272 /* Restore huart->gState to Ready */
2273 huart->gState = HAL_UART_STATE_READY;
2275 /* Call user Abort complete callback */
2276 HAL_UART_AbortTransmitCpltCallback(huart);
2280 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
2281 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
2282 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2283 * and leads to user Rx Abort Complete callback execution).
2284 * @param hdma DMA handle.
2285 * @retval None
2287 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2289 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2291 huart->RxXferCount = 0x00U;
2293 /* Restore huart->RxState to Ready */
2294 huart->RxState = HAL_UART_STATE_READY;
2296 /* Call user Abort complete callback */
2297 HAL_UART_AbortReceiveCpltCallback(huart);
2301 * @brief Sends an amount of data in non blocking mode.
2302 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
2303 * the configuration information for the specified UART module.
2304 * @retval HAL status
2306 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
2308 uint16_t* tmp;
2310 /* Check that a Tx process is ongoing */
2311 if(huart->gState == HAL_UART_STATE_BUSY_TX)
2313 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
2315 tmp = (uint16_t*) huart->pTxBuffPtr;
2316 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
2317 if(huart->Init.Parity == UART_PARITY_NONE)
2319 huart->pTxBuffPtr += 2U;
2321 else
2323 huart->pTxBuffPtr += 1U;
2326 else
2328 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
2331 if(--huart->TxXferCount == 0U)
2333 /* Disable the UART Transmit Complete Interrupt */
2334 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
2336 /* Enable the UART Transmit Complete Interrupt */
2337 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
2339 return HAL_OK;
2341 else
2343 return HAL_BUSY;
2348 * @brief Wraps up transmission in non blocking mode.
2349 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2350 * the configuration information for the specified UART module.
2351 * @retval HAL status
2353 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
2355 /* Disable the UART Transmit Complete Interrupt */
2356 CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
2358 /* Tx process is ended, restore huart->gState to Ready */
2359 huart->gState = HAL_UART_STATE_READY;
2361 HAL_UART_TxCpltCallback(huart);
2363 return HAL_OK;
2367 * @brief Receives an amount of data in non blocking mode
2368 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2369 * the configuration information for the specified UART module.
2370 * @retval HAL status
2372 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
2374 uint16_t* tmp;
2376 /* Check that a Rx process is ongoing */
2377 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
2379 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
2381 tmp = (uint16_t*) huart->pRxBuffPtr;
2382 if(huart->Init.Parity == UART_PARITY_NONE)
2384 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
2385 huart->pRxBuffPtr += 2U;
2387 else
2389 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
2390 huart->pRxBuffPtr += 1U;
2393 else
2395 if(huart->Init.Parity == UART_PARITY_NONE)
2397 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
2399 else
2401 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
2405 if(--huart->RxXferCount == 0U)
2407 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
2408 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2410 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
2411 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2413 /* Rx process is completed, restore huart->RxState to Ready */
2414 huart->RxState = HAL_UART_STATE_READY;
2416 HAL_UART_RxCpltCallback(huart);
2418 return HAL_OK;
2420 return HAL_OK;
2422 else
2424 return HAL_BUSY;
2429 * @brief Configures the UART peripheral.
2430 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2431 * the configuration information for the specified UART module.
2432 * @retval None
2434 static void UART_SetConfig(UART_HandleTypeDef *huart)
2436 uint32_t tmpreg = 0x00U;
2438 /* Check the parameters */
2439 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
2440 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
2441 assert_param(IS_UART_PARITY(huart->Init.Parity));
2442 assert_param(IS_UART_MODE(huart->Init.Mode));
2444 /*-------------------------- USART CR2 Configuration -----------------------*/
2445 tmpreg = huart->Instance->CR2;
2447 /* Clear STOP[13:12] bits */
2448 tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
2450 /* Configure the UART Stop Bits: Set STOP[13:12] bits according to huart->Init.StopBits value */
2451 tmpreg |= (uint32_t)huart->Init.StopBits;
2453 /* Write to USART CR2 */
2454 WRITE_REG(huart->Instance->CR2, (uint32_t)tmpreg);
2456 /*-------------------------- USART CR1 Configuration -----------------------*/
2457 tmpreg = huart->Instance->CR1;
2459 /* Clear M, PCE, PS, TE and RE bits */
2460 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | \
2461 USART_CR1_RE | USART_CR1_OVER8));
2463 /* Configure the UART Word Length, Parity and mode:
2464 Set the M bits according to huart->Init.WordLength value
2465 Set PCE and PS bits according to huart->Init.Parity value
2466 Set TE and RE bits according to huart->Init.Mode value
2467 Set OVER8 bit according to huart->Init.OverSampling value */
2468 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
2470 /* Write to USART CR1 */
2471 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
2473 /*-------------------------- USART CR3 Configuration -----------------------*/
2474 tmpreg = huart->Instance->CR3;
2476 /* Clear CTSE and RTSE bits */
2477 tmpreg &= (uint32_t)~((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE));
2479 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
2480 tmpreg |= huart->Init.HwFlowCtl;
2482 /* Write to USART CR3 */
2483 WRITE_REG(huart->Instance->CR3, (uint32_t)tmpreg);
2485 /* Check the Over Sampling */
2486 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
2488 /*-------------------------- USART BRR Configuration ---------------------*/
2489 #if defined(USART6)
2490 if((huart->Instance == USART1) || (huart->Instance == USART6))
2492 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2494 #else
2495 if(huart->Instance == USART1)
2497 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2499 #endif /* USART6 */
2500 else
2502 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2505 else
2507 /*-------------------------- USART BRR Configuration ---------------------*/
2508 #if defined(USART6)
2509 if((huart->Instance == USART1) || (huart->Instance == USART6))
2511 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2513 #else
2514 if(huart->Instance == USART1)
2516 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2518 #endif /* USART6 */
2519 else
2521 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2527 * @}
2530 #endif /* HAL_UART_MODULE_ENABLED */
2532 * @}
2536 * @}
2539 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/