2021-07-24 03:41 PM
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:
__RNG_CLK_ENABLE()
__HAL_RNG_ENABLE()
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
2021-07-24 03:59 PM
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
2021-07-24 04:27 PM
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.
2021-07-24 04:48 PM
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.
2021-07-24 04:53 PM
Yeah, I should have clarified, I Did try calling RNG_Init() as well after enabling the clock. Same issue with returning the same number
2021-07-24 04:55 PM
Read our and check/post the relevant RCC registers content.
Output PLL to MCO pin and measure.
JW
2021-07-24 04:58 PM
Should work. Post the actual code you're using rather than random snippets.
2021-07-24 06:03 PM
Don't you need the F217 for the RNG?
2021-07-24 06:59 PM
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);
}
}
2021-07-24 07:18 PM
The F207 datasheet says that it has a TRNG: Datasheet
The HAL lib i'm using is also specifically for the F207.