stm32l4xx_hal_rng.h 13 KB

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