2 ******************************************************************************
3 * @file stm32f7xx_hal_uart.c
4 * @author MCD Application Team
7 * @brief UART HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State and Errors functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The UART HAL driver can be used as follows:
22 (#) Declare a UART_HandleTypeDef handle structure.
24 (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
25 (##) Enable the USARTx interface clock.
26 (##) UART pins configuration:
27 (+++) Enable the clock for the UART GPIOs.
28 (+++) Configure these UART pins as alternate function pull-up.
29 (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
30 and HAL_UART_Receive_IT() APIs):
31 (+++) Configure the USARTx interrupt priority.
32 (+++) Enable the NVIC USART IRQ handle.
33 (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
34 and HAL_UART_Receive_DMA() APIs):
35 (+++) Declare a DMA handle structure for the Tx/Rx stream.
36 (+++) Enable the DMAx interface clock.
37 (+++) Configure the declared DMA handle structure with the required
39 (+++) Configure the DMA Tx/Rx Stream.
40 (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
41 (+++) Configure the priority and enable the NVIC for the transfer complete
42 interrupt on the DMA Tx/Rx Stream.
44 (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
45 flow control and Mode(Receiver/Transmitter) in the Init structure.
47 (#) For the UART asynchronous mode, initialize the UART registers by calling
48 the HAL_UART_Init() API.
50 (#) For the UART Half duplex mode, initialize the UART registers by calling
51 the HAL_HalfDuplex_Init() API.
53 (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
55 (#) For the Multi-Processor mode, initialize the UART registers by calling
56 the HAL_MultiProcessor_Init() API.
59 (@) The specific UART interrupts (Transmission complete interrupt,
60 RXNE interrupt and Error Interrupts) will be managed using the macros
61 __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
65 (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
66 low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
67 HAL_UART_MspInit() API.
70 Three operation modes are available within this driver :
72 *** Polling mode IO operation ***
73 =================================
75 (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
76 (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
78 *** Interrupt mode IO operation ***
79 ===================================
81 (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
82 (+) At transmission end of 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 ==============================
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 =============================================
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_IT : Clears the specified UART ISR 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
123 (@) You can refer to the UART HAL driver header file for more useful macros
126 ******************************************************************************
129 * <h2><center>© COPYRIGHT(c) 2016 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 "stm32f7xx_hal.h"
159 /** @addtogroup STM32F7xx_HAL_Driver
163 /** @defgroup UART UART
164 * @brief HAL UART module driver
168 #ifdef HAL_UART_MODULE_ENABLED
170 /* Private typedef -----------------------------------------------------------*/
171 /* Private define ------------------------------------------------------------*/
172 /** @defgroup UART_Private_Constants UART Private Constants
175 #define UART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
176 USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
180 /* Private macro -------------------------------------------------------------*/
181 /* Private variables ---------------------------------------------------------*/
182 /* Private function prototypes -----------------------------------------------*/
183 /** @addtogroup UART_Private_Functions
186 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
);
187 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
);
188 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
189 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
190 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
);
191 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
);
192 static void UART_DMAError(DMA_HandleTypeDef
*hdma
);
193 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
194 static HAL_StatusTypeDef
UART_Transmit_IT(UART_HandleTypeDef
*huart
);
195 static HAL_StatusTypeDef
UART_EndTransmit_IT(UART_HandleTypeDef
*huart
);
196 static HAL_StatusTypeDef
UART_Receive_IT(UART_HandleTypeDef
*huart
);
201 /* Exported functions --------------------------------------------------------*/
203 /** @defgroup UART_Exported_Functions UART Exported Functions
207 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
208 * @brief Initialization and Configuration functions
211 ===============================================================================
212 ##### Initialization and Configuration functions #####
213 ===============================================================================
215 This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
216 in asynchronous mode.
217 (+) For the asynchronous mode only these parameters can be configured:
221 (++) Parity: If the parity is enabled, then the MSB bit of the data written
222 in the data register is transmitted but is changed by the parity bit.
223 Depending on the frame length defined by the M bit (8-bits or 9-bits),
224 please refer to Reference manual for possible UART frame formats.
225 (++) Hardware flow control
226 (++) Receiver/transmitter modes
227 (++) Over Sampling Method
229 The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
230 follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
231 configuration procedures (details for the procedures are available in reference manual (RM0329)).
238 * @brief Initializes the UART mode according to the specified
239 * parameters in the UART_InitTypeDef and creates the associated handle .
240 * @param huart: uart handle
243 HAL_StatusTypeDef
HAL_UART_Init(UART_HandleTypeDef
*huart
)
245 /* Check the UART handle allocation */
251 if(huart
->Init
.HwFlowCtl
!= UART_HWCONTROL_NONE
)
253 /* Check the parameters */
254 assert_param(IS_UART_HWFLOW_INSTANCE(huart
->Instance
));
258 /* Check the parameters */
259 assert_param(IS_UART_INSTANCE(huart
->Instance
));
262 if(huart
->gState
== HAL_UART_STATE_RESET
)
264 /* Allocate lock resource and initialize it */
265 huart
->Lock
= HAL_UNLOCKED
;
267 /* Init the low level hardware : GPIO, CLOCK */
268 HAL_UART_MspInit(huart
);
271 huart
->gState
= HAL_UART_STATE_BUSY
;
273 /* Disable the Peripheral */
274 __HAL_UART_DISABLE(huart
);
276 /* Set the UART Communication parameters */
277 if (UART_SetConfig(huart
) == HAL_ERROR
)
282 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
284 UART_AdvFeatureConfig(huart
);
287 /* In asynchronous mode, the following bits must be kept cleared:
288 - LINEN and CLKEN bits in the USART_CR2 register,
289 - SCEN, HDSEL and IREN bits in the USART_CR3 register.*/
290 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
291 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
293 /* Enable the Peripheral */
294 __HAL_UART_ENABLE(huart
);
296 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
297 return (UART_CheckIdleState(huart
));
301 * @brief Initializes the half-duplex mode according to the specified
302 * parameters in the UART_InitTypeDef and creates the associated handle .
303 * @param huart: UART handle
306 HAL_StatusTypeDef
HAL_HalfDuplex_Init(UART_HandleTypeDef
*huart
)
308 /* Check the UART handle allocation */
314 if(huart
->gState
== HAL_UART_STATE_RESET
)
316 /* Allocate lock resource and initialize it */
317 huart
->Lock
= HAL_UNLOCKED
;
319 /* Init the low level hardware : GPIO, CLOCK */
320 HAL_UART_MspInit(huart
);
323 huart
->gState
= HAL_UART_STATE_BUSY
;
325 /* Disable the Peripheral */
326 __HAL_UART_DISABLE(huart
);
328 /* Set the UART Communication parameters */
329 if (UART_SetConfig(huart
) == HAL_ERROR
)
334 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
336 UART_AdvFeatureConfig(huart
);
339 /* In half-duplex mode, the following bits must be kept cleared:
340 - LINEN and CLKEN bits in the USART_CR2 register,
341 - SCEN and IREN bits in the USART_CR3 register.*/
342 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
343 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_IREN
| USART_CR3_SCEN
));
345 /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
346 SET_BIT(huart
->Instance
->CR3
, USART_CR3_HDSEL
);
348 /* Enable the Peripheral */
349 __HAL_UART_ENABLE(huart
);
351 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
352 return (UART_CheckIdleState(huart
));
357 * @brief Initialize the LIN mode according to the specified
358 * parameters in the UART_InitTypeDef and creates the associated handle .
359 * @param huart: UART handle.
360 * @param BreakDetectLength: specifies the LIN break detection length.
361 * This parameter can be one of the following values:
362 * @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
363 * @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
366 HAL_StatusTypeDef
HAL_LIN_Init(UART_HandleTypeDef
*huart
, uint32_t BreakDetectLength
)
368 /* Check the UART handle allocation */
374 /* Check the parameters */
375 assert_param(IS_UART_INSTANCE(huart
->Instance
));
376 assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength
));
377 assert_param(IS_LIN_WORD_LENGTH(huart
->Init
.WordLength
));
379 if(huart
->gState
== HAL_UART_STATE_RESET
)
381 /* Allocate lock resource and initialize it */
382 huart
->Lock
= HAL_UNLOCKED
;
384 /* Init the low level hardware : GPIO, CLOCK */
385 HAL_UART_MspInit(huart
);
388 huart
->gState
= HAL_UART_STATE_BUSY
;
390 /* Disable the Peripheral */
391 __HAL_UART_DISABLE(huart
);
393 /* Set the UART Communication parameters */
394 if (UART_SetConfig(huart
) == HAL_ERROR
)
399 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
401 UART_AdvFeatureConfig(huart
);
404 /* In LIN mode, the following bits must be kept cleared:
405 - LINEN and CLKEN bits in the USART_CR2 register,
406 - SCEN and IREN bits in the USART_CR3 register.*/
407 CLEAR_BIT(huart
->Instance
->CR2
, USART_CR2_CLKEN
);
408 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_HDSEL
| USART_CR3_IREN
| USART_CR3_SCEN
));
410 /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
411 SET_BIT(huart
->Instance
->CR2
, USART_CR2_LINEN
);
413 /* Set the USART LIN Break detection length. */
414 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_LBDL
, BreakDetectLength
);
416 /* Enable the Peripheral */
417 __HAL_UART_ENABLE(huart
);
419 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
420 return (UART_CheckIdleState(huart
));
425 * @brief Initialize the multiprocessor mode according to the specified
426 * parameters in the UART_InitTypeDef and initialize the associated handle.
427 * @param huart: UART handle.
428 * @param Address: UART node address (4-, 6-, 7- or 8-bit long).
429 * @param WakeUpMethod: specifies the UART wakeup method.
430 * This parameter can be one of the following values:
431 * @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
432 * @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
433 * @note If the user resorts to idle line detection wake up, the Address parameter
434 * is useless and ignored by the initialization function.
435 * @note If the user resorts to address mark wake up, the address length detection
436 * is configured by default to 4 bits only. For the UART to be able to
437 * manage 6-, 7- or 8-bit long addresses detection
440 HAL_StatusTypeDef
HAL_MultiProcessor_Init(UART_HandleTypeDef
*huart
, uint8_t Address
, uint32_t WakeUpMethod
)
442 /* Check the UART handle allocation */
448 /* Check the wake up method parameter */
449 assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod
));
451 if(huart
->gState
== HAL_UART_STATE_RESET
)
453 /* Allocate lock resource and initialize it */
454 huart
->Lock
= HAL_UNLOCKED
;
456 /* Init the low level hardware : GPIO, CLOCK */
457 HAL_UART_MspInit(huart
);
460 huart
->gState
= HAL_UART_STATE_BUSY
;
462 /* Disable the Peripheral */
463 __HAL_UART_DISABLE(huart
);
465 /* Set the UART Communication parameters */
466 if (UART_SetConfig(huart
) == HAL_ERROR
)
471 if (huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
473 UART_AdvFeatureConfig(huart
);
476 /* In multiprocessor mode, the following bits must be kept cleared:
477 - LINEN and CLKEN bits in the USART_CR2 register,
478 - SCEN, HDSEL and IREN bits in the USART_CR3 register. */
479 CLEAR_BIT(huart
->Instance
->CR2
, (USART_CR2_LINEN
| USART_CR2_CLKEN
));
480 CLEAR_BIT(huart
->Instance
->CR3
, (USART_CR3_SCEN
| USART_CR3_HDSEL
| USART_CR3_IREN
));
482 if (WakeUpMethod
== UART_WAKEUPMETHOD_ADDRESSMARK
)
484 /* If address mark wake up method is chosen, set the USART address node */
485 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADD
, ((uint32_t)Address
<< UART_CR2_ADDRESS_LSB_POS
));
488 /* Set the wake up method by setting the WAKE bit in the CR1 register */
489 MODIFY_REG(huart
->Instance
->CR1
, USART_CR1_WAKE
, WakeUpMethod
);
491 /* Enable the Peripheral */
492 __HAL_UART_ENABLE(huart
);
494 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
495 return (UART_CheckIdleState(huart
));
500 * @brief Initialize the RS485 Driver enable feature according to the specified
501 * parameters in the UART_InitTypeDef and creates the associated handle.
502 * @param huart: UART handle.
503 * @param Polarity: select the driver enable polarity.
504 * This parameter can be one of the following values:
505 * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
506 * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
507 * @param AssertionTime: Driver Enable assertion time:
508 * 5-bit value defining the time between the activation of the DE (Driver Enable)
509 * signal and the beginning of the start bit. It is expressed in sample time
510 * units (1/8 or 1/16 bit time, depending on the oversampling rate)
511 * @param DeassertionTime: Driver Enable deassertion time:
512 * 5-bit value defining the time between the end of the last stop bit, in a
513 * transmitted message, and the de-activation of the DE (Driver Enable) signal.
514 * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
515 * oversampling rate).
518 HAL_StatusTypeDef
HAL_RS485Ex_Init(UART_HandleTypeDef
*huart
, uint32_t Polarity
, uint32_t AssertionTime
, uint32_t DeassertionTime
)
522 /* Check the UART handle allocation */
527 /* Check the Driver Enable UART instance */
528 assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart
->Instance
));
530 /* Check the Driver Enable polarity */
531 assert_param(IS_UART_DE_POLARITY(Polarity
));
533 /* Check the Driver Enable assertion time */
534 assert_param(IS_UART_ASSERTIONTIME(AssertionTime
));
536 /* Check the Driver Enable deassertion time */
537 assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime
));
539 if(huart
->gState
== HAL_UART_STATE_RESET
)
541 /* Allocate lock resource and initialize it */
542 huart
->Lock
= HAL_UNLOCKED
;
544 /* Init the low level hardware : GPIO, CLOCK, CORTEX */
545 HAL_UART_MspInit(huart
);
548 huart
->gState
= HAL_UART_STATE_BUSY
;
550 /* Disable the Peripheral */
551 __HAL_UART_DISABLE(huart
);
553 /* Set the UART Communication parameters */
554 if (UART_SetConfig(huart
) == HAL_ERROR
)
559 if(huart
->AdvancedInit
.AdvFeatureInit
!= UART_ADVFEATURE_NO_INIT
)
561 UART_AdvFeatureConfig(huart
);
564 /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
565 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DEM
);
567 /* Set the Driver Enable polarity */
568 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DEP
, Polarity
);
570 /* Set the Driver Enable assertion and deassertion times */
571 temp
= (AssertionTime
<< UART_CR1_DEAT_ADDRESS_LSB_POS
);
572 temp
|= (DeassertionTime
<< UART_CR1_DEDT_ADDRESS_LSB_POS
);
573 MODIFY_REG(huart
->Instance
->CR1
, (USART_CR1_DEDT
|USART_CR1_DEAT
), temp
);
575 /* Enable the Peripheral */
576 __HAL_UART_ENABLE(huart
);
578 /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
579 return (UART_CheckIdleState(huart
));
583 * @brief DeInitializes the UART peripheral
584 * @param huart: uart handle
587 HAL_StatusTypeDef
HAL_UART_DeInit(UART_HandleTypeDef
*huart
)
589 /* Check the UART handle allocation */
595 /* Check the parameters */
596 assert_param(IS_UART_INSTANCE(huart
->Instance
));
598 huart
->gState
= HAL_UART_STATE_BUSY
;
600 /* Disable the Peripheral */
601 __HAL_UART_DISABLE(huart
);
603 huart
->Instance
->CR1
= 0x0U
;
604 huart
->Instance
->CR2
= 0x0U
;
605 huart
->Instance
->CR3
= 0x0U
;
607 /* DeInit the low level hardware */
608 HAL_UART_MspDeInit(huart
);
610 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
611 huart
->gState
= HAL_UART_STATE_RESET
;
612 huart
->RxState
= HAL_UART_STATE_RESET
;
621 * @brief UART MSP Init
622 * @param huart: uart handle
625 __weak
void HAL_UART_MspInit(UART_HandleTypeDef
*huart
)
627 /* Prevent unused argument(s) compilation warning */
630 /* NOTE : This function should not be modified, when the callback is needed,
631 the HAL_UART_MspInit can be implemented in the user file
636 * @brief UART MSP DeInit
637 * @param huart: uart handle
640 __weak
void HAL_UART_MspDeInit(UART_HandleTypeDef
*huart
)
642 /* Prevent unused argument(s) compilation warning */
645 /* NOTE : This function should not be modified, when the callback is needed,
646 the HAL_UART_MspDeInit can be implemented in the user file
654 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
655 * @brief UART Transmit/Receive functions
658 ===============================================================================
659 ##### IO operation functions #####
660 ===============================================================================
661 This subsection provides a set of functions allowing to manage the UART asynchronous
662 and Half duplex data transfers.
664 (#) There are two mode of transfer:
665 (+) Blocking mode: The communication is performed in polling mode.
666 The HAL status of all data processing is returned by the same function
667 after finishing transfer.
668 (+) Non-Blocking mode: The communication is performed using Interrupts
669 or DMA, These API's return the HAL status.
670 The end of the data processing will be indicated through the
671 dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
673 The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
674 will be executed respectively at the end of the transmit or Receive process
675 The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
677 (#) Blocking mode API's are :
678 (+) HAL_UART_Transmit()
679 (+) HAL_UART_Receive()
681 (#) Non-Blocking mode API's with Interrupt are :
682 (+) HAL_UART_Transmit_IT()
683 (+) HAL_UART_Receive_IT()
684 (+) HAL_UART_IRQHandler()
685 (+) UART_Transmit_IT()
686 (+) UART_Receive_IT()
688 (#) Non-Blocking mode API's with DMA are :
689 (+) HAL_UART_Transmit_DMA()
690 (+) HAL_UART_Receive_DMA()
691 (+) HAL_UART_DMAPause()
692 (+) HAL_UART_DMAResume()
693 (+) HAL_UART_DMAStop()
695 (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
696 (+) HAL_UART_TxHalfCpltCallback()
697 (+) HAL_UART_TxCpltCallback()
698 (+) HAL_UART_RxHalfCpltCallback()
699 (+) HAL_UART_RxCpltCallback()
700 (+) HAL_UART_ErrorCallback()
703 -@- In the Half duplex communication, it is forbidden to run the transmit
704 and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
711 * @brief Send an amount of data in blocking mode.
712 * @param huart: UART handle.
713 * @param pData: Pointer to data buffer.
714 * @param Size: Amount of data to be sent.
715 * @param Timeout: Timeout duration.
718 HAL_StatusTypeDef
HAL_UART_Transmit(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
721 uint32_t tickstart
= 0U;
723 /* Check that a Tx process is not already ongoing */
724 if(huart
->gState
== HAL_UART_STATE_READY
)
726 if((pData
== NULL
) || (Size
== 0U))
734 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
735 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
737 /* Init tickstart for timeout managment*/
738 tickstart
= HAL_GetTick();
740 huart
->TxXferSize
= Size
;
741 huart
->TxXferCount
= Size
;
742 while(huart
->TxXferCount
> 0U)
744 huart
->TxXferCount
--;
745 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TXE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
749 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
751 tmp
= (uint16_t*) pData
;
752 huart
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FFU
);
757 huart
->Instance
->TDR
= (*pData
++ & (uint8_t)0xFFU
);
760 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_TC
, RESET
, tickstart
, Timeout
) != HAL_OK
)
765 /* At end of Tx process, restore huart->gState to Ready */
766 huart
->gState
= HAL_UART_STATE_READY
;
768 /* Process Unlocked */
780 * @brief Receive an amount of data in blocking mode.
781 * @param huart: UART handle.
782 * @param pData: pointer to data buffer.
783 * @param Size: amount of data to be received.
784 * @param Timeout: Timeout duration.
787 HAL_StatusTypeDef
HAL_UART_Receive(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
791 uint32_t tickstart
= 0U;
793 /* Check that a Rx process is not already ongoing */
794 if(huart
->RxState
== HAL_UART_STATE_READY
)
796 if((pData
== NULL
) || (Size
== 0U))
804 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
805 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
807 /* Init tickstart for timeout managment*/
808 tickstart
= HAL_GetTick();
810 huart
->RxXferSize
= Size
;
811 huart
->RxXferCount
= Size
;
813 /* Computation of UART mask to apply to RDR register */
814 UART_MASK_COMPUTATION(huart
);
815 uhMask
= huart
->Mask
;
817 /* as long as data have to be received */
818 while(huart
->RxXferCount
> 0U)
820 huart
->RxXferCount
--;
821 if(UART_WaitOnFlagUntilTimeout(huart
, UART_FLAG_RXNE
, RESET
, tickstart
, Timeout
) != HAL_OK
)
825 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
827 tmp
= (uint16_t*) pData
;
828 *tmp
= (uint16_t)(huart
->Instance
->RDR
& uhMask
);
833 *pData
++ = (uint8_t)(huart
->Instance
->RDR
& (uint8_t)uhMask
);
837 /* At end of Rx process, restore huart->RxState to Ready */
838 huart
->RxState
= HAL_UART_STATE_READY
;
840 /* Process Unlocked */
852 * @brief Send an amount of data in interrupt mode.
853 * @param huart: UART handle.
854 * @param pData: pointer to data buffer.
855 * @param Size: amount of data to be sent.
858 HAL_StatusTypeDef
HAL_UART_Transmit_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
860 /* Check that a Tx process is not already ongoing */
861 if(huart
->gState
== HAL_UART_STATE_READY
)
863 if((pData
== NULL
) || (Size
== 0U))
871 huart
->pTxBuffPtr
= pData
;
872 huart
->TxXferSize
= Size
;
873 huart
->TxXferCount
= Size
;
875 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
876 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
878 /* Process Unlocked */
881 /* Enable the UART Transmit Data Register Empty Interrupt */
882 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
893 * @brief Receive an amount of data in interrupt mode.
894 * @param huart: UART handle.
895 * @param pData: pointer to data buffer.
896 * @param Size: amount of data to be received.
899 HAL_StatusTypeDef
HAL_UART_Receive_IT(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
901 /* Check that a Rx process is not already ongoing */
902 if(huart
->RxState
== HAL_UART_STATE_READY
)
904 if((pData
== NULL
) || (Size
== 0U))
912 huart
->pRxBuffPtr
= pData
;
913 huart
->RxXferSize
= Size
;
914 huart
->RxXferCount
= Size
;
916 /* Computation of UART mask to apply to RDR register */
917 UART_MASK_COMPUTATION(huart
);
919 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
920 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
922 /* Process Unlocked */
925 /* Enable the UART Parity Error Interrupt */
926 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
928 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
929 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
931 /* Enable the UART Data Register not empty Interrupt */
932 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RXNEIE
);
943 * @brief Send an amount of data in DMA mode.
944 * @param huart: UART handle.
945 * @param pData: pointer to data buffer.
946 * @param Size: amount of data to be sent.
949 HAL_StatusTypeDef
HAL_UART_Transmit_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
953 /* Check that a Tx process is not already ongoing */
954 if(huart
->gState
== HAL_UART_STATE_READY
)
956 if((pData
== NULL
) || (Size
== 0U))
964 huart
->pTxBuffPtr
= pData
;
965 huart
->TxXferSize
= Size
;
966 huart
->TxXferCount
= Size
;
968 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
969 huart
->gState
= HAL_UART_STATE_BUSY_TX
;
971 /* Set the UART DMA transfer complete callback */
972 huart
->hdmatx
->XferCpltCallback
= UART_DMATransmitCplt
;
974 /* Set the UART DMA Half transfer complete callback */
975 huart
->hdmatx
->XferHalfCpltCallback
= UART_DMATxHalfCplt
;
977 /* Set the DMA error callback */
978 huart
->hdmatx
->XferErrorCallback
= UART_DMAError
;
980 /* Set the DMA abort callback */
981 huart
->hdmatx
->XferAbortCallback
= NULL
;
983 /* Enable the UART transmit DMA channel */
984 tmp
= (uint32_t*)&pData
;
985 HAL_DMA_Start_IT(huart
->hdmatx
, *(uint32_t*)tmp
, (uint32_t)&huart
->Instance
->TDR
, Size
);
987 /* Clear the TC flag in the SR register by writing 0 to it */
988 __HAL_UART_CLEAR_IT(huart
, UART_FLAG_TC
);
990 /* Process Unlocked */
993 /* Enable the DMA transfer for transmit request by setting the DMAT bit
994 in the UART CR3 register */
995 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1006 * @brief Receive an amount of data in DMA mode.
1007 * @param huart: UART handle.
1008 * @param pData: pointer to data buffer.
1009 * @param Size: amount of data to be received.
1010 * @note When the UART parity is enabled (PCE = 1), the received data contain
1011 * the parity bit (MSB position).
1012 * @retval HAL status
1014 HAL_StatusTypeDef
HAL_UART_Receive_DMA(UART_HandleTypeDef
*huart
, uint8_t *pData
, uint16_t Size
)
1018 /* Check that a Rx process is not already ongoing */
1019 if(huart
->RxState
== HAL_UART_STATE_READY
)
1021 if((pData
== NULL
) || (Size
== 0U))
1026 /* Process Locked */
1029 huart
->pRxBuffPtr
= pData
;
1030 huart
->RxXferSize
= Size
;
1032 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1033 huart
->RxState
= HAL_UART_STATE_BUSY_RX
;
1035 /* Set the UART DMA transfer complete callback */
1036 huart
->hdmarx
->XferCpltCallback
= UART_DMAReceiveCplt
;
1038 /* Set the UART DMA Half transfer complete callback */
1039 huart
->hdmarx
->XferHalfCpltCallback
= UART_DMARxHalfCplt
;
1041 /* Set the DMA error callback */
1042 huart
->hdmarx
->XferErrorCallback
= UART_DMAError
;
1044 /* Set the DMA abort callback */
1045 huart
->hdmarx
->XferAbortCallback
= NULL
;
1047 /* Enable the DMA channel */
1048 tmp
= (uint32_t*)&pData
;
1049 HAL_DMA_Start_IT(huart
->hdmarx
, (uint32_t)&huart
->Instance
->RDR
, *(uint32_t*)tmp
, Size
);
1051 /* Process Unlocked */
1052 __HAL_UNLOCK(huart
);
1054 /* Enable the UART Parity Error Interrupt */
1055 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1057 /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1058 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1060 /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1061 in the UART CR3 register */
1062 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1073 * @brief Pause the DMA Transfer.
1074 * @param huart: UART handle.
1075 * @retval HAL status
1077 HAL_StatusTypeDef
HAL_UART_DMAPause(UART_HandleTypeDef
*huart
)
1079 /* Process Locked */
1082 if ((huart
->gState
== HAL_UART_STATE_BUSY_TX
) &&
1083 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)))
1085 /* Disable the UART DMA Tx request */
1086 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1088 if ((huart
->RxState
== HAL_UART_STATE_BUSY_RX
) &&
1089 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1091 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1092 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1093 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1095 /* Disable the UART DMA Rx request */
1096 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1099 /* Process Unlocked */
1100 __HAL_UNLOCK(huart
);
1106 * @brief Resume the DMA Transfer.
1107 * @param huart: UART handle.
1108 * @retval HAL status
1110 HAL_StatusTypeDef
HAL_UART_DMAResume(UART_HandleTypeDef
*huart
)
1112 /* Process Locked */
1115 if(huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1117 /* Enable the UART DMA Tx request */
1118 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1120 if(huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1122 /* Clear the Overrun flag before resuming the Rx transfer*/
1123 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_OREF
);
1125 /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
1126 SET_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1127 SET_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1129 /* Enable the UART DMA Rx request */
1130 SET_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1133 /* If the UART peripheral is still not enabled, enable it */
1134 if ((huart
->Instance
->CR1
& USART_CR1_UE
) == 0U)
1136 /* Enable UART peripheral */
1137 __HAL_UART_ENABLE(huart
);
1144 * @brief Stop the DMA Transfer.
1145 * @param huart: UART handle.
1146 * @retval HAL status
1148 HAL_StatusTypeDef
HAL_UART_DMAStop(UART_HandleTypeDef
*huart
)
1150 /* The Lock is not implemented on this API to allow the user application
1151 to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1152 HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1153 indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1154 interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1155 the stream and the corresponding call back is executed. */
1157 /* Stop UART DMA Tx request if ongoing */
1158 if ((huart
->gState
== HAL_UART_STATE_BUSY_TX
) &&
1159 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)))
1161 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1163 /* Abort the UART DMA Tx channel */
1164 if(huart
->hdmatx
!= NULL
)
1166 HAL_DMA_Abort(huart
->hdmatx
);
1169 UART_EndTxTransfer(huart
);
1172 /* Stop UART DMA Rx request if ongoing */
1173 if ((huart
->RxState
== HAL_UART_STATE_BUSY_RX
) &&
1174 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1176 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1178 /* Abort the UART DMA Rx channel */
1179 if(huart
->hdmarx
!= NULL
)
1181 HAL_DMA_Abort(huart
->hdmarx
);
1184 UART_EndRxTransfer(huart
);
1191 * @brief This function handles UART interrupt request.
1192 * @param huart: uart handle
1195 void HAL_UART_IRQHandler(UART_HandleTypeDef
*huart
)
1197 uint32_t isrflags
= READ_REG(huart
->Instance
->ISR
);
1198 uint32_t cr1its
= READ_REG(huart
->Instance
->CR1
);
1199 uint32_t cr3its
= READ_REG(huart
->Instance
->CR3
);
1200 uint32_t errorflags
;
1202 /* If no error occurs */
1203 errorflags
= (isrflags
& (uint32_t)(USART_ISR_PE
| USART_ISR_FE
| USART_ISR_ORE
| USART_ISR_NE
));
1204 if (errorflags
== RESET
)
1206 /* UART in mode Receiver ---------------------------------------------------*/
1207 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1209 UART_Receive_IT(huart
);
1214 /* If some errors occur */
1215 if((errorflags
!= RESET
) && ((cr3its
& (USART_CR3_EIE
| USART_CR1_PEIE
)) != RESET
))
1218 /* UART parity error interrupt occurred -------------------------------------*/
1219 if(((isrflags
& USART_ISR_PE
) != RESET
) && ((cr1its
& USART_CR1_PEIE
) != RESET
))
1221 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_PEF
);
1223 huart
->ErrorCode
|= HAL_UART_ERROR_PE
;
1226 /* UART frame error interrupt occurred --------------------------------------*/
1227 if(((isrflags
& USART_ISR_FE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1229 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_FEF
);
1231 huart
->ErrorCode
|= HAL_UART_ERROR_FE
;
1234 /* UART noise error interrupt occurred --------------------------------------*/
1235 if(((isrflags
& USART_ISR_NE
) != RESET
) && ((cr3its
& USART_CR3_EIE
) != RESET
))
1237 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_NEF
);
1239 huart
->ErrorCode
|= HAL_UART_ERROR_NE
;
1242 /* UART Over-Run interrupt occurred -----------------------------------------*/
1243 if(((isrflags
& USART_ISR_ORE
) != RESET
) &&
1244 (((cr1its
& USART_CR1_RXNEIE
) != RESET
) || ((cr3its
& USART_CR3_EIE
) != RESET
)))
1246 __HAL_UART_CLEAR_IT(huart
, UART_CLEAR_OREF
);
1248 huart
->ErrorCode
|= HAL_UART_ERROR_ORE
;
1251 /* Call UART Error Call back function if need be --------------------------*/
1252 if(huart
->ErrorCode
!= HAL_UART_ERROR_NONE
)
1254 /* UART in mode Receiver ---------------------------------------------------*/
1255 if(((isrflags
& USART_ISR_RXNE
) != RESET
) && ((cr1its
& USART_CR1_RXNEIE
) != RESET
))
1257 UART_Receive_IT(huart
);
1260 /* If Overrun error occurs, or if any error occurs in DMA mode reception,
1261 consider error as blocking */
1262 if (((huart
->ErrorCode
& HAL_UART_ERROR_ORE
) != RESET
) ||
1263 (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)))
1265 /* Blocking error : transfer is aborted
1266 Set the UART state ready to be able to start again the process,
1267 Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
1268 UART_EndRxTransfer(huart
);
1270 /* Disable the UART DMA Rx request if enabled */
1271 if (HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
))
1273 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1275 /* Abort the UART DMA Rx channel */
1276 if(huart
->hdmarx
!= NULL
)
1278 /* Set the UART DMA Abort callback :
1279 will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
1280 huart
->hdmarx
->XferAbortCallback
= UART_DMAAbortOnError
;
1283 if(HAL_DMA_Abort_IT(huart
->hdmarx
) != HAL_OK
)
1285 /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
1286 huart
->hdmarx
->XferAbortCallback(huart
->hdmarx
);
1291 /* Call user error callback */
1292 HAL_UART_ErrorCallback(huart
);
1297 /* Call user error callback */
1298 HAL_UART_ErrorCallback(huart
);
1303 /* Non Blocking error : transfer could go on.
1304 Error is notified to user through user error callback */
1305 HAL_UART_ErrorCallback(huart
);
1306 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
1311 } /* End if some error occurs */
1313 /* UART in mode Transmitter ------------------------------------------------*/
1314 if(((isrflags
& USART_ISR_TXE
) != RESET
) && ((cr1its
& USART_CR1_TXEIE
) != RESET
))
1316 UART_Transmit_IT(huart
);
1320 /* UART in mode Transmitter (transmission end) -----------------------------*/
1321 if(((isrflags
& USART_ISR_TC
) != RESET
) && ((cr1its
& USART_CR1_TCIE
) != RESET
))
1323 UART_EndTransmit_IT(huart
);
1330 * @brief This function handles UART Communication Timeout.
1331 * @param huart UART handle
1332 * @param Flag specifies the UART flag to check.
1333 * @param Status The new Flag status (SET or RESET).
1334 * @param Tickstart Tick start value
1335 * @param Timeout Timeout duration
1336 * @retval HAL status
1338 HAL_StatusTypeDef
UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef
*huart
, uint32_t Flag
, FlagStatus Status
, uint32_t Tickstart
, uint32_t Timeout
)
1340 /* Wait until flag is set */
1341 while((__HAL_UART_GET_FLAG(huart
, Flag
) ? SET
: RESET
) == Status
)
1343 /* Check for the Timeout */
1344 if(Timeout
!= HAL_MAX_DELAY
)
1346 if((Timeout
== 0U)||((HAL_GetTick()-Tickstart
) >= Timeout
))
1348 /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
1349 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
| USART_CR1_TXEIE
));
1350 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1352 huart
->gState
= HAL_UART_STATE_READY
;
1353 huart
->RxState
= HAL_UART_STATE_READY
;
1355 /* Process Unlocked */
1356 __HAL_UNLOCK(huart
);
1365 * @brief DMA UART transmit process complete callback
1366 * @param hdma: DMA handle
1369 static void UART_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
1371 UART_HandleTypeDef
* huart
= ( UART_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1373 /* DMA Normal mode*/
1374 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1376 huart
->TxXferCount
= 0U;
1378 /* Disable the DMA transfer for transmit request by setting the DMAT bit
1379 in the UART CR3 register */
1380 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAT
);
1382 /* Enable the UART Transmit Complete Interrupt */
1383 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
1385 /* DMA Circular mode */
1388 HAL_UART_TxCpltCallback(huart
);
1393 * @brief DMA UART transmit process half complete callback
1394 * @param hdma : DMA handle
1397 static void UART_DMATxHalfCplt(DMA_HandleTypeDef
*hdma
)
1399 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
1401 HAL_UART_TxHalfCpltCallback(huart
);
1405 * @brief DMA UART receive process complete callback
1406 * @param hdma: DMA handle
1409 static void UART_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
1411 UART_HandleTypeDef
* huart
= ( UART_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1413 /* DMA Normal mode */
1414 if((hdma
->Instance
->CR
& DMA_SxCR_CIRC
) == 0U)
1416 huart
->RxXferCount
= 0U;
1418 /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1419 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_PEIE
);
1420 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1422 /* Disable the DMA transfer for the receiver request by setting the DMAR bit
1423 in the UART CR3 register */
1424 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_DMAR
);
1426 /* At end of Rx process, restore huart->RxState to Ready */
1427 huart
->RxState
= HAL_UART_STATE_READY
;
1429 HAL_UART_RxCpltCallback(huart
);
1433 * @brief DMA UART receive process half complete callback
1434 * @param hdma : DMA handle
1437 static void UART_DMARxHalfCplt(DMA_HandleTypeDef
*hdma
)
1439 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)((DMA_HandleTypeDef
*)hdma
)->Parent
;
1441 HAL_UART_RxHalfCpltCallback(huart
);
1445 * @brief DMA UART communication error callback
1446 * @param hdma: DMA handle
1449 static void UART_DMAError(DMA_HandleTypeDef
*hdma
)
1451 UART_HandleTypeDef
* huart
= ( UART_HandleTypeDef
* )((DMA_HandleTypeDef
* )hdma
)->Parent
;
1452 huart
->RxXferCount
= 0U;
1453 huart
->TxXferCount
= 0U;
1454 /* Stop UART DMA Tx request if ongoing */
1455 if ( (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1456 &&(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAT
)) )
1458 UART_EndTxTransfer(huart
);
1461 /* Stop UART DMA Rx request if ongoing */
1462 if ( (huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1463 &&(HAL_IS_BIT_SET(huart
->Instance
->CR3
, USART_CR3_DMAR
)) )
1465 UART_EndRxTransfer(huart
);
1467 SET_BIT(huart
->ErrorCode
, HAL_UART_ERROR_DMA
);
1468 HAL_UART_ErrorCallback(huart
);
1472 * @brief DMA UART communication abort callback, when call by HAL services on Error
1473 * (To be called at end of DMA Abort procedure following error occurrence).
1474 * @param hdma: DMA handle.
1477 static void UART_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
1479 UART_HandleTypeDef
* huart
= (UART_HandleTypeDef
*)(hdma
->Parent
);
1480 huart
->RxXferCount
= 0U;
1481 huart
->TxXferCount
= 0U;
1483 HAL_UART_ErrorCallback(huart
);
1487 * @brief Tx Transfer completed callbacks
1488 * @param huart: uart handle
1491 __weak
void HAL_UART_TxCpltCallback(UART_HandleTypeDef
*huart
)
1493 /* Prevent unused argument(s) compilation warning */
1496 /* NOTE : This function should not be modified, when the callback is needed,
1497 the HAL_UART_TxCpltCallback can be implemented in the user file
1502 * @brief Tx Half Transfer completed callbacks.
1503 * @param huart: UART handle
1506 __weak
void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef
*huart
)
1508 /* Prevent unused argument(s) compilation warning */
1511 /* NOTE: This function should not be modified, when the callback is needed,
1512 the HAL_UART_TxHalfCpltCallback can be implemented in the user file
1517 * @brief Rx Transfer completed callbacks
1518 * @param huart: uart handle
1521 __weak
void HAL_UART_RxCpltCallback(UART_HandleTypeDef
*huart
)
1523 /* Prevent unused argument(s) compilation warning */
1526 /* NOTE : This function should not be modified, when the callback is needed,
1527 the HAL_UART_RxCpltCallback can be implemented in the user file
1532 * @brief Rx Half Transfer completed callbacks.
1533 * @param huart: UART handle
1536 __weak
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef
*huart
)
1538 /* Prevent unused argument(s) compilation warning */
1541 /* NOTE: This function should not be modified, when the callback is needed,
1542 the HAL_UART_RxHalfCpltCallback can be implemented in the user file
1547 * @brief UART error callbacks
1548 * @param huart: uart handle
1551 __weak
void HAL_UART_ErrorCallback(UART_HandleTypeDef
*huart
)
1553 /* Prevent unused argument(s) compilation warning */
1556 /* NOTE : This function should not be modified, when the callback is needed,
1557 the HAL_UART_ErrorCallback can be implemented in the user file
1562 * @brief Send an amount of data in interrupt mode
1563 * Function called under interruption only, once
1564 * interruptions have been enabled by HAL_UART_Transmit_IT()
1565 * @param huart: UART handle
1566 * @retval HAL status
1568 static HAL_StatusTypeDef
UART_Transmit_IT(UART_HandleTypeDef
*huart
)
1572 /* Check that a Tx process is ongoing */
1573 if (huart
->gState
== HAL_UART_STATE_BUSY_TX
)
1576 if(huart
->TxXferCount
== 0U)
1578 /* Disable the UART Transmit Data Register Empty Interrupt */
1579 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TXEIE
);
1581 /* Enable the UART Transmit Complete Interrupt */
1582 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
1588 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1590 tmp
= (uint16_t*) huart
->pTxBuffPtr
;
1591 huart
->Instance
->TDR
= (*tmp
& (uint16_t)0x01FFU
);
1592 huart
->pTxBuffPtr
+= 2U;
1596 huart
->Instance
->TDR
= (uint8_t)(*huart
->pTxBuffPtr
++ & (uint8_t)0xFFU
);
1599 huart
->TxXferCount
--;
1611 * @brief Wrap up transmission in non-blocking mode.
1612 * @param huart: pointer to a UART_HandleTypeDef structure that contains
1613 * the configuration information for the specified UART module.
1614 * @retval HAL status
1616 static HAL_StatusTypeDef
UART_EndTransmit_IT(UART_HandleTypeDef
*huart
)
1618 /* Disable the UART Transmit Complete Interrupt */
1619 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_TCIE
);
1621 /* Tx process is ended, restore huart->gState to Ready */
1622 huart
->gState
= HAL_UART_STATE_READY
;
1624 HAL_UART_TxCpltCallback(huart
);
1630 * @brief Receive an amount of data in interrupt mode
1631 * Function called under interruption only, once
1632 * interruptions have been enabled by HAL_UART_Receive_IT()
1633 * @param huart: UART handle
1634 * @retval HAL status
1636 static HAL_StatusTypeDef
UART_Receive_IT(UART_HandleTypeDef
*huart
)
1639 uint16_t uhMask
= huart
->Mask
;
1641 /* Check that a Rx process is ongoing */
1642 if(huart
->RxState
== HAL_UART_STATE_BUSY_RX
)
1645 if ((huart
->Init
.WordLength
== UART_WORDLENGTH_9B
) && (huart
->Init
.Parity
== UART_PARITY_NONE
))
1647 tmp
= (uint16_t*) huart
->pRxBuffPtr
;
1648 *tmp
= (uint16_t)(huart
->Instance
->RDR
& uhMask
);
1649 huart
->pRxBuffPtr
+=2;
1653 *huart
->pRxBuffPtr
++ = (uint8_t)(huart
->Instance
->RDR
& (uint8_t)uhMask
);
1656 if(--huart
->RxXferCount
== 0)
1658 /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
1659 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1661 /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
1662 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1664 /* Rx process is completed, restore huart->RxState to Ready */
1665 huart
->RxState
= HAL_UART_STATE_READY
;
1667 HAL_UART_RxCpltCallback(huart
);
1676 /* Clear RXNE interrupt flag */
1677 __HAL_UART_SEND_REQ(huart
, UART_RXDATA_FLUSH_REQUEST
);
1684 * @brief End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
1685 * @param huart: UART handle.
1688 static void UART_EndTxTransfer(UART_HandleTypeDef
*huart
)
1690 /* Disable TXEIE and TCIE interrupts */
1691 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TXEIE
| USART_CR1_TCIE
));
1693 /* At end of Tx process, restore huart->gState to Ready */
1694 huart
->gState
= HAL_UART_STATE_READY
;
1699 * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
1700 * @param huart: UART handle.
1703 static void UART_EndRxTransfer(UART_HandleTypeDef
*huart
)
1705 /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
1706 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_RXNEIE
| USART_CR1_PEIE
));
1707 CLEAR_BIT(huart
->Instance
->CR3
, USART_CR3_EIE
);
1709 /* At end of Rx process, restore huart->RxState to Ready */
1710 huart
->RxState
= HAL_UART_STATE_READY
;
1717 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
1718 * @brief UART control functions
1721 ===============================================================================
1722 ##### Peripheral Control functions #####
1723 ===============================================================================
1725 This subsection provides a set of functions allowing to control the UART.
1726 (+) HAL_UART_GetState() API is helpful to check in run-time the state of the UART peripheral.
1727 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
1728 (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
1729 (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
1730 (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
1731 (+) UART_SetConfig() API configures the UART peripheral
1732 (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
1733 (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
1734 (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
1735 (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
1736 (+) HAL_LIN_SendBreak() API transmits the break characters
1737 (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
1738 detection length to more than 4 bits for multiprocessor address mark wake up.
1744 * @brief Enable UART in mute mode (doesn't mean UART enters mute mode;
1745 * to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called)
1746 * @param huart: UART handle
1747 * @retval HAL status
1749 HAL_StatusTypeDef
HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef
*huart
)
1751 /* Process Locked */
1754 huart
->gState
= HAL_UART_STATE_BUSY
;
1756 /* Enable USART mute mode by setting the MME bit in the CR1 register */
1757 SET_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
1759 huart
->gState
= HAL_UART_STATE_READY
;
1761 return (UART_CheckIdleState(huart
));
1765 * @brief Disable UART mute mode (doesn't mean it actually wakes up the software,
1766 * as it may not have been in mute mode at this very moment).
1767 * @param huart: uart handle
1768 * @retval HAL status
1770 HAL_StatusTypeDef
HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef
*huart
)
1772 /* Process Locked */
1775 huart
->gState
= HAL_UART_STATE_BUSY
;
1777 /* Disable USART mute mode by clearing the MME bit in the CR1 register */
1778 CLEAR_BIT(huart
->Instance
->CR1
, USART_CR1_MME
);
1780 huart
->gState
= HAL_UART_STATE_READY
;
1782 return (UART_CheckIdleState(huart
));
1786 * @brief Enter UART mute mode (means UART actually enters mute mode).
1787 * To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
1788 * @param huart: uart handle
1789 * @retval HAL status
1791 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef
*huart
)
1793 __HAL_UART_SEND_REQ(huart
, UART_MUTE_MODE_REQUEST
);
1799 * @brief return the UART state
1800 * @param huart: uart handle
1803 HAL_UART_StateTypeDef
HAL_UART_GetState(UART_HandleTypeDef
*huart
)
1805 uint32_t temp1
= 0x00U
, temp2
= 0x00U
;
1806 temp1
= huart
->gState
;
1807 temp2
= huart
->RxState
;
1809 return (HAL_UART_StateTypeDef
)(temp1
| temp2
);
1813 * @brief Return the UART error code
1814 * @param huart : pointer to a UART_HandleTypeDef structure that contains
1815 * the configuration information for the specified UART.
1816 * @retval UART Error Code
1818 uint32_t HAL_UART_GetError(UART_HandleTypeDef
*huart
)
1820 return huart
->ErrorCode
;
1824 * @brief Configure the UART peripheral
1825 * @param huart: uart handle
1828 HAL_StatusTypeDef
UART_SetConfig(UART_HandleTypeDef
*huart
)
1830 uint32_t tmpreg
= 0x00000000U
;
1831 UART_ClockSourceTypeDef clocksource
= UART_CLOCKSOURCE_UNDEFINED
;
1832 uint16_t brrtemp
= 0x0000U
;
1833 uint16_t usartdiv
= 0x0000U
;
1834 HAL_StatusTypeDef ret
= HAL_OK
;
1836 /* Check the parameters */
1837 assert_param(IS_UART_BAUDRATE(huart
->Init
.BaudRate
));
1838 assert_param(IS_UART_WORD_LENGTH(huart
->Init
.WordLength
));
1839 assert_param(IS_UART_STOPBITS(huart
->Init
.StopBits
));
1840 assert_param(IS_UART_PARITY(huart
->Init
.Parity
));
1841 assert_param(IS_UART_MODE(huart
->Init
.Mode
));
1842 assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart
->Init
.HwFlowCtl
));
1843 assert_param(IS_UART_ONE_BIT_SAMPLE(huart
->Init
.OneBitSampling
));
1846 /*-------------------------- USART CR1 Configuration -----------------------*/
1847 /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
1848 * the UART Word Length, Parity, Mode and oversampling:
1849 * set the M bits according to huart->Init.WordLength value
1850 * set PCE and PS bits according to huart->Init.Parity value
1851 * set TE and RE bits according to huart->Init.Mode value
1852 * set OVER8 bit according to huart->Init.OverSampling value */
1853 tmpreg
= (uint32_t)huart
->Init
.WordLength
| huart
->Init
.Parity
| huart
->Init
.Mode
| huart
->Init
.OverSampling
;
1854 MODIFY_REG(huart
->Instance
->CR1
, UART_CR1_FIELDS
, tmpreg
);
1856 /*-------------------------- USART CR2 Configuration -----------------------*/
1857 /* Configure the UART Stop Bits: Set STOP[13:12] bits according
1858 * to huart->Init.StopBits value */
1859 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_STOP
, huart
->Init
.StopBits
);
1861 /*-------------------------- USART CR3 Configuration -----------------------*/
1863 * - UART HardWare Flow Control: set CTSE and RTSE bits according
1864 * to huart->Init.HwFlowCtl value
1865 * - one-bit sampling method versus three samples' majority rule according
1866 * to huart->Init.OneBitSampling */
1867 tmpreg
= (uint32_t)huart
->Init
.HwFlowCtl
| huart
->Init
.OneBitSampling
;
1868 MODIFY_REG(huart
->Instance
->CR3
, (USART_CR3_RTSE
| USART_CR3_CTSE
| USART_CR3_ONEBIT
), tmpreg
);
1870 /*-------------------------- USART BRR Configuration -----------------------*/
1871 UART_GETCLOCKSOURCE(huart
, clocksource
);
1873 /* Check UART Over Sampling to set Baud Rate Register */
1874 if (huart
->Init
.OverSampling
== UART_OVERSAMPLING_8
)
1876 switch (clocksource
)
1878 case UART_CLOCKSOURCE_PCLK1
:
1879 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
1881 case UART_CLOCKSOURCE_PCLK2
:
1882 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
1884 case UART_CLOCKSOURCE_HSI
:
1885 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE
, huart
->Init
.BaudRate
));
1887 case UART_CLOCKSOURCE_SYSCLK
:
1888 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
1890 case UART_CLOCKSOURCE_LSE
:
1891 usartdiv
= (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE
, huart
->Init
.BaudRate
));
1893 case UART_CLOCKSOURCE_UNDEFINED
:
1899 brrtemp
= usartdiv
& 0xFFF0U
;
1900 brrtemp
|= (uint16_t)((usartdiv
& (uint16_t)0x000FU
) >> 1U);
1901 huart
->Instance
->BRR
= brrtemp
;
1905 switch (clocksource
)
1907 case UART_CLOCKSOURCE_PCLK1
:
1908 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart
->Init
.BaudRate
));
1910 case UART_CLOCKSOURCE_PCLK2
:
1911 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart
->Init
.BaudRate
));
1913 case UART_CLOCKSOURCE_HSI
:
1914 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE
, huart
->Init
.BaudRate
));
1916 case UART_CLOCKSOURCE_SYSCLK
:
1917 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(HAL_RCC_GetSysClockFreq(), huart
->Init
.BaudRate
));
1919 case UART_CLOCKSOURCE_LSE
:
1920 huart
->Instance
->BRR
= (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE
, huart
->Init
.BaudRate
));
1922 case UART_CLOCKSOURCE_UNDEFINED
:
1935 * @brief Configure the UART peripheral advanced features
1936 * @param huart: uart handle
1939 void UART_AdvFeatureConfig(UART_HandleTypeDef
*huart
)
1941 /* Check whether the set of advanced features to configure is properly set */
1942 assert_param(IS_UART_ADVFEATURE_INIT(huart
->AdvancedInit
.AdvFeatureInit
));
1944 /* if required, configure TX pin active level inversion */
1945 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_TXINVERT_INIT
))
1947 assert_param(IS_UART_ADVFEATURE_TXINV(huart
->AdvancedInit
.TxPinLevelInvert
));
1948 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_TXINV
, huart
->AdvancedInit
.TxPinLevelInvert
);
1951 /* if required, configure RX pin active level inversion */
1952 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXINVERT_INIT
))
1954 assert_param(IS_UART_ADVFEATURE_RXINV(huart
->AdvancedInit
.RxPinLevelInvert
));
1955 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_RXINV
, huart
->AdvancedInit
.RxPinLevelInvert
);
1958 /* if required, configure data inversion */
1959 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DATAINVERT_INIT
))
1961 assert_param(IS_UART_ADVFEATURE_DATAINV(huart
->AdvancedInit
.DataInvert
));
1962 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_DATAINV
, huart
->AdvancedInit
.DataInvert
);
1965 /* if required, configure RX/TX pins swap */
1966 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_SWAP_INIT
))
1968 assert_param(IS_UART_ADVFEATURE_SWAP(huart
->AdvancedInit
.Swap
));
1969 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_SWAP
, huart
->AdvancedInit
.Swap
);
1972 /* if required, configure RX overrun detection disabling */
1973 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT
))
1975 assert_param(IS_UART_OVERRUN(huart
->AdvancedInit
.OverrunDisable
));
1976 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_OVRDIS
, huart
->AdvancedInit
.OverrunDisable
);
1979 /* if required, configure DMA disabling on reception error */
1980 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_DMADISABLEONERROR_INIT
))
1982 assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart
->AdvancedInit
.DMADisableonRxError
));
1983 MODIFY_REG(huart
->Instance
->CR3
, USART_CR3_DDRE
, huart
->AdvancedInit
.DMADisableonRxError
);
1986 /* if required, configure auto Baud rate detection scheme */
1987 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_AUTOBAUDRATE_INIT
))
1989 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart
->AdvancedInit
.AutoBaudRateEnable
));
1990 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABREN
, huart
->AdvancedInit
.AutoBaudRateEnable
);
1991 /* set auto Baudrate detection parameters if detection is enabled */
1992 if(huart
->AdvancedInit
.AutoBaudRateEnable
== UART_ADVFEATURE_AUTOBAUDRATE_ENABLE
)
1994 assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart
->AdvancedInit
.AutoBaudRateMode
));
1995 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ABRMODE
, huart
->AdvancedInit
.AutoBaudRateMode
);
1999 /* if required, configure MSB first on communication line */
2000 if(HAL_IS_BIT_SET(huart
->AdvancedInit
.AdvFeatureInit
, UART_ADVFEATURE_MSBFIRST_INIT
))
2002 assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart
->AdvancedInit
.MSBFirst
));
2003 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_MSBFIRST
, huart
->AdvancedInit
.MSBFirst
);
2010 * @brief Check the UART Idle State
2011 * @param huart: uart handle
2012 * @retval HAL status
2014 HAL_StatusTypeDef
UART_CheckIdleState(UART_HandleTypeDef
*huart
)
2016 uint32_t tickstart
= 0U;
2018 /* Initialize the UART ErrorCode */
2019 huart
->ErrorCode
= HAL_UART_ERROR_NONE
;
2021 /* Init tickstart for timeout managment*/
2022 tickstart
= HAL_GetTick();
2024 /* Check if the Transmitter is enabled */
2025 if((huart
->Instance
->CR1
& USART_CR1_TE
) == USART_CR1_TE
)
2027 /* Wait until TEACK flag is set */
2028 if(UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_TEACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
2030 /* Timeout Occurred */
2034 /* Check if the Receiver is enabled */
2035 if((huart
->Instance
->CR1
& USART_CR1_RE
) == USART_CR1_RE
)
2037 /* Wait until REACK flag is set */
2038 if(UART_WaitOnFlagUntilTimeout(huart
, USART_ISR_REACK
, RESET
, tickstart
, HAL_UART_TIMEOUT_VALUE
) != HAL_OK
)
2040 /* Timeout Occurred */
2045 /* Initialize the UART State */
2046 huart
->gState
= HAL_UART_STATE_READY
;
2047 huart
->RxState
= HAL_UART_STATE_READY
;
2049 /* Process Unlocked */
2050 __HAL_UNLOCK(huart
);
2056 * @brief Enables the UART transmitter and disables the UART receiver.
2057 * @param huart: UART handle
2058 * @retval HAL status
2061 HAL_StatusTypeDef
HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef
*huart
)
2063 /* Process Locked */
2065 huart
->gState
= HAL_UART_STATE_BUSY
;
2067 /* Clear TE and RE bits */
2068 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2069 /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2070 SET_BIT(huart
->Instance
->CR1
, USART_CR1_TE
);
2072 huart
->gState
= HAL_UART_STATE_READY
;
2073 /* Process Unlocked */
2074 __HAL_UNLOCK(huart
);
2080 * @brief Enables the UART receiver and disables the UART transmitter.
2081 * @param huart: UART handle
2082 * @retval HAL status
2084 HAL_StatusTypeDef
HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef
*huart
)
2086 /* Process Locked */
2088 huart
->gState
= HAL_UART_STATE_BUSY
;
2090 /* Clear TE and RE bits */
2091 CLEAR_BIT(huart
->Instance
->CR1
, (USART_CR1_TE
| USART_CR1_RE
));
2092 /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2093 SET_BIT(huart
->Instance
->CR1
, USART_CR1_RE
);
2095 huart
->gState
= HAL_UART_STATE_READY
;
2096 /* Process Unlocked */
2097 __HAL_UNLOCK(huart
);
2104 * @brief Transmits break characters.
2105 * @param huart: UART handle
2106 * @retval HAL status
2108 HAL_StatusTypeDef
HAL_LIN_SendBreak(UART_HandleTypeDef
*huart
)
2110 /* Check the parameters */
2111 assert_param(IS_UART_INSTANCE(huart
->Instance
));
2113 /* Process Locked */
2116 huart
->gState
= HAL_UART_STATE_BUSY
;
2118 /* Send break characters */
2119 SET_BIT(huart
->Instance
->RQR
, UART_SENDBREAK_REQUEST
);
2121 huart
->gState
= HAL_UART_STATE_READY
;
2123 /* Process Unlocked */
2124 __HAL_UNLOCK(huart
);
2130 * @brief By default in multiprocessor mode, when the wake up method is set
2131 * to address mark, the UART handles only 4-bit long addresses detection;
2132 * this API allows to enable longer addresses detection (6-, 7- or 8-bit
2134 * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
2135 * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
2136 * @param huart: UART handle.
2137 * @param AddressLength: this parameter can be one of the following values:
2138 * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
2139 * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
2140 * @retval HAL status
2142 HAL_StatusTypeDef
HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef
*huart
, uint32_t AddressLength
)
2144 /* Check the UART handle allocation */
2150 /* Check the address length parameter */
2151 assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength
));
2153 huart
->gState
= HAL_UART_STATE_BUSY
;
2155 /* Disable the Peripheral */
2156 __HAL_UART_DISABLE(huart
);
2158 /* Set the address length */
2159 MODIFY_REG(huart
->Instance
->CR2
, USART_CR2_ADDM7
, AddressLength
);
2161 /* Enable the Peripheral */
2162 __HAL_UART_ENABLE(huart
);
2164 /* TEACK and/or REACK to check before moving huart->gState to Ready */
2165 return (UART_CheckIdleState(huart
));
2176 #endif /* HAL_UART_MODULE_ENABLED */
2185 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/