Skip to main content
Anvi
Associate III
July 24, 2021
Question

STM32F207 RNG

  • July 24, 2021
  • 6 replies
  • 4106 views

Hello,

I'm having trouble using the RNG peripheral on the STM32F207.

I'm using the HAL functions to set things up and I keep getting back the same number. I've made sure to enable the RNG Clock. See the steps below:

  • Enabled RNG CLK:
__RNG_CLK_ENABLE()
  • Enabled the peripheral:
 __HAL_RNG_ENABLE()
  • Get the random number when available (polling on the ready flag):
HAL_RNG_GetRandomNumber()

The polling doesn't time out, it just keeps returning the same number. I've tried to enable the interrupt as well and the interrupt never arrives.

What am I doing wrong?

Thank you

This topic has been closed for replies.

6 replies

waclawek.jan
Super User
July 24, 2021

Make sure you have enabled and working the main PLL, to output 48MHz clock (PLL48CK) from the PLL's Q tap.

It's frequency does not need to be 48MHz, it needs to be lower or equal 48MHz.

JW

Anvi
AnviAuthor
Associate III
July 24, 2021

Hi, thank your response. My PLL is currently setup this way:

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 25;
 RCC_OscInitStruct.PLL.PLLN = 240;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 5;

How do I verify that it's operating @ 48 MHz? Also, the AHB2 bus is @ 120 MHz according to the datasheet: Datasheet. Fairly new to this, not entirely sure how to properly setup.

TDK
Super User
July 24, 2021

If you're using HAL, you need to use it's handle structure and call HAL_RNG_Init rather than just enabling the clock. Otherwise, HAL thinks the peripheral is reset and will return HAL_ERROR.

Enable clock with __HAL_RCC_RNG_CLK_ENABLE().

Call HAL_RNG_Init(..).

Call HAL_RNG_GenerateRandomNumber(...).

Or skip that and use the register-level access, which for RNG is simple.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Anvi
AnviAuthor
Associate III
July 24, 2021

Yeah, I should have clarified, I Did try calling RNG_Init() as well after enabling the clock. Same issue with returning the same number

TDK
Super User
July 24, 2021

Should work. Post the actual code you're using rather than random snippets.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
July 24, 2021

Read our and check/post the relevant RCC registers content.

Output PLL to MCO pin and measure.

JW

Anvi
AnviAuthor
Associate III
July 25, 2021

So this is the output of the RCC_PLLCFGR register:

0x25403C19

So this tells me that PLLQ is configured to divide by 5: Reference manual.

So:

freq(RNG_CLK) = f(VCO_CLK)/5
 
PLL_VCO = (HSE_VALUE / PLLM) * PLLN = (25000000 / 25) * 240 = 240 Mhz
 
240 Mhz / 5 = 48 Mhz

So the PLLQ is good...

I also just noticed that the number is the same across resets as well: 0x080001A5.

This is the code I'm using:

void generate_random_num(uint8_t *rnd_num)
{
 uint32_t new_rnd_num = 0;
 
 /* Enable RNG peripheral */
 HAL_RNG_Init(); /* Clock is enabled here as well, prior to enabling the peripheral */
 
...
 
 while ( 1 )
 {
...
 new_rnd_num = HAL_RNG_GetRandomNumber();
 
 sprintf(buff, "%08X\r\n", new_rnd_num);
 printDebug(buff);
 }
}

Tesla DeLorean
Guru
July 25, 2021

Don't you need the F217 for the RNG?

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Anvi
AnviAuthor
Associate III
July 25, 2021

The F207 datasheet says that it has a TRNG: Datasheet

The HAL lib i'm using is also specifically for the F207.

waclawek.jan
Super User
July 25, 2021

Rewrite your code for direct register access, at least the directly RNG related part - that's fairly easy, just enable RNG, and then check status bit and if set, read out data.

You should get a debugger, makes life easier (although sometimes can get into way, too).

JW

Anvi
AnviAuthor
Associate III
July 25, 2021

Figured out the problem... I was never setting the Instance of the RNG handler to point to the base of the RNG peripheral so it was never getting enabled. :|

Thanks for all your help.