source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rng.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

File size: 16.0 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_hal_rng.c
4 * @author MCD Application Team
5 * @version V1.4.1
6 * @date 09-October-2015
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
13 *
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The RNG HAL driver can be used as follows:
20
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.
27
28 @endverbatim
29 ******************************************************************************
30 * @attention
31 *
32 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
33 *
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.
44 *
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.
55 *
56 ******************************************************************************
57 */
58
59/* Includes ------------------------------------------------------------------*/
60#include "stm32f4xx_hal.h"
61
62/** @addtogroup STM32F4xx_HAL_Driver
63 * @{
64 */
65
66/** @addtogroup RNG
67 * @{
68 */
69
70#ifdef HAL_RNG_MODULE_ENABLED
71
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)
76
77
78/* Private types -------------------------------------------------------------*/
79/* Private defines -----------------------------------------------------------*/
80/* Private variables ---------------------------------------------------------*/
81/* Private constants ---------------------------------------------------------*/
82/** @addtogroup RNG_Private_Constants
83 * @{
84 */
85#define RNG_TIMEOUT_VALUE 2
86/**
87 * @}
88 */
89/* Private macros ------------------------------------------------------------*/
90/* Private functions prototypes ----------------------------------------------*/
91/* Private functions ---------------------------------------------------------*/
92/* Exported functions --------------------------------------------------------*/
93
94/** @addtogroup RNG_Exported_Functions
95 * @{
96 */
97
98/** @addtogroup RNG_Exported_Functions_Group1
99 * @brief Initialization and de-initialization functions
100 *
101@verbatim
102 ===============================================================================
103 ##### Initialization and de-initialization functions #####
104 ===============================================================================
105 [..] This section provides functions allowing to:
106 (+) Initialize the RNG according to the specified parameters
107 in the RNG_InitTypeDef and create the associated handle
108 (+) DeInitialize the RNG peripheral
109 (+) Initialize the RNG MSP
110 (+) DeInitialize RNG MSP
111
112@endverbatim
113 * @{
114 */
115
116/**
117 * @brief Initializes the RNG peripheral and creates the associated handle.
118 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
119 * the configuration information for RNG.
120 * @retval HAL status
121 */
122HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
123{
124 /* Check the RNG handle allocation */
125 if(hrng == NULL)
126 {
127 return HAL_ERROR;
128 }
129
130 __HAL_LOCK(hrng);
131
132 if(hrng->State == HAL_RNG_STATE_RESET)
133 {
134 /* Allocate lock resource and initialize it */
135 hrng->Lock = HAL_UNLOCKED;
136 /* Init the low level hardware */
137 HAL_RNG_MspInit(hrng);
138 }
139
140 /* Change RNG peripheral state */
141 hrng->State = HAL_RNG_STATE_BUSY;
142
143 /* Enable the RNG Peripheral */
144 __HAL_RNG_ENABLE(hrng);
145
146 /* Initialize the RNG state */
147 hrng->State = HAL_RNG_STATE_READY;
148
149 __HAL_UNLOCK(hrng);
150
151 /* Return function status */
152 return HAL_OK;
153}
154
155/**
156 * @brief DeInitializes the RNG peripheral.
157 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
158 * the configuration information for RNG.
159 * @retval HAL status
160 */
161HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
162{
163 /* Check the RNG handle allocation */
164 if(hrng == NULL)
165 {
166 return HAL_ERROR;
167 }
168 /* Disable the RNG Peripheral */
169 CLEAR_BIT(hrng->Instance->CR, RNG_CR_IE | RNG_CR_RNGEN);
170
171 /* Clear RNG interrupt status flags */
172 CLEAR_BIT(hrng->Instance->SR, RNG_SR_CEIS | RNG_SR_SEIS);
173
174 /* DeInit the low level hardware */
175 HAL_RNG_MspDeInit(hrng);
176
177 /* Update the RNG state */
178 hrng->State = HAL_RNG_STATE_RESET;
179
180 /* Release Lock */
181 __HAL_UNLOCK(hrng);
182
183 /* Return the function status */
184 return HAL_OK;
185}
186
187/**
188 * @brief Initializes the RNG MSP.
189 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
190 * the configuration information for RNG.
191 * @retval None
192 */
193__weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
194{
195 /* NOTE : This function should not be modified. When the callback is needed,
196 function HAL_RNG_MspInit must be implemented in the user file.
197 */
198}
199
200/**
201 * @brief DeInitializes the RNG MSP.
202 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
203 * the configuration information for RNG.
204 * @retval None
205 */
206__weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
207{
208 /* NOTE : This function should not be modified. When the callback is needed,
209 function HAL_RNG_MspDeInit must be implemented in the user file.
210 */
211}
212
213/**
214 * @}
215 */
216
217/** @addtogroup RNG_Exported_Functions_Group2
218 * @brief Peripheral Control functions
219 *
220@verbatim
221 ===============================================================================
222 ##### Peripheral Control functions #####
223 ===============================================================================
224 [..] This section provides functions allowing to:
225 (+) Get the 32 bit Random number
226 (+) Get the 32 bit Random number with interrupt enabled
227 (+) Handle RNG interrupt request
228
229@endverbatim
230 * @{
231 */
232
233/**
234 * @brief Generates a 32-bit random number.
235 * @note Each time the random number data is read the RNG_FLAG_DRDY flag
236 * is automatically cleared.
237 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
238 * the configuration information for RNG.
239 * @param random32bit: pointer to generated random number variable if successful.
240 * @retval HAL status
241 */
242
243HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)
244{
245 uint32_t tickstart = 0;
246 HAL_StatusTypeDef status = HAL_OK;
247
248 /* Process Locked */
249 __HAL_LOCK(hrng);
250
251 /* Check RNG peripheral state */
252 if(hrng->State == HAL_RNG_STATE_READY)
253 {
254 /* Change RNG peripheral state */
255 hrng->State = HAL_RNG_STATE_BUSY;
256
257 /* Get tick */
258 tickstart = HAL_GetTick();
259
260 /* Check if data register contains valid random data */
261 while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
262 {
263 if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
264 {
265 hrng->State = HAL_RNG_STATE_ERROR;
266
267 /* Process Unlocked */
268 __HAL_UNLOCK(hrng);
269
270 return HAL_TIMEOUT;
271 }
272 }
273
274 /* Get a 32bit Random number */
275 hrng->RandomNumber = hrng->Instance->DR;
276 *random32bit = hrng->RandomNumber;
277
278 hrng->State = HAL_RNG_STATE_READY;
279 }
280 else
281 {
282 status = HAL_ERROR;
283 }
284
285 /* Process Unlocked */
286 __HAL_UNLOCK(hrng);
287
288 return status;
289}
290
291/**
292 * @brief Generates a 32-bit random number in interrupt mode.
293 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
294 * the configuration information for RNG.
295 * @retval HAL status
296 */
297HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
298{
299 HAL_StatusTypeDef status = HAL_OK;
300
301 /* Process Locked */
302 __HAL_LOCK(hrng);
303
304 /* Check RNG peripheral state */
305 if(hrng->State == HAL_RNG_STATE_READY)
306 {
307 /* Change RNG peripheral state */
308 hrng->State = HAL_RNG_STATE_BUSY;
309
310 /* Process Unlocked */
311 __HAL_UNLOCK(hrng);
312
313 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
314 __HAL_RNG_ENABLE_IT(hrng);
315 }
316 else
317 {
318 /* Process Unlocked */
319 __HAL_UNLOCK(hrng);
320
321 status = HAL_ERROR;
322 }
323
324 return status;
325}
326
327/**
328 * @brief Handles RNG interrupt request.
329 * @note In the case of a clock error, the RNG is no more able to generate
330 * random numbers because the PLL48CLK clock is not correct. User has
331 * to check that the clock controller is correctly configured to provide
332 * the RNG clock and clear the CEIS bit using __HAL_RNG_CLEAR_IT().
333 * The clock error has no impact on the previously generated
334 * random numbers, and the RNG_DR register contents can be used.
335 * @note In the case of a seed error, the generation of random numbers is
336 * interrupted as long as the SECS bit is '1'. If a number is
337 * available in the RNG_DR register, it must not be used because it may
338 * not have enough entropy. In this case, it is recommended to clear the
339 * SEIS bit using __HAL_RNG_CLEAR_IT(), then disable and enable
340 * the RNG peripheral to reinitialize and restart the RNG.
341 * @note User-written HAL_RNG_ErrorCallback() API is called once whether SEIS
342 * or CEIS are set.
343 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
344 * the configuration information for RNG.
345 * @retval None
346
347 */
348void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
349{
350 /* RNG clock error interrupt occurred */
351 if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
352 {
353 /* Change RNG peripheral state */
354 hrng->State = HAL_RNG_STATE_ERROR;
355
356 HAL_RNG_ErrorCallback(hrng);
357
358 /* Clear the clock error flag */
359 __HAL_RNG_CLEAR_IT(hrng, RNG_IT_CEI|RNG_IT_SEI);
360
361 }
362
363 /* Check RNG data ready interrupt occurred */
364 if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
365 {
366 /* Generate random number once, so disable the IT */
367 __HAL_RNG_DISABLE_IT(hrng);
368
369 /* Get the 32bit Random number (DRDY flag automatically cleared) */
370 hrng->RandomNumber = hrng->Instance->DR;
371
372 if(hrng->State != HAL_RNG_STATE_ERROR)
373 {
374 /* Change RNG peripheral state */
375 hrng->State = HAL_RNG_STATE_READY;
376
377 /* Data Ready callback */
378 HAL_RNG_ReadyDataCallback(hrng, hrng->RandomNumber);
379 }
380 }
381}
382
383/**
384 * @brief Returns generated random number in polling mode (Obsolete)
385 * Use HAL_RNG_GenerateRandomNumber() API instead.
386 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
387 * the configuration information for RNG.
388 * @retval Random value
389 */
390uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
391{
392 if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
393 {
394 return hrng->RandomNumber;
395 }
396 else
397 {
398 return 0;
399 }
400}
401
402/**
403 * @brief Returns a 32-bit random number with interrupt enabled (Obsolete),
404 * Use HAL_RNG_GenerateRandomNumber_IT() API instead.
405 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
406 * the configuration information for RNG.
407 * @retval 32-bit random number
408 */
409uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
410{
411 uint32_t random32bit = 0;
412
413 /* Process locked */
414 __HAL_LOCK(hrng);
415
416 /* Change RNG peripheral state */
417 hrng->State = HAL_RNG_STATE_BUSY;
418
419 /* Get a 32bit Random number */
420 random32bit = hrng->Instance->DR;
421
422 /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
423 __HAL_RNG_ENABLE_IT(hrng);
424
425 /* Return the 32 bit random number */
426 return random32bit;
427}
428
429/**
430 * @brief Read latest generated random number.
431 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
432 * the configuration information for RNG.
433 * @retval random value
434 */
435uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
436{
437 return(hrng->RandomNumber);
438}
439
440/**
441 * @brief Data Ready callback in non-blocking mode.
442 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
443 * the configuration information for RNG.
444 * @param random32bit: generated random number.
445 * @retval None
446 */
447__weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
448{
449 /* NOTE : This function should not be modified. When the callback is needed,
450 function HAL_RNG_ReadyDataCallback must be implemented in the user file.
451 */
452}
453
454/**
455 * @brief RNG error callbacks.
456 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
457 * the configuration information for RNG.
458 * @retval None
459 */
460__weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
461{
462 /* NOTE : This function should not be modified. When the callback is needed,
463 function HAL_RNG_ErrorCallback must be implemented in the user file.
464 */
465}
466/**
467 * @}
468 */
469
470
471/** @addtogroup RNG_Exported_Functions_Group3
472 * @brief Peripheral State functions
473 *
474@verbatim
475 ===============================================================================
476 ##### Peripheral State functions #####
477 ===============================================================================
478 [..]
479 This subsection permits to get in run-time the status of the peripheral
480 and the data flow.
481
482@endverbatim
483 * @{
484 */
485
486/**
487 * @brief Returns the RNG state.
488 * @param hrng: pointer to a RNG_HandleTypeDef structure that contains
489 * the configuration information for RNG.
490 * @retval HAL state
491 */
492HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
493{
494 return hrng->State;
495}
496
497/**
498 * @}
499 */
500
501/**
502 * @}
503 */
504
505#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx ||\
506 STM32F429xx || STM32F439xx || STM32F410xx || STM32F469xx || STM32F479xx */
507
508#endif /* HAL_RNG_MODULE_ENABLED */
509
510/**
511 * @}
512 */
513
514/**
515 * @}
516 */
517
518/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.