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