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
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * Copyright (c) 2017 STMicroelectronics.
16   * All rights reserved.
17   *
18   * This software is licensed under terms that can be found in the LICENSE file
19   * in the root directory of this software component.
20   * If no LICENSE file comes with this software, it is provided AS-IS.
21   *
22   ******************************************************************************
23   @verbatim
24   ==============================================================================
25                      ##### How to use this driver #####
26   ==============================================================================
27   [..]
28       The RNG HAL driver can be used as follows:
29 
30       (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
31           in HAL_RNG_MspInit().
32       (#) Activate the RNG peripheral using HAL_RNG_Init() function.
33       (#) Wait until the 32 bit Random Number Generator contains a valid
34           random data using (polling/interrupt) mode.
35       (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
36 
37     ##### Callback registration #####
38     ==================================
39 
40     [..]
41     The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
42     allows the user to configure dynamically the driver callbacks.
43 
44     [..]
45     Use Function HAL_RNG_RegisterCallback() to register a user callback.
46     Function HAL_RNG_RegisterCallback() allows to register following callbacks:
47     (+) ErrorCallback             : RNG Error Callback.
48     (+) MspInitCallback           : RNG MspInit.
49     (+) MspDeInitCallback         : RNG MspDeInit.
50     This function takes as parameters the HAL peripheral handle, the Callback ID
51     and a pointer to the user callback function.
52 
53     [..]
54     Use function HAL_RNG_UnRegisterCallback() to reset a callback to the default
55     weak (overridden) function.
56     HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
57     and the Callback ID.
58     This function allows to reset following callbacks:
59     (+) ErrorCallback             : RNG Error Callback.
60     (+) MspInitCallback           : RNG MspInit.
61     (+) MspDeInitCallback         : RNG MspDeInit.
62 
63     [..]
64     For specific callback ReadyDataCallback, use dedicated register callbacks:
65     respectively HAL_RNG_RegisterReadyDataCallback() , HAL_RNG_UnRegisterReadyDataCallback().
66 
67     [..]
68     By default, after the HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET
69     all callbacks are set to the corresponding weak (overridden) functions:
70     example HAL_RNG_ErrorCallback().
71     Exception done for MspInit and MspDeInit functions that are respectively
72     reset to the legacy weak (overridden) functions in the HAL_RNG_Init()
73     and HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand).
74     If not, MspInit or MspDeInit are not null, the HAL_RNG_Init() and HAL_RNG_DeInit()
75     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
76 
77     [..]
78     Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only.
79     Exception done MspInit/MspDeInit that can be registered/unregistered
80     in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user)
81     MspInit/DeInit callbacks can be used during the Init/DeInit.
82     In that case first register the MspInit/MspDeInit user callbacks
83     using HAL_RNG_RegisterCallback() before calling HAL_RNG_DeInit()
84     or HAL_RNG_Init() function.
85 
86     [..]
87     When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or
88     not defined, the callback registration feature is not available
89     and weak (overridden) callbacks are used.
90 
91   @endverbatim
92   ******************************************************************************
93   */
94 
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32f7xx_hal.h"
97 
98 /** @addtogroup STM32F7xx_HAL_Driver
99   * @{
100   */
101 
102 #if defined (RNG)
103 
104 /** @addtogroup RNG
105   * @brief RNG HAL module driver.
106   * @{
107   */
108 
109 #ifdef HAL_RNG_MODULE_ENABLED
110 
111 /* Private types -------------------------------------------------------------*/
112 /* Private defines -----------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 /* Private constants ---------------------------------------------------------*/
115 /** @defgroup RNG_Private_Constants RNG Private Constants
116   * @{
117   */
118 #define RNG_TIMEOUT_VALUE     2U
119 /**
120   * @}
121   */
122 /* Private macros ------------------------------------------------------------*/
123 /* Private functions prototypes ----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
126 
127 /** @addtogroup RNG_Exported_Functions
128   * @{
129   */
130 
131 /** @addtogroup RNG_Exported_Functions_Group1
132   *  @brief   Initialization and configuration functions
133   *
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
144 
145 @endverbatim
146   * @{
147   */
148 
149 /**
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
154   */
HAL_RNG_Init(RNG_HandleTypeDef * hrng)155 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
156 {
157   /* Check the RNG handle allocation */
158   if (hrng == NULL)
159   {
160     return HAL_ERROR;
161   }
162   /* Check the parameters */
163   assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
164 
165 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
166   if (hrng->State == HAL_RNG_STATE_RESET)
167   {
168     /* Allocate lock resource and initialize it */
169     hrng->Lock = HAL_UNLOCKED;
170 
171     hrng->ReadyDataCallback  = HAL_RNG_ReadyDataCallback;  /* Legacy weak ReadyDataCallback  */
172     hrng->ErrorCallback      = HAL_RNG_ErrorCallback;      /* Legacy weak ErrorCallback      */
173 
174     if (hrng->MspInitCallback == NULL)
175     {
176       hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit  */
177     }
178 
179     /* Init the low level hardware */
180     hrng->MspInitCallback(hrng);
181   }
182 #else
183   if (hrng->State == HAL_RNG_STATE_RESET)
184   {
185     /* Allocate lock resource and initialize it */
186     hrng->Lock = HAL_UNLOCKED;
187 
188     /* Init the low level hardware */
189     HAL_RNG_MspInit(hrng);
190   }
191 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
192 
193   /* Change RNG peripheral state */
194   hrng->State = HAL_RNG_STATE_BUSY;
195 
196 
197   /* Enable the RNG Peripheral */
198   __HAL_RNG_ENABLE(hrng);
199 
200   /* Initialize the RNG state */
201   hrng->State = HAL_RNG_STATE_READY;
202 
203   /* Initialise the error code */
204   hrng->ErrorCode = HAL_RNG_ERROR_NONE;
205 
206   /* Return function status */
207   return HAL_OK;
208 }
209 
210 /**
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
215   */
HAL_RNG_DeInit(RNG_HandleTypeDef * hrng)216 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
217 {
218   /* Check the RNG handle allocation */
219   if (hrng == NULL)
220   {
221     return HAL_ERROR;
222   }
223 
224   /* Disable the RNG Peripheral */
225   CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
226 
227   /* Clear RNG interrupt status flags */
228   CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
229 
230 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
231   if (hrng->MspDeInitCallback == NULL)
232   {
233     hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit  */
234   }
235 
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 */
242 
243   /* Update the RNG state */
244   hrng->State = HAL_RNG_STATE_RESET;
245 
246   /* Initialise the error code */
247   hrng->ErrorCode = HAL_RNG_ERROR_NONE;
248 
249   /* Release Lock */
250   __HAL_UNLOCK(hrng);
251 
252   /* Return the function status */
253   return HAL_OK;
254 }
255 
256 /**
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
261   */
HAL_RNG_MspInit(RNG_HandleTypeDef * hrng)262 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
263 {
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.
268    */
269 }
270 
271 /**
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
276   */
HAL_RNG_MspDeInit(RNG_HandleTypeDef * hrng)277 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
278 {
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.
283    */
284 }
285 
286 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
287 /**
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
298   */
HAL_RNG_RegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID,pRNG_CallbackTypeDef pCallback)299 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID,
300                                            pRNG_CallbackTypeDef pCallback)
301 {
302   HAL_StatusTypeDef status = HAL_OK;
303 
304   if (pCallback == NULL)
305   {
306     /* Update the error code */
307     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
308     return HAL_ERROR;
309   }
310 
311   if (HAL_RNG_STATE_READY == hrng->State)
312   {
313     switch (CallbackID)
314     {
315       case HAL_RNG_ERROR_CB_ID :
316         hrng->ErrorCallback = pCallback;
317         break;
318 
319       case HAL_RNG_MSPINIT_CB_ID :
320         hrng->MspInitCallback = pCallback;
321         break;
322 
323       case HAL_RNG_MSPDEINIT_CB_ID :
324         hrng->MspDeInitCallback = pCallback;
325         break;
326 
327       default :
328         /* Update the error code */
329         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
330         /* Return error status */
331         status =  HAL_ERROR;
332         break;
333     }
334   }
335   else if (HAL_RNG_STATE_RESET == hrng->State)
336   {
337     switch (CallbackID)
338     {
339       case HAL_RNG_MSPINIT_CB_ID :
340         hrng->MspInitCallback = pCallback;
341         break;
342 
343       case HAL_RNG_MSPDEINIT_CB_ID :
344         hrng->MspDeInitCallback = pCallback;
345         break;
346 
347       default :
348         /* Update the error code */
349         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
350         /* Return error status */
351         status =  HAL_ERROR;
352         break;
353     }
354   }
355   else
356   {
357     /* Update the error code */
358     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
359     /* Return error status */
360     status =  HAL_ERROR;
361   }
362 
363   return status;
364 }
365 
366 /**
367   * @brief  Unregister an RNG Callback
368   *         RNG callback is redirected to the weak predefined callback
369   * @param  hrng RNG handle
370   * @param  CallbackID ID of the callback to be unregistered
371   *         This parameter can be one of the following values:
372   *          @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
373   *          @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
374   *          @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
375   * @retval HAL status
376   */
HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID)377 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
378 {
379   HAL_StatusTypeDef status = HAL_OK;
380 
381 
382   if (HAL_RNG_STATE_READY == hrng->State)
383   {
384     switch (CallbackID)
385     {
386       case HAL_RNG_ERROR_CB_ID :
387         hrng->ErrorCallback = HAL_RNG_ErrorCallback;          /* Legacy weak ErrorCallback  */
388         break;
389 
390       case HAL_RNG_MSPINIT_CB_ID :
391         hrng->MspInitCallback = HAL_RNG_MspInit;              /* Legacy weak MspInit  */
392         break;
393 
394       case HAL_RNG_MSPDEINIT_CB_ID :
395         hrng->MspDeInitCallback = HAL_RNG_MspDeInit;          /* Legacy weak MspDeInit  */
396         break;
397 
398       default :
399         /* Update the error code */
400         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
401         /* Return error status */
402         status =  HAL_ERROR;
403         break;
404     }
405   }
406   else if (HAL_RNG_STATE_RESET == hrng->State)
407   {
408     switch (CallbackID)
409     {
410       case HAL_RNG_MSPINIT_CB_ID :
411         hrng->MspInitCallback = HAL_RNG_MspInit;              /* Legacy weak MspInit  */
412         break;
413 
414       case HAL_RNG_MSPDEINIT_CB_ID :
415         hrng->MspDeInitCallback = HAL_RNG_MspDeInit;          /* Legacy weak MspInit  */
416         break;
417 
418       default :
419         /* Update the error code */
420         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
421         /* Return error status */
422         status =  HAL_ERROR;
423         break;
424     }
425   }
426   else
427   {
428     /* Update the error code */
429     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
430     /* Return error status */
431     status =  HAL_ERROR;
432   }
433 
434   return status;
435 }
436 
437 /**
438   * @brief  Register Data Ready RNG Callback
439   *         To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
440   * @param  hrng RNG handle
441   * @param  pCallback pointer to the Data Ready Callback function
442   * @retval HAL status
443   */
HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef * hrng,pRNG_ReadyDataCallbackTypeDef pCallback)444 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
445 {
446   HAL_StatusTypeDef status = HAL_OK;
447 
448   if (pCallback == NULL)
449   {
450     /* Update the error code */
451     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
452     return HAL_ERROR;
453   }
454   /* Process locked */
455   __HAL_LOCK(hrng);
456 
457   if (HAL_RNG_STATE_READY == hrng->State)
458   {
459     hrng->ReadyDataCallback = pCallback;
460   }
461   else
462   {
463     /* Update the error code */
464     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
465     /* Return error status */
466     status =  HAL_ERROR;
467   }
468 
469   /* Release Lock */
470   __HAL_UNLOCK(hrng);
471   return status;
472 }
473 
474 /**
475   * @brief  UnRegister the Data Ready RNG Callback
476   *         Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
477   * @param  hrng RNG handle
478   * @retval HAL status
479   */
HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef * hrng)480 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
481 {
482   HAL_StatusTypeDef status = HAL_OK;
483 
484   /* Process locked */
485   __HAL_LOCK(hrng);
486 
487   if (HAL_RNG_STATE_READY == hrng->State)
488   {
489     hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback  */
490   }
491   else
492   {
493     /* Update the error code */
494     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
495     /* Return error status */
496     status =  HAL_ERROR;
497   }
498 
499   /* Release Lock */
500   __HAL_UNLOCK(hrng);
501   return status;
502 }
503 
504 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
505 
506 /**
507   * @}
508   */
509 
510 /** @addtogroup RNG_Exported_Functions_Group2
511   *  @brief   Peripheral Control functions
512   *
513 @verbatim
514  ===============================================================================
515                       ##### Peripheral Control functions #####
516  ===============================================================================
517     [..]  This section provides functions allowing to:
518       (+) Get the 32 bit Random number
519       (+) Get the 32 bit Random number with interrupt enabled
520       (+) Handle RNG interrupt request
521 
522 @endverbatim
523   * @{
524   */
525 
526 /**
527   * @brief  Generates a 32-bit random number.
528   * @note   This function checks value of RNG_FLAG_DRDY flag to know if valid
529   *         random number is available in the DR register (RNG_FLAG_DRDY flag set
530   *         whenever a random number is available through the RNG_DR register).
531   *         After transitioning from 0 to 1 (random number available),
532   *         RNG_FLAG_DRDY flag remains high until output buffer becomes empty after reading
533   *         four words from the RNG_DR register, i.e. further function calls
534   *         will immediately return a new u32 random number (additional words are
535   *         available and can be read by the application, till RNG_FLAG_DRDY flag remains high).
536   * @note   When no more random number data is available in DR register, RNG_FLAG_DRDY
537   *         flag is automatically cleared.
538   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
539   *                the configuration information for RNG.
540   * @param  random32bit pointer to generated random number variable if successful.
541   * @retval HAL status
542   */
543 
HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef * hrng,uint32_t * random32bit)544 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
545 {
546   uint32_t tickstart;
547   HAL_StatusTypeDef status = HAL_OK;
548 
549   /* Process Locked */
550   __HAL_LOCK(hrng);
551 
552   /* Check RNG peripheral state */
553   if (hrng->State == HAL_RNG_STATE_READY)
554   {
555     /* Change RNG peripheral state */
556     hrng->State = HAL_RNG_STATE_BUSY;
557 
558     /* Get tick */
559     tickstart = HAL_GetTick();
560 
561     /* Check if data register contains valid random data */
562     while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
563     {
564       if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
565       {
566         /* New check to avoid false timeout detection in case of preemption */
567         if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
568         {
569           hrng->State = HAL_RNG_STATE_READY;
570           hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
571           /* Process Unlocked */
572           __HAL_UNLOCK(hrng);
573           return HAL_ERROR;
574         }
575       }
576     }
577 
578     /* Get a 32bit Random number */
579     hrng->RandomNumber = hrng->Instance->DR;
580     *random32bit = hrng->RandomNumber;
581 
582     hrng->State = HAL_RNG_STATE_READY;
583   }
584   else
585   {
586     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
587     status = HAL_ERROR;
588   }
589 
590   /* Process Unlocked */
591   __HAL_UNLOCK(hrng);
592 
593   return status;
594 }
595 
596 /**
597   * @brief  Generates a 32-bit random number in interrupt mode.
598   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
599   *                the configuration information for RNG.
600   * @retval HAL status
601   */
HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef * hrng)602 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
603 {
604   HAL_StatusTypeDef status = HAL_OK;
605 
606   /* Process Locked */
607   __HAL_LOCK(hrng);
608 
609   /* Check RNG peripheral state */
610   if (hrng->State == HAL_RNG_STATE_READY)
611   {
612     /* Change RNG peripheral state */
613     hrng->State = HAL_RNG_STATE_BUSY;
614 
615     /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
616     __HAL_RNG_ENABLE_IT(hrng);
617   }
618   else
619   {
620     /* Process Unlocked */
621     __HAL_UNLOCK(hrng);
622 
623     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
624     status = HAL_ERROR;
625   }
626 
627   return status;
628 }
629 
630 /**
631   * @brief  Returns generated random number in polling mode (Obsolete)
632   *         Use HAL_RNG_GenerateRandomNumber() API instead.
633   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
634   *                the configuration information for RNG.
635   * @retval Random value
636   */
HAL_RNG_GetRandomNumber(RNG_HandleTypeDef * hrng)637 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
638 {
639   if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
640   {
641     return hrng->RandomNumber;
642   }
643   else
644   {
645     return 0U;
646   }
647 }
648 
649 /**
650   * @brief  Returns a 32-bit random number with interrupt enabled (Obsolete),
651   *         Use HAL_RNG_GenerateRandomNumber_IT() API instead.
652   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
653   *                the configuration information for RNG.
654   * @retval 32-bit random number
655   */
HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef * hrng)656 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
657 {
658   uint32_t random32bit = 0U;
659 
660   /* Process locked */
661   __HAL_LOCK(hrng);
662 
663   /* Change RNG peripheral state */
664   hrng->State = HAL_RNG_STATE_BUSY;
665 
666   /* Get a 32bit Random number */
667   random32bit = hrng->Instance->DR;
668 
669   /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
670   __HAL_RNG_ENABLE_IT(hrng);
671 
672   /* Return the 32 bit random number */
673   return random32bit;
674 }
675 
676 /**
677   * @brief  Handles RNG interrupt request.
678   * @note   In the case of a clock error, the RNG is no more able to generate
679   *         random numbers because the PLL48CLK clock is not correct. User has
680   *         to check that the clock controller is correctly configured to provide
681   *         the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
682   *         The clock error has no impact on the previously generated
683   *         random numbers, and the RNG_DR register contents can be used.
684   * @note   In the case of a seed error, the generation of random numbers is
685   *         interrupted as long as the SECS bit is '1'. If a number is
686   *         available in the RNG_DR register, it must not be used because it may
687   *         not have enough entropy. In this case, it is recommended to clear the
688   *         SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
689   *         the RNG peripheral to reinitialize and restart the RNG.
690   * @note   User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
691   *         or CEIS are set.
692   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
693   *                the configuration information for RNG.
694   * @retval None
695 
696   */
HAL_RNG_IRQHandler(RNG_HandleTypeDef * hrng)697 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
698 {
699   uint32_t rngclockerror = 0U;
700   uint32_t itflag   = hrng->Instance->SR;
701 
702   /* RNG clock error interrupt occurred */
703   if ((itflag & RNG_IT_CEI) == RNG_IT_CEI)
704   {
705     /* Update the error code */
706     hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
707     rngclockerror = 1U;
708   }
709   else if ((itflag & RNG_IT_SEI) == RNG_IT_SEI)
710   {
711     /* Update the error code */
712     hrng->ErrorCode = HAL_RNG_ERROR_SEED;
713     rngclockerror = 1U;
714   }
715   else
716   {
717     /* Nothing to do */
718   }
719 
720   if (rngclockerror == 1U)
721   {
722     /* Change RNG peripheral state */
723     hrng->State = HAL_RNG_STATE_ERROR;
724 
725 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
726     /* Call registered Error callback */
727     hrng->ErrorCallback(hrng);
728 #else
729     /* Call legacy weak Error callback */
730     HAL_RNG_ErrorCallback(hrng);
731 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
732 
733     /* Clear the clock error flag */
734     __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
735 
736     return;
737   }
738 
739   /* Check RNG data ready interrupt occurred */
740   if ((itflag & RNG_IT_DRDY) == RNG_IT_DRDY)
741   {
742     /* Generate random number once, so disable the IT */
743     __HAL_RNG_DISABLE_IT(hrng);
744 
745     /* Get the 32bit Random number (DRDY flag automatically cleared) */
746     hrng->RandomNumber = hrng->Instance->DR;
747 
748     if (hrng->State != HAL_RNG_STATE_ERROR)
749     {
750       /* Change RNG peripheral state */
751       hrng->State = HAL_RNG_STATE_READY;
752       /* Process Unlocked */
753       __HAL_UNLOCK(hrng);
754 
755 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
756       /* Call registered Data Ready callback */
757       hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
758 #else
759       /* Call legacy weak Data Ready callback */
760       HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
761 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
762     }
763   }
764 }
765 
766 /**
767   * @brief  Read latest generated random number.
768   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
769   *                the configuration information for RNG.
770   * @retval random value
771   */
HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef * hrng)772 uint32_t HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef *hrng)
773 {
774   return (hrng->RandomNumber);
775 }
776 
777 /**
778   * @brief  Data Ready callback in non-blocking mode.
779   * @note   When RNG_FLAG_DRDY flag value is set, first random number has been read
780   *         from DR register in IRQ Handler and is provided as callback parameter.
781   *         Depending on valid data available in the conditioning output buffer,
782   *         additional words can be read by the application from DR register till
783   *         DRDY bit remains high.
784   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
785   *                the configuration information for RNG.
786   * @param  random32bit generated random number.
787   * @retval None
788   */
HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef * hrng,uint32_t random32bit)789 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
790 {
791   /* Prevent unused argument(s) compilation warning */
792   UNUSED(hrng);
793   UNUSED(random32bit);
794   /* NOTE : This function should not be modified. When the callback is needed,
795             function HAL_RNG_ReadyDataCallback must be implemented in the user file.
796    */
797 }
798 
799 /**
800   * @brief  RNG error callbacks.
801   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
802   *                the configuration information for RNG.
803   * @retval None
804   */
HAL_RNG_ErrorCallback(RNG_HandleTypeDef * hrng)805 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
806 {
807   /* Prevent unused argument(s) compilation warning */
808   UNUSED(hrng);
809   /* NOTE : This function should not be modified. When the callback is needed,
810             function HAL_RNG_ErrorCallback must be implemented in the user file.
811    */
812 }
813 /**
814   * @}
815   */
816 
817 
818 /** @addtogroup RNG_Exported_Functions_Group3
819   *  @brief   Peripheral State functions
820   *
821 @verbatim
822  ===============================================================================
823                       ##### Peripheral State functions #####
824  ===============================================================================
825     [..]
826     This subsection permits to get in run-time the status of the peripheral
827     and the data flow.
828 
829 @endverbatim
830   * @{
831   */
832 
833 /**
834   * @brief  Returns the RNG state.
835   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
836   *                the configuration information for RNG.
837   * @retval HAL state
838   */
HAL_RNG_GetState(const RNG_HandleTypeDef * hrng)839 HAL_RNG_StateTypeDef HAL_RNG_GetState(const RNG_HandleTypeDef *hrng)
840 {
841   return hrng->State;
842 }
843 
844 /**
845   * @brief  Return the RNG handle error code.
846   * @param  hrng: pointer to a RNG_HandleTypeDef structure.
847   * @retval RNG Error Code
848   */
HAL_RNG_GetError(const RNG_HandleTypeDef * hrng)849 uint32_t HAL_RNG_GetError(const RNG_HandleTypeDef *hrng)
850 {
851   /* Return RNG Error Code */
852   return hrng->ErrorCode;
853 }
854 /**
855   * @}
856   */
857 
858 /**
859   * @}
860   */
861 
862 
863 #endif /* HAL_RNG_MODULE_ENABLED */
864 /**
865   * @}
866   */
867 
868 #endif /* RNG */
869 
870 /**
871   * @}
872   */
873 
874