2 ******************************************************************************
3 * @file stm32h7xx_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
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
17 The RNG HAL driver can be used as follows:
19 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
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 ==================================
30 The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
31 allows the user to configure dynamically the driver callbacks.
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.
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,
47 This function allows to reset following callbacks:
48 (+) ErrorCallback : RNG Error Callback.
49 (+) MspInitCallback : RNG MspInit.
50 (+) MspDeInitCallback : RNG MspDeInit.
53 For specific callback ReadyDataCallback, use dedicated register callbacks:
54 respectively @ref HAL_RNG_RegisterReadyDataCallback() , @ref HAL_RNG_UnRegisterReadyDataCallback().
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).
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.
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.
81 ******************************************************************************
84 * <h2><center>© 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 "stm32h7xx_hal.h"
98 /** @addtogroup STM32H7xx_HAL_Driver
105 * @brief RNG HAL module driver.
109 #ifdef HAL_RNG_MODULE_ENABLED
111 /* Private types -------------------------------------------------------------*/
112 /* Private defines -----------------------------------------------------------*/
113 /** @defgroup RNG_Private_Defines RNG Private Defines
116 /* Health test control register information to use in CCM algorithm */
117 #define RNG_HTCFG_1 0x17590ABCU /*!< magic number */
118 #if defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
119 #define RNG_HTCFG 0x000CAA74U /*!< for best latency and To be compliant with NIST */
120 #else /*RNG_VER_3_2*/
121 #define RNG_HTCFG 0x00007274U /*!< for best latency and To be compliant with NIST */
126 /* Private variables ---------------------------------------------------------*/
127 /* Private constants ---------------------------------------------------------*/
128 /** @defgroup RNG_Private_Constants RNG Private Constants
131 #define RNG_TIMEOUT_VALUE 2U
135 /* Private macros ------------------------------------------------------------*/
136 /* Private functions prototypes ----------------------------------------------*/
137 /* Private functions ---------------------------------------------------------*/
138 /* Exported functions --------------------------------------------------------*/
140 /** @addtogroup RNG_Exported_Functions
144 /** @addtogroup RNG_Exported_Functions_Group1
145 * @brief Initialization and configuration functions
148 ===============================================================================
149 ##### Initialization and configuration functions #####
150 ===============================================================================
151 [..] This section provides functions allowing to:
152 (+) Initialize the RNG according to the specified parameters
153 in the RNG_InitTypeDef and create the associated handle
154 (+) DeInitialize the RNG peripheral
155 (+) Initialize the RNG MSP
156 (+) DeInitialize RNG MSP
163 * @brief Initializes the RNG peripheral and creates the associated handle.
164 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
165 * the configuration information for RNG.
168 HAL_StatusTypeDef
HAL_RNG_Init(RNG_HandleTypeDef
*hrng
)
171 /* Check the RNG handle allocation */
176 /* Check the parameters */
177 assert_param(IS_RNG_ALL_INSTANCE(hrng
->Instance
));
178 assert_param(IS_RNG_CED(hrng
->Init
.ClockErrorDetection
));
180 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
181 if (hrng
->State
== HAL_RNG_STATE_RESET
)
183 /* Allocate lock resource and initialize it */
184 hrng
->Lock
= HAL_UNLOCKED
;
186 hrng
->ReadyDataCallback
= HAL_RNG_ReadyDataCallback
; /* Legacy weak ReadyDataCallback */
187 hrng
->ErrorCallback
= HAL_RNG_ErrorCallback
; /* Legacy weak ErrorCallback */
189 if (hrng
->MspInitCallback
== NULL
)
191 hrng
->MspInitCallback
= HAL_RNG_MspInit
; /* Legacy weak MspInit */
194 /* Init the low level hardware */
195 hrng
->MspInitCallback(hrng
);
198 if (hrng
->State
== HAL_RNG_STATE_RESET
)
200 /* Allocate lock resource and initialize it */
201 hrng
->Lock
= HAL_UNLOCKED
;
203 /* Init the low level hardware */
204 HAL_RNG_MspInit(hrng
);
206 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
208 /* Change RNG peripheral state */
209 hrng
->State
= HAL_RNG_STATE_BUSY
;
211 #if defined(RNG_CR_CONDRST)
213 __HAL_RNG_DISABLE(hrng
);
215 /* Clock Error Detection Configuration when CONDRT bit is set to 1 */
216 MODIFY_REG(hrng
->Instance
->CR
, RNG_CR_CED
| RNG_CR_CONDRST
, hrng
->Init
.ClockErrorDetection
| RNG_CR_CONDRST
);
218 #if defined(RNG_VER_3_2) || defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
219 /*!< magic number must be written immediately before to RNG_HTCRG */
220 WRITE_REG(hrng
->Instance
->HTCR
, RNG_HTCFG_1
);
221 /* for best latency and to be compliant with NIST */
222 WRITE_REG(hrng
->Instance
->HTCR
, RNG_HTCFG
);
225 /* Writing bits CONDRST=0*/
226 CLEAR_BIT(hrng
->Instance
->CR
, RNG_CR_CONDRST
);
229 tickstart
= HAL_GetTick();
231 /* Wait for conditioning reset process to be completed */
232 while (HAL_IS_BIT_SET(hrng
->Instance
->CR
, RNG_CR_CONDRST
))
234 if ((HAL_GetTick() - tickstart
) > RNG_TIMEOUT_VALUE
)
236 hrng
->State
= HAL_RNG_STATE_READY
;
237 hrng
->ErrorCode
= HAL_RNG_ERROR_TIMEOUT
;
242 /* Clock Error Detection Configuration */
243 MODIFY_REG(hrng
->Instance
->CR
, RNG_CR_CED
, hrng
->Init
.ClockErrorDetection
);
244 #endif /* end of RNG_CR_CONDRST */
246 /* Enable the RNG Peripheral */
247 __HAL_RNG_ENABLE(hrng
);
249 /* verify that no seed error */
250 if (__HAL_RNG_GET_IT(hrng
, RNG_IT_SEI
) != RESET
)
252 hrng
->State
= HAL_RNG_STATE_ERROR
;
256 tickstart
= HAL_GetTick();
257 /* Check if data register contains valid random data */
258 while (__HAL_RNG_GET_FLAG(hrng
, RNG_FLAG_SECS
) != RESET
)
260 if ((HAL_GetTick() - tickstart
) > RNG_TIMEOUT_VALUE
)
262 hrng
->State
= HAL_RNG_STATE_ERROR
;
263 hrng
->ErrorCode
= HAL_RNG_ERROR_TIMEOUT
;
268 /* Initialize the RNG state */
269 hrng
->State
= HAL_RNG_STATE_READY
;
271 /* Initialise the error code */
272 hrng
->ErrorCode
= HAL_RNG_ERROR_NONE
;
274 /* Return function status */
279 * @brief DeInitializes the RNG peripheral.
280 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
281 * the configuration information for RNG.
284 HAL_StatusTypeDef
HAL_RNG_DeInit(RNG_HandleTypeDef
*hrng
)
286 #if defined(RNG_CR_CONDRST)
289 #endif /* RNG_CR_CONDRST */
290 /* Check the RNG handle allocation */
296 #if defined(RNG_CR_CONDRST)
297 /* Clear Clock Error Detection bit when CONDRT bit is set to 1 */
298 MODIFY_REG(hrng
->Instance
->CR
, RNG_CR_CED
| RNG_CR_CONDRST
, RNG_CED_ENABLE
| RNG_CR_CONDRST
);
300 /* Writing bits CONDRST=0*/
301 CLEAR_BIT(hrng
->Instance
->CR
, RNG_CR_CONDRST
);
304 tickstart
= HAL_GetTick();
306 /* Wait for conditioning reset process to be completed */
307 while (HAL_IS_BIT_SET(hrng
->Instance
->CR
, RNG_CR_CONDRST
))
309 if ((HAL_GetTick() - tickstart
) > RNG_TIMEOUT_VALUE
)
311 hrng
->State
= HAL_RNG_STATE_READY
;
312 hrng
->ErrorCode
= HAL_RNG_ERROR_TIMEOUT
;
313 /* Process Unlocked */
320 /* Clear Clock Error Detection bit */
321 CLEAR_BIT(hrng
->Instance
->CR
, RNG_CR_CED
);
322 #endif /* RNG_CR_CONDRST */
323 /* Disable the RNG Peripheral */
324 CLEAR_BIT(hrng
->Instance
->CR
, RNG_CR_IE
| RNG_CR_RNGEN
);
326 /* Clear RNG interrupt status flags */
327 CLEAR_BIT(hrng
->Instance
->SR
, RNG_SR_CEIS
| RNG_SR_SEIS
);
329 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
330 if (hrng
->MspDeInitCallback
== NULL
)
332 hrng
->MspDeInitCallback
= HAL_RNG_MspDeInit
; /* Legacy weak MspDeInit */
335 /* DeInit the low level hardware */
336 hrng
->MspDeInitCallback(hrng
);
338 /* DeInit the low level hardware */
339 HAL_RNG_MspDeInit(hrng
);
340 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
342 /* Update the RNG state */
343 hrng
->State
= HAL_RNG_STATE_RESET
;
345 /* Initialise the error code */
346 hrng
->ErrorCode
= HAL_RNG_ERROR_NONE
;
351 /* Return the function status */
356 * @brief Initializes the RNG MSP.
357 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
358 * the configuration information for RNG.
361 __weak
void HAL_RNG_MspInit(RNG_HandleTypeDef
*hrng
)
363 /* Prevent unused argument(s) compilation warning */
365 /* NOTE : This function should not be modified. When the callback is needed,
366 function HAL_RNG_MspInit must be implemented in the user file.
371 * @brief DeInitializes the RNG MSP.
372 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
373 * the configuration information for RNG.
376 __weak
void HAL_RNG_MspDeInit(RNG_HandleTypeDef
*hrng
)
378 /* Prevent unused argument(s) compilation warning */
380 /* NOTE : This function should not be modified. When the callback is needed,
381 function HAL_RNG_MspDeInit must be implemented in the user file.
385 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
387 * @brief Register a User RNG Callback
388 * To be used instead of the weak predefined callback
389 * @param hrng RNG handle
390 * @param CallbackID ID of the callback to be registered
391 * This parameter can be one of the following values:
392 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
393 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
394 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
395 * @param pCallback pointer to the Callback function
398 HAL_StatusTypeDef
HAL_RNG_RegisterCallback(RNG_HandleTypeDef
*hrng
, HAL_RNG_CallbackIDTypeDef CallbackID
, pRNG_CallbackTypeDef pCallback
)
400 HAL_StatusTypeDef status
= HAL_OK
;
402 if (pCallback
== NULL
)
404 /* Update the error code */
405 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
411 if (HAL_RNG_STATE_READY
== hrng
->State
)
415 case HAL_RNG_ERROR_CB_ID
:
416 hrng
->ErrorCallback
= pCallback
;
419 case HAL_RNG_MSPINIT_CB_ID
:
420 hrng
->MspInitCallback
= pCallback
;
423 case HAL_RNG_MSPDEINIT_CB_ID
:
424 hrng
->MspDeInitCallback
= pCallback
;
428 /* Update the error code */
429 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
430 /* Return error status */
435 else if (HAL_RNG_STATE_RESET
== hrng
->State
)
439 case HAL_RNG_MSPINIT_CB_ID
:
440 hrng
->MspInitCallback
= pCallback
;
443 case HAL_RNG_MSPDEINIT_CB_ID
:
444 hrng
->MspDeInitCallback
= pCallback
;
448 /* Update the error code */
449 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
450 /* Return error status */
457 /* Update the error code */
458 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
459 /* Return error status */
469 * @brief Unregister an RNG Callback
470 * RNG callabck is redirected to the weak predefined callback
471 * @param hrng RNG handle
472 * @param CallbackID ID of the callback to be unregistered
473 * This parameter can be one of the following values:
474 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
475 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
476 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
479 HAL_StatusTypeDef
HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef
*hrng
, HAL_RNG_CallbackIDTypeDef CallbackID
)
481 HAL_StatusTypeDef status
= HAL_OK
;
486 if (HAL_RNG_STATE_READY
== hrng
->State
)
490 case HAL_RNG_ERROR_CB_ID
:
491 hrng
->ErrorCallback
= HAL_RNG_ErrorCallback
; /* Legacy weak ErrorCallback */
494 case HAL_RNG_MSPINIT_CB_ID
:
495 hrng
->MspInitCallback
= HAL_RNG_MspInit
; /* Legacy weak MspInit */
498 case HAL_RNG_MSPDEINIT_CB_ID
:
499 hrng
->MspDeInitCallback
= HAL_RNG_MspDeInit
; /* Legacy weak MspDeInit */
503 /* Update the error code */
504 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
505 /* Return error status */
510 else if (HAL_RNG_STATE_RESET
== hrng
->State
)
514 case HAL_RNG_MSPINIT_CB_ID
:
515 hrng
->MspInitCallback
= HAL_RNG_MspInit
; /* Legacy weak MspInit */
518 case HAL_RNG_MSPDEINIT_CB_ID
:
519 hrng
->MspDeInitCallback
= HAL_RNG_MspDeInit
; /* Legacy weak MspInit */
523 /* Update the error code */
524 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
525 /* Return error status */
532 /* Update the error code */
533 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
534 /* Return error status */
544 * @brief Register Data Ready RNG Callback
545 * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
546 * @param hrng RNG handle
547 * @param pCallback pointer to the Data Ready Callback function
550 HAL_StatusTypeDef
HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef
*hrng
, pRNG_ReadyDataCallbackTypeDef pCallback
)
552 HAL_StatusTypeDef status
= HAL_OK
;
554 if (pCallback
== NULL
)
556 /* Update the error code */
557 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
563 if (HAL_RNG_STATE_READY
== hrng
->State
)
565 hrng
->ReadyDataCallback
= pCallback
;
569 /* Update the error code */
570 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
571 /* Return error status */
581 * @brief UnRegister the Data Ready RNG Callback
582 * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
583 * @param hrng RNG handle
586 HAL_StatusTypeDef
HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef
*hrng
)
588 HAL_StatusTypeDef status
= HAL_OK
;
593 if (HAL_RNG_STATE_READY
== hrng
->State
)
595 hrng
->ReadyDataCallback
= HAL_RNG_ReadyDataCallback
; /* Legacy weak ReadyDataCallback */
599 /* Update the error code */
600 hrng
->ErrorCode
= HAL_RNG_ERROR_INVALID_CALLBACK
;
601 /* Return error status */
610 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
616 /** @addtogroup RNG_Exported_Functions_Group2
617 * @brief Peripheral Control functions
620 ===============================================================================
621 ##### Peripheral Control functions #####
622 ===============================================================================
623 [..] This section provides functions allowing to:
624 (+) Get the 32 bit Random number
625 (+) Get the 32 bit Random number with interrupt enabled
626 (+) Handle RNG interrupt request
633 * @brief Generates a 32-bit random number.
634 * @note This function checks value of RNG_FLAG_DRDY flag to know if valid
635 * random number is available in the DR register (RNG_FLAG_DRDY flag set
636 * whenever a random number is available through the RNG_DR register).
637 * After transitioning from 0 to 1 (random number available),
638 * RNG_FLAG_DRDY flag remains high until output buffer becomes empty after reading
639 * four words from the RNG_DR register, i.e. further function calls
640 * will immediately return a new u32 random number (additional words are
641 * available and can be read by the application, till RNG_FLAG_DRDY flag remains high).
642 * @note When no more random number data is available in DR register, RNG_FLAG_DRDY
643 * flag is automatically cleared.
644 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
645 * the configuration information for RNG.
646 * @param random32bit pointer to generated random number variable if successful.
650 HAL_StatusTypeDef
HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef
*hrng
, uint32_t *random32bit
)
653 HAL_StatusTypeDef status
= HAL_OK
;
658 /* Check RNG peripheral state */
659 if (hrng
->State
== HAL_RNG_STATE_READY
)
661 /* Change RNG peripheral state */
662 hrng
->State
= HAL_RNG_STATE_BUSY
;
665 tickstart
= HAL_GetTick();
667 /* Check if data register contains valid random data */
668 while (__HAL_RNG_GET_FLAG(hrng
, RNG_FLAG_DRDY
) == RESET
)
670 if ((HAL_GetTick() - tickstart
) > RNG_TIMEOUT_VALUE
)
672 hrng
->State
= HAL_RNG_STATE_READY
;
673 hrng
->ErrorCode
= HAL_RNG_ERROR_TIMEOUT
;
674 /* Process Unlocked */
680 /* Get a 32bit Random number */
681 hrng
->RandomNumber
= hrng
->Instance
->DR
;
682 *random32bit
= hrng
->RandomNumber
;
684 hrng
->State
= HAL_RNG_STATE_READY
;
688 hrng
->ErrorCode
= HAL_RNG_ERROR_BUSY
;
692 /* Process Unlocked */
699 * @brief Generates a 32-bit random number in interrupt mode.
700 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
701 * the configuration information for RNG.
704 HAL_StatusTypeDef
HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef
*hrng
)
706 HAL_StatusTypeDef status
= HAL_OK
;
711 /* Check RNG peripheral state */
712 if (hrng
->State
== HAL_RNG_STATE_READY
)
714 /* Change RNG peripheral state */
715 hrng
->State
= HAL_RNG_STATE_BUSY
;
717 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
718 __HAL_RNG_ENABLE_IT(hrng
);
722 /* Process Unlocked */
725 hrng
->ErrorCode
= HAL_RNG_ERROR_BUSY
;
733 * @brief Handles RNG interrupt request.
734 * @note In the case of a clock error, the RNG is no more able to generate
735 * random numbers because the PLL48CLK clock is not correct. User has
736 * to check that the clock controller is correctly configured to provide
737 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
738 * The clock error has no impact on the previously generated
739 * random numbers, and the RNG_DR register contents can be used.
740 * @note In the case of a seed error, the generation of random numbers is
741 * interrupted as long as the SECS bit is '1'. If a number is
742 * available in the RNG_DR register, it must not be used because it may
743 * not have enough entropy. In this case, it is recommended to clear the
744 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
745 * the RNG peripheral to reinitialize and restart the RNG.
746 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
748 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
749 * the configuration information for RNG.
753 void HAL_RNG_IRQHandler(RNG_HandleTypeDef
*hrng
)
755 uint32_t rngclockerror
= 0U;
757 /* RNG clock error interrupt occurred */
758 if (__HAL_RNG_GET_IT(hrng
, RNG_IT_CEI
) != RESET
)
760 /* Update the error code */
761 hrng
->ErrorCode
= HAL_RNG_ERROR_CLOCK
;
764 else if (__HAL_RNG_GET_IT(hrng
, RNG_IT_SEI
) != RESET
)
766 /* Update the error code */
767 hrng
->ErrorCode
= HAL_RNG_ERROR_SEED
;
775 if (rngclockerror
== 1U)
777 /* Change RNG peripheral state */
778 hrng
->State
= HAL_RNG_STATE_ERROR
;
780 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
781 /* Call registered Error callback */
782 hrng
->ErrorCallback(hrng
);
784 /* Call legacy weak Error callback */
785 HAL_RNG_ErrorCallback(hrng
);
786 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
788 /* Clear the clock error flag */
789 __HAL_RNG_CLEAR_IT(hrng
, RNG_IT_CEI
| RNG_IT_SEI
);
792 /* Check RNG data ready interrupt occurred */
793 if (__HAL_RNG_GET_IT(hrng
, RNG_IT_DRDY
) != RESET
)
795 /* Generate random number once, so disable the IT */
796 __HAL_RNG_DISABLE_IT(hrng
);
798 /* Get the 32bit Random number (DRDY flag automatically cleared) */
799 hrng
->RandomNumber
= hrng
->Instance
->DR
;
801 if (hrng
->State
!= HAL_RNG_STATE_ERROR
)
803 /* Change RNG peripheral state */
804 hrng
->State
= HAL_RNG_STATE_READY
;
805 /* Process Unlocked */
808 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
809 /* Call registered Data Ready callback */
810 hrng
->ReadyDataCallback(hrng
, hrng
->RandomNumber
);
812 /* Call legacy weak Data Ready callback */
813 HAL_RNG_ReadyDataCallback(hrng
, hrng
->RandomNumber
);
814 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
820 * @brief Read latest generated random number.
821 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
822 * the configuration information for RNG.
823 * @retval random value
825 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef
*hrng
)
827 return (hrng
->RandomNumber
);
831 * @brief Data Ready callback in non-blocking mode.
832 * @note When RNG_FLAG_DRDY flag value is set, first random number has been read
833 * from DR register in IRQ Handler and is provided as callback parameter.
834 * Depending on valid data available in the conditioning output buffer,
835 * additional words can be read by the application from DR register till
836 * DRDY bit remains high.
837 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
838 * the configuration information for RNG.
839 * @param random32bit generated random number.
842 __weak
void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef
*hrng
, uint32_t random32bit
)
844 /* Prevent unused argument(s) compilation warning */
847 /* NOTE : This function should not be modified. When the callback is needed,
848 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
853 * @brief RNG error callbacks.
854 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
855 * the configuration information for RNG.
858 __weak
void HAL_RNG_ErrorCallback(RNG_HandleTypeDef
*hrng
)
860 /* Prevent unused argument(s) compilation warning */
862 /* NOTE : This function should not be modified. When the callback is needed,
863 function HAL_RNG_ErrorCallback must be implemented in the user file.
871 /** @addtogroup RNG_Exported_Functions_Group3
872 * @brief Peripheral State functions
875 ===============================================================================
876 ##### Peripheral State functions #####
877 ===============================================================================
879 This subsection permits to get in run-time the status of the peripheral
887 * @brief Returns the RNG state.
888 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
889 * the configuration information for RNG.
892 HAL_RNG_StateTypeDef
HAL_RNG_GetState(RNG_HandleTypeDef
*hrng
)
898 * @brief Return the RNG handle error code.
899 * @param hrng: pointer to a RNG_HandleTypeDef structure.
900 * @retval RNG Error Code
902 uint32_t HAL_RNG_GetError(RNG_HandleTypeDef
*hrng
)
904 /* Return RNG Error Code */
905 return hrng
->ErrorCode
;
916 #endif /* HAL_RNG_MODULE_ENABLED */
927 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/