STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_rng.c
Go to the documentation of this file.
1 
59 /* Includes ------------------------------------------------------------------*/
60 #include "stm32f7xx_hal.h"
61 
70 #ifdef HAL_RNG_MODULE_ENABLED
71 
72 /* Private types -------------------------------------------------------------*/
73 /* Private defines -----------------------------------------------------------*/
74 /* Private variables ---------------------------------------------------------*/
75 /* Private constants ---------------------------------------------------------*/
79 #define RNG_TIMEOUT_VALUE 2
80 
83 /* Private macros ------------------------------------------------------------*/
84 /* Private functions prototypes ----------------------------------------------*/
85 /* Private functions ---------------------------------------------------------*/
86 /* Exported functions --------------------------------------------------------*/
87 
117 {
118  /* Check the RNG handle allocation */
119  if(hrng == NULL)
120  {
121  return HAL_ERROR;
122  }
123 
124  __HAL_LOCK(hrng);
125 
126  if(hrng->State == HAL_RNG_STATE_RESET)
127  {
128  /* Allocate lock resource and initialize it */
129  hrng->Lock = HAL_UNLOCKED;
130 
131  /* Init the low level hardware */
132  HAL_RNG_MspInit(hrng);
133  }
134 
135  /* Change RNG peripheral state */
136  hrng->State = HAL_RNG_STATE_BUSY;
137 
138  /* Enable the RNG Peripheral */
139  __HAL_RNG_ENABLE(hrng);
140 
141  /* Initialize the RNG state */
142  hrng->State = HAL_RNG_STATE_READY;
143 
144  __HAL_UNLOCK(hrng);
145 
146  /* Return function status */
147  return HAL_OK;
148 }
149 
157 {
158  /* Check the RNG handle allocation */
159  if(hrng == NULL)
160  {
161  return HAL_ERROR;
162  }
163  /* Disable the RNG Peripheral */
165 
166  /* Clear RNG interrupt status flags */
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 
188 __weak void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
189 {
190  /* Prevent unused argument(s) compilation warning */
191  UNUSED(hrng);
192 
193  /* NOTE : This function should not be modified. When the callback is needed,
194  function HAL_RNG_MspInit must be implemented in the user file.
195  */
196 }
197 
204 __weak void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
205 {
206  /* Prevent unused argument(s) compilation warning */
207  UNUSED(hrng);
208 
209  /* NOTE : This function should not be modified. When the callback is needed,
210  function HAL_RNG_MspDeInit must be implemented in the user file.
211  */
212 }
213 
245 {
246  uint32_t tickstart = 0;
247  HAL_StatusTypeDef status = HAL_OK;
248 
249  /* Process Locked */
250  __HAL_LOCK(hrng);
251 
252  /* Check RNG peripheral state */
253  if(hrng->State == HAL_RNG_STATE_READY)
254  {
255  /* Change RNG peripheral state */
256  hrng->State = HAL_RNG_STATE_BUSY;
257 
258  /* Get tick */
259  tickstart = HAL_GetTick();
260 
261  /* Check if data register contains valid random data */
262  while(__HAL_RNG_GET_FLAG(hrng, RNG_FLAG_DRDY) == RESET)
263  {
264  if((HAL_GetTick() - tickstart ) > RNG_TIMEOUT_VALUE)
265  {
266  hrng->State = HAL_RNG_STATE_ERROR;
267 
268  /* Process Unlocked */
269  __HAL_UNLOCK(hrng);
270 
271  return HAL_TIMEOUT;
272  }
273  }
274 
275  /* Get a 32bit Random number */
276  hrng->RandomNumber = hrng->Instance->DR;
277  *random32bit = hrng->RandomNumber;
278 
279  hrng->State = HAL_RNG_STATE_READY;
280  }
281  else
282  {
283  status = HAL_ERROR;
284  }
285 
286  /* Process Unlocked */
287  __HAL_UNLOCK(hrng);
288 
289  return status;
290 }
291 
299 {
300  HAL_StatusTypeDef status = HAL_OK;
301 
302  /* Process Locked */
303  __HAL_LOCK(hrng);
304 
305  /* Check RNG peripheral state */
306  if(hrng->State == HAL_RNG_STATE_READY)
307  {
308  /* Change RNG peripheral state */
309  hrng->State = HAL_RNG_STATE_BUSY;
310 
311  /* Process Unlocked */
312  __HAL_UNLOCK(hrng);
313 
314  /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
315  __HAL_RNG_ENABLE_IT(hrng);
316  }
317  else
318  {
319  /* Process Unlocked */
320  __HAL_UNLOCK(hrng);
321 
322  status = HAL_ERROR;
323  }
324 
325  return status;
326 }
327 
350 {
351  /* RNG clock error interrupt occurred */
352  if((__HAL_RNG_GET_IT(hrng, RNG_IT_CEI) != RESET) || (__HAL_RNG_GET_IT(hrng, RNG_IT_SEI) != RESET))
353  {
354  /* Change RNG peripheral state */
355  hrng->State = HAL_RNG_STATE_ERROR;
356 
357  HAL_RNG_ErrorCallback(hrng);
358 
359  /* Clear the clock error flag */
361 
362  }
363 
364  /* Check RNG data ready interrupt occurred */
365  if(__HAL_RNG_GET_IT(hrng, RNG_IT_DRDY) != RESET)
366  {
367  /* Generate random number once, so disable the IT */
368  __HAL_RNG_DISABLE_IT(hrng);
369 
370  /* Get the 32bit Random number (DRDY flag automatically cleared) */
371  hrng->RandomNumber = hrng->Instance->DR;
372 
373  if(hrng->State != HAL_RNG_STATE_ERROR)
374  {
375  /* Change RNG peripheral state */
376  hrng->State = HAL_RNG_STATE_READY;
377 
378  /* Data Ready callback */
380  }
381  }
382 }
383 
392 {
393  if(HAL_RNG_GenerateRandomNumber(hrng, &(hrng->RandomNumber)) == HAL_OK)
394  {
395  return hrng->RandomNumber;
396  }
397  else
398  {
399  return 0;
400  }
401 }
402 
411 {
412  uint32_t random32bit = 0;
413 
414  /* Process locked */
415  __HAL_LOCK(hrng);
416 
417  /* Change RNG peripheral state */
418  hrng->State = HAL_RNG_STATE_BUSY;
419 
420  /* Get a 32bit Random number */
421  random32bit = hrng->Instance->DR;
422 
423  /* Enable the RNG Interrupts: Data Ready, Clock error, Seed error */
424  __HAL_RNG_ENABLE_IT(hrng);
425 
426  /* Return the 32 bit random number */
427  return random32bit;
428 }
429 
437 {
438  return(hrng->RandomNumber);
439 }
440 
448 __weak void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
449 {
450  /* Prevent unused argument(s) compilation warning */
451  UNUSED(hrng);
452 
453  /* NOTE : This function should not be modified. When the callback is needed,
454  function HAL_RNG_ReadyDataCallback must be implemented in the user file.
455  */
456 }
457 
464 __weak void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
465 {
466  /* Prevent unused argument(s) compilation warning */
467  UNUSED(hrng);
468 
469  /* NOTE : This function should not be modified. When the callback is needed,
470  function HAL_RNG_ErrorCallback must be implemented in the user file.
471  */
472 }
500 {
501  return hrng->State;
502 }
503 
512 #endif /* HAL_RNG_MODULE_ENABLED */
513 
522 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define __HAL_RNG_ENABLE(__HANDLE__)
Enables the RNG peripheral.
#define CLEAR_BIT(REG, BIT)
Definition: stm32f7xx.h:180
void HAL_RNG_ErrorCallback(RNG_HandleTypeDef *hrng)
#define RNG_IT_SEI
__IO uint32_t DR
Definition: stm32f745xx.h:964
#define __HAL_UNLOCK(__HANDLE__)
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber_IT(RNG_HandleTypeDef *hrng)
HAL_RNG_StateTypeDef
#define RNG_IT_CEI
#define __HAL_LOCK(__HANDLE__)
__IO uint32_t CR
Definition: stm32f745xx.h:962
#define NULL
Definition: usbd_def.h:53
#define RNG_CR_RNGEN
Definition: stm32f745xx.h:5757
uint32_t HAL_RNG_GetRandomNumber_IT(RNG_HandleTypeDef *hrng)
#define __HAL_RNG_GET_IT(__HANDLE__, __INTERRUPT__)
Checks whether the specified RNG interrupt has occurred or not.
This file contains all the functions prototypes for the HAL module driver.
void HAL_RNG_MspDeInit(RNG_HandleTypeDef *hrng)
#define __HAL_RNG_GET_FLAG(__HANDLE__, __FLAG__)
Check the selected RNG flag status.
#define RNG_IT_DRDY
uint32_t HAL_RNG_ReadLastRandomNumber(RNG_HandleTypeDef *hrng)
__IO HAL_RNG_StateTypeDef State
HAL_StatusTypeDef HAL_RNG_Init(RNG_HandleTypeDef *hrng)
#define UNUSED(x)
#define RNG_SR_CEIS
Definition: stm32f745xx.h:5764
HAL_StatusTypeDef HAL_RNG_DeInit(RNG_HandleTypeDef *hrng)
uint32_t HAL_RNG_GetRandomNumber(RNG_HandleTypeDef *hrng)
RNG_TypeDef * Instance
#define __HAL_RNG_ENABLE_IT(__HANDLE__)
Enables the RNG interrupts.
HAL_LockTypeDef Lock
__IO uint32_t SR
Definition: stm32f745xx.h:963
#define RNG_FLAG_DRDY
#define __HAL_RNG_CLEAR_IT(__HANDLE__, __INTERRUPT__)
Clear the RNG interrupt status flags.
void HAL_RNG_ReadyDataCallback(RNG_HandleTypeDef *hrng, uint32_t random32bit)
void HAL_RNG_MspInit(RNG_HandleTypeDef *hrng)
#define RNG_SR_SEIS
Definition: stm32f745xx.h:5765
HAL_RNG_StateTypeDef HAL_RNG_GetState(RNG_HandleTypeDef *hrng)
HAL_StatusTypeDef
HAL Status structures definition.
#define __HAL_RNG_DISABLE_IT(__HANDLE__)
Disables the RNG interrupts.
uint32_t HAL_GetTick(void)
Provides a tick value in millisecond.
#define RNG_CR_IE
Definition: stm32f745xx.h:5758
void HAL_RNG_IRQHandler(RNG_HandleTypeDef *hrng)
HAL_StatusTypeDef HAL_RNG_GenerateRandomNumber(RNG_HandleTypeDef *hrng, uint32_t *random32bit)