cancel
Showing results for 
Search instead for 
Did you mean: 

How to use RNG on STM32H743ZI2?

MLang.7
Associate III

I would like to use the RNG of the H743ZI2. The provided example works, but on a new project I can't get it work, The CPU clock is configured to 400MHz just like in the example.

/* RNG Initialization */
 
if (HAL_RNG_DeInit(&hrng) != HAL_OK)
{
    /* DeInitialization Error */
    Error_Handler();
}
 
/* Initialize the RNG peripheral */
if (HAL_RNG_Init(&hrng) != HAL_OK)
{
     /* Initialization Error */
    Error_Handler();
}
uint32_t rnd_number;
 
void main()
{
    while (1)
    {
 
        status = HAL_RNG_GenerateRandomNumber(&hrng, &rnd_number);
        if (status != HAL_OK)
        {
            Error_Handler();
        }
    }
}
 

1 ACCEPTED SOLUTION

Accepted Solutions
MLang.7
Associate III
HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
{
  uint32_t tickstart;
  /* Check the RNG handle allocation */
  if (hrng == NULL)
  {
    return HAL_ERROR;
  }
  /* Check the parameters */
  assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
  assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
 
  /** My fix */
 
  hrng->Instance = RNG;

Since the passed handle showed different values than the real registers in the SFRs vie I digged down into the HAL driver. As I suspected, the assertion of the Instance wasn't working. I added a manual fix and as of now, it seems to work.

View solution in original post

7 REPLIES 7
Imen.D
ST Employee

Hello @MLang.7​ ,

Welcome to the STM32 Community 😊.

Can you please check if the RNG clock is enabled in the MSP file ?

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
MLang.7
Associate III

0693W000007DjFQQA0.pngHello Imen,

I'm currently debugging the problem. The RNGEN isn't set. Even writing directly to the register doesen't change any bits in it.

At the moment it "smells" like there is something wrong with the clock configuration. I attached a screenshot of the current clock configuration.

MLang.7
Associate III
HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
{
  uint32_t tickstart;
  /* Check the RNG handle allocation */
  if (hrng == NULL)
  {
    return HAL_ERROR;
  }
  /* Check the parameters */
  assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
  assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));
 
  /** My fix */
 
  hrng->Instance = RNG;

Since the passed handle showed different values than the real registers in the SFRs vie I digged down into the HAL driver. As I suspected, the assertion of the Instance wasn't working. I added a manual fix and as of now, it seems to work.

Imen.D
ST Employee

@MLang.7​ ,

I found different code that you may want to check out, as your code is not properly implemented and you shouldn't update the HAL.

I see that you passed an empty handle (hrng) to the HAL_RNG_Init (& hrng) function ! So, what will you initialize in this case ?

I advise you to follow the HAL working examples and you may re-use sections available in the example or get inspired from them:

hrng.Instance = RNG;
  //hrng.Init.ClockErrorDetection = RNG_CED_ENABLE;
  if (HAL_RNG_Init(&hrng) != HAL_OK)
  {
    Error_Handler();
  }

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

@Imen DAHMEN​ 

The "fix" I posted was in the stm32h7xx_hal_rng.c file, which is part of the HAL system. Therefore, please unmark the post.

I advise against any following readers to apply this fix, since it changes the driver!

Ok, I understand now the issue and your fix, which is in the stm32h7xx_hal_rng.c file.

I'm glad to know that your issue is resolved and I marked your answer as "Best" =)

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
MLang.7
Associate III

In the example, there is one line:

    /*## Configure the RNG peripheral #######################################*/
  RngHandle.Instance = RNG;
  
/* DeInitialize the RNG peripheral */
  if (HAL_RNG_DeInit(&RngHandle) != HAL_OK)
  {
    /* DeInitialization Error */
    Error_Handler();
  }    
 
...

which I missed copying and totally overread later. Thats the line where the handle is asserted. When missing, the driver doesen't notice the invalid handle.