FIX: Flash page size check is STM (or clone) specific (#14130)
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Inc / stm32f4xx_hal_uart.h
blobba2ed4f839286eae45f90b6bb80a19b82e6232d6
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_uart.h
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief Header file of UART HAL module.
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 ******************************************************************************
36 */
38 /* Define to prevent recursive inclusion -------------------------------------*/
39 #ifndef __STM32F4xx_HAL_UART_H
40 #define __STM32F4xx_HAL_UART_H
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f4xx_hal_def.h"
49 /** @addtogroup STM32F4xx_HAL_Driver
50 * @{
53 /** @addtogroup UART
54 * @{
55 */
57 /* Exported types ------------------------------------------------------------*/
58 /** @defgroup UART_Exported_Types UART Exported Types
59 * @{
62 /**
63 * @brief UART Init Structure definition
64 */
65 typedef struct
67 uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
68 The baud rate is computed using the following formula:
69 - IntegerDivider = ((PCLKx) / (8 * (OVR8+1) * (huart->Init.BaudRate)))
70 - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8 * (OVR8+1)) + 0.5
71 Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */
73 uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
74 This parameter can be a value of @ref UART_Word_Length */
76 uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
77 This parameter can be a value of @ref UART_Stop_Bits */
79 uint32_t Parity; /*!< Specifies the parity mode.
80 This parameter can be a value of @ref UART_Parity
81 @note When parity is enabled, the computed parity is inserted
82 at the MSB position of the transmitted data (9th bit when
83 the word length is set to 9 data bits; 8th bit when the
84 word length is set to 8 data bits). */
86 uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
87 This parameter can be a value of @ref UART_Mode */
89 uint32_t HwFlowCtl; /*!< Specifies whether the hardware flow control mode is enabled
90 or disabled.
91 This parameter can be a value of @ref UART_Hardware_Flow_Control */
93 uint32_t OverSampling; /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
94 This parameter can be a value of @ref UART_Over_Sampling */
95 }UART_InitTypeDef;
97 /**
98 * @brief HAL UART State structures definition
99 * @note HAL UART State value is a combination of 2 different substates: gState and RxState.
100 * - gState contains UART state information related to global Handle management
101 * and also information related to Tx operations.
102 * gState value coding follow below described bitmap :
103 * b7-b6 Error information
104 * 00 : No Error
105 * 01 : (Not Used)
106 * 10 : Timeout
107 * 11 : Error
108 * b5 IP initilisation status
109 * 0 : Reset (IP not initialized)
110 * 1 : Init done (IP not initialized. HAL UART Init function already called)
111 * b4-b3 (not used)
112 * xx : Should be set to 00
113 * b2 Intrinsic process state
114 * 0 : Ready
115 * 1 : Busy (IP busy with some configuration or internal operations)
116 * b1 (not used)
117 * x : Should be set to 0
118 * b0 Tx state
119 * 0 : Ready (no Tx operation ongoing)
120 * 1 : Busy (Tx operation ongoing)
121 * - RxState contains information related to Rx operations.
122 * RxState value coding follow below described bitmap :
123 * b7-b6 (not used)
124 * xx : Should be set to 00
125 * b5 IP initilisation status
126 * 0 : Reset (IP not initialized)
127 * 1 : Init done (IP not initialized)
128 * b4-b2 (not used)
129 * xxx : Should be set to 000
130 * b1 Rx state
131 * 0 : Ready (no Rx operation ongoing)
132 * 1 : Busy (Rx operation ongoing)
133 * b0 (not used)
134 * x : Should be set to 0.
136 typedef enum
138 HAL_UART_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized
139 Value is allowed for gState and RxState */
140 HAL_UART_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use
141 Value is allowed for gState and RxState */
142 HAL_UART_STATE_BUSY = 0x24U, /*!< an internal process is ongoing
143 Value is allowed for gState only */
144 HAL_UART_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing
145 Value is allowed for gState only */
146 HAL_UART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing
147 Value is allowed for RxState only */
148 HAL_UART_STATE_BUSY_TX_RX = 0x23U, /*!< Data Transmission and Reception process is ongoing
149 Not to be used for neither gState nor RxState.
150 Value is result of combination (Or) between gState and RxState values */
151 HAL_UART_STATE_TIMEOUT = 0xA0U, /*!< Timeout state
152 Value is allowed for gState only */
153 HAL_UART_STATE_ERROR = 0xE0U /*!< Error
154 Value is allowed for gState only */
155 }HAL_UART_StateTypeDef;
157 /**
158 * @brief UART handle Structure definition
160 typedef struct
162 USART_TypeDef *Instance; /*!< UART registers base address */
164 UART_InitTypeDef Init; /*!< UART communication parameters */
166 uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer */
168 uint16_t TxXferSize; /*!< UART Tx Transfer size */
170 __IO uint16_t TxXferCount; /*!< UART Tx Transfer Counter */
172 uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer */
174 uint16_t RxXferSize; /*!< UART Rx Transfer size */
176 __IO uint16_t RxXferCount; /*!< UART Rx Transfer Counter */
178 DMA_HandleTypeDef *hdmatx; /*!< UART Tx DMA Handle parameters */
180 DMA_HandleTypeDef *hdmarx; /*!< UART Rx DMA Handle parameters */
182 HAL_LockTypeDef Lock; /*!< Locking object */
184 __IO HAL_UART_StateTypeDef gState; /*!< UART state information related to global Handle management
185 and also related to Tx operations.
186 This parameter can be a value of @ref HAL_UART_StateTypeDef */
188 __IO HAL_UART_StateTypeDef RxState; /*!< UART state information related to Rx operations.
189 This parameter can be a value of @ref HAL_UART_StateTypeDef */
191 __IO uint32_t ErrorCode; /*!< UART Error code */
193 }UART_HandleTypeDef;
195 * @}
198 /* Exported constants --------------------------------------------------------*/
199 /** @defgroup UART_Exported_Constants UART Exported constants
200 * @{
203 /** @defgroup UART_Error_Code UART Error Code
204 * @brief UART Error Code
205 * @{
207 #define HAL_UART_ERROR_NONE 0x00000000U /*!< No error */
208 #define HAL_UART_ERROR_PE 0x00000001U /*!< Parity error */
209 #define HAL_UART_ERROR_NE 0x00000002U /*!< Noise error */
210 #define HAL_UART_ERROR_FE 0x00000004U /*!< Frame error */
211 #define HAL_UART_ERROR_ORE 0x00000008U /*!< Overrun error */
212 #define HAL_UART_ERROR_DMA 0x00000010U /*!< DMA transfer error */
214 * @}
217 /** @defgroup UART_Word_Length UART Word Length
218 * @{
220 #define UART_WORDLENGTH_8B 0x00000000U
221 #define UART_WORDLENGTH_9B ((uint32_t)USART_CR1_M)
223 * @}
226 /** @defgroup UART_Stop_Bits UART Number of Stop Bits
227 * @{
229 #define UART_STOPBITS_1 0x00000000U
230 #define UART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1)
232 * @}
235 /** @defgroup UART_Parity UART Parity
236 * @{
238 #define UART_PARITY_NONE 0x00000000U
239 #define UART_PARITY_EVEN ((uint32_t)USART_CR1_PCE)
240 #define UART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS))
242 * @}
245 /** @defgroup UART_Hardware_Flow_Control UART Hardware Flow Control
246 * @{
248 #define UART_HWCONTROL_NONE 0x00000000U
249 #define UART_HWCONTROL_RTS ((uint32_t)USART_CR3_RTSE)
250 #define UART_HWCONTROL_CTS ((uint32_t)USART_CR3_CTSE)
251 #define UART_HWCONTROL_RTS_CTS ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
253 * @}
256 /** @defgroup UART_Mode UART Transfer Mode
257 * @{
259 #define UART_MODE_RX ((uint32_t)USART_CR1_RE)
260 #define UART_MODE_TX ((uint32_t)USART_CR1_TE)
261 #define UART_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE))
263 * @}
266 /** @defgroup UART_State UART State
267 * @{
269 #define UART_STATE_DISABLE 0x00000000U
270 #define UART_STATE_ENABLE ((uint32_t)USART_CR1_UE)
272 * @}
275 /** @defgroup UART_Over_Sampling UART Over Sampling
276 * @{
278 #define UART_OVERSAMPLING_16 0x00000000U
279 #define UART_OVERSAMPLING_8 ((uint32_t)USART_CR1_OVER8)
281 * @}
284 /** @defgroup UART_LIN_Break_Detection_Length UART LIN Break Detection Length
285 * @{
287 #define UART_LINBREAKDETECTLENGTH_10B 0x00000000U
288 #define UART_LINBREAKDETECTLENGTH_11B 0x00000020U
290 * @}
293 /** @defgroup UART_WakeUp_functions UART Wakeup Functions
294 * @{
296 #define UART_WAKEUPMETHOD_IDLELINE 0x00000000U
297 #define UART_WAKEUPMETHOD_ADDRESSMARK 0x00000800U
299 * @}
302 /** @defgroup UART_Flags UART FLags
303 * Elements values convention: 0xXXXX
304 * - 0xXXXX : Flag mask in the SR register
305 * @{
307 #define UART_FLAG_CTS ((uint32_t)USART_SR_CTS)
308 #define UART_FLAG_LBD ((uint32_t)USART_SR_LBD)
309 #define UART_FLAG_TXE ((uint32_t)USART_SR_TXE)
310 #define UART_FLAG_TC ((uint32_t)USART_SR_TC)
311 #define UART_FLAG_RXNE ((uint32_t)USART_SR_RXNE)
312 #define UART_FLAG_IDLE ((uint32_t)USART_SR_IDLE)
313 #define UART_FLAG_ORE ((uint32_t)USART_SR_ORE)
314 #define UART_FLAG_NE ((uint32_t)USART_SR_NE)
315 #define UART_FLAG_FE ((uint32_t)USART_SR_FE)
316 #define UART_FLAG_PE ((uint32_t)USART_SR_PE)
318 * @}
321 /** @defgroup UART_Interrupt_definition UART Interrupt Definitions
322 * Elements values convention: 0xY000XXXX
323 * - XXXX : Interrupt mask (16 bits) in the Y register
324 * - Y : Interrupt source register (2bits)
325 * - 0001: CR1 register
326 * - 0010: CR2 register
327 * - 0011: CR3 register
329 * @{
332 #define UART_IT_PE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_PEIE))
333 #define UART_IT_TXE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_TXEIE))
334 #define UART_IT_TC ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_TCIE))
335 #define UART_IT_RXNE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_RXNEIE))
336 #define UART_IT_IDLE ((uint32_t)(UART_CR1_REG_INDEX << 28U | USART_CR1_IDLEIE))
338 #define UART_IT_LBD ((uint32_t)(UART_CR2_REG_INDEX << 28U | USART_CR2_LBDIE))
340 #define UART_IT_CTS ((uint32_t)(UART_CR3_REG_INDEX << 28U | USART_CR3_CTSIE))
341 #define UART_IT_ERR ((uint32_t)(UART_CR3_REG_INDEX << 28U | USART_CR3_EIE))
343 * @}
347 * @}
350 /* Exported macro ------------------------------------------------------------*/
351 /** @defgroup UART_Exported_Macros UART Exported Macros
352 * @{
355 /** @brief Reset UART handle gstate & RxState
356 * @param __HANDLE__: specifies the UART Handle.
357 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
358 * UART peripheral.
359 * @retval None
361 #define __HAL_UART_RESET_HANDLE_STATE(__HANDLE__) do{ \
362 (__HANDLE__)->gState = HAL_UART_STATE_RESET; \
363 (__HANDLE__)->RxState = HAL_UART_STATE_RESET; \
364 } while(0U)
366 /** @brief Flushes the UART DR register
367 * @param __HANDLE__: specifies the UART Handle.
369 #define __HAL_UART_FLUSH_DRREGISTER(__HANDLE__) ((__HANDLE__)->Instance->DR)
371 /** @brief Checks whether the specified UART flag is set or not.
372 * @param __HANDLE__: specifies the UART Handle.
373 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
374 * UART peripheral.
375 * @param __FLAG__: specifies the flag to check.
376 * This parameter can be one of the following values:
377 * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
378 * @arg UART_FLAG_LBD: LIN Break detection flag
379 * @arg UART_FLAG_TXE: Transmit data register empty flag
380 * @arg UART_FLAG_TC: Transmission Complete flag
381 * @arg UART_FLAG_RXNE: Receive data register not empty flag
382 * @arg UART_FLAG_IDLE: Idle Line detection flag
383 * @arg UART_FLAG_ORE: Overrun Error flag
384 * @arg UART_FLAG_NE: Noise Error flag
385 * @arg UART_FLAG_FE: Framing Error flag
386 * @arg UART_FLAG_PE: Parity Error flag
387 * @retval The new state of __FLAG__ (TRUE or FALSE).
390 #define __HAL_UART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
392 /** @brief Clears the specified UART pending flag.
393 * @param __HANDLE__: specifies the UART Handle.
394 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
395 * UART peripheral.
396 * @param __FLAG__: specifies the flag to check.
397 * This parameter can be any combination of the following values:
398 * @arg UART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
399 * @arg UART_FLAG_LBD: LIN Break detection flag.
400 * @arg UART_FLAG_TC: Transmission Complete flag.
401 * @arg UART_FLAG_RXNE: Receive data register not empty flag.
403 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (Overrun
404 * error) and IDLE (Idle line detected) flags are cleared by software
405 * sequence: a read operation to USART_SR register followed by a read
406 * operation to USART_DR register.
407 * @note RXNE flag can be also cleared by a read to the USART_DR register.
408 * @note TC flag can be also cleared by software sequence: a read operation to
409 * USART_SR register followed by a write operation to USART_DR register.
410 * @note TXE flag is cleared only by a write to the USART_DR register.
412 * @retval None
414 #define __HAL_UART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__))
416 /** @brief Clear the UART PE pending flag.
417 * @param __HANDLE__: specifies the UART Handle.
418 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
419 * UART peripheral.
420 * @retval None
422 #define __HAL_UART_CLEAR_PEFLAG(__HANDLE__) \
423 do{ \
424 __IO uint32_t tmpreg = 0x00U; \
425 tmpreg = (__HANDLE__)->Instance->SR; \
426 tmpreg = (__HANDLE__)->Instance->DR; \
427 UNUSED(tmpreg); \
428 } while(0U)
430 /** @brief Clear the UART FE pending flag.
431 * @param __HANDLE__: specifies the UART Handle.
432 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
433 * UART peripheral.
434 * @retval None
436 #define __HAL_UART_CLEAR_FEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
438 /** @brief Clear the UART NE pending flag.
439 * @param __HANDLE__: specifies the UART Handle.
440 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
441 * UART peripheral.
442 * @retval None
444 #define __HAL_UART_CLEAR_NEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
446 /** @brief Clear the UART ORE pending flag.
447 * @param __HANDLE__: specifies the UART Handle.
448 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
449 * UART peripheral.
450 * @retval None
452 #define __HAL_UART_CLEAR_OREFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
454 /** @brief Clear the UART IDLE pending flag.
455 * @param __HANDLE__: specifies the UART Handle.
456 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
457 * UART peripheral.
458 * @retval None
460 #define __HAL_UART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_UART_CLEAR_PEFLAG(__HANDLE__)
462 /** @brief Enable the specified UART interrupt.
463 * @param __HANDLE__: specifies the UART Handle.
464 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
465 * UART peripheral.
466 * @param __INTERRUPT__: specifies the UART interrupt source to enable.
467 * This parameter can be one of the following values:
468 * @arg UART_IT_CTS: CTS change interrupt
469 * @arg UART_IT_LBD: LIN Break detection interrupt
470 * @arg UART_IT_TXE: Transmit Data Register empty interrupt
471 * @arg UART_IT_TC: Transmission complete interrupt
472 * @arg UART_IT_RXNE: Receive Data register not empty interrupt
473 * @arg UART_IT_IDLE: Idle line detection interrupt
474 * @arg UART_IT_PE: Parity Error interrupt
475 * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
476 * @retval None
478 #define UART_IT_MASK 0x0000FFFFU
479 #define __HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == 1U)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & UART_IT_MASK)): \
480 (((__INTERRUPT__) >> 28U) == 2U)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & UART_IT_MASK)): \
481 ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & UART_IT_MASK)))
482 /** @brief Disable the specified UART interrupt.
483 * @param __HANDLE__: specifies the UART Handle.
484 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
485 * UART peripheral.
486 * @param __INTERRUPT__: specifies the UART interrupt source to disable.
487 * This parameter can be one of the following values:
488 * @arg UART_IT_CTS: CTS change interrupt
489 * @arg UART_IT_LBD: LIN Break detection interrupt
490 * @arg UART_IT_TXE: Transmit Data Register empty interrupt
491 * @arg UART_IT_TC: Transmission complete interrupt
492 * @arg UART_IT_RXNE: Receive Data register not empty interrupt
493 * @arg UART_IT_IDLE: Idle line detection interrupt
494 * @arg UART_IT_PE: Parity Error interrupt
495 * @arg UART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
496 * @retval None
498 #define __HAL_UART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28U) == 1U)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
499 (((__INTERRUPT__) >> 28U) == 2U)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & UART_IT_MASK)): \
500 ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & UART_IT_MASK)))
502 /** @brief Checks whether the specified UART interrupt has occurred or not.
503 * @param __HANDLE__: specifies the UART Handle.
504 * This parameter can be UARTx where x: 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
505 * UART peripheral.
506 * @param __IT__: specifies the UART interrupt source to check.
507 * This parameter can be one of the following values:
508 * @arg UART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
509 * @arg UART_IT_LBD: LIN Break detection interrupt
510 * @arg UART_IT_TXE: Transmit Data Register empty interrupt
511 * @arg UART_IT_TC: Transmission complete interrupt
512 * @arg UART_IT_RXNE: Receive Data register not empty interrupt
513 * @arg UART_IT_IDLE: Idle line detection interrupt
514 * @arg USART_IT_ERR: Error interrupt
515 * @retval The new state of __IT__ (TRUE or FALSE).
517 #define __HAL_UART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28U) == 1U)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28U) == 2U)? \
518 (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & UART_IT_MASK))
520 /** @brief Enable CTS flow control
521 * This macro allows to enable CTS hardware flow control for a given UART instance,
522 * without need to call HAL_UART_Init() function.
523 * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
524 * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
525 * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
526 * - UART instance should have already been initialised (through call of HAL_UART_Init() )
527 * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
528 * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
529 * @param __HANDLE__: specifies the UART Handle.
530 * The Handle Instance can be USART1, USART2 or LPUART.
531 * @retval None
533 #define __HAL_UART_HWCONTROL_CTS_ENABLE(__HANDLE__) \
534 do{ \
535 SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
536 (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_CTSE; \
537 } while(0U)
539 /** @brief Disable CTS flow control
540 * This macro allows to disable CTS hardware flow control for a given UART instance,
541 * without need to call HAL_UART_Init() function.
542 * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
543 * @note As macro is expected to be used for modifying CTS Hw flow control feature activation, without need
544 * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
545 * - UART instance should have already been initialised (through call of HAL_UART_Init() )
546 * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
547 * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
548 * @param __HANDLE__: specifies the UART Handle.
549 * The Handle Instance can be USART1, USART2 or LPUART.
550 * @retval None
552 #define __HAL_UART_HWCONTROL_CTS_DISABLE(__HANDLE__) \
553 do{ \
554 CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_CTSE); \
555 (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_CTSE); \
556 } while(0U)
558 /** @brief Enable RTS flow control
559 * This macro allows to enable RTS hardware flow control for a given UART instance,
560 * without need to call HAL_UART_Init() function.
561 * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
562 * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
563 * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
564 * - UART instance should have already been initialised (through call of HAL_UART_Init() )
565 * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
566 * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
567 * @param __HANDLE__: specifies the UART Handle.
568 * The Handle Instance can be USART1, USART2 or LPUART.
569 * @retval None
571 #define __HAL_UART_HWCONTROL_RTS_ENABLE(__HANDLE__) \
572 do{ \
573 SET_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE); \
574 (__HANDLE__)->Init.HwFlowCtl |= USART_CR3_RTSE; \
575 } while(0U)
577 /** @brief Disable RTS flow control
578 * This macro allows to disable RTS hardware flow control for a given UART instance,
579 * without need to call HAL_UART_Init() function.
580 * As involving direct access to UART registers, usage of this macro should be fully endorsed by user.
581 * @note As macro is expected to be used for modifying RTS Hw flow control feature activation, without need
582 * for USART instance Deinit/Init, following conditions for macro call should be fulfilled :
583 * - UART instance should have already been initialised (through call of HAL_UART_Init() )
584 * - macro could only be called when corresponding UART instance is disabled (i.e __HAL_UART_DISABLE(__HANDLE__))
585 * and should be followed by an Enable macro (i.e __HAL_UART_ENABLE(__HANDLE__)).
586 * @param __HANDLE__: specifies the UART Handle.
587 * The Handle Instance can be USART1, USART2 or LPUART.
588 * @retval None
590 #define __HAL_UART_HWCONTROL_RTS_DISABLE(__HANDLE__) \
591 do{ \
592 CLEAR_BIT((__HANDLE__)->Instance->CR3, USART_CR3_RTSE);\
593 (__HANDLE__)->Init.HwFlowCtl &= ~(USART_CR3_RTSE); \
594 } while(0U)
596 /** @brief macros to enables the UART's one bit sample method
597 * @param __HANDLE__: specifies the UART Handle.
598 * @retval None
600 #define __HAL_UART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT)
602 /** @brief macros to disables the UART's one bit sample method
603 * @param __HANDLE__: specifies the UART Handle.
604 * @retval None
606 #define __HAL_UART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT))
608 /** @brief Enable UART
609 * @param __HANDLE__: specifies the UART Handle.
610 * @retval None
612 #define __HAL_UART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE)
614 /** @brief Disable UART
615 * @param __HANDLE__: specifies the UART Handle.
616 * @retval None
618 #define __HAL_UART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE)
620 * @}
623 /* Exported functions --------------------------------------------------------*/
624 /** @addtogroup UART_Exported_Functions
625 * @{
628 /** @addtogroup UART_Exported_Functions_Group1
629 * @{
631 /* Initialization/de-initialization functions **********************************/
632 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart);
633 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart);
634 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength);
635 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod);
636 HAL_StatusTypeDef HAL_UART_DeInit (UART_HandleTypeDef *huart);
637 void HAL_UART_MspInit(UART_HandleTypeDef *huart);
638 void HAL_UART_MspDeInit(UART_HandleTypeDef *huart);
640 * @}
643 /** @addtogroup UART_Exported_Functions_Group2
644 * @{
646 /* IO operation functions *******************************************************/
647 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
648 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
649 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
650 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
651 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
652 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size);
653 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart);
654 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart);
655 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart);
656 /* Transfer Abort functions */
657 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart);
658 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart);
659 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart);
660 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart);
661 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart);
662 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart);
664 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart);
665 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
666 void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart);
667 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
668 void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart);
669 void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
670 void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart);
671 void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart);
672 void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart);
674 * @}
677 /** @addtogroup UART_Exported_Functions_Group3
678 * @{
680 /* Peripheral Control functions ************************************************/
681 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart);
682 HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart);
683 HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart);
684 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart);
685 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart);
687 * @}
690 /** @addtogroup UART_Exported_Functions_Group4
691 * @{
693 /* Peripheral State functions **************************************************/
694 HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart);
695 uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart);
697 * @}
701 * @}
703 /* Private types -------------------------------------------------------------*/
704 /* Private variables ---------------------------------------------------------*/
705 /* Private constants ---------------------------------------------------------*/
706 /** @defgroup UART_Private_Constants UART Private Constants
707 * @{
709 /** @brief UART interruptions flag mask
712 #define UART_CR1_REG_INDEX 1U
713 #define UART_CR2_REG_INDEX 2U
714 #define UART_CR3_REG_INDEX 3U
716 * @}
719 /* Private macros ------------------------------------------------------------*/
720 /** @defgroup UART_Private_Macros UART Private Macros
721 * @{
723 #define IS_UART_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B) || \
724 ((LENGTH) == UART_WORDLENGTH_9B))
725 #define IS_UART_LIN_WORD_LENGTH(LENGTH) (((LENGTH) == UART_WORDLENGTH_8B))
726 #define IS_UART_STOPBITS(STOPBITS) (((STOPBITS) == UART_STOPBITS_1) || \
727 ((STOPBITS) == UART_STOPBITS_2))
728 #define IS_UART_PARITY(PARITY) (((PARITY) == UART_PARITY_NONE) || \
729 ((PARITY) == UART_PARITY_EVEN) || \
730 ((PARITY) == UART_PARITY_ODD))
731 #define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\
732 (((CONTROL) == UART_HWCONTROL_NONE) || \
733 ((CONTROL) == UART_HWCONTROL_RTS) || \
734 ((CONTROL) == UART_HWCONTROL_CTS) || \
735 ((CONTROL) == UART_HWCONTROL_RTS_CTS))
736 #define IS_UART_MODE(MODE) ((((MODE) & 0x0000FFF3U) == 0x00U) && ((MODE) != 0x00U))
737 #define IS_UART_STATE(STATE) (((STATE) == UART_STATE_DISABLE) || \
738 ((STATE) == UART_STATE_ENABLE))
739 #define IS_UART_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16) || \
740 ((SAMPLING) == UART_OVERSAMPLING_8))
741 #define IS_UART_LIN_OVERSAMPLING(SAMPLING) (((SAMPLING) == UART_OVERSAMPLING_16))
742 #define IS_UART_LIN_BREAK_DETECT_LENGTH(LENGTH) (((LENGTH) == UART_LINBREAKDETECTLENGTH_10B) || \
743 ((LENGTH) == UART_LINBREAKDETECTLENGTH_11B))
744 #define IS_UART_WAKEUPMETHOD(WAKEUP) (((WAKEUP) == UART_WAKEUPMETHOD_IDLELINE) || \
745 ((WAKEUP) == UART_WAKEUPMETHOD_ADDRESSMARK))
746 #define IS_UART_BAUDRATE(BAUDRATE) ((BAUDRATE) < 10500001U)
747 #define IS_UART_ADDRESS(ADDRESS) ((ADDRESS) <= 0x0FU)
749 #define UART_DIV_SAMPLING16(_PCLK_, _BAUD_) (((_PCLK_)*25U)/(4U*(_BAUD_)))
750 #define UART_DIVMANT_SAMPLING16(_PCLK_, _BAUD_) (UART_DIV_SAMPLING16((_PCLK_), (_BAUD_))/100U)
751 #define UART_DIVFRAQ_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING16((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) * 100U)) * 16U + 50U) / 100U)
752 /* UART BRR = mantissa + overflow + fraction
753 = (UART DIVMANT << 4) + (UART DIVFRAQ & 0xF0) + (UART DIVFRAQ & 0x0FU) */
754 #define UART_BRR_SAMPLING16(_PCLK_, _BAUD_) (((UART_DIVMANT_SAMPLING16((_PCLK_), (_BAUD_)) << 4U) + \
755 (UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0xF0U)) + \
756 (UART_DIVFRAQ_SAMPLING16((_PCLK_), (_BAUD_)) & 0x0FU))
758 #define UART_DIV_SAMPLING8(_PCLK_, _BAUD_) (((_PCLK_)*25U)/(2U*(_BAUD_)))
759 #define UART_DIVMANT_SAMPLING8(_PCLK_, _BAUD_) (UART_DIV_SAMPLING8((_PCLK_), (_BAUD_))/100U)
760 #define UART_DIVFRAQ_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIV_SAMPLING8((_PCLK_), (_BAUD_)) - (UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) * 100U)) * 8U + 50U) / 100U)
761 /* UART BRR = mantissa + overflow + fraction
762 = (UART DIVMANT << 4) + ((UART DIVFRAQ & 0xF8) << 1) + (UART DIVFRAQ & 0x07U) */
763 #define UART_BRR_SAMPLING8(_PCLK_, _BAUD_) (((UART_DIVMANT_SAMPLING8((_PCLK_), (_BAUD_)) << 4U) + \
764 ((UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0xF8U) << 1U)) + \
765 (UART_DIVFRAQ_SAMPLING8((_PCLK_), (_BAUD_)) & 0x07U))
768 * @}
771 /* Private functions ---------------------------------------------------------*/
772 /** @defgroup UART_Private_Functions UART Private Functions
773 * @{
777 * @}
781 * @}
785 * @}
788 #ifdef __cplusplus
790 #endif
792 #endif /* __STM32F4xx_HAL_UART_H */
794 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/