1 /** 2 ****************************************************************************** 3 * @file stm32l1xx_hal_usart.h 4 * @author MCD Application Team 5 * @version V1.2.0 6 * @date 01-July-2016 7 * @brief This file contains all the functions prototypes for the USART 8 * firmware library. 9 ****************************************************************************** 10 * @attention 11 * 12 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 13 * 14 * Redistribution and use in source and binary forms, with or without modification, 15 * are permitted provided that the following conditions are met: 16 * 1. Redistributions of source code must retain the above copyright notice, 17 * this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright notice, 19 * this list of conditions and the following disclaimer in the documentation 20 * and/or other materials provided with the distribution. 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 ****************************************************************************** 37 */ 38 39 /* Define to prevent recursive inclusion -------------------------------------*/ 40 #ifndef __STM32L1xx_HAL_USART_H 41 #define __STM32L1xx_HAL_USART_H 42 43 #ifdef __cplusplus 44 extern "C" { 45 #endif 46 47 /* Includes ------------------------------------------------------------------*/ 48 #include "stm32l1xx_hal_def.h" 49 50 /** @addtogroup STM32L1xx_HAL_Driver 51 * @{ 52 */ 53 54 /** @addtogroup USART 55 * @{ 56 */ 57 58 /* Exported types ------------------------------------------------------------*/ 59 /** @defgroup USART_Exported_Types USART Exported Types 60 * @{ 61 */ 62 63 64 /** 65 * @brief USART Init Structure definition 66 */ 67 typedef struct 68 { 69 uint32_t BaudRate; /*!< This member configures the Usart communication baud rate. 70 The baud rate is computed using the following formula: 71 - IntegerDivider = ((PCLKx) / (8 * (husart->Init.BaudRate))) 72 - FractionalDivider = ((IntegerDivider - ((uint32_t) IntegerDivider)) * 8) + 0.5 */ 73 74 uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame. 75 This parameter can be a value of @ref USART_Word_Length */ 76 77 uint32_t StopBits; /*!< Specifies the number of stop bits transmitted. 78 This parameter can be a value of @ref USART_Stop_Bits */ 79 80 uint32_t Parity; /*!< Specifies the parity mode. 81 This parameter can be a value of @ref USART_Parity 82 @note When parity is enabled, the computed parity is inserted 83 at the MSB position of the transmitted data (9th bit when 84 the word length is set to 9 data bits; 8th bit when the 85 word length is set to 8 data bits). */ 86 87 uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled. 88 This parameter can be a value of @ref USART_Mode */ 89 90 uint32_t CLKPolarity; /*!< Specifies the steady state of the serial clock. 91 This parameter can be a value of @ref USART_Clock_Polarity */ 92 93 uint32_t CLKPhase; /*!< Specifies the clock transition on which the bit capture is made. 94 This parameter can be a value of @ref USART_Clock_Phase */ 95 96 uint32_t CLKLastBit; /*!< Specifies whether the clock pulse corresponding to the last transmitted 97 data bit (MSB) has to be output on the SCLK pin in synchronous mode. 98 This parameter can be a value of @ref USART_Last_Bit */ 99 }USART_InitTypeDef; 100 101 /** 102 * @brief HAL State structures definition 103 */ 104 typedef enum 105 { 106 HAL_USART_STATE_RESET = 0x00, /*!< Peripheral is not initialized */ 107 HAL_USART_STATE_READY = 0x01, /*!< Peripheral Initialized and ready for use */ 108 HAL_USART_STATE_BUSY = 0x02, /*!< an internal process is ongoing */ 109 HAL_USART_STATE_BUSY_TX = 0x12, /*!< Data Transmission process is ongoing */ 110 HAL_USART_STATE_BUSY_RX = 0x22, /*!< Data Reception process is ongoing */ 111 HAL_USART_STATE_BUSY_TX_RX = 0x32, /*!< Data Transmission Reception process is ongoing */ 112 HAL_USART_STATE_TIMEOUT = 0x03, /*!< Timeout state */ 113 HAL_USART_STATE_ERROR = 0x04 /*!< Error */ 114 }HAL_USART_StateTypeDef; 115 116 117 /** 118 * @brief USART handle Structure definition 119 */ 120 typedef struct 121 { 122 USART_TypeDef *Instance; /*!< USART registers base address */ 123 124 USART_InitTypeDef Init; /*!< Usart communication parameters */ 125 126 uint8_t *pTxBuffPtr; /*!< Pointer to Usart Tx transfer Buffer */ 127 128 uint16_t TxXferSize; /*!< Usart Tx Transfer size */ 129 130 __IO uint16_t TxXferCount; /*!< Usart Tx Transfer Counter */ 131 132 uint8_t *pRxBuffPtr; /*!< Pointer to Usart Rx transfer Buffer */ 133 134 uint16_t RxXferSize; /*!< Usart Rx Transfer size */ 135 136 __IO uint16_t RxXferCount; /*!< Usart Rx Transfer Counter */ 137 138 DMA_HandleTypeDef *hdmatx; /*!< Usart Tx DMA Handle parameters */ 139 140 DMA_HandleTypeDef *hdmarx; /*!< Usart Rx DMA Handle parameters */ 141 142 HAL_LockTypeDef Lock; /*!< Locking object */ 143 144 __IO HAL_USART_StateTypeDef State; /*!< Usart communication state */ 145 146 __IO uint32_t ErrorCode; /*!< USART Error code */ 147 148 }USART_HandleTypeDef; 149 150 /** 151 * @} 152 */ 153 154 /* Exported constants --------------------------------------------------------*/ 155 /** @defgroup USART_Exported_Constants USART Exported constants 156 * @{ 157 */ 158 159 /** @defgroup USART_Error_Codes USART Error Codes 160 * @{ 161 */ 162 #define HAL_USART_ERROR_NONE ((uint32_t)0x00) /*!< No error */ 163 #define HAL_USART_ERROR_PE ((uint32_t)0x01) /*!< Parity error */ 164 #define HAL_USART_ERROR_NE ((uint32_t)0x02) /*!< Noise error */ 165 #define HAL_USART_ERROR_FE ((uint32_t)0x04) /*!< frame error */ 166 #define HAL_USART_ERROR_ORE ((uint32_t)0x08) /*!< Overrun error */ 167 #define HAL_USART_ERROR_DMA ((uint32_t)0x10) /*!< DMA transfer error */ 168 /** 169 * @} 170 */ 171 172 /** @defgroup USART_Word_Length USART Word Length 173 * @{ 174 */ 175 #define USART_WORDLENGTH_8B ((uint32_t)0x00000000) 176 #define USART_WORDLENGTH_9B ((uint32_t)USART_CR1_M) 177 /** 178 * @} 179 */ 180 181 /** @defgroup USART_Stop_Bits USART Number of Stop Bits 182 * @{ 183 */ 184 #define USART_STOPBITS_1 ((uint32_t)0x00000000) 185 #define USART_STOPBITS_0_5 ((uint32_t)USART_CR2_STOP_0) 186 #define USART_STOPBITS_2 ((uint32_t)USART_CR2_STOP_1) 187 #define USART_STOPBITS_1_5 ((uint32_t)(USART_CR2_STOP_0 | USART_CR2_STOP_1)) 188 /** 189 * @} 190 */ 191 192 /** @defgroup USART_Parity USART Parity 193 * @{ 194 */ 195 #define USART_PARITY_NONE ((uint32_t)0x00000000) 196 #define USART_PARITY_EVEN ((uint32_t)USART_CR1_PCE) 197 #define USART_PARITY_ODD ((uint32_t)(USART_CR1_PCE | USART_CR1_PS)) 198 /** 199 * @} 200 */ 201 202 /** @defgroup USART_Mode USART Mode 203 * @{ 204 */ 205 #define USART_MODE_RX ((uint32_t)USART_CR1_RE) 206 #define USART_MODE_TX ((uint32_t)USART_CR1_TE) 207 #define USART_MODE_TX_RX ((uint32_t)(USART_CR1_TE |USART_CR1_RE)) 208 209 /** 210 * @} 211 */ 212 213 /** @defgroup USART_Clock USART Clock 214 * @{ 215 */ 216 #define USART_CLOCK_DISABLE ((uint32_t)0x00000000) 217 #define USART_CLOCK_ENABLE ((uint32_t)USART_CR2_CLKEN) 218 /** 219 * @} 220 */ 221 222 /** @defgroup USART_Clock_Polarity USART Clock Polarity 223 * @{ 224 */ 225 #define USART_POLARITY_LOW ((uint32_t)0x00000000) 226 #define USART_POLARITY_HIGH ((uint32_t)USART_CR2_CPOL) 227 /** 228 * @} 229 */ 230 231 /** @defgroup USART_Clock_Phase USART Clock Phase 232 * @{ 233 */ 234 #define USART_PHASE_1EDGE ((uint32_t)0x00000000) 235 #define USART_PHASE_2EDGE ((uint32_t)USART_CR2_CPHA) 236 /** 237 * @} 238 */ 239 240 /** @defgroup USART_Last_Bit USART Last Bit 241 * @{ 242 */ 243 #define USART_LASTBIT_DISABLE ((uint32_t)0x00000000) 244 #define USART_LASTBIT_ENABLE ((uint32_t)USART_CR2_LBCL) 245 /** 246 * @} 247 */ 248 249 /** @defgroup USART_NACK_State USART NACK State 250 * @{ 251 */ 252 #define USART_NACK_ENABLE ((uint32_t)USART_CR3_NACK) 253 #define USART_NACK_DISABLE ((uint32_t)0x00000000) 254 /** 255 * @} 256 */ 257 258 /** @defgroup USART_Flags USART Flags 259 * Elements values convention: 0xXXXX 260 * - 0xXXXX : Flag mask in the SR register 261 * @{ 262 */ 263 264 #define USART_FLAG_CTS ((uint32_t)USART_SR_CTS) 265 #define USART_FLAG_LBD ((uint32_t)USART_SR_LBD) 266 #define USART_FLAG_TXE ((uint32_t)USART_SR_TXE) 267 #define USART_FLAG_TC ((uint32_t)USART_SR_TC) 268 #define USART_FLAG_RXNE ((uint32_t)USART_SR_RXNE) 269 #define USART_FLAG_IDLE ((uint32_t)USART_SR_IDLE) 270 #define USART_FLAG_ORE ((uint32_t)USART_SR_ORE) 271 #define USART_FLAG_NE ((uint32_t)USART_SR_NE) 272 #define USART_FLAG_FE ((uint32_t)USART_SR_FE) 273 #define USART_FLAG_PE ((uint32_t)USART_SR_PE) 274 /** 275 * @} 276 */ 277 278 /** @defgroup USART_Interrupt_definition USART Interrupts Definition 279 * Elements values convention: 0xY000XXXX 280 * - XXXX : Interrupt mask (16 bits) in the Y register 281 * - Y : Interrupt source register (4bits) 282 * - 0001: CR1 register 283 * - 0010: CR2 register 284 * - 0011: CR3 register 285 * 286 * @{ 287 */ 288 289 #define USART_IT_PE ((uint32_t)(USART_CR1_REG_INDEX << 28 | USART_CR1_PEIE)) 290 #define USART_IT_TXE ((uint32_t)(USART_CR1_REG_INDEX << 28 | USART_CR1_TXEIE)) 291 #define USART_IT_TC ((uint32_t)(USART_CR1_REG_INDEX << 28 | USART_CR1_TCIE)) 292 #define USART_IT_RXNE ((uint32_t)(USART_CR1_REG_INDEX << 28 | USART_CR1_RXNEIE)) 293 #define USART_IT_IDLE ((uint32_t)(USART_CR1_REG_INDEX << 28 | USART_CR1_IDLEIE)) 294 295 #define USART_IT_LBD ((uint32_t)(USART_CR2_REG_INDEX << 28 | USART_CR2_LBDIE)) 296 297 #define USART_IT_CTS ((uint32_t)(USART_CR3_REG_INDEX << 28 | USART_CR3_CTSIE)) 298 #define USART_IT_ERR ((uint32_t)(USART_CR3_REG_INDEX << 28 | USART_CR3_EIE)) 299 300 301 /** 302 * @} 303 */ 304 305 /** 306 * @} 307 */ 308 309 310 /* Exported macro ------------------------------------------------------------*/ 311 /** @defgroup USART_Exported_Macros USART Exported Macros 312 * @{ 313 */ 314 315 316 /** @brief Reset USART handle state 317 * @param __HANDLE__: specifies the USART Handle. 318 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 319 * @retval None 320 */ 321 #define __HAL_USART_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_USART_STATE_RESET) 322 323 /** @brief Check whether the specified USART flag is set or not. 324 * @param __HANDLE__: specifies the USART Handle. 325 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 326 * @param __FLAG__: specifies the flag to check. 327 * This parameter can be one of the following values: 328 * @arg USART_FLAG_TXE: Transmit data register empty flag 329 * @arg USART_FLAG_TC: Transmission Complete flag 330 * @arg USART_FLAG_RXNE: Receive data register not empty flag 331 * @arg USART_FLAG_IDLE: Idle Line detection flag 332 * @arg USART_FLAG_ORE: OverRun Error flag 333 * @arg USART_FLAG_NE: Noise Error flag 334 * @arg USART_FLAG_FE: Framing Error flag 335 * @arg USART_FLAG_PE: Parity Error flag 336 * @retval The new state of __FLAG__ (TRUE or FALSE). 337 */ 338 339 #define __HAL_USART_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__)) 340 341 /** @brief Clear the specified USART pending flags. 342 * @param __HANDLE__: specifies the USART Handle. 343 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 344 * @param __FLAG__: specifies the flag to check. 345 * This parameter can be any combination of the following values: 346 * @arg USART_FLAG_TC: Transmission Complete flag. 347 * @arg USART_FLAG_RXNE: Receive data register not empty flag. 348 * 349 * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun 350 * error) and IDLE (Idle line detected) flags are cleared by software 351 * sequence: a read operation to USART_SR register followed by a read 352 * operation to USART_DR register. 353 * @note RXNE flag can be also cleared by a read to the USART_DR register. 354 * @note TC flag can be also cleared by software sequence: a read operation to 355 * USART_SR register followed by a write operation to USART_DR register. 356 * @note TXE flag is cleared only by a write to the USART_DR register. 357 * 358 * @retval None 359 */ 360 #define __HAL_USART_CLEAR_FLAG(__HANDLE__, __FLAG__) ((__HANDLE__)->Instance->SR = ~(__FLAG__)) 361 362 /** @brief Clear the USART PE pending flag. 363 * @param __HANDLE__: specifies the USART Handle. 364 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 365 * @retval None 366 */ 367 #define __HAL_USART_CLEAR_PEFLAG(__HANDLE__) \ 368 do{ \ 369 __IO uint32_t tmpreg; \ 370 tmpreg = (__HANDLE__)->Instance->SR; \ 371 tmpreg = (__HANDLE__)->Instance->DR; \ 372 UNUSED(tmpreg); \ 373 }while(0) 374 375 376 /** @brief Clear the USART FE pending flag. 377 * @param __HANDLE__: specifies the USART Handle. 378 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 379 * @retval None 380 */ 381 #define __HAL_USART_CLEAR_FEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__) 382 383 /** @brief Clear the USART NE pending flag. 384 * @param __HANDLE__: specifies the USART Handle. 385 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 386 * @retval None 387 */ 388 #define __HAL_USART_CLEAR_NEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__) 389 390 /** @brief Clear the USART ORE pending flag. 391 * @param __HANDLE__: specifies the USART Handle. 392 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 393 * @retval None 394 */ 395 #define __HAL_USART_CLEAR_OREFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__) 396 397 /** @brief Clear the USART IDLE pending flag. 398 * @param __HANDLE__: specifies the USART Handle. 399 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 400 * @retval None 401 */ 402 #define __HAL_USART_CLEAR_IDLEFLAG(__HANDLE__) __HAL_USART_CLEAR_PEFLAG(__HANDLE__) 403 404 /** @brief Enable the specified Usart interrupts. 405 * @param __HANDLE__: specifies the USART Handle. 406 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 407 * @param __INTERRUPT__: specifies the USART interrupt source to enable. 408 * This parameter can be one of the following values: 409 * @arg USART_IT_TXE: Transmit Data Register empty interrupt 410 * @arg USART_IT_TC: Transmission complete interrupt 411 * @arg USART_IT_RXNE: Receive Data register not empty interrupt 412 * @arg USART_IT_IDLE: Idle line detection interrupt 413 * @arg USART_IT_PE: Parity Error interrupt 414 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error) 415 * @retval None 416 */ 417 #define __HAL_USART_ENABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == USART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 |= ((__INTERRUPT__) & USART_IT_MASK)): \ 418 (((__INTERRUPT__) >> 28) == USART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 |= ((__INTERRUPT__) & USART_IT_MASK)): \ 419 ((__HANDLE__)->Instance->CR3 |= ((__INTERRUPT__) & USART_IT_MASK))) 420 421 422 /** @brief Disable the specified Usart interrupts. 423 * @param __HANDLE__: specifies the USART Handle. 424 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 425 * @param __INTERRUPT__: specifies the USART interrupt source to disable. 426 * This parameter can be one of the following values: 427 * @arg USART_IT_TXE: Transmit Data Register empty interrupt 428 * @arg USART_IT_TC: Transmission complete interrupt 429 * @arg USART_IT_RXNE: Receive Data register not empty interrupt 430 * @arg USART_IT_IDLE: Idle line detection interrupt 431 * @arg USART_IT_PE: Parity Error interrupt 432 * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error) 433 * @retval None 434 */ 435 #define __HAL_USART_DISABLE_IT(__HANDLE__, __INTERRUPT__) ((((__INTERRUPT__) >> 28) == USART_CR1_REG_INDEX)? ((__HANDLE__)->Instance->CR1 &= ~((__INTERRUPT__) & USART_IT_MASK)): \ 436 (((__INTERRUPT__) >> 28) == USART_CR2_REG_INDEX)? ((__HANDLE__)->Instance->CR2 &= ~((__INTERRUPT__) & USART_IT_MASK)): \ 437 ((__HANDLE__)->Instance->CR3 &= ~ ((__INTERRUPT__) & USART_IT_MASK))) 438 439 440 441 /** @brief Check whether the specified Usart interrupt has occurred or not. 442 * @param __HANDLE__: specifies the USART Handle. 443 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 444 * @param __IT__: specifies the USART interrupt source to check. 445 * This parameter can be one of the following values: 446 * @arg USART_IT_TXE: Transmit Data Register empty interrupt 447 * @arg USART_IT_TC: Transmission complete interrupt 448 * @arg USART_IT_RXNE: Receive Data register not empty interrupt 449 * @arg USART_IT_IDLE: Idle line detection interrupt 450 * @arg USART_IT_ERR: Error interrupt 451 * @arg USART_IT_PE: Parity Error interrupt 452 * @retval The new state of __IT__ (TRUE or FALSE). 453 */ 454 #define __HAL_USART_GET_IT_SOURCE(__HANDLE__, __IT__) (((((__IT__) >> 28) == USART_CR1_REG_INDEX)? (__HANDLE__)->Instance->CR1:(((((uint32_t)(__IT__)) >> 28) == USART_CR2_REG_INDEX)? \ 455 (__HANDLE__)->Instance->CR2 : (__HANDLE__)->Instance->CR3)) & (((uint32_t)(__IT__)) & USART_IT_MASK)) 456 457 /** @brief Enables the USART one bit sample method 458 * @param __HANDLE__: specifies the USART Handle. 459 * @retval None 460 */ 461 #define __HAL_USART_ONE_BIT_SAMPLE_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR3, (USART_CR3_ONEBIT)) 462 463 /** @brief Disables the UART one bit sample method 464 * @param __HANDLE__: specifies the UART Handle. 465 * @retval None 466 */ 467 #define __HAL_USART_ONE_BIT_SAMPLE_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR3,(USART_CR3_ONEBIT)) 468 469 /** @brief Enable USART 470 * @param __HANDLE__: specifies the USART Handle. 471 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 472 * @retval None 473 */ 474 #define __HAL_USART_ENABLE(__HANDLE__) SET_BIT((__HANDLE__)->Instance->CR1,(USART_CR1_UE)) 475 476 /** @brief Disable USART 477 * @param __HANDLE__: specifies the USART Handle. 478 * USART Handle selects the USARTx peripheral (USART availability and x value depending on device). 479 * @retval None 480 */ 481 #define __HAL_USART_DISABLE(__HANDLE__) CLEAR_BIT((__HANDLE__)->Instance->CR1,(USART_CR1_UE)) 482 483 484 /** 485 * @} 486 */ 487 488 489 /* Private macros --------------------------------------------------------*/ 490 /** @defgroup USART_Private_Macros USART Private Macros 491 * @{ 492 */ 493 494 #define USART_CR1_REG_INDEX 1 495 #define USART_CR2_REG_INDEX 2 496 #define USART_CR3_REG_INDEX 3 497 498 #define USART_DIV(__PCLK__, __BAUD__) (((__PCLK__)*25)/(2*(__BAUD__))) 499 #define USART_DIVMANT(__PCLK__, __BAUD__) (USART_DIV((__PCLK__), (__BAUD__))/100) 500 #define USART_DIVFRAQ(__PCLK__, __BAUD__) (((USART_DIV((__PCLK__), (__BAUD__)) - (USART_DIVMANT((__PCLK__), (__BAUD__)) * 100)) * 16 + 50) / 100) 501 #define USART_BRR(__PCLK__, __BAUD__) ((USART_DIVMANT((__PCLK__), (__BAUD__)) << 4)|(USART_DIVFRAQ((__PCLK__), (__BAUD__)) & 0x07)) 502 503 /** Check USART Baud rate 504 * __BAUDRATE__: Baudrate specified by the user 505 * The maximum Baud Rate is derived from the maximum clock on APB (i.e. 32 MHz) 506 * divided by the smallest oversampling used on the USART (i.e. 8) 507 * return : TRUE or FALSE 508 */ 509 #define IS_USART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 4000001) 510 511 #define IS_USART_WORD_LENGTH(LENGTH) (((LENGTH) == USART_WORDLENGTH_8B) || \ 512 ((LENGTH) == USART_WORDLENGTH_9B)) 513 514 #define IS_USART_STOPBITS(STOPBITS) (((STOPBITS) == USART_STOPBITS_1) || \ 515 ((STOPBITS) == USART_STOPBITS_0_5) || \ 516 ((STOPBITS) == USART_STOPBITS_1_5) || \ 517 ((STOPBITS) == USART_STOPBITS_2)) 518 519 #define IS_USART_PARITY(PARITY) (((PARITY) == USART_PARITY_NONE) || \ 520 ((PARITY) == USART_PARITY_EVEN) || \ 521 ((PARITY) == USART_PARITY_ODD)) 522 523 #define IS_USART_MODE(MODE) ((((MODE) & (~((uint32_t)USART_MODE_TX_RX))) == 0x00) && ((MODE) != (uint32_t)0x00000000)) 524 525 #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_CLOCK_DISABLE) || \ 526 ((CLOCK) == USART_CLOCK_ENABLE)) 527 528 #define IS_USART_POLARITY(CPOL) (((CPOL) == USART_POLARITY_LOW) || ((CPOL) == USART_POLARITY_HIGH)) 529 530 #define IS_USART_PHASE(CPHA) (((CPHA) == USART_PHASE_1EDGE) || ((CPHA) == USART_PHASE_2EDGE)) 531 532 #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LASTBIT_DISABLE) || \ 533 ((LASTBIT) == USART_LASTBIT_ENABLE)) 534 535 #define IS_USART_NACK_STATE(NACK) (((NACK) == USART_NACK_ENABLE) || \ 536 ((NACK) == USART_NACK_DISABLE)) 537 538 /** USART interruptions flag mask 539 * 540 */ 541 #define USART_IT_MASK ((uint32_t) USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RXNEIE | \ 542 USART_CR1_IDLEIE | USART_CR2_LBDIE | USART_CR3_CTSIE | USART_CR3_EIE ) 543 544 /** 545 * @} 546 */ 547 548 549 /* Exported functions --------------------------------------------------------*/ 550 551 /** @addtogroup USART_Exported_Functions USART Exported Functions 552 * @{ 553 */ 554 555 /** @addtogroup USART_Exported_Functions_Group1 Initialization and de-initialization functions 556 * @{ 557 */ 558 559 /* Initialization and de-initialization functions ******************************/ 560 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart); 561 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart); 562 void HAL_USART_MspInit(USART_HandleTypeDef *husart); 563 void HAL_USART_MspDeInit(USART_HandleTypeDef *husart); 564 565 /** 566 * @} 567 */ 568 569 /** @addtogroup USART_Exported_Functions_Group2 IO operation functions 570 * @{ 571 */ 572 573 /* IO operation functions *******************************************************/ 574 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout); 575 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout); 576 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout); 577 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size); 578 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size); 579 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size); 580 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size); 581 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size); 582 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size); 583 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart); 584 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart); 585 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart); 586 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart); 587 void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart); 588 void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart); 589 void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart); 590 void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart); 591 void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart); 592 void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart); 593 594 /** 595 * @} 596 */ 597 598 /* Peripheral Control functions ***********************************************/ 599 600 /** @addtogroup USART_Exported_Functions_Group3 Peripheral State and Errors functions 601 * @{ 602 */ 603 604 /* Peripheral State and Error functions ***************************************/ 605 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart); 606 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart); 607 608 /** 609 * @} 610 */ 611 612 /** 613 * @} 614 */ 615 616 /** 617 * @} 618 */ 619 620 /** 621 * @} 622 */ 623 624 #ifdef __cplusplus 625 } 626 #endif 627 628 #endif /* __STM32L1xx_HAL_USART_H */ 629 630 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 631