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