2 ******************************************************************************
\r
3 * @file stm32f7xx_hal_rng.c
\r
4 * @author MCD Application Team
\r
6 * @date 24-March-2015
\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 #if defined(STM32F746xx) || defined(STM32F756xx)
\r
75 /* Private types -------------------------------------------------------------*/
\r
76 /* Private defines -----------------------------------------------------------*/
\r
77 /* Private variables ---------------------------------------------------------*/
\r
78 /* Private constants ---------------------------------------------------------*/
\r
79 /** @addtogroup RNG_Private_Constants
\r
82 #define RNG_TIMEOUT_VALUE 2
\r
86 /* Private macros ------------------------------------------------------------*/
\r
87 /* Private functions prototypes ----------------------------------------------*/
\r
88 /* Private functions ---------------------------------------------------------*/
\r
89 /* Exported functions --------------------------------------------------------*/
\r
91 /** @addtogroup RNG_Exported_Functions
\r
95 /** @addtogroup RNG_Exported_Functions_Group1
\r
96 * @brief Initialization and de-initialization functions
\r
99 ===============================================================================
\r
100 ##### Initialization and de-initialization functions #####
\r
101 ===============================================================================
\r
102 [..] This section provides functions allowing to:
\r
103 (+) Initialize the RNG according to the specified parameters
\r
104 in the RNG_InitTypeDef and create the associated handle
\r
105 (+) DeInitialize the RNG peripheral
\r
106 (+) Initialize the RNG MSP
\r
107 (+) DeInitialize RNG MSP
\r
114 * @brief Initializes the RNG peripheral and creates the associated handle.
\r
115 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
116 * the configuration information for RNG.
\r
117 * @retval HAL status
\r
119 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
\r
121 /* Check the RNG handle allocation */
\r
129 if(hrng->State == HAL_RNG_STATE_RESET)
\r
131 /* Allocate lock resource and initialize it */
\r
132 hrng->Lock = HAL_UNLOCKED;
\r
134 /* Init the low level hardware */
\r
135 HAL_RNG_MspInit(hrng);
\r
138 /* Change RNG peripheral state */
\r
139 hrng->State = HAL_RNG_STATE_BUSY;
\r
141 /* Enable the RNG Peripheral */
\r
142 __HAL_RNG_ENABLE(hrng);
\r
144 /* Initialize the RNG state */
\r
145 hrng->State = HAL_RNG_STATE_READY;
\r
147 __HAL_UNLOCK(hrng);
\r
149 /* Return function status */
\r
154 * @brief DeInitializes the RNG peripheral.
\r
155 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
156 * the configuration information for RNG.
\r
157 * @retval HAL status
\r
159 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
\r
161 /* Check the RNG handle allocation */
\r
166 /* Disable the RNG Peripheral */
\r
167 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
\r
169 /* Clear RNG interrupt status flags */
\r
170 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
\r
172 /* DeInit the low level hardware */
\r
173 HAL_RNG_MspDeInit(hrng);
\r
175 /* Update the RNG state */
\r
176 hrng->State = HAL_RNG_STATE_RESET;
\r
179 __HAL_UNLOCK(hrng);
\r
181 /* Return the function status */
\r
186 * @brief Initializes the RNG MSP.
\r
187 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
188 * the configuration information for RNG.
\r
191 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
\r
193 /* NOTE : This function should not be modified. When the callback is needed,
\r
194 function HAL_RNG_MspInit must be implemented in the user file.
\r
199 * @brief DeInitializes the RNG MSP.
\r
200 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
201 * the configuration information for RNG.
\r
204 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
\r
206 /* NOTE : This function should not be modified. When the callback is needed,
\r
207 function HAL_RNG_MspDeInit must be implemented in the user file.
\r
215 /** @addtogroup RNG_Exported_Functions_Group2
\r
216 * @brief Peripheral Control functions
\r
219 ===============================================================================
\r
220 ##### Peripheral Control functions #####
\r
221 ===============================================================================
\r
222 [..] This section provides functions allowing to:
\r
223 (+) Get the 32 bit Random number
\r
224 (+) Get the 32 bit Random number with interrupt enabled
\r
225 (+) Handle RNG interrupt request
\r
232 * @brief Generates a 32-bit random number.
\r
233 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
\r
234 * is automatically cleared.
\r
235 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
236 * the configuration information for RNG.
\r
237 * @param random32bit: pointer to generated random number variable if successful.
\r
238 * @retval HAL status
\r
241 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
\r
243 uint32_t tickstart = 0;
\r
244 HAL_StatusTypeDef status = HAL_OK;
\r
246 /* Process Locked */
\r
249 /* Check RNG peripheral state */
\r
250 if(hrng->State == HAL_RNG_STATE_READY)
\r
252 /* Change RNG peripheral state */
\r
253 hrng->State = HAL_RNG_STATE_BUSY;
\r
256 tickstart = HAL_GetTick();
\r
258 /* Check if data register contains valid random data */
\r
259 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
\r
261 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
\r
263 hrng->State = HAL_RNG_STATE_ERROR;
\r
265 /* Process Unlocked */
\r
266 __HAL_UNLOCK(hrng);
\r
268 return HAL_TIMEOUT;
\r
272 /* Get a 32bit Random number */
\r
273 hrng->RandomNumber = hrng->Instance->DR;
\r
274 *random32bit = hrng->RandomNumber;
\r
276 hrng->State = HAL_RNG_STATE_READY;
\r
280 status = HAL_ERROR;
\r
283 /* Process Unlocked */
\r
284 __HAL_UNLOCK(hrng);
\r
290 * @brief Generates a 32-bit random number in interrupt mode.
\r
291 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
292 * the configuration information for RNG.
\r
293 * @retval HAL status
\r
295 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
\r
297 HAL_StatusTypeDef status = HAL_OK;
\r
299 /* Process Locked */
\r
302 /* Check RNG peripheral state */
\r
303 if(hrng->State == HAL_RNG_STATE_READY)
\r
305 /* Change RNG peripheral state */
\r
306 hrng->State = HAL_RNG_STATE_BUSY;
\r
308 /* Process Unlocked */
\r
309 __HAL_UNLOCK(hrng);
\r
311 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
\r
312 __HAL_RNG_ENABLE_IT(hrng);
\r
316 /* Process Unlocked */
\r
317 __HAL_UNLOCK(hrng);
\r
319 status = HAL_ERROR;
\r
326 * @brief Handles RNG interrupt request.
\r
327 * @note In the case of a clock error, the RNG is no more able to generate
\r
328 * random numbers because the PLL48CLK clock is not correct. User has
\r
329 * to check that the clock controller is correctly configured to provide
\r
330 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
\r
331 * The clock error has no impact on the previously generated
\r
332 * random numbers, and the RNG_DR register contents can be used.
\r
333 * @note In the case of a seed error, the generation of random numbers is
\r
334 * interrupted as long as the SECS bit is '1'. If a number is
\r
335 * available in the RNG_DR register, it must not be used because it may
\r
336 * not have enough entropy. In this case, it is recommended to clear the
\r
337 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
\r
338 * the RNG peripheral to reinitialize and restart the RNG.
\r
339 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
\r
340 * or CEIS are set.
\r
341 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
342 * the configuration information for RNG.
\r
346 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
\r
348 /* RNG clock error interrupt occurred */
\r
349 if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
\r
351 /* Change RNG peripheral state */
\r
352 hrng->State = HAL_RNG_STATE_ERROR;
\r
354 HAL_RNG_ErrorCallback(hrng);
\r
356 /* Clear the clock error flag */
\r
357 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
\r
361 /* Check RNG data ready interrupt occurred */
\r
362 if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
\r
364 /* Generate random number once, so disable the IT */
\r
365 __HAL_RNG_DISABLE_IT(hrng);
\r
367 /* Get the 32bit Random number (DRDY flag automatically cleared) */
\r
368 hrng->RandomNumber = hrng->Instance->DR;
\r
370 if(hrng->State != HAL_RNG_STATE_ERROR)
\r
372 /* Change RNG peripheral state */
\r
373 hrng->State = HAL_RNG_STATE_READY;
\r
375 /* Data Ready callback */
\r
376 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
\r
382 * @brief Returns generated random number in polling mode (Obsolete)
\r
383 * Use HAL_RNG_GenerateRandomNumber() API instead.
\r
384 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
385 * the configuration information for RNG.
\r
386 * @retval Random value
\r
388 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
\r
390 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
\r
392 return hrng->RandomNumber;
\r
401 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
\r
402 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
\r
403 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
404 * the configuration information for RNG.
\r
405 * @retval 32-bit random number
\r
407 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
\r
409 uint32_t random32bit = 0;
\r
411 /* Process locked */
\r
414 /* Change RNG peripheral state */
\r
415 hrng->State = HAL_RNG_STATE_BUSY;
\r
417 /* Get a 32bit Random number */
\r
418 random32bit = hrng->Instance->DR;
\r
420 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
\r
421 __HAL_RNG_ENABLE_IT(hrng);
\r
423 /* Return the 32 bit random number */
\r
424 return random32bit;
\r
428 * @brief Read latest generated random number.
\r
429 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
430 * the configuration information for RNG.
\r
431 * @retval random value
\r
433 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
\r
435 return(hrng->RandomNumber);
\r
439 * @brief Data Ready callback in non-blocking mode.
\r
440 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
441 * the configuration information for RNG.
\r
442 * @param random32bit: generated random number.
\r
445 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
\r
447 /* NOTE : This function should not be modified. When the callback is needed,
\r
448 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
\r
453 * @brief RNG error callbacks.
\r
454 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
455 * the configuration information for RNG.
\r
458 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
\r
460 /* NOTE : This function should not be modified. When the callback is needed,
\r
461 function HAL_RNG_ErrorCallback must be implemented in the user file.
\r
469 /** @addtogroup RNG_Exported_Functions_Group3
\r
470 * @brief Peripheral State functions
\r
473 ===============================================================================
\r
474 ##### Peripheral State functions #####
\r
475 ===============================================================================
\r
477 This subsection permits to get in run-time the status of the peripheral
\r
485 * @brief Returns the RNG state.
\r
486 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
\r
487 * the configuration information for RNG.
\r
488 * @retval HAL state
\r
490 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
\r
492 return hrng->State;
\r
503 #endif /* STM32F746xx || STM32F756xx */
\r
504 #endif /* HAL_RNG_MODULE_ENABLED */
\r
514 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r