Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_rng_ex.c
0004   * @author  MCD Application Team
0005   * @brief   Extended RNG HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Random Number Generator (RNG) peripheral:
0008   *           + Lock configuration functions
0009   *           + Reset the RNG
0010   *
0011   ******************************************************************************
0012   * @attention
0013   *
0014   * Copyright (c) 2017 STMicroelectronics.
0015   * All rights reserved.
0016   *
0017   * This software is licensed under terms that can be found in the LICENSE file
0018   * in the root directory of this software component.
0019   * If no LICENSE file comes with this software, it is provided AS-IS.
0020   *
0021   ******************************************************************************
0022   */
0023 
0024 /* Includes ------------------------------------------------------------------*/
0025 #include "stm32h7xx_hal.h"
0026 
0027 /** @addtogroup STM32H7xx_HAL_Driver
0028   * @{
0029   */
0030 
0031 #if defined(RNG)
0032 
0033 /** @addtogroup RNG_Ex
0034   * @brief RNG Extended HAL module driver.
0035   * @{
0036   */
0037 
0038 #ifdef HAL_RNG_MODULE_ENABLED
0039 #if defined(RNG_CR_CONDRST)
0040 /* Private types -------------------------------------------------------------*/
0041 /* Private defines -----------------------------------------------------------*/
0042 /** @defgroup RNG_Ex_Private_Defines RNGEx Private Defines
0043   * @ingroup RTEMSBSPsARMSTM32H7
0044   * @{
0045   */
0046 /*  Health test control register information to use in CCM algorithm */
0047 #define RNG_HTCFG_1   0x17590ABCU /*!< Magic number */
0048 #if defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
0049 #define RNG_HTCFG     0x000CAA74U /*!< For best latency and to be compliant with NIST */
0050 #else /* RNG_VER_3_2 */
0051 #define RNG_HTCFG     0x00007274U /*!< For best latency and to be compliant with NIST */
0052 #endif /* RNG_VER_3_1 || RNG_VER_3_0 */
0053 /**
0054   * @}
0055   */
0056 /* Private variables ---------------------------------------------------------*/
0057 /* Private constants ---------------------------------------------------------*/
0058 /** @addtogroup RNG_Ex_Private_Constants
0059   * @{
0060   */
0061 #define RNG_TIMEOUT_VALUE     2U
0062 /**
0063   * @}
0064   */
0065 /* Private macros ------------------------------------------------------------*/
0066 /* Private functions prototypes ----------------------------------------------*/
0067 /* Private functions  --------------------------------------------------------*/
0068 /* Exported functions --------------------------------------------------------*/
0069 
0070 /** @defgroup RNG_Ex_Exported_Functions RNG_Ex Exported Functions
0071   * @{
0072   */
0073 
0074 /** @defgroup RNG_Ex_Exported_Functions_Group1 Configuration and lock functions
0075   *  @brief   Configuration functions
0076   *
0077 @verbatim
0078  ===============================================================================
0079           ##### Configuration and lock functions #####
0080  ===============================================================================
0081     [..]  This section provides functions allowing to:
0082       (+) Configure the RNG with the specified parameters in the RNG_ConfigTypeDef
0083       (+) Lock RNG configuration Allows user to lock a configuration until next reset.
0084 
0085 @endverbatim
0086   * @{
0087   */
0088 
0089 /**
0090   * @brief  Configure the RNG with the specified parameters in the
0091   *         RNG_ConfigTypeDef.
0092   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
0093   *          the configuration information for RNG.
0094   * @param  pConf pointer to a RNG_ConfigTypeDef structure that contains
0095   *         the configuration information for RNG module
0096 
0097   * @retval HAL status
0098   */
0099 HAL_StatusTypeDef HAL_RNGEx_SetConfig(RNG_HandleTypeDef *hrng, const RNG_ConfigTypeDef *pConf)
0100 {
0101   uint32_t tickstart;
0102   uint32_t cr_value;
0103   HAL_StatusTypeDef status ;
0104 
0105   /* Check the RNG handle allocation */
0106   if ((hrng == NULL) || (pConf == NULL))
0107   {
0108     return HAL_ERROR;
0109   }
0110 
0111   /* Check the parameters */
0112   assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance));
0113   assert_param(IS_RNG_CLOCK_DIVIDER(pConf->ClockDivider));
0114   assert_param(IS_RNG_NIST_COMPLIANCE(pConf->NistCompliance));
0115   assert_param(IS_RNG_CONFIG1(pConf->Config1));
0116   assert_param(IS_RNG_CONFIG2(pConf->Config2));
0117   assert_param(IS_RNG_CONFIG3(pConf->Config3));
0118 
0119   /* Check RNG peripheral state */
0120   if (hrng->State == HAL_RNG_STATE_READY)
0121   {
0122     /* Change RNG peripheral state */
0123     hrng->State = HAL_RNG_STATE_BUSY;
0124 
0125     /* Disable RNG */
0126     __HAL_RNG_DISABLE(hrng);
0127 
0128     /* RNG CR register configuration. Set value in CR register for :
0129         - NIST Compliance setting
0130         - Clock divider value
0131         - CONFIG 1, CONFIG 2 and CONFIG 3 values */
0132 
0133     cr_value = (uint32_t)(pConf->ClockDivider | pConf->NistCompliance
0134                           | (pConf->Config1 << RNG_CR_RNG_CONFIG1_Pos)
0135                           | (pConf->Config2 << RNG_CR_RNG_CONFIG2_Pos)
0136                           | (pConf->Config3 << RNG_CR_RNG_CONFIG3_Pos));
0137 
0138     MODIFY_REG(hrng->Instance->CR, RNG_CR_NISTC | RNG_CR_CLKDIV | RNG_CR_RNG_CONFIG1
0139                | RNG_CR_RNG_CONFIG2 | RNG_CR_RNG_CONFIG3,
0140                (uint32_t)(RNG_CR_CONDRST | cr_value));
0141 
0142 #if defined(RNG_VER_3_2) || defined(RNG_VER_3_1) || defined(RNG_VER_3_0)
0143     /*!< magic number must be written immediately before to RNG_HTCRG */
0144     WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG_1);
0145     /* for best latency and to be compliant with NIST */
0146     WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG);
0147 #endif /* RNG_VER_3_2 || RNG_VER_3_1 || RNG_VER_3_0 */
0148 
0149     /* Writing bit CONDRST=0*/
0150     CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST);
0151     /* Get tick */
0152     tickstart = HAL_GetTick();
0153 
0154     /* Wait for conditioning reset process to be completed */
0155     while (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
0156     {
0157       if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE)
0158       {
0159         /* New check to avoid false timeout detection in case of prememption */
0160         if (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST))
0161         {
0162           hrng->State = HAL_RNG_STATE_READY;
0163           hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT;
0164           return HAL_ERROR;
0165         }
0166       }
0167     }
0168 
0169     /* Enable RNG */
0170     __HAL_RNG_ENABLE(hrng);
0171 
0172     /* Initialize the RNG state */
0173     hrng->State = HAL_RNG_STATE_READY;
0174 
0175     /* function status */
0176     status = HAL_OK;
0177   }
0178   else
0179   {
0180     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
0181     status = HAL_ERROR;
0182   }
0183 
0184   /* Return the function status */
0185   return status;
0186 }
0187 
0188 /**
0189   * @brief  Get the RNG Configuration and fill parameters in the
0190   *         RNG_ConfigTypeDef.
0191   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
0192   *          the configuration information for RNG.
0193   * @param  pConf pointer to a RNG_ConfigTypeDef structure that contains
0194   *         the configuration information for RNG module
0195 
0196   * @retval HAL status
0197   */
0198 HAL_StatusTypeDef HAL_RNGEx_GetConfig(RNG_HandleTypeDef *hrng, RNG_ConfigTypeDef *pConf)
0199 {
0200 
0201   HAL_StatusTypeDef status ;
0202 
0203   /* Check the RNG handle allocation */
0204   if ((hrng == NULL) || (pConf == NULL))
0205   {
0206     return HAL_ERROR;
0207   }
0208 
0209   /* Check RNG peripheral state */
0210   if (hrng->State == HAL_RNG_STATE_READY)
0211   {
0212     /* Change RNG peripheral state */
0213     hrng->State = HAL_RNG_STATE_BUSY;
0214 
0215     /* Get  RNG parameters  */
0216     pConf->Config1        = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG1) >> RNG_CR_RNG_CONFIG1_Pos) ;
0217     pConf->Config2        = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG2) >> RNG_CR_RNG_CONFIG2_Pos);
0218     pConf->Config3        = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG3) >> RNG_CR_RNG_CONFIG3_Pos);
0219     pConf->ClockDivider   = (hrng->Instance->CR & RNG_CR_CLKDIV);
0220     pConf->NistCompliance = (hrng->Instance->CR & RNG_CR_NISTC);
0221 
0222     /* Initialize the RNG state */
0223     hrng->State = HAL_RNG_STATE_READY;
0224 
0225     /* function status */
0226     status = HAL_OK;
0227   }
0228   else
0229   {
0230     hrng->ErrorCode |= HAL_RNG_ERROR_BUSY;
0231     status = HAL_ERROR;
0232   }
0233 
0234   /* Return the function status */
0235   return status;
0236 }
0237 
0238 /**
0239   * @brief  RNG current configuration lock.
0240   * @note   This function allows to lock RNG peripheral configuration.
0241   *         Once locked, HW RNG reset has to be performed prior any further
0242   *         configuration update.
0243   * @param  hrng pointer to a RNG_HandleTypeDef structure that contains
0244   *                the configuration information for RNG.
0245   * @retval HAL status
0246   */
0247 HAL_StatusTypeDef HAL_RNGEx_LockConfig(RNG_HandleTypeDef *hrng)
0248 {
0249   HAL_StatusTypeDef status;
0250 
0251   /* Check the RNG handle allocation */
0252   if (hrng == NULL)
0253   {
0254     return HAL_ERROR;
0255   }
0256 
0257   /* Check RNG peripheral state */
0258   if (hrng->State == HAL_RNG_STATE_READY)
0259   {
0260     /* Change RNG peripheral state */
0261     hrng->State = HAL_RNG_STATE_BUSY;
0262 
0263     /* Perform RNG configuration Lock */
0264     MODIFY_REG(hrng->Instance->CR, RNG_CR_CONFIGLOCK, RNG_CR_CONFIGLOCK);
0265 
0266     /* Change RNG peripheral state */
0267     hrng->State = HAL_RNG_STATE_READY;
0268 
0269     /* function status */
0270     status = HAL_OK;
0271   }
0272   else
0273   {
0274     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
0275     status = HAL_ERROR;
0276   }
0277 
0278   /* Return the function status */
0279   return status;
0280 }
0281 
0282 
0283 /**
0284   * @}
0285   */
0286 
0287 /** @defgroup RNG_Ex_Exported_Functions_Group2 Recover from seed error function
0288   *  @brief   Recover from seed error function
0289   *
0290 @verbatim
0291  ===============================================================================
0292           ##### Recover from seed error function #####
0293  ===============================================================================
0294     [..]  This section provide function allowing to:
0295       (+) Recover from a seed error
0296 
0297 @endverbatim
0298   * @{
0299   */
0300 
0301 /**
0302   * @brief  RNG sequence to recover from a seed error
0303   * @param  hrng: pointer to a RNG_HandleTypeDef structure.
0304   * @retval HAL status
0305   */
0306 HAL_StatusTypeDef HAL_RNGEx_RecoverSeedError(RNG_HandleTypeDef *hrng)
0307 {
0308   HAL_StatusTypeDef status;
0309 
0310   /* Check the RNG handle allocation */
0311   if (hrng == NULL)
0312   {
0313     return HAL_ERROR;
0314   }
0315 
0316   /* Check RNG peripheral state */
0317   if (hrng->State == HAL_RNG_STATE_READY)
0318   {
0319     /* Change RNG peripheral state */
0320     hrng->State = HAL_RNG_STATE_BUSY;
0321 
0322     /* sequence to fully recover from a seed error */
0323     status = RNG_RecoverSeedError(hrng);
0324   }
0325   else
0326   {
0327     hrng->ErrorCode = HAL_RNG_ERROR_BUSY;
0328     status = HAL_ERROR;
0329   }
0330 
0331   /* Return the function status */
0332   return status;
0333 }
0334 
0335 /**
0336   * @}
0337   */
0338 
0339 /**
0340   * @}
0341   */
0342 
0343 #endif /* RNG_CR_CONDRST */
0344 #endif /* HAL_RNG_MODULE_ENABLED */
0345 /**
0346   * @}
0347   */
0348 
0349 #endif /* RNG */
0350 
0351 /**
0352   * @}
0353   */
0354