Eric Lewiston / STM32L4xx_HAL_Driver

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Committer:
EricLew
Date:
Mon Nov 02 19:37:23 2015 +0000
Revision:
0:80ee8f3b695e
Errors are with definitions of LCD and QSPI functions. I believe all .h and .c files are  uploaded, but there may need to be certain functions called.

Who changed what in which revision?

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