Updated and Validated
[betaflight.git] / lib / main / STM32F7 / Drivers / STM32F7xx_HAL_Driver / Src / stm32f7xx_hal_rng.c
blob3e073e291dd36f31acdb6875e31199510bc3dc9c
1 /**
2 ******************************************************************************
3 * @file stm32f7xx_hal_rng.c
4 * @author MCD Application Team
5 * @brief RNG HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Random Number Generator (RNG) peripheral:
8 * + Initialization and configuration functions
9 * + Peripheral Control functions
10 * + Peripheral State functions
12 @verbatim
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
16 [..]
17 The RNG HAL driver can be used as follows:
19 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
20 in HAL_RNG_MspInit().
21 (#) Activate the RNG peripheral using HAL_RNG_Init() function.
22 (#) Wait until the 32 bit Random Number Generator contains a valid
23 random data using (polling/interrupt) mode.
24 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
26 ##### Callback registration #####
27 ==================================
29 [..]
30 The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
31 allows the user to configure dynamically the driver callbacks.
33 [..]
34 Use Function @ref HAL_RNG_RegisterCallback() to register a user callback.
35 Function @ref HAL_RNG_RegisterCallback() allows to register following callbacks:
36 (+) ErrorCallback : RNG Error Callback.
37 (+) MspInitCallback : RNG MspInit.
38 (+) MspDeInitCallback : RNG MspDeInit.
39 This function takes as parameters the HAL peripheral handle, the Callback ID
40 and a pointer to the user callback function.
42 [..]
43 Use function @ref HAL_RNG_UnRegisterCallback() to reset a callback to the default
44 weak (surcharged) function.
45 @ref HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
46 and the Callback ID.
47 This function allows to reset following callbacks:
48 (+) ErrorCallback : RNG Error Callback.
49 (+) MspInitCallback : RNG MspInit.
50 (+) MspDeInitCallback : RNG MspDeInit.
52 [..]
53 For specific callback ReadyDataCallback, use dedicated register callbacks:
54 respectively @ref HAL_RNG_RegisterReadyDataCallback() , @ref HAL_RNG_UnRegisterReadyDataCallback().
56 [..]
57 By default, after the @ref HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET
58 all callbacks are set to the corresponding weak (surcharged) functions:
59 example @ref HAL_RNG_ErrorCallback().
60 Exception done for MspInit and MspDeInit functions that are respectively
61 reset to the legacy weak (surcharged) functions in the @ref HAL_RNG_Init()
62 and @ref HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand).
63 If not, MspInit or MspDeInit are not null, the @ref HAL_RNG_Init() and @ref HAL_RNG_DeInit()
64 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
66 [..]
67 Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only.
68 Exception done MspInit/MspDeInit that can be registered/unregistered
69 in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user)
70 MspInit/DeInit callbacks can be used during the Init/DeInit.
71 In that case first register the MspInit/MspDeInit user callbacks
72 using @ref HAL_RNG_RegisterCallback() before calling @ref HAL_RNG_DeInit()
73 or @ref HAL_RNG_Init() function.
75 [..]
76 When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or
77 not defined, the callback registration feature is not available
78 and weak (surcharged) callbacks are used.
80 @endverbatim
81 ******************************************************************************
82 * @attention
84 * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
85 * All rights reserved.</center></h2>
87 * This software component is licensed by ST under BSD 3-Clause license,
88 * the "License"; You may not use this file except in compliance with the
89 * License. You may obtain a copy of the License at:
90 * opensource.org/licenses/BSD-3-Clause
92 ******************************************************************************
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32f7xx_hal.h"
98 /** @addtogroup STM32F7xx_HAL_Driver
99 * @{
102 #if defined (RNG)
104 /** @addtogroup RNG
105 * @brief RNG HAL module driver.
106 * @{
109 #ifdef HAL_RNG_MODULE_ENABLED
111 /* Private types -------------------------------------------------------------*/
112 /* Private defines -----------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 /* Private constants ---------------------------------------------------------*/
115 /** @defgroup RNG_Private_Constants RNG Private Constants
116 * @{
118 #define RNG_TIMEOUT_VALUE 2U
120 * @}
122 /* Private macros ------------------------------------------------------------*/
123 /* Private functions prototypes ----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
127 /** @addtogroup RNG_Exported_Functions
128 * @{
131 /** @addtogroup RNG_Exported_Functions_Group1
132 * @brief Initialization and configuration functions
134 @verbatim
135 ===============================================================================
136 ##### Initialization and configuration functions #####
137 ===============================================================================
138 [..] This section provides functions allowing to:
139 (+) Initialize the RNG according to the specified parameters
140 in the RNG_InitTypeDef and create the associated handle
141 (+) DeInitialize the RNG peripheral
142 (+) Initialize the RNG MSP
143 (+) DeInitialize RNG MSP
145 @endverbatim
146 * @{
150 * @brief Initializes the RNG peripheral and creates the associated handle.
151 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
152 * the configuration information for RNG.
153 * @retval HAL status
155 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
157 /* Check the RNG handle allocation */
158 if (hrng == NULL)
160 return HAL_ERROR;
162 /* Check the parameters */
163 assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
165 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
166 if (hrng->State == HAL_RNG_STATE_RESET)
168 /* Allocate lock resource and initialize it */
169 hrng->Lock = HAL_UNLOCKED;
171 hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
172 hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
174 if (hrng->MspInitCallback == NULL)
176 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
179 /* Init the low level hardware */
180 hrng->MspInitCallback(hrng);
182 #else
183 if (hrng->State == HAL_RNG_STATE_RESET)
185 /* Allocate lock resource and initialize it */
186 hrng->Lock = HAL_UNLOCKED;
188 /* Init the low level hardware */
189 HAL_RNG_MspInit(hrng);
191 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
193 /* Change RNG peripheral state */
194 hrng->State = HAL_RNG_STATE_BUSY;
197 /* Enable the RNG Peripheral */
198 __HAL_RNG_ENABLE(hrng);
200 /* Initialize the RNG state */
201 hrng->State = HAL_RNG_STATE_READY;
203 /* Initialise the error code */
204 hrng->ErrorCode = HAL_RNG_ERROR_NONE;
206 /* Return function status */
207 return HAL_OK;
211 * @brief DeInitializes the RNG peripheral.
212 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
213 * the configuration information for RNG.
214 * @retval HAL status
216 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
218 /* Check the RNG handle allocation */
219 if (hrng == NULL)
221 return HAL_ERROR;
224 /* Disable the RNG Peripheral */
225 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
227 /* Clear RNG interrupt status flags */
228 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
230 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
231 if (hrng->MspDeInitCallback == NULL)
233 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
236 /* DeInit the low level hardware */
237 hrng->MspDeInitCallback(hrng);
238 #else
239 /* DeInit the low level hardware */
240 HAL_RNG_MspDeInit(hrng);
241 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
243 /* Update the RNG state */
244 hrng->State = HAL_RNG_STATE_RESET;
246 /* Initialise the error code */
247 hrng->ErrorCode = HAL_RNG_ERROR_NONE;
249 /* Release Lock */
250 __HAL_UNLOCK(hrng);
252 /* Return the function status */
253 return HAL_OK;
257 * @brief Initializes the RNG MSP.
258 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
259 * the configuration information for RNG.
260 * @retval None
262 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
264 /* Prevent unused argument(s) compilation warning */
265 UNUSED(hrng);
266 /* NOTE : This function should not be modified. When the callback is needed,
267 function HAL_RNG_MspInit must be implemented in the user file.
272 * @brief DeInitializes the RNG MSP.
273 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
274 * the configuration information for RNG.
275 * @retval None
277 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
279 /* Prevent unused argument(s) compilation warning */
280 UNUSED(hrng);
281 /* NOTE : This function should not be modified. When the callback is needed,
282 function HAL_RNG_MspDeInit must be implemented in the user file.
286 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
288 * @brief Register a User RNG Callback
289 * To be used instead of the weak predefined callback
290 * @param hrng RNG handle
291 * @param CallbackID ID of the callback to be registered
292 * This parameter can be one of the following values:
293 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
294 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
295 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
296 * @param pCallback pointer to the Callback function
297 * @retval HAL status
299 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback)
301 HAL_StatusTypeDef status = HAL_OK;
303 if (pCallback == NULL)
305 /* Update the error code */
306 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
307 return HAL_ERROR;
309 /* Process locked */
310 __HAL_LOCK(hrng);
312 if (HAL_RNG_STATE_READY == hrng->State)
314 switch (CallbackID)
316 case HAL_RNG_ERROR_CB_ID :
317 hrng->ErrorCallback = pCallback;
318 break;
320 case HAL_RNG_MSPINIT_CB_ID :
321 hrng->MspInitCallback = pCallback;
322 break;
324 case HAL_RNG_MSPDEINIT_CB_ID :
325 hrng->MspDeInitCallback = pCallback;
326 break;
328 default :
329 /* Update the error code */
330 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
331 /* Return error status */
332 status = HAL_ERROR;
333 break;
336 else if (HAL_RNG_STATE_RESET == hrng->State)
338 switch (CallbackID)
340 case HAL_RNG_MSPINIT_CB_ID :
341 hrng->MspInitCallback = pCallback;
342 break;
344 case HAL_RNG_MSPDEINIT_CB_ID :
345 hrng->MspDeInitCallback = pCallback;
346 break;
348 default :
349 /* Update the error code */
350 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
351 /* Return error status */
352 status = HAL_ERROR;
353 break;
356 else
358 /* Update the error code */
359 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
360 /* Return error status */
361 status = HAL_ERROR;
364 /* Release Lock */
365 __HAL_UNLOCK(hrng);
366 return status;
370 * @brief Unregister an RNG Callback
371 * RNG callabck is redirected to the weak predefined callback
372 * @param hrng RNG handle
373 * @param CallbackID ID of the callback to be unregistered
374 * This parameter can be one of the following values:
375 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
376 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
377 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
378 * @retval HAL status
380 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
382 HAL_StatusTypeDef status = HAL_OK;
384 /* Process locked */
385 __HAL_LOCK(hrng);
387 if (HAL_RNG_STATE_READY == hrng->State)
389 switch (CallbackID)
391 case HAL_RNG_ERROR_CB_ID :
392 hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
393 break;
395 case HAL_RNG_MSPINIT_CB_ID :
396 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
397 break;
399 case HAL_RNG_MSPDEINIT_CB_ID :
400 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
401 break;
403 default :
404 /* Update the error code */
405 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
406 /* Return error status */
407 status = HAL_ERROR;
408 break;
411 else if (HAL_RNG_STATE_RESET == hrng->State)
413 switch (CallbackID)
415 case HAL_RNG_MSPINIT_CB_ID :
416 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
417 break;
419 case HAL_RNG_MSPDEINIT_CB_ID :
420 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */
421 break;
423 default :
424 /* Update the error code */
425 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
426 /* Return error status */
427 status = HAL_ERROR;
428 break;
431 else
433 /* Update the error code */
434 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
435 /* Return error status */
436 status = HAL_ERROR;
439 /* Release Lock */
440 __HAL_UNLOCK(hrng);
441 return status;
445 * @brief Register Data Ready RNG Callback
446 * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
447 * @param hrng RNG handle
448 * @param pCallback pointer to the Data Ready Callback function
449 * @retval HAL status
451 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
453 HAL_StatusTypeDef status = HAL_OK;
455 if (pCallback == NULL)
457 /* Update the error code */
458 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
459 return HAL_ERROR;
461 /* Process locked */
462 __HAL_LOCK(hrng);
464 if (HAL_RNG_STATE_READY == hrng->State)
466 hrng->ReadyDataCallback = pCallback;
468 else
470 /* Update the error code */
471 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
472 /* Return error status */
473 status = HAL_ERROR;
476 /* Release Lock */
477 __HAL_UNLOCK(hrng);
478 return status;
482 * @brief UnRegister the Data Ready RNG Callback
483 * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
484 * @param hrng RNG handle
485 * @retval HAL status
487 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
489 HAL_StatusTypeDef status = HAL_OK;
491 /* Process locked */
492 __HAL_LOCK(hrng);
494 if (HAL_RNG_STATE_READY == hrng->State)
496 hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
498 else
500 /* Update the error code */
501 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
502 /* Return error status */
503 status = HAL_ERROR;
506 /* Release Lock */
507 __HAL_UNLOCK(hrng);
508 return status;
511 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
514 * @}
517 /** @addtogroup RNG_Exported_Functions_Group2
518 * @brief Peripheral Control functions
520 @verbatim
521 ===============================================================================
522 ##### Peripheral Control functions #####
523 ===============================================================================
524 [..] This section provides functions allowing to:
525 (+) Get the 32 bit Random number
526 (+) Get the 32 bit Random number with interrupt enabled
527 (+) Handle RNG interrupt request
529 @endverbatim
530 * @{
534 * @brief Generates a 32-bit random number.
535 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
536 * is automatically cleared.
537 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
538 * the configuration information for RNG.
539 * @param random32bit pointer to generated random number variable if successful.
540 * @retval HAL status
543 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
545 uint32_t tickstart;
546 HAL_StatusTypeDef status = HAL_OK;
548 /* Process Locked */
549 __HAL_LOCK(hrng);
551 /* Check RNG peripheral state */
552 if (hrng->State == HAL_RNG_STATE_READY)
554 /* Change RNG peripheral state */
555 hrng->State = HAL_RNG_STATE_BUSY;
557 /* Get tick */
558 tickstart = HAL_GetTick();
560 /* Check if data register contains valid random data */
561 while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
563 if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
565 hrng->State = HAL_RNG_STATE_READY;
566 hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
567 /* Process Unlocked */
568 __HAL_UNLOCK(hrng);
569 return HAL_ERROR;
573 /* Get a 32bit Random number */
574 hrng->RandomNumber = hrng->Instance->DR;
575 *random32bit = hrng->RandomNumber;
577 hrng->State = HAL_RNG_STATE_READY;
579 else
581 hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
582 status = HAL_ERROR;
585 /* Process Unlocked */
586 __HAL_UNLOCK(hrng);
588 return status;
592 * @brief Generates a 32-bit random number in interrupt mode.
593 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
594 * the configuration information for RNG.
595 * @retval HAL status
597 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
599 HAL_StatusTypeDef status = HAL_OK;
601 /* Process Locked */
602 __HAL_LOCK(hrng);
604 /* Check RNG peripheral state */
605 if (hrng->State == HAL_RNG_STATE_READY)
607 /* Change RNG peripheral state */
608 hrng->State = HAL_RNG_STATE_BUSY;
610 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
611 __HAL_RNG_ENABLE_IT(hrng);
613 else
615 /* Process Unlocked */
616 __HAL_UNLOCK(hrng);
618 hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
619 status = HAL_ERROR;
622 return status;
626 * @brief Returns generated random number in polling mode (Obsolete)
627 * Use HAL_RNG_GenerateRandomNumber() API instead.
628 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
629 * the configuration information for RNG.
630 * @retval Random value
632 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
634 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
636 return hrng->RandomNumber;
638 else
640 return 0U;
645 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
646 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
647 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
648 * the configuration information for RNG.
649 * @retval 32-bit random number
651 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
653 uint32_t random32bit = 0U;
655 /* Process locked */
656 __HAL_LOCK(hrng);
658 /* Change RNG peripheral state */
659 hrng->State = HAL_RNG_STATE_BUSY;
661 /* Get a 32bit Random number */
662 random32bit = hrng->Instance->DR;
664 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
665 __HAL_RNG_ENABLE_IT(hrng);
667 /* Return the 32 bit random number */
668 return random32bit;
672 * @brief Handles RNG interrupt request.
673 * @note In the case of a clock error, the RNG is no more able to generate
674 * random numbers because the PLL48CLK clock is not correct. User has
675 * to check that the clock controller is correctly configured to provide
676 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
677 * The clock error has no impact on the previously generated
678 * random numbers, and the RNG_DR register contents can be used.
679 * @note In the case of a seed error, the generation of random numbers is
680 * interrupted as long as the SECS bit is '1'. If a number is
681 * available in the RNG_DR register, it must not be used because it may
682 * not have enough entropy. In this case, it is recommended to clear the
683 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
684 * the RNG peripheral to reinitialize and restart the RNG.
685 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
686 * or CEIS are set.
687 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
688 * the configuration information for RNG.
689 * @retval None
692 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
694 uint32_t rngclockerror = 0U;
696 /* RNG clock error interrupt occurred */
697 if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET)
699 /* Update the error code */
700 hrng->ErrorCode = HAL_RNG_ERROR_SEED;
701 rngclockerror = 1U;
703 else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
705 /* Update the error code */
706 hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
707 rngclockerror = 1U;
709 else
711 /* Nothing to do */
714 if (rngclockerror == 1U)
716 /* Change RNG peripheral state */
717 hrng->State = HAL_RNG_STATE_ERROR;
719 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
720 /* Call registered Error callback */
721 hrng->ErrorCallback(hrng);
722 #else
723 /* Call legacy weak Error callback */
724 HAL_RNG_ErrorCallback(hrng);
725 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
727 /* Clear the clock error flag */
728 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
731 /* Check RNG data ready interrupt occurred */
732 if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
734 /* Generate random number once, so disable the IT */
735 __HAL_RNG_DISABLE_IT(hrng);
737 /* Get the 32bit Random number (DRDY flag automatically cleared) */
738 hrng->RandomNumber = hrng->Instance->DR;
740 if (hrng->State != HAL_RNG_STATE_ERROR)
742 /* Change RNG peripheral state */
743 hrng->State = HAL_RNG_STATE_READY;
744 /* Process Unlocked */
745 __HAL_UNLOCK(hrng);
747 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
748 /* Call registered Data Ready callback */
749 hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
750 #else
751 /* Call legacy weak Data Ready callback */
752 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
753 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
759 * @brief Read latest generated random number.
760 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
761 * the configuration information for RNG.
762 * @retval random value
764 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
766 return (hrng->RandomNumber);
770 * @brief Data Ready callback in non-blocking mode.
771 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
772 * the configuration information for RNG.
773 * @param random32bit generated random number.
774 * @retval None
776 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
778 /* Prevent unused argument(s) compilation warning */
779 UNUSED(hrng);
780 UNUSED(random32bit);
781 /* NOTE : This function should not be modified. When the callback is needed,
782 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
787 * @brief RNG error callbacks.
788 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
789 * the configuration information for RNG.
790 * @retval None
792 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
794 /* Prevent unused argument(s) compilation warning */
795 UNUSED(hrng);
796 /* NOTE : This function should not be modified. When the callback is needed,
797 function HAL_RNG_ErrorCallback must be implemented in the user file.
801 * @}
805 /** @addtogroup RNG_Exported_Functions_Group3
806 * @brief Peripheral State functions
808 @verbatim
809 ===============================================================================
810 ##### Peripheral State functions #####
811 ===============================================================================
812 [..]
813 This subsection permits to get in run-time the status of the peripheral
814 and the data flow.
816 @endverbatim
817 * @{
821 * @brief Returns the RNG state.
822 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
823 * the configuration information for RNG.
824 * @retval HAL state
826 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
828 return hrng->State;
832 * @brief Return the RNG handle error code.
833 * @param hrng: pointer to a RNG_HandleTypeDef structure.
834 * @retval RNG Error Code
836 uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng)
838 /* Return RNG Error Code */
839 return hrng->ErrorCode;
842 * @}
846 * @}
850 #endif /* HAL_RNG_MODULE_ENABLED */
852 * @}
855 #endif /* RNG */
858 * @}
861 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/