Beispiel #1
0
void stm32_rng_init(void)
{
    RNG_HandleTypeDef rng_handle = { 0 };

    __HAL_RCC_RNG_CLK_ENABLE();

    rng_handle.Instance = RNG;
    HAL_StatusTypeDef status = HAL_RNG_Init(&rng_handle);
    if (status != HAL_OK) {
        panic("error initializing random number hardware\n");
    }

    /* seed the pseudo random number generator with this */
#if STM32_SEED_RAND_FROM_HWRNG
    uint32_t r;

    /* discard he first result */
    status = HAL_RNG_GenerateRandomNumber(&rng_handle, &r);
    if (status != HAL_OK) {
        panic("error getting random number from hardware\n");
    }

    status = HAL_RNG_GenerateRandomNumber(&rng_handle, &r);
    if (status != HAL_OK) {
        panic("error getting random number from hardware\n");
    }

    srand(r);
#endif
}
Beispiel #2
0
/**
  * @brief  Returns generated random number in polling mode (Obsolete)
  *         Use HAL_RNG_GenerateRandomNumber() API instead.
  * @param  hrng: pointer to a RNG_HandleTypeDef structure that contains
  *                the configuration information for RNG.
  * @retval Random value
  */
uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
{
    if (HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK) {
        return hrng->RandomNumber;
    } else {
        return 0;
    }
}
Beispiel #3
0
float whiteNoise (void)
{
	HAL_RNG_GenerateRandomNumber(&hrng, &myRandomNumber);
	float white = (((float)(myRandomNumber >> 16))- 32768.f) / 32768.f;
	return white;
}
Beispiel #4
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  uint32_t counter = 0;
  
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization: global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /*## Configure peripherals #################################################*/
  
  /* Initialize LEDs on board */
  BSP_LED_Init(LED3);
  BSP_LED_Init(LED1);

  /* Configure Tamper push-button in Interrupt mode */
  BSP_PB_Init(BUTTON_TAMPER, BUTTON_MODE_EXTI);
  
    /*## Configure the RNG peripheral #######################################*/
  RngHandle.Instance = RNG;
  
  /* DeInitialize the RNG peripheral */
  if (HAL_RNG_DeInit(&RngHandle) != HAL_OK)
  {
    /* DeInitialization Error */
    Error_Handler();
  }    

  /* Initialize the RNG peripheral */
  if (HAL_RNG_Init(&RngHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while (1)
  {

    /* Wait for event on push button to perform following actions */
    while ((ubUserButtonClickEvent) == RESET)
    {
      __NOP();
    }
    /* Reset variable for next loop iteration */
    ubUserButtonClickEvent = RESET;
    
 
     /*## Generate eight 32-bit long random numbers ##################################*/
    for (counter = 0; counter < 8; counter++)
    {
      if (HAL_RNG_GenerateRandomNumber(&RngHandle, &aRandom32bit[counter]) != HAL_OK)
      {
        /* Random number generation error */
        Error_Handler();      
      }
    }
   
  }
}