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</center></h2>
10   *
11   * Redistribution and use in source and binary forms, with or without modification,
12   * are permitted provided that the following conditions are met:
13   *   1. Redistributions of source code must retain the above copyright notice,
14   *      this list of conditions and the following disclaimer.
15   *   2. Redistributions in binary form must reproduce the above copyright notice,
16   *      this list of conditions and the following disclaimer in the documentation
17   *      and/or other materials provided with the distribution.
18   *   3. Neither the name of STMicroelectronics nor the names of its contributors
19   *      may be used to endorse or promote products derived from this software
20   *      without specific prior written permission.
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   *
33   ******************************************************************************
34   */
35 
36 /* Define to prevent recursive inclusion -------------------------------------*/
37 #ifndef __STM32L4xx_HAL_RNG_H
38 #define __STM32L4xx_HAL_RNG_H
39 
40 #ifdef __cplusplus
41  extern "C" {
42 #endif
43 
44 /* Includes ------------------------------------------------------------------*/
45 #include "stm32l4xx_hal_def.h"
46 
47 /** @addtogroup STM32L4xx_HAL_Driver
48   * @{
49   */
50 
51 /** @addtogroup RNG
52   * @{
53   */
54 
55 /* Exported types ------------------------------------------------------------*/
56 /** @defgroup RNG_Exported_Types RNG Exported Types
57   * @{
58   */
59 
60 #if defined(RNG_CR_CED)
61 /**
62   * @brief  RNG Configuration Structure definition
63   */
64 typedef struct
65 {
66   uint32_t                    ClockErrorDetection; /*!< Clock error detection */
67 }RNG_InitTypeDef;
68 #endif /* defined(RNG_CR_CED) */
69 
70 /**
71   * @brief  RNG HAL State Structure definition
72   */
73 typedef enum
74 {
75   HAL_RNG_STATE_RESET     = 0x00,  /*!< RNG not yet initialized or disabled */
76   HAL_RNG_STATE_READY     = 0x01,  /*!< RNG initialized and ready for use   */
77   HAL_RNG_STATE_BUSY      = 0x02,  /*!< RNG internal process is ongoing     */
78   HAL_RNG_STATE_TIMEOUT   = 0x03,  /*!< RNG timeout state                   */
79   HAL_RNG_STATE_ERROR     = 0x04   /*!< RNG error state                     */
80 
81 }HAL_RNG_StateTypeDef;
82 
83 /**
84   * @brief  RNG Handle Structure definition
85   */
86 typedef struct __RNG_HandleTypeDef
87 {
88   RNG_TypeDef                 *Instance;     /*!< Register base address        */
89 
90 #if defined(RNG_CR_CED)
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 /* Exported constants --------------------------------------------------------*/
138 /** @defgroup RNG_Exported_Constants RNG Exported Constants
139   * @{
140   */
141 
142 /** @defgroup RNG_Interrupt_definition  RNG Interrupts Definition
143   * @{
144   */
145 #define RNG_IT_DRDY  RNG_SR_DRDY  /*!< Data Ready interrupt  */
146 #define RNG_IT_CEI   RNG_SR_CEIS  /*!< Clock error interrupt */
147 #define RNG_IT_SEI   RNG_SR_SEIS  /*!< Seed error interrupt  */
148 /**
149   * @}
150   */
151 
152 /** @defgroup RNG_Flag_definition  RNG Flags Definition
153   * @{
154   */
155 #define RNG_FLAG_DRDY   RNG_SR_DRDY  /*!< Data ready                 */
156 #define RNG_FLAG_CECS   RNG_SR_CECS  /*!< Clock error current status */
157 #define RNG_FLAG_SECS   RNG_SR_SECS  /*!< Seed error current status  */
158 /**
159   * @}
160   */
161 
162 #if defined(RNG_CR_CED)
163 /** @defgroup RNG_Clock_Error_Detection RNG Clock Error Detection
164   * @{
165   */
166 #define RNG_CED_ENABLE         ((uint32_t)0x00000000) /*!< Clock error detection enabled  */
167 #define RNG_CED_DISABLE        RNG_CR_CED             /*!< Clock error detection disabled */
168 /**
169   * @}
170   */
171 #endif /* defined(RNG_CR_CED) */
172 
173 /** @defgroup RNG_Error_Definition   RNG Error Definition
174   * @{
175   */
176 #define  HAL_RNG_ERROR_NONE             ((uint32_t)0x00000000U)    /*!< No error                */
177 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
178 #define  HAL_RNG_ERROR_INVALID_CALLBACK ((uint32_t)0x00000001U)    /*!< Invalid Callback error  */
179 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
180 /**
181   * @}
182   */
183 
184 /**
185   * @}
186   */
187 
188 /* Exported macros -----------------------------------------------------------*/
189 /** @defgroup RNG_Exported_Macros RNG Exported Macros
190   * @{
191   */
192 
193 /** @brief Reset RNG handle state.
194   * @param  __HANDLE__: RNG Handle
195   * @retval None
196   */
197 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
198 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__)  do{                                                   \
199                                                        (__HANDLE__)->State = HAL_RNG_STATE_RESET;       \
200                                                        (__HANDLE__)->MspInitCallback = NULL;            \
201                                                        (__HANDLE__)->MspDeInitCallback = NULL;          \
202                                                     } while(0U)
203 #else
204 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_RNG_STATE_RESET)
205 #endif /*USE_HAL_RNG_REGISTER_CALLBACKS */
206 
207 /**
208   * @brief  Enable the RNG peripheral.
209   * @param  __HANDLE__: RNG Handle
210   * @retval None
211   */
212 #define __HAL_RNG_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |=  RNG_CR_RNGEN)
213 
214 /**
215   * @brief  Disable the RNG peripheral.
216   * @param  __HANDLE__: RNG Handle
217   * @retval None
218   */
219 #define __HAL_RNG_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_RNGEN)
220 
221 /**
222   * @brief  Check whether the specified RNG flag is set or not.
223   * @param  __HANDLE__: RNG Handle
224   * @param  __FLAG__: RNG flag
225   *          This parameter can be one of the following values:
226   *            @arg RNG_FLAG_DRDY:  Data ready
227   *            @arg RNG_FLAG_CECS:  Clock error current status
228   *            @arg RNG_FLAG_SECS:  Seed error current status
229   * @retval The new state of __FLAG__ (SET or RESET).
230   */
231 #define __HAL_RNG_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
232 
233 /**
234   * @brief  Clear the selected RNG flag status.
235   * @param  __HANDLE__: RNG handle
236   * @param  __FLAG__: RNG flag to clear
237   * @note   WARNING: This is a dummy macro for HAL code alignment,
238   *         flags RNG_FLAG_DRDY, RNG_FLAG_CECS and RNG_FLAG_SECS are read-only.
239   * @retval None
240   */
241 #define __HAL_RNG_CLEAR_FLAG(__HANDLE__, __FLAG__)                      /* dummy  macro */
242 
243 /**
244   * @brief  Enable the RNG interrupt.
245   * @param  __HANDLE__: RNG Handle
246   * @retval None
247   */
248 #define __HAL_RNG_ENABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR |=  RNG_CR_IE)
249 
250 /**
251   * @brief  Disable the RNG interrupt.
252   * @param  __HANDLE__: RNG Handle
253   * @retval None
254   */
255 #define __HAL_RNG_DISABLE_IT(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_IE)
256 
257 /**
258   * @brief  Check whether the specified RNG interrupt has occurred or not.
259   * @param  __HANDLE__: RNG Handle
260   * @param  __INTERRUPT__: specifies the RNG interrupt status flag to check.
261   *         This parameter can be one of the following values:
262   *            @arg RNG_IT_DRDY: Data ready interrupt
263   *            @arg RNG_IT_CEI: Clock error interrupt
264   *            @arg RNG_IT_SEI: Seed error interrupt
265   * @retval The new state of __INTERRUPT__ (SET or RESET).
266   */
267 #define __HAL_RNG_GET_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR & (__INTERRUPT__)) == (__INTERRUPT__))
268 
269 /**
270   * @brief  Clear the RNG interrupt status flags.
271   * @param  __HANDLE__: RNG Handle
272   * @param  __INTERRUPT__: specifies the RNG interrupt status flag to clear.
273   *          This parameter can be one of the following values:
274   *            @arg RNG_IT_CEI: Clock error interrupt
275   *            @arg RNG_IT_SEI: Seed error interrupt
276   * @note   RNG_IT_DRDY flag is read-only, reading RNG_DR register automatically clears RNG_IT_DRDY.
277   * @retval None
278   */
279 #define __HAL_RNG_CLEAR_IT(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->SR) = ~(__INTERRUPT__))
280 
281 /**
282   * @}
283   */
284 
285 
286 /* Exported functions --------------------------------------------------------*/
287 /** @defgroup RNG_Exported_Functions RNG Exported Functions
288   * @{
289   */
290 
291 /* Initialization and de-initialization functions  ******************************/
292 /** @defgroup RNG_Exported_Functions_Group1 Initialization and de-initialization functions
293   * @{
294   */
295 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng);
296 HAL_StatusTypeDef HAL_RNG_DeInit (RNG_HandleTypeDef *hrng);
297 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng);
298 void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng);
299 
300 /* Callbacks Register/UnRegister functions  ***********************************/
301 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
302 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback);
303 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID);
304 
305 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback);
306 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng);
307 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
308 
309 /**
310   * @}
311   */
312 
313 /* Peripheral Control functions  ************************************************/
314 /** @defgroup RNG_Exported_Functions_Group2 Peripheral Control functions
315   * @{
316   */
317 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng);    /* Obsolete, use HAL_RNG_GenerateRandomNumber() instead    */
318 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng); /* Obsolete, use HAL_RNG_GenerateRandomNumber_IT() instead */
319 
320 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit);
321 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng);
322 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng);
323 
324 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng);
325 void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng);
326 void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef* hrng, uint32_t random32bit);
327 /**
328   * @}
329   */
330 
331 /* Peripheral State functions  **************************************************/
332 /** @defgroup RNG_Exported_Functions_Group3 Peripheral State and Error functions
333   * @{
334   */
335 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng);
336 uint32_t             HAL_RNG_GetError(RNG_HandleTypeDef *hrng);
337 /**
338   * @}
339   */
340 
341 /**
342   * @}
343   */
344 
345 /* Private types -------------------------------------------------------------*/
346 /* Private defines -----------------------------------------------------------*/
347 /* Private variables ---------------------------------------------------------*/
348 /* Private constants ---------------------------------------------------------*/
349 /* Private macros ------------------------------------------------------------*/
350 /** @addtogroup  RNG_Private_Macros   RNG Private Macros
351   * @{
352   */
353 
354 #if defined(RNG_CR_CED)
355 /**
356   * @brief Verify the RNG Clock Error Detection mode.
357   * @param __MODE__: RNG Clock Error Detection mode
358   * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
359   */
360 #define IS_RNG_CED(__MODE__) (((__MODE__) == RNG_CED_ENABLE) || \
361                               ((__MODE__) == RNG_CED_DISABLE))
362 #endif /* defined(RNG_CR_CED) */
363 
364 /**
365   * @}
366   */
367 /* Private functions prototypes ----------------------------------------------*/
368 
369 /**
370   * @}
371   */
372 
373 /**
374   * @}
375   */
376 
377 #ifdef __cplusplus
378 }
379 #endif
380 
381 #endif /* __STM32L4xx_HAL_RNG_H */
382 
383 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
384