1 /**
2 ******************************************************************************
3 * @file stm32mp1xx_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 @verbatim
13 ==============================================================================
14 ##### How to use this driver #####
15 ==============================================================================
16 [..]
17 The RNG HAL driver can be used as follows:
18
19 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
20 in HAL_RNG_MspInit().
21 (#) Activate the RNG peripheral using HAL_RNG_Init() function.
22 (#) Wait until the 32 bit Random Number Generator contains a valid
23 random data using (polling/interrupt) mode.
24 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
25
26 ##### Callback registration #####
27 ==================================
28
29 [..]
30 The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
31 allows the user to configure dynamically the driver callbacks.
32
33 [..]
34 Use Function @ref HAL_RNG_RegisterCallback() to register a user callback.
35 Function @ref HAL_RNG_RegisterCallback() allows to register following callbacks:
36 (+) ErrorCallback : RNG Error Callback.
37 (+) MspInitCallback : RNG MspInit.
38 (+) MspDeInitCallback : RNG MspDeInit.
39 This function takes as parameters the HAL peripheral handle, the Callback ID
40 and a pointer to the user callback function.
41
42 [..]
43 Use function @ref HAL_RNG_UnRegisterCallback() to reset a callback to the default
44 weak (surcharged) function.
45 @ref HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
46 and the Callback ID.
47 This function allows to reset following callbacks:
48 (+) ErrorCallback : RNG Error Callback.
49 (+) MspInitCallback : RNG MspInit.
50 (+) MspDeInitCallback : RNG MspDeInit.
51
52 [..]
53 For specific callback ReadyDataCallback, use dedicated register callbacks:
54 respectively @ref HAL_RNG_RegisterReadyDataCallback() , @ref HAL_RNG_UnRegisterReadyDataCallback().
55
56 [..]
57 By default, after the @ref HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET
58 all callbacks are set to the corresponding weak (surcharged) functions:
59 example @ref HAL_RNG_ErrorCallback().
60 Exception done for MspInit and MspDeInit functions that are respectively
61 reset to the legacy weak (surcharged) functions in the @ref HAL_RNG_Init()
62 and @ref HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand).
63 If not, MspInit or MspDeInit are not null, the @ref HAL_RNG_Init() and @ref HAL_RNG_DeInit()
64 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
65
66 [..]
67 Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only.
68 Exception done MspInit/MspDeInit that can be registered/unregistered
69 in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user)
70 MspInit/DeInit callbacks can be used during the Init/DeInit.
71 In that case first register the MspInit/MspDeInit user callbacks
72 using @ref HAL_RNG_RegisterCallback() before calling @ref HAL_RNG_DeInit()
73 or @ref HAL_RNG_Init() function.
74
75 [..]
76 When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or
77 not defined, the callback registration feature is not available
78 and weak (surcharged) callbacks are used.
79
80 @endverbatim
81 ******************************************************************************
82 * @attention
83 *
84 * <h2><center>© Copyright (c) 2019 STMicroelectronics.
85 * All rights reserved.</center></h2>
86 *
87 * This software component is licensed by ST under BSD 3-Clause license,
88 * the "License"; You may not use this file except in compliance with the
89 * License. You may obtain a copy of the License at:
90 * opensource.org/licenses/BSD-3-Clause
91 *
92 ******************************************************************************
93 */
94
95 /* Includes ------------------------------------------------------------------*/
96 #include "stm32mp1xx_hal.h"
97
98 /** @addtogroup STM32MP1xx_HAL_Driver
99 * @{
100 */
101
102 #if defined (RNG1) || defined (RNG2)
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 10U
119 /**
120 * @}
121 */
122 /* Private macros ------------------------------------------------------------*/
123 /* Private functions prototypes ----------------------------------------------*/
124 /* Private functions ---------------------------------------------------------*/
125 /* Exported functions --------------------------------------------------------*/
126
127 /** @addtogroup RNG_Exported_Functions
128 * @{
129 */
130
131 /** @addtogroup RNG_Exported_Functions_Group1
132 * @brief Initialization and configuration functions
133 *
134 @verbatim
135 ===============================================================================
136 ##### Initialization and configuration functions #####
137 ===============================================================================
138 [..] This section provides functions allowing to:
139 (+) Initialize the RNG according to the specified parameters
140 in the RNG_InitTypeDef and create the associated handle
141 (+) DeInitialize the RNG peripheral
142 (+) Initialize the RNG MSP
143 (+) DeInitialize RNG MSP
144
145 @endverbatim
146 * @{
147 */
148
149 /**
150 * @brief Initializes the RNG peripheral and creates the associated handle.
151 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
152 * the configuration information for RNG.
153 * @retval HAL status
154 */
HAL_RNG_Init(RNG_HandleTypeDef * hrng)155 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
156 {
157 /* Check the RNG handle allocation */
158 if (hrng == NULL)
159 {
160 return HAL_ERROR;
161 }
162 /* Check the parameters */
163 // assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
164 assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
165 // dprintf("--------------%d----%x\r\n",__LINE__,hrng->Instance);
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 if (hrng->MspInitCallback == NULL)
175 {
176 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
177 }
178 /* Init the low level hardware */
179 hrng->MspInitCallback(hrng);
180 }
181 #else
182 if (hrng->State == HAL_RNG_STATE_RESET)
183 {
184 /* Allocate lock resource and initialize it */
185 hrng->Lock = HAL_UNLOCKED;
186 /* Init the low level hardware */
187 HAL_RNG_MspInit(hrng);
188 }
189 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
190 /* Change RNG peripheral state */
191 hrng->State = HAL_RNG_STATE_BUSY;
192 /* Clock Error Detection Configuration */
193 MODIFY_REG(hrng->Instance->CR, RNG_CR_CED, hrng->Init.ClockErrorDetection);
194 /* Enable the RNG Peripheral */
195 __HAL_RNG_ENABLE(hrng);
196
197 /* Initialize the RNG state */
198 hrng->State = HAL_RNG_STATE_READY;
199
200 /* Initialise the error code */
201 hrng->ErrorCode = HAL_RNG_ERROR_NONE;
202 /* Return function status */
203 return HAL_OK;
204 }
205
206 /**
207 * @brief DeInitializes the RNG peripheral.
208 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
209 * the configuration information for RNG.
210 * @retval HAL status
211 */
HAL_RNG_DeInit(RNG_HandleTypeDef * hrng)212 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
213 {
214 /* Check the RNG handle allocation */
215 if (hrng == NULL)
216 {
217 return HAL_ERROR;
218 }
219
220 /* Clear Clock Error Detection bit */
221 CLEAR_BIT(hrng->Instance->CR, RNG_CR_CED);
222 /* Disable the RNG Peripheral */
223 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
224
225 /* Clear RNG interrupt status flags */
226 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
227
228 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
229 if (hrng->MspDeInitCallback == NULL)
230 {
231 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
232 }
233
234 /* DeInit the low level hardware */
235 hrng->MspDeInitCallback(hrng);
236 #else
237 /* DeInit the low level hardware */
238 HAL_RNG_MspDeInit(hrng);
239 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
240
241 /* Update the RNG state */
242 hrng->State = HAL_RNG_STATE_RESET;
243
244 /* Initialise the error code */
245 hrng->ErrorCode = HAL_RNG_ERROR_NONE;
246
247 /* Release Lock */
248 __HAL_UNLOCK(hrng);
249
250 /* Return the function status */
251 return HAL_OK;
252 }
253
254 /**
255 * @brief Initializes the RNG MSP.
256 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
257 * the configuration information for RNG.
258 * @retval None
259 */
HAL_RNG_MspInit(RNG_HandleTypeDef * hrng)260 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
261 {
262 /* Prevent unused argument(s) compilation warning */
263 UNUSED(hrng);
264 /* NOTE : This function should not be modified. When the callback is needed,
265 function HAL_RNG_MspInit must be implemented in the user file.
266 */
267 }
268
269 /**
270 * @brief DeInitializes the RNG MSP.
271 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
272 * the configuration information for RNG.
273 * @retval None
274 */
HAL_RNG_MspDeInit(RNG_HandleTypeDef * hrng)275 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
276 {
277 /* Prevent unused argument(s) compilation warning */
278 UNUSED(hrng);
279 /* NOTE : This function should not be modified. When the callback is needed,
280 function HAL_RNG_MspDeInit must be implemented in the user file.
281 */
282 }
283
284 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
285 /**
286 * @brief Register a User RNG Callback
287 * To be used instead of the weak predefined callback
288 * @param hrng RNG handle
289 * @param CallbackID ID of the callback to be registered
290 * This parameter can be one of the following values:
291 * @arg @ref HAL_RNG_ERROR_CB_ID Error callback ID
292 * @arg @ref HAL_RNG_MSPINIT_CB_ID MspInit callback ID
293 * @arg @ref HAL_RNG_MSPDEINIT_CB_ID MspDeInit callback ID
294 * @param pCallback pointer to the Callback function
295 * @retval HAL status
296 */
HAL_RNG_RegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID,pRNG_CallbackTypeDef pCallback)297 HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback)
298 {
299 HAL_StatusTypeDef status = HAL_OK;
300
301 if (pCallback == NULL)
302 {
303 /* Update the error code */
304 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
305 return HAL_ERROR;
306 }
307 /* Process locked */
308 __HAL_LOCK(hrng);
309
310 if (HAL_RNG_STATE_READY == hrng->State)
311 {
312 switch (CallbackID)
313 {
314 case HAL_RNG_ERROR_CB_ID :
315 hrng->ErrorCallback = pCallback;
316 break;
317
318 case HAL_RNG_MSPINIT_CB_ID :
319 hrng->MspInitCallback = pCallback;
320 break;
321
322 case HAL_RNG_MSPDEINIT_CB_ID :
323 hrng->MspDeInitCallback = pCallback;
324 break;
325
326 default :
327 /* Update the error code */
328 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
329 /* Return error status */
330 status = HAL_ERROR;
331 break;
332 }
333 }
334 else if (HAL_RNG_STATE_RESET == hrng->State)
335 {
336 switch (CallbackID)
337 {
338 case HAL_RNG_MSPINIT_CB_ID :
339 hrng->MspInitCallback = pCallback;
340 break;
341
342 case HAL_RNG_MSPDEINIT_CB_ID :
343 hrng->MspDeInitCallback = pCallback;
344 break;
345
346 default :
347 /* Update the error code */
348 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
349 /* Return error status */
350 status = HAL_ERROR;
351 break;
352 }
353 }
354 else
355 {
356 /* Update the error code */
357 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
358 /* Return error status */
359 status = HAL_ERROR;
360 }
361
362 /* Release Lock */
363 __HAL_UNLOCK(hrng);
364 return status;
365 }
366
367 /**
368 * @brief Unregister an RNG Callback
369 * RNG callabck is redirected to the weak predefined callback
370 * @param hrng RNG handle
371 * @param CallbackID ID of the callback to be unregistered
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 * @retval HAL status
377 */
HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef * hrng,HAL_RNG_CallbackIDTypeDef CallbackID)378 HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
379 {
380 HAL_StatusTypeDef status = HAL_OK;
381
382 /* Process locked */
383 __HAL_LOCK(hrng);
384
385 if (HAL_RNG_STATE_READY == hrng->State)
386 {
387 switch (CallbackID)
388 {
389 case HAL_RNG_ERROR_CB_ID :
390 hrng->ErrorCallback = HAL_RNG_ErrorCallback; /* Legacy weak ErrorCallback */
391 break;
392
393 case HAL_RNG_MSPINIT_CB_ID :
394 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
395 break;
396
397 case HAL_RNG_MSPDEINIT_CB_ID :
398 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit */
399 break;
400
401 default :
402 /* Update the error code */
403 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
404 /* Return error status */
405 status = HAL_ERROR;
406 break;
407 }
408 }
409 else if (HAL_RNG_STATE_RESET == hrng->State)
410 {
411 switch (CallbackID)
412 {
413 case HAL_RNG_MSPINIT_CB_ID :
414 hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit */
415 break;
416
417 case HAL_RNG_MSPDEINIT_CB_ID :
418 hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspInit */
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
430 {
431 /* Update the error code */
432 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
433 /* Return error status */
434 status = HAL_ERROR;
435 }
436
437 /* Release Lock */
438 __HAL_UNLOCK(hrng);
439 return status;
440 }
441
442 /**
443 * @brief Register Data Ready RNG Callback
444 * To be used instead of the weak HAL_RNG_ReadyDataCallback() predefined callback
445 * @param hrng RNG handle
446 * @param pCallback pointer to the Data Ready Callback function
447 * @retval HAL status
448 */
HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef * hrng,pRNG_ReadyDataCallbackTypeDef pCallback)449 HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
450 {
451 HAL_StatusTypeDef status = HAL_OK;
452
453 if (pCallback == NULL)
454 {
455 /* Update the error code */
456 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
457 return HAL_ERROR;
458 }
459 /* Process locked */
460 __HAL_LOCK(hrng);
461
462 if (HAL_RNG_STATE_READY == hrng->State)
463 {
464 hrng->ReadyDataCallback = pCallback;
465 }
466 else
467 {
468 /* Update the error code */
469 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
470 /* Return error status */
471 status = HAL_ERROR;
472 }
473
474 /* Release Lock */
475 __HAL_UNLOCK(hrng);
476 return status;
477 }
478
479 /**
480 * @brief UnRegister the Data Ready RNG Callback
481 * Data Ready RNG Callback is redirected to the weak HAL_RNG_ReadyDataCallback() predefined callback
482 * @param hrng RNG handle
483 * @retval HAL status
484 */
HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef * hrng)485 HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
486 {
487 HAL_StatusTypeDef status = HAL_OK;
488
489 /* Process locked */
490 __HAL_LOCK(hrng);
491
492 if (HAL_RNG_STATE_READY == hrng->State)
493 {
494 hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback; /* Legacy weak ReadyDataCallback */
495 }
496 else
497 {
498 /* Update the error code */
499 hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
500 /* Return error status */
501 status = HAL_ERROR;
502 }
503
504 /* Release Lock */
505 __HAL_UNLOCK(hrng);
506 return status;
507 }
508
509 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
510
511 /**
512 * @}
513 */
514
515 /** @addtogroup RNG_Exported_Functions_Group2
516 * @brief Peripheral Control functions
517 *
518 @verbatim
519 ===============================================================================
520 ##### Peripheral Control functions #####
521 ===============================================================================
522 [..] This section provides functions allowing to:
523 (+) Get the 32 bit Random number
524 (+) Get the 32 bit Random number with interrupt enabled
525 (+) Handle RNG interrupt request
526
527 @endverbatim
528 * @{
529 */
530
531 /**
532 * @brief Generates a 32-bit random number.
533 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
534 * is automatically cleared.
535 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
536 * the configuration information for RNG.
537 * @param random32bit pointer to generated random number variable if successful.
538 * @retval HAL status
539 */
540
HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef * hrng,uint32_t * random32bit)541 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
542 {
543 uint32_t tickstart;
544 HAL_StatusTypeDef status = HAL_OK;
545
546 /* Process Locked */
547 __HAL_LOCK(hrng);
548 /* Check RNG peripheral state */
549 if (hrng->State == HAL_RNG_STATE_READY)
550 {
551 /* Change RNG peripheral state */
552 hrng->State = HAL_RNG_STATE_BUSY;
553
554 /* Get tick */
555 tickstart = HAL_GetTick();
556
557 /* Check if data register contains valid random data */
558 while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
559 {
560 if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
561 {
562 hrng->State = HAL_RNG_STATE_READY;
563 hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
564 /* Process Unlocked */
565 __HAL_UNLOCK(hrng);
566 return HAL_ERROR;
567 }
568 }
569
570 /* Get a 32bit Random number */
571 hrng->RandomNumber = hrng->Instance->DR;
572 *random32bit = hrng->RandomNumber;
573 hrng->State = HAL_RNG_STATE_READY;
574 }
575 else
576 {
577 hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
578 status = HAL_ERROR;
579 }
580
581 /* Process Unlocked */
582 __HAL_UNLOCK(hrng);
583 return status;
584 }
585
586 /**
587 * @brief Generates a 32-bit random number in interrupt mode.
588 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
589 * the configuration information for RNG.
590 * @retval HAL status
591 */
HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef * hrng)592 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
593 {
594 HAL_StatusTypeDef status = HAL_OK;
595
596 /* Process Locked */
597 __HAL_LOCK(hrng);
598
599 /* Check RNG peripheral state */
600 if (hrng->State == HAL_RNG_STATE_READY)
601 {
602 /* Change RNG peripheral state */
603 hrng->State = HAL_RNG_STATE_BUSY;
604
605 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
606 __HAL_RNG_ENABLE_IT(hrng);
607 }
608 else
609 {
610 /* Process Unlocked */
611 __HAL_UNLOCK(hrng);
612
613 hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
614 status = HAL_ERROR;
615 }
616
617 return status;
618 }
619
620 /**
621 * @brief Handles RNG interrupt request.
622 * @note In the case of a clock error, the RNG is no more able to generate
623 * random numbers because the PLL48CLK clock is not correct. User has
624 * to check that the clock controller is correctly configured to provide
625 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
626 * The clock error has no impact on the previously generated
627 * random numbers, and the RNG_DR register contents can be used.
628 * @note In the case of a seed error, the generation of random numbers is
629 * interrupted as long as the SECS bit is '1'. If a number is
630 * available in the RNG_DR register, it must not be used because it may
631 * not have enough entropy. In this case, it is recommended to clear the
632 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
633 * the RNG peripheral to reinitialize and restart the RNG.
634 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
635 * or CEIS are set.
636 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
637 * the configuration information for RNG.
638 * @retval None
639
640 */
HAL_RNG_IRQHandler(RNG_HandleTypeDef * hrng)641 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
642 {
643 uint32_t rngclockerror = 0U;
644
645 /* RNG clock error interrupt occurred */
646 if (__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET)
647 {
648 /* Update the error code */
649 hrng->ErrorCode = HAL_RNG_ERROR_SEED;
650 rngclockerror = 1U;
651 }
652 else if (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET)
653 {
654 /* Update the error code */
655 hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
656 rngclockerror = 1U;
657 }
658 else
659 {
660 /* Nothing to do */
661 }
662
663 if (rngclockerror == 1U)
664 {
665 /* Change RNG peripheral state */
666 hrng->State = HAL_RNG_STATE_ERROR;
667
668 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
669 /* Call registered Error callback */
670 hrng->ErrorCallback(hrng);
671 #else
672 /* Call legacy weak Error callback */
673 HAL_RNG_ErrorCallback(hrng);
674 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
675
676 /* Clear the clock error flag */
677 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
678 }
679
680 /* Check RNG data ready interrupt occurred */
681 if (__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
682 {
683 /* Generate random number once, so disable the IT */
684 __HAL_RNG_DISABLE_IT(hrng);
685
686 /* Get the 32bit Random number (DRDY flag automatically cleared) */
687 hrng->RandomNumber = hrng->Instance->DR;
688
689 if (hrng->State != HAL_RNG_STATE_ERROR)
690 {
691 /* Change RNG peripheral state */
692 hrng->State = HAL_RNG_STATE_READY;
693 /* Process Unlocked */
694 __HAL_UNLOCK(hrng);
695
696 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
697 /* Call registered Data Ready callback */
698 hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
699 #else
700 /* Call legacy weak Data Ready callback */
701 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
702 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
703 }
704 }
705 }
706
707 /**
708 * @brief Read latest generated random number.
709 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
710 * the configuration information for RNG.
711 * @retval random value
712 */
HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef * hrng)713 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
714 {
715 return (hrng->RandomNumber);
716 }
717
718 /**
719 * @brief Data Ready callback in non-blocking mode.
720 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
721 * the configuration information for RNG.
722 * @param random32bit generated random number.
723 * @retval None
724 */
HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef * hrng,uint32_t random32bit)725 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
726 {
727 /* Prevent unused argument(s) compilation warning */
728 UNUSED(hrng);
729 UNUSED(random32bit);
730 /* NOTE : This function should not be modified. When the callback is needed,
731 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
732 */
733 }
734
735 /**
736 * @brief RNG error callbacks.
737 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
738 * the configuration information for RNG.
739 * @retval None
740 */
HAL_RNG_ErrorCallback(RNG_HandleTypeDef * hrng)741 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
742 {
743 /* Prevent unused argument(s) compilation warning */
744 UNUSED(hrng);
745 /* NOTE : This function should not be modified. When the callback is needed,
746 function HAL_RNG_ErrorCallback must be implemented in the user file.
747 */
748 }
749 /**
750 * @}
751 */
752
753
754 /** @addtogroup RNG_Exported_Functions_Group3
755 * @brief Peripheral State functions
756 *
757 @verbatim
758 ===============================================================================
759 ##### Peripheral State functions #####
760 ===============================================================================
761 [..]
762 This subsection permits to get in run-time the status of the peripheral
763 and the data flow.
764
765 @endverbatim
766 * @{
767 */
768
769 /**
770 * @brief Returns the RNG state.
771 * @param hrng pointer to a RNG_HandleTypeDef structure that contains
772 * the configuration information for RNG.
773 * @retval HAL state
774 */
HAL_RNG_GetState(RNG_HandleTypeDef * hrng)775 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
776 {
777 return hrng->State;
778 }
779
780 /**
781 * @brief Return the RNG handle error code.
782 * @param hrng: pointer to a RNG_HandleTypeDef structure.
783 * @retval RNG Error Code
784 */
HAL_RNG_GetError(RNG_HandleTypeDef * hrng)785 uint32_t HAL_RNG_GetError(RNG_HandleTypeDef *hrng)
786 {
787 /* Return RNG Error Code */
788 return hrng->ErrorCode;
789 }
790 /**
791 * @}
792 */
793
794 /**
795 * @}
796 */
797
798
799 #endif /* HAL_RNG_MODULE_ENABLED */
800 /**
801 * @}
802 */
803
804 #endif /* RNG1 || RNG2 */
805
806 /**
807 * @}
808 */
809
810 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
811