Simplify initRandomSeed() to reduce code size

Use only an ADC reading from an unconnected pin shifted left 16 bits
then added to micros().
This commit is contained in:
Scott Allen 2017-03-31 17:51:00 -04:00
parent 3572a013e1
commit cfb5e89d5c
4 changed files with 29 additions and 28 deletions

View File

@ -197,25 +197,16 @@ int Arduboy2Base::cpuLoad()
void Arduboy2Base::initRandomSeed() void Arduboy2Base::initRandomSeed()
{ {
power_adc_enable(); // ADC on 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 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 */ /* Graphics */
void Arduboy2Base::clear() void Arduboy2Base::clear()

View File

@ -82,11 +82,6 @@
#define CLEAR_BUFFER true /**< Value to be passed to `display()` to clear the screen buffer. */ #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 /** \brief
* A rectangle object for collision functions. * A rectangle object for collision functions.
@ -712,9 +707,6 @@ class Arduboy2Base : public Arduboy2Core
*/ */
int cpuLoad(); int cpuLoad();
// Useful for getting raw approximate voltage values.
uint16_t rawADC(uint8_t adc_bits);
/** \brief /** \brief
* Test if the specified buttons are pressed. * Test if the specified buttons are pressed.
* *

View File

@ -80,6 +80,9 @@ void Arduboy2Core::boot()
setCPUSpeed8MHz(); setCPUSpeed8MHz();
#endif #endif
// Select the ADC input here so a delay isn't required in initRandomSeed()
ADMUX = RAND_SEED_IN_ADMUX;
bootPins(); bootPins();
bootOLED(); bootOLED();
bootPowerSaving(); bootPowerSaving();
@ -139,10 +142,12 @@ void Arduboy2Core::bootPins()
// Port F INPUT_PULLUP or HIGH // Port F INPUT_PULLUP or HIGH
PORTF |= _BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) | PORTF |= _BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) |
_BV(UP_BUTTON_BIT) | _BV(DOWN_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 // Port F inputs
DDRF &= ~(_BV(LEFT_BUTTON_BIT) | _BV(RIGHT_BUTTON_BIT) | 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) // Port F outputs (none)
#elif defined(AB_DEVKIT) #elif defined(AB_DEVKIT)
@ -175,9 +180,10 @@ void Arduboy2Core::bootPins()
// Port F INPUT_PULLUP or HIGH // Port F INPUT_PULLUP or HIGH
PORTF |= _BV(A_BUTTON_BIT) | _BV(B_BUTTON_BIT); 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 // 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) // Port F outputs (none)
// Speaker: Not set here. Controlled by audio class // Speaker: Not set here. Controlled by audio class

View File

@ -112,6 +112,12 @@
#define SPEAKER_2_PORT PORTC #define SPEAKER_2_PORT PORTC
#define SPEAKER_2_DDR DDRC #define SPEAKER_2_DDR DDRC
#define SPEAKER_2_BIT PORTC7 #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 ----- // ----- DevKit pins -----
@ -186,6 +192,12 @@
// //
// Reference: https://github.com/Arduboy/Arduboy/issues/108 // 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 #endif
// -------------------- // --------------------