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:
Wed Jul 01 09:45:11 2015 +0100
Revision:
579:53297373a894
Parent:
489:119543c9f674
Synchronized with git revision d5b4d2ab9c47edb4dc5776e7177b0c2263459081

Full URL: https://github.com/mbedmicro/mbed/commit/d5b4d2ab9c47edb4dc5776e7177b0c2263459081/

Initial version of drivers for SAMR21

Who changed what in which revision?

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