Skip to main content
raiyans
Associate II
November 10, 2016
Solved

failing to generate a random number using stm32f4xx_rng

  • November 10, 2016
  • 6 replies
  • 4593 views
Posted on November 10, 2016 at 17:34

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
This topic has been closed for replies.
Best answer by raiyans
Posted on November 14, 2016 at 12:01

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 initiation

RNG_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,

6 replies

Tesla DeLorean
Guru
November 10, 2016
Posted on November 10, 2016 at 20:18

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.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
November 10, 2016
Posted on November 10, 2016 at 20:21

Don't exit main()

Provide output from

while(1)
{
 DEBUG_PRINT(''%'' PRIu32 ''
'',code_[0]);
 HAL_RNG_GenerateRandomNumber(&hRNG1,code_);



}

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
raiyans
raiyansAuthor
Associate II
November 11, 2016
Posted on November 11, 2016 at 10:56

0690X00000602dFQAQ.jpg

There you go ;)

I have initiated the rng clock as suggested in the documentations.

Tesla DeLorean
Guru
November 11, 2016
Posted on November 11, 2016 at 17:20

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.
Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
November 11, 2016
Posted on November 11, 2016 at 21:07

Interestingly the STM32F446ZE doesn't have an RNG

On the STM32F429ZI I had more joy

void 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

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
raiyans
raiyansAuthorBest answer
Associate II
November 14, 2016
Posted on November 14, 2016 at 12:01

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 initiation

RNG_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,