STM32G474xx HAL用户手册
stm32g4xx_hal_rng.c
查看此文件的文档。
00001 /**
  ******************************************************************************
  * @file    stm32g4xx_hal_rng.c
  * @author  MCD Application Team
  * @brief   RNG HAL module driver.
  *          This file provides firmware functions to manage the following
  *          functionalities of the Random Number Generator (RNG) peripheral:
  *           + Initialization and configuration functions
  *           + Peripheral Control functions
  *           + Peripheral State functions
  *
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  @verbatim
  ==============================================================================
                     ##### How to use this driver #####
  ==============================================================================
  [..]
      The RNG HAL driver can be used as follows:

      (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
          in HAL_RNG_MspInit().
      (#) Activate the RNG peripheral using HAL_RNG_Init() function.
      (#) Wait until the 32 bit Random Number Generator contains a valid
          random data using (polling/interrupt) mode.
      (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.

    ##### Callback registration #####
    ==================================

    [..]
    The compilation define USE_HAL_RNG_REGISTER_CALLBACKS when set to 1
    allows the user to configure dynamically the driver callbacks.

    [..]
    Use Function HAL_RNG_RegisterCallback() to register a user callback.
    Function HAL_RNG_RegisterCallback() allows to register following callbacks:
    (+) ErrorCallback             : RNG Error Callback.
    (+) MspInitCallback           : RNG MspInit.
    (+) MspDeInitCallback         : RNG MspDeInit.
    This function takes as parameters the HAL peripheral handle, the Callback ID
    and a pointer to the user callback function.

    [..]
    Use function HAL_RNG_UnRegisterCallback() to reset a callback to the default
    weak (overridden) function.
    HAL_RNG_UnRegisterCallback() takes as parameters the HAL peripheral handle,
    and the Callback ID.
    This function allows to reset following callbacks:
    (+) ErrorCallback             : RNG Error Callback.
    (+) MspInitCallback           : RNG MspInit.
    (+) MspDeInitCallback         : RNG MspDeInit.

    [..]
    For specific callback ReadyDataCallback, use dedicated register callbacks:
    respectively HAL_RNG_RegisterReadyDataCallback() , HAL_RNG_UnRegisterReadyDataCallback().

    [..]
    By default, after the HAL_RNG_Init() and when the state is HAL_RNG_STATE_RESET
    all callbacks are set to the corresponding weak (overridden) functions:
    example HAL_RNG_ErrorCallback().
    Exception done for MspInit and MspDeInit functions that are respectively
    reset to the legacy weak (overridden) functions in the HAL_RNG_Init()
    and HAL_RNG_DeInit() only when these callbacks are null (not registered beforehand).
    If not, MspInit or MspDeInit are not null, the HAL_RNG_Init() and HAL_RNG_DeInit()
    keep and use the user MspInit/MspDeInit callbacks (registered beforehand).

    [..]
    Callbacks can be registered/unregistered in HAL_RNG_STATE_READY state only.
    Exception done MspInit/MspDeInit that can be registered/unregistered
    in HAL_RNG_STATE_READY or HAL_RNG_STATE_RESET state, thus registered (user)
    MspInit/DeInit callbacks can be used during the Init/DeInit.
    In that case first register the MspInit/MspDeInit user callbacks
    using HAL_RNG_RegisterCallback() before calling HAL_RNG_DeInit()
    or HAL_RNG_Init() function.

    [..]
    When The compilation define USE_HAL_RNG_REGISTER_CALLBACKS is set to 0 or
    not defined, the callback registration feature is not available
    and weak (overridden) callbacks are used.

  @endverbatim
  ******************************************************************************
  */

00095 /* Includes ------------------------------------------------------------------*/
00096 #include "stm32g4xx_hal.h"
00097 
00098 /** @addtogroup STM32G4xx_HAL_Driver
00099   * @{
00100   */
00101 #if defined (RNG)
00102 
00103 /** @addtogroup RNG
00104   * @brief RNG HAL module driver.
00105   * @{
00106   */
00107 
00108 #ifdef HAL_RNG_MODULE_ENABLED
00109 /* Private types -------------------------------------------------------------*/
00111 /* Private defines -----------------------------------------------------------*/
00112 /* Private variables ---------------------------------------------------------*/
00113 /* Private constants ---------------------------------------------------------*/
00114 /** @defgroup RNG_Private_Constants RNG Private Constants
00115   * @{
00116   */
00117 */
00118 #define RNG_TIMEOUT_VALUE     2U
00119 /**
  * @}
  */
  */
