- /* Delay long enough for the HV power supply to stabilize so that the
- * first bits we read aren't of poor quality
- */
- ao_delay(AO_MS_TO_TICKS(250));
+ for (s = 0; s < AO_TRNG_START_WAIT; s++) {
+ 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));
+ }
+
+ /* Validate the hardware before enabling USB */
+ failed = 0;
+ for (s = 0; s < AO_TRNG_START_CHECK; s++) {
+ if (!ao_trng_get_cooked(buffer[0])) {
+ failed++;
+ ao_delay(AO_MS_TO_TICKS(10));
+ }
+ }
+ if (failed > AO_TRNG_START_CHECK / 4)
+ ao_panic(AO_PANIC_DMA);
+
+#if AO_USB_HAS_IN2
+ ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
+#endif
+
+#ifdef AO_USB_START_DISABLED
+ ao_usb_enable();
+#endif