diff --git a/src/Arduboy2.cpp b/src/Arduboy2.cpp index 71e0088..460cb36 100644 --- a/src/Arduboy2.cpp +++ b/src/Arduboy2.cpp @@ -197,25 +197,16 @@ int Arduboy2Base::cpuLoad() void Arduboy2Base::initRandomSeed() { power_adc_enable(); // ADC on - randomSeed(~rawADC(ADC_TEMP) * ~rawADC(ADC_VOLTAGE) * ~micros() + micros()); + + // do an ADC read from an unconnected input pin + ADCSRA |= _BV(ADSC); // start conversion (ADMUX has been pre-set in boot()) + while (bit_is_set(ADCSRA, ADSC)) { } // wait for conversion complete + + randomSeed(((unsigned long)ADC << 16) + micros()); + power_adc_disable(); // ADC off } -uint16_t Arduboy2Base::rawADC(uint8_t adc_bits) -{ - ADMUX = adc_bits; - // we also need MUX5 for temperature check - if (adc_bits == ADC_TEMP) { - ADCSRB = _BV(MUX5); - } - - delay(2); // Wait for ADMUX setting to settle - ADCSRA |= _BV(ADSC); // Start conversion - while (bit_is_set(ADCSRA,ADSC)); // measuring - - return ADC; -} - /* Graphics */ void Arduboy2Base::clear() diff --git a/src/Arduboy2.h b/src/Arduboy2.h index 8bf8c79..b4f908b 100644 --- a/src/Arduboy2.h +++ b/src/Arduboy2.h @@ -82,11 +82,6 @@ #define CLEAR_BUFFER true /**< Value to be passed to `display()` to clear the screen buffer. */ -// compare Vcc to 1.1 bandgap -#define ADC_VOLTAGE (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1)) -// compare temperature to 2.5 internal reference and _BV(MUX5) -#define ADC_TEMP (_BV(REFS0) | _BV(REFS1) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0)) - /** \brief * A rectangle object for collision functions. @@ -712,9 +707,6 @@ class Arduboy2Base : public Arduboy2Core */ int cpuLoad(); - // Useful for getting raw approximate voltage values. - uint16_t rawADC(uint8_t adc_bits); - /** \brief * Test if the specified buttons are pressed. * diff --git a/src/Arduboy2Core.cpp b/src/Arduboy2Core.cpp index 9955aad..471f067 100644 --- a/src/Arduboy2Core.cpp +++ b/src/Arduboy2Core.cpp @@ -80,6 +80,9 @@ void Arduboy2Core::boot() setCPUSpeed8MHz(); #endif + // Select the ADC input here so a delay isn't required in initRandomSeed() + ADMUX = RAND_SEED_IN_ADMUX; + bootPins(); bootOLED(); bootPowerSaving(); @@ -139,10 +142,12 @@ void Arduboy2Core::bootPins() // Port F INPUT_PULLUP or HIGH PORTF |= _BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) | _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT); - // Port F INPUT or LOW (none) + // Port F INPUT or LOW + PORTF &= ~(_BV(RAND_SEED_IN_BIT)); // Port F inputs DDRF &= ~(_BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) | - _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT)); + _BV(UP_BUTTON_BIT) | _BV(DOWN_BUTTON_BIT) | + _BV(RAND_SEED_IN_BIT)); // Port F outputs (none) #elif defined(AB_DEVKIT) @@ -175,9 +180,10 @@ void Arduboy2Core::bootPins() // Port F INPUT_PULLUP or HIGH PORTF |= _BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT); - // Port F INPUT or LOW (none) + // Port F INPUT or LOW + PORTF &= ~(_BV(RAND_SEED_IN_BIT)); // Port F inputs - DDRF &= ~(_BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT)); + DDRF &= ~(_BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT) | _BV(RAND_SEED_IN_BIT)); // Port F outputs (none) // Speaker: Not set here. Controlled by audio class diff --git a/src/Arduboy2Core.h b/src/Arduboy2Core.h index 17647ad..a7ac697 100644 --- a/src/Arduboy2Core.h +++ b/src/Arduboy2Core.h @@ -112,6 +112,12 @@ #define SPEAKER_2_PORT PORTC #define SPEAKER_2_DDR DDRC #define SPEAKER_2_BIT PORTC7 + +#define RAND_SEED_IN A4 // Open analog input used for noise by initRandomSeed() +#define RAND_SEED_IN_PORTF +#define RAND_SEED_IN_BIT PORTF1 +// Value for ADMUX to read the random seed pin: 2.56V reference, ADC1 +#define RAND_SEED_IN_ADMUX (_BV(REFS0) | _BV(REFS1) | _BV(MUX0)) // ----------------------- // ----- DevKit pins ----- @@ -186,6 +192,12 @@ // // Reference: https://github.com/Arduboy/Arduboy/issues/108 +#define RAND_SEED_IN A4 // Open analog input used for noise by initRandomSeed() +#define RAND_SEED_IN_PORTF +#define RAND_SEED_IN_BIT PORTF1 +// Value for ADMUX to read the random seed pin: 2.56V reference, ADC1 +#define RAND_SEED_IN_ADMUX (_BV(REFS0) | _BV(REFS1) | _BV(MUX0)) + #endif // --------------------