1 /** 2 ****************************************************************************** 3 * @file stm32l5xx_hal_rng.h 4 * @author MCD Application Team 5 * @brief Header file of RNG HAL module. 6 ****************************************************************************** 7 * @attention 8 * 9 * <h2><center>© Copyright (c) 2019 STMicroelectronics. 10 * All rights reserved.</center></h2> 11 * 12 * This software component is licensed by ST under BSD 3-Clause license, 13 * the "License"; You may not use this file except in compliance with the 14 * License. You may obtain a copy of the License at: 15 * opensource.org/licenses/BSD-3-Clause 16 * 17 ****************************************************************************** 18 */ 19 20 /* Define to prevent recursive inclusion -------------------------------------*/ 21 #ifndef STM32L5xx_HAL_RNG_H 22 #define STM32L5xx_HAL_RNG_H 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /* Includes ------------------------------------------------------------------*/ 29 #include "stm32l5xx_hal_def.h" 30 31 /** @addtogroup STM32L5xx_HAL_Driver 32 * @{ 33 */ 34 35 #if defined (RNG) 36 37 /** @defgroup RNG RNG 38 * @brief RNG HAL module driver 39 * @{ 40 */ 41 42 /* Exported types ------------------------------------------------------------*/ 43 44 /** @defgroup RNG_Exported_Types RNG Exported Types 45 * @{ 46 */ 47 48 /** @defgroup RNG_Exported_Types_Group1 RNG Init Structure definition 49 * @{ 50 */ 51 typedef struct 52 { 53 uint32_t ClockErrorDetection; /*!< CED Clock error detection */ 54 } RNG_InitTypeDef; 55 56 /** 57 * @} 58 */ 59 60 /** @defgroup RNG_Exported_Types_Group2 RNG State Structure definition 61 * @{ 62 */ 63 typedef enum 64 { 65 HAL_RNG_STATE_RESET = 0x00U, /*!< RNG not yet initialized or disabled */ 66 HAL_RNG_STATE_READY = 0x01U, /*!< RNG initialized and ready for use */ 67 HAL_RNG_STATE_BUSY = 0x02U, /*!< RNG internal process is ongoing */ 68 HAL_RNG_STATE_TIMEOUT = 0x03U, /*!< RNG timeout state */ 69 HAL_RNG_STATE_ERROR = 0x04U /*!< RNG error state */ 70 71 } HAL_RNG_StateTypeDef; 72 73 /** 74 * @} 75 */ 76 77 /** @defgroup RNG_Exported_Types_Group3 RNG Handle Structure definition 78 * @{ 79 */ 80 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 81 typedef struct __RNG_HandleTypeDef 82 #else 83 typedef struct 84 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 85 { 86 RNG_TypeDef *Instance; /*!< Register base address */ 87 88 RNG_InitTypeDef Init; /*!< RNG configuration parameters */ 89 90 HAL_LockTypeDef Lock; /*!< RNG locking object */ 91 92 __IO HAL_RNG_StateTypeDef State; /*!< RNG communication state */ 93 94 __IO uint32_t ErrorCode; /*!< RNG Error code */ 95 96 uint32_t RandomNumber; /*!< Last Generated RNG Data */ 97 98 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 99 void (* ReadyDataCallback)(struct __RNG_HandleTypeDef *hrng, uint32_t random32bit); /*!< RNG Data Ready Callback */ 100 void (* ErrorCallback)(struct __RNG_HandleTypeDef *hrng); /*!< RNG Error Callback */ 101 102 void (* MspInitCallback)(struct __RNG_HandleTypeDef *hrng); /*!< RNG Msp Init callback */ 103 void (* MspDeInitCallback)(struct __RNG_HandleTypeDef *hrng); /*!< RNG Msp DeInit callback */ 104 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 105 106 } RNG_HandleTypeDef; 107 108 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 109 /** 110 * @brief HAL RNG Callback ID enumeration definition 111 */ 112 typedef enum 113 { 114 HAL_RNG_ERROR_CB_ID = 0x00U, /*!< RNG Error Callback ID */ 115 116 HAL_RNG_MSPINIT_CB_ID = 0x01U, /*!< RNG MspInit callback ID */ 117 HAL_RNG_MSPDEINIT_CB_ID = 0x02U /*!< RNG MspDeInit callback ID */ 118 119 } HAL_RNG_CallbackIDTypeDef; 120 121 /** 122 * @brief HAL RNG Callback pointer definition 123 */ 124 typedef void (*pRNG_CallbackTypeDef)(RNG_HandleTypeDef *hrng); /*!< pointer to a common RNG callback function */ 125 typedef void (*pRNG_ReadyDataCallbackTypeDef)(RNG_HandleTypeDef *hrng, uint32_t random32bit); /*!< pointer to an RNG Data Ready specific callback function */ 126 127 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 128 129 /** 130 * @} 131 */ 132 133 /** 134 * @} 135 */ 136 137 /* Exported constants --------------------------------------------------------*/ 138 /** @defgroup RNG_Exported_Constants RNG Exported Constants 139 * @{ 140 */ 141 142 /** @defgroup RNG_Exported_Constants_Group1 RNG Interrupt definition 143 * @{ 144 */ 145 #define RNG_IT_DRDY RNG_SR_DRDY /*!< Data Ready interrupt */ 146 #define RNG_IT_CEI RNG_SR_CEIS /*!< Clock error interrupt */ 147 #define RNG_IT_SEI RNG_SR_SEIS /*!< Seed error interrupt */ 148 /** 149 * @} 150 */ 151 152 /** @defgroup RNG_Exported_Constants_Group2 RNG Flag definition 153 * @{ 154 */ 155 #define RNG_FLAG_DRDY RNG_SR_DRDY /*!< Data ready */ 156 #define RNG_FLAG_CECS RNG_SR_CECS /*!< Clock error current status */ 157 #define RNG_FLAG_SECS RNG_SR_SECS /*!< Seed error current status */ 158 /** 159 * @} 160 */ 161 162 /** @defgroup RNG_Exported_Constants_Group3 RNG Clock Error Detection 163 * @{ 164 */ 165 #define RNG_CED_ENABLE 0x00000000U /*!< Clock error detection Enabled */ 166 #define RNG_CED_DISABLE RNG_CR_CED /*!< Clock error detection Disabled */ 167 /** 168 * @} 169 */ 170 171 /** @defgroup RNG_Error_Definition RNG Error Definition 172 * @{ 173 */ 174 #define HAL_RNG_ERROR_NONE 0x00000000U /*!< No error */ 175 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 176 #define HAL_RNG_ERROR_INVALID_CALLBACK 0x00000001U /*!< Invalid Callback error */ 177 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 178 #define HAL_RNG_ERROR_TIMEOUT 0x00000002U /*!< Timeout error */ 179 #define HAL_RNG_ERROR_BUSY 0x00000004U /*!< Busy error */ 180 #define HAL_RNG_ERROR_SEED 0x00000008U /*!< Seed error */ 181 #define HAL_RNG_ERROR_CLOCK 0x00000010U /*!< Clock error */ 182 /** 183 * @} 184 */ 185 186 /** 187 * @} 188 */ 189 190 /* Exported macros -----------------------------------------------------------*/ 191 /** @defgroup RNG_Exported_Macros RNG Exported Macros 192 * @{ 193 */ 194 195 /** @brief Reset RNG handle state 196 * @param __HANDLE__ RNG Handle 197 * @retval None 198 */ 199 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 200 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__) do{ \ 201 (__HANDLE__)->State = HAL_RNG_STATE_RESET; \ 202 (__HANDLE__)->MspInitCallback = NULL; \ 203 (__HANDLE__)->MspDeInitCallback = NULL; \ 204 } while(0U) 205 #else 206 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_RNG_STATE_RESET) 207 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 208 209 /** 210 * @brief Enables the RNG peripheral. 211 * @param __HANDLE__ RNG Handle 212 * @retval None 213 */ 214 #define __HAL_RNG_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= RNG_CR_RNGEN) 215 216 /** 217 * @brief Disables the RNG peripheral. 218 * @param __HANDLE__ RNG Handle 219 * @retval None 220 */ 221 #define __HAL_RNG_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_RNGEN) 222 223 /** 224 * @brief Check the selected RNG flag status. 225 * @param __HANDLE__ RNG Handle 226 * @param __FLAG__ RNG flag 227 * This parameter can be one of the following values: 228 * @arg RNG_FLAG_DRDY: Data ready 229 * @arg RNG_FLAG_CECS: Clock error current status 230 * @arg RNG_FLAG_SECS: Seed error current status 231 * @retval The new state of __FLAG__ (SET or RESET). 232 */ 233 #define __HAL_RNG_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__)) 234 235 /** 236 * @brief Clears the selected RNG flag status. 237 * @param __HANDLE__ RNG handle 238 * @param __FLAG__ RNG flag to clear 239 * @note WARNING: This is a dummy macro for HAL code alignment, 240 * flags RNG_FLAG_DRDY, RNG_FLAG_CECS and RNG_FLAG_SECS are read-only. 241 * @retval None 242 */ 243 #define __HAL_RNG_CLEAR_FLAG(__HANDLE__, __FLAG__) /* dummy macro */ 244 245 /** 246 * @brief Enables the RNG interrupts. 247 * @param __HANDLE__ RNG Handle 248 * @retval None 249 */ 250 #define __HAL_RNG_ENABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR |= RNG_CR_IE) 251 252 /** 253 * @brief Disables the RNG interrupts. 254 * @param __HANDLE__ RNG Handle 255 * @retval None 256 */ 257 #define __HAL_RNG_DISABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_IE) 258 259 /** 260 * @brief Checks whether the specified RNG interrupt has occurred or not. 261 * @param __HANDLE__ RNG Handle 262 * @param __INTERRUPT__ specifies the RNG interrupt status flag to check. 263 * This parameter can be one of the following values: 264 * @arg RNG_IT_DRDY: Data ready interrupt 265 * @arg RNG_IT_CEI: Clock error interrupt 266 * @arg RNG_IT_SEI: Seed error interrupt 267 * @retval The new state of __INTERRUPT__ (SET or RESET). 268 */ 269 #define __HAL_RNG_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR & (__INTERRUPT__)) == (__INTERRUPT__)) 270 271 /** 272 * @brief Clear the RNG interrupt status flags. 273 * @param __HANDLE__ RNG Handle 274 * @param __INTERRUPT__ specifies the RNG interrupt status flag to clear. 275 * This parameter can be one of the following values: 276 * @arg RNG_IT_CEI: Clock error interrupt 277 * @arg RNG_IT_SEI: Seed error interrupt 278 * @note RNG_IT_DRDY flag is read-only, reading RNG_DR register automatically clears RNG_IT_DRDY. 279 * @retval None 280 */ 281 #define __HAL_RNG_CLEAR_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR) = ~(__INTERRUPT__)) 282 283 /** 284 * @} 285 */ 286 287 /* Include RNG HAL Extended module */ 288 #include "stm32l5xx_hal_rng_ex.h" 289 /* Exported functions --------------------------------------------------------*/ 290 /** @defgroup RNG_Exported_Functions RNG Exported Functions 291 * @{ 292 */ 293 294 /** @defgroup RNG_Exported_Functions_Group1 Initialization and configuration functions 295 * @{ 296 */ 297 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng); 298 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng); 299 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng); 300 void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng); 301 302 /* Callbacks Register/UnRegister functions ***********************************/ 303 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1) 304 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, 305 pRNG_CallbackTypeDef pCallback); 306 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID); 307 308 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback); 309 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng); 310 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */ 311 312 /** 313 * @} 314 */ 315 316 /** @defgroup RNG_Exported_Functions_Group2 Peripheral Control functions 317 * @{ 318 */ 319 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit); 320 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng); 321 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng); 322 323 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng); 324 void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng); 325 void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit); 326 327 /** 328 * @} 329 */ 330 331 /** @defgroup RNG_Exported_Functions_Group3 Peripheral State functions 332 * @{ 333 */ 334 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng); 335 uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng); 336 /** 337 * @} 338 */ 339 340 /** 341 * @} 342 */ 343 344 /* Private macros ------------------------------------------------------------*/ 345 /** @defgroup RNG_Private_Macros RNG Private Macros 346 * @{ 347 */ 348 #define IS_RNG_IT(IT) (((IT) == RNG_IT_CEI) || \ 349 ((IT) == RNG_IT_SEI)) 350 351 #define IS_RNG_FLAG(FLAG) (((FLAG) == RNG_FLAG_DRDY) || \ 352 ((FLAG) == RNG_FLAG_CECS) || \ 353 ((FLAG) == RNG_FLAG_SECS)) 354 355 /** 356 * @brief Verify the RNG Clock Error Detection mode. 357 * @param __MODE__ RNG Clock Error Detection mode 358 * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid) 359 */ 360 #define IS_RNG_CED(__MODE__) (((__MODE__) == RNG_CED_ENABLE) || \ 361 ((__MODE__) == RNG_CED_DISABLE)) 362 /** 363 * @} 364 */ 365 366 /* Private functions ---------------------------------------------------------*/ 367 /** @defgroup RNG_Private_Functions RNG Private functions 368 * @{ 369 */ 370 HAL_StatusTypeDef RNG_RecoverSeedError(RNG_HandleTypeDef *hrng); 371 /** 372 * @} 373 */ 374 /** 375 * @} 376 */ 377 378 #endif /* RNG */ 379 380 /** 381 * @} 382 */ 383 384 #ifdef __cplusplus 385 } 386 #endif 387 388 389 #endif /* STM32L5xx_HAL_RNG_H */ 390 391 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 392