Merge pull request #11189 from klutvott123/move-telemetry-displayport-init
[betaflight.git] / lib / main / STM32F1 / Drivers / STM32F1xx_HAL_Driver / Src / stm32f1xx_hal_uart.c
blob88829d5356ff447dd2c02fb69da574a354213941
1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_uart.c
4 * @author MCD Application Team
5 * @version V1.1.1
6 * @date 12-May-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
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The UART HAL driver can be used as follows:
21 (#) Declare a UART_HandleTypeDef handle structure.
22 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
23 (##) Enable the USARTx interface clock.
24 (##) UART pins configuration:
25 (+++) Enable the clock for the UART GPIOs.
26 (+++) Configure the UART pins (TX as alternate function pull-up, RX as alternate function Input).
27 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
28 and HAL_UART_Receive_IT() APIs):
29 (+++) Configure the USARTx interrupt priority.
30 (+++) Enable the NVIC USART IRQ handle.
31 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
32 and HAL_UART_Receive_DMA() APIs):
33 (+++) Declare a DMA handle structure for the Tx/Rx channel.
34 (+++) Enable the DMAx interface clock.
35 (+++) Configure the declared DMA handle structure with the required
36 Tx/Rx parameters.
37 (+++) Configure the DMA Tx/Rx channel.
38 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
39 (+++) Configure the priority and enable the NVIC for the transfer complete
40 interrupt on the DMA Tx/Rx channel.
41 (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
42 (used for last byte sending completion detection in DMA non circular mode)
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
45 flow control and Mode(Receiver/Transmitter) in the huart 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
124 @endverbatim
125 [..]
126 (@) Additionnal remark: If the parity is enabled, then the MSB bit of the data written
127 in the data register is transmitted but is changed by the parity bit.
128 Depending on the frame length defined by the M bit (8-bits or 9-bits),
129 the possible UART frame formats are as listed in the following table:
130 +-------------------------------------------------------------+
131 | M bit | PCE bit | UART frame |
132 |---------------------|---------------------------------------|
133 | 0 | 0 | | SB | 8 bit data | STB | |
134 |---------|-----------|---------------------------------------|
135 | 0 | 1 | | SB | 7 bit data | PB | STB | |
136 |---------|-----------|---------------------------------------|
137 | 1 | 0 | | SB | 9 bit data | STB | |
138 |---------|-----------|---------------------------------------|
139 | 1 | 1 | | SB | 8 bit data | PB | STB | |
140 +-------------------------------------------------------------+
141 ******************************************************************************
142 * @attention
144 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
146 * Redistribution and use in source and binary forms, with or without modification,
147 * are permitted provided that the following conditions are met:
148 * 1. Redistributions of source code must retain the above copyright notice,
149 * this list of conditions and the following disclaimer.
150 * 2. Redistributions in binary form must reproduce the above copyright notice,
151 * this list of conditions and the following disclaimer in the documentation
152 * and/or other materials provided with the distribution.
153 * 3. Neither the name of STMicroelectronics nor the names of its contributors
154 * may be used to endorse or promote products derived from this software
155 * without specific prior written permission.
157 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
158 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
159 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
160 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
161 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
162 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
163 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
164 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
165 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
166 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 ******************************************************************************
171 /* Includes ------------------------------------------------------------------*/
172 #include "stm32f1xx_hal.h"
174 /** @addtogroup STM32F1xx_HAL_Driver
175 * @{
178 /** @defgroup UART UART
179 * @brief HAL UART module driver
180 * @{
182 #ifdef HAL_UART_MODULE_ENABLED
184 /* Private typedef -----------------------------------------------------------*/
185 /* Private define ------------------------------------------------------------*/
186 /** @addtogroup UART_Private_Constants
187 * @{
190 * @}
192 /* Private macro -------------------------------------------------------------*/
193 /* Private variables ---------------------------------------------------------*/
194 /* Private function prototypes -----------------------------------------------*/
195 /** @addtogroup UART_Private_Functions
196 * @{
198 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
199 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
200 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
201 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
202 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
203 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
204 static void UART_DMAError(DMA_HandleTypeDef *hdma);
205 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
206 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
207 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
208 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
209 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
210 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
211 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
212 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
213 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
214 static void UART_SetConfig (UART_HandleTypeDef *huart);
216 * @}
218 /* Exported functions ---------------------------------------------------------*/
219 /** @defgroup UART_Exported_Functions UART Exported Functions
220 * @{
223 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
224 * @brief Initialization and Configuration functions
226 @verbatim
227 ==============================================================================
228 ##### Initialization and Configuration functions #####
229 ==============================================================================
230 [..]
231 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
232 in asynchronous mode.
233 (+) For the asynchronous mode only these parameters can be configured:
234 (++) Baud Rate
235 (++) Word Length
236 (++) Stop Bit
237 (++) Parity: If the parity is enabled, then the MSB bit of the data written
238 in the data register is transmitted but is changed by the parity bit.
239 Depending on the frame length defined by the M bit (8-bits or 9-bits),
240 please refer to Reference manual for possible UART frame formats.
241 (++) Hardware flow control
242 (++) Receiver/transmitter modes
243 (++) Over Sampling Method
244 [..]
245 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
246 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
247 configuration procedures (details for the procedures are available in reference manuals
248 (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
250 @endverbatim
251 * @{
255 * @brief Initializes the UART mode according to the specified parameters in
256 * the UART_InitTypeDef and create the associated handle.
257 * @param huart: pointer to a UART_HandleTypeDef structure that contains
258 * the configuration information for the specified UART module.
259 * @retval HAL status
261 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
263 /* Check the UART handle allocation */
264 if(huart == NULL)
266 return HAL_ERROR;
269 /* Check the parameters */
270 if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
272 /* The hardware flow control is available only for USART1, USART2, USART3 */
273 assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
274 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
276 else
278 assert_param(IS_UART_INSTANCE(huart->Instance));
280 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
281 #if defined(USART_CR1_OVER8)
282 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
283 #endif /* USART_CR1_OVER8 */
285 if(huart->gState == HAL_UART_STATE_RESET)
287 /* Allocate lock resource and initialize it */
288 huart->Lock = HAL_UNLOCKED;
290 /* Init the low level hardware */
291 HAL_UART_MspInit(huart);
294 huart->gState = HAL_UART_STATE_BUSY;
296 /* Disable the peripheral */
297 __HAL_UART_DISABLE(huart);
299 /* Set the UART Communication parameters */
300 UART_SetConfig(huart);
302 /* In asynchronous mode, the following bits must be kept cleared:
303 - LINEN and CLKEN bits in the USART_CR2 register,
304 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
305 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
306 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
308 /* Enable the peripheral */
309 __HAL_UART_ENABLE(huart);
311 /* Initialize the UART state */
312 huart->ErrorCode = HAL_UART_ERROR_NONE;
313 huart->gState= HAL_UART_STATE_READY;
314 huart->RxState= HAL_UART_STATE_READY;
316 return HAL_OK;
320 * @brief Initializes the half-duplex mode according to the specified
321 * parameters in the UART_InitTypeDef and create the associated handle.
322 * @param huart: pointer to a UART_HandleTypeDef structure that contains
323 * the configuration information for the specified UART module.
324 * @retval HAL status
326 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
328 /* Check the UART handle allocation */
329 if(huart == NULL)
331 return HAL_ERROR;
334 /* Check the parameters */
335 assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
336 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
337 #if defined(USART_CR1_OVER8)
338 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
339 #endif /* USART_CR1_OVER8 */
340 if(huart->gState == HAL_UART_STATE_RESET)
342 /* Allocate lock resource and initialize it */
343 huart->Lock = HAL_UNLOCKED;
344 /* Init the low level hardware */
345 HAL_UART_MspInit(huart);
348 huart->gState = HAL_UART_STATE_BUSY;
350 /* Disable the peripheral */
351 __HAL_UART_DISABLE(huart);
353 /* Set the UART Communication parameters */
354 UART_SetConfig(huart);
356 /* In half-duplex mode, the following bits must be kept cleared:
357 - LINEN and CLKEN bits in the USART_CR2 register,
358 - SCEN and IREN bits in the USART_CR3 register.*/
359 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
360 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
362 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
363 SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
365 /* Enable the peripheral */
366 __HAL_UART_ENABLE(huart);
368 /* Initialize the UART state*/
369 huart->ErrorCode = HAL_UART_ERROR_NONE;
370 huart->gState= HAL_UART_STATE_READY;
371 huart->RxState= HAL_UART_STATE_READY;
373 return HAL_OK;
377 * @brief Initializes the LIN mode according to the specified
378 * parameters in the UART_InitTypeDef and create the associated handle.
379 * @param huart: pointer to a UART_HandleTypeDef structure that contains
380 * the configuration information for the specified UART module.
381 * @param BreakDetectLength: Specifies the LIN break detection length.
382 * This parameter can be one of the following values:
383 * @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
384 * @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
385 * @retval HAL status
387 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
389 /* Check the UART handle allocation */
390 if(huart == NULL)
392 return HAL_ERROR;
395 /* Check the LIN UART instance */
396 assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
397 /* Check the Break detection length parameter */
398 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
399 assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
400 #if defined(USART_CR1_OVER8)
401 assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
402 #endif /* USART_CR1_OVER8 */
404 if(huart->gState == HAL_UART_STATE_RESET)
406 /* Allocate lock resource and initialize it */
407 huart->Lock = HAL_UNLOCKED;
408 /* Init the low level hardware */
409 HAL_UART_MspInit(huart);
412 huart->gState = HAL_UART_STATE_BUSY;
414 /* Disable the peripheral */
415 __HAL_UART_DISABLE(huart);
417 /* Set the UART Communication parameters */
418 UART_SetConfig(huart);
420 /* In LIN mode, the following bits must be kept cleared:
421 - CLKEN bits in the USART_CR2 register,
422 - SCEN and IREN bits in the USART_CR3 register.*/
423 CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
424 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
426 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
427 SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
429 /* Set the USART LIN Break detection length. */
430 MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
432 /* Enable the peripheral */
433 __HAL_UART_ENABLE(huart);
435 /* Initialize the UART state*/
436 huart->ErrorCode = HAL_UART_ERROR_NONE;
437 huart->gState= HAL_UART_STATE_READY;
438 huart->RxState= HAL_UART_STATE_READY;
440 return HAL_OK;
444 * @brief Initializes the Multi-Processor mode according to the specified
445 * parameters in the UART_InitTypeDef and create the associated handle.
446 * @param huart: pointer to a UART_HandleTypeDef structure that contains
447 * the configuration information for the specified UART module.
448 * @param Address: USART address
449 * @param WakeUpMethod: specifies the USART wake-up method.
450 * This parameter can be one of the following values:
451 * @arg UART_WAKEUPMETHOD_IDLELINE: Wake-up by an idle line detection
452 * @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wake-up by an address mark
453 * @retval HAL status
455 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
457 /* Check the UART handle allocation */
458 if(huart == NULL)
460 return HAL_ERROR;
463 /* Check UART instance capabilities */
464 assert_param(IS_UART_MULTIPROCESSOR_INSTANCE(huart->Instance));
466 /* Check the Address & wake up method parameters */
467 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
468 assert_param(IS_UART_ADDRESS(Address));
469 assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
470 #if defined(USART_CR1_OVER8)
471 assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
472 #endif /* USART_CR1_OVER8 */
474 if(huart->gState == HAL_UART_STATE_RESET)
476 /* Allocate lock resource and initialize it */
477 huart->Lock = HAL_UNLOCKED;
478 /* Init the low level hardware */
479 HAL_UART_MspInit(huart);
482 huart->gState = HAL_UART_STATE_BUSY;
484 /* Disable the peripheral */
485 __HAL_UART_DISABLE(huart);
487 /* Set the UART Communication parameters */
488 UART_SetConfig(huart);
490 /* In Multi-Processor mode, the following bits must be kept cleared:
491 - LINEN and CLKEN bits in the USART_CR2 register,
492 - SCEN, HDSEL and IREN bits in the USART_CR3 register */
493 CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
494 CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
496 /* Set the USART address node */
497 MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, Address);
499 /* Set the wake up method by setting the WAKE bit in the CR1 register */
500 MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
502 /* Enable the peripheral */
503 __HAL_UART_ENABLE(huart);
505 /* Initialize the UART state */
506 huart->ErrorCode = HAL_UART_ERROR_NONE;
507 huart->gState = HAL_UART_STATE_READY;
508 huart->RxState = HAL_UART_STATE_READY;
510 return HAL_OK;
514 * @brief DeInitializes the UART peripheral.
515 * @param huart: pointer to a UART_HandleTypeDef structure that contains
516 * the configuration information for the specified UART module.
517 * @retval HAL status
519 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
521 /* Check the UART handle allocation */
522 if(huart == NULL)
524 return HAL_ERROR;
527 /* Check the parameters */
528 assert_param(IS_UART_INSTANCE(huart->Instance));
530 huart->gState = HAL_UART_STATE_BUSY;
532 /* DeInit the low level hardware */
533 HAL_UART_MspDeInit(huart);
535 huart->ErrorCode = HAL_UART_ERROR_NONE;
536 huart->gState = HAL_UART_STATE_RESET;
537 huart->RxState = HAL_UART_STATE_RESET;
539 /* Process Unlock */
540 __HAL_UNLOCK(huart);
542 return HAL_OK;
546 * @brief UART MSP Init.
547 * @param huart: pointer to a UART_HandleTypeDef structure that contains
548 * the configuration information for the specified UART module.
549 * @retval None
551 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
553 /* Prevent unused argument(s) compilation warning */
554 UNUSED(huart);
555 /* NOTE: This function should not be modified, when the callback is needed,
556 the HAL_UART_MspInit could be implemented in the user file
561 * @brief UART MSP DeInit.
562 * @param huart: pointer to a UART_HandleTypeDef structure that contains
563 * the configuration information for the specified UART module.
564 * @retval None
566 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
568 /* Prevent unused argument(s) compilation warning */
569 UNUSED(huart);
570 /* NOTE: This function should not be modified, when the callback is needed,
571 the HAL_UART_MspDeInit could be implemented in the user file
576 * @}
579 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
580 * @brief UART Transmit and Receive functions
582 @verbatim
583 ==============================================================================
584 ##### IO operation functions #####
585 ==============================================================================
586 [..]
587 This subsection provides a set of functions allowing to manage the UART asynchronous
588 and Half duplex data transfers.
590 (#) There are two modes of transfer:
591 (++) Blocking mode: The communication is performed in polling mode.
592 The HAL status of all data processing is returned by the same function
593 after finishing transfer.
594 (++) Non blocking mode: The communication is performed using Interrupts
595 or DMA, these APIs return the HAL status.
596 The end of the data processing will be indicated through the
597 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
598 using DMA mode.
599 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
600 will be executed respectively at the end of the transmit or receive process.
601 The HAL_UART_ErrorCallback() user callback will be executed when
602 a communication error is detected.
604 (#) Blocking mode APIs are:
605 (++) HAL_UART_Transmit()
606 (++) HAL_UART_Receive()
608 (#) Non Blocking mode APIs with Interrupt are:
609 (++) HAL_UART_Transmit_IT()
610 (++) HAL_UART_Receive_IT()
611 (++) HAL_UART_IRQHandler()
613 (#) Non Blocking mode functions with DMA are:
614 (++) HAL_UART_Transmit_DMA()
615 (++) HAL_UART_Receive_DMA()
616 (++) HAL_UART_DMAPause()
617 (++) HAL_UART_DMAResume()
618 (++) HAL_UART_DMAStop()
620 (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
621 (++) HAL_UART_TxHalfCpltCallback()
622 (++) HAL_UART_TxCpltCallback()
623 (++) HAL_UART_RxHalfCpltCallback()
624 (++) HAL_UART_RxCpltCallback()
625 (++) HAL_UART_ErrorCallback()
627 [..]
628 (@) In the Half duplex communication, it is forbidden to run the transmit
629 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
630 can't be useful.
632 @endverbatim
633 * @{
637 * @brief Sends an amount of data in blocking mode.
638 * @param huart: pointer to a UART_HandleTypeDef structure that contains
639 * the configuration information for the specified UART module.
640 * @param pData: Pointer to data buffer
641 * @param Size: Amount of data to be sent
642 * @param Timeout: Timeout duration
643 * @retval HAL status
645 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
647 uint16_t* tmp;
648 uint32_t tickstart = 0U;
650 /* Check that a Tx process is not already ongoing */
651 if(huart->gState == HAL_UART_STATE_READY)
653 if((pData == NULL) || (Size == 0U))
655 return HAL_ERROR;
658 /* Process Locked */
659 __HAL_LOCK(huart);
661 huart->ErrorCode = HAL_UART_ERROR_NONE;
662 huart->gState = HAL_UART_STATE_BUSY_TX;
664 /* Init tickstart for timeout managment */
665 tickstart = HAL_GetTick();
667 huart->TxXferSize = Size;
668 huart->TxXferCount = Size;
669 while(huart->TxXferCount > 0U)
671 huart->TxXferCount--;
672 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
674 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
676 return HAL_TIMEOUT;
678 tmp = (uint16_t*) pData;
679 huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
680 if(huart->Init.Parity == UART_PARITY_NONE)
682 pData +=2U;
684 else
686 pData +=1U;
689 else
691 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
693 return HAL_TIMEOUT;
695 huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
699 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
701 return HAL_TIMEOUT;
704 /* At end of Tx process, restore huart->gState to Ready */
705 huart->gState = HAL_UART_STATE_READY;
707 /* Process Unlocked */
708 __HAL_UNLOCK(huart);
710 return HAL_OK;
712 else
714 return HAL_BUSY;
719 * @brief Receive an amount of data in blocking mode.
720 * @param huart: pointer to a UART_HandleTypeDef structure that contains
721 * the configuration information for the specified UART module.
722 * @param pData: Pointer to data buffer
723 * @param Size: Amount of data to be received
724 * @param Timeout: Timeout duration
725 * @retval HAL status
727 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
729 uint16_t* tmp;
730 uint32_t tickstart = 0U;
732 /* Check that a Rx process is not already ongoing */
733 if(huart->RxState == HAL_UART_STATE_READY)
735 if((pData == NULL) || (Size == 0U))
737 return HAL_ERROR;
740 /* Process Locked */
741 __HAL_LOCK(huart);
743 huart->ErrorCode = HAL_UART_ERROR_NONE;
744 huart->RxState = HAL_UART_STATE_BUSY_RX;
746 /* Init tickstart for timeout managment */
747 tickstart = HAL_GetTick();
749 huart->RxXferSize = Size;
750 huart->RxXferCount = Size;
752 /* Check the remain data to be received */
753 while(huart->RxXferCount > 0U)
755 huart->RxXferCount--;
756 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
758 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
760 return HAL_TIMEOUT;
762 tmp = (uint16_t*)pData;
763 if(huart->Init.Parity == UART_PARITY_NONE)
765 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
766 pData +=2U;
768 else
770 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
771 pData +=1U;
775 else
777 if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
779 return HAL_TIMEOUT;
781 if(huart->Init.Parity == UART_PARITY_NONE)
783 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
785 else
787 *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
793 /* At end of Rx process, restore huart->RxState to Ready */
794 huart->RxState = HAL_UART_STATE_READY;
796 /* Process Unlocked */
797 __HAL_UNLOCK(huart);
799 return HAL_OK;
801 else
803 return HAL_BUSY;
808 * @brief Sends an amount of data in non blocking mode.
809 * @param huart: pointer to a UART_HandleTypeDef structure that contains
810 * the configuration information for the specified UART module.
811 * @param pData: Pointer to data buffer
812 * @param Size: Amount of data to be sent
813 * @retval HAL status
815 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
817 /* Check that a Tx process is not already ongoing */
818 if(huart->gState == HAL_UART_STATE_READY)
820 if((pData == NULL) || (Size == 0U))
822 return HAL_ERROR;
824 /* Process Locked */
825 __HAL_LOCK(huart);
827 huart->pTxBuffPtr = pData;
828 huart->TxXferSize = Size;
829 huart->TxXferCount = Size;
831 huart->ErrorCode = HAL_UART_ERROR_NONE;
832 huart->gState = HAL_UART_STATE_BUSY_TX;
834 /* Process Unlocked */
835 __HAL_UNLOCK(huart);
837 /* Enable the UART Transmit data register empty Interrupt */
838 __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
840 return HAL_OK;
842 else
844 return HAL_BUSY;
849 * @brief Receives an amount of data in non blocking mode.
850 * @param huart: pointer to a UART_HandleTypeDef structure that contains
851 * the configuration information for the specified UART module.
852 * @param pData: Pointer to data buffer
853 * @param Size: Amount of data to be received
854 * @retval HAL status
856 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
858 /* Check that a Rx process is not already ongoing */
859 if(huart->RxState == HAL_UART_STATE_READY)
861 if((pData == NULL) || (Size == 0U))
863 return HAL_ERROR;
866 /* Process Locked */
867 __HAL_LOCK(huart);
869 huart->pRxBuffPtr = pData;
870 huart->RxXferSize = Size;
871 huart->RxXferCount = Size;
873 huart->ErrorCode = HAL_UART_ERROR_NONE;
874 huart->RxState = HAL_UART_STATE_BUSY_RX;
876 /* Process Unlocked */
877 __HAL_UNLOCK(huart);
879 /* Enable the UART Parity Error Interrupt */
880 __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
882 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
883 __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
885 /* Enable the UART Data Register not empty Interrupt */
886 __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
888 return HAL_OK;
890 else
892 return HAL_BUSY;
897 * @brief Sends an amount of data in non blocking mode.
898 * @param huart: pointer to a UART_HandleTypeDef structure that contains
899 * the configuration information for the specified UART module.
900 * @param pData: Pointer to data buffer
901 * @param Size: Amount of data to be sent
902 * @retval HAL status
904 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
906 uint32_t *tmp;
908 /* Check that a Tx process is not already ongoing */
909 if(huart->gState == HAL_UART_STATE_READY)
911 if((pData == NULL) || (Size == 0U))
913 return HAL_ERROR;
916 /* Process Locked */
917 __HAL_LOCK(huart);
919 huart->pTxBuffPtr = pData;
920 huart->TxXferSize = Size;
921 huart->TxXferCount = Size;
923 huart->ErrorCode = HAL_UART_ERROR_NONE;
924 huart->gState = HAL_UART_STATE_BUSY_TX;
926 /* Set the UART DMA transfer complete callback */
927 huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
929 /* Set the UART DMA Half transfer complete callback */
930 huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
932 /* Set the DMA error callback */
933 huart->hdmatx->XferErrorCallback = UART_DMAError;
935 /* Set the DMA abort callback */
936 huart->hdmatx->XferAbortCallback = NULL;
938 /* Enable the UART transmit DMA channel */
939 tmp = (uint32_t*)&pData;
940 HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
942 /* Clear the TC flag in the SR register by writing 0 to it */
943 __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
945 /* Process Unlocked */
946 __HAL_UNLOCK(huart);
948 /* Enable the DMA transfer for transmit request by setting the DMAT bit
949 in the UART CR3 register */
950 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
952 return HAL_OK;
954 else
956 return HAL_BUSY;
961 * @brief Receives an amount of data in non blocking mode.
962 * @param huart: pointer to a UART_HandleTypeDef structure that contains
963 * the configuration information for the specified UART module.
964 * @param pData: Pointer to data buffer
965 * @param Size: Amount of data to be received
966 * @note When the UART parity is enabled (PCE = 1) the data received contain the parity bit.
967 * @retval HAL status
969 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
971 uint32_t *tmp;
973 /* Check that a Rx process is not already ongoing */
974 if(huart->RxState == HAL_UART_STATE_READY)
976 if((pData == NULL) || (Size == 0U))
978 return HAL_ERROR;
981 /* Process Locked */
982 __HAL_LOCK(huart);
984 huart->pRxBuffPtr = pData;
985 huart->RxXferSize = Size;
987 huart->ErrorCode = HAL_UART_ERROR_NONE;
988 huart->RxState = HAL_UART_STATE_BUSY_RX;
990 /* Set the UART DMA transfer complete callback */
991 huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
993 /* Set the UART DMA Half transfer complete callback */
994 huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
996 /* Set the DMA error callback */
997 huart->hdmarx->XferErrorCallback = UART_DMAError;
999 /* Set the DMA abort callback */
1000 huart->hdmarx->XferAbortCallback = NULL;
1002 /* Enable the DMA channel */
1003 tmp = (uint32_t*)&pData;
1004 HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
1006 /* Clear the Overrun flag just before enabling the DMA Rx request: can be mandatory for the second transfer */
1007 __HAL_UART_CLEAR_OREFLAG(huart);
1009 /* Process Unlocked */
1010 __HAL_UNLOCK(huart);
1012 /* Enable the UART Parity Error Interrupt */
1013 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1015 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1016 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1018 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1019 in the UART CR3 register */
1020 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1022 return HAL_OK;
1024 else
1026 return HAL_BUSY;
1031 * @brief Pauses the DMA Transfer.
1032 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1033 * the configuration information for the specified UART module.
1034 * @retval HAL status
1036 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1038 uint32_t dmarequest = 0x00U;
1040 /* Process Locked */
1041 __HAL_LOCK(huart);
1043 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1044 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1046 /* Disable the UART DMA Tx request */
1047 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1050 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1051 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1053 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1054 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1055 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1057 /* Disable the UART DMA Rx request */
1058 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1061 /* Process Unlocked */
1062 __HAL_UNLOCK(huart);
1064 return HAL_OK;
1068 * @brief Resumes the DMA Transfer.
1069 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1070 * the configuration information for the specified UART module.
1071 * @retval HAL status
1073 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1075 /* Process Locked */
1076 __HAL_LOCK(huart);
1078 if(huart->gState == HAL_UART_STATE_BUSY_TX)
1080 /* Enable the UART DMA Tx request */
1081 SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1084 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
1086 /* Clear the Overrun flag before resuming the Rx transfer*/
1087 __HAL_UART_CLEAR_OREFLAG(huart);
1089 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1090 SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1091 SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1093 /* Enable the UART DMA Rx request */
1094 SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1097 /* Process Unlocked */
1098 __HAL_UNLOCK(huart);
1100 return HAL_OK;
1104 * @brief Stops the DMA Transfer.
1105 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1106 * the configuration information for the specified UART module.
1107 * @retval HAL status
1109 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1111 uint32_t dmarequest = 0x00U;
1112 /* The Lock is not implemented on this API to allow the user application
1113 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
1114 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1115 and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
1118 /* Stop UART DMA Tx request if ongoing */
1119 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
1120 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
1122 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1124 /* Abort the UART DMA Tx channel */
1125 if(huart->hdmatx != NULL)
1127 HAL_DMA_Abort(huart->hdmatx);
1129 UART_EndTxTransfer(huart);
1132 /* Stop UART DMA Rx request if ongoing */
1133 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1134 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
1136 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1138 /* Abort the UART DMA Rx channel */
1139 if(huart->hdmarx != NULL)
1141 HAL_DMA_Abort(huart->hdmarx);
1143 UART_EndRxTransfer(huart);
1146 return HAL_OK;
1150 * @brief Abort ongoing transfers (blocking mode).
1151 * @param huart UART handle.
1152 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1153 * This procedure performs following operations :
1154 * - Disable PPP Interrupts
1155 * - Disable the DMA transfer in the peripheral register (if enabled)
1156 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1157 * - Set handle State to READY
1158 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1159 * @retval HAL status
1161 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1163 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1164 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1165 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1167 /* Disable the UART DMA Tx request if enabled */
1168 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1170 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1172 /* Abort the UART DMA Tx channel: use blocking DMA Abort API (no callback) */
1173 if(huart->hdmatx != NULL)
1175 /* Set the UART DMA Abort callback to Null.
1176 No call back execution at end of DMA abort procedure */
1177 huart->hdmatx->XferAbortCallback = NULL;
1179 HAL_DMA_Abort(huart->hdmatx);
1183 /* Disable the UART DMA Rx request if enabled */
1184 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1186 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1188 /* Abort the UART DMA Rx channel: use blocking DMA Abort API (no callback) */
1189 if(huart->hdmarx != NULL)
1191 /* Set the UART DMA Abort callback to Null.
1192 No call back execution at end of DMA abort procedure */
1193 huart->hdmarx->XferAbortCallback = NULL;
1195 HAL_DMA_Abort(huart->hdmarx);
1199 /* Reset Tx and Rx transfer counters */
1200 huart->TxXferCount = 0x00U;
1201 huart->RxXferCount = 0x00U;
1203 /* Reset ErrorCode */
1204 huart->ErrorCode = HAL_UART_ERROR_NONE;
1206 /* Restore huart->RxState and huart->gState to Ready */
1207 huart->RxState = HAL_UART_STATE_READY;
1208 huart->gState = HAL_UART_STATE_READY;
1210 return HAL_OK;
1214 * @brief Abort ongoing Transmit transfer (blocking mode).
1215 * @param huart UART handle.
1216 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1217 * This procedure performs following operations :
1218 * - Disable PPP Interrupts
1219 * - Disable the DMA transfer in the peripheral register (if enabled)
1220 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1221 * - Set handle State to READY
1222 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1223 * @retval HAL status
1225 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1227 /* Disable TXEIE and TCIE interrupts */
1228 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1230 /* Disable the UART DMA Tx request if enabled */
1231 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1233 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1235 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1236 if(huart->hdmatx != NULL)
1238 /* Set the UART DMA Abort callback to Null.
1239 No call back execution at end of DMA abort procedure */
1240 huart->hdmatx->XferAbortCallback = NULL;
1242 HAL_DMA_Abort(huart->hdmatx);
1246 /* Reset Tx transfer counter */
1247 huart->TxXferCount = 0x00U;
1249 /* Restore huart->gState to Ready */
1250 huart->gState = HAL_UART_STATE_READY;
1252 return HAL_OK;
1256 * @brief Abort ongoing Receive transfer (blocking mode).
1257 * @param huart UART handle.
1258 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1259 * This procedure performs following operations :
1260 * - Disable PPP Interrupts
1261 * - Disable the DMA transfer in the peripheral register (if enabled)
1262 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1263 * - Set handle State to READY
1264 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1265 * @retval HAL status
1267 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1269 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1270 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1271 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1273 /* Disable the UART DMA Rx request if enabled */
1274 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1276 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1278 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1279 if(huart->hdmarx != NULL)
1281 /* Set the UART DMA Abort callback to Null.
1282 No call back execution at end of DMA abort procedure */
1283 huart->hdmarx->XferAbortCallback = NULL;
1285 HAL_DMA_Abort(huart->hdmarx);
1289 /* Reset Rx transfer counter */
1290 huart->RxXferCount = 0x00U;
1292 /* Restore huart->RxState to Ready */
1293 huart->RxState = HAL_UART_STATE_READY;
1295 return HAL_OK;
1299 * @brief Abort ongoing transfers (Interrupt mode).
1300 * @param huart UART handle.
1301 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1302 * This procedure performs following operations :
1303 * - Disable PPP Interrupts
1304 * - Disable the DMA transfer in the peripheral register (if enabled)
1305 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1306 * - Set handle State to READY
1307 * - At abort completion, call user abort complete callback
1308 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1309 * considered as completed only when user abort complete callback is executed (not when exiting function).
1310 * @retval HAL status
1312 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1314 uint32_t AbortCplt = 0x01U;
1316 /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1317 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
1318 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1320 /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1321 before any call to DMA Abort functions */
1322 /* DMA Tx Handle is valid */
1323 if(huart->hdmatx != NULL)
1325 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1326 Otherwise, set it to NULL */
1327 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1329 huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
1331 else
1333 huart->hdmatx->XferAbortCallback = NULL;
1336 /* DMA Rx Handle is valid */
1337 if(huart->hdmarx != NULL)
1339 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1340 Otherwise, set it to NULL */
1341 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1343 huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
1345 else
1347 huart->hdmarx->XferAbortCallback = NULL;
1351 /* Disable the UART DMA Tx request if enabled */
1352 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1354 /* Disable DMA Tx at UART level */
1355 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1357 /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1358 if(huart->hdmatx != NULL)
1360 /* UART Tx DMA Abort callback has already been initialised :
1361 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1363 /* Abort DMA TX */
1364 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1366 huart->hdmatx->XferAbortCallback = NULL;
1368 else
1370 AbortCplt = 0x00U;
1375 /* Disable the UART DMA Rx request if enabled */
1376 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1378 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1380 /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1381 if(huart->hdmarx != NULL)
1383 /* UART Rx DMA Abort callback has already been initialised :
1384 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1386 /* Abort DMA RX */
1387 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1389 huart->hdmarx->XferAbortCallback = NULL;
1390 AbortCplt = 0x01U;
1392 else
1394 AbortCplt = 0x00U;
1399 /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1400 if(AbortCplt == 0x01U)
1402 /* Reset Tx and Rx transfer counters */
1403 huart->TxXferCount = 0x00U;
1404 huart->RxXferCount = 0x00U;
1406 /* Reset ErrorCode */
1407 huart->ErrorCode = HAL_UART_ERROR_NONE;
1409 /* Restore huart->gState and huart->RxState to Ready */
1410 huart->gState = HAL_UART_STATE_READY;
1411 huart->RxState = HAL_UART_STATE_READY;
1413 /* As no DMA to be aborted, call directly user Abort complete callback */
1414 HAL_UART_AbortCpltCallback(huart);
1417 return HAL_OK;
1421 * @brief Abort ongoing Transmit transfer (Interrupt mode).
1422 * @param huart UART handle.
1423 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1424 * This procedure performs following operations :
1425 * - Disable PPP Interrupts
1426 * - Disable the DMA transfer in the peripheral register (if enabled)
1427 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1428 * - Set handle State to READY
1429 * - At abort completion, call user abort complete callback
1430 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1431 * considered as completed only when user abort complete callback is executed (not when exiting function).
1432 * @retval HAL status
1434 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
1436 /* Disable TXEIE and TCIE interrupts */
1437 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
1439 /* Disable the UART DMA Tx request if enabled */
1440 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1442 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1444 /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1445 if(huart->hdmatx != NULL)
1447 /* Set the UART DMA Abort callback :
1448 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1449 huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
1451 /* Abort DMA TX */
1452 if(HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1454 /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
1455 huart->hdmatx->XferAbortCallback(huart->hdmatx);
1458 else
1460 /* Reset Tx transfer counter */
1461 huart->TxXferCount = 0x00U;
1463 /* Restore huart->gState to Ready */
1464 huart->gState = HAL_UART_STATE_READY;
1466 /* As no DMA to be aborted, call directly user Abort complete callback */
1467 HAL_UART_AbortTransmitCpltCallback(huart);
1470 else
1472 /* Reset Tx transfer counter */
1473 huart->TxXferCount = 0x00U;
1475 /* Restore huart->gState to Ready */
1476 huart->gState = HAL_UART_STATE_READY;
1478 /* As no DMA to be aborted, call directly user Abort complete callback */
1479 HAL_UART_AbortTransmitCpltCallback(huart);
1482 return HAL_OK;
1486 * @brief Abort ongoing Receive transfer (Interrupt mode).
1487 * @param huart UART handle.
1488 * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1489 * This procedure performs following operations :
1490 * - Disable PPP Interrupts
1491 * - Disable the DMA transfer in the peripheral register (if enabled)
1492 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1493 * - Set handle State to READY
1494 * - At abort completion, call user abort complete callback
1495 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
1496 * considered as completed only when user abort complete callback is executed (not when exiting function).
1497 * @retval HAL status
1499 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
1501 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1502 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
1503 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1505 /* Disable the UART DMA Rx request if enabled */
1506 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1508 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1510 /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1511 if(huart->hdmarx != NULL)
1513 /* Set the UART DMA Abort callback :
1514 will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1515 huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
1517 /* Abort DMA RX */
1518 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1520 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1521 huart->hdmarx->XferAbortCallback(huart->hdmarx);
1524 else
1526 /* Reset Rx transfer counter */
1527 huart->RxXferCount = 0x00U;
1529 /* Restore huart->RxState to Ready */
1530 huart->RxState = HAL_UART_STATE_READY;
1532 /* As no DMA to be aborted, call directly user Abort complete callback */
1533 HAL_UART_AbortReceiveCpltCallback(huart);
1536 else
1538 /* Reset Rx transfer counter */
1539 huart->RxXferCount = 0x00U;
1541 /* Restore huart->RxState to Ready */
1542 huart->RxState = HAL_UART_STATE_READY;
1544 /* As no DMA to be aborted, call directly user Abort complete callback */
1545 HAL_UART_AbortReceiveCpltCallback(huart);
1548 return HAL_OK;
1552 * @brief This function handles UART interrupt request.
1553 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1554 * the configuration information for the specified UART module.
1555 * @retval None
1557 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
1559 uint32_t isrflags = READ_REG(huart->Instance->SR);
1560 uint32_t cr1its = READ_REG(huart->Instance->CR1);
1561 uint32_t cr3its = READ_REG(huart->Instance->CR3);
1562 uint32_t errorflags = 0x00U;
1563 uint32_t dmarequest = 0x00U;
1565 /* If no error occurs */
1566 errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
1567 if(errorflags == RESET)
1569 /* UART in mode Receiver -------------------------------------------------*/
1570 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1572 UART_Receive_IT(huart);
1573 return;
1577 /* If some errors occur */
1578 if((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
1580 /* UART parity error interrupt occurred ----------------------------------*/
1581 if(((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
1583 huart->ErrorCode |= HAL_UART_ERROR_PE;
1586 /* UART noise error interrupt occurred -----------------------------------*/
1587 if(((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1589 huart->ErrorCode |= HAL_UART_ERROR_NE;
1592 /* UART frame error interrupt occurred -----------------------------------*/
1593 if(((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1595 huart->ErrorCode |= HAL_UART_ERROR_FE;
1598 /* UART Over-Run interrupt occurred --------------------------------------*/
1599 if(((isrflags & USART_SR_ORE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
1601 huart->ErrorCode |= HAL_UART_ERROR_ORE;
1604 /* Call UART Error Call back function if need be --------------------------*/
1605 if(huart->ErrorCode != HAL_UART_ERROR_NONE)
1607 /* UART in mode Receiver -----------------------------------------------*/
1608 if(((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
1610 UART_Receive_IT(huart);
1613 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1614 consider error as blocking */
1615 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
1616 if(((huart->ErrorCode & HAL_UART_ERROR_ORE) != RESET) || dmarequest)
1618 /* Blocking error : transfer is aborted
1619 Set the UART state ready to be able to start again the process,
1620 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1621 UART_EndRxTransfer(huart);
1623 /* Disable the UART DMA Rx request if enabled */
1624 if(HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1626 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1628 /* Abort the UART DMA Rx channel */
1629 if(huart->hdmarx != NULL)
1631 /* Set the UART DMA Abort callback :
1632 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1633 huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
1634 if(HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1636 /* Call Directly XferAbortCallback function in case of error */
1637 huart->hdmarx->XferAbortCallback(huart->hdmarx);
1640 else
1642 /* Call user error callback */
1643 HAL_UART_ErrorCallback(huart);
1646 else
1648 /* Call user error callback */
1649 HAL_UART_ErrorCallback(huart);
1652 else
1654 /* Non Blocking error : transfer could go on.
1655 Error is notified to user through user error callback */
1656 HAL_UART_ErrorCallback(huart);
1657 huart->ErrorCode = HAL_UART_ERROR_NONE;
1660 return;
1661 } /* End if some error occurs */
1663 /* UART in mode Transmitter ------------------------------------------------*/
1664 if(((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
1666 UART_Transmit_IT(huart);
1667 return;
1670 /* UART in mode Transmitter end --------------------------------------------*/
1671 if(((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
1673 UART_EndTransmit_IT(huart);
1674 return;
1679 * @brief Tx Transfer completed callbacks.
1680 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1681 * the configuration information for the specified UART module.
1682 * @retval None
1684 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
1686 /* Prevent unused argument(s) compilation warning */
1687 UNUSED(huart);
1688 /* NOTE: This function Should not be modified, when the callback is needed,
1689 the HAL_UART_TxCpltCallback could be implemented in the user file
1694 * @brief Tx Half Transfer completed callbacks.
1695 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1696 * the configuration information for the specified UART module.
1697 * @retval None
1699 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
1701 /* Prevent unused argument(s) compilation warning */
1702 UNUSED(huart);
1703 /* NOTE: This function Should not be modified, when the callback is needed,
1704 the HAL_UART_TxHalfCpltCallback could be implemented in the user file
1709 * @brief Rx Transfer completed callbacks.
1710 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1711 * the configuration information for the specified UART module.
1712 * @retval None
1714 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
1716 /* Prevent unused argument(s) compilation warning */
1717 UNUSED(huart);
1718 /* NOTE: This function Should not be modified, when the callback is needed,
1719 the HAL_UART_RxCpltCallback could be implemented in the user file
1724 * @brief Rx Half Transfer completed callbacks.
1725 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1726 * the configuration information for the specified UART module.
1727 * @retval None
1729 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
1731 /* Prevent unused argument(s) compilation warning */
1732 UNUSED(huart);
1733 /* NOTE: This function Should not be modified, when the callback is needed,
1734 the HAL_UART_RxHalfCpltCallback could be implemented in the user file
1739 * @brief UART error callbacks.
1740 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1741 * the configuration information for the specified UART module.
1742 * @retval None
1744 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
1746 /* Prevent unused argument(s) compilation warning */
1747 UNUSED(huart);
1748 /* NOTE: This function Should not be modified, when the callback is needed,
1749 the HAL_UART_ErrorCallback could be implemented in the user file
1754 * @brief UART Abort Complete callback.
1755 * @param huart UART handle.
1756 * @retval None
1758 __weak void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart)
1760 /* Prevent unused argument(s) compilation warning */
1761 UNUSED(huart);
1763 /* NOTE : This function should not be modified, when the callback is needed,
1764 the HAL_UART_AbortCpltCallback can be implemented in the user file.
1768 * @brief UART Abort Complete callback.
1769 * @param huart UART handle.
1770 * @retval None
1772 __weak void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart)
1774 /* Prevent unused argument(s) compilation warning */
1775 UNUSED(huart);
1777 /* NOTE : This function should not be modified, when the callback is needed,
1778 the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
1783 * @brief UART Abort Receive Complete callback.
1784 * @param huart UART handle.
1785 * @retval None
1787 __weak void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart)
1789 /* Prevent unused argument(s) compilation warning */
1790 UNUSED(huart);
1792 /* NOTE : This function should not be modified, when the callback is needed,
1793 the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
1798 * @}
1801 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1802 * @brief UART control functions
1804 @verbatim
1805 ==============================================================================
1806 ##### Peripheral Control functions #####
1807 ==============================================================================
1808 [..]
1809 This subsection provides a set of functions allowing to control the UART:
1810 (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
1811 (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
1812 (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
1813 (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode
1814 (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode
1816 @endverbatim
1817 * @{
1821 * @brief Transmits break characters.
1822 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1823 * the configuration information for the specified UART module.
1824 * @retval HAL status
1826 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
1828 /* Check the parameters */
1829 assert_param(IS_UART_INSTANCE(huart->Instance));
1831 /* Process Locked */
1832 __HAL_LOCK(huart);
1834 huart->gState = HAL_UART_STATE_BUSY;
1836 /* Send break characters */
1837 SET_BIT(huart->Instance->CR1, USART_CR1_SBK);
1839 huart->gState = HAL_UART_STATE_READY;
1841 /* Process Unlocked */
1842 __HAL_UNLOCK(huart);
1844 return HAL_OK;
1848 * @brief Enters the UART in mute mode.
1849 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1850 * the configuration information for the specified UART module.
1851 * @retval HAL status
1853 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
1855 /* Check the parameters */
1856 assert_param(IS_UART_INSTANCE(huart->Instance));
1858 /* Process Locked */
1859 __HAL_LOCK(huart);
1861 huart->gState = HAL_UART_STATE_BUSY;
1863 /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
1864 SET_BIT(huart->Instance->CR1, USART_CR1_RWU);
1866 huart->gState = HAL_UART_STATE_READY;
1868 /* Process Unlocked */
1869 __HAL_UNLOCK(huart);
1871 return HAL_OK;
1875 * @brief Exits the UART mute mode: wake up software.
1876 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1877 * the configuration information for the specified UART module.
1878 * @retval HAL status
1880 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
1882 /* Check the parameters */
1883 assert_param(IS_UART_INSTANCE(huart->Instance));
1885 /* Process Locked */
1886 __HAL_LOCK(huart);
1888 huart->gState = HAL_UART_STATE_BUSY;
1890 /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
1891 CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU);
1893 huart->gState = HAL_UART_STATE_READY;
1895 /* Process Unlocked */
1896 __HAL_UNLOCK(huart);
1898 return HAL_OK;
1902 * @brief Enables the UART transmitter and disables the UART receiver.
1903 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1904 * the configuration information for the specified UART module.
1905 * @retval HAL status
1907 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
1909 uint32_t tmpreg = 0x00U;
1911 /* Process Locked */
1912 __HAL_LOCK(huart);
1914 huart->gState = HAL_UART_STATE_BUSY;
1916 /*-------------------------- USART CR1 Configuration -----------------------*/
1917 tmpreg = huart->Instance->CR1;
1919 /* Clear TE and RE bits */
1920 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1922 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
1923 tmpreg |= (uint32_t)USART_CR1_TE;
1925 /* Write to USART CR1 */
1926 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
1928 huart->gState = HAL_UART_STATE_READY;
1930 /* Process Unlocked */
1931 __HAL_UNLOCK(huart);
1933 return HAL_OK;
1937 * @brief Enables the UART receiver and disables the UART transmitter.
1938 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1939 * the configuration information for the specified UART module.
1940 * @retval HAL status
1942 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
1944 uint32_t tmpreg = 0x00U;
1946 /* Process Locked */
1947 __HAL_LOCK(huart);
1949 huart->gState = HAL_UART_STATE_BUSY;
1951 /*-------------------------- USART CR1 Configuration -----------------------*/
1952 tmpreg = huart->Instance->CR1;
1954 /* Clear TE and RE bits */
1955 tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_TE | USART_CR1_RE));
1957 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
1958 tmpreg |= (uint32_t)USART_CR1_RE;
1960 /* Write to USART CR1 */
1961 WRITE_REG(huart->Instance->CR1, (uint32_t)tmpreg);
1963 huart->gState = HAL_UART_STATE_READY;
1965 /* Process Unlocked */
1966 __HAL_UNLOCK(huart);
1968 return HAL_OK;
1972 * @}
1975 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
1976 * @brief UART State and Errors functions
1978 @verbatim
1979 ==============================================================================
1980 ##### Peripheral State and Errors functions #####
1981 ==============================================================================
1982 [..]
1983 This subsection provides a set of functions allowing to return the State of
1984 UART communication process, return Peripheral Errors occurred during communication
1985 process
1986 (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
1987 (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
1989 @endverbatim
1990 * @{
1994 * @brief Returns the UART state.
1995 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1996 * the configuration information for the specified UART module.
1997 * @retval HAL state
1999 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
2001 uint32_t temp1= 0x00U, temp2 = 0x00U;
2002 temp1 = huart->gState;
2003 temp2 = huart->RxState;
2005 return (HAL_UART_StateTypeDef)(temp1 | temp2);
2009 * @brief Return the UART error code
2010 * @param huart : pointer to a UART_HandleTypeDef structure that contains
2011 * the configuration information for the specified UART.
2012 * @retval UART Error Code
2014 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
2016 return huart->ErrorCode;
2020 * @}
2024 * @brief DMA UART transmit process complete callback.
2025 * @param hdma: DMA handle
2026 * @retval None
2028 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2030 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2031 /* DMA Normal mode*/
2032 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
2034 huart->TxXferCount = 0U;
2036 /* Disable the DMA transfer for transmit request by setting the DMAT bit
2037 in the UART CR3 register */
2038 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2040 /* Enable the UART Transmit Complete Interrupt */
2041 SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
2044 /* DMA Circular mode */
2045 else
2047 HAL_UART_TxCpltCallback(huart);
2052 * @brief DMA UART transmit process half complete callback
2053 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2054 * the configuration information for the specified DMA module.
2055 * @retval None
2057 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2059 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2061 HAL_UART_TxHalfCpltCallback(huart);
2065 * @brief DMA UART receive process complete callback.
2066 * @param hdma: DMA handle
2067 * @retval None
2069 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2071 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2072 /* DMA Normal mode*/
2073 if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
2075 huart->RxXferCount = 0U;
2077 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2078 CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2079 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2081 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
2082 in the UART CR3 register */
2083 CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2085 /* At end of Rx process, restore huart->RxState to Ready */
2086 huart->RxState = HAL_UART_STATE_READY;
2088 HAL_UART_RxCpltCallback(huart);
2092 * @brief DMA UART receive process half complete callback
2093 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2094 * the configuration information for the specified DMA module.
2095 * @retval None
2097 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2099 UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
2100 HAL_UART_RxHalfCpltCallback(huart);
2104 * @brief DMA UART communication error callback.
2105 * @param hdma: DMA handle
2106 * @retval None
2108 static void UART_DMAError(DMA_HandleTypeDef *hdma)
2110 uint32_t dmarequest = 0x00U;
2111 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2113 /* Stop UART DMA Tx request if ongoing */
2114 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT);
2115 if((huart->gState == HAL_UART_STATE_BUSY_TX) && dmarequest)
2117 huart->TxXferCount = 0U;
2118 UART_EndTxTransfer(huart);
2121 /* Stop UART DMA Rx request if ongoing */
2122 dmarequest = HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR);
2123 if((huart->RxState == HAL_UART_STATE_BUSY_RX) && dmarequest)
2125 huart->RxXferCount = 0U;
2126 UART_EndRxTransfer(huart);
2129 huart->ErrorCode |= HAL_UART_ERROR_DMA;
2130 HAL_UART_ErrorCallback(huart);
2134 * @brief This function handles UART Communication Timeout.
2135 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2136 * the configuration information for the specified UART module.
2137 * @param Flag: specifies the UART flag to check.
2138 * @param Status: The new Flag status (SET or RESET).
2139 * @param Tickstart Tick start value
2140 * @param Timeout: Timeout duration
2141 * @retval HAL status
2143 static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
2145 /* Wait until flag is set */
2146 while((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
2148 /* Check for the Timeout */
2149 if(Timeout != HAL_MAX_DELAY)
2151 if((Timeout == 0U)||((HAL_GetTick() - Tickstart ) > Timeout))
2153 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
2154 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
2155 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2157 huart->gState = HAL_UART_STATE_READY;
2158 huart->RxState = HAL_UART_STATE_READY;
2160 /* Process Unlocked */
2161 __HAL_UNLOCK(huart);
2163 return HAL_TIMEOUT;
2168 return HAL_OK;
2172 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
2173 * @param huart: UART handle.
2174 * @retval None
2176 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
2178 /* Disable TXEIE and TCIE interrupts */
2179 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
2181 /* At end of Tx process, restore huart->gState to Ready */
2182 huart->gState = HAL_UART_STATE_READY;
2186 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
2187 * @param huart: UART handle.
2188 * @retval None
2190 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
2192 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2193 CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
2194 CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2196 /* At end of Rx process, restore huart->RxState to Ready */
2197 huart->RxState = HAL_UART_STATE_READY;
2201 * @brief DMA UART communication abort callback, when initiated by HAL services on Error
2202 * (To be called at end of DMA Abort procedure following error occurrence).
2203 * @param hdma DMA handle.
2204 * @retval None
2206 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2208 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2209 huart->RxXferCount = 0x00U;
2210 huart->TxXferCount = 0x00U;
2212 HAL_UART_ErrorCallback(huart);
2216 * @brief DMA UART Tx communication abort callback, when initiated by user
2217 * (To be called at end of DMA Tx Abort procedure following user abort request).
2218 * @note When this callback is executed, User Abort complete call back is called only if no
2219 * Abort still ongoing for Rx DMA Handle.
2220 * @param hdma DMA handle.
2221 * @retval None
2223 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2225 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2227 huart->hdmatx->XferAbortCallback = NULL;
2229 /* Check if an Abort process is still ongoing */
2230 if(huart->hdmarx != NULL)
2232 if(huart->hdmarx->XferAbortCallback != NULL)
2234 return;
2238 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2239 huart->TxXferCount = 0x00U;
2240 huart->RxXferCount = 0x00U;
2242 /* Reset ErrorCode */
2243 huart->ErrorCode = HAL_UART_ERROR_NONE;
2245 /* Restore huart->gState and huart->RxState to Ready */
2246 huart->gState = HAL_UART_STATE_READY;
2247 huart->RxState = HAL_UART_STATE_READY;
2249 /* Call user Abort complete callback */
2250 HAL_UART_AbortCpltCallback(huart);
2254 * @brief DMA UART Rx communication abort callback, when initiated by user
2255 * (To be called at end of DMA Rx Abort procedure following user abort request).
2256 * @note When this callback is executed, User Abort complete call back is called only if no
2257 * Abort still ongoing for Tx DMA Handle.
2258 * @param hdma DMA handle.
2259 * @retval None
2261 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2263 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2265 huart->hdmarx->XferAbortCallback = NULL;
2267 /* Check if an Abort process is still ongoing */
2268 if(huart->hdmatx != NULL)
2270 if(huart->hdmatx->XferAbortCallback != NULL)
2272 return;
2276 /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2277 huart->TxXferCount = 0x00U;
2278 huart->RxXferCount = 0x00U;
2280 /* Reset ErrorCode */
2281 huart->ErrorCode = HAL_UART_ERROR_NONE;
2283 /* Restore huart->gState and huart->RxState to Ready */
2284 huart->gState = HAL_UART_STATE_READY;
2285 huart->RxState = HAL_UART_STATE_READY;
2287 /* Call user Abort complete callback */
2288 HAL_UART_AbortCpltCallback(huart);
2292 * @brief DMA UART Tx communication abort callback, when initiated by user by a call to
2293 * HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
2294 * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
2295 * and leads to user Tx Abort Complete callback execution).
2296 * @param hdma DMA handle.
2297 * @retval None
2299 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2301 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2303 huart->TxXferCount = 0x00U;
2305 /* Restore huart->gState to Ready */
2306 huart->gState = HAL_UART_STATE_READY;
2308 /* Call user Abort complete callback */
2309 HAL_UART_AbortTransmitCpltCallback(huart);
2313 * @brief DMA UART Rx communication abort callback, when initiated by user by a call to
2314 * HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
2315 * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
2316 * and leads to user Rx Abort Complete callback execution).
2317 * @param hdma DMA handle.
2318 * @retval None
2320 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
2322 UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2324 huart->RxXferCount = 0x00U;
2326 /* Restore huart->RxState to Ready */
2327 huart->RxState = HAL_UART_STATE_READY;
2329 /* Call user Abort complete callback */
2330 HAL_UART_AbortReceiveCpltCallback(huart);
2334 * @brief Sends an amount of data in non blocking mode.
2335 * @param huart: Pointer to a UART_HandleTypeDef structure that contains
2336 * the configuration information for the specified UART module.
2337 * @retval HAL status
2339 static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
2341 uint16_t* tmp;
2343 /* Check that a Tx process is ongoing */
2344 if(huart->gState == HAL_UART_STATE_BUSY_TX)
2346 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
2348 tmp = (uint16_t*) huart->pTxBuffPtr;
2349 huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
2350 if(huart->Init.Parity == UART_PARITY_NONE)
2352 huart->pTxBuffPtr += 2U;
2354 else
2356 huart->pTxBuffPtr += 1U;
2359 else
2361 huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
2364 if(--huart->TxXferCount == 0U)
2366 /* Disable the UART Transmit Complete Interrupt */
2367 __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
2369 /* Enable the UART Transmit Complete Interrupt */
2370 __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
2372 return HAL_OK;
2374 else
2376 return HAL_BUSY;
2381 * @brief Wraps up transmission in non blocking mode.
2382 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2383 * the configuration information for the specified UART module.
2384 * @retval HAL status
2386 static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
2388 /* Disable the UART Transmit Complete Interrupt */
2389 __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
2391 /* Tx process is ended, restore huart->gState to Ready */
2392 huart->gState = HAL_UART_STATE_READY;
2393 HAL_UART_TxCpltCallback(huart);
2395 return HAL_OK;
2399 * @brief Receives an amount of data in non blocking mode
2400 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2401 * the configuration information for the specified UART module.
2402 * @retval HAL status
2404 static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
2406 uint16_t* tmp;
2408 /* Check that a Rx process is ongoing */
2409 if(huart->RxState == HAL_UART_STATE_BUSY_RX)
2411 if(huart->Init.WordLength == UART_WORDLENGTH_9B)
2413 tmp = (uint16_t*) huart->pRxBuffPtr;
2414 if(huart->Init.Parity == UART_PARITY_NONE)
2416 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
2417 huart->pRxBuffPtr += 2U;
2419 else
2421 *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
2422 huart->pRxBuffPtr += 1U;
2425 else
2427 if(huart->Init.Parity == UART_PARITY_NONE)
2429 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
2431 else
2433 *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
2437 if(--huart->RxXferCount == 0U)
2439 /* Disable the IRDA Data Register not empty Interrupt */
2440 __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
2442 /* Disable the UART Parity Error Interrupt */
2443 __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
2444 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
2445 __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
2447 /* Rx process is completed, restore huart->RxState to Ready */
2448 huart->RxState = HAL_UART_STATE_READY;
2450 HAL_UART_RxCpltCallback(huart);
2452 return HAL_OK;
2454 return HAL_OK;
2456 else
2458 return HAL_BUSY;
2463 * @brief Configures the UART peripheral.
2464 * @param huart: pointer to a UART_HandleTypeDef structure that contains
2465 * the configuration information for the specified UART module.
2466 * @retval None
2468 static void UART_SetConfig(UART_HandleTypeDef *huart)
2470 uint32_t tmpreg = 0x00U;
2472 /* Check the parameters */
2473 assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
2474 assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
2475 assert_param(IS_UART_PARITY(huart->Init.Parity));
2476 assert_param(IS_UART_MODE(huart->Init.Mode));
2478 /*------- UART-associated USART registers setting : CR2 Configuration ------*/
2479 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
2480 * to huart->Init.StopBits value */
2481 MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
2483 /*------- UART-associated USART registers setting : CR1 Configuration ------*/
2484 /* Configure the UART Word Length, Parity and mode:
2485 Set the M bits according to huart->Init.WordLength value
2486 Set PCE and PS bits according to huart->Init.Parity value
2487 Set TE and RE bits according to huart->Init.Mode value
2488 Set OVER8 bit according to huart->Init.OverSampling value */
2490 #if defined(USART_CR1_OVER8)
2491 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling;
2492 MODIFY_REG(huart->Instance->CR1,
2493 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8),
2494 tmpreg);
2495 #else
2496 tmpreg |= (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode;
2497 MODIFY_REG(huart->Instance->CR1,
2498 (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
2499 tmpreg);
2500 #endif /* USART_CR1_OVER8 */
2502 /*------- UART-associated USART registers setting : CR3 Configuration ------*/
2503 /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
2504 MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
2506 #if defined(USART_CR1_OVER8)
2507 /* Check the Over Sampling */
2508 if(huart->Init.OverSampling == UART_OVERSAMPLING_8)
2510 /*-------------------------- USART BRR Configuration ---------------------*/
2511 if(huart->Instance == USART1)
2513 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2515 else
2517 huart->Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2520 else
2522 /*-------------------------- USART BRR Configuration ---------------------*/
2523 if(huart->Instance == USART1)
2525 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2527 else
2529 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2532 #else
2533 /*-------------------------- USART BRR Configuration ---------------------*/
2534 if(huart->Instance == USART1)
2536 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
2538 else
2540 huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
2542 #endif /* USART_CR1_OVER8 */
2546 * @}
2549 #endif /* HAL_UART_MODULE_ENABLED */
2551 * @}
2555 * @}
2558 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/