2016-11-10 08:34 AM
Hi all,
I am using an stm32f4 micro to generate a random number. I am using the RNG Driver from the stm32cubef4 library. I followed the examples but the micro is generating the same number over and over again. here is a code snippet.
int
main(
void
)
{
HAL_Init();
system_init();
if
(HAL_RNG_Init(&hRNG1) != HAL_OK)
{
DEBUG_PRINT(
''Random Number generation failed\r''
);
}
if
(HAL_RNG_GenerateRandomNumber(&hRNG1,code_)==HAL_OK)
{
DEBUG_PRINT(
''RNG initiated\r''
);
}
else
{
DEBUG_PRINT(
''RNG failed\r''
);
}
DEBUG_PRINT(
''%''
PRIu32
''\r''
,code_[0]);
}
Does anybody have a clue on what am missing. I am using the DEBUG_PRINT function to get the random value through the serial port.
Best Regards
Raiyan
#no-hablo-hal #stm32f407-random-number-generate
Solved! Go to Solution.
2016-11-14 03:01 AM
Hi clive1,
I finally managed to solve the issue. What I was missing from the code is the instance initiation and a correct PLL clock initiationRNG_HandleTypeDef hRNG1;
int
main()
{
hRNG1.Instance = RNG;
}
and also I might have not configured my PLL correctly. Now I configured it as follow
/*Enable HSE Oscillator and activate PLL with HSE as source*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers*/
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
Thanks a lot for your help
Regards from Austria,
2016-11-10 11:18 AM
What does it actually output?
The RNG must have its AHB clock enabled, and the PLL Q must be clocking and outputting 48MHz. Things won't work if the PLL isn't running.Review the Reference Manual, examine the registers and settings directly.2016-11-10 11:21 AM
Don't exit main()
Provide output fromwhile(1)
{
DEBUG_PRINT(''%'' PRIu32 ''
'',code_[0]);
HAL_RNG_GenerateRandomNumber(&hRNG1,code_);
}
2016-11-11 01:56 AM
There you go ;) I have initiated the rng clock as suggested in the documentations.
2016-11-11 08:20 AM
Does this work?
code_[0] = 12345;
DEBUG_PRINT(''%'' PRIu32 ''
'',code_[0]);
I might examine the RNG hardware on the F4 under the SPL if I get time. You should review the Reference Manual, and output/advance the registers manually.
2016-11-11 12:07 PM
Interestingly the STM32F446ZE doesn't have an RNG
On the STM32F429ZI I had more joyvoid RNG_Configuration(void)
{
/* Enable RNG clock source */
RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
/* RNG Peripheral enable */
RNG_Cmd(ENABLE);
}
...
RNG_Configuration();
printf(''%10lu
'', RNG_GetRandomNumber());
printf(''%10lu
'', RNG_GetRandomNumber());
printf(''%10lu
'', RNG_GetRandomNumber());
printf(''%10lu
'', RNG_GetRandomNumber());
...
1820376166
544183808
417758785
2790747703
2016-11-14 03:01 AM
Hi clive1,
I finally managed to solve the issue. What I was missing from the code is the instance initiation and a correct PLL clock initiationRNG_HandleTypeDef hRNG1;
int
main()
{
hRNG1.Instance = RNG;
}
and also I might have not configured my PLL correctly. Now I configured it as follow
/*Enable HSE Oscillator and activate PLL with HSE as source*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers*/
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
Thanks a lot for your help
Regards from Austria,