STM32L4xx_HAL_Driver  1.14.0
Peripheral Control functions

Management functions. More...

Functions

uint32_t HAL_RNG_GetRandomNumber (RNG_HandleTypeDef *hrng)
 Return generated random number in polling mode (Obsolete). More...
 
uint32_t HAL_RNG_GetRandomNumber_IT (RNG_HandleTypeDef *hrng)
 Return a 32-bit random number with interrupt enabled (Obsolete). More...
 
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber (RNG_HandleTypeDef *hrng, uint32_t *random32bit)
 Generate a 32-bit random number. More...
 
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT (RNG_HandleTypeDef *hrng)
 Generate a 32-bit random number in interrupt mode. More...
 
uint32_t HAL_RNG_ReadLastRandomNumber (RNG_HandleTypeDef *hrng)
 Read latest generated random number. More...
 
void HAL_RNG_IRQHandler (RNG_HandleTypeDef *hrng)
 Handle RNG interrupt request. More...
 
void HAL_RNG_ErrorCallback (RNG_HandleTypeDef *hrng)
 RNG error callback. More...
 
void HAL_RNG_ReadyDataCallback (RNG_HandleTypeDef *hrng, uint32_t random32bit)
 Data Ready callback in non-blocking mode. More...
 

Detailed Description

Management functions.

 ===============================================================================
                      ##### Peripheral Control functions #####
 ===============================================================================  
    [..]  This section provides functions allowing to:
      (+) Get the 32 bit Random number
      (+) Get the 32 bit Random number with interrupt enabled
      (+) Handle RNG interrupt request 

Function Documentation

◆ HAL_RNG_ErrorCallback()

__weak void HAL_RNG_ErrorCallback ( RNG_HandleTypeDef hrng)

RNG error callback.

Parameters
hrngpointer to a RNG_HandleTypeDef structure.
Return values
None

Definition at line 780 of file stm32l4xx_hal_rng.c.

781 {
782  /* Prevent unused argument(s) compilation warning */
783  UNUSED(hrng);
784 
785  /* NOTE : This function should not be modified. When the callback is needed,
786  function HAL_RNG_ErrorCallback must be implemented in the user file.
787  */
788 }

◆ HAL_RNG_GenerateRandomNumber()

HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber ( RNG_HandleTypeDef hrng,
uint32_t *  random32bit 
)

Generate a 32-bit random number.

Note
Each time the random number data is read the RNG_FLAG_DRDY flag is automatically cleared.
Parameters
hrngpointer to a RNG_HandleTypeDef structure.
random32bitpointer to generated random number variable if successful.
Return values
HALstatus

Definition at line 552 of file stm32l4xx_hal_rng.c.

553 {
554  uint32_t tickstart = 0;
555  HAL_StatusTypeDef status = HAL_OK;
556 
557  /* Process Locked */
558  __HAL_LOCK(hrng);
559 
560  /* Check RNS peripheral state */
561  if(hrng->State == HAL_RNG_STATE_READY)
562  {
563  /* Change RNG peripheral state */
564  hrng->State = HAL_RNG_STATE_BUSY;
565 
566  /* Get tick */
567  tickstart = HAL_GetTick();
568 
569  /* Check if data register contains valid random data */
570  while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
571  {
572  if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
573  {
574  hrng->State = HAL_RNG_STATE_ERROR;
575 
576  /* Process Unlocked */
577  __HAL_UNLOCK(hrng);
578 
579  return HAL_TIMEOUT;
580  }
581  }
582 
583  /* Get a 32bit Random number */
584  hrng->RandomNumber = hrng->Instance->DR;
585  *random32bit = hrng->RandomNumber;
586 
587  hrng->State = HAL_RNG_STATE_READY;
588  }
589  else
590  {
591  status = HAL_ERROR;
592  }
593 
594  /* Process Unlocked */
595  __HAL_UNLOCK(hrng);
596 
597  return status;
598 }
__IO HAL_RNG_StateTypeDef State
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK
RNG_TypeDef * Instance

◆ HAL_RNG_GenerateRandomNumber_IT()

HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT ( RNG_HandleTypeDef hrng)

Generate a 32-bit random number in interrupt mode.

Parameters
hrngpointer to a RNG_HandleTypeDef structure.
Return values
HALstatus

Definition at line 605 of file stm32l4xx_hal_rng.c.

606 {
607  HAL_StatusTypeDef status = HAL_OK;
608 
609  /* Process Locked */
610  __HAL_LOCK(hrng);
611 
612  /* Check RNG peripheral state */
613  if(hrng->State == HAL_RNG_STATE_READY)
614  {
615  /* Change RNG peripheral state */
616  hrng->State = HAL_RNG_STATE_BUSY;
617 
618  /* Process Unlocked */
619  __HAL_UNLOCK(hrng);
620 
621  /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
622  __HAL_RNG_ENABLE_IT(hrng);
623  }
624  else
625  {
626  /* Process Unlocked */
627  __HAL_UNLOCK(hrng);
628 
629  status = HAL_ERROR;
630  }
631 
632  return status;
633 }
__IO HAL_RNG_StateTypeDef State
__HAL_UNLOCK(hrtc)
__HAL_LOCK(hrtc)
return HAL_OK

◆ HAL_RNG_GetRandomNumber()

uint32_t HAL_RNG_GetRandomNumber ( RNG_HandleTypeDef hrng)

Return generated random number in polling mode (Obsolete).

Note
Use HAL_RNG_GenerateRandomNumber() API instead.
Parameters
hrngpointer to a RNG_HandleTypeDef structure that contains the configuration information for RNG.
Return values
randomvalue

Definition at line 707 of file stm32l4xx_hal_rng.c.

708 {
709  if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
710  {
711  return hrng->RandomNumber;
712  }
713  else
714  {
715  return 0;
716  }
717 }
return HAL_OK
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
Generate a 32-bit random number.

◆ HAL_RNG_GetRandomNumber_IT()

uint32_t HAL_RNG_GetRandomNumber_IT ( RNG_HandleTypeDef hrng)

Return a 32-bit random number with interrupt enabled (Obsolete).

Note
Use HAL_RNG_GenerateRandomNumber_IT() API instead.
Parameters
hrngRNG handle
Return values
32-bitrandom number

Definition at line 726 of file stm32l4xx_hal_rng.c.

727 {
728  uint32_t random32bit = 0;
729 
730  /* Process locked */
731  __HAL_LOCK(hrng);
732 
733  /* Change RNG peripheral state */
734  hrng->State = HAL_RNG_STATE_BUSY;
735 
736  /* Get a 32bit Random number */
737  random32bit = hrng->Instance->DR;
738 
739  /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
740  __HAL_RNG_ENABLE_IT(hrng);
741 
742  /* Return the 32 bit random number */
743  return random32bit;
744 }
__IO HAL_RNG_StateTypeDef State
__HAL_LOCK(hrtc)
RNG_TypeDef * Instance

◆ HAL_RNG_IRQHandler()

void HAL_RNG_IRQHandler ( RNG_HandleTypeDef hrng)

Handle RNG interrupt request.

Note
In the case of a clock error, the RNG is no more able to generate random numbers because the PLL48CLK clock is not correct. User has to check that the clock controller is correctly configured to provide the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT(). The clock error has no impact on the previously generated random numbers, and the RNG_DR register contents can be used.
In the case of a seed error, the generation of random numbers is interrupted as long as the SECS bit is '1'. If a number is available in the RNG_DR register, it must not be used because it may not have enough entropy. In this case, it is recommended to clear the SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable the RNG peripheral to reinitialize and restart the RNG.
RNG ErrorCallback() API is called once whether SEIS or CEIS are set.
Parameters
hrngpointer to a RNG_HandleTypeDef structure.
Return values
None

Definition at line 654 of file stm32l4xx_hal_rng.c.

655 {
656  /* RNG clock error interrupt occurred */
657  if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
658  {
659  /* Change RNG peripheral state */
660  hrng->State = HAL_RNG_STATE_ERROR;
661 
662 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
663  /* Call registered Error callback */
664  hrng->ErrorCallback(hrng);
665 #else
666  /* Call legacy weak Error callback */
667  HAL_RNG_ErrorCallback(hrng);
668 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
669 
670  /* Clear the clock error flag */
671  __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
672 
673  }
674 
675  /* Check RNG data ready interrupt occurred */
676  if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
677  {
678  /* Generate random number once, so disable the IT */
679  __HAL_RNG_DISABLE_IT(hrng);
680 
681  /* Get the 32bit Random number (DRDY flag automatically cleared) */
682  hrng->RandomNumber = hrng->Instance->DR;
683 
684  if(hrng->State != HAL_RNG_STATE_ERROR)
685  {
686  /* Change RNG peripheral state */
687  hrng->State = HAL_RNG_STATE_READY;
688 
689 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
690  /* Call registered Data Ready callback */
691  hrng->ReadyDataCallback(hrng, hrng->RandomNumber);
692 #else
693  /* Call legacy weak Data Ready callback */
695 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */
696  }
697  }
698 }
void(* ErrorCallback)(struct __RNG_HandleTypeDef *hrng)
void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
Data Ready callback in non-blocking mode.
void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
RNG error callback.
__IO HAL_RNG_StateTypeDef State
RNG_TypeDef * Instance
void(* ReadyDataCallback)(struct __RNG_HandleTypeDef *hrng, uint32_t random32bit)

◆ HAL_RNG_ReadLastRandomNumber()

uint32_t HAL_RNG_ReadLastRandomNumber ( RNG_HandleTypeDef hrng)

Read latest generated random number.

Parameters
hrngpointer to a RNG_HandleTypeDef structure.
Return values
randomvalue

Definition at line 753 of file stm32l4xx_hal_rng.c.

754 {
755  return(hrng->RandomNumber);
756 }

◆ HAL_RNG_ReadyDataCallback()

__weak void HAL_RNG_ReadyDataCallback ( RNG_HandleTypeDef hrng,
uint32_t  random32bit 
)

Data Ready callback in non-blocking mode.

Parameters
hrngpointer to a RNG_HandleTypeDef structure.
random32bitgenerated random value
Return values
None

Definition at line 764 of file stm32l4xx_hal_rng.c.

765 {
766  /* Prevent unused argument(s) compilation warning */
767  UNUSED(hrng);
768  UNUSED(random32bit);
769 
770  /* NOTE : This function should not be modified. When the callback is needed,
771  function HAL_RNG_ReadyDataCallback must be implemented in the user file.
772  */
773 }