Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Inc / stm32f7xx_hal_usart.h
blob9f615f39625d2c273e5877ed55a4e67cf55a9e49
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_usart.h
4 * @author MCD Application Team
5 * @brief Header file of USART HAL module.
6 ******************************************************************************
7 * @attention
9 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10 * All rights reserved.</center></h2>
12 * This software component is licensed by ST under BSD 3-Clause license,
13 * the "License"; You may not use this file except in compliance with the
14 * License. You may obtain a copy of the License at:
15 * opensource.org/licenses/BSD-3-Clause
17 ******************************************************************************
20 /* Define to prevent recursive inclusion -------------------------------------*/
21 #ifndef STM32F7xx_HAL_USART_H
22 #define STM32F7xx_HAL_USART_H
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
28 /* Includes ------------------------------------------------------------------*/
29 #include "stm32f7xx_hal_def.h"
31 /** @addtogroup STM32F7xx_HAL_Driver
32 * @{
35 /** @addtogroup USART
36 * @{
39 /* Exported types ------------------------------------------------------------*/
40 /** @defgroup USART_Exported_Types USART Exported Types
41 * @{
44 /**
45 * @brief USART Init Structure definition
47 typedef struct
49 uint32_t BaudRate; /*!< This member configures the Usart communication baud rate.
50 The baud rate is computed using the following formula:
51 Baud Rate Register[15:4] = ((2 * fclk_pres) / ((huart->Init.BaudRate)))[15:4]
52 Baud Rate Register[3] = 0
53 Baud Rate Register[2:0] = (((2 * fclk_pres) / ((huart->Init.BaudRate)))[3:0]) >> 1
54 where fclk_pres is the USART input clock frequency
55 @note Oversampling by 8 is systematically applied to achieve high baud rates. */
57 uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
58 This parameter can be a value of @ref USARTEx_Word_Length. */
60 uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
61 This parameter can be a value of @ref USART_Stop_Bits. */
63 uint32_t Parity; /*!< Specifies the parity mode.
64 This parameter can be a value of @ref USART_Parity
65 @note When parity is enabled, the computed parity is inserted
66 at the MSB position of the transmitted data (9th bit when
67 the word length is set to 9 data bits; 8th bit when the
68 word length is set to 8 data bits). */
70 uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
71 This parameter can be a value of @ref USART_Mode. */
73 uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock.
74 This parameter can be a value of @ref USART_Clock_Polarity. */
76 uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made.
77 This parameter can be a value of @ref USART_Clock_Phase. */
79 uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted
80 data bit (MSB) has to be output on the SCLK pin in synchronous mode.
81 This parameter can be a value of @ref USART_Last_Bit. */
83 } USART_InitTypeDef;
85 /**
86 * @brief HAL USART State structures definition
88 typedef enum
90 HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not initialized */
91 HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */
92 HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */
93 HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */
94 HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
95 HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */
96 HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */
97 HAL_USART_STATE_ERROR = 0x04U /*!< Error */
98 } HAL_USART_StateTypeDef;
101 * @brief USART clock sources definitions
103 typedef enum
105 USART_CLOCKSOURCE_PCLK1 = 0x00U, /*!< PCLK1 clock source */
106 USART_CLOCKSOURCE_PCLK2 = 0x01U, /*!< PCLK2 clock source */
107 USART_CLOCKSOURCE_HSI = 0x02U, /*!< HSI clock source */
108 USART_CLOCKSOURCE_SYSCLK = 0x04U, /*!< SYSCLK clock source */
109 USART_CLOCKSOURCE_LSE = 0x08U, /*!< LSE clock source */
110 USART_CLOCKSOURCE_UNDEFINED = 0x10U /*!< Undefined clock source */
111 } USART_ClockSourceTypeDef;
114 * @brief USART handle Structure definition
116 typedef struct __USART_HandleTypeDef
118 USART_TypeDef *Instance; /*!< USART registers base address */
120 USART_InitTypeDef Init; /*!< USART communication parameters */
122 uint8_t *pTxBuffPtr; /*!< Pointer to USART Tx transfer Buffer */
124 uint16_t TxXferSize; /*!< USART Tx Transfer size */
126 __IO uint16_t TxXferCount; /*!< USART Tx Transfer Counter */
128 uint8_t *pRxBuffPtr; /*!< Pointer to USART Rx transfer Buffer */
130 uint16_t RxXferSize; /*!< USART Rx Transfer size */
132 __IO uint16_t RxXferCount; /*!< USART Rx Transfer Counter */
134 uint16_t Mask; /*!< USART Rx RDR register mask */
136 void (*RxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Rx IRQ handler */
138 void (*TxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Tx IRQ handler */
140 DMA_HandleTypeDef *hdmatx; /*!< USART Tx DMA Handle parameters */
142 DMA_HandleTypeDef *hdmarx; /*!< USART Rx DMA Handle parameters */
144 HAL_LockTypeDef Lock; /*!< Locking object */
146 __IO HAL_USART_StateTypeDef State; /*!< USART communication state */
148 __IO uint32_t ErrorCode; /*!< USART Error code */
150 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
151 void (* TxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Half Complete Callback */
152 void (* TxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Complete Callback */
153 void (* RxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Half Complete Callback */
154 void (* RxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Complete Callback */
155 void (* TxRxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Rx Complete Callback */
156 void (* ErrorCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Error Callback */
157 void (* AbortCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Abort Complete Callback */
159 void (* MspInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp Init callback */
160 void (* MspDeInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp DeInit callback */
161 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
163 } USART_HandleTypeDef;
165 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
167 * @brief HAL USART Callback ID enumeration definition
169 typedef enum
171 HAL_USART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< USART Tx Half Complete Callback ID */
172 HAL_USART_TX_COMPLETE_CB_ID = 0x01U, /*!< USART Tx Complete Callback ID */
173 HAL_USART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< USART Rx Half Complete Callback ID */
174 HAL_USART_RX_COMPLETE_CB_ID = 0x03U, /*!< USART Rx Complete Callback ID */
175 HAL_USART_TX_RX_COMPLETE_CB_ID = 0x04U, /*!< USART Tx Rx Complete Callback ID */
176 HAL_USART_ERROR_CB_ID = 0x05U, /*!< USART Error Callback ID */
177 HAL_USART_ABORT_COMPLETE_CB_ID = 0x06U, /*!< USART Abort Complete Callback ID */
179 HAL_USART_MSPINIT_CB_ID = 0x09U, /*!< USART MspInit callback ID */
180 HAL_USART_MSPDEINIT_CB_ID = 0x0AU /*!< USART MspDeInit callback ID */
182 } HAL_USART_CallbackIDTypeDef;
185 * @brief HAL USART Callback pointer definition
187 typedef void (*pUSART_CallbackTypeDef)(USART_HandleTypeDef *husart); /*!< pointer to an USART callback function */
189 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
192 * @}
195 /* Exported constants --------------------------------------------------------*/
196 /** @defgroup USART_Exported_Constants USART Exported Constants
197 * @{
200 /** @defgroup USART_Error_Definition USART Error Definition
201 * @{
203 #define HAL_USART_ERROR_NONE ((uint32_t)0x00000000U) /*!< No error */
204 #define HAL_USART_ERROR_PE ((uint32_t)0x00000001U) /*!< Parity error */
205 #define HAL_USART_ERROR_NE ((uint32_t)0x00000002U) /*!< Noise error */
206 #define HAL_USART_ERROR_FE ((uint32_t)0x00000004U) /*!< Frame error */
207 #define HAL_USART_ERROR_ORE ((uint32_t)0x00000008U) /*!< Overrun error */
208 #define HAL_USART_ERROR_DMA ((uint32_t)0x00000010U) /*!< DMA transfer error */
209 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
210 #define HAL_USART_ERROR_INVALID_CALLBACK ((uint32_t)0x00000040U) /*!< Invalid Callback error */
211 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
213 * @}
216 /** @defgroup USART_Stop_Bits USART Number of Stop Bits
217 * @{
219 #define USART_STOPBITS_0_5 USART_CR2_STOP_0 /*!< USART frame with 0.5 stop bit */
220 #define USART_STOPBITS_1 0x00000000U /*!< USART frame with 1 stop bit */
221 #define USART_STOPBITS_1_5 (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< USART frame with 1.5 stop bits */
222 #define USART_STOPBITS_2 USART_CR2_STOP_1 /*!< USART frame with 2 stop bits */
224 * @}
227 /** @defgroup USART_Parity USART Parity
228 * @{
230 #define USART_PARITY_NONE 0x00000000U /*!< No parity */
231 #define USART_PARITY_EVEN USART_CR1_PCE /*!< Even parity */
232 #define USART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS) /*!< Odd parity */
234 * @}
237 /** @defgroup USART_Mode USART Mode
238 * @{
240 #define USART_MODE_RX USART_CR1_RE /*!< RX mode */
241 #define USART_MODE_TX USART_CR1_TE /*!< TX mode */
242 #define USART_MODE_TX_RX (USART_CR1_TE |USART_CR1_RE) /*!< RX and TX mode */
244 * @}
247 /** @defgroup USART_Over_Sampling USART Over Sampling
248 * @{
250 #define USART_OVERSAMPLING_16 0x00000000U /*!< Oversampling by 16 */
251 #define USART_OVERSAMPLING_8 USART_CR1_OVER8 /*!< Oversampling by 8 */
253 * @}
256 /** @defgroup USART_Clock USART Clock
257 * @{
259 #define USART_CLOCK_DISABLE 0x00000000U /*!< USART clock disable */
260 #define USART_CLOCK_ENABLE USART_CR2_CLKEN /*!< USART clock enable */
262 * @}
265 /** @defgroup USART_Clock_Polarity USART Clock Polarity
266 * @{
268 #define USART_POLARITY_LOW 0x00000000U /*!< Driver enable signal is active high */
269 #define USART_POLARITY_HIGH USART_CR2_CPOL /*!< Driver enable signal is active low */
271 * @}
274 /** @defgroup USART_Clock_Phase USART Clock Phase
275 * @{
277 #define USART_PHASE_1EDGE 0x00000000U /*!< USART frame phase on first clock transition */
278 #define USART_PHASE_2EDGE USART_CR2_CPHA /*!< USART frame phase on second clock transition */
280 * @}
283 /** @defgroup USART_Last_Bit USART Last Bit
284 * @{
286 #define USART_LASTBIT_DISABLE 0x00000000U /*!< USART frame last data bit clock pulse not output to SCLK pin */
287 #define USART_LASTBIT_ENABLE USART_CR2_LBCL /*!< USART frame last data bit clock pulse output to SCLK pin */
289 * @}
293 /** @defgroup USART_Request_Parameters USART Request Parameters
294 * @{
296 #define USART_RXDATA_FLUSH_REQUEST USART_RQR_RXFRQ /*!< Receive Data flush Request */
297 #define USART_TXDATA_FLUSH_REQUEST USART_RQR_TXFRQ /*!< Transmit data flush Request */
299 * @}
302 /** @defgroup USART_Flags USART Flags
303 * Elements values convention: 0xXXXX
304 * - 0xXXXX : Flag mask in the ISR register
305 * @{
307 #define USART_FLAG_TEACK USART_ISR_TEACK /*!< USART transmit enable acknowledge flag */
308 #define USART_FLAG_BUSY USART_ISR_BUSY /*!< USART busy flag */
309 #define USART_FLAG_TXE USART_ISR_TXE /*!< USART transmit data register empty */
310 #define USART_FLAG_TC USART_ISR_TC /*!< USART transmission complete */
311 #define USART_FLAG_RXNE USART_ISR_RXNE /*!< USART read data register not empty */
312 #define USART_FLAG_IDLE USART_ISR_IDLE /*!< USART idle flag */
313 #define USART_FLAG_ORE USART_ISR_ORE /*!< USART overrun error */
314 #define USART_FLAG_NE USART_ISR_NE /*!< USART noise error */
315 #define USART_FLAG_FE USART_ISR_FE /*!< USART frame error */
316 #define USART_FLAG_PE USART_ISR_PE /*!< USART parity error */
318 * @}
321 /** @defgroup USART_Interrupt_definition USART Interrupts Definition
322 * Elements values convention: 0000ZZZZ0XXYYYYYb
323 * - YYYYY : Interrupt source position in the XX register (5bits)
324 * - XX : Interrupt source register (2bits)
325 * - 01: CR1 register
326 * - 10: CR2 register
327 * - 11: CR3 register
328 * - ZZZZ : Flag position in the ISR register(4bits)
329 * @{
332 #define USART_IT_PE 0x0028U /*!< USART parity error interruption */
333 #define USART_IT_TXE 0x0727U /*!< USART transmit data register empty interruption */
334 #define USART_IT_TC 0x0626U /*!< USART transmission complete interruption */
335 #define USART_IT_RXNE 0x0525U /*!< USART read data register not empty interruption */
336 #define USART_IT_IDLE 0x0424U /*!< USART idle interruption */
337 #define USART_IT_ERR 0x0060U /*!< USART error interruption */
338 #define USART_IT_ORE 0x0300U /*!< USART overrun error interruption */
339 #define USART_IT_NE 0x0200U /*!< USART noise error interruption */
340 #define USART_IT_FE 0x0100U /*!< USART frame error interruption */
343 * @}
346 /** @defgroup USART_IT_CLEAR_Flags USART Interruption Clear Flags
347 * @{
349 #define USART_CLEAR_PEF USART_ICR_PECF /*!< Parity Error Clear Flag */
350 #define USART_CLEAR_FEF USART_ICR_FECF /*!< Framing Error Clear Flag */
351 #define USART_CLEAR_NEF USART_ICR_NCF /*!< Noise Error detected Clear Flag */
352 #define USART_CLEAR_OREF USART_ICR_ORECF /*!< OverRun Error Clear Flag */
353 #define USART_CLEAR_IDLEF USART_ICR_IDLECF /*!< IDLE line detected Clear Flag */
354 #define USART_CLEAR_TCF USART_ICR_TCCF /*!< Transmission Complete Clear Flag */
356 * @}
359 /** @defgroup USART_Interruption_Mask USART Interruption Flags Mask
360 * @{
362 #define USART_IT_MASK 0x001FU /*!< USART interruptions flags mask */
363 #define USART_CR_MASK 0x00E0U /*!< USART control register mask */
364 #define USART_CR_POS 5U /*!< USART control register position */
365 #define USART_ISR_MASK 0x1F00U /*!< USART ISR register mask */
366 #define USART_ISR_POS 8U /*!< USART ISR register position */
368 * @}
372 * @}
375 /* Exported macros -----------------------------------------------------------*/
376 /** @defgroup USART_Exported_Macros USART Exported Macros
377 * @{
380 /** @brief Reset USART handle state.
381 * @param __HANDLE__ USART handle.
382 * @retval None
384 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
385 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) do{ \
386 (__HANDLE__)->State = HAL_USART_STATE_RESET; \
387 (__HANDLE__)->MspInitCallback = NULL; \
388 (__HANDLE__)->MspDeInitCallback = NULL; \
389 } while(0U)
390 #else
391 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET)
392 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
394 /** @brief Check whether the specified USART flag is set or not.
395 * @param __HANDLE__ specifies the USART Handle
396 * @param __FLAG__ specifies the flag to check.
397 * This parameter can be one of the following values:
398 * @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag
399 * @arg @ref USART_FLAG_BUSY Busy flag
400 * @arg @ref USART_FLAG_TXE Transmit data register empty flag
401 * @arg @ref USART_FLAG_TC Transmission Complete flag
402 * @arg @ref USART_FLAG_RXNE Receive data register not empty flag
403 * @arg @ref USART_FLAG_IDLE Idle Line detection flag
404 * @arg @ref USART_FLAG_ORE OverRun Error flag
405 * @arg @ref USART_FLAG_NE Noise Error flag
406 * @arg @ref USART_FLAG_FE Framing Error flag
407 * @arg @ref USART_FLAG_PE Parity Error flag
408 * @retval The new state of __FLAG__ (TRUE or FALSE).
410 #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__))
412 /** @brief Clear the specified USART pending flag.
413 * @param __HANDLE__ specifies the USART Handle.
414 * @param __FLAG__ specifies the flag to check.
415 * This parameter can be any combination of the following values:
416 * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
417 * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
418 * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
419 * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
420 * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
421 * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
422 * @retval None
424 #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__))
426 /** @brief Clear the USART PE pending flag.
427 * @param __HANDLE__ specifies the USART Handle.
428 * @retval None
430 #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF)
432 /** @brief Clear the USART FE pending flag.
433 * @param __HANDLE__ specifies the USART Handle.
434 * @retval None
436 #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF)
438 /** @brief Clear the USART NE pending flag.
439 * @param __HANDLE__ specifies the USART Handle.
440 * @retval None
442 #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF)
444 /** @brief Clear the USART ORE pending flag.
445 * @param __HANDLE__ specifies the USART Handle.
446 * @retval None
448 #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF)
450 /** @brief Clear the USART IDLE pending flag.
451 * @param __HANDLE__ specifies the USART Handle.
452 * @retval None
454 #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF)
458 /** @brief Enable the specified USART interrupt.
459 * @param __HANDLE__ specifies the USART Handle.
460 * @param __INTERRUPT__ specifies the USART interrupt source to enable.
461 * This parameter can be one of the following values:
462 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
463 * @arg @ref USART_IT_TC Transmission complete interrupt
464 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
465 * @arg @ref USART_IT_IDLE Idle line detection interrupt
466 * @arg @ref USART_IT_PE Parity Error interrupt
467 * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
468 * @retval None
470 #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)? ((__HANDLE__)->Instance->CR1 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
471 ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)? ((__HANDLE__)->Instance->CR2 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
472 ((__HANDLE__)->Instance->CR3 |= ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))))
474 /** @brief Disable the specified USART interrupt.
475 * @param __HANDLE__ specifies the USART Handle.
476 * @param __INTERRUPT__ specifies the USART interrupt source to disable.
477 * This parameter can be one of the following values:
478 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
479 * @arg @ref USART_IT_TC Transmission complete interrupt
480 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
481 * @arg @ref USART_IT_IDLE Idle line detection interrupt
482 * @arg @ref USART_IT_PE Parity Error interrupt
483 * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error)
484 * @retval None
486 #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__) (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)? ((__HANDLE__)->Instance->CR1 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
487 ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)? ((__HANDLE__)->Instance->CR2 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))): \
488 ((__HANDLE__)->Instance->CR3 &= ~ ((uint32_t)1U << ((__INTERRUPT__) & USART_IT_MASK))))
491 /** @brief Check whether the specified USART interrupt has occurred or not.
492 * @param __HANDLE__ specifies the USART Handle.
493 * @param __INTERRUPT__ specifies the USART interrupt source to check.
494 * This parameter can be one of the following values:
495 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
496 * @arg @ref USART_IT_TC Transmission complete interrupt
497 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
498 * @arg @ref USART_IT_IDLE Idle line detection interrupt
499 * @arg @ref USART_IT_ORE OverRun Error interrupt
500 * @arg @ref USART_IT_NE Noise Error interrupt
501 * @arg @ref USART_IT_FE Framing Error interrupt
502 * @arg @ref USART_IT_PE Parity Error interrupt
503 * @retval The new state of __INTERRUPT__ (SET or RESET).
505 #define __HAL_USART_GET_IT(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->ISR & ((uint32_t)0x01U << (((__INTERRUPT__) & USART_ISR_MASK)>> USART_ISR_POS))) != 0U) ? SET : RESET)
507 /** @brief Check whether the specified USART interrupt source is enabled or not.
508 * @param __HANDLE__ specifies the USART Handle.
509 * @param __INTERRUPT__ specifies the USART interrupt source to check.
510 * This parameter can be one of the following values:
511 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt
512 * @arg @ref USART_IT_TC Transmission complete interrupt
513 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt
514 * @arg @ref USART_IT_IDLE Idle line detection interrupt
515 * @arg @ref USART_IT_ORE OverRun Error interrupt
516 * @arg @ref USART_IT_NE Noise Error interrupt
517 * @arg @ref USART_IT_FE Framing Error interrupt
518 * @arg @ref USART_IT_PE Parity Error interrupt
519 * @retval The new state of __INTERRUPT__ (SET or RESET).
521 #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x01U) ? (__HANDLE__)->Instance->CR1 : \
522 (((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x02U) ? (__HANDLE__)->Instance->CR2 : \
523 (__HANDLE__)->Instance->CR3)) & (0x01U << (((uint16_t)(__INTERRUPT__)) & USART_IT_MASK))) != 0U) ? SET : RESET)
526 /** @brief Clear the specified USART ISR flag, in setting the proper ICR register flag.
527 * @param __HANDLE__ specifies the USART Handle.
528 * @param __IT_CLEAR__ specifies the interrupt clear register flag that needs to be set
529 * to clear the corresponding interrupt.
530 * This parameter can be one of the following values:
531 * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag
532 * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag
533 * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag
534 * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag
535 * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag
536 * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag
537 * @retval None
539 #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__))
541 /** @brief Set a specific USART request flag.
542 * @param __HANDLE__ specifies the USART Handle.
543 * @param __REQ__ specifies the request flag to set.
544 * This parameter can be one of the following values:
545 * @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request
546 * @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request
548 * @retval None
550 #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__) ((__HANDLE__)->Instance->RQR |= (uint16_t)(__REQ__))
552 /** @brief Enable the USART one bit sample method.
553 * @param __HANDLE__ specifies the USART Handle.
554 * @retval None
556 #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
558 /** @brief Disable the USART one bit sample method.
559 * @param __HANDLE__ specifies the USART Handle.
560 * @retval None
562 #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= ~USART_CR3_ONEBIT)
564 /** @brief Enable USART.
565 * @param __HANDLE__ specifies the USART Handle.
566 * @retval None
568 #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
570 /** @brief Disable USART.
571 * @param __HANDLE__ specifies the USART Handle.
572 * @retval None
574 #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
577 * @}
580 /* Private macros --------------------------------------------------------*/
581 /** @defgroup USART_Private_Macros USART Private Macros
582 * @{
585 /** @brief BRR division operation to set BRR register in 8-bit oversampling mode.
586 * @param __PCLK__ USART clock.
587 * @param __BAUD__ Baud rate set by the user.
588 * @retval Division result
590 #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__) ((((__PCLK__)*2U) + ((__BAUD__)/2U)) / (__BAUD__))
592 /** @brief Report the USART clock source.
593 * @param __HANDLE__ specifies the USART Handle.
594 * @param __CLOCKSOURCE__ output variable.
595 * @retval the USART clocking source, written in __CLOCKSOURCE__.
597 #define USART_GETCLOCKSOURCE(__HANDLE__, __CLOCKSOURCE__) \
598 do { \
599 if((__HANDLE__)->Instance == USART1) \
601 switch(__HAL_RCC_GET_USART1_SOURCE()) \
603 case RCC_USART1CLKSOURCE_PCLK2: \
604 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
605 break; \
606 case RCC_USART1CLKSOURCE_HSI: \
607 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
608 break; \
609 case RCC_USART1CLKSOURCE_SYSCLK: \
610 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
611 break; \
612 case RCC_USART1CLKSOURCE_LSE: \
613 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
614 break; \
615 default: \
616 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
617 break; \
620 else if((__HANDLE__)->Instance == USART2) \
622 switch(__HAL_RCC_GET_USART2_SOURCE()) \
624 case RCC_USART2CLKSOURCE_PCLK1: \
625 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
626 break; \
627 case RCC_USART2CLKSOURCE_HSI: \
628 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
629 break; \
630 case RCC_USART2CLKSOURCE_SYSCLK: \
631 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
632 break; \
633 case RCC_USART2CLKSOURCE_LSE: \
634 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
635 break; \
636 default: \
637 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
638 break; \
641 else if((__HANDLE__)->Instance == USART3) \
643 switch(__HAL_RCC_GET_USART3_SOURCE()) \
645 case RCC_USART3CLKSOURCE_PCLK1: \
646 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \
647 break; \
648 case RCC_USART3CLKSOURCE_HSI: \
649 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
650 break; \
651 case RCC_USART3CLKSOURCE_SYSCLK: \
652 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
653 break; \
654 case RCC_USART3CLKSOURCE_LSE: \
655 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
656 break; \
657 default: \
658 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
659 break; \
662 else if((__HANDLE__)->Instance == USART6) \
664 switch(__HAL_RCC_GET_USART6_SOURCE()) \
666 case RCC_USART6CLKSOURCE_PCLK2: \
667 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK2; \
668 break; \
669 case RCC_USART6CLKSOURCE_HSI: \
670 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \
671 break; \
672 case RCC_USART6CLKSOURCE_SYSCLK: \
673 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \
674 break; \
675 case RCC_USART6CLKSOURCE_LSE: \
676 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \
677 break; \
678 default: \
679 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
680 break; \
683 else \
685 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \
687 } while(0U)
689 /** @brief Check USART Baud rate.
690 * @param __BAUDRATE__ Baudrate specified by the user.
691 * The maximum Baud Rate is derived from the maximum clock on F7 (i.e. 216 MHz)
692 * divided by the smallest oversampling used on the USART (i.e. 8)
693 * @retval SET (__BAUDRATE__ is valid) or RESET (__BAUDRATE__ is invalid) */
694 #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 27000000U)
697 * @brief Ensure that USART frame number of stop bits is valid.
698 * @param __STOPBITS__ USART frame number of stop bits.
699 * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid)
701 #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \
702 ((__STOPBITS__) == USART_STOPBITS_1) || \
703 ((__STOPBITS__) == USART_STOPBITS_1_5) || \
704 ((__STOPBITS__) == USART_STOPBITS_2))
707 * @brief Ensure that USART frame parity is valid.
708 * @param __PARITY__ USART frame parity.
709 * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid)
711 #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \
712 ((__PARITY__) == USART_PARITY_EVEN) || \
713 ((__PARITY__) == USART_PARITY_ODD))
716 * @brief Ensure that USART communication mode is valid.
717 * @param __MODE__ USART communication mode.
718 * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
720 #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U))
723 * @brief Ensure that USART oversampling is valid.
724 * @param __SAMPLING__ USART oversampling.
725 * @retval SET (__SAMPLING__ is valid) or RESET (__SAMPLING__ is invalid)
727 #define IS_USART_OVERSAMPLING(__SAMPLING__) (((__SAMPLING__) == USART_OVERSAMPLING_16) || \
728 ((__SAMPLING__) == USART_OVERSAMPLING_8))
731 * @brief Ensure that USART clock state is valid.
732 * @param __CLOCK__ USART clock state.
733 * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid)
735 #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \
736 ((__CLOCK__) == USART_CLOCK_ENABLE))
739 * @brief Ensure that USART frame polarity is valid.
740 * @param __CPOL__ USART frame polarity.
741 * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid)
743 #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH))
746 * @brief Ensure that USART frame phase is valid.
747 * @param __CPHA__ USART frame phase.
748 * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid)
750 #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE))
753 * @brief Ensure that USART frame last bit clock pulse setting is valid.
754 * @param __LASTBIT__ USART frame last bit clock pulse setting.
755 * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid)
757 #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \
758 ((__LASTBIT__) == USART_LASTBIT_ENABLE))
761 * @brief Ensure that USART request parameter is valid.
762 * @param __PARAM__ USART request parameter.
763 * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid)
765 #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \
766 ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST))
769 * @}
772 /* Include USART HAL Extended module */
773 #include "stm32f7xx_hal_usart_ex.h"
775 /* Exported functions --------------------------------------------------------*/
776 /** @addtogroup USART_Exported_Functions USART Exported Functions
777 * @{
780 /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
781 * @{
784 /* Initialization and de-initialization functions ****************************/
785 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart);
786 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart);
787 void HAL_USART_MspInit(USART_HandleTypeDef *husart);
788 void HAL_USART_MspDeInit(USART_HandleTypeDef *husart);
790 /* Callbacks Register/UnRegister functions ***********************************/
791 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
792 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback);
793 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID);
794 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
797 * @}
800 /** @addtogroup USART_Exported_Functions_Group2 IO operation functions
801 * @{
804 /* IO operation functions *****************************************************/
805 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout);
806 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
807 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout);
808 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
809 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
810 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
811 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size);
812 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size);
813 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size);
814 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart);
815 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart);
816 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart);
817 /* Transfer Abort functions */
818 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart);
819 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart);
821 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart);
822 void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart);
823 void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart);
824 void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart);
825 void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart);
826 void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart);
827 void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart);
828 void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart);
831 * @}
834 /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions
835 * @{
838 /* Peripheral State and Error functions ***************************************/
839 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart);
840 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart);
843 * @}
847 * @}
851 * @}
855 * @}
858 #ifdef __cplusplus
860 #endif
862 #endif /* STM32F7xx_HAL_USART_H */
864 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/