Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / lib / main / STM32F4 / Drivers / STM32F4xx_HAL_Driver / Src / stm32f4xx_hal_rng.c
blob38124aab74c930601849b4eee3f357f94b4cc665
1 /**
2 ******************************************************************************
3 * @file stm32f4xx_hal_rng.c
4 * @author MCD Application Team
5 * @version V1.7.1
6 * @date 14-April-2017
7 * @brief RNG HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Random Number Generator (RNG) peripheral:
10 * + Initialization/de-initialization functions
11 * + Peripheral Control functions
12 * + Peripheral State functions
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The RNG HAL driver can be used as follows:
21 (#) Enable the RNG controller clock using __HAL_RCC_RNG_CLK_ENABLE() macro
22 in HAL_RNG_MspInit().
23 (#) Activate the RNG peripheral using HAL_RNG_Init() function.
24 (#) Wait until the 32 bit Random Number Generator contains a valid
25 random data using (polling/interrupt) mode.
26 (#) Get the 32 bit random number using HAL_RNG_GenerateRandomNumber() function.
28 @endverbatim
29 ******************************************************************************
30 * @attention
32 * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
34 * Redistribution and use in source and binary forms, with or without modification,
35 * are permitted provided that the following conditions are met:
36 * 1. Redistributions of source code must retain the above copyright notice,
37 * this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright notice,
39 * this list of conditions and the following disclaimer in the documentation
40 * and/or other materials provided with the distribution.
41 * 3. Neither the name of STMicroelectronics nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
51 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
52 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
54 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 ******************************************************************************
57 */
59 /* Includes ------------------------------------------------------------------*/
60 #include "stm32f4xx_hal.h"
62 /** @addtogroup STM32F4xx_HAL_Driver
63 * @{
66 /** @addtogroup RNG
67 * @{
70 #ifdef HAL_RNG_MODULE_ENABLED
72 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
73 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
74 defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F469xx) ||\
75 defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||\
76 defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
79 /* Private types -------------------------------------------------------------*/
80 /* Private defines -----------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private constants ---------------------------------------------------------*/
83 /** @addtogroup RNG_Private_Constants
84 * @{
86 #define RNG_TIMEOUT_VALUE 2U
87 /**
88 * @}
89 */
90 /* Private macros ------------------------------------------------------------*/
91 /* Private functions prototypes ----------------------------------------------*/
92 /* Private functions ---------------------------------------------------------*/
93 /* Exported functions --------------------------------------------------------*/
95 /** @addtogroup RNG_Exported_Functions
96 * @{
99 /** @addtogroup RNG_Exported_Functions_Group1
100 * @brief Initialization and de-initialization functions
102 @verbatim
103 ===============================================================================
104 ##### Initialization and de-initialization functions #####
105 ===============================================================================
106 [..] This section provides functions allowing to:
107 (+) Initialize the RNG according to the specified parameters
108 in the RNG_InitTypeDef and create the associated handle
109 (+) DeInitialize the RNG peripheral
110 (+) Initialize the RNG MSP
111 (+) DeInitialize RNG MSP
113 @endverbatim
114 * @{
118 * @brief Initializes the RNG peripheral and creates the associated handle.
119 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
120 * the configuration information for RNG.
121 * @retval HAL status
123 HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
125 /* Check the RNG handle allocation */
126 if(hrng == NULL)
128 return HAL_ERROR;
131 __HAL_LOCK(hrng);
133 if(hrng->State == HAL_RNG_STATE_RESET)
135 /* Allocate lock resource and initialize it */
136 hrng->Lock = HAL_UNLOCKED;
137 /* Init the low level hardware */
138 HAL_RNG_MspInit(hrng);
141 /* Change RNG peripheral state */
142 hrng->State = HAL_RNG_STATE_BUSY;
144 /* Enable the RNG Peripheral */
145 __HAL_RNG_ENABLE(hrng);
147 /* Initialize the RNG state */
148 hrng->State = HAL_RNG_STATE_READY;
150 __HAL_UNLOCK(hrng);
152 /* Return function status */
153 return HAL_OK;
157 * @brief DeInitializes the RNG peripheral.
158 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
159 * the configuration information for RNG.
160 * @retval HAL status
162 HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
164 /* Check the RNG handle allocation */
165 if(hrng == NULL)
167 return HAL_ERROR;
169 /* Disable the RNG Peripheral */
170 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
172 /* Clear RNG interrupt status flags */
173 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
175 /* DeInit the low level hardware */
176 HAL_RNG_MspDeInit(hrng);
178 /* Update the RNG state */
179 hrng->State = HAL_RNG_STATE_RESET;
181 /* Release Lock */
182 __HAL_UNLOCK(hrng);
184 /* Return the function status */
185 return HAL_OK;
189 * @brief Initializes the RNG MSP.
190 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
191 * the configuration information for RNG.
192 * @retval None
194 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
196 /* Prevent unused argument(s) compilation warning */
197 UNUSED(hrng);
198 /* NOTE : This function should not be modified. When the callback is needed,
199 function HAL_RNG_MspInit must be implemented in the user file.
204 * @brief DeInitializes the RNG MSP.
205 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
206 * the configuration information for RNG.
207 * @retval None
209 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
211 /* Prevent unused argument(s) compilation warning */
212 UNUSED(hrng);
213 /* NOTE : This function should not be modified. When the callback is needed,
214 function HAL_RNG_MspDeInit must be implemented in the user file.
219 * @}
222 /** @addtogroup RNG_Exported_Functions_Group2
223 * @brief Peripheral Control functions
225 @verbatim
226 ===============================================================================
227 ##### Peripheral Control functions #####
228 ===============================================================================
229 [..] This section provides functions allowing to:
230 (+) Get the 32 bit Random number
231 (+) Get the 32 bit Random number with interrupt enabled
232 (+) Handle RNG interrupt request
234 @endverbatim
235 * @{
239 * @brief Generates a 32-bit random number.
240 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
241 * is automatically cleared.
242 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
243 * the configuration information for RNG.
244 * @param random32bit: pointer to generated random number variable if successful.
245 * @retval HAL status
248 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
250 uint32_t tickstart = 0U;
251 HAL_StatusTypeDef status = HAL_OK;
253 /* Process Locked */
254 __HAL_LOCK(hrng);
256 /* Check RNG peripheral state */
257 if(hrng->State == HAL_RNG_STATE_READY)
259 /* Change RNG peripheral state */
260 hrng->State = HAL_RNG_STATE_BUSY;
262 /* Get tick */
263 tickstart = HAL_GetTick();
265 /* Check if data register contains valid random data */
266 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
268 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
270 hrng->State = HAL_RNG_STATE_ERROR;
272 /* Process Unlocked */
273 __HAL_UNLOCK(hrng);
275 return HAL_TIMEOUT;
279 /* Get a 32bit Random number */
280 hrng->RandomNumber = hrng->Instance->DR;
281 *random32bit = hrng->RandomNumber;
283 hrng->State = HAL_RNG_STATE_READY;
285 else
287 status = HAL_ERROR;
290 /* Process Unlocked */
291 __HAL_UNLOCK(hrng);
293 return status;
297 * @brief Generates a 32-bit random number in interrupt mode.
298 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
299 * the configuration information for RNG.
300 * @retval HAL status
302 HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
304 HAL_StatusTypeDef status = HAL_OK;
306 /* Process Locked */
307 __HAL_LOCK(hrng);
309 /* Check RNG peripheral state */
310 if(hrng->State == HAL_RNG_STATE_READY)
312 /* Change RNG peripheral state */
313 hrng->State = HAL_RNG_STATE_BUSY;
315 /* Process Unlocked */
316 __HAL_UNLOCK(hrng);
318 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
319 __HAL_RNG_ENABLE_IT(hrng);
321 else
323 /* Process Unlocked */
324 __HAL_UNLOCK(hrng);
326 status = HAL_ERROR;
329 return status;
333 * @brief Handles RNG interrupt request.
334 * @note In the case of a clock error, the RNG is no more able to generate
335 * random numbers because the PLL48CLK clock is not correct. User has
336 * to check that the clock controller is correctly configured to provide
337 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
338 * The clock error has no impact on the previously generated
339 * random numbers, and the RNG_DR register contents can be used.
340 * @note In the case of a seed error, the generation of random numbers is
341 * interrupted as long as the SECS bit is '1'. If a number is
342 * available in the RNG_DR register, it must not be used because it may
343 * not have enough entropy. In this case, it is recommended to clear the
344 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
345 * the RNG peripheral to reinitialize and restart the RNG.
346 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
347 * or CEIS are set.
348 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
349 * the configuration information for RNG.
350 * @retval None
353 void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
355 /* RNG clock error interrupt occurred */
356 if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
358 /* Change RNG peripheral state */
359 hrng->State = HAL_RNG_STATE_ERROR;
361 HAL_RNG_ErrorCallback(hrng);
363 /* Clear the clock error flag */
364 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
368 /* Check RNG data ready interrupt occurred */
369 if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
371 /* Generate random number once, so disable the IT */
372 __HAL_RNG_DISABLE_IT(hrng);
374 /* Get the 32bit Random number (DRDY flag automatically cleared) */
375 hrng->RandomNumber = hrng->Instance->DR;
377 if(hrng->State != HAL_RNG_STATE_ERROR)
379 /* Change RNG peripheral state */
380 hrng->State = HAL_RNG_STATE_READY;
382 /* Data Ready callback */
383 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
389 * @brief Returns generated random number in polling mode (Obsolete)
390 * Use HAL_RNG_GenerateRandomNumber() API instead.
391 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
392 * the configuration information for RNG.
393 * @retval Random value
395 uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
397 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
399 return hrng->RandomNumber;
401 else
403 return 0U;
408 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
409 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
410 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
411 * the configuration information for RNG.
412 * @retval 32-bit random number
414 uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
416 uint32_t random32bit = 0U;
418 /* Process locked */
419 __HAL_LOCK(hrng);
421 /* Change RNG peripheral state */
422 hrng->State = HAL_RNG_STATE_BUSY;
424 /* Get a 32bit Random number */
425 random32bit = hrng->Instance->DR;
427 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
428 __HAL_RNG_ENABLE_IT(hrng);
430 /* Return the 32 bit random number */
431 return random32bit;
435 * @brief Read latest generated random number.
436 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
437 * the configuration information for RNG.
438 * @retval random value
440 uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
442 return(hrng->RandomNumber);
446 * @brief Data Ready callback in non-blocking mode.
447 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
448 * the configuration information for RNG.
449 * @param random32bit: generated random number.
450 * @retval None
452 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
454 /* Prevent unused argument(s) compilation warning */
455 UNUSED(hrng);
456 UNUSED(random32bit);
457 /* NOTE : This function should not be modified. When the callback is needed,
458 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
463 * @brief RNG error callbacks.
464 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
465 * the configuration information for RNG.
466 * @retval None
468 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
470 /* Prevent unused argument(s) compilation warning */
471 UNUSED(hrng);
472 /* NOTE : This function should not be modified. When the callback is needed,
473 function HAL_RNG_ErrorCallback must be implemented in the user file.
477 * @}
481 /** @addtogroup RNG_Exported_Functions_Group3
482 * @brief Peripheral State functions
484 @verbatim
485 ===============================================================================
486 ##### Peripheral State functions #####
487 ===============================================================================
488 [..]
489 This subsection permits to get in run-time the status of the peripheral
490 and the data flow.
492 @endverbatim
493 * @{
497 * @brief Returns the RNG state.
498 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
499 * the configuration information for RNG.
500 * @retval HAL state
502 HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
504 return hrng->State;
508 * @}
512 * @}
515 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
516 STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx || STM32F412Zx ||\
517 STM32F412Vx || STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
519 #endif /* HAL_RNG_MODULE_ENABLED */
522 * @}
526 * @}
529 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/