cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F207 RNG

Anvi
Associate III

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

11 REPLIES 11

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

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
Guru

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
Associate III

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

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

Output PLL to MCO pin and measure.

JW

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".

Don't you need the F217 for the RNG?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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);
    }
}

The F207 datasheet says that it has a TRNG: Datasheet

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