mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Aug 14 13:15:17 2015 +0100
Revision:
610:813dcc80987e
Parent:
573:ad23fe03a082
Synchronized with git revision 6d84db41c6833e0b9b024741eb0616a5f62d5599

Full URL: https://github.com/mbedmicro/mbed/commit/6d84db41c6833e0b9b024741eb0616a5f62d5599/

DISCO_F746NG - Improvements

Who changed what in which revision?

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