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