Skip to main content
Anvi
Associate III
Jul 24, 2021 10:41 PM UTC
Question

STM32F207 RNG

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.

11 replies

waclawek.jan
Super User
Jul 24, 2021 10:59 PM UTC

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
Jul 24, 2021 11:27 PM UTC

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
Jul 24, 2021 11:48 PM UTC

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
Jul 24, 2021 11:53 PM UTC

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
Jul 24, 2021 11:58 PM UTC

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
Jul 24, 2021 11:55 PM UTC

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

Output PLL to MCO pin and measure.

JW

Anvi
AnviAuthor
Associate III
Jul 25, 2021 1:59 AM UTC

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
Jul 25, 2021 1:03 AM UTC

Don't you need the F217 for the RNG?

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Anvi
AnviAuthor
Associate III
Jul 25, 2021 2:18 AM UTC

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
Jul 25, 2021 8:05 AM UTC

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
Jul 25, 2021 2:13 PM UTC

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.