// ADC-based strong RNG // Very slow, but who cares---if you need fast random numbers, use a PRNG. #include "rng.h" #include #include #include #include "contiki.h" #ifndef RNG_CONF_USE_ADC #define RNG_CONF_USE_ADC (!RNG_CONF_USE_RADIO_CLOCK && defined(ADMUX) && defined(ADCSRA) && defined(ADCSRB) && defined(ADSC) && defined(ADEN)) #endif #ifndef RNG_CONF_USE_RADIO_CLOCK #define RNG_CONF_USE_RADIO_CLOCK ((!RNG_CONF_USE_ADC) && RF230BB) #endif /* delay_us uses floating point which includes (in some avr-gcc's) a 256 byte __clz_tab in the RAM through the .data section. */ /* _delay_loop_1 avoids this, it is 3 CPU cycles per loop, 375ns @ 8MHz */ //#define TEMPORAL_AGITATION() do { static uint8_t agitator; agitator*=97; agitator+=101; _delay_us(agitator>>1); } while (0); #define TEMPORAL_AGITATION() do { static uint8_t agitator; agitator*=97; agitator+=101; _delay_loop_1(agitator>>1); } while (0); // ------------------------------------------------------------------------- #if RNG_CONF_USE_ADC /* The hope is that there is enough noise in the LSB when pointing the ** ADC at the internal band-gap input and using the internal 2.56v ** AREF. ** ** TODO: Run some randomness tests on the output of this RNG! */ #define BITS_TO_SHIFT 9 #define ADC_CHAN_ADC1 ((0<