2 ******************************************************************************
3 * @file stm32f4xx_hal_rng.c
4 * @author MCD Application Team
7 * @brief RNG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Random Number Generator (RNG) peripheral:
10 * + Initialization/de-initialization functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
19 The RNG HAL driver can be used as follows:
21 (#) Enable the RNG controller clock using __RNG_CLK_ENABLE() macro.
22 (#) Activate the RNG peripheral using __HAL_RNG_ENABLE() macro.
23 (#) Wait until the 32 bit Random Number Generator contains a valid
24 random data using (polling/interrupt) mode.
25 (#) Get the 32 bit random number using HAL_RNG_GetRandomNumber() function.
28 ******************************************************************************
31 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
33 * Redistribution and use in source and binary forms, with or without modification,
34 * are permitted provided that the following conditions are met:
35 * 1. Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
39 * and/or other materials provided with the distribution.
40 * 3. Neither the name of STMicroelectronics nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
50 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 ******************************************************************************
58 /* Includes ------------------------------------------------------------------*/
59 #include "stm32f4xx_hal.h"
61 /** @addtogroup STM32F4xx_HAL_Driver
66 * @brief RNG HAL module driver.
70 #ifdef HAL_RNG_MODULE_ENABLED
72 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
73 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx)
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 #define RNG_TIMEOUT_VALUE 1000
78 /* Private macro -------------------------------------------------------------*/
79 /* Private variables ---------------------------------------------------------*/
80 /* Private function prototypes -----------------------------------------------*/
81 /* Private functions ---------------------------------------------------------*/
83 /** @defgroup RNG_Private_Functions
87 /** @defgroup RNG_Group1 Initialization and de-initialization functions
88 * @brief Initialization and Configuration functions.
91 ===============================================================================
92 ##### Initialization and de-initialization functions #####
93 ===============================================================================
94 [..] This section provides functions allowing to:
95 (+) Initialize the RNG according to the specified parameters
96 in the RNG_InitTypeDef and create the associated handle
97 (+) DeInitialize the RNG peripheral
98 (+) Initialize the RNG MSP
99 (+) DeInitialize RNG MSP
106 * @brief Initializes the RNG according to the specified
107 * parameters in the RNG_InitTypeDef and creates the associated handle.
108 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
109 * the configuration information for RNG.
112 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
114 /* Check the RNG handle allocation */
120 if(hrng->State == HAL_RNG_STATE_RESET)
122 /* Init the low level hardware */
123 HAL_RNG_MspInit(hrng);
125 /* Change RNG peripheral state */
126 hrng->State = HAL_RNG_STATE_BUSY;
128 /* Enable the RNG Peripheral */
129 __HAL_RNG_ENABLE(hrng);
131 /* Initialize the RNG state */
132 hrng->State = HAL_RNG_STATE_READY;
134 /* Return function status */
139 * @brief DeInitializes the RNG peripheral.
140 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
141 * the configuration information for RNG.
144 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
146 /* Check the RNG peripheral state */
147 if(hrng->State == HAL_RNG_STATE_BUSY)
152 /* Update the RNG state */
153 hrng->State = HAL_RNG_STATE_BUSY;
155 /* Disable the RNG Peripheral */
156 __HAL_RNG_DISABLE(hrng);
158 /* Set the RNG registers to their reset values */
159 hrng->Instance->CR &= 0xFFFFFFF3;
160 hrng->Instance->SR &= 0xFFFFFF98;
161 hrng->Instance->DR &= 0x0;
163 /* DeInit the low level hardware */
164 HAL_RNG_MspDeInit(hrng);
166 /* Update the RNG state */
167 hrng->State = HAL_RNG_STATE_RESET;
172 /* Return the function status */
177 * @brief Initializes the RNG MSP.
178 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
179 * the configuration information for RNG.
182 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
184 /* NOTE : This function Should not be modified, when the callback is needed,
185 the HAL_RNG_MspInit could be implemented in the user file
190 * @brief DeInitializes the RNG MSP.
191 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
192 * the configuration information for RNG.
195 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
197 /* NOTE : This function Should not be modified, when the callback is needed,
198 the HAL_RNG_MspDeInit could be implemented in the user file
206 /** @defgroup RNG_Group2 Peripheral Control functions
207 * @brief management functions.
210 ===============================================================================
211 ##### Peripheral Control functions #####
212 ===============================================================================
213 [..] This section provides functions allowing to:
214 (+) Get the 32 bit Random number
215 (+) Get the 32 bit Random number with interrupt enabled
216 (+) Handle RNG interrupt request
224 * @brief Returns a 32-bit random number.
225 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
226 * is automatically cleared.
227 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
228 * the configuration information for RNG.
229 * @retval 32-bit random number
231 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
233 uint32_t random32bit = 0;
234 uint32_t tickstart = 0;
240 tickstart = HAL_GetTick();
242 /* Check if data register contains valid random data */
243 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
245 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
251 /* Get a 32bit Random number */
252 random32bit = hrng->Instance->DR;
254 /* Process Unlocked */
257 /* Return the 32 bit random number */
262 * @brief Returns a 32-bit random number with interrupt enabled.
263 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
264 * the configuration information for RNG.
265 * @retval 32-bit random number
267 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
269 uint32_t random32bit = 0;
274 /* Change RNG peripheral state */
275 hrng->State = HAL_RNG_STATE_BUSY;
277 /* Get a 32bit Random number */
278 random32bit = hrng->Instance->DR;
280 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
281 __HAL_RNG_ENABLE_IT(hrng);
283 /* Return the 32 bit random number */
288 * @brief Handles RNG interrupt request.
289 * @note In the case of a clock error, the RNG is no more able to generate
290 * random numbers because the PLL48CLK clock is not correct. User has
291 * to check that the clock controller is correctly configured to provide
292 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_FLAG().
293 * The clock error has no impact on the previously generated
294 * random numbers, and the RNG_DR register contents can be used.
295 * @note In the case of a seed error, the generation of random numbers is
296 * interrupted as long as the SECS bit is '1'. If a number is
297 * available in the RNG_DR register, it must not be used because it may
298 * not have enough entropy. In this case, it is recommended to clear the
299 * SEIS bit using __HAL_RNG_CLEAR_FLAG(), then disable and enable
300 * the RNG peripheral to reinitialize and restart the RNG.
301 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
302 * the configuration information for RNG.
306 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
308 /* RNG clock error interrupt occurred */
309 if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_CEI) != RESET)
311 HAL_RNG_ErrorCallback(hrng);
313 /* Clear the clock error flag */
314 __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_CEI);
316 /* Change RNG peripheral state */
317 hrng->State = HAL_RNG_STATE_ERROR;
319 /* Process Unlocked */
323 /* RNG seed error interrupt occurred */
324 if(__HAL_RNG_GET_FLAG(hrng, RNG_IT_SEI) != RESET)
326 HAL_RNG_ErrorCallback(hrng);
328 /* Clear the seed error flag */
329 __HAL_RNG_CLEAR_FLAG(hrng, RNG_IT_SEI);
331 /* Change RNG peripheral state */
332 hrng->State = HAL_RNG_STATE_ERROR;
334 /* Process Unlocked */
338 /* Check RNG data ready flag */
339 if(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) != RESET)
341 /* Data Ready callback */
342 HAL_RNG_ReadyCallback(hrng);
344 /* Change RNG peripheral state */
345 hrng->State = HAL_RNG_STATE_READY;
347 /* Clear the RNG Data Ready flag */
348 __HAL_RNG_CLEAR_FLAG(hrng, RNG_FLAG_DRDY);
350 /* Process Unlocked */
356 * @brief Data Ready callback in non-blocking mode.
357 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
358 * the configuration information for RNG.
362 __weak void HAL_RNG_ReadyCallback(RNG_HandleTypeDef* hrng)
364 /* NOTE : This function Should not be modified, when the callback is needed,
365 the HAL_RNG_ReadyCallback could be implemented in the user file
370 * @brief RNG error callbacks.
371 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
372 * the configuration information for RNG.
375 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
377 /* NOTE : This function Should not be modified, when the callback is needed,
378 the HAL_RNG_ErrorCallback could be implemented in the user file
386 /** @defgroup RNG_Group3 Peripheral State functions
387 * @brief Peripheral State functions.
390 ===============================================================================
391 ##### Peripheral State functions #####
392 ===============================================================================
394 This subsection permits to get in run-time the status of the peripheral
402 * @brief Returns the RNG state.
403 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
404 * the configuration information for RNG.
407 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
420 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx */
422 #endif /* HAL_RNG_MODULE_ENABLED */
431 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/