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