1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
32
33
34
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
117
118
123
124
125
126
127
128
129
130
131
132
135
136
137
138
139
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
196
197
198
199
200
201
203
204
205
206
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
231
236
237
238
239
240
241
242
243
244
245
246
255
256
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
287
288
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
328
329
330
331
332
333
334
335
336
337
340
341
342
343
346
347
348
349
350
353
354
355
356
357
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
392
393
394
395
396
397
398
399
400
401
417
418
419
420
421
422
423
424
425
430
431
432
433
434
435
436
437
438
439
440
441
442
447
448
449
450
451
452
453
454
455
456
461
466
467
468
487
488
491
492
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#if (USE_TIMEOUT == 1)
#define RNG_GENERATION_TIMEOUT 20
#endif
#define NB_OF_GENERATED_RANDOM_NUMBERS 8
Private define
#if (USE_TIMEOUT == 1)
uint32_t Timeout = 0;
#endif
__IO uint8_t ubButtonPress = 0;
__IO uint8_t ubIndex = 0;
__IO uint8_t ubIsNbGenerated = 0;
__IO uint32_t aRandom32bit[NB_OF_GENERATED_RANDOM_NUMBERS];
Private variables
void SystemClock_Config(void);
void Configure_RNG(void);
void RandomNumbersGenerationIT(void);
void LED_Init(void);
void LED_On(void);
void LED_Blinking(uint32_t Period);
void UserButton_Init(void);
void WaitForUserButtonPress(void);
uint32_t SetSystemToHSI(void);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
UserButton_Init();
Configure_RNG();
WaitForUserButtonPress();
RandomNumbersGenerationIT();
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
void Configure_RNG(void)
{
LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_RNG);
while (SetSystemToHSI()!= 0)
{
}while (SetSystemToHSI()!= 0) { ... };
/* ... */
LL_RCC_PLL_ConfigDomain_48M(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_8, 336, LL_RCC_PLLQ_DIV_7);
LL_FLASH_SetLatency(LL_FLASH_LATENCY_5);
LL_RCC_PLL_Enable();
while(LL_RCC_PLL_IsReady() != 1)
{
}while (LL_RCC_PLL_IsReady() != 1) { ... };
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { ... };
SysTick_Config(168000000 / 1000);
SystemCoreClock = 168000000;
NVIC_SetPriority(HASH_RNG_IRQn, 0);
NVIC_EnableIRQ(HASH_RNG_IRQn);
}{ ... }
/* ... */
void RandomNumbersGenerationIT(void)
{
LL_RNG_Enable(RNG);
for(ubIndex = 0; ubIndex < NB_OF_GENERATED_RANDOM_NUMBERS; ubIndex++)
{
#if (USE_TIMEOUT == 1)
Timeout = RNG_GENERATION_TIMEOUT;
#endif
ubIsNbGenerated = 0;
LL_RNG_EnableIT(RNG);
while (ubIsNbGenerated == 0)
{
#if (USE_TIMEOUT == 1)
if (LL_SYSTICK_IsActiveCounterFlag())
{
if(Timeout-- == 0)
{
LED_Blinking(LED_BLINK_SLOW);
}if (Timeout-- == 0) { ... }
}if (LL_SYSTICK_IsActiveCounterFlag()) { ... } /* ... */
#endif
}while (ubIsNbGenerated == 0) { ... }
}for (ubIndex = 0; ubIndex < NB_OF_GENERATED_RANDOM_NUMBERS; ubIndex++) { ... }
LL_RNG_Disable(RNG);
/* ... */
LED_On();
}{ ... }
/* ... */
void LED_Init(void)
{
LED1_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED1_GPIO_PORT, LED1_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
void LED_On(void)
{
LL_GPIO_SetOutputPin(LED1_GPIO_PORT, LED1_PIN);
}{ ... }
/* ... */
void LED_Blinking(uint32_t Period)
{
while (1)
{
LL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
void UserButton_Init(void)
{
USER_BUTTON_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_PULL_NO);
USER_BUTTON_SYSCFG_SET_EXTI();
USER_BUTTON_EXTI_LINE_ENABLE();
USER_BUTTON_EXTI_FALLING_TRIG_ENABLE();
NVIC_SetPriority(USER_BUTTON_EXTI_IRQn, 0x03);
NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn);
}{ ... }
/* ... */
void WaitForUserButtonPress(void)
{
while (ubButtonPress == 0)
{
LL_GPIO_TogglePin(LED1_GPIO_PORT, LED1_PIN);
LL_mDelay(LED_BLINK_FAST);
}while (ubButtonPress == 0) { ... }
LL_GPIO_ResetOutputPin(LED1_GPIO_PORT, LED1_PIN);
}{ ... }
/* ... */
void SystemClock_Config(void)
{
LL_RCC_HSE_EnableBypass();
LL_RCC_HSE_Enable();
while(LL_RCC_HSE_IsReady() != 1)
{
}while (LL_RCC_HSE_IsReady() != 1) { ... };
LL_FLASH_SetLatency(LL_FLASH_LATENCY_5);
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
LL_PWR_EnableOverDriveMode();
while(LL_PWR_IsActiveFlag_OD() != 1)
{
}while (LL_PWR_IsActiveFlag_OD() != 1) { ... };
LL_PWR_EnableOverDriveSwitching();
while(LL_PWR_IsActiveFlag_ODSW() != 1)
{
}while (LL_PWR_IsActiveFlag_ODSW() != 1) { ... };
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_8, 360, LL_RCC_PLLP_DIV_2);
LL_RCC_PLL_Enable();
while(LL_RCC_PLL_IsReady() != 1)
{
}while (LL_RCC_PLL_IsReady() != 1) { ... };
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { ... };
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
SysTick_Config(180000000 / 1000);
SystemCoreClock = 180000000;
}{ ... }
uint32_t SetSystemToHSI(void)
{
uint32_t timeout = 0;
if (LL_RCC_HSI_IsReady() != 1)
{
LL_RCC_HSI_Enable();
timeout = 1000;
while ((LL_RCC_HSI_IsReady() != 1) && (timeout != 0))
{
if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
{
timeout --;
}if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) { ... }
if (timeout == 0)
{
return 1;
}if (timeout == 0) { ... }
}while ((LL_RCC_HSI_IsReady() != 1) && (timeout != 0)) { ... }
}if (LL_RCC_HSI_IsReady() != 1) { ... }
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_HSI);
LL_FLASH_SetLatency(LL_FLASH_LATENCY_0);
SystemCoreClock = HSI_VALUE;
if (LL_RCC_PLL_IsReady() != 0)
{
LL_RCC_PLL_Disable();
timeout = 1000;
while ((LL_RCC_PLL_IsReady() != 0) && (timeout != 0))
{
if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk)
{
timeout --;
}if (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) { ... }
if (timeout == 0)
{
return 1;
}if (timeout == 0) { ... }
}while ((LL_RCC_PLL_IsReady() != 0) && (timeout != 0)) { ... }
}if (LL_RCC_PLL_IsReady() != 0) { ... }
return 0;
}{ ... }
/* ... */
void RNG_DataReady_Callback(void)
{
LL_RNG_DisableIT(RNG);
aRandom32bit[ubIndex] = LL_RNG_ReadRandData32(RNG);
ubIsNbGenerated = 1;
}{ ... }
/* ... */
void Error_Callback(void)
{
NVIC_DisableIRQ(HASH_RNG_IRQn);
LED_Blinking(LED_BLINK_ERROR);
}{ ... }
/* ... */
void UserButton_Callback(void)
{
ubButtonPress = 1;
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */#endif
/* ... */
/* ... */