RSeries astromech firmware
Enthropy.h
Go to the documentation of this file.
1 #ifndef RandomSeed_h
2 #define RandomSeed_h
3 
4 #include "ReelTwo.h"
5 #include <avr/interrupt.h>
6 #include <avr/wdt.h>
7 #include <util/atomic.h>
8 
10 
11 volatile uint32_t Enthropy_seed;
12 volatile int8_t Enthropy_iter;
13 
18 class Enthropy
19 {
20 public:
21  static uint32_t generate(int8_t iter = 32)
22  {
23  Enthropy_seed = 0;
24  Enthropy_iter = iter;
25 
26  // Enable watch dog
27  setWatchDog(true);
28 
29  while (Enthropy_iter > 0);
30 
31  // Disable watch dog
32  setWatchDog(false);
33 
34  return Enthropy_seed;
35  }
36 
37 private:
38  static void setWatchDog(bool state)
39  {
40  cli();
41  MCUSR = 0;
42  _WD_CONTROL_REG |= (1 <<_WD_CHANGE_BIT) | (state << WDE);
43  _WD_CONTROL_REG = (state << WDIE);
44  sei();
45  }
46 };
47 
48 ISR(WDT_vect)
49 {
50  Enthropy_iter--;
52  Enthropy_seed = Enthropy_seed ^ TCNT1L;
53 }
54 
55 #endif
Enthropy_seed
volatile uint32_t Enthropy_seed
Definition: Enthropy.h:11
ReelTwo.h
Enthropy_iter
volatile int8_t Enthropy_iter
Definition: Enthropy.h:12
ISR
ISR(WDT_vect)
Definition: Enthropy.h:48
Enthropy
Generate enthropy used for random seed without using analog pins.
Definition: Enthropy.h:18
Enthropy::generate
static uint32_t generate(int8_t iter=32)
Definition: Enthropy.h:21