comparison Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c @ 160:e3ca52b8e7fa

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