Modified version of the mbed library for use with the Nucleo boards.

Dependents:   EEPROMWrite Full-Project

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Aug 20 10:45:13 2015 +0100
Revision:
613:bc40b8d2aec4
Parent:
532:fe11edbda85c
Synchronized with git revision 92ca8c7b60a283b6bb60eb65b183dac1599f0ade

Full URL: https://github.com/mbedmicro/mbed/commit/92ca8c7b60a283b6bb60eb65b183dac1599f0ade/

Nordic: update application start address in GCC linker script

Who changed what in which revision?

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