2021-02-02 03:02 PM
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();
}
}
}
Solved! Go to Solution.
2021-02-03 01:42 AM
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.
2021-02-03 01:01 AM
Hello @MLang.7 ,
Welcome to the STM32 Community :smiling_face_with_smiling_eyes:.
Can you please check if the RNG clock is enabled in the MSP file ?
Imen
2021-02-03 01:13 AM
Hello 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.
2021-02-03 01:42 AM
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.
2021-02-03 01:47 AM
@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
2021-02-03 01:55 AM
@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!
2021-02-03 02:07 AM
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
2021-02-03 02:08 AM
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.