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