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