00121 /* Private macros ------------------------------------------------------------*/
00122 /* Private functions prototypes ----------------------------------------------*/
00123 /* Private functions ---------------------------------------------------------*/
00124 /* Exported functions --------------------------------------------------------*/
00125 
00126 /** @addtogroup RNG_Exported_Functions
00127   * @{
00128   */
00129 
00130 /** @addtogroup RNG_Exported_Functions_Group1
00131   *  @brief   Initialization and configuration functions
00132   *
00133 @verbatim
00134  ===============================================================================
00135           ##### Initialization and configuration functions #####
00136  ===============================================================================
00137     [..]  This section provides functions allowing to:
00138       (+) Initialize the RNG according to the specified parameters
00139           in the RNG_InitTypeDef and create the associated handle
00140       (+) DeInitialize the RNG peripheral
00141       (+) Initialize the RNG MSP
00142       (+) DeInitialize RNG MSP
00143 
00144 @endverbatim
00145   * @{
00146   */
00147 
00148 /**
00149   * @brief  Initializes the RNG peripheral and creates the associated handle.
00150   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
00151   *                the configuration information for RNG.
00152   * @retval HAL status
00153   */
00154 */
00155 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
00156 {
00157   /* Check the RNG handle allocation */
00158   if (hrng == NULL)
00159   {
00160     return HAL_ERROR;
00161   }
00162   /* Check the parameters */
00163   assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
00164   assert_param(IS_RNG_CED(hrng->Init.ClockErrorDetection));

00166 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00167   if (hrng->State == HAL_RNG_STATE_RESET)
00168   {
00169     /* Allocate lock resource and initialize it */
00170     hrng->Lock = HAL_UNLOCKED;
00171 
00172     hrng->ReadyDataCallback  = HAL_RNG_ReadyDataCallback;  /* Legacy weak ReadyDataCallback  */
00173     hrng->ErrorCallback      = HAL_RNG_ErrorCallback;      /* Legacy weak ErrorCallback      */
00174 
00175     if (hrng->MspInitCallback == NULL)
00176     {
00177       hrng->MspInitCallback = HAL_RNG_MspInit; /* Legacy weak MspInit  */
00178     }
00179 
00180     /* Init the low level hardware */
00181     hrng->MspInitCallback(hrng);
00182   }
00183 #else
00184   if (hrng->State == HAL_RNG_STATE_RESET)
00185   {
00186     /* Allocate lock resource and initialize it */
00187     hrng->Lock = HAL_UNLOCKED;
00188 
00189     /* Init the low level hardware */
00190     HAL_RNG_MspInit(hrng);
00191   }
00192 #endif /* USE_HAL_RNG_REGISTER_CALLBACKS */

00194   /* Change RNG peripheral state */
00195   hrng->State = HAL_RNG_STATE_BUSY;
00196 
00197   /* Clock Error Detection Configuration */
00198   MODIFY_REG(hrng->Instance->CR, RNG_CR_CED, hrng->Init.ClockErrorDetection);
00199 
00200   /* Enable the RNG Peripheral */
00201   __HAL_RNG_ENABLE(hrng);
00202 
00203   /* Initialize the RNG state */
00204   hrng->State = HAL_RNG_STATE_READY;
00205 
00206   /* Initialise the error code */
00207   hrng->ErrorCode = HAL_RNG_ERROR_NONE;
00208 
00209   /* Return function status */
00210   return HAL_OK;
00211 }
00212 
00213 /**
  * @brief  DeInitializes the RNG peripheral.
  * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
  *                the configuration information for RNG.
  * @retval HAL status
  */
00214 */
00215   * @brief  去初始化RNG外设。
00216   * @param  hrng 指向包含RNG配置信息的RNG_HandleTypeDef结构的指针。
00217   * @retval HAL状态
00218   */
00219 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
00220 {
00221   /* Check the RNG handle allocation */
00222   if (hrng == NULL)
00223   {
00224     return HAL_ERROR;
00225   }
00226 
00227   /* Clear Clock Error Detection bit */
00228   CLEAR_BIT(hrng->Instance->CR, RNG_CR_CED);
00229   /* Disable the RNG Peripheral */
00230   CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
00231 
00232   /* Clear RNG interrupt status flags */
00233   CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
00234 
00235 #if (USE_HAL_RNG_REGISTER_CALLBACKS == 1)
00236   if (hrng->MspDeInitCallback == NULL)
00237   {
00238     hrng->MspDeInitCallback = HAL_RNG_MspDeInit; /* Legacy weak MspDeInit  */
00239   }
00240 
00241   /* DeInit the low level hardware */
00242   hrng->MspDeInitCallback(hrng);
00243 #else
00244