2 ******************************************************************************
3 * @file stm32f4xx_rng.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Random Number Generator (RNG) peripheral:
9 * - Initialization and Configuration
10 * - Get 32 bit Random number
11 * - Interrupts and flags management
15 * ===================================================================
16 * How to use this driver
17 * ===================================================================
18 * 1. Enable The RNG controller clock using
19 * RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE) function.
21 * 2. Activate the RNG peripheral using RNG_Cmd() function.
23 * 3. Wait until the 32 bit Random number Generator contains a valid
24 * random data (using polling/interrupt mode). For more details,
25 * refer to "Interrupts and flags management functions" module
28 * 4. Get the 32 bit Random number using RNG_GetRandomNumber() function
30 * 5. To get another 32 bit Random number, go to step 3.
36 ******************************************************************************
39 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
40 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
41 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
42 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
43 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
44 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
46 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
47 ******************************************************************************
50 /* Includes ------------------------------------------------------------------*/
51 #include "stm32f4xx_rng.h"
52 #include "stm32f4xx_rcc.h"
54 /** @addtogroup STM32F4xx_StdPeriph_Driver
59 * @brief RNG driver modules
63 /* Private typedef -----------------------------------------------------------*/
64 /* Private define ------------------------------------------------------------*/
65 /* Private macro -------------------------------------------------------------*/
66 /* Private variables ---------------------------------------------------------*/
67 /* Private function prototypes -----------------------------------------------*/
68 /* Private functions ---------------------------------------------------------*/
70 /** @defgroup RNG_Private_Functions
74 /** @defgroup RNG_Group1 Initialization and Configuration functions
75 * @brief Initialization and Configuration functions
78 ===============================================================================
79 Initialization and Configuration functions
80 ===============================================================================
81 This section provides functions allowing to
82 - Initialize the RNG peripheral
83 - Enable or disable the RNG peripheral
90 * @brief Deinitializes the RNG peripheral registers to their default reset values.
96 /* Enable RNG reset state */
97 RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, ENABLE);
99 /* Release RNG from reset state */
100 RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, DISABLE);
104 * @brief Enables or disables the RNG peripheral.
105 * @param NewState: new state of the RNG peripheral.
106 * This parameter can be: ENABLE or DISABLE.
109 void RNG_Cmd(FunctionalState NewState)
111 /* Check the parameters */
112 assert_param(IS_FUNCTIONAL_STATE(NewState));
114 if (NewState != DISABLE)
117 RNG->CR |= RNG_CR_RNGEN;
121 /* Disable the RNG */
122 RNG->CR &= ~RNG_CR_RNGEN;
129 /** @defgroup RNG_Group2 Get 32 bit Random number function
130 * @brief Get 32 bit Random number function
134 ===============================================================================
135 Get 32 bit Random number function
136 ===============================================================================
137 This section provides a function allowing to get the 32 bit Random number
139 @note Before to call this function you have to wait till DRDY flag is set,
140 using RNG_GetFlagStatus(RNG_FLAG_DRDY) function.
148 * @brief Returns a 32-bit random number.
150 * @note Before to call this function you have to wait till DRDY (data ready)
151 * flag is set, using RNG_GetFlagStatus(RNG_FLAG_DRDY) function.
152 * @note Each time the the Random number data is read (using RNG_GetRandomNumber()
153 * function), the RNG_FLAG_DRDY flag is automatically cleared.
154 * @note In the case of a seed error, the generation of random numbers is
155 * interrupted for as long as the SECS bit is '1'. If a number is
156 * available in the RNG_DR register, it must not be used because it may
157 * not have enough entropy. In this case, it is recommended to clear the
158 * SEIS bit(using RNG_ClearFlag(RNG_FLAG_SECS) function), then disable
159 * and enable the RNG peripheral (using RNG_Cmd() function) to
160 * reinitialize and restart the RNG.
161 * @note In the case of a clock error, the RNG is no more able to generate
162 * random numbers because the PLL48CLK clock is not correct. User have
163 * to check that the clock controller is correctly configured to provide
164 * the RNG clock and clear the CEIS bit (using RNG_ClearFlag(RNG_FLAG_CECS)
165 * function) . The clock error has no impact on the previously generated
166 * random numbers, and the RNG_DR register contents can be used.
169 * @retval 32-bit random number.
171 uint32_t RNG_GetRandomNumber(void)
173 /* Return the 32 bit random number from the DR register */
182 /** @defgroup RNG_Group3 Interrupts and flags management functions
183 * @brief Interrupts and flags management functions
186 ===============================================================================
187 Interrupts and flags management functions
188 ===============================================================================
190 This section provides functions allowing to configure the RNG Interrupts and
191 to get the status and clear flags and Interrupts pending bits.
193 The RNG provides 3 Interrupts sources and 3 Flags:
197 1. RNG_FLAG_DRDY : In the case of the RNG_DR register contains valid
198 random data. it is cleared by reading the valid data
199 (using RNG_GetRandomNumber() function).
201 2. RNG_FLAG_CECS : In the case of a seed error detection.
203 3. RNG_FLAG_SECS : In the case of a clock error detection.
208 if enabled, an RNG interrupt is pending :
210 1. In the case of the RNG_DR register contains valid random data.
211 This interrupt source is cleared once the RNG_DR register has been read
212 (using RNG_GetRandomNumber() function) until a new valid value is
216 2. In the case of a seed error : One of the following faulty sequences has
218 - More than 64 consecutive bits at the same value (0 or 1)
219 - More than 32 consecutive alternance of 0 and 1 (0101010101...01)
220 This interrupt source is cleared using RNG_ClearITPendingBit(RNG_IT_SEI)
224 3. In the case of a clock error : the PLL48CLK (RNG peripheral clock source)
225 was not correctly detected (fPLL48CLK< fHCLK/16).
226 This interrupt source is cleared using RNG_ClearITPendingBit(RNG_IT_CEI)
228 @note In this case, User have to check that the clock controller is
229 correctly configured to provide the RNG clock.
231 Managing the RNG controller events :
232 ------------------------------------
233 The user should identify which mode will be used in his application to manage
234 the RNG controller events: Polling mode or Interrupt mode.
236 1. In the Polling Mode it is advised to use the following functions:
237 - RNG_GetFlagStatus() : to check if flags events occur.
238 - RNG_ClearFlag() : to clear the flags events.
240 @note RNG_FLAG_DRDY can not be cleared by RNG_ClearFlag(). it is cleared only
241 by reading the Random number data.
243 2. In the Interrupt Mode it is advised to use the following functions:
244 - RNG_ITConfig() : to enable or disable the interrupt source.
245 - RNG_GetITStatus() : to check if Interrupt occurs.
246 - RNG_ClearITPendingBit() : to clear the Interrupt pending Bit
247 (corresponding Flag).
255 * @brief Enables or disables the RNG interrupt.
256 * @note The RNG provides 3 interrupt sources,
257 * - Computed data is ready event (DRDY), and
258 * - Seed error Interrupt (SEI) and
259 * - Clock error Interrupt (CEI),
260 * all these interrupts sources are enabled by setting the IE bit in
261 * CR register. However, each interrupt have its specific status bit
262 * (see RNG_GetITStatus() function) and clear bit except the DRDY event
263 * (see RNG_ClearITPendingBit() function).
264 * @param NewState: new state of the RNG interrupt.
265 * This parameter can be: ENABLE or DISABLE.
268 void RNG_ITConfig(FunctionalState NewState)
270 /* Check the parameters */
271 assert_param(IS_FUNCTIONAL_STATE(NewState));
273 if (NewState != DISABLE)
275 /* Enable the RNG interrupt */
276 RNG->CR |= RNG_CR_IE;
280 /* Disable the RNG interrupt */
281 RNG->CR &= ~RNG_CR_IE;
286 * @brief Checks whether the specified RNG flag is set or not.
287 * @param RNG_FLAG: specifies the RNG flag to check.
288 * This parameter can be one of the following values:
289 * @arg RNG_FLAG_DRDY: Data Ready flag.
290 * @arg RNG_FLAG_CECS: Clock Error Current flag.
291 * @arg RNG_FLAG_SECS: Seed Error Current flag.
292 * @retval The new state of RNG_FLAG (SET or RESET).
294 FlagStatus RNG_GetFlagStatus(uint8_t RNG_FLAG)
296 FlagStatus bitstatus = RESET;
297 /* Check the parameters */
298 assert_param(IS_RNG_GET_FLAG(RNG_FLAG));
300 /* Check the status of the specified RNG flag */
301 if ((RNG->SR & RNG_FLAG) != (uint8_t)RESET)
303 /* RNG_FLAG is set */
308 /* RNG_FLAG is reset */
311 /* Return the RNG_FLAG status */
317 * @brief Clears the RNG flags.
318 * @param RNG_FLAG: specifies the flag to clear.
319 * This parameter can be any combination of the following values:
320 * @arg RNG_FLAG_CECS: Clock Error Current flag.
321 * @arg RNG_FLAG_SECS: Seed Error Current flag.
322 * @note RNG_FLAG_DRDY can not be cleared by RNG_ClearFlag() function.
323 * This flag is cleared only by reading the Random number data (using
324 * RNG_GetRandomNumber() function).
327 void RNG_ClearFlag(uint8_t RNG_FLAG)
329 /* Check the parameters */
330 assert_param(IS_RNG_CLEAR_FLAG(RNG_FLAG));
331 /* Clear the selected RNG flags */
332 RNG->SR = ~(uint32_t)(((uint32_t)RNG_FLAG) << 4);
336 * @brief Checks whether the specified RNG interrupt has occurred or not.
337 * @param RNG_IT: specifies the RNG interrupt source to check.
338 * This parameter can be one of the following values:
339 * @arg RNG_IT_CEI: Clock Error Interrupt.
340 * @arg RNG_IT_SEI: Seed Error Interrupt.
341 * @retval The new state of RNG_IT (SET or RESET).
343 ITStatus RNG_GetITStatus(uint8_t RNG_IT)
345 ITStatus bitstatus = RESET;
346 /* Check the parameters */
347 assert_param(IS_RNG_GET_IT(RNG_IT));
349 /* Check the status of the specified RNG interrupt */
350 if ((RNG->SR & RNG_IT) != (uint8_t)RESET)
357 /* RNG_IT is reset */
360 /* Return the RNG_IT status */
366 * @brief Clears the RNG interrupt pending bit(s).
367 * @param RNG_IT: specifies the RNG interrupt pending bit(s) to clear.
368 * This parameter can be any combination of the following values:
369 * @arg RNG_IT_CEI: Clock Error Interrupt.
370 * @arg RNG_IT_SEI: Seed Error Interrupt.
373 void RNG_ClearITPendingBit(uint8_t RNG_IT)
375 /* Check the parameters */
376 assert_param(IS_RNG_IT(RNG_IT));
378 /* Clear the selected RNG interrupt pending bit */
379 RNG->SR = (uint8_t)~RNG_IT;
399 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/