inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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