1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
101
102
103
104
108
109
110
111
112
113
114
123
124
125
126
127
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
155
156
157
158
162
163
164
165
166
167
168
169
170
171
172
173
174
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
216
217
218
219
223
224
225
226
227
228
229
230
231
238
239
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
262
270
271
277
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
310
311
335
355
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
406
426
433
434
435
436
437
444
445
446
447
448
454
455
456
457
461
468
469
470
471
472
473
474
480
481
482
483
484
485
486
487
491
498
499
500
501
502
503
504
505
506
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
581
582
583
584
585
586
587
588
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
629
640
641
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
701
707
711
712
713
714
715
716
717
718
720
721
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
750
751
753
754
755
756
757
758
764
768
769
776
785
786
792
800
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
826
830
831
836
841
844
845
848
849
850
851
854
855
856
857
860
/* ... */
#include "stm32f4xx_hal.h"
/* ... */
#if defined (RNG)
/* ... */
#ifdef HAL_RNG_MODULE_ENABLED
/* ... */
#define RNG_TIMEOUT_VALUE 2U
/* ... */
Private constants
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
{
if (hrng == NULL)
{
return HAL_ERROR;
}if (hrng == NULL) { ... }
assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
#if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
if (hrng->State == HAL_RNG_STATE_RESET)
{
hrng->Lock = HAL_UNLOCKED;
hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback;
hrng->ErrorCallback = HAL_RNG_ErrorCallback;
if (hrng->MspInitCallback == NULL)
{
hrng->MspInitCallback = HAL_RNG_MspInit;
}if (hrng->MspInitCallback == NULL) { ... }
hrng->MspInitCallback(hrng);
}if (hrng->State == HAL_RNG_STATE_RESET) { ... }
/* ... */#else
if (hrng->State == HAL_RNG_STATE_RESET)
{
hrng->Lock = HAL_UNLOCKED;
HAL_RNG_MspInit(hrng);
}if (hrng->State == HAL_RNG_STATE_RESET) { ... }
/* ... */#endif
hrng->State = HAL_RNG_STATE_BUSY;
__HAL_RNG_ENABLE(hrng);
hrng->State = HAL_RNG_STATE_READY;
hrng->ErrorCode = HAL_RNG_ERROR_NONE;
return HAL_OK;
}HAL_RNG_Init (RNG_HandleTypeDef *hrng) { ... }
/* ... */
HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
{
if (hrng == NULL)
{
return HAL_ERROR;
}if (hrng == NULL) { ... }
CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
#if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
if (hrng->MspDeInitCallback == NULL)
{
hrng->MspDeInitCallback = HAL_RNG_MspDeInit;
}if (hrng->MspDeInitCallback == NULL) { ... }
hrng->MspDeInitCallback(hrng);/* ... */
#else
HAL_RNG_MspDeInit(hrng);/* ... */
#endif
hrng->State = HAL_RNG_STATE_RESET;
hrng->ErrorCode = HAL_RNG_ERROR_NONE;
__HAL_UNLOCK(hrng);
return HAL_OK;
}HAL_RNG_DeInit (RNG_HandleTypeDef *hrng) { ... }
/* ... */
__weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
{
UNUSED(hrng);
/* ... */
}HAL_RNG_MspInit (RNG_HandleTypeDef *hrng) { ... }
/* ... */
__weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
{
UNUSED(hrng);
/* ... */
}HAL_RNG_MspDeInit (RNG_HandleTypeDef *hrng) { ... }
#if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
/* ... */
HAL_StatusTypeDef HAL_RNG_RegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID,
pRNG_CallbackTypeDef pCallback)
{
HAL_StatusTypeDef status = HAL_OK;
if (pCallback == NULL)
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
return HAL_ERROR;
}if (pCallback == NULL) { ... }
if (HAL_RNG_STATE_READY == hrng->State)
{
switch (CallbackID)
{
case HAL_RNG_ERROR_CB_ID :
hrng->ErrorCallback = pCallback;
break;
case HAL_RNG_ERROR_CB_ID :
case HAL_RNG_MSPINIT_CB_ID :
hrng->MspInitCallback = pCallback;
break;
case HAL_RNG_MSPINIT_CB_ID :
case HAL_RNG_MSPDEINIT_CB_ID :
hrng->MspDeInitCallback = pCallback;
break;
case HAL_RNG_MSPDEINIT_CB_ID :
default :
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}if (HAL_RNG_STATE_READY == hrng->State) { ... }
else if (HAL_RNG_STATE_RESET == hrng->State)
{
switch (CallbackID)
{
case HAL_RNG_MSPINIT_CB_ID :
hrng->MspInitCallback = pCallback;
break;
case HAL_RNG_MSPINIT_CB_ID :
case HAL_RNG_MSPDEINIT_CB_ID :
hrng->MspDeInitCallback = pCallback;
break;
case HAL_RNG_MSPDEINIT_CB_ID :
default :
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}else if (HAL_RNG_STATE_RESET == hrng->State) { ... }
else
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
return status;
}HAL_RNG_RegisterCallback (RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID, pRNG_CallbackTypeDef pCallback) { ... }
/* ... */
HAL_StatusTypeDef HAL_RNG_UnRegisterCallback(RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID)
{
HAL_StatusTypeDef status = HAL_OK;
if (HAL_RNG_STATE_READY == hrng->State)
{
switch (CallbackID)
{
case HAL_RNG_ERROR_CB_ID :
hrng->ErrorCallback = HAL_RNG_ErrorCallback;
break;
case HAL_RNG_ERROR_CB_ID :
case HAL_RNG_MSPINIT_CB_ID :
hrng->MspInitCallback = HAL_RNG_MspInit;
break;
case HAL_RNG_MSPINIT_CB_ID :
case HAL_RNG_MSPDEINIT_CB_ID :
hrng->MspDeInitCallback = HAL_RNG_MspDeInit;
break;
case HAL_RNG_MSPDEINIT_CB_ID :
default :
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}if (HAL_RNG_STATE_READY == hrng->State) { ... }
else if (HAL_RNG_STATE_RESET == hrng->State)
{
switch (CallbackID)
{
case HAL_RNG_MSPINIT_CB_ID :
hrng->MspInitCallback = HAL_RNG_MspInit;
break;
case HAL_RNG_MSPINIT_CB_ID :
case HAL_RNG_MSPDEINIT_CB_ID :
hrng->MspDeInitCallback = HAL_RNG_MspDeInit;
break;
case HAL_RNG_MSPDEINIT_CB_ID :
default :
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}else if (HAL_RNG_STATE_RESET == hrng->State) { ... }
else
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
return status;
}HAL_RNG_UnRegisterCallback (RNG_HandleTypeDef *hrng, HAL_RNG_CallbackIDTypeDef CallbackID) { ... }
/* ... */
HAL_StatusTypeDef HAL_RNG_RegisterReadyDataCallback(RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback)
{
HAL_StatusTypeDef status = HAL_OK;
if (pCallback == NULL)
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
return HAL_ERROR;
}if (pCallback == NULL) { ... }
__HAL_LOCK(hrng);
if (HAL_RNG_STATE_READY == hrng->State)
{
hrng->ReadyDataCallback = pCallback;
}if (HAL_RNG_STATE_READY == hrng->State) { ... }
else
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
__HAL_UNLOCK(hrng);
return status;
}HAL_RNG_RegisterReadyDataCallback (RNG_HandleTypeDef *hrng, pRNG_ReadyDataCallbackTypeDef pCallback) { ... }
/* ... */
HAL_StatusTypeDef HAL_RNG_UnRegisterReadyDataCallback(RNG_HandleTypeDef *hrng)
{
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(hrng);
if (HAL_RNG_STATE_READY == hrng->State)
{
hrng->ReadyDataCallback = HAL_RNG_ReadyDataCallback;
}if (HAL_RNG_STATE_READY == hrng->State) { ... }
else
{
hrng->ErrorCode = HAL_RNG_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
__HAL_UNLOCK(hrng);
return status;
}HAL_RNG_UnRegisterReadyDataCallback (RNG_HandleTypeDef *hrng) { ... }
/* ... */
#endif
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
{
uint32_t tickstart;
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(hrng);
if (hrng->State == HAL_RNG_STATE_READY)
{
hrng->State = HAL_RNG_STATE_BUSY;
tickstart = HAL_GetTick();
while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
{
if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
{
if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
{
hrng->State = HAL_RNG_STATE_READY;
hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
__HAL_UNLOCK(hrng);
return HAL_ERROR;
}if (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET) { ... }
}if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE) { ... }
}while (__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET) { ... }
hrng->RandomNumber = hrng->Instance->DR;
*random32bit = hrng->RandomNumber;
hrng->State = HAL_RNG_STATE_READY;
}if (hrng->State == HAL_RNG_STATE_READY) { ... }
else
{
hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
status = HAL_ERROR;
}else { ... }
__HAL_UNLOCK(hrng);
return status;
}HAL_RNG_GenerateRandomNumber (RNG_HandleTypeDef *hrng, uint32_t *random32bit) { ... }
/* ... */
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
{
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(hrng);
if (hrng->State == HAL_RNG_STATE_READY)
{
hrng->State = HAL_RNG_STATE_BUSY;
__HAL_RNG_ENABLE_IT(hrng);
}if (hrng->State == HAL_RNG_STATE_READY) { ... }
else
{
__HAL_UNLOCK(hrng);
hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
status = HAL_ERROR;
}else { ... }
return status;
}HAL_RNG_GenerateRandomNumber_IT (RNG_HandleTypeDef *hrng) { ... }
/* ... */
uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
{
if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
{
return hrng->RandomNumber;
}if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK) { ... }
else
{
return 0U;
}else { ... }
}HAL_RNG_GetRandomNumber (RNG_HandleTypeDef *hrng) { ... }
/* ... */
uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
{
uint32_t random32bit = 0U;
__HAL_LOCK(hrng);
hrng->State = HAL_RNG_STATE_BUSY;
random32bit = hrng->Instance->DR;
__HAL_RNG_ENABLE_IT(hrng);
return random32bit;
}HAL_RNG_GetRandomNumber_IT (RNG_HandleTypeDef *hrng) { ... }
/* ... */
void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
{
uint32_t rngclockerror = 0U;
uint32_t itflag = hrng->Instance->SR;
if ((itflag & RNG_IT_CEI) == RNG_IT_CEI)
{
hrng->ErrorCode = HAL_RNG_ERROR_CLOCK;
rngclockerror = 1U;
}if ((itflag & RNG_IT_CEI) == RNG_IT_CEI) { ... }
else if ((itflag & RNG_IT_SEI) == RNG_IT_SEI)
{
hrng->ErrorCode = HAL_RNG_ERROR_SEED;
rngclockerror = 1U;
}else if ((itflag & RNG_IT_SEI) == RNG_IT_SEI) { ... }
else
{
}else { ... }
if (rngclockerror == 1U)
{
hrng->State = HAL_RNG_STATE_ERROR;
#if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
hrng->ErrorCallback(hrng);/* ... */
#else
HAL_RNG_ErrorCallback(hrng);/* ... */
#endif
__HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI | RNG_IT_SEI);
return;
}if (rngclockerror == 1U) { ... }
if ((itflag & RNG_IT_DRDY) == RNG_IT_DRDY)
{
__HAL_RNG_DISABLE_IT(hrng);
hrng->RandomNumber = hrng->Instance->DR;
if (hrng->State != HAL_RNG_STATE_ERROR)
{
hrng->State = HAL_RNG_STATE_READY;
__HAL_UNLOCK(hrng);
#if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
hrng->ReadyDataCallback(hrng, hrng->RandomNumber);/* ... */
#else
HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);/* ... */
#endif
}if (hrng->State != HAL_RNG_STATE_ERROR) { ... }
}if ((itflag & RNG_IT_DRDY) == RNG_IT_DRDY) { ... }
}HAL_RNG_IRQHandler (RNG_HandleTypeDef *hrng) { ... }
/* ... */
uint32_t HAL_RNG_ReadLastRandomNumber(const RNG_HandleTypeDef *hrng)
{
return (hrng->RandomNumber);
}HAL_RNG_ReadLastRandomNumber (const RNG_HandleTypeDef *hrng) { ... }
/* ... */
__weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
{
UNUSED(hrng);
UNUSED(random32bit);
/* ... */
}HAL_RNG_ReadyDataCallback (RNG_HandleTypeDef *hrng, uint32_t random32bit) { ... }
/* ... */
__weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
{
UNUSED(hrng);
/* ... */
}HAL_RNG_ErrorCallback (RNG_HandleTypeDef *hrng) { ... }
/* ... */
/* ... */
/* ... */
HAL_RNG_StateTypeDef HAL_RNG_GetState(const RNG_HandleTypeDef *hrng)
{
return hrng->State;
}HAL_RNG_GetState (const RNG_HandleTypeDef *hrng) { ... }
/* ... */
uint32_t HAL_RNG_GetError(const RNG_HandleTypeDef *hrng)
{
return hrng->ErrorCode;
}HAL_RNG_GetError (const RNG_HandleTypeDef *hrng) { ... }
/* ... */
/* ... */
/* ... */
#endif
/* ... */
/* ... */
#endif
/* ... */