1 /**
2   ******************************************************************************
3   * @file    stm32wb0x_hal_rng.h
4   * @author  MCD Application Team
5   * @brief   Header file of RNG HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2024 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 STM32WB0x_HAL_RNG_H
21 #define STM32WB0x_HAL_RNG_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 #include "stm32wb0x_hal_def.h"
29 
30 /** @addtogroup STM32WB0x_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 not use for WB09 product */
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 typedef struct
80 {
81   RNG_TypeDef                 *Instance;    /*!< Register base address   */
82 
83   RNG_InitTypeDef             Init;         /*!< RNG configuration parameters */
84 
85   HAL_LockTypeDef             Lock;         /*!< RNG locking object      */
86 
87   __IO HAL_RNG_StateTypeDef   State;        /*!< RNG communication state */
88 
89   __IO  uint32_t              ErrorCode;    /*!< RNG Error code          */
90 
91   uint32_t                    RandomNumber; /*!< Last Generated RNG Data */
92 
93 } RNG_HandleTypeDef;
94 
95 /**
96   * @}
97   */
98 
99 /**
100   * @}
101   */
102 
103 /* Exported constants --------------------------------------------------------*/
104 
105 /** @defgroup RNG_Exported_Constants_Group1 RNG Flag definition
106   * @{
107   */
108 #if defined (RNG_SR_RNGRDY)
109 #define RNG_FLAG_DRDY   RNG_SR_RNGRDY  /*!< New Random Data Ready */
110 #else
111 #define RNG_FLAG_DRDY   RNG_SR_VAL_READY  /*!< New Random Data Ready */
112 #endif /*RNG_SR_RNGRDY*/
113 #if defined (RNG_SR_REVCLK)
114 #define RNG_FLAG_REVCLK RNG_SR_REVCLK  /*!< Clock Detection flag  */
115 #endif /*RNG_SR_REVCLK*/
116 #if defined (RNG_SR_FAULT)
117 #define RNG_FLAG_FAULT  RNG_SR_FAULT   /*!< Fault flag            */
118 #endif /*RNG_SR_FAULT*/
119 /**
120   * @}
121   */
122 /** @defgroup RNG_Exported_Constants_Group2 RNG Clock Error Detection
123   * @{
124   */
125 #if defined (RNG_CR_TST_CLK)
126 #define RNG_CED_DISABLE          0x00000000U     /*!< Clock error detection Disabled */
127 #define RNG_CED_ENABLE           RNG_CR_TST_CLK  /*!< Clock error detection Enabled */
128 #endif /*RNG_CR_TST_CLK*/
129 
130 /** @defgroup RNG_Error_Definition   RNG Error Definition
131   * @{
132   */
133 #define  HAL_RNG_ERROR_NONE             0x00000000U    /*!< No error          */
134 #define  HAL_RNG_ERROR_TIMEOUT          0x00000002U    /*!< Timeout error     */
135 #define  HAL_RNG_ERROR_BUSY             0x00000004U    /*!< Busy error        */
136 #if defined (RNG_CR_TST_CLK)
137 #define  HAL_RNG_ERROR_SEQUENCE         0x00000001U    /*!< This bit is set by hardware when a faulty sequence of bits occurs */
138 #endif /* RNG_CR_TST_CLK */
139 /**
140   * @}
141   */
142 
143 /**
144   * @}
145   */
146 
147 /* Exported macros -----------------------------------------------------------*/
148 /** @defgroup RNG_Exported_Macros RNG Exported Macros
149   * @{
150   */
151 
152 /** @brief Reset RNG handle state
153   * @param  __HANDLE__ RNG Handle
154   * @retval None
155   */
156 #define __HAL_RNG_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_RNG_STATE_RESET)
157 
158 /**
159   * @brief  Enables the RNG peripheral.
160   * @param  __HANDLE__ RNG Handle
161   * @retval None
162   */
163 #if defined (RNG_CR_RNG_DIS)
164 #define __HAL_RNG_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_RNG_DIS)
165 #else
166 #define __HAL_RNG_ENABLE(__HANDLE__) ((__HANDLE__)->Instance->CR &= ~RNG_CR_DISABLE)
167 #endif /* RNG_CR_RNG_DIS */
168 
169 /**
170   * @brief  Disables the RNG peripheral.
171   * @param  __HANDLE__ RNG Handle
172   * @retval None
173   */
174 #if defined (RNG_CR_RNG_DIS)
175 #define __HAL_RNG_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= RNG_CR_RNG_DIS)
176 #else
177 #define __HAL_RNG_DISABLE(__HANDLE__) ((__HANDLE__)->Instance->CR |= RNG_CR_DISABLE)
178 #endif /* RNG_CR_RNG_DIS */
179 
180 /**
181   * @brief  Check the selected RNG flag status.
182   * @param  __HANDLE__ RNG Handle
183   * @param  __FLAG__ RNG flag
184   *          This parameter can be one of the following values:
185   *            @arg RNG_FLAG_DRDY:  Data ready
186   *            @arg RNG_FLAG_CECS:  Clock error current status
187   *            @arg RNG_FLAG_SECS:  Seed error current status
188   * @retval The new state of __FLAG__ (SET or RESET).
189   */
190 #define __HAL_RNG_GET_FLAG(__HANDLE__, __FLAG__) (((__HANDLE__)->Instance->SR & (__FLAG__)) == (__FLAG__))
191 
192 #if defined (RNG_SR_FAULT)
193 /**
194   * @brief  Clears the selected RNG flag status.
195   * @param  __HANDLE__ RNG handle
196   * @param  __FLAG__ RNG flag to clear
197   * @note   WARNING: This macro clear only RNG_FLAG_FAULT flag because
198   *         flags RNG_FLAG_DRDY and RNG_FLAG_REVCLK are read-only.
199   * @retval None
200   */
201 #define __HAL_RNG_CLEAR_FLAG(__HANDLE__, __FLAG__)  (if((__FLAG__)\
202                                                         ==  RNG_FLAG_FAULT) (__HANDLE__)->Instance->SR = RNG_FLAG_FAULT)
203 #endif /* RNG_SR_FAULT */
204 
205 
206 /**
207   * @}
208   */
209 
210 /* Exported functions --------------------------------------------------------*/
211 /** @defgroup RNG_Exported_Functions RNG Exported Functions
212   * @{
213   */
214 
215 /** @defgroup RNG_Exported_Functions_Group1 Initialization and configuration functions
216   * @{
217   */
218 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng);
219 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng);
220 void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng);
221 void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng);
222 
223 /**
224   * @}
225   */
226 
227 /** @defgroup RNG_Exported_Functions_Group2 Peripheral Control functions
228   * @{
229   */
230 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random_number);
231 uint32_t HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef *hrng);
232 
233 /**
234   * @}
235   */
236 
237 /** @defgroup RNG_Exported_Functions_Group3 Peripheral State functions
238   * @{
239   */
240 HAL_RNG_StateTypeDef HAL_RNG_GetState(const RNG_HandleTypeDef *hrng);
241 uint32_t             HAL_RNG_GetError(const RNG_HandleTypeDef *hrng);
242 /**
243   * @}
244   */
245 
246 /**
247   * @}
248   */
249 
250 /* Private macros ------------------------------------------------------------*/
251 /** @defgroup RNG_Private_Macros RNG Private Macros
252   * @{
253   */
254 #if defined (RNG_SR_FAULT)
255 #define IS_RNG_FLAG(FLAG) (((FLAG) == RNG_FLAG_DRDY) || \
256                            ((FLAG) == RNG_FLAG_REVCLK) || \
257                            ((FLAG) == RNG_FLAG_FAULT))
258 #else
259 #define IS_RNG_FLAG(FLAG) (((FLAG) == RNG_FLAG_DRDY)
260 #endif /* RNG_SR_FAULT */
261 
262 #if defined(RNG_CR_TST_CLK)
263 /**
264   * @brief Verify the RNG Clock Error Detection mode.
265   * @param __MODE__ RNG Clock Error Detection mode
266   * @retval SET (__MODE__ is valid) or RESET (__MODE__ is invalid)
267   */
268 #define IS_RNG_CED(__MODE__)   (((__MODE__) == RNG_CED_ENABLE) || \
269                                 ((__MODE__) == RNG_CED_DISABLE))
270 #endif /* RNG_CR_TST_CLK */
271 /**
272   * @}
273   */
274 
275 /**
276   * @}
277   */
278 
279 #endif /* RNG */
280 
281 /**
282   * @}
283   */
284 
285 #ifdef __cplusplus
286 }
287 #endif
288 
289 
290 #endif /* STM32WB0x_HAL_RNG_H */
291