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 4f58fa710a
commit c962257488
4 changed files with 29 additions and 28 deletions

View File

@ -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()

View File

@ -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.
*

View File

@ -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

View File

@ -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
// --------------------