001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /**
ganlikun 0:13413ea9a877 2 ******************************************************************************
ganlikun 0:13413ea9a877 3 * @file stm32f4xx_hal_rng.c
ganlikun 0:13413ea9a877 4 * @author MCD Application Team
ganlikun 0:13413ea9a877 5 * @version V1.7.1
ganlikun 0:13413ea9a877 6 * @date 14-April-2017
ganlikun 0:13413ea9a877 7 * @brief RNG HAL module driver.
ganlikun 0:13413ea9a877 8 * This file provides firmware functions to manage the following
ganlikun 0:13413ea9a877 9 * functionalities of the Random Number Generator (RNG) peripheral:
ganlikun 0:13413ea9a877 10 * + Initialization/de-initialization functions
ganlikun 0:13413ea9a877 11 * + Peripheral Control functions
ganlikun 0:13413ea9a877 12 * + Peripheral State functions
ganlikun 0:13413ea9a877 13 *
ganlikun 0:13413ea9a877 14 @verbatim
ganlikun 0:13413ea9a877 15 ==============================================================================
ganlikun 0:13413ea9a877 16 ##### How to use this driver #####
ganlikun 0:13413ea9a877 17 ==============================================================================
ganlikun 0:13413ea9a877 18 [..]
ganlikun 0:13413ea9a877 19 The RNG HAL driver can be used as follows:
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
ganlikun 0:13413ea9a877 22 in HAL_RNG_MspInit().
ganlikun 0:13413ea9a877 23 (#) Activate the RNG peripheral using HAL_RNG_Init() function.
ganlikun 0:13413ea9a877 24 (#) Wait until the 32 bit Random Number Generator contains a valid
ganlikun 0:13413ea9a877 25 random data using (polling/interrupt) mode.
ganlikun 0:13413ea9a877 26 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
ganlikun 0:13413ea9a877 27
ganlikun 0:13413ea9a877 28 @endverbatim
ganlikun 0:13413ea9a877 29 ******************************************************************************
ganlikun 0:13413ea9a877 30 * @attention
ganlikun 0:13413ea9a877 31 *
ganlikun 0:13413ea9a877 32 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
ganlikun 0:13413ea9a877 33 *
ganlikun 0:13413ea9a877 34 * Redistribution and use in source and binary forms, with or without modification,
ganlikun 0:13413ea9a877 35 * are permitted provided that the following conditions are met:
ganlikun 0:13413ea9a877 36 * 1. Redistributions of source code must retain the above copyright notice,
ganlikun 0:13413ea9a877 37 * this list of conditions and the following disclaimer.
ganlikun 0:13413ea9a877 38 * 2. Redistributions in binary form must reproduce the above copyright notice,
ganlikun 0:13413ea9a877 39 * this list of conditions and the following disclaimer in the documentation
ganlikun 0:13413ea9a877 40 * and/or other materials provided with the distribution.
ganlikun 0:13413ea9a877 41 * 3. Neither the name of STMicroelectronics nor the names of its contributors
ganlikun 0:13413ea9a877 42 * may be used to endorse or promote products derived from this software
ganlikun 0:13413ea9a877 43 * without specific prior written permission.
ganlikun 0:13413ea9a877 44 *
ganlikun 0:13413ea9a877 45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
ganlikun 0:13413ea9a877 46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ganlikun 0:13413ea9a877 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
ganlikun 0:13413ea9a877 48 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
ganlikun 0:13413ea9a877 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ganlikun 0:13413ea9a877 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
ganlikun 0:13413ea9a877 51 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
ganlikun 0:13413ea9a877 52 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
ganlikun 0:13413ea9a877 53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
ganlikun 0:13413ea9a877 54 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ganlikun 0:13413ea9a877 55 *
ganlikun 0:13413ea9a877 56 ******************************************************************************
ganlikun 0:13413ea9a877 57 */
ganlikun 0:13413ea9a877 58
ganlikun 0:13413ea9a877 59 /* Includes ------------------------------------------------------------------*/
ganlikun 0:13413ea9a877 60 #include "stm32f4xx_hal.h"
ganlikun 0:13413ea9a877 61
ganlikun 0:13413ea9a877 62 /** @addtogroup STM32F4xx_HAL_Driver
ganlikun 0:13413ea9a877 63 * @{
ganlikun 0:13413ea9a877 64 */
ganlikun 0:13413ea9a877 65
ganlikun 0:13413ea9a877 66 /** @addtogroup RNG
ganlikun 0:13413ea9a877 67 * @{
ganlikun 0:13413ea9a877 68 */
ganlikun 0:13413ea9a877 69
ganlikun 0:13413ea9a877 70 #ifdef HAL_RNG_MODULE_ENABLED
ganlikun 0:13413ea9a877 71
ganlikun 0:13413ea9a877 72 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
ganlikun 0:13413ea9a877 73 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
ganlikun 0:13413ea9a877 74 defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F469xx) ||\
ganlikun 0:13413ea9a877 75 defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
ganlikun 0:13413ea9a877 76 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
ganlikun 0:13413ea9a877 77
ganlikun 0:13413ea9a877 78
ganlikun 0:13413ea9a877 79 /* Private types -------------------------------------------------------------*/
ganlikun 0:13413ea9a877 80 /* Private defines -----------------------------------------------------------*/
ganlikun 0:13413ea9a877 81 /* Private variables ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 82 /* Private constants ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 83 /** @addtogroup RNG_Private_Constants
ganlikun 0:13413ea9a877 84 * @{
ganlikun 0:13413ea9a877 85 */
ganlikun 0:13413ea9a877 86 #define RNG_TIMEOUT_VALUE 2U
ganlikun 0:13413ea9a877 87 /**
ganlikun 0:13413ea9a877 88 * @}
ganlikun 0:13413ea9a877 89 */
ganlikun 0:13413ea9a877 90 /* Private macros ------------------------------------------------------------*/
ganlikun 0:13413ea9a877 91 /* Private functions prototypes ----------------------------------------------*/
ganlikun 0:13413ea9a877 92 /* Private functions ---------------------------------------------------------*/
ganlikun 0:13413ea9a877 93 /* Exported functions --------------------------------------------------------*/
ganlikun 0:13413ea9a877 94
ganlikun 0:13413ea9a877 95 /** @addtogroup RNG_Exported_Functions
ganlikun 0:13413ea9a877 96 * @{
ganlikun 0:13413ea9a877 97 */
ganlikun 0:13413ea9a877 98
ganlikun 0:13413ea9a877 99 /** @addtogroup RNG_Exported_Functions_Group1
ganlikun 0:13413ea9a877 100 * @brief Initialization and de-initialization functions
ganlikun 0:13413ea9a877 101 *
ganlikun 0:13413ea9a877 102 @verbatim
ganlikun 0:13413ea9a877 103 ===============================================================================
ganlikun 0:13413ea9a877 104 ##### Initialization and de-initialization functions #####
ganlikun 0:13413ea9a877 105 ===============================================================================
ganlikun 0:13413ea9a877 106 [..] This section provides functions allowing to:
ganlikun 0:13413ea9a877 107 (+) Initialize the RNG according to the specified parameters
ganlikun 0:13413ea9a877 108 in the RNG_InitTypeDef and create the associated handle
ganlikun 0:13413ea9a877 109 (+) DeInitialize the RNG peripheral
ganlikun 0:13413ea9a877 110 (+) Initialize the RNG MSP
ganlikun 0:13413ea9a877 111 (+) DeInitialize RNG MSP
ganlikun 0:13413ea9a877 112
ganlikun 0:13413ea9a877 113 @endverbatim
ganlikun 0:13413ea9a877 114 * @{
ganlikun 0:13413ea9a877 115 */
ganlikun 0:13413ea9a877 116
ganlikun 0:13413ea9a877 117 /**
ganlikun 0:13413ea9a877 118 * @brief Initializes the RNG peripheral and creates the associated handle.
ganlikun 0:13413ea9a877 119 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 120 * the configuration information for RNG.
ganlikun 0:13413ea9a877 121 * @retval HAL status
ganlikun 0:13413ea9a877 122 */
ganlikun 0:13413ea9a877 123 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 124 {
ganlikun 0:13413ea9a877 125 /* Check the RNG handle allocation */
ganlikun 0:13413ea9a877 126 if(hrng == NULL)
ganlikun 0:13413ea9a877 127 {
ganlikun 0:13413ea9a877 128 return HAL_ERROR;
ganlikun 0:13413ea9a877 129 }
ganlikun 0:13413ea9a877 130
ganlikun 0:13413ea9a877 131 __HAL_LOCK(hrng);
ganlikun 0:13413ea9a877 132
ganlikun 0:13413ea9a877 133 if(hrng->State == HAL_RNG_STATE_RESET)
ganlikun 0:13413ea9a877 134 {
ganlikun 0:13413ea9a877 135 /* Allocate lock resource and initialize it */
ganlikun 0:13413ea9a877 136 hrng->Lock = HAL_UNLOCKED;
ganlikun 0:13413ea9a877 137 /* Init the low level hardware */
ganlikun 0:13413ea9a877 138 HAL_RNG_MspInit(hrng);
ganlikun 0:13413ea9a877 139 }
ganlikun 0:13413ea9a877 140
ganlikun 0:13413ea9a877 141 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 142 hrng->State = HAL_RNG_STATE_BUSY;
ganlikun 0:13413ea9a877 143
ganlikun 0:13413ea9a877 144 /* Enable the RNG Peripheral */
ganlikun 0:13413ea9a877 145 __HAL_RNG_ENABLE(hrng);
ganlikun 0:13413ea9a877 146
ganlikun 0:13413ea9a877 147 /* Initialize the RNG state */
ganlikun 0:13413ea9a877 148 hrng->State = HAL_RNG_STATE_READY;
ganlikun 0:13413ea9a877 149
ganlikun 0:13413ea9a877 150 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 151
ganlikun 0:13413ea9a877 152 /* Return function status */
ganlikun 0:13413ea9a877 153 return HAL_OK;
ganlikun 0:13413ea9a877 154 }
ganlikun 0:13413ea9a877 155
ganlikun 0:13413ea9a877 156 /**
ganlikun 0:13413ea9a877 157 * @brief DeInitializes the RNG peripheral.
ganlikun 0:13413ea9a877 158 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 159 * the configuration information for RNG.
ganlikun 0:13413ea9a877 160 * @retval HAL status
ganlikun 0:13413ea9a877 161 */
ganlikun 0:13413ea9a877 162 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 163 {
ganlikun 0:13413ea9a877 164 /* Check the RNG handle allocation */
ganlikun 0:13413ea9a877 165 if(hrng == NULL)
ganlikun 0:13413ea9a877 166 {
ganlikun 0:13413ea9a877 167 return HAL_ERROR;
ganlikun 0:13413ea9a877 168 }
ganlikun 0:13413ea9a877 169 /* Disable the RNG Peripheral */
ganlikun 0:13413ea9a877 170 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
ganlikun 0:13413ea9a877 171
ganlikun 0:13413ea9a877 172 /* Clear RNG interrupt status flags */
ganlikun 0:13413ea9a877 173 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
ganlikun 0:13413ea9a877 174
ganlikun 0:13413ea9a877 175 /* DeInit the low level hardware */
ganlikun 0:13413ea9a877 176 HAL_RNG_MspDeInit(hrng);
ganlikun 0:13413ea9a877 177
ganlikun 0:13413ea9a877 178 /* Update the RNG state */
ganlikun 0:13413ea9a877 179 hrng->State = HAL_RNG_STATE_RESET;
ganlikun 0:13413ea9a877 180
ganlikun 0:13413ea9a877 181 /* Release Lock */
ganlikun 0:13413ea9a877 182 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 183
ganlikun 0:13413ea9a877 184 /* Return the function status */
ganlikun 0:13413ea9a877 185 return HAL_OK;
ganlikun 0:13413ea9a877 186 }
ganlikun 0:13413ea9a877 187
ganlikun 0:13413ea9a877 188 /**
ganlikun 0:13413ea9a877 189 * @brief Initializes the RNG MSP.
ganlikun 0:13413ea9a877 190 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 191 * the configuration information for RNG.
ganlikun 0:13413ea9a877 192 * @retval None
ganlikun 0:13413ea9a877 193 */
ganlikun 0:13413ea9a877 194 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 195 {
ganlikun 0:13413ea9a877 196 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 197 UNUSED(hrng);
ganlikun 0:13413ea9a877 198 /* NOTE : This function should not be modified. When the callback is needed,
ganlikun 0:13413ea9a877 199 function HAL_RNG_MspInit must be implemented in the user file.
ganlikun 0:13413ea9a877 200 */
ganlikun 0:13413ea9a877 201 }
ganlikun 0:13413ea9a877 202
ganlikun 0:13413ea9a877 203 /**
ganlikun 0:13413ea9a877 204 * @brief DeInitializes the RNG MSP.
ganlikun 0:13413ea9a877 205 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 206 * the configuration information for RNG.
ganlikun 0:13413ea9a877 207 * @retval None
ganlikun 0:13413ea9a877 208 */
ganlikun 0:13413ea9a877 209 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 210 {
ganlikun 0:13413ea9a877 211 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 212 UNUSED(hrng);
ganlikun 0:13413ea9a877 213 /* NOTE : This function should not be modified. When the callback is needed,
ganlikun 0:13413ea9a877 214 function HAL_RNG_MspDeInit must be implemented in the user file.
ganlikun 0:13413ea9a877 215 */
ganlikun 0:13413ea9a877 216 }
ganlikun 0:13413ea9a877 217
ganlikun 0:13413ea9a877 218 /**
ganlikun 0:13413ea9a877 219 * @}
ganlikun 0:13413ea9a877 220 */
ganlikun 0:13413ea9a877 221
ganlikun 0:13413ea9a877 222 /** @addtogroup RNG_Exported_Functions_Group2
ganlikun 0:13413ea9a877 223 * @brief Peripheral Control functions
ganlikun 0:13413ea9a877 224 *
ganlikun 0:13413ea9a877 225 @verbatim
ganlikun 0:13413ea9a877 226 ===============================================================================
ganlikun 0:13413ea9a877 227 ##### Peripheral Control functions #####
ganlikun 0:13413ea9a877 228 ===============================================================================
ganlikun 0:13413ea9a877 229 [..] This section provides functions allowing to:
ganlikun 0:13413ea9a877 230 (+) Get the 32 bit Random number
ganlikun 0:13413ea9a877 231 (+) Get the 32 bit Random number with interrupt enabled
ganlikun 0:13413ea9a877 232 (+) Handle RNG interrupt request
ganlikun 0:13413ea9a877 233
ganlikun 0:13413ea9a877 234 @endverbatim
ganlikun 0:13413ea9a877 235 * @{
ganlikun 0:13413ea9a877 236 */
ganlikun 0:13413ea9a877 237
ganlikun 0:13413ea9a877 238 /**
ganlikun 0:13413ea9a877 239 * @brief Generates a 32-bit random number.
ganlikun 0:13413ea9a877 240 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
ganlikun 0:13413ea9a877 241 * is automatically cleared.
ganlikun 0:13413ea9a877 242 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 243 * the configuration information for RNG.
ganlikun 0:13413ea9a877 244 * @param random32bit: pointer to generated random number variable if successful.
ganlikun 0:13413ea9a877 245 * @retval HAL status
ganlikun 0:13413ea9a877 246 */
ganlikun 0:13413ea9a877 247
ganlikun 0:13413ea9a877 248 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
ganlikun 0:13413ea9a877 249 {
ganlikun 0:13413ea9a877 250 uint32_t tickstart = 0U;
ganlikun 0:13413ea9a877 251 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 252
ganlikun 0:13413ea9a877 253 /* Process Locked */
ganlikun 0:13413ea9a877 254 __HAL_LOCK(hrng);
ganlikun 0:13413ea9a877 255
ganlikun 0:13413ea9a877 256 /* Check RNG peripheral state */
ganlikun 0:13413ea9a877 257 if(hrng->State == HAL_RNG_STATE_READY)
ganlikun 0:13413ea9a877 258 {
ganlikun 0:13413ea9a877 259 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 260 hrng->State = HAL_RNG_STATE_BUSY;
ganlikun 0:13413ea9a877 261
ganlikun 0:13413ea9a877 262 /* Get tick */
ganlikun 0:13413ea9a877 263 tickstart = HAL_GetTick();
ganlikun 0:13413ea9a877 264
ganlikun 0:13413ea9a877 265 /* Check if data register contains valid random data */
ganlikun 0:13413ea9a877 266 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
ganlikun 0:13413ea9a877 267 {
ganlikun 0:13413ea9a877 268 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
ganlikun 0:13413ea9a877 269 {
ganlikun 0:13413ea9a877 270 hrng->State = HAL_RNG_STATE_ERROR;
ganlikun 0:13413ea9a877 271
ganlikun 0:13413ea9a877 272 /* Process Unlocked */
ganlikun 0:13413ea9a877 273 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 274
ganlikun 0:13413ea9a877 275 return HAL_TIMEOUT;
ganlikun 0:13413ea9a877 276 }
ganlikun 0:13413ea9a877 277 }
ganlikun 0:13413ea9a877 278
ganlikun 0:13413ea9a877 279 /* Get a 32bit Random number */
ganlikun 0:13413ea9a877 280 hrng->RandomNumber = hrng->Instance->DR;
ganlikun 0:13413ea9a877 281 *random32bit = hrng->RandomNumber;
ganlikun 0:13413ea9a877 282
ganlikun 0:13413ea9a877 283 hrng->State = HAL_RNG_STATE_READY;
ganlikun 0:13413ea9a877 284 }
ganlikun 0:13413ea9a877 285 else
ganlikun 0:13413ea9a877 286 {
ganlikun 0:13413ea9a877 287 status = HAL_ERROR;
ganlikun 0:13413ea9a877 288 }
ganlikun 0:13413ea9a877 289
ganlikun 0:13413ea9a877 290 /* Process Unlocked */
ganlikun 0:13413ea9a877 291 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 292
ganlikun 0:13413ea9a877 293 return status;
ganlikun 0:13413ea9a877 294 }
ganlikun 0:13413ea9a877 295
ganlikun 0:13413ea9a877 296 /**
ganlikun 0:13413ea9a877 297 * @brief Generates a 32-bit random number in interrupt mode.
ganlikun 0:13413ea9a877 298 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 299 * the configuration information for RNG.
ganlikun 0:13413ea9a877 300 * @retval HAL status
ganlikun 0:13413ea9a877 301 */
ganlikun 0:13413ea9a877 302 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 303 {
ganlikun 0:13413ea9a877 304 HAL_StatusTypeDef status = HAL_OK;
ganlikun 0:13413ea9a877 305
ganlikun 0:13413ea9a877 306 /* Process Locked */
ganlikun 0:13413ea9a877 307 __HAL_LOCK(hrng);
ganlikun 0:13413ea9a877 308
ganlikun 0:13413ea9a877 309 /* Check RNG peripheral state */
ganlikun 0:13413ea9a877 310 if(hrng->State == HAL_RNG_STATE_READY)
ganlikun 0:13413ea9a877 311 {
ganlikun 0:13413ea9a877 312 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 313 hrng->State = HAL_RNG_STATE_BUSY;
ganlikun 0:13413ea9a877 314
ganlikun 0:13413ea9a877 315 /* Process Unlocked */
ganlikun 0:13413ea9a877 316 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 317
ganlikun 0:13413ea9a877 318 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
ganlikun 0:13413ea9a877 319 __HAL_RNG_ENABLE_IT(hrng);
ganlikun 0:13413ea9a877 320 }
ganlikun 0:13413ea9a877 321 else
ganlikun 0:13413ea9a877 322 {
ganlikun 0:13413ea9a877 323 /* Process Unlocked */
ganlikun 0:13413ea9a877 324 __HAL_UNLOCK(hrng);
ganlikun 0:13413ea9a877 325
ganlikun 0:13413ea9a877 326 status = HAL_ERROR;
ganlikun 0:13413ea9a877 327 }
ganlikun 0:13413ea9a877 328
ganlikun 0:13413ea9a877 329 return status;
ganlikun 0:13413ea9a877 330 }
ganlikun 0:13413ea9a877 331
ganlikun 0:13413ea9a877 332 /**
ganlikun 0:13413ea9a877 333 * @brief Handles RNG interrupt request.
ganlikun 0:13413ea9a877 334 * @note In the case of a clock error, the RNG is no more able to generate
ganlikun 0:13413ea9a877 335 * random numbers because the PLL48CLK clock is not correct. User has
ganlikun 0:13413ea9a877 336 * to check that the clock controller is correctly configured to provide
ganlikun 0:13413ea9a877 337 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
ganlikun 0:13413ea9a877 338 * The clock error has no impact on the previously generated
ganlikun 0:13413ea9a877 339 * random numbers, and the RNG_DR register contents can be used.
ganlikun 0:13413ea9a877 340 * @note In the case of a seed error, the generation of random numbers is
ganlikun 0:13413ea9a877 341 * interrupted as long as the SECS bit is '1'. If a number is
ganlikun 0:13413ea9a877 342 * available in the RNG_DR register, it must not be used because it may
ganlikun 0:13413ea9a877 343 * not have enough entropy. In this case, it is recommended to clear the
ganlikun 0:13413ea9a877 344 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
ganlikun 0:13413ea9a877 345 * the RNG peripheral to reinitialize and restart the RNG.
ganlikun 0:13413ea9a877 346 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
ganlikun 0:13413ea9a877 347 * or CEIS are set.
ganlikun 0:13413ea9a877 348 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 349 * the configuration information for RNG.
ganlikun 0:13413ea9a877 350 * @retval None
ganlikun 0:13413ea9a877 351
ganlikun 0:13413ea9a877 352 */
ganlikun 0:13413ea9a877 353 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 354 {
ganlikun 0:13413ea9a877 355 /* RNG clock error interrupt occurred */
ganlikun 0:13413ea9a877 356 if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
ganlikun 0:13413ea9a877 357 {
ganlikun 0:13413ea9a877 358 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 359 hrng->State = HAL_RNG_STATE_ERROR;
ganlikun 0:13413ea9a877 360
ganlikun 0:13413ea9a877 361 HAL_RNG_ErrorCallback(hrng);
ganlikun 0:13413ea9a877 362
ganlikun 0:13413ea9a877 363 /* Clear the clock error flag */
ganlikun 0:13413ea9a877 364 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
ganlikun 0:13413ea9a877 365
ganlikun 0:13413ea9a877 366 }
ganlikun 0:13413ea9a877 367
ganlikun 0:13413ea9a877 368 /* Check RNG data ready interrupt occurred */
ganlikun 0:13413ea9a877 369 if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
ganlikun 0:13413ea9a877 370 {
ganlikun 0:13413ea9a877 371 /* Generate random number once, so disable the IT */
ganlikun 0:13413ea9a877 372 __HAL_RNG_DISABLE_IT(hrng);
ganlikun 0:13413ea9a877 373
ganlikun 0:13413ea9a877 374 /* Get the 32bit Random number (DRDY flag automatically cleared) */
ganlikun 0:13413ea9a877 375 hrng->RandomNumber = hrng->Instance->DR;
ganlikun 0:13413ea9a877 376
ganlikun 0:13413ea9a877 377 if(hrng->State != HAL_RNG_STATE_ERROR)
ganlikun 0:13413ea9a877 378 {
ganlikun 0:13413ea9a877 379 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 380 hrng->State = HAL_RNG_STATE_READY;
ganlikun 0:13413ea9a877 381
ganlikun 0:13413ea9a877 382 /* Data Ready callback */
ganlikun 0:13413ea9a877 383 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
ganlikun 0:13413ea9a877 384 }
ganlikun 0:13413ea9a877 385 }
ganlikun 0:13413ea9a877 386 }
ganlikun 0:13413ea9a877 387
ganlikun 0:13413ea9a877 388 /**
ganlikun 0:13413ea9a877 389 * @brief Returns generated random number in polling mode (Obsolete)
ganlikun 0:13413ea9a877 390 * Use HAL_RNG_GenerateRandomNumber() API instead.
ganlikun 0:13413ea9a877 391 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 392 * the configuration information for RNG.
ganlikun 0:13413ea9a877 393 * @retval Random value
ganlikun 0:13413ea9a877 394 */
ganlikun 0:13413ea9a877 395 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 396 {
ganlikun 0:13413ea9a877 397 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
ganlikun 0:13413ea9a877 398 {
ganlikun 0:13413ea9a877 399 return hrng->RandomNumber;
ganlikun 0:13413ea9a877 400 }
ganlikun 0:13413ea9a877 401 else
ganlikun 0:13413ea9a877 402 {
ganlikun 0:13413ea9a877 403 return 0U;
ganlikun 0:13413ea9a877 404 }
ganlikun 0:13413ea9a877 405 }
ganlikun 0:13413ea9a877 406
ganlikun 0:13413ea9a877 407 /**
ganlikun 0:13413ea9a877 408 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
ganlikun 0:13413ea9a877 409 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
ganlikun 0:13413ea9a877 410 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 411 * the configuration information for RNG.
ganlikun 0:13413ea9a877 412 * @retval 32-bit random number
ganlikun 0:13413ea9a877 413 */
ganlikun 0:13413ea9a877 414 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 415 {
ganlikun 0:13413ea9a877 416 uint32_t random32bit = 0U;
ganlikun 0:13413ea9a877 417
ganlikun 0:13413ea9a877 418 /* Process locked */
ganlikun 0:13413ea9a877 419 __HAL_LOCK(hrng);
ganlikun 0:13413ea9a877 420
ganlikun 0:13413ea9a877 421 /* Change RNG peripheral state */
ganlikun 0:13413ea9a877 422 hrng->State = HAL_RNG_STATE_BUSY;
ganlikun 0:13413ea9a877 423
ganlikun 0:13413ea9a877 424 /* Get a 32bit Random number */
ganlikun 0:13413ea9a877 425 random32bit = hrng->Instance->DR;
ganlikun 0:13413ea9a877 426
ganlikun 0:13413ea9a877 427 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
ganlikun 0:13413ea9a877 428 __HAL_RNG_ENABLE_IT(hrng);
ganlikun 0:13413ea9a877 429
ganlikun 0:13413ea9a877 430 /* Return the 32 bit random number */
ganlikun 0:13413ea9a877 431 return random32bit;
ganlikun 0:13413ea9a877 432 }
ganlikun 0:13413ea9a877 433
ganlikun 0:13413ea9a877 434 /**
ganlikun 0:13413ea9a877 435 * @brief Read latest generated random number.
ganlikun 0:13413ea9a877 436 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 437 * the configuration information for RNG.
ganlikun 0:13413ea9a877 438 * @retval random value
ganlikun 0:13413ea9a877 439 */
ganlikun 0:13413ea9a877 440 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 441 {
ganlikun 0:13413ea9a877 442 return(hrng->RandomNumber);
ganlikun 0:13413ea9a877 443 }
ganlikun 0:13413ea9a877 444
ganlikun 0:13413ea9a877 445 /**
ganlikun 0:13413ea9a877 446 * @brief Data Ready callback in non-blocking mode.
ganlikun 0:13413ea9a877 447 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 448 * the configuration information for RNG.
ganlikun 0:13413ea9a877 449 * @param random32bit: generated random number.
ganlikun 0:13413ea9a877 450 * @retval None
ganlikun 0:13413ea9a877 451 */
ganlikun 0:13413ea9a877 452 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
ganlikun 0:13413ea9a877 453 {
ganlikun 0:13413ea9a877 454 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 455 UNUSED(hrng);
ganlikun 0:13413ea9a877 456 UNUSED(random32bit);
ganlikun 0:13413ea9a877 457 /* NOTE : This function should not be modified. When the callback is needed,
ganlikun 0:13413ea9a877 458 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
ganlikun 0:13413ea9a877 459 */
ganlikun 0:13413ea9a877 460 }
ganlikun 0:13413ea9a877 461
ganlikun 0:13413ea9a877 462 /**
ganlikun 0:13413ea9a877 463 * @brief RNG error callbacks.
ganlikun 0:13413ea9a877 464 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 465 * the configuration information for RNG.
ganlikun 0:13413ea9a877 466 * @retval None
ganlikun 0:13413ea9a877 467 */
ganlikun 0:13413ea9a877 468 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 469 {
ganlikun 0:13413ea9a877 470 /* Prevent unused argument(s) compilation warning */
ganlikun 0:13413ea9a877 471 UNUSED(hrng);
ganlikun 0:13413ea9a877 472 /* NOTE : This function should not be modified. When the callback is needed,
ganlikun 0:13413ea9a877 473 function HAL_RNG_ErrorCallback must be implemented in the user file.
ganlikun 0:13413ea9a877 474 */
ganlikun 0:13413ea9a877 475 }
ganlikun 0:13413ea9a877 476 /**
ganlikun 0:13413ea9a877 477 * @}
ganlikun 0:13413ea9a877 478 */
ganlikun 0:13413ea9a877 479
ganlikun 0:13413ea9a877 480
ganlikun 0:13413ea9a877 481 /** @addtogroup RNG_Exported_Functions_Group3
ganlikun 0:13413ea9a877 482 * @brief Peripheral State functions
ganlikun 0:13413ea9a877 483 *
ganlikun 0:13413ea9a877 484 @verbatim
ganlikun 0:13413ea9a877 485 ===============================================================================
ganlikun 0:13413ea9a877 486 ##### Peripheral State functions #####
ganlikun 0:13413ea9a877 487 ===============================================================================
ganlikun 0:13413ea9a877 488 [..]
ganlikun 0:13413ea9a877 489 This subsection permits to get in run-time the status of the peripheral
ganlikun 0:13413ea9a877 490 and the data flow.
ganlikun 0:13413ea9a877 491
ganlikun 0:13413ea9a877 492 @endverbatim
ganlikun 0:13413ea9a877 493 * @{
ganlikun 0:13413ea9a877 494 */
ganlikun 0:13413ea9a877 495
ganlikun 0:13413ea9a877 496 /**
ganlikun 0:13413ea9a877 497 * @brief Returns the RNG state.
ganlikun 0:13413ea9a877 498 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
ganlikun 0:13413ea9a877 499 * the configuration information for RNG.
ganlikun 0:13413ea9a877 500 * @retval HAL state
ganlikun 0:13413ea9a877 501 */
ganlikun 0:13413ea9a877 502 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
ganlikun 0:13413ea9a877 503 {
ganlikun 0:13413ea9a877 504 return hrng->State;
ganlikun 0:13413ea9a877 505 }
ganlikun 0:13413ea9a877 506
ganlikun 0:13413ea9a877 507 /**
ganlikun 0:13413ea9a877 508 * @}
ganlikun 0:13413ea9a877 509 */
ganlikun 0:13413ea9a877 510
ganlikun 0:13413ea9a877 511 /**
ganlikun 0:13413ea9a877 512 * @}
ganlikun 0:13413ea9a877 513 */
ganlikun 0:13413ea9a877 514
ganlikun 0:13413ea9a877 515 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
ganlikun 0:13413ea9a877 516 STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx || STM32F412Zx ||\
ganlikun 0:13413ea9a877 517 STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
ganlikun 0:13413ea9a877 518
ganlikun 0:13413ea9a877 519 #endif /* HAL_RNG_MODULE_ENABLED */
ganlikun 0:13413ea9a877 520
ganlikun 0:13413ea9a877 521 /**
ganlikun 0:13413ea9a877 522 * @}
ganlikun 0:13413ea9a877 523 */
ganlikun 0:13413ea9a877 524
ganlikun 0:13413ea9a877 525 /**
ganlikun 0:13413ea9a877 526 * @}
ganlikun 0:13413ea9a877 527 */
ganlikun 0:13413ea9a877 528
ganlikun 0:13413ea9a877 529 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
ganlikun 0:13413ea9a877 530