2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_rng.c
\r
4 * @author MCD Application Team
\r
7 * @brief RNG HAL module driver.
\r
8 * This file provides firmware functions to manage the following
\r
9 * functionalities of the Random Number Generator (RNG) peripheral:
\r
10 * + Initialization/de-initialization functions
\r
11 * + Peripheral Control functions
\r
12 * + Peripheral State functions
\r
15 ==============================================================================
\r
16 ##### How to use this driver #####
\r
17 ==============================================================================
\r
19 The RNG HAL driver can be used as follows:
\r
21 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
\r
22 in HAL_RNG_MspInit().
\r
23 (#) Activate the RNG peripheral using HAL_RNG_Init() function.
\r
24 (#) Wait until the 32 bit Random Number Generator contains a valid
\r
25 random data using (polling/interrupt) mode.
\r
26 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
\r
29 ******************************************************************************
\r
32 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
\r
34 * Redistribution and use in source and binary forms, with or without modification,
\r
35 * are permitted provided that the following conditions are met:
\r
36 * 1. Redistributions of source code must retain the above copyright notice,
\r
37 * this list of conditions and the following disclaimer.
\r
38 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
39 * this list of conditions and the following disclaimer in the documentation
\r
40 * and/or other materials provided with the distribution.
\r
41 * 3. Neither the name of STMicroelectronics nor the names of its contributors
\r
42 * may be used to endorse or promote products derived from this software
\r
43 * without specific prior written permission.
\r
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
48 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
\r
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
\r
51 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
\r
52 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
\r
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
\r
54 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
56 ******************************************************************************
\r
59 /* Includes ------------------------------------------------------------------*/
\r
60 #include "stm32f7xx_hal.h"
\r
62 /** @addtogroup STM32F7xx_HAL_Driver
\r
66 /** @addtogroup RNG
\r
70 #ifdef HAL_RNG_MODULE_ENABLED
\r
72 /* Private types -------------------------------------------------------------*/
\r
73 /* Private defines -----------------------------------------------------------*/
\r
74 /* Private variables ---------------------------------------------------------*/
\r
75 /* Private constants ---------------------------------------------------------*/
\r
76 /** @addtogroup RNG_Private_Constants
\r
79 #define RNG_TIMEOUT_VALUE 2
\r
83 /* Private macros ------------------------------------------------------------*/
\r
84 /* Private functions prototypes ----------------------------------------------*/
\r
85 /* Private functions ---------------------------------------------------------*/
\r
86 /* Exported functions --------------------------------------------------------*/
\r
88 /** @addtogroup RNG_Exported_Functions
\r
92 /** @addtogroup RNG_Exported_Functions_Group1
\r
93 * @brief Initialization and de-initialization functions
\r
96 ===============================================================================
\r
97 ##### Initialization and de-initialization functions #####
\r
98 ===============================================================================
\r
99 [..] This section provides functions allowing to:
\r
100 (+) Initialize the RNG according to the specified parameters
\r
101 in the RNG_InitTypeDef and create the associated handle
\r
102 (+) DeInitialize the RNG peripheral
\r
103 (+) Initialize the RNG MSP
\r
104 (+) DeInitialize RNG MSP
\r
111 * @brief Initializes the RNG peripheral and creates the associated handle.
\r
112 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
113 * the configuration information for RNG.
\r
114 * @retval HAL status
\r
116 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
\r
118 /* Check the RNG handle allocation */
\r
126 if(hrng->State == HAL_RNG_STATE_RESET)
\r
128 /* Allocate lock resource and initialize it */
\r
129 hrng->Lock = HAL_UNLOCKED;
\r
131 /* Init the low level hardware */
\r
132 HAL_RNG_MspInit(hrng);
\r
135 /* Change RNG peripheral state */
\r
136 hrng->State = HAL_RNG_STATE_BUSY;
\r
138 /* Enable the RNG Peripheral */
\r
139 __HAL_RNG_ENABLE(hrng);
\r
141 /* Initialize the RNG state */
\r
142 hrng->State = HAL_RNG_STATE_READY;
\r
144 __HAL_UNLOCK(hrng);
\r
146 /* Return function status */
\r
151 * @brief DeInitializes the RNG peripheral.
\r
152 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
153 * the configuration information for RNG.
\r
154 * @retval HAL status
\r
156 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
\r
158 /* Check the RNG handle allocation */
\r
163 /* Disable the RNG Peripheral */
\r
164 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
\r
166 /* Clear RNG interrupt status flags */
\r
167 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
\r
169 /* DeInit the low level hardware */
\r
170 HAL_RNG_MspDeInit(hrng);
\r
172 /* Update the RNG state */
\r
173 hrng->State = HAL_RNG_STATE_RESET;
\r
176 __HAL_UNLOCK(hrng);
\r
178 /* Return the function status */
\r
183 * @brief Initializes the RNG MSP.
\r
184 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
185 * the configuration information for RNG.
\r
188 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
\r
190 /* NOTE : This function should not be modified. When the callback is needed,
\r
191 function HAL_RNG_MspInit must be implemented in the user file.
\r
196 * @brief DeInitializes the RNG MSP.
\r
197 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
198 * the configuration information for RNG.
\r
201 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
\r
203 /* NOTE : This function should not be modified. When the callback is needed,
\r
204 function HAL_RNG_MspDeInit must be implemented in the user file.
\r
212 /** @addtogroup RNG_Exported_Functions_Group2
\r
213 * @brief Peripheral Control functions
\r
216 ===============================================================================
\r
217 ##### Peripheral Control functions #####
\r
218 ===============================================================================
\r
219 [..] This section provides functions allowing to:
\r
220 (+) Get the 32 bit Random number
\r
221 (+) Get the 32 bit Random number with interrupt enabled
\r
222 (+) Handle RNG interrupt request
\r
229 * @brief Generates a 32-bit random number.
\r
230 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
\r
231 * is automatically cleared.
\r
232 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
233 * the configuration information for RNG.
\r
234 * @param random32bit: pointer to generated random number variable if successful.
\r
235 * @retval HAL status
\r
238 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
\r
240 uint32_t tickstart = 0;
\r
241 HAL_StatusTypeDef status = HAL_OK;
\r
243 /* Process Locked */
\r
246 /* Check RNG peripheral state */
\r
247 if(hrng->State == HAL_RNG_STATE_READY)
\r
249 /* Change RNG peripheral state */
\r
250 hrng->State = HAL_RNG_STATE_BUSY;
\r
253 tickstart = HAL_GetTick();
\r
255 /* Check if data register contains valid random data */
\r
256 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
\r
258 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
\r
260 hrng->State = HAL_RNG_STATE_ERROR;
\r
262 /* Process Unlocked */
\r
263 __HAL_UNLOCK(hrng);
\r
265 return HAL_TIMEOUT;
\r
269 /* Get a 32bit Random number */
\r
270 hrng->RandomNumber = hrng->Instance->DR;
\r
271 *random32bit = hrng->RandomNumber;
\r
273 hrng->State = HAL_RNG_STATE_READY;
\r
277 status = HAL_ERROR;
\r
280 /* Process Unlocked */
\r
281 __HAL_UNLOCK(hrng);
\r
287 * @brief Generates a 32-bit random number in interrupt mode.
\r
288 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
289 * the configuration information for RNG.
\r
290 * @retval HAL status
\r
292 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
\r
294 HAL_StatusTypeDef status = HAL_OK;
\r
296 /* Process Locked */
\r
299 /* Check RNG peripheral state */
\r
300 if(hrng->State == HAL_RNG_STATE_READY)
\r
302 /* Change RNG peripheral state */
\r
303 hrng->State = HAL_RNG_STATE_BUSY;
\r
305 /* Process Unlocked */
\r
306 __HAL_UNLOCK(hrng);
\r
308 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
\r
309 __HAL_RNG_ENABLE_IT(hrng);
\r
313 /* Process Unlocked */
\r
314 __HAL_UNLOCK(hrng);
\r
316 status = HAL_ERROR;
\r
323 * @brief Handles RNG interrupt request.
\r
324 * @note In the case of a clock error, the RNG is no more able to generate
\r
325 * random numbers because the PLL48CLK clock is not correct. User has
\r
326 * to check that the clock controller is correctly configured to provide
\r
327 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
\r
328 * The clock error has no impact on the previously generated
\r
329 * random numbers, and the RNG_DR register contents can be used.
\r
330 * @note In the case of a seed error, the generation of random numbers is
\r
331 * interrupted as long as the SECS bit is '1'. If a number is
\r
332 * available in the RNG_DR register, it must not be used because it may
\r
333 * not have enough entropy. In this case, it is recommended to clear the
\r
334 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
\r
335 * the RNG peripheral to reinitialize and restart the RNG.
\r
336 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
\r
337 * or CEIS are set.
\r
338 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
339 * the configuration information for RNG.
\r
343 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
\r
345 /* RNG clock error interrupt occurred */
\r
346 if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
\r
348 /* Change RNG peripheral state */
\r
349 hrng->State = HAL_RNG_STATE_ERROR;
\r
351 HAL_RNG_ErrorCallback(hrng);
\r
353 /* Clear the clock error flag */
\r
354 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
\r
358 /* Check RNG data ready interrupt occurred */
\r
359 if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
\r
361 /* Generate random number once, so disable the IT */
\r
362 __HAL_RNG_DISABLE_IT(hrng);
\r
364 /* Get the 32bit Random number (DRDY flag automatically cleared) */
\r
365 hrng->RandomNumber = hrng->Instance->DR;
\r
367 if(hrng->State != HAL_RNG_STATE_ERROR)
\r
369 /* Change RNG peripheral state */
\r
370 hrng->State = HAL_RNG_STATE_READY;
\r
372 /* Data Ready callback */
\r
373 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
\r
379 * @brief Returns generated random number in polling mode (Obsolete)
\r
380 * Use HAL_RNG_GenerateRandomNumber() API instead.
\r
381 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
382 * the configuration information for RNG.
\r
383 * @retval Random value
\r
385 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
\r
387 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
\r
389 return hrng->RandomNumber;
\r
398 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
\r
399 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
\r
400 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
401 * the configuration information for RNG.
\r
402 * @retval 32-bit random number
\r
404 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
\r
406 uint32_t random32bit = 0;
\r
408 /* Process locked */
\r
411 /* Change RNG peripheral state */
\r
412 hrng->State = HAL_RNG_STATE_BUSY;
\r
414 /* Get a 32bit Random number */
\r
415 random32bit = hrng->Instance->DR;
\r
417 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
\r
418 __HAL_RNG_ENABLE_IT(hrng);
\r
420 /* Return the 32 bit random number */
\r
421 return random32bit;
\r
425 * @brief Read latest generated random number.
\r
426 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
427 * the configuration information for RNG.
\r
428 * @retval random value
\r
430 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
\r
432 return(hrng->RandomNumber);
\r
436 * @brief Data Ready callback in non-blocking mode.
\r
437 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
438 * the configuration information for RNG.
\r
439 * @param random32bit: generated random number.
\r
442 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
\r
444 /* NOTE : This function should not be modified. When the callback is needed,
\r
445 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
\r
450 * @brief RNG error callbacks.
\r
451 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
452 * the configuration information for RNG.
\r
455 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
\r
457 /* NOTE : This function should not be modified. When the callback is needed,
\r
458 function HAL_RNG_ErrorCallback must be implemented in the user file.
\r
466 /** @addtogroup RNG_Exported_Functions_Group3
\r
467 * @brief Peripheral State functions
\r
470 ===============================================================================
\r
471 ##### Peripheral State functions #####
\r
472 ===============================================================================
\r
474 This subsection permits to get in run-time the status of the peripheral
\r
482 * @brief Returns the RNG state.
\r
483 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
484 * the configuration information for RNG.
\r
485 * @retval HAL state
\r
487 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
\r
489 return hrng->State;
\r
500 #endif /* HAL_RNG_MODULE_ENABLED */
\r
510 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r