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