failing to generate a random number using stm32f4xx_rng
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-10 8: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.
- Labels:
-
STM32Cube MCU Packages
-
STM32F4 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-14 3: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,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-10 11:21 AM
Don't exit main()
Provide output fromwhile(1)
{
DEBUG_PRINT(''%'' PRIu32 ''
'',code_[0]);
HAL_RNG_GenerateRandomNumber(&hRNG1,code_);
}
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-11 1:56 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-11 8: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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2016-11-14 3: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,
