5 #include <avr/interrupt.h>
7 #include <util/delay.h>
10 #ifndef RNG_CONF_USE_ADC
11 #define RNG_CONF_USE_ADC (!RNG_CONF_USE_RADIO_CLOCK && defined(ADMUX) && defined(ADCSRA) && defined(ADCSRB) && defined(ADSC) && defined(ADEN))
14 #ifndef RNG_CONF_USE_RADIO_CLOCK
15 #define RNG_CONF_USE_RADIO_CLOCK ((!RNG_CONF_USE_ADC) && RF230BB)
21 #define TEMPORAL_AGITATION() do { static uint8_t agitator; agitator*=97; agitator+=101; _delay_loop_1(agitator>>1); } while (0);
33 #define BITS_TO_SHIFT 9
35 #define ADC_CHAN_ADC1 ((0<<MUX4)|(0<<MUX3)|(0<<MUX2)|(0<<MUX1)|(1<<MUX0))
36 #define ADC_CHAN_BAND_GAP ((1<<MUX4)|(1<<MUX3)|(1<<MUX2)|(1<<MUX1)|(0<<MUX0))
37 #define ADC_REF_AREF ((0<<REFS1)|(0<<REFS0))
38 #define ADC_REF_AVCC ((0<<REFS1)|(1<<REFS0))
39 #define ADC_REF_INT ((1<<REFS1)|(1<<REFS0))
40 #define ADC_TRIG_FREE_RUN ((0<<ADTS2)|(0<<ADTS1)|(0<<ADTS0))
41 #define ADC_PS_128 ((1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0))
42 #define ADC_PS_2 ((0<<ADPS2)|(0<<ADPS1)|(1<<ADPS0))
44 #ifndef CONTIKI_CONF_RNG_ADC_CHANNEL
45 #define CONTIKI_CONF_RNG_ADC_CHANNEL ADC_CHAN_BAND_GAP
48 #ifndef CONTIKI_CONF_RNG_ADC_REF
49 #define CONTIKI_CONF_RNG_ADC_REF ADC_REF_INT
53 extract_random_bit_() {
58 uint8_t adcsra = ADCSRA;
59 uint8_t admux = ADMUX;
60 uint8_t adcsrb = ADCSRB;
75 while(ADCSRA & (1<<ADSC));
78 ADCSRA = (1<<ADEN)|ADC_PS_128;
79 ADMUX = (uint8_t)CONTIKI_CONF_RNG_ADC_REF|(uint8_t)CONTIKI_CONF_RNG_ADC_CHANNEL;
80 ADCSRB = ADC_TRIG_FREE_RUN;
90 while(ADCSRA & (1<<ADSC));
97 while(ADCSRA & (1<<ADSC));
107 }
while((ret==0)||(ret==3));
127 #elif RNG_CONF_USE_RADIO_CLOCK
146 #define BITS_TO_SHIFT 8
152 #define TRX_CTRL_0 0x03
156 extract_random_bit_() {
164 TEMPORAL_AGITATION();
166 ret = !!(PIND&(1<<6));
168 ret |= !!(PIND&(1<<6));
169 }
while((ret==0)||(ret==3));
185 extract_random_bit() {
193 while(extract_random_bit_()==1);
194 while(extract_random_bit_()==0);
197 ret = extract_random_bit_();
199 ret |= extract_random_bit_();
200 }
while((ret==0)||(ret==3));
211 for(i=0;i<BITS_TO_SHIFT;i++) {
216 ret |= extract_random_bit();