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