chaoskey: Wait for input data to stabilize before using it
authorKeith Packard <keithp@keithp.com>
Tue, 12 Sep 2017 20:40:06 +0000 (13:40 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 12 Sep 2017 20:40:06 +0000 (13:40 -0700)
The ADC data takes a while to start working after power on; wait for
the range of input values to look reasonable before using the data.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/drivers/ao_trng_send.c

index b1227aaab8dba5a0213e50c4fd9b30ac7d1cdef9..7cda053d3ff2263d9bb7c89b55d6947a786b52d1 100644 (file)
@@ -153,10 +153,20 @@ ao_trng_send(void)
 
        ao_crc_reset();
 
-       ao_delay(TRNG_ENABLE_DELAY);
-
        for (s = 0; s < AO_TRNG_START_WAIT; s++) {
-               if (ao_trng_get_cooked(buffer[0]))
+               int i;
+               uint16_t        min, max;
+               uint16_t        buf[AO_USB_IN_SIZE>>1];
+
+               ao_trng_get_raw(buf);
+               min = max = buf[0];
+               for (i = 1; i < (AO_USB_IN_SIZE>>1); i++) {
+                       uint16_t v = buf[i];
+                       if (v < min) min = v;
+                       if (v > max) max = v;
+               }
+               /* Wait for at least 10 bits of range */
+               if ((uint16_t) (max - min) >= 1024)
                        break;
                ao_delay(AO_MS_TO_TICKS(10));
        }