cancel
Showing results for 
Search instead for 
Did you mean: 

B-LO72Z-LRWAN1 HAL_RNG

GErma.1
Associate III

Hi

I am trying to generate a true random number on the B-LO72Z-LRWAN1 using the HAL_RNG_GenerateRandomNumber function with the code below:

uint32_t rnd_generator (void) {

RNG_HandleTypeDef RngHandle;  // RNG handler declaration 

uint32_t rv;

  RngHandle.Instance = RNG;

  if (HAL_RNG_Init(&RngHandle) != HAL_OK) {

    printf ("\n Error RNG_Init \n");

  }

  if (HAL_RNG_GenerateRandomNumber(&RngHandle, &rv) != HAL_OK) {

    printf ("\n Error RNG_Generate \n");

  }

  HAL_RNG_DeInit(&RngHandle);

  return rv;

}

The HAL_RNG_GenerateRandomNumber always returns an error and the generated number is always the same.

I believe that I must initialize the clock for the RND module using my own version of the HAL_RNG_MspInit and HAL_RNG_MspDeInit functions.

Can you provide me some help on this?

BR

Gilberto

1 REPLY 1

STM32Cube_FW_L0_V1.11.0\Projects\NUCLEO-L073RZ\Examples\RNG\RNG_MultiRNG\Src\stm32l0xx_hal_msp.c

/**
  * @brief RNG MSP Initialization
  *        This function configures the hardware resources used in this example:
  *           - Peripheral's clock enable
  * @param hrng: RNG handle pointer
  * @retval None
  */
void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
{
 
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct = {0};
 
  /* Enable HSI48 Oscillator for USB/RNG */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
 
  /* configure RNG clock for USB or RNG analog part */
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_HSI48;
  if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct)!= HAL_OK)
  {
    /* Initialization Error */
    while(1);
  }
 
  /* RNG Peripheral clock enable */
  __HAL_RCC_RNG_CLK_ENABLE();
 
}
 
/**
  * @brief RNG MSP De-Initialization
  *        This function freeze the hardware resources used in this example:
  *          - Disable the Peripheral's clock
  * @param hrng: RNG handle pointer
  * @retval None
  */
void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
{
  /* Enable RNG reset state */
  __HAL_RCC_RNG_FORCE_RESET();
 
  /* Release RNG from reset state */
  __HAL_RCC_RNG_RELEASE_RESET();
}

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