1 /** 2 ****************************************************************************** 3 * @file stm32f0xx_hal_usart_ex.h 4 * @author MCD Application Team 5 * @brief Header file of USART HAL Extended 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_EX_H 21 #define STM32F0xx_HAL_USART_EX_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 USARTEx 35 * @{ 36 */ 37 38 /* Exported types ------------------------------------------------------------*/ 39 /* Exported constants --------------------------------------------------------*/ 40 /** @defgroup USARTEx_Exported_Constants USARTEx Exported Constants 41 * @{ 42 */ 43 44 /** @defgroup USARTEx_Word_Length USARTEx Word Length 45 * @{ 46 */ 47 #if defined(USART_CR1_M0)&& defined(USART_CR1_M1) 48 #define USART_WORDLENGTH_7B (USART_CR1_M1) /*!< 7-bit long USART frame */ 49 #define USART_WORDLENGTH_8B (0x00000000U) /*!< 8-bit long USART frame */ 50 #define USART_WORDLENGTH_9B (USART_CR1_M0) /*!< 9-bit long USART frame */ 51 #elif defined(USART_CR1_M) 52 #define USART_WORDLENGTH_8B (0x00000000U) /*!< 8-bit long USART frame */ 53 #define USART_WORDLENGTH_9B (USART_CR1_M) /*!< 9-bit long USART frame */ 54 #endif /* USART_CR1_M0 && USART_CR1_M */ 55 /** 56 * @} 57 */ 58 59 60 /** 61 * @} 62 */ 63 64 /* Private macros ------------------------------------------------------------*/ 65 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros 66 * @{ 67 */ 68 69 /** @brief Report the USART clock source. 70 * @param __HANDLE__ specifies the USART Handle. 71 * @param __CLOCKSOURCE__ output variable. 72 * @retval the USART clocking source, written in __CLOCKSOURCE__. 73 */ 74 /** @brief Report the UART clock source. 75 * @param __HANDLE__ specifies the UART Handle. 76 * @param __CLOCKSOURCE__ output variable. 77 * @retval UART clocking source, written in __CLOCKSOURCE__. 78 */ 79 80 #if defined(STM32F030x6) || defined(STM32F031x6) || defined(STM32F038xx) 81 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 82 do { \ 83 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 84 { \ 85 case RCC_USART1CLKSOURCE_PCLK1: \ 86 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 87 break; \ 88 case RCC_USART1CLKSOURCE_HSI: \ 89 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 90 break; \ 91 case RCC_USART1CLKSOURCE_SYSCLK: \ 92 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 93 break; \ 94 case RCC_USART1CLKSOURCE_LSE: \ 95 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 96 break; \ 97 default: \ 98 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 99 break; \ 100 } \ 101 } while(0) 102 #elif defined (STM32F030x8) || defined (STM32F070x6) || defined (STM32F042x6) || defined (STM32F048xx) \ 103 || defined (STM32F051x8) || defined (STM32F058xx) 104 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 105 do { \ 106 if((__HANDLE__)->Instance == USART1) \ 107 { \ 108 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 109 { \ 110 case RCC_USART1CLKSOURCE_PCLK1: \ 111 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 112 break; \ 113 case RCC_USART1CLKSOURCE_HSI: \ 114 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 115 break; \ 116 case RCC_USART1CLKSOURCE_SYSCLK: \ 117 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 118 break; \ 119 case RCC_USART1CLKSOURCE_LSE: \ 120 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 121 break; \ 122 default: \ 123 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 124 break; \ 125 } \ 126 } \ 127 else if((__HANDLE__)->Instance == USART2) \ 128 { \ 129 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 130 } \ 131 else \ 132 { \ 133 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 134 } \ 135 } while(0) 136 #elif defined (STM32F070xB) 137 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 138 do { \ 139 if((__HANDLE__)->Instance == USART1) \ 140 { \ 141 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 142 { \ 143 case RCC_USART1CLKSOURCE_PCLK1: \ 144 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 145 break; \ 146 case RCC_USART1CLKSOURCE_HSI: \ 147 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 148 break; \ 149 case RCC_USART1CLKSOURCE_SYSCLK: \ 150 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 151 break; \ 152 case RCC_USART1CLKSOURCE_LSE: \ 153 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 154 break; \ 155 default: \ 156 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 157 break; \ 158 } \ 159 } \ 160 else if((__HANDLE__)->Instance == USART2) \ 161 { \ 162 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 163 } \ 164 else if((__HANDLE__)->Instance == USART3) \ 165 { \ 166 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 167 } \ 168 else if((__HANDLE__)->Instance == USART4) \ 169 { \ 170 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 171 } \ 172 else \ 173 { \ 174 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 175 } \ 176 } while(0) 177 #elif defined(STM32F071xB) || defined(STM32F072xB) || defined(STM32F078xx) 178 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 179 do { \ 180 if((__HANDLE__)->Instance == USART1) \ 181 { \ 182 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 183 { \ 184 case RCC_USART1CLKSOURCE_PCLK1: \ 185 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 186 break; \ 187 case RCC_USART1CLKSOURCE_HSI: \ 188 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 189 break; \ 190 case RCC_USART1CLKSOURCE_SYSCLK: \ 191 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 192 break; \ 193 case RCC_USART1CLKSOURCE_LSE: \ 194 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 195 break; \ 196 default: \ 197 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 198 break; \ 199 } \ 200 } \ 201 else if((__HANDLE__)->Instance == USART2) \ 202 { \ 203 switch(__HAL_RCC_GET_USART2_SOURCE()) \ 204 { \ 205 case RCC_USART2CLKSOURCE_PCLK1: \ 206 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 207 break; \ 208 case RCC_USART2CLKSOURCE_HSI: \ 209 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 210 break; \ 211 case RCC_USART2CLKSOURCE_SYSCLK: \ 212 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 213 break; \ 214 case RCC_USART2CLKSOURCE_LSE: \ 215 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 216 break; \ 217 default: \ 218 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 219 break; \ 220 } \ 221 } \ 222 else if((__HANDLE__)->Instance == USART3) \ 223 { \ 224 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 225 } \ 226 else if((__HANDLE__)->Instance == USART4) \ 227 { \ 228 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 229 } \ 230 else \ 231 { \ 232 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 233 } \ 234 } while(0) 235 #elif defined(STM32F091xC) || defined (STM32F098xx) 236 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 237 do { \ 238 if((__HANDLE__)->Instance == USART1) \ 239 { \ 240 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 241 { \ 242 case RCC_USART1CLKSOURCE_PCLK1: \ 243 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 244 break; \ 245 case RCC_USART1CLKSOURCE_HSI: \ 246 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 247 break; \ 248 case RCC_USART1CLKSOURCE_SYSCLK: \ 249 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 250 break; \ 251 case RCC_USART1CLKSOURCE_LSE: \ 252 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 253 break; \ 254 default: \ 255 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 256 break; \ 257 } \ 258 } \ 259 else if((__HANDLE__)->Instance == USART2) \ 260 { \ 261 switch(__HAL_RCC_GET_USART2_SOURCE()) \ 262 { \ 263 case RCC_USART2CLKSOURCE_PCLK1: \ 264 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 265 break; \ 266 case RCC_USART2CLKSOURCE_HSI: \ 267 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 268 break; \ 269 case RCC_USART2CLKSOURCE_SYSCLK: \ 270 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 271 break; \ 272 case RCC_USART2CLKSOURCE_LSE: \ 273 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 274 break; \ 275 default: \ 276 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 277 break; \ 278 } \ 279 } \ 280 else if((__HANDLE__)->Instance == USART3) \ 281 { \ 282 switch(__HAL_RCC_GET_USART3_SOURCE()) \ 283 { \ 284 case RCC_USART3CLKSOURCE_PCLK1: \ 285 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 286 break; \ 287 case RCC_USART3CLKSOURCE_HSI: \ 288 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 289 break; \ 290 case RCC_USART3CLKSOURCE_SYSCLK: \ 291 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 292 break; \ 293 case RCC_USART3CLKSOURCE_LSE: \ 294 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 295 break; \ 296 default: \ 297 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 298 break; \ 299 } \ 300 } \ 301 else if((__HANDLE__)->Instance == USART4) \ 302 { \ 303 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 304 } \ 305 else if((__HANDLE__)->Instance == USART5) \ 306 { \ 307 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 308 } \ 309 else if((__HANDLE__)->Instance == USART6) \ 310 { \ 311 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 312 } \ 313 else if((__HANDLE__)->Instance == USART7) \ 314 { \ 315 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 316 } \ 317 else if((__HANDLE__)->Instance == USART8) \ 318 { \ 319 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 320 } \ 321 else \ 322 { \ 323 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 324 } \ 325 } while(0) 326 #elif defined(STM32F030xC) 327 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 328 do { \ 329 if((__HANDLE__)->Instance == USART1) \ 330 { \ 331 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 332 { \ 333 case RCC_USART1CLKSOURCE_PCLK1: \ 334 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 335 break; \ 336 case RCC_USART1CLKSOURCE_HSI: \ 337 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 338 break; \ 339 case RCC_USART1CLKSOURCE_SYSCLK: \ 340 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 341 break; \ 342 case RCC_USART1CLKSOURCE_LSE: \ 343 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 344 break; \ 345 default: \ 346 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 347 break; \ 348 } \ 349 } \ 350 else if((__HANDLE__)->Instance == USART2) \ 351 { \ 352 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 353 } \ 354 else if((__HANDLE__)->Instance == USART3) \ 355 { \ 356 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 357 } \ 358 else if((__HANDLE__)->Instance == USART4) \ 359 { \ 360 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 361 } \ 362 else if((__HANDLE__)->Instance == USART5) \ 363 { \ 364 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 365 } \ 366 else if((__HANDLE__)->Instance == USART6) \ 367 { \ 368 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 369 } \ 370 else \ 371 { \ 372 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 373 } \ 374 } while(0) 375 #endif /* defined(STM32F030x6) || defined(STM32F031x6) || defined(STM32F038xx) */ 376 /** @brief Compute the USART mask to apply to retrieve the received data 377 * according to the word length and to the parity bits activation. 378 * @note If PCE = 1, the parity bit is not included in the data extracted 379 * by the reception API(). 380 * This masking operation is not carried out in the case of 381 * DMA transfers. 382 * @param __HANDLE__ specifies the USART Handle. 383 * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field. 384 */ 385 #if defined(USART_CR1_M0)&& defined(USART_CR1_M1) 386 #define USART_MASK_COMPUTATION(__HANDLE__) \ 387 do { \ 388 if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \ 389 { \ 390 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 391 { \ 392 (__HANDLE__)->Mask = 0x01FFU; \ 393 } \ 394 else \ 395 { \ 396 (__HANDLE__)->Mask = 0x00FFU; \ 397 } \ 398 } \ 399 else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \ 400 { \ 401 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 402 { \ 403 (__HANDLE__)->Mask = 0x00FFU; \ 404 } \ 405 else \ 406 { \ 407 (__HANDLE__)->Mask = 0x007FU; \ 408 } \ 409 } \ 410 else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B) \ 411 { \ 412 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 413 { \ 414 (__HANDLE__)->Mask = 0x007FU; \ 415 } \ 416 else \ 417 { \ 418 (__HANDLE__)->Mask = 0x003FU; \ 419 } \ 420 } \ 421 else \ 422 { \ 423 (__HANDLE__)->Mask = 0x0000U; \ 424 } \ 425 } while(0U) 426 #elif defined(USART_CR1_M) 427 #define USART_MASK_COMPUTATION(__HANDLE__) \ 428 do { \ 429 if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \ 430 { \ 431 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 432 { \ 433 (__HANDLE__)->Mask = 0x01FFU; \ 434 } \ 435 else \ 436 { \ 437 (__HANDLE__)->Mask = 0x00FFU; \ 438 } \ 439 } \ 440 else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \ 441 { \ 442 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 443 { \ 444 (__HANDLE__)->Mask = 0x00FFU; \ 445 } \ 446 else \ 447 { \ 448 (__HANDLE__)->Mask = 0x007FU; \ 449 } \ 450 } \ 451 else \ 452 { \ 453 (__HANDLE__)->Mask = 0x0000U; \ 454 } \ 455 } while(0U) 456 #endif /* USART_CR1_M0 && USART_CR1_M */ 457 458 /** 459 * @brief Ensure that USART frame length is valid. 460 * @param __LENGTH__ USART frame length. 461 * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid) 462 */ 463 #if defined(USART_CR1_M0)&& defined(USART_CR1_M1) 464 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \ 465 ((__LENGTH__) == USART_WORDLENGTH_8B) || \ 466 ((__LENGTH__) == USART_WORDLENGTH_9B)) 467 #elif defined(USART_CR1_M) 468 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_8B) || \ 469 ((__LENGTH__) == USART_WORDLENGTH_9B)) 470 #endif /* USART_CR1_M0 && USART_CR1_M */ 471 472 473 /** 474 * @} 475 */ 476 477 /* Exported functions --------------------------------------------------------*/ 478 /** @addtogroup USARTEx_Exported_Functions 479 * @{ 480 */ 481 482 /** @addtogroup USARTEx_Exported_Functions_Group1 483 * @{ 484 */ 485 486 /* IO operation functions *****************************************************/ 487 488 /** 489 * @} 490 */ 491 492 /** @addtogroup USARTEx_Exported_Functions_Group2 493 * @{ 494 */ 495 496 /* Peripheral Control functions ***********************************************/ 497 498 /** 499 * @} 500 */ 501 502 /** 503 * @} 504 */ 505 506 /** 507 * @} 508 */ 509 510 /** 511 * @} 512 */ 513 514 #ifdef __cplusplus 515 } 516 #endif 517 518 #endif /* STM32F0xx_HAL_USART_EX_H */ 519 520