1 /** 2 ****************************************************************************** 3 * @file stm32g0xx_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) 2018 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 STM32G0xx_HAL_USART_EX_H 21 #define STM32G0xx_HAL_USART_EX_H 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 /* Includes ------------------------------------------------------------------*/ 28 #include "stm32g0xx_hal_def.h" 29 30 /** @addtogroup STM32G0xx_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 #define USART_WORDLENGTH_7B (USART_CR1_M1) /*!< 7-bit long USART frame */ 48 #define USART_WORDLENGTH_8B (0x00000000U) /*!< 8-bit long USART frame */ 49 #define USART_WORDLENGTH_9B (USART_CR1_M0) /*!< 9-bit long USART frame */ 50 /** 51 * @} 52 */ 53 54 /** @defgroup USARTEx_Slave_Select_management USARTEx Slave Select Management 55 * @{ 56 */ 57 #define USART_NSS_HARD 0x00000000U /*!< SPI slave selection depends on NSS input pin */ 58 #define USART_NSS_SOFT USART_CR2_DIS_NSS /*!< SPI slave is always selected and NSS input pin is ignored */ 59 /** 60 * @} 61 */ 62 63 64 /** @defgroup USARTEx_Slave_Mode USARTEx Synchronous Slave mode enable 65 * @brief USART SLAVE mode 66 * @{ 67 */ 68 #define USART_SLAVEMODE_DISABLE 0x00000000U /*!< USART SPI Slave Mode Enable */ 69 #define USART_SLAVEMODE_ENABLE USART_CR2_SLVEN /*!< USART SPI Slave Mode Disable */ 70 /** 71 * @} 72 */ 73 74 /** @defgroup USARTEx_FIFO_mode USARTEx FIFO mode 75 * @brief USART FIFO mode 76 * @{ 77 */ 78 #define USART_FIFOMODE_DISABLE 0x00000000U /*!< FIFO mode disable */ 79 #define USART_FIFOMODE_ENABLE USART_CR1_FIFOEN /*!< FIFO mode enable */ 80 /** 81 * @} 82 */ 83 84 /** @defgroup USARTEx_TXFIFO_threshold_level USARTEx TXFIFO threshold level 85 * @brief USART TXFIFO level 86 * @{ 87 */ 88 #define USART_TXFIFO_THRESHOLD_1_8 0x00000000U /*!< TXFIFO reaches 1/8 of its depth */ 89 #define USART_TXFIFO_THRESHOLD_1_4 USART_CR3_TXFTCFG_0 /*!< TXFIFO reaches 1/4 of its depth */ 90 #define USART_TXFIFO_THRESHOLD_1_2 USART_CR3_TXFTCFG_1 /*!< TXFIFO reaches 1/2 of its depth */ 91 #define USART_TXFIFO_THRESHOLD_3_4 (USART_CR3_TXFTCFG_0|USART_CR3_TXFTCFG_1) /*!< TXFIFO reaches 3/4 of its depth */ 92 #define USART_TXFIFO_THRESHOLD_7_8 USART_CR3_TXFTCFG_2 /*!< TXFIFO reaches 7/8 of its depth */ 93 #define USART_TXFIFO_THRESHOLD_8_8 (USART_CR3_TXFTCFG_2|USART_CR3_TXFTCFG_0) /*!< TXFIFO becomes empty */ 94 /** 95 * @} 96 */ 97 98 /** @defgroup USARTEx_RXFIFO_threshold_level USARTEx RXFIFO threshold level 99 * @brief USART RXFIFO level 100 * @{ 101 */ 102 #define USART_RXFIFO_THRESHOLD_1_8 0x00000000U /*!< RXFIFO FIFO reaches 1/8 of its depth */ 103 #define USART_RXFIFO_THRESHOLD_1_4 USART_CR3_RXFTCFG_0 /*!< RXFIFO FIFO reaches 1/4 of its depth */ 104 #define USART_RXFIFO_THRESHOLD_1_2 USART_CR3_RXFTCFG_1 /*!< RXFIFO FIFO reaches 1/2 of its depth */ 105 #define USART_RXFIFO_THRESHOLD_3_4 (USART_CR3_RXFTCFG_0|USART_CR3_RXFTCFG_1) /*!< RXFIFO FIFO reaches 3/4 of its depth */ 106 #define USART_RXFIFO_THRESHOLD_7_8 USART_CR3_RXFTCFG_2 /*!< RXFIFO FIFO reaches 7/8 of its depth */ 107 #define USART_RXFIFO_THRESHOLD_8_8 (USART_CR3_RXFTCFG_2|USART_CR3_RXFTCFG_0) /*!< RXFIFO FIFO becomes full */ 108 /** 109 * @} 110 */ 111 112 /** 113 * @} 114 */ 115 116 /* Private macros ------------------------------------------------------------*/ 117 /** @defgroup USARTEx_Private_Macros USARTEx Private Macros 118 * @{ 119 */ 120 121 /** @brief Report the USART clock source. 122 * @param __HANDLE__ specifies the USART Handle. 123 * @param __CLOCKSOURCE__ output variable. 124 * @retval the USART clocking source, written in __CLOCKSOURCE__. 125 */ 126 #if defined(STM32G0C1xx) || defined(STM32G0B1xx) || defined(STM32G0B0xx) 127 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 128 do { \ 129 if((__HANDLE__)->Instance == USART1) \ 130 { \ 131 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 132 { \ 133 case RCC_USART1CLKSOURCE_PCLK1: \ 134 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 135 break; \ 136 case RCC_USART1CLKSOURCE_HSI: \ 137 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 138 break; \ 139 case RCC_USART1CLKSOURCE_SYSCLK: \ 140 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 141 break; \ 142 case RCC_USART1CLKSOURCE_LSE: \ 143 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 144 break; \ 145 default: \ 146 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 147 break; \ 148 } \ 149 } \ 150 else if((__HANDLE__)->Instance == USART2) \ 151 { \ 152 switch(__HAL_RCC_GET_USART2_SOURCE()) \ 153 { \ 154 case RCC_USART2CLKSOURCE_PCLK1: \ 155 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 156 break; \ 157 case RCC_USART2CLKSOURCE_HSI: \ 158 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 159 break; \ 160 case RCC_USART2CLKSOURCE_SYSCLK: \ 161 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 162 break; \ 163 case RCC_USART2CLKSOURCE_LSE: \ 164 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 165 break; \ 166 default: \ 167 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 168 break; \ 169 } \ 170 } \ 171 else if((__HANDLE__)->Instance == USART3) \ 172 { \ 173 switch(__HAL_RCC_GET_USART3_SOURCE()) \ 174 { \ 175 case RCC_USART3CLKSOURCE_PCLK1: \ 176 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 177 break; \ 178 case RCC_USART3CLKSOURCE_HSI: \ 179 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 180 break; \ 181 case RCC_USART3CLKSOURCE_SYSCLK: \ 182 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 183 break; \ 184 case RCC_USART3CLKSOURCE_LSE: \ 185 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 186 break; \ 187 default: \ 188 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 189 break; \ 190 } \ 191 } \ 192 else if((__HANDLE__)->Instance == USART4) \ 193 { \ 194 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 195 } \ 196 else if((__HANDLE__)->Instance == USART5) \ 197 { \ 198 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 199 } \ 200 else if((__HANDLE__)->Instance == USART6) \ 201 { \ 202 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 203 } \ 204 else \ 205 { \ 206 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 207 } \ 208 } while(0U) 209 #elif defined(STM32G081xx) || defined(STM32G071xx) || defined(STM32G070xx) 210 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 211 do { \ 212 if((__HANDLE__)->Instance == USART1) \ 213 { \ 214 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 215 { \ 216 case RCC_USART1CLKSOURCE_PCLK1: \ 217 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 218 break; \ 219 case RCC_USART1CLKSOURCE_HSI: \ 220 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 221 break; \ 222 case RCC_USART1CLKSOURCE_SYSCLK: \ 223 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 224 break; \ 225 case RCC_USART1CLKSOURCE_LSE: \ 226 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 227 break; \ 228 default: \ 229 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 230 break; \ 231 } \ 232 } \ 233 else if((__HANDLE__)->Instance == USART2) \ 234 { \ 235 switch(__HAL_RCC_GET_USART2_SOURCE()) \ 236 { \ 237 case RCC_USART2CLKSOURCE_PCLK1: \ 238 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 239 break; \ 240 case RCC_USART2CLKSOURCE_HSI: \ 241 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 242 break; \ 243 case RCC_USART2CLKSOURCE_SYSCLK: \ 244 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 245 break; \ 246 case RCC_USART2CLKSOURCE_LSE: \ 247 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 248 break; \ 249 default: \ 250 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 251 break; \ 252 } \ 253 } \ 254 else if((__HANDLE__)->Instance == USART3) \ 255 { \ 256 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 257 } \ 258 else if((__HANDLE__)->Instance == USART4) \ 259 { \ 260 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 261 } \ 262 else \ 263 { \ 264 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 265 } \ 266 } while(0U) 267 #else 268 #define USART_GETCLOCKSOURCE(__HANDLE__,__CLOCKSOURCE__) \ 269 do { \ 270 if((__HANDLE__)->Instance == USART1) \ 271 { \ 272 switch(__HAL_RCC_GET_USART1_SOURCE()) \ 273 { \ 274 case RCC_USART1CLKSOURCE_PCLK1: \ 275 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 276 break; \ 277 case RCC_USART1CLKSOURCE_HSI: \ 278 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_HSI; \ 279 break; \ 280 case RCC_USART1CLKSOURCE_SYSCLK: \ 281 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_SYSCLK; \ 282 break; \ 283 case RCC_USART1CLKSOURCE_LSE: \ 284 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_LSE; \ 285 break; \ 286 default: \ 287 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 288 break; \ 289 } \ 290 } \ 291 else if((__HANDLE__)->Instance == USART2) \ 292 { \ 293 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_PCLK1; \ 294 } \ 295 else \ 296 { \ 297 (__CLOCKSOURCE__) = USART_CLOCKSOURCE_UNDEFINED; \ 298 } \ 299 } while(0U) 300 #endif /* STM32G0C1xx || STM32G0B1xx || STM32G0B0xx */ 301 /** @brief Compute the USART mask to apply to retrieve the received data 302 * according to the word length and to the parity bits activation. 303 * @note If PCE = 1, the parity bit is not included in the data extracted 304 * by the reception API(). 305 * This masking operation is not carried out in the case of 306 * DMA transfers. 307 * @param __HANDLE__ specifies the USART Handle. 308 * @retval None, the mask to apply to USART RDR register is stored in (__HANDLE__)->Mask field. 309 */ 310 #define USART_MASK_COMPUTATION(__HANDLE__) \ 311 do { \ 312 if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_9B) \ 313 { \ 314 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 315 { \ 316 (__HANDLE__)->Mask = 0x01FFU; \ 317 } \ 318 else \ 319 { \ 320 (__HANDLE__)->Mask = 0x00FFU; \ 321 } \ 322 } \ 323 else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_8B) \ 324 { \ 325 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 326 { \ 327 (__HANDLE__)->Mask = 0x00FFU; \ 328 } \ 329 else \ 330 { \ 331 (__HANDLE__)->Mask = 0x007FU; \ 332 } \ 333 } \ 334 else if ((__HANDLE__)->Init.WordLength == USART_WORDLENGTH_7B) \ 335 { \ 336 if ((__HANDLE__)->Init.Parity == USART_PARITY_NONE) \ 337 { \ 338 (__HANDLE__)->Mask = 0x007FU; \ 339 } \ 340 else \ 341 { \ 342 (__HANDLE__)->Mask = 0x003FU; \ 343 } \ 344 } \ 345 else \ 346 { \ 347 (__HANDLE__)->Mask = 0x0000U; \ 348 } \ 349 } while(0U) 350 351 /** 352 * @brief Ensure that USART frame length is valid. 353 * @param __LENGTH__ USART frame length. 354 * @retval SET (__LENGTH__ is valid) or RESET (__LENGTH__ is invalid) 355 */ 356 #define IS_USART_WORD_LENGTH(__LENGTH__) (((__LENGTH__) == USART_WORDLENGTH_7B) || \ 357 ((__LENGTH__) == USART_WORDLENGTH_8B) || \ 358 ((__LENGTH__) == USART_WORDLENGTH_9B)) 359 360 /** 361 * @brief Ensure that USART Negative Slave Select (NSS) pin management is valid. 362 * @param __NSS__ USART Negative Slave Select pin management. 363 * @retval SET (__NSS__ is valid) or RESET (__NSS__ is invalid) 364 */ 365 #define IS_USART_NSS(__NSS__) (((__NSS__) == USART_NSS_HARD) || \ 366 ((__NSS__) == USART_NSS_SOFT)) 367 368 /** 369 * @brief Ensure that USART Slave Mode is valid. 370 * @param __STATE__ USART Slave Mode. 371 * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid) 372 */ 373 #define IS_USART_SLAVEMODE(__STATE__) (((__STATE__) == USART_SLAVEMODE_DISABLE ) || \ 374 ((__STATE__) == USART_SLAVEMODE_ENABLE)) 375 376 /** 377 * @brief Ensure that USART FIFO mode is valid. 378 * @param __STATE__ USART FIFO mode. 379 * @retval SET (__STATE__ is valid) or RESET (__STATE__ is invalid) 380 */ 381 #define IS_USART_FIFO_MODE_STATE(__STATE__) (((__STATE__) == USART_FIFOMODE_DISABLE ) || \ 382 ((__STATE__) == USART_FIFOMODE_ENABLE)) 383 384 /** 385 * @brief Ensure that USART TXFIFO threshold level is valid. 386 * @param __THRESHOLD__ USART TXFIFO threshold level. 387 * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid) 388 */ 389 #define IS_USART_TXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_8) || \ 390 ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_4) || \ 391 ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_1_2) || \ 392 ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_3_4) || \ 393 ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_7_8) || \ 394 ((__THRESHOLD__) == USART_TXFIFO_THRESHOLD_8_8)) 395 396 /** 397 * @brief Ensure that USART RXFIFO threshold level is valid. 398 * @param __THRESHOLD__ USART RXFIFO threshold level. 399 * @retval SET (__THRESHOLD__ is valid) or RESET (__THRESHOLD__ is invalid) 400 */ 401 #define IS_USART_RXFIFO_THRESHOLD(__THRESHOLD__) (((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_8) || \ 402 ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_4) || \ 403 ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_1_2) || \ 404 ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_3_4) || \ 405 ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_7_8) || \ 406 ((__THRESHOLD__) == USART_RXFIFO_THRESHOLD_8_8)) 407 /** 408 * @} 409 */ 410 411 /* Exported functions --------------------------------------------------------*/ 412 /** @addtogroup USARTEx_Exported_Functions 413 * @{ 414 */ 415 416 /** @addtogroup USARTEx_Exported_Functions_Group1 417 * @{ 418 */ 419 420 /* IO operation functions *****************************************************/ 421 void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart); 422 void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart); 423 424 /** 425 * @} 426 */ 427 428 /** @addtogroup USARTEx_Exported_Functions_Group2 429 * @{ 430 */ 431 432 /* Peripheral Control functions ***********************************************/ 433 HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart); 434 HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart); 435 HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig); 436 HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart); 437 HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart); 438 HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold); 439 HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold); 440 441 /** 442 * @} 443 */ 444 445 /** 446 * @} 447 */ 448 449 /** 450 * @} 451 */ 452 453 /** 454 * @} 455 */ 456 457 #ifdef __cplusplus 458 } 459 #endif 460 461 #endif /* STM32G0xx_HAL_USART_EX_H */ 462 463