ARMEBS4  revision-26.06.2015
stm32_random.c
Go to the documentation of this file.
1 /**
2  * \file heivs/stm32_random.c
3  * \see heivs/random.h
4  */
5 
6 #include "heivs/random.h"
7 #include "stm32/stm32f4xx.h"
8 #include "stm32/stm32f4xx_rcc.h"
9 #include "heivs/time.h"
10 
12 {
13  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, ENABLE);
14  RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, ENABLE);
15  RCC_AHB2PeriphResetCmd(RCC_AHB2Periph_RNG, DISABLE);
16 
17  RNG->CR |= RNG_CR_RNGEN;
18 
19  return NO_ERROR;
20 }
21 
23 {
24  RCC_AHB2PeriphClockCmd(RCC_AHB2Periph_RNG, DISABLE);
25 
26  return NO_ERROR;
27 }
28 
29 status_e random_get_nowait(uint32_t *data)
30 {
31 #if FIPS_140_2
32  static uint32_t last = 0;
33 #endif
34  uint32_t sr = RNG->SR;
35 
36  *data = 0;
37 
38  /* There is an */
39  if (sr & (RNG_SR_SECS | RNG_SR_CECS))
40  {
41  return ERROR_HW_FAILED;
42  }
43 
44  if (!sr & RNG_SR_DRDY)
45  {
46  return ERROR_AGAIN;
47  }
48 
49  *data = RNG->DR;
50 
51 #if FIPS_140_2
52  /**
53  * FIPS 140-2
54  */
55  if (last == *data)
56  {
57  *data = 0;
58  return ERROR_AGAIN;
59  }
60  last = *data;
61 #endif
62 
63  return NO_ERROR;
64 }
65 
66 status_e random_get(uint32_t *data, uint32_t timeout_ms)
67 {
68  timeout_t timeout = time_set_timeout_ms(timeout_ms);
69 
70  *data = 0;
71 
72  while (1)
73  {
74  status_e status = random_get_nowait(data);
75 
76  switch (status)
77  {
78  case NO_ERROR:
79  return status;
80  break;
81 
82  case ERROR_HW_FAILED:
83  /* status will be checked on the next loop */
84  (void)random_init();
85  break;
86 
87  case ERROR_AGAIN:
88  break;
89 
90  default:
91  return ERROR_BAD_STATE;
92  break;
93  }
94 
95  if (time_elapsed(timeout))
96  {
97  return ERROR_TIMEOUT;
98  }
99  }
100 
101  return ERROR_BAD_STATE;
102 }
103 
This file contains all the functions prototypes for the RCC firmware library.
No comment.
Definition: error.h:64
simple time abstraction
CMSIS Cortex-M4 Device Peripheral Access Layer Header File. This file contains all the peripheral reg...
static uint32_t time_elapsed(timeout_t timeout)
Is this time passed?
Definition: time.h:71
Function called at bad time.
Definition: error.h:67
status_e random_suspend(void)
Disable the HW random number generator.
Definition: stm32_random.c:22
Retry later.
Definition: error.h:49
You should check your solder/cables.
Definition: error.h:66
Timeout structure.
Definition: time.h:34
status_e random_init(void)
Initialize the HW random number generator.
Definition: stm32_random.c:11
timeout_t time_set_timeout_ms(uint32_t ms)
Set an obscure time at least ms milliseconds in the future.
Definition: time.c:15
Random number generation.
status_e
Known errors.
Definition: error.h:21
No error.
Definition: error.h:28