STM32H735xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32h7xx_hal_rng_ex.c 00004 * @author MCD Application Team 00005 * @brief Extended RNG HAL module driver. 00006 * This file provides firmware functions to manage the following 00007 * functionalities of the Random Number Generator (RNG) peripheral: 00008 * + Lock configuration functions 00009 * + Reset the RNG 00010 * 00011 ****************************************************************************** 00012 * @attention 00013 * 00014 * Copyright (c) 2017 STMicroelectronics. 00015 * All rights reserved. 00016 * 00017 * This software is licensed under terms that can be found in the LICENSE file 00018 * in the root directory of this software component. 00019 * If no LICENSE file comes with this software, it is provided AS-IS. 00020 * 00021 ****************************************************************************** 00022 */ 00023 00024 /* Includes ------------------------------------------------------------------*/ 00025 #include "stm32h7xx_hal.h" 00026 00027 /** @addtogroup STM32H7xx_HAL_Driver 00028 * @{ 00029 */ 00030 00031 #if defined(RNG) 00032 00033 /** @addtogroup RNG_Ex 00034 * @brief RNG Extended HAL module driver. 00035 * @{ 00036 */ 00037 00038 #ifdef HAL_RNG_MODULE_ENABLED 00039 #if defined(RNG_CR_CONDRST) 00040 /* Private types -------------------------------------------------------------*/ 00041 /* Private defines -----------------------------------------------------------*/ 00042 /** @defgroup RNG_Ex_Private_Defines RNGEx Private Defines 00043 * @{ 00044 */ 00045 /* Health test control register information to use in CCM algorithm */ 00046 #define RNG_HTCFG_1 0x17590ABCU /*!< Magic number */ 00047 #if defined(RNG_VER_3_1) || defined(RNG_VER_3_0) 00048 #define RNG_HTCFG 0x000CAA74U /*!< For best latency and to be compliant with NIST */ 00049 #else /* RNG_VER_3_2 */ 00050 #define RNG_HTCFG 0x00007274U /*!< For best latency and to be compliant with NIST */ 00051 #endif /* RNG_VER_3_1 || RNG_VER_3_0 */ 00052 /** 00053 * @} 00054 */ 00055 /* Private variables ---------------------------------------------------------*/ 00056 /* Private constants ---------------------------------------------------------*/ 00057 /** @defgroup RNG_Ex_Private_Constants RNGEx Private Constants 00058 * @{ 00059 */ 00060 #define RNG_TIMEOUT_VALUE 2U 00061 /** 00062 * @} 00063 */ 00064 /* Private macros ------------------------------------------------------------*/ 00065 /* Private functions prototypes ----------------------------------------------*/ 00066 /* Private functions --------------------------------------------------------*/ 00067 /* Exported functions --------------------------------------------------------*/ 00068 00069 /** @addtogroup RNG_Ex_Exported_Functions 00070 * @{ 00071 */ 00072 00073 /** @addtogroup RNG_Ex_Exported_Functions_Group1 00074 * @brief Configuration functions 00075 * 00076 @verbatim 00077 =============================================================================== 00078 ##### Configuration and lock functions ##### 00079 =============================================================================== 00080 [..] This section provides functions allowing to: 00081 (+) Configure the RNG with the specified parameters in the RNG_ConfigTypeDef 00082 (+) Lock RNG configuration Allows user to lock a configuration until next reset. 00083 00084 @endverbatim 00085 * @{ 00086 */ 00087 00088 /** 00089 * @brief Configure the RNG with the specified parameters in the 00090 * RNG_ConfigTypeDef. 00091 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00092 * the configuration information for RNG. 00093 * @param pConf: pointer to a RNG_ConfigTypeDef structure that contains 00094 * the configuration information for RNG module 00095 00096 * @retval HAL status 00097 */ 00098 HAL_StatusTypeDef HAL_RNGEx_SetConfig(RNG_HandleTypeDef *hrng, RNG_ConfigTypeDef *pConf) 00099 { 00100 uint32_t tickstart; 00101 uint32_t cr_value; 00102 HAL_StatusTypeDef status ; 00103 00104 /* Check the RNG handle allocation */ 00105 if ((hrng == NULL) || (pConf == NULL)) 00106 { 00107 return HAL_ERROR; 00108 } 00109 00110 /* Check the parameters */ 00111 assert_param(IS_RNG_ALL_INSTANCE(hrng->Instance)); 00112 assert_param(IS_RNG_CLOCK_DIVIDER(pConf->ClockDivider)); 00113 assert_param(IS_RNG_NIST_COMPLIANCE(pConf->NistCompliance)); 00114 assert_param(IS_RNG_CONFIG1(pConf->Config1)); 00115 assert_param(IS_RNG_CONFIG2(pConf->Config2)); 00116 assert_param(IS_RNG_CONFIG3(pConf->Config3)); 00117 00118 /* Check RNG peripheral state */ 00119 if (hrng->State == HAL_RNG_STATE_READY) 00120 { 00121 /* Change RNG peripheral state */ 00122 hrng->State = HAL_RNG_STATE_BUSY; 00123 00124 /* Disable RNG */ 00125 __HAL_RNG_DISABLE(hrng); 00126 00127 /* RNG CR register configuration. Set value in CR register for : 00128 - NIST Compliance setting 00129 - Clock divider value 00130 - CONFIG 1, CONFIG 2 and CONFIG 3 values */ 00131 00132 cr_value = (uint32_t)(pConf->ClockDivider | pConf->NistCompliance 00133 | (pConf->Config1 << RNG_CR_RNG_CONFIG1_Pos) 00134 | (pConf->Config2 << RNG_CR_RNG_CONFIG2_Pos) 00135 | (pConf->Config3 << RNG_CR_RNG_CONFIG3_Pos)); 00136 00137 MODIFY_REG(hrng->Instance->CR, RNG_CR_NISTC | RNG_CR_CLKDIV | RNG_CR_RNG_CONFIG1 00138 | RNG_CR_RNG_CONFIG2 | RNG_CR_RNG_CONFIG3, 00139 (uint32_t)(RNG_CR_CONDRST | cr_value)); 00140 00141 #if defined(RNG_VER_3_2) || defined(RNG_VER_3_1) || defined(RNG_VER_3_0) 00142 /*!< magic number must be written immediately before to RNG_HTCRG */ 00143 WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG_1); 00144 /* for best latency and to be compliant with NIST */ 00145 WRITE_REG(hrng->Instance->HTCR, RNG_HTCFG); 00146 #endif /* RNG_VER_3_2 || RNG_VER_3_1 || RNG_VER_3_0 */ 00147 00148 /* Writing bit CONDRST=0*/ 00149 CLEAR_BIT(hrng->Instance->CR, RNG_CR_CONDRST); 00150 /* Get tick */ 00151 tickstart = HAL_GetTick(); 00152 00153 /* Wait for conditioning reset process to be completed */ 00154 while (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST)) 00155 { 00156 if ((HAL_GetTick() - tickstart) > RNG_TIMEOUT_VALUE) 00157 { 00158 /* New check to avoid false timeout detection in case of prememption */ 00159 if (HAL_IS_BIT_SET(hrng->Instance->CR, RNG_CR_CONDRST)) 00160 { 00161 hrng->State = HAL_RNG_STATE_READY; 00162 hrng->ErrorCode = HAL_RNG_ERROR_TIMEOUT; 00163 return HAL_ERROR; 00164 } 00165 } 00166 } 00167 00168 /* Enable RNG */ 00169 __HAL_RNG_ENABLE(hrng); 00170 00171 /* Initialize the RNG state */ 00172 hrng->State = HAL_RNG_STATE_READY; 00173 00174 /* function status */ 00175 status = HAL_OK; 00176 } 00177 else 00178 { 00179 hrng->ErrorCode = HAL_RNG_ERROR_BUSY; 00180 status = HAL_ERROR; 00181 } 00182 00183 /* Return the function status */ 00184 return status; 00185 } 00186 00187 /** 00188 * @brief Get the RNG Configuration and fill parameters in the 00189 * RNG_ConfigTypeDef. 00190 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00191 * the configuration information for RNG. 00192 * @param pConf: pointer to a RNG_ConfigTypeDef structure that contains 00193 * the configuration information for RNG module 00194 00195 * @retval HAL status 00196 */ 00197 HAL_StatusTypeDef HAL_RNGEx_GetConfig(RNG_HandleTypeDef *hrng, RNG_ConfigTypeDef *pConf) 00198 { 00199 00200 HAL_StatusTypeDef status ; 00201 00202 /* Check the RNG handle allocation */ 00203 if ((hrng == NULL) || (pConf == NULL)) 00204 { 00205 return HAL_ERROR; 00206 } 00207 00208 /* Check RNG peripheral state */ 00209 if (hrng->State == HAL_RNG_STATE_READY) 00210 { 00211 /* Change RNG peripheral state */ 00212 hrng->State = HAL_RNG_STATE_BUSY; 00213 00214 /* Get RNG parameters */ 00215 pConf->Config1 = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG1) >> RNG_CR_RNG_CONFIG1_Pos) ; 00216 pConf->Config2 = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG2) >> RNG_CR_RNG_CONFIG2_Pos); 00217 pConf->Config3 = (uint32_t)((hrng->Instance->CR & RNG_CR_RNG_CONFIG3) >> RNG_CR_RNG_CONFIG3_Pos); 00218 pConf->ClockDivider = (hrng->Instance->CR & RNG_CR_CLKDIV); 00219 pConf->NistCompliance = (hrng->Instance->CR & RNG_CR_NISTC); 00220 00221 /* Initialize the RNG state */ 00222 hrng->State = HAL_RNG_STATE_READY; 00223 00224 /* function status */ 00225 status = HAL_OK; 00226 } 00227 else 00228 { 00229 hrng->ErrorCode |= HAL_RNG_ERROR_BUSY; 00230 status = HAL_ERROR; 00231 } 00232 00233 /* Return the function status */ 00234 return status; 00235 } 00236 00237 /** 00238 * @brief RNG current configuration lock. 00239 * @note This function allows to lock RNG peripheral configuration. 00240 * Once locked, HW RNG reset has to be performed prior any further 00241 * configuration update. 00242 * @param hrng pointer to a RNG_HandleTypeDef structure that contains 00243 * the configuration information for RNG. 00244 * @retval HAL status 00245 */ 00246 HAL_StatusTypeDef HAL_RNGEx_LockConfig(RNG_HandleTypeDef *hrng) 00247 { 00248 HAL_StatusTypeDef status; 00249 00250 /* Check the RNG handle allocation */ 00251 if (hrng == NULL) 00252 { 00253 return HAL_ERROR; 00254 } 00255 00256 /* Check RNG peripheral state */ 00257 if (hrng->State == HAL_RNG_STATE_READY) 00258 { 00259 /* Change RNG peripheral state */ 00260 hrng->State = HAL_RNG_STATE_BUSY; 00261 00262 /* Perform RNG configuration Lock */ 00263 MODIFY_REG(hrng->Instance->CR, RNG_CR_CONFIGLOCK, RNG_CR_CONFIGLOCK); 00264 00265 /* Change RNG peripheral state */ 00266 hrng->State = HAL_RNG_STATE_READY; 00267 00268 /* function status */ 00269 status = HAL_OK; 00270 } 00271 else 00272 { 00273 hrng->ErrorCode = HAL_RNG_ERROR_BUSY; 00274 status = HAL_ERROR; 00275 } 00276 00277 /* Return the function status */ 00278 return status; 00279 } 00280 00281 00282 /** 00283 * @} 00284 */ 00285 00286 /** @addtogroup RNG_Ex_Exported_Functions_Group2 00287 * @brief Recover from seed error function 00288 * 00289 @verbatim 00290 =============================================================================== 00291 ##### Configuration and lock functions ##### 00292 =============================================================================== 00293 [..] This section provide function allowing to: 00294 (+) Recover from a seed error 00295 00296 @endverbatim 00297 * @{ 00298 */ 00299 00300 /** 00301 * @brief RNG sequence to recover from a seed error 00302 * @param hrng: pointer to a RNG_HandleTypeDef structure. 00303 * @retval HAL status 00304 */ 00305 HAL_StatusTypeDef HAL_RNGEx_RecoverSeedError(RNG_HandleTypeDef *hrng) 00306 { 00307 HAL_StatusTypeDef status; 00308 00309 /* Check the RNG handle allocation */ 00310 if (hrng == NULL) 00311 { 00312 return HAL_ERROR; 00313 } 00314 00315 /* Check RNG peripheral state */ 00316 if (hrng->State == HAL_RNG_STATE_READY) 00317 { 00318 /* Change RNG peripheral state */ 00319 hrng->State = HAL_RNG_STATE_BUSY; 00320 00321 /* sequence to fully recover from a seed error */ 00322 status = RNG_RecoverSeedError(hrng); 00323 } 00324 else 00325 { 00326 hrng->ErrorCode = HAL_RNG_ERROR_BUSY; 00327 status = HAL_ERROR; 00328 } 00329 00330 /* Return the function status */ 00331 return status; 00332 } 00333 00334 /** 00335 * @} 00336 */ 00337 00338 /** 00339 * @} 00340 */ 00341 00342 #endif /* RNG_CR_CONDRST */ 00343 #endif /* HAL_RNG_MODULE_ENABLED */ 00344 /** 00345 * @} 00346 */ 00347 00348 #endif /* RNG */ 00349 00350 /** 00351 * @} 00352 */ 00353