stm32h7xx_hal_rng.h 13 KB

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