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