1 /**
2   ******************************************************************************
3   * @file    stm32u0xx_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) 2023 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 "stm32u0xx_hal.h"
97 
98 /** @addtogroup STM32U0xx_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     4U
119 /**
120   * @}
121   */
122 /* Private macros ------------------------------------------------------------*/
123 /* Private functions prototypes ----------------------------------------------*/
124 /* Exported functions --------------------------------------------------------*/
125 
126 /** @addtogroup RNG_Exported_Functions
127   * @{
128   */
129 
130 /** @addtogroup RNG_Exported_Functions_Group1
131   *  @brief   Initialization and configuration functions
132   *
133 @verbatim
134  ===============================================================================
135           ##### Initialization and configuration functions #####
136  ===============================================================================
137     [..]  This section provides functions allowing to:
138       (+) Initialize the RNG according to the specified parameters
139           in the RNG_InitTypeDef and create the associated handle
140       (+) DeInitialize the RNG peripheral
141       (+) Initialize the RNG MSP
142       (+) DeInitialize RNG MSP
143 
144 @endverbatim
145   * @{
146   */
147 
148 /**
149   * @brief  Initializes the RNG peripheral and creates the associated handle.
150   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
151   *                the configuration information for RNG.
152   * @retval HAL status
153   */
HAL_RNG_Init(RNG_HandleTypeDef * hrng)154 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
155 {
156   uint32_t tickstart;
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   assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
165 
166 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
167   if (hrng->State == HAL_RNG_STATE_RESET)
168   {
169     /* Allocate lock resource and initialize it */
170     hrng->Lock = HAL_UNLOCKED;
171 
172     hrng->ReadyDataCallback  = HAL_RNG_ReadyDataCallback;  /* Legacy weak ReadyDataCallback  */
173     hrng->ErrorCallback      = HAL_RNG_ErrorCallback;      /* Legacy weak ErrorCallback      */
174 
175     if (hrng->MspInitCallback == NULL)
176     {
177       hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit  */
178     }
179 
180     /* Init the low level hardware */
181     hrng->MspInitCallback(hrng);
182   }
183 #else
184   if (hrng->State == HAL_RNG_STATE_RESET)
185   {
186     /* Allocate lock resource and initialize it */
187     hrng->Lock = HAL_UNLOCKED;
188 
189     /* Init the low level hardware */
190     HAL_RNG_MspInit(hrng);
191   }
192 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
193 
194   /* Change RNG peripheral state */
195   hrng->State = HAL_RNG_STATE_BUSY;
196 
197   /* Disable RNG */
198   __HAL_RNG_DISABLE(hrng);
199 
200   /* Clock Error Detection Configuration when CONDRT bit is set to 1 */
201   MODIFY_REG(hrng->Instance->CR, RNG_CR_CED | RNG_CR_CONDRST, hrng->Init.ClockErrorDetection | RNG_CR_CONDRST);
202 
203   /* Writing bit CONDRST=0 */
204   CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
205 
206   /* Get tick */
207   tickstart = HAL_GetTick();
208 
209   /* Wait for conditioning reset process to be completed */
210   while (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
211   {
212     if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
213     {
214       /* New check to avoid false timeout detection in case of preemption */
215       if (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
216       {
217         hrng->State = HAL_RNG_STATE_READY;
218         hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
219         return HAL_ERROR;
220       }
221     }
222   }
223 
224   /* Enable the RNG Peripheral */
225   __HAL_RNG_ENABLE(hrng);
226 
227   /* verify that no seed error */
228   if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
229   {
230     hrng->State = HAL_RNG_STATE_ERROR;
231     return HAL_ERROR;
232   }
233   /* Get tick */
234   tickstart = HAL_GetTick();
235   /* Check if data register contains valid random data */
236   while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) != SET)
237   {
238     if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
239     {
240       /* New check to avoid false timeout detection in case of preemption */
241       if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) != SET)
242       {
243         hrng->State = HAL_RNG_STATE_ERROR;
244         hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
245         return HAL_ERROR;
246       }
247     }
248   }
249 
250   /* Initialize the RNG state */
251   hrng->State = HAL_RNG_STATE_READY;
252 
253   /* Initialise the error code */
254   hrng->ErrorCode = HAL_RNG_ERROR_NONE;
255 
256   /* Return function status */
257   return HAL_OK;
258 }
259 
260 /**
261   * @brief  DeInitializes the RNG peripheral.
262   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
263   *                the configuration information for RNG.
264   * @retval HAL status
265   */
HAL_RNG_DeInit(RNG_HandleTypeDef * hrng)266 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
267 {
268   uint32_t tickstart;
269 
270   /* Check the RNG handle allocation */
271   if (hrng == NULL)
272   {
273     return HAL_ERROR;
274   }
275 
276   /* Clear Clock Error Detection bit when CONDRT bit is set to 1 */
277   MODIFY_REG(hrng->Instance->CR, RNG_CR_CED | RNG_CR_CONDRST, RNG_CED_ENABLE | RNG_CR_CONDRST);
278 
279   /* Writing bit CONDRST=0 */
280   CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
281 
282   /* Get tick */
283   tickstart = HAL_GetTick();
284 
285   /* Wait for conditioning reset process to be completed */
286   while (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
287   {
288     if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
289     {
290       /* New check to avoid false timeout detection in case of preemption */
291       if (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
292       {
293         hrng->State = HAL_RNG_STATE_READY;
294         hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
295         /* Process Unlocked */
296         __HAL_UNLOCK(hrng);
297         return HAL_ERROR;
298       }
299     }
300   }
301 
302   /* Disable the RNG Peripheral */
303   CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
304 
305   /* Clear RNG interrupt status flags */
306   CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
307 
308 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
309   if (hrng->MspDeInitCallback == NULL)
310   {
311     hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit  */
312   }
313 
314   /* DeInit the low level hardware */
315   hrng->MspDeInitCallback(hrng);
316 #else
317   /* DeInit the low level hardware */
318   HAL_RNG_MspDeInit(hrng);
319 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
320 
321   /* Update the RNG state */
322   hrng->State = HAL_RNG_STATE_RESET;
323 
324   /* Initialise the error code */
325   hrng->ErrorCode = HAL_RNG_ERROR_NONE;
326 
327   /* Release Lock */
328   __HAL_UNLOCK(hrng);
329 
330   /* Return the function status */
331   return HAL_OK;
332 }
333 
334 /**
335   * @brief  Initializes the RNG MSP.
336   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
337   *                the configuration information for RNG.
338   * @retval None
339   */
HAL_RNG_MspInit(RNG_HandleTypeDef * hrng)340 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
341 {
342   /* Prevent unused argument(s) compilation warning */
343   UNUSED(hrng);
344   /* NOTE : This function should not be modified. When the callback is needed,
345             function HAL_RNG_MspInit must be implemented in the user file.
346    */
347 }
348 
349 /**
350   * @brief  DeInitializes the RNG MSP.
351   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
352   *                the configuration information for RNG.
353   * @retval None
354   */
HAL_RNG_MspDeInit(RNG_HandleTypeDef * hrng)355 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
356 {
357   /* Prevent unused argument(s) compilation warning */
358   UNUSED(hrng);
359   /* NOTE : This function should not be modified. When the callback is needed,
360             function HAL_RNG_MspDeInit must be implemented in the user file.
361    */
362 }
363 
364 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
365 /**
366   * @brief  Register a User RNG Callback
367   *         To be used instead of the weak predefined callback
368   * @param  hrng RNG handle
369   * @param  CallbackID ID of the callback to be registered
370   *         This parameter can be one of the following values:
371   *          @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
372   *          @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
373   *          @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
374   * @param  pCallback pointer to the Callback function
375   * @retval HAL status
376   */
HAL_RNG_RegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID,pRNG_CallbackTypeDef pCallback)377 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID,
378                                            pRNG_CallbackTypeDef pCallback)
379 {
380   HAL_StatusTypeDef status = HAL_OK;
381 
382   if (pCallback == NULL)
383   {
384     /* Update the error code */
385     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
386     return HAL_ERROR;
387   }
388 
389   if (HAL_RNG_STATE_READY == hrng->State)
390   {
391     switch (CallbackID)
392     {
393       case HAL_RNG_ERROR_CB_ID :
394         hrng->ErrorCallback = pCallback;
395         break;
396 
397       case HAL_RNG_MSPINIT_CB_ID :
398         hrng->MspInitCallback = pCallback;
399         break;
400 
401       case HAL_RNG_MSPDEINIT_CB_ID :
402         hrng->MspDeInitCallback = pCallback;
403         break;
404 
405       default :
406         /* Update the error code */
407         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
408         /* Return error status */
409         status =  HAL_ERROR;
410         break;
411     }
412   }
413   else if (HAL_RNG_STATE_RESET == hrng->State)
414   {
415     switch (CallbackID)
416     {
417       case HAL_RNG_MSPINIT_CB_ID :
418         hrng->MspInitCallback = pCallback;
419         break;
420 
421       case HAL_RNG_MSPDEINIT_CB_ID :
422         hrng->MspDeInitCallback = pCallback;
423         break;
424 
425       default :
426         /* Update the error code */
427         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
428         /* Return error status */
429         status =  HAL_ERROR;
430         break;
431     }
432   }
433   else
434   {
435     /* Update the error code */
436     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
437     /* Return error status */
438     status =  HAL_ERROR;
439   }
440 
441   return status;
442 }
443 
444 /**
445   * @brief  Unregister an RNG Callback
446   *         RNG callback is redirected to the weak predefined callback
447   * @param  hrng RNG handle
448   * @param  CallbackID ID of the callback to be unregistered
449   *         This parameter can be one of the following values:
450   *          @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
451   *          @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
452   *          @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
453   * @retval HAL status
454   */
HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID)455 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
456 {
457   HAL_StatusTypeDef status = HAL_OK;
458 
459 
460   if (HAL_RNG_STATE_READY == hrng->State)
461   {
462     switch (CallbackID)
463     {
464       case HAL_RNG_ERROR_CB_ID :
465         hrng->ErrorCallback = HAL_RNG_ErrorCallback;          /* Legacy weak ErrorCallback  */
466         break;
467 
468       case HAL_RNG_MSPINIT_CB_ID :
469         hrng->MspInitCallback = HAL_RNG_MspInit;              /* Legacy weak MspInit  */
470         break;
471 
472       case HAL_RNG_MSPDEINIT_CB_ID :
473         hrng->MspDeInitCallback = HAL_RNG_MspDeInit;          /* Legacy weak MspDeInit  */
474         break;
475 
476       default :
477         /* Update the error code */
478         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
479         /* Return error status */
480         status =  HAL_ERROR;
481         break;
482     }
483   }
484   else if (HAL_RNG_STATE_RESET == hrng->State)
485   {
486     switch (CallbackID)
487     {
488       case HAL_RNG_MSPINIT_CB_ID :
489         hrng->MspInitCallback = HAL_RNG_MspInit;              /* Legacy weak MspInit  */
490         break;
491 
492       case HAL_RNG_MSPDEINIT_CB_ID :
493         hrng->MspDeInitCallback = HAL_RNG_MspDeInit;          /* Legacy weak MspInit  */
494         break;
495 
496       default :
497         /* Update the error code */
498         hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
499         /* Return error status */
500         status =  HAL_ERROR;
501         break;
502     }
503   }
504   else
505   {
506     /* Update the error code */
507     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
508     /* Return error status */
509     status =  HAL_ERROR;
510   }
511 
512   return status;
513 }
514 
515 /**
516   * @brief  Register Data Ready RNG Callback
517   *         To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
518   * @param  hrng RNG handle
519   * @param  pCallback pointer to the Data Ready Callback function
520   * @retval HAL status
521   */
HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef * hrng,pRNG_ReadyDataCallbackTypeDef pCallback)522 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
523 {
524   HAL_StatusTypeDef status = HAL_OK;
525 
526   if (pCallback == NULL)
527   {
528     /* Update the error code */
529     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
530     return HAL_ERROR;
531   }
532   /* Process locked */
533   __HAL_LOCK(hrng);
534 
535   if (HAL_RNG_STATE_READY == hrng->State)
536   {
537     hrng->ReadyDataCallback = pCallback;
538   }
539   else
540   {
541     /* Update the error code */
542     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
543     /* Return error status */
544     status =  HAL_ERROR;
545   }
546 
547   /* Release Lock */
548   __HAL_UNLOCK(hrng);
549   return status;
550 }
551 
552 /**
553   * @brief  UnRegister the Data Ready RNG Callback
554   *         Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
555   * @param  hrng RNG handle
556   * @retval HAL status
557   */
HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef * hrng)558 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
559 {
560   HAL_StatusTypeDef status = HAL_OK;
561 
562   /* Process locked */
563   __HAL_LOCK(hrng);
564 
565   if (HAL_RNG_STATE_READY == hrng->State)
566   {
567     hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback  */
568   }
569   else
570   {
571     /* Update the error code */
572     hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
573     /* Return error status */
574     status =  HAL_ERROR;
575   }
576 
577   /* Release Lock */
578   __HAL_UNLOCK(hrng);
579   return status;
580 }
581 
582 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
583 
584 /**
585   * @}
586   */
587 
588 /** @addtogroup RNG_Exported_Functions_Group2
589   *  @brief   Peripheral Control functions
590   *
591 @verbatim
592  ===============================================================================
593                       ##### Peripheral Control functions #####
594  ===============================================================================
595     [..]  This section provides functions allowing to:
596       (+) Get the 32 bit Random number
597       (+) Get the 32 bit Random number with interrupt enabled
598       (+) Handle RNG interrupt request
599 
600 @endverbatim
601   * @{
602   */
603 
604 /**
605   * @brief  Generates a 32-bit random number.
606   * @note   This function checks value of RNG_FLAG_DRDY flag to know if valid
607   *         random number is available in the DR register (RNG_FLAG_DRDY flag set
608   *         whenever a random number is available through the RNG_DR register).
609   *         After transitioning from 0 to 1 (random number available),
610   *         RNG_FLAG_DRDY flag remains high until output buffer becomes empty after reading
611   *         four words from the RNG_DR register, i.e. further function calls
612   *         will immediately return a new u32 random number (additional words are
613   *         available and can be read by the application, till RNG_FLAG_DRDY flag remains high).
614   * @note   When no more random number data is available in DR register, RNG_FLAG_DRDY
615   *         flag is automatically cleared.
616   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
617   *                the configuration information for RNG.
618   * @param  random32bit pointer to generated random number variable if successful.
619   * @retval HAL status
620   */
621 
HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef * hrng,uint32_t * random32bit)622 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
623 {
624   uint32_t tickstart;
625   HAL_StatusTypeDef status = HAL_OK;
626 
627   /* Process Locked */
628   __HAL_LOCK(hrng);
629 
630   /* Check RNG peripheral state */
631   if (hrng->State == HAL_RNG_STATE_READY)
632   {
633     /* Change RNG peripheral state */
634     hrng->State = HAL_RNG_STATE_BUSY;
635     /* Check if there is a seed error */
636     if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
637     {
638       /* Update the error code */
639       hrng->ErrorCode = HAL_RNG_ERROR_SEED;
640       /* Reset from seed error */
641       status = RNG_RecoverSeedError(hrng);
642       if (status == HAL_ERROR)
643       {
644         return status;
645       }
646     }
647 
648     /* Get tick */
649     tickstart = HAL_GetTick();
650 
651     /* Check if data register contains valid random data */
652     while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
653     {
654       if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
655       {
656         /* New check to avoid false timeout detection in case of preemption */
657         if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
658         {
659           hrng->State = HAL_RNG_STATE_READY;
660           hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
661           /* Process Unlocked */
662           __HAL_UNLOCK(hrng);
663           return HAL_ERROR;
664         }
665       }
666     }
667 
668     /* Get a 32bit Random number */
669     hrng->RandomNumber = hrng->Instance->DR;
670     /* In case of seed error, the value available in the RNG_DR register must not
671        be used as it may not have enough entropy */
672     if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
673     {
674       /* Update the error code and status */
675       hrng->ErrorCode = HAL_RNG_ERROR_SEED;
676       status = HAL_ERROR;
677     }
678     else /* No seed error */
679     {
680       *random32bit = hrng->RandomNumber;
681     }
682     hrng->State = HAL_RNG_STATE_READY;
683   }
684   else
685   {
686     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
687     status = HAL_ERROR;
688   }
689 
690   /* Process Unlocked */
691   __HAL_UNLOCK(hrng);
692 
693   return status;
694 }
695 
696 /**
697   * @brief  Generates a 32-bit random number in interrupt mode.
698   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
699   *                the configuration information for RNG.
700   * @retval HAL status
701   */
HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef * hrng)702 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
703 {
704   HAL_StatusTypeDef status = HAL_OK;
705 
706   /* Process Locked */
707   __HAL_LOCK(hrng);
708 
709   /* Check RNG peripheral state */
710   if (hrng->State == HAL_RNG_STATE_READY)
711   {
712     /* Change RNG peripheral state */
713     hrng->State = HAL_RNG_STATE_BUSY;
714 
715     /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
716     __HAL_RNG_ENABLE_IT(hrng);
717   }
718   else
719   {
720     /* Process Unlocked */
721     __HAL_UNLOCK(hrng);
722 
723     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
724     status = HAL_ERROR;
725   }
726 
727   return status;
728 }
729 
730 /**
731   * @brief  Handles RNG interrupt request.
732   * @note   In the case of a clock error, the RNG is no more able to generate
733   *         random numbers because the PLL48CLK clock is not correct. User has
734   *         to check that the clock controller is correctly configured to provide
735   *         the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
736   *         The clock error has no impact on the previously generated
737   *         random numbers, and the RNG_DR register contents can be used.
738   * @note   In the case of a seed error, the generation of random numbers is
739   *         interrupted as long as the SECS bit is '1'. If a number is
740   *         available in the RNG_DR register, it must not be used because it may
741   *         not have enough entropy. In this case, it is recommended to clear the
742   *         SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
743   *         the RNG peripheral to reinitialize and restart the RNG.
744   * @note   User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
745   *         or CEIS are set.
746   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
747   *                the configuration information for RNG.
748   * @retval None
749 
750   */
HAL_RNG_IRQHandler(RNG_HandleTypeDef * hrng)751 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
752 {
753   uint32_t rngclockerror = 0U;
754   uint32_t itflag   = hrng->Instance->SR;
755 
756   /* RNG clock error interrupt occurred */
757   if ((itflag & RNG_IT_CEI) == RNG_IT_CEI)
758   {
759     /* Update the error code */
760     hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
761     rngclockerror = 1U;
762   }
763   else if ((itflag & RNG_IT_SEI) == RNG_IT_SEI)
764   {
765     /* Check if Seed Error Current Status (SECS) is set */
766     if ((itflag & RNG_FLAG_SECS) != RNG_FLAG_SECS)
767     {
768       /* RNG IP performed the reset automatically (auto-reset) */
769       /* Clear bit SEIS */
770       CLEAR_BIT(hrng->Instance->SR, RNG_IT_SEI);
771     }
772     else
773     {
774       /* Seed Error has not been recovered : Update the error code */
775       hrng->ErrorCode = HAL_RNG_ERROR_SEED;
776       rngclockerror = 1U;
777       /* Disable the IT */
778       __HAL_RNG_DISABLE_IT(hrng);
779     }
780   }
781   else
782   {
783     /* Nothing to do */
784   }
785 
786   if (rngclockerror == 1U)
787   {
788     /* Change RNG peripheral state */
789     hrng->State = HAL_RNG_STATE_ERROR;
790 
791 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
792     /* Call registered Error callback */
793     hrng->ErrorCallback(hrng);
794 #else
795     /* Call legacy weak Error callback */
796     HAL_RNG_ErrorCallback(hrng);
797 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
798 
799     /* Clear the clock error flag */
800     __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
801 
802     return;
803   }
804 
805   /* Check RNG data ready interrupt occurred */
806   if ((itflag & RNG_IT_DRDY) == RNG_IT_DRDY)
807   {
808     /* Generate random number once, so disable the IT */
809     __HAL_RNG_DISABLE_IT(hrng);
810 
811     /* Get the 32bit Random number (DRDY flag automatically cleared) */
812     hrng->RandomNumber = hrng->Instance->DR;
813 
814     if (hrng->State != HAL_RNG_STATE_ERROR)
815     {
816       /* Change RNG peripheral state */
817       hrng->State = HAL_RNG_STATE_READY;
818       /* Process Unlocked */
819       __HAL_UNLOCK(hrng);
820 
821 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
822       /* Call registered Data Ready callback */
823       hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
824 #else
825       /* Call legacy weak Data Ready callback */
826       HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
827 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
828     }
829   }
830 }
831 
832 /**
833   * @brief  Read latest generated random number.
834   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
835   *                the configuration information for RNG.
836   * @retval random value
837   */
HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef * hrng)838 uint32_t HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef *hrng)
839 {
840   return (hrng->RandomNumber);
841 }
842 
843 /**
844   * @brief  Data Ready callback in non-blocking mode.
845   * @note   When RNG_FLAG_DRDY flag value is set, first random number has been read
846   *         from DR register in IRQ Handler and is provided as callback parameter.
847   *         Depending on valid data available in the conditioning output buffer,
848   *         additional words can be read by the application from DR register till
849   *         DRDY bit remains high.
850   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
851   *                the configuration information for RNG.
852   * @param  random32bit generated random number.
853   * @retval None
854   */
HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef * hrng,uint32_t random32bit)855 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
856 {
857   /* Prevent unused argument(s) compilation warning */
858   UNUSED(hrng);
859   UNUSED(random32bit);
860   /* NOTE : This function should not be modified. When the callback is needed,
861             function HAL_RNG_ReadyDataCallback must be implemented in the user file.
862    */
863 }
864 
865 /**
866   * @brief  RNG error callbacks.
867   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
868   *                the configuration information for RNG.
869   * @retval None
870   */
HAL_RNG_ErrorCallback(RNG_HandleTypeDef * hrng)871 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
872 {
873   /* Prevent unused argument(s) compilation warning */
874   UNUSED(hrng);
875   /* NOTE : This function should not be modified. When the callback is needed,
876             function HAL_RNG_ErrorCallback must be implemented in the user file.
877    */
878 }
879 /**
880   * @}
881   */
882 
883 
884 /** @addtogroup RNG_Exported_Functions_Group3
885   *  @brief   Peripheral State functions
886   *
887 @verbatim
888  ===============================================================================
889                       ##### Peripheral State functions #####
890  ===============================================================================
891     [..]
892     This subsection permits to get in run-time the status of the peripheral
893     and the data flow.
894 
895 @endverbatim
896   * @{
897   */
898 
899 /**
900   * @brief  Returns the RNG state.
901   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
902   *                the configuration information for RNG.
903   * @retval HAL state
904   */
HAL_RNG_GetState(const RNG_HandleTypeDef * hrng)905 HAL_RNG_StateTypeDef HAL_RNG_GetState(const RNG_HandleTypeDef *hrng)
906 {
907   return hrng->State;
908 }
909 
910 /**
911   * @brief  Return the RNG handle error code.
912   * @param  hrng: pointer to a RNG_HandleTypeDef structure.
913   * @retval RNG Error Code
914   */
HAL_RNG_GetError(const RNG_HandleTypeDef * hrng)915 uint32_t HAL_RNG_GetError(const RNG_HandleTypeDef *hrng)
916 {
917   /* Return RNG Error Code */
918   return hrng->ErrorCode;
919 }
920 /**
921   * @}
922   */
923 
924 /**
925   * @}
926   */
927 /* Private functions ---------------------------------------------------------*/
928 /** @addtogroup RNG_Private_Functions
929   * @{
930   */
931 
932 /**
933   * @brief  RNG sequence to recover from a seed error
934   * @param  hrng pointer to a RNG_HandleTypeDef structure.
935   * @retval HAL status
936   */
RNG_RecoverSeedError(RNG_HandleTypeDef * hrng)937 HAL_StatusTypeDef RNG_RecoverSeedError(RNG_HandleTypeDef *hrng)
938 {
939   __IO uint32_t count = 0U;
940 
941   /*Check if seed error current status (SECS)is set */
942   if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_SECS) == RESET)
943   {
944     /* RNG performed the reset automatically (auto-reset) */
945     /* Clear bit SEIS */
946     CLEAR_BIT(hrng->Instance->SR, RNG_IT_SEI);
947   }
948   else  /* Sequence to fully recover from a seed error*/
949   {
950     /* Writing bit CONDRST=1*/
951     SET_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
952     /* Writing bit CONDRST=0*/
953     CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
954 
955     /* Wait for conditioning reset process to be completed */
956     count = RNG_TIMEOUT_VALUE;
957     do
958     {
959       count-- ;
960       if (count == 0U)
961       {
962         hrng->State = HAL_RNG_STATE_READY;
963         hrng->ErrorCode |= HAL_RNG_ERROR_TIMEOUT;
964         /* Process Unlocked */
965         __HAL_UNLOCK(hrng);
966 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
967         /* Call registered Error callback */
968         hrng->ErrorCallback(hrng);
969 #else
970         /* Call legacy weak Error callback */
971         HAL_RNG_ErrorCallback(hrng);
972 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
973         return HAL_ERROR;
974       }
975     } while (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST));
976 
977     if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
978     {
979       /* Clear bit SEIS */
980       CLEAR_BIT(hrng->Instance->SR, RNG_IT_SEI);
981     }
982 
983     /* Wait for SECS to be cleared */
984     count = RNG_TIMEOUT_VALUE;
985     do
986     {
987       count-- ;
988       if (count == 0U)
989       {
990         hrng->State = HAL_RNG_STATE_READY;
991         hrng->ErrorCode |= HAL_RNG_ERROR_TIMEOUT;
992         /* Process Unlocked */
993         __HAL_UNLOCK(hrng);
994 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
995         /* Call registered Error callback */
996         hrng->ErrorCallback(hrng);
997 #else
998         /* Call legacy weak Error callback */
999         HAL_RNG_ErrorCallback(hrng);
1000 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
1001         return HAL_ERROR;
1002       }
1003     } while (HAL_IS_BIT_SET(hrng->Instance->SR, RNG_FLAG_SECS));
1004   }
1005   /* Update the error code */
1006   hrng->ErrorCode &= ~ HAL_RNG_ERROR_SEED;
1007   return HAL_OK;
1008 }
1009 
1010 /**
1011   * @}
1012   */
1013 
1014 
1015 #endif /* HAL_RNG_MODULE_ENABLED */
1016 /**
1017   * @}
1018   */
1019 
1020 #endif /* RNG */
1021 
1022 /**
1023   * @}
1024   */
1025 
1026