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