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