1 /** 2 ****************************************************************************** 3 * @file stm32f0xx_hal_usart.h 4 * @author MCD Application Team 5 * @brief Header file of USART HAL module. 6 ****************************************************************************** 7 * @attention 8 * 9 * Copyright (c) 2016 STMicroelectronics. 10 * All rights reserved. 11 * 12 * This software is licensed under terms that can be found in the LICENSE file 13 * in the root directory of this software component. 14 * If no LICENSE file comes with this software, it is provided AS-IS. 15 * 16 ****************************************************************************** 17 */ 18 19 /* Define to prevent recursive inclusion -------------------------------------*/ 20 #ifndef STM32F0xx_HAL_USART_H 21 #define STM32F0xx_HAL_USART_H 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 /* Includes ------------------------------------------------------------------*/ 28 #include "stm32f0xx_hal_def.h" 29 30 /** @addtogroup STM32F0xx_HAL_Driver 31 * @{ 32 */ 33 34 /** @addtogroup USART 35 * @{ 36 */ 37 38 /* Exported types ------------------------------------------------------------*/ 39 /** @defgroup USART_Exported_Types USART Exported Types 40 * @{ 41 */ 42 43 /** 44 * @brief USART Init Structure definition 45 */ 46 typedef struct 47 { 48 uint32_t BaudRate; /*!< This member configures the Usart communication baud rate. 49 The baud rate is computed using the following formula: 50 Baud Rate Register[15:4] = ((2 * fclk_pres) / 51 ((huart->Init.BaudRate)))[15:4] 52 Baud Rate Register[3] = 0 53 Baud Rate Register[2:0] = (((2 * fclk_pres) / 54 ((huart->Init.BaudRate)))[3:0]) >> 1 55 where fclk_pres is the USART input clock frequency 56 @note Oversampling by 8 is systematically applied to 57 achieve high baud rates. */ 58 59 uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame. 60 This parameter can be a value of @ref USARTEx_Word_Length. */ 61 62 uint32_t StopBits; /*!< Specifies the number of stop bits transmitted. 63 This parameter can be a value of @ref USART_Stop_Bits. */ 64 65 uint32_t Parity; /*!< Specifies the parity mode. 66 This parameter can be a value of @ref USART_Parity 67 @note When parity is enabled, the computed parity is inserted 68 at the MSB position of the transmitted data (9th bit when 69 the word length is set to 9 data bits; 8th bit when the 70 word length is set to 8 data bits). */ 71 72 uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled. 73 This parameter can be a value of @ref USART_Mode. */ 74 75 uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock. 76 This parameter can be a value of @ref USART_Clock_Polarity. */ 77 78 uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made. 79 This parameter can be a value of @ref USART_Clock_Phase. */ 80 81 uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted 82 data bit (MSB) has to be output on the SCLK pin in synchronous mode. 83 This parameter can be a value of @ref USART_Last_Bit. */ 84 85 } USART_InitTypeDef; 86 87 /** 88 * @brief HAL USART State structures definition 89 */ 90 typedef enum 91 { 92 HAL_USART_STATE_RESET = 0x00U, /*!< Peripheral is not initialized */ 93 HAL_USART_STATE_READY = 0x01U, /*!< Peripheral Initialized and ready for use */ 94 HAL_USART_STATE_BUSY = 0x02U, /*!< an internal process is ongoing */ 95 HAL_USART_STATE_BUSY_TX = 0x12U, /*!< Data Transmission process is ongoing */ 96 HAL_USART_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */ 97 HAL_USART_STATE_BUSY_TX_RX = 0x32U, /*!< Data Transmission Reception process is ongoing */ 98 HAL_USART_STATE_TIMEOUT = 0x03U, /*!< Timeout state */ 99 HAL_USART_STATE_ERROR = 0x04U /*!< Error */ 100 } HAL_USART_StateTypeDef; 101 102 /** 103 * @brief USART clock sources definitions 104 */ 105 typedef enum 106 { 107 USART_CLOCKSOURCE_PCLK1 = 0x00U, /*!< PCLK1 clock source */ 108 USART_CLOCKSOURCE_HSI = 0x02U, /*!< HSI clock source */ 109 USART_CLOCKSOURCE_SYSCLK = 0x04U, /*!< SYSCLK clock source */ 110 USART_CLOCKSOURCE_LSE = 0x08U, /*!< LSE clock source */ 111 USART_CLOCKSOURCE_UNDEFINED = 0x10U /*!< Undefined clock source */ 112 } USART_ClockSourceTypeDef; 113 114 /** 115 * @brief USART handle Structure definition 116 */ 117 typedef struct __USART_HandleTypeDef 118 { 119 USART_TypeDef *Instance; /*!< USART registers base address */ 120 121 USART_InitTypeDef Init; /*!< USART communication parameters */ 122 123 const uint8_t *pTxBuffPtr; /*!< Pointer to USART Tx transfer Buffer */ 124 125 uint16_t TxXferSize; /*!< USART Tx Transfer size */ 126 127 __IO uint16_t TxXferCount; /*!< USART Tx Transfer Counter */ 128 129 uint8_t *pRxBuffPtr; /*!< Pointer to USART Rx transfer Buffer */ 130 131 uint16_t RxXferSize; /*!< USART Rx Transfer size */ 132 133 __IO uint16_t RxXferCount; /*!< USART Rx Transfer Counter */ 134 135 uint16_t Mask; /*!< USART Rx RDR register mask */ 136 137 void (*RxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Rx IRQ handler */ 138 139 void (*TxISR)(struct __USART_HandleTypeDef *husart); /*!< Function pointer on Tx IRQ handler */ 140 141 DMA_HandleTypeDef *hdmatx; /*!< USART Tx DMA Handle parameters */ 142 143 DMA_HandleTypeDef *hdmarx; /*!< USART Rx DMA Handle parameters */ 144 145 HAL_LockTypeDef Lock; /*!< Locking object */ 146 147 __IO HAL_USART_StateTypeDef State; /*!< USART communication state */ 148 149 __IO uint32_t ErrorCode; /*!< USART Error code */ 150 151 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 152 void (* TxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Half Complete Callback */ 153 void (* TxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Complete Callback */ 154 void (* RxHalfCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Half Complete Callback */ 155 void (* RxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Rx Complete Callback */ 156 void (* TxRxCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Tx Rx Complete Callback */ 157 void (* ErrorCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Error Callback */ 158 void (* AbortCpltCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Abort Complete Callback */ 159 160 void (* MspInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp Init callback */ 161 void (* MspDeInitCallback)(struct __USART_HandleTypeDef *husart); /*!< USART Msp DeInit callback */ 162 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 163 164 } USART_HandleTypeDef; 165 166 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 167 /** 168 * @brief HAL USART Callback ID enumeration definition 169 */ 170 typedef enum 171 { 172 HAL_USART_TX_HALFCOMPLETE_CB_ID = 0x00U, /*!< USART Tx Half Complete Callback ID */ 173 HAL_USART_TX_COMPLETE_CB_ID = 0x01U, /*!< USART Tx Complete Callback ID */ 174 HAL_USART_RX_HALFCOMPLETE_CB_ID = 0x02U, /*!< USART Rx Half Complete Callback ID */ 175 HAL_USART_RX_COMPLETE_CB_ID = 0x03U, /*!< USART Rx Complete Callback ID */ 176 HAL_USART_TX_RX_COMPLETE_CB_ID = 0x04U, /*!< USART Tx Rx Complete Callback ID */ 177 HAL_USART_ERROR_CB_ID = 0x05U, /*!< USART Error Callback ID */ 178 HAL_USART_ABORT_COMPLETE_CB_ID = 0x06U, /*!< USART Abort Complete Callback ID */ 179 180 HAL_USART_MSPINIT_CB_ID = 0x09U, /*!< USART MspInit callback ID */ 181 HAL_USART_MSPDEINIT_CB_ID = 0x0AU /*!< USART MspDeInit callback ID */ 182 183 } HAL_USART_CallbackIDTypeDef; 184 185 /** 186 * @brief HAL USART Callback pointer definition 187 */ 188 typedef void (*pUSART_CallbackTypeDef)(USART_HandleTypeDef *husart); /*!< pointer to an USART callback function */ 189 190 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 191 192 /** 193 * @} 194 */ 195 196 /* Exported constants --------------------------------------------------------*/ 197 /** @defgroup USART_Exported_Constants USART Exported Constants 198 * @{ 199 */ 200 201 /** @defgroup USART_Error_Definition USART Error Definition 202 * @{ 203 */ 204 #define HAL_USART_ERROR_NONE (0x00000000U) /*!< No error */ 205 #define HAL_USART_ERROR_PE (0x00000001U) /*!< Parity error */ 206 #define HAL_USART_ERROR_NE (0x00000002U) /*!< Noise error */ 207 #define HAL_USART_ERROR_FE (0x00000004U) /*!< Frame error */ 208 #define HAL_USART_ERROR_ORE (0x00000008U) /*!< Overrun error */ 209 #define HAL_USART_ERROR_DMA (0x00000010U) /*!< DMA transfer error */ 210 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 211 #define HAL_USART_ERROR_INVALID_CALLBACK (0x00000040U) /*!< Invalid Callback error */ 212 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 213 #define HAL_USART_ERROR_RTO (0x00000080U) /*!< Receiver Timeout error */ 214 /** 215 * @} 216 */ 217 218 /** @defgroup USART_Stop_Bits USART Number of Stop Bits 219 * @{ 220 */ 221 #if defined(USART_SMARTCARD_SUPPORT) 222 #define USART_STOPBITS_0_5 USART_CR2_STOP_0 /*!< USART frame with 0.5 stop bit */ 223 #define USART_STOPBITS_1 0x00000000U /*!< USART frame with 1 stop bit */ 224 #define USART_STOPBITS_1_5 (USART_CR2_STOP_0 | USART_CR2_STOP_1) /*!< USART frame with 1.5 stop bits */ 225 #define USART_STOPBITS_2 USART_CR2_STOP_1 /*!< USART frame with 2 stop bits */ 226 #else 227 #define USART_STOPBITS_1 (0x00000000U) /*!< USART frame with 1 stop bit */ 228 #define USART_STOPBITS_2 (USART_CR2_STOP_1) /*!< USART frame with 2 stop bits */ 229 #endif /* USART_SMARTCARD_SUPPORT */ 230 /** 231 * @} 232 */ 233 234 /** @defgroup USART_Parity USART Parity 235 * @{ 236 */ 237 #define USART_PARITY_NONE 0x00000000U /*!< No parity */ 238 #define USART_PARITY_EVEN USART_CR1_PCE /*!< Even parity */ 239 #define USART_PARITY_ODD (USART_CR1_PCE | USART_CR1_PS) /*!< Odd parity */ 240 /** 241 * @} 242 */ 243 244 /** @defgroup USART_Mode USART Mode 245 * @{ 246 */ 247 #define USART_MODE_RX USART_CR1_RE /*!< RX mode */ 248 #define USART_MODE_TX USART_CR1_TE /*!< TX mode */ 249 #define USART_MODE_TX_RX (USART_CR1_TE |USART_CR1_RE) /*!< RX and TX mode */ 250 /** 251 * @} 252 */ 253 254 /** @defgroup USART_Clock USART Clock 255 * @{ 256 */ 257 #define USART_CLOCK_DISABLE 0x00000000U /*!< USART clock disable */ 258 #define USART_CLOCK_ENABLE USART_CR2_CLKEN /*!< USART clock enable */ 259 /** 260 * @} 261 */ 262 263 /** @defgroup USART_Clock_Polarity USART Clock Polarity 264 * @{ 265 */ 266 #define USART_POLARITY_LOW 0x00000000U /*!< Driver enable signal is active high */ 267 #define USART_POLARITY_HIGH USART_CR2_CPOL /*!< Driver enable signal is active low */ 268 /** 269 * @} 270 */ 271 272 /** @defgroup USART_Clock_Phase USART Clock Phase 273 * @{ 274 */ 275 #define USART_PHASE_1EDGE 0x00000000U /*!< USART frame phase on first clock transition */ 276 #define USART_PHASE_2EDGE USART_CR2_CPHA /*!< USART frame phase on second clock transition */ 277 /** 278 * @} 279 */ 280 281 /** @defgroup USART_Last_Bit USART Last Bit 282 * @{ 283 */ 284 #define USART_LASTBIT_DISABLE 0x00000000U /*!< USART frame last data bit clock pulse not output to SCLK pin */ 285 #define USART_LASTBIT_ENABLE USART_CR2_LBCL /*!< USART frame last data bit clock pulse output to SCLK pin */ 286 /** 287 * @} 288 */ 289 290 291 /** @defgroup USART_Request_Parameters USART Request Parameters 292 * @{ 293 */ 294 #define USART_RXDATA_FLUSH_REQUEST USART_RQR_RXFRQ /*!< Receive Data flush Request */ 295 #if defined(USART_RQR_TXFRQ) 296 #define USART_TXDATA_FLUSH_REQUEST USART_RQR_TXFRQ /*!< Transmit data flush Request */ 297 #endif /* USART_RQR_TXFRQ */ 298 /** 299 * @} 300 */ 301 302 /** @defgroup USART_Flags USART Flags 303 * Elements values convention: 0xXXXX 304 * - 0xXXXX : Flag mask in the ISR register 305 * @{ 306 */ 307 #define USART_FLAG_REACK USART_ISR_REACK /*!< USART receive enable acknowledge flag */ 308 #define USART_FLAG_TEACK USART_ISR_TEACK /*!< USART transmit enable acknowledge flag */ 309 #define USART_FLAG_BUSY USART_ISR_BUSY /*!< USART busy flag */ 310 #define USART_FLAG_TXE USART_ISR_TXE /*!< USART transmit data register empty */ 311 #define USART_FLAG_RTOF USART_ISR_RTOF /*!< USART receiver timeout flag */ 312 #define USART_FLAG_TC USART_ISR_TC /*!< USART transmission complete */ 313 #define USART_FLAG_RXNE USART_ISR_RXNE /*!< USART read data register not empty */ 314 #define USART_FLAG_IDLE USART_ISR_IDLE /*!< USART idle flag */ 315 #define USART_FLAG_ORE USART_ISR_ORE /*!< USART overrun error */ 316 #define USART_FLAG_NE USART_ISR_NE /*!< USART noise error */ 317 #define USART_FLAG_FE USART_ISR_FE /*!< USART frame error */ 318 #define USART_FLAG_PE USART_ISR_PE /*!< USART parity error */ 319 /** 320 * @} 321 */ 322 323 /** @defgroup USART_Interrupt_definition USART Interrupts Definition 324 * Elements values convention: 0000ZZZZ0XXYYYYYb 325 * - YYYYY : Interrupt source position in the XX register (5bits) 326 * - XX : Interrupt source register (2bits) 327 * - 01: CR1 register 328 * - 10: CR2 register 329 * - 11: CR3 register 330 * - ZZZZ : Flag position in the ISR register(4bits) 331 * @{ 332 */ 333 334 #define USART_IT_PE 0x0028U /*!< USART parity error interruption */ 335 #define USART_IT_TXE 0x0727U /*!< USART transmit data register empty interruption */ 336 #define USART_IT_TC 0x0626U /*!< USART transmission complete interruption */ 337 #define USART_IT_RXNE 0x0525U /*!< USART read data register not empty interruption */ 338 #define USART_IT_IDLE 0x0424U /*!< USART idle interruption */ 339 #define USART_IT_ERR 0x0060U /*!< USART error interruption */ 340 #define USART_IT_ORE 0x0300U /*!< USART overrun error interruption */ 341 #define USART_IT_NE 0x0200U /*!< USART noise error interruption */ 342 #define USART_IT_FE 0x0100U /*!< USART frame error interruption */ 343 344 /** 345 * @} 346 */ 347 348 /** @defgroup USART_IT_CLEAR_Flags USART Interruption Clear Flags 349 * @{ 350 */ 351 #define USART_CLEAR_PEF USART_ICR_PECF /*!< Parity Error Clear Flag */ 352 #define USART_CLEAR_FEF USART_ICR_FECF /*!< Framing Error Clear Flag */ 353 #define USART_CLEAR_NEF USART_ICR_NCF /*!< Noise Error detected Clear Flag */ 354 #define USART_CLEAR_OREF USART_ICR_ORECF /*!< OverRun Error Clear Flag */ 355 #define USART_CLEAR_IDLEF USART_ICR_IDLECF /*!< IDLE line detected Clear Flag */ 356 #define USART_CLEAR_TCF USART_ICR_TCCF /*!< Transmission Complete Clear Flag */ 357 #define USART_CLEAR_CTSF USART_ICR_CTSCF /*!< CTS Interrupt Clear Flag */ 358 #define USART_CLEAR_RTOF USART_ICR_RTOCF /*!< USART receiver timeout clear flag */ 359 /** 360 * @} 361 */ 362 363 /** @defgroup USART_Interruption_Mask USART Interruption Flags Mask 364 * @{ 365 */ 366 #define USART_IT_MASK 0x001FU /*!< USART interruptions flags mask */ 367 #define USART_CR_MASK 0x00E0U /*!< USART control register mask */ 368 #define USART_CR_POS 5U /*!< USART control register position */ 369 #define USART_ISR_MASK 0x1F00U /*!< USART ISR register mask */ 370 #define USART_ISR_POS 8U /*!< USART ISR register position */ 371 /** 372 * @} 373 */ 374 375 /** 376 * @} 377 */ 378 379 /* Exported macros -----------------------------------------------------------*/ 380 /** @defgroup USART_Exported_Macros USART Exported Macros 381 * @{ 382 */ 383 384 /** @brief Reset USART handle state. 385 * @param __HANDLE__ USART handle. 386 * @retval None 387 */ 388 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 389 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) do{ \ 390 (__HANDLE__)->State = HAL_USART_STATE_RESET; \ 391 (__HANDLE__)->MspInitCallback = NULL; \ 392 (__HANDLE__)->MspDeInitCallback = NULL; \ 393 } while(0U) 394 #else 395 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET) 396 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 397 398 /** @brief Check whether the specified USART flag is set or not. 399 * @param __HANDLE__ specifies the USART Handle 400 * @param __FLAG__ specifies the flag to check. 401 * This parameter can be one of the following values: 402 * @arg @ref USART_FLAG_REACK Receive enable acknowledge flag 403 * @arg @ref USART_FLAG_TEACK Transmit enable acknowledge flag 404 * @arg @ref USART_FLAG_BUSY Busy flag 405 * @arg @ref USART_FLAG_CTS CTS Change flag 406 * @arg @ref USART_FLAG_TXE Transmit data register empty flag 407 * @arg @ref USART_FLAG_TC Transmission Complete flag 408 * @arg @ref USART_FLAG_RXNE Receive data register not empty flag 409 * @arg @ref USART_FLAG_RTOF Receiver Timeout flag 410 * @arg @ref USART_FLAG_IDLE Idle Line detection flag 411 * @arg @ref USART_FLAG_ORE OverRun Error flag 412 * @arg @ref USART_FLAG_NE Noise Error flag 413 * @arg @ref USART_FLAG_FE Framing Error flag 414 * @arg @ref USART_FLAG_PE Parity Error flag 415 * @retval The new state of __FLAG__ (TRUE or FALSE). 416 */ 417 #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->ISR & (__FLAG__)) == (__FLAG__)) 418 419 /** @brief Clear the specified USART pending flag. 420 * @param __HANDLE__ specifies the USART Handle. 421 * @param __FLAG__ specifies the flag to check. 422 * This parameter can be any combination of the following values: 423 * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag 424 * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag 425 * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag 426 * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag 427 * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag 428 * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag 429 * @arg @ref USART_CLEAR_RTOF Receiver Timeout clear flag 430 * @arg @ref USART_CLEAR_CTSF 431 * @retval None 432 */ 433 #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->ICR = (__FLAG__)) 434 435 /** @brief Clear the USART PE pending flag. 436 * @param __HANDLE__ specifies the USART Handle. 437 * @retval None 438 */ 439 #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_PEF) 440 441 /** @brief Clear the USART FE pending flag. 442 * @param __HANDLE__ specifies the USART Handle. 443 * @retval None 444 */ 445 #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_FEF) 446 447 /** @brief Clear the USART NE pending flag. 448 * @param __HANDLE__ specifies the USART Handle. 449 * @retval None 450 */ 451 #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_NEF) 452 453 /** @brief Clear the USART ORE pending flag. 454 * @param __HANDLE__ specifies the USART Handle. 455 * @retval None 456 */ 457 #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_OREF) 458 459 /** @brief Clear the USART IDLE pending flag. 460 * @param __HANDLE__ specifies the USART Handle. 461 * @retval None 462 */ 463 #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_FLAG((__HANDLE__), USART_CLEAR_IDLEF) 464 465 466 467 /** @brief Enable the specified USART interrupt. 468 * @param __HANDLE__ specifies the USART Handle. 469 * @param __INTERRUPT__ specifies the USART interrupt source to enable. 470 * This parameter can be one of the following values: 471 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt 472 * @arg @ref USART_IT_TC Transmission complete interrupt 473 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt 474 * @arg @ref USART_IT_IDLE Idle line detection interrupt 475 * @arg @ref USART_IT_PE Parity Error interrupt 476 * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error) 477 * @retval None 478 */ 479 #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__)\ 480 (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\ 481 ((__HANDLE__)->Instance->CR1 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \ 482 ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\ 483 ((__HANDLE__)->Instance->CR2 |= (1U << ((__INTERRUPT__) & USART_IT_MASK))): \ 484 ((__HANDLE__)->Instance->CR3 |= (1U << ((__INTERRUPT__) & USART_IT_MASK)))) 485 486 /** @brief Disable the specified USART interrupt. 487 * @param __HANDLE__ specifies the USART Handle. 488 * @param __INTERRUPT__ specifies the USART interrupt source to disable. 489 * This parameter can be one of the following values: 490 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt 491 * @arg @ref USART_IT_TC Transmission complete interrupt 492 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt 493 * @arg @ref USART_IT_IDLE Idle line detection interrupt 494 * @arg @ref USART_IT_PE Parity Error interrupt 495 * @arg @ref USART_IT_ERR Error interrupt(Frame error, noise error, overrun error) 496 * @retval None 497 */ 498 #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__)\ 499 (((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 1U)?\ 500 ((__HANDLE__)->Instance->CR1 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \ 501 ((((__INTERRUPT__) & USART_CR_MASK) >> USART_CR_POS) == 2U)?\ 502 ((__HANDLE__)->Instance->CR2 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK))): \ 503 ((__HANDLE__)->Instance->CR3 &= ~ (1U << ((__INTERRUPT__) & USART_IT_MASK)))) 504 505 /** @brief Check whether the specified USART interrupt has occurred or not. 506 * @param __HANDLE__ specifies the USART Handle. 507 * @param __INTERRUPT__ specifies the USART interrupt source to check. 508 * This parameter can be one of the following values: 509 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt 510 * @arg @ref USART_IT_TC Transmission complete interrupt 511 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt 512 * @arg @ref USART_IT_IDLE Idle line detection interrupt 513 * @arg @ref USART_IT_ORE OverRun Error interrupt 514 * @arg @ref USART_IT_NE Noise Error interrupt 515 * @arg @ref USART_IT_FE Framing Error interrupt 516 * @arg @ref USART_IT_PE Parity Error interrupt 517 * @retval The new state of __INTERRUPT__ (SET or RESET). 518 */ 519 #define __HAL_USART_GET_IT(__HANDLE__, __INTERRUPT__) ((((__HANDLE__)->Instance->ISR\ 520 & (0x01U << (((__INTERRUPT__) & USART_ISR_MASK)>>\ 521 USART_ISR_POS))) != 0U) ? SET : RESET) 522 523 /** @brief Check whether the specified USART interrupt source is enabled or not. 524 * @param __HANDLE__ specifies the USART Handle. 525 * @param __INTERRUPT__ specifies the USART interrupt source to check. 526 * This parameter can be one of the following values: 527 * @arg @ref USART_IT_TXE Transmit Data Register empty interrupt 528 * @arg @ref USART_IT_TC Transmission complete interrupt 529 * @arg @ref USART_IT_RXNE Receive Data register not empty interrupt 530 * @arg @ref USART_IT_IDLE Idle line detection interrupt 531 * @arg @ref USART_IT_ORE OverRun Error interrupt 532 * @arg @ref USART_IT_NE Noise Error interrupt 533 * @arg @ref USART_IT_FE Framing Error interrupt 534 * @arg @ref USART_IT_PE Parity Error interrupt 535 * @retval The new state of __INTERRUPT__ (SET or RESET). 536 */ 537 #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) ((((((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x01U) ?\ 538 (__HANDLE__)->Instance->CR1 : \ 539 (((((uint8_t)(__INTERRUPT__)) >> 0x05U) == 0x02U) ?\ 540 (__HANDLE__)->Instance->CR2 : \ 541 (__HANDLE__)->Instance->CR3)) & (0x01U <<\ 542 (((uint16_t)(__INTERRUPT__)) &\ 543 USART_IT_MASK))) != 0U) ? SET : RESET) 544 545 /** @brief Clear the specified USART ISR flag, in setting the proper ICR register flag. 546 * @param __HANDLE__ specifies the USART Handle. 547 * @param __IT_CLEAR__ specifies the interrupt clear register flag that needs to be set 548 * to clear the corresponding interrupt. 549 * This parameter can be one of the following values: 550 * @arg @ref USART_CLEAR_PEF Parity Error Clear Flag 551 * @arg @ref USART_CLEAR_FEF Framing Error Clear Flag 552 * @arg @ref USART_CLEAR_NEF Noise detected Clear Flag 553 * @arg @ref USART_CLEAR_OREF Overrun Error Clear Flag 554 * @arg @ref USART_CLEAR_IDLEF IDLE line detected Clear Flag 555 * @arg @ref USART_CLEAR_RTOF Receiver timeout clear flag 556 * @arg @ref USART_CLEAR_TCF Transmission Complete Clear Flag 557 * @retval None 558 */ 559 #define __HAL_USART_CLEAR_IT(__HANDLE__, __IT_CLEAR__) ((__HANDLE__)->Instance->ICR = (uint32_t)(__IT_CLEAR__)) 560 561 /** @brief Set a specific USART request flag. 562 * @param __HANDLE__ specifies the USART Handle. 563 * @param __REQ__ specifies the request flag to set. 564 * This parameter can be one of the following values: 565 * @arg @ref USART_RXDATA_FLUSH_REQUEST Receive Data flush Request 566 #if defined(USART_RQR_TXFRQ) 567 * @arg @ref USART_TXDATA_FLUSH_REQUEST Transmit data flush Request 568 #endif 569 * 570 * @retval None 571 */ 572 #define __HAL_USART_SEND_REQ(__HANDLE__, __REQ__) ((__HANDLE__)->Instance->RQR |= (uint16_t)(__REQ__)) 573 574 /** @brief Enable the USART one bit sample method. 575 * @param __HANDLE__ specifies the USART Handle. 576 * @retval None 577 */ 578 #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3|= USART_CR3_ONEBIT) 579 580 /** @brief Disable the USART one bit sample method. 581 * @param __HANDLE__ specifies the USART Handle. 582 * @retval None 583 */ 584 #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR3 &= ~USART_CR3_ONEBIT) 585 586 /** @brief Enable USART. 587 * @param __HANDLE__ specifies the USART Handle. 588 * @retval None 589 */ 590 #define __HAL_USART_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 |= USART_CR1_UE) 591 592 /** @brief Disable USART. 593 * @param __HANDLE__ specifies the USART Handle. 594 * @retval None 595 */ 596 #define __HAL_USART_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR1 &= ~USART_CR1_UE) 597 598 /** 599 * @} 600 */ 601 602 /* Private macros --------------------------------------------------------*/ 603 /** @defgroup USART_Private_Macros USART Private Macros 604 * @{ 605 */ 606 607 /** @brief BRR division operation to set BRR register in 8-bit oversampling mode. 608 * @param __PCLK__ USART clock. 609 * @param __BAUD__ Baud rate set by the user. 610 * @retval Division result 611 */ 612 #define USART_DIV_SAMPLING8(__PCLK__, __BAUD__) ((((__PCLK__)*2U) + ((__BAUD__)/2U)) / (__BAUD__)) 613 614 /** @brief Check USART Baud rate. 615 * @param __BAUDRATE__ Baudrate specified by the user. 616 * The maximum Baud Rate is derived from the maximum clock on F0 (i.e. 48 MHz) 617 * divided by the smallest oversampling used on the USART (i.e. 8) 618 * @retval SET (__BAUDRATE__ is valid) or RESET (__BAUDRATE__ is invalid) */ 619 #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) <= 6000000U) 620 621 /** 622 * @brief Ensure that USART frame number of stop bits is valid. 623 * @param __STOPBITS__ USART frame number of stop bits. 624 * @retval SET (__STOPBITS__ is valid) or RESET (__STOPBITS__ is invalid) 625 */ 626 #if defined(USART_SMARTCARD_SUPPORT) 627 #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_0_5) || \ 628 ((__STOPBITS__) == USART_STOPBITS_1) || \ 629 ((__STOPBITS__) == USART_STOPBITS_1_5) || \ 630 ((__STOPBITS__) == USART_STOPBITS_2)) 631 #else 632 #define IS_USART_STOPBITS(__STOPBITS__) (((__STOPBITS__) == USART_STOPBITS_1) || \ 633 ((__STOPBITS__) == USART_STOPBITS_2)) 634 #endif /* USART_SMARTCARD_SUPPORT */ 635 636 /** 637 * @brief Ensure that USART frame parity is valid. 638 * @param __PARITY__ USART frame parity. 639 * @retval SET (__PARITY__ is valid) or RESET (__PARITY__ is invalid) 640 */ 641 #define IS_USART_PARITY(__PARITY__) (((__PARITY__) == USART_PARITY_NONE) || \ 642 ((__PARITY__) == USART_PARITY_EVEN) || \ 643 ((__PARITY__) == USART_PARITY_ODD)) 644 645 /** 646 * @brief Ensure that USART communication mode is valid. 647 * @param __MODE__ USART communication mode. 648 * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid) 649 */ 650 #define IS_USART_MODE(__MODE__) ((((__MODE__) & 0xFFFFFFF3U) == 0x00U) && ((__MODE__) != 0x00U)) 651 652 /** 653 * @brief Ensure that USART clock state is valid. 654 * @param __CLOCK__ USART clock state. 655 * @retval SET (__CLOCK__ is valid) or RESET (__CLOCK__ is invalid) 656 */ 657 #define IS_USART_CLOCK(__CLOCK__) (((__CLOCK__) == USART_CLOCK_DISABLE) || \ 658 ((__CLOCK__) == USART_CLOCK_ENABLE)) 659 660 /** 661 * @brief Ensure that USART frame polarity is valid. 662 * @param __CPOL__ USART frame polarity. 663 * @retval SET (__CPOL__ is valid) or RESET (__CPOL__ is invalid) 664 */ 665 #define IS_USART_POLARITY(__CPOL__) (((__CPOL__) == USART_POLARITY_LOW) || ((__CPOL__) == USART_POLARITY_HIGH)) 666 667 /** 668 * @brief Ensure that USART frame phase is valid. 669 * @param __CPHA__ USART frame phase. 670 * @retval SET (__CPHA__ is valid) or RESET (__CPHA__ is invalid) 671 */ 672 #define IS_USART_PHASE(__CPHA__) (((__CPHA__) == USART_PHASE_1EDGE) || ((__CPHA__) == USART_PHASE_2EDGE)) 673 674 /** 675 * @brief Ensure that USART frame last bit clock pulse setting is valid. 676 * @param __LASTBIT__ USART frame last bit clock pulse setting. 677 * @retval SET (__LASTBIT__ is valid) or RESET (__LASTBIT__ is invalid) 678 */ 679 #define IS_USART_LASTBIT(__LASTBIT__) (((__LASTBIT__) == USART_LASTBIT_DISABLE) || \ 680 ((__LASTBIT__) == USART_LASTBIT_ENABLE)) 681 682 /** 683 * @brief Ensure that USART request parameter is valid. 684 * @param __PARAM__ USART request parameter. 685 * @retval SET (__PARAM__ is valid) or RESET (__PARAM__ is invalid) 686 */ 687 #if defined(USART_RQR_TXFRQ) 688 #define IS_USART_REQUEST_PARAMETER(__PARAM__) (((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) || \ 689 ((__PARAM__) == USART_TXDATA_FLUSH_REQUEST)) 690 #else 691 #define IS_USART_REQUEST_PARAMETER(__PARAM__) ((__PARAM__) == USART_RXDATA_FLUSH_REQUEST) 692 #endif /* USART_RQR_TXFRQ */ 693 694 /** 695 * @} 696 */ 697 698 /* Include USART HAL Extended module */ 699 #include "stm32f0xx_hal_usart_ex.h" 700 701 /* Exported functions --------------------------------------------------------*/ 702 /** @addtogroup USART_Exported_Functions USART Exported Functions 703 * @{ 704 */ 705 706 /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions 707 * @{ 708 */ 709 710 /* Initialization and de-initialization functions ****************************/ 711 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart); 712 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart); 713 void HAL_USART_MspInit(USART_HandleTypeDef *husart); 714 void HAL_USART_MspDeInit(USART_HandleTypeDef *husart); 715 716 /* Callbacks Register/UnRegister functions ***********************************/ 717 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1) 718 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, 719 pUSART_CallbackTypeDef pCallback); 720 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID); 721 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */ 722 723 /** 724 * @} 725 */ 726 727 /** @addtogroup USART_Exported_Functions_Group2 IO operation functions 728 * @{ 729 */ 730 731 /* IO operation functions *****************************************************/ 732 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size, 733 uint32_t Timeout); 734 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout); 735 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 736 uint16_t Size, uint32_t Timeout); 737 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size); 738 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size); 739 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 740 uint16_t Size); 741 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size); 742 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size); 743 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData, 744 uint16_t Size); 745 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart); 746 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart); 747 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart); 748 /* Transfer Abort functions */ 749 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart); 750 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart); 751 752 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart); 753 void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart); 754 void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart); 755 void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart); 756 void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart); 757 void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart); 758 void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart); 759 void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart); 760 761 /** 762 * @} 763 */ 764 765 /** @addtogroup USART_Exported_Functions_Group4 Peripheral State and Error functions 766 * @{ 767 */ 768 769 /* Peripheral State and Error functions ***************************************/ 770 HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart); 771 uint32_t HAL_USART_GetError(const USART_HandleTypeDef *husart); 772 773 /** 774 * @} 775 */ 776 777 /** 778 * @} 779 */ 780 781 /** 782 * @} 783 */ 784 785 /** 786 * @} 787 */ 788 789 #ifdef __cplusplus 790 } 791 #endif 792 793 #endif /* STM32F0xx_HAL_USART_H */ 794 795