2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_adc_fast.h>
22 #include <ao_trng_send.h>
26 static struct ao_task ao_trng_send_task;
27 static uint8_t trng_running;
28 static AO_TICK_TYPE trng_power_time;
30 #define TRNG_ENABLE_DELAY AO_MS_TO_TICKS(100)
32 static uint8_t random_mutex;
36 static struct ao_task ao_trng_send_raw_task;
39 ao_trng_get_raw(uint16_t *buf)
45 t = ao_adc_get(AO_USB_IN_SIZE>>1); /* one 16-bit value per two output bytes */
46 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
49 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
51 ao_adc_ack(AO_USB_IN_SIZE>>1);
55 ao_trng_send_raw(void)
57 static uint16_t *buffer[2];
61 buffer[0] = ao_usb_alloc();
62 buffer[1] = ao_usb_alloc();
70 ao_mutex_get(&random_mutex);
74 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
75 if (delay > TRNG_ENABLE_DELAY)
76 delay = TRNG_ENABLE_DELAY;
78 /* Delay long enough for the HV power supply
79 * to stabilize so that the first bits we read
80 * aren't of poor quality
85 #ifdef AO_LED_TRNG_RAW
86 ao_led_on(AO_LED_TRNG_RAW);
88 ao_trng_get_raw(buffer[usb_buf_id]);
89 #ifdef AO_LED_TRNG_RAW
90 ao_led_off(AO_LED_TRNG_RAW);
92 ao_mutex_put(&random_mutex);
93 ao_usb_write2(buffer[usb_buf_id], AO_USB_IN_SIZE);
94 usb_buf_id = 1-usb_buf_id;
100 /* Make sure there's at least 8 bits of variance in the samples */
101 #define MIN_VARIANCE (128 * 128)
103 /* Make sure the signal is spread around a bit */
104 #define MAX_VARIANCE (512 * 512)
106 #define ADD_STATS(value) do { \
108 sum2 += (value) * (value); \
111 #define VARIANCE(n) ((sum2 - (sum / (n) * sum)) / (n))
114 ao_trng_get_cooked(uint16_t *buf)
118 uint32_t *rnd = (uint32_t *) ao_adc_ring;
119 int32_t sum, sum2, var;
122 t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
123 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
127 /* Fetch two values in one operation */
129 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
131 *buf++ = ao_crc_in_32_out_16(v);
139 ao_adc_ack(AO_USB_IN_SIZE);
140 var = VARIANCE(2 * AO_USB_IN_SIZE / sizeof (uint16_t));
141 return var >= MIN_VARIANCE && var <= MAX_VARIANCE;
144 #define AO_TRNG_START_WAIT 1024
145 #define AO_TRNG_START_CHECK 32
150 static uint16_t *buffer[2];
157 buffer[0] = ao_usb_alloc();
158 buffer[1] = ao_usb_alloc();
165 #ifdef AO_TRNG_ENABLE_PORT
166 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
168 trng_power_time = ao_time();
172 ao_delay(TRNG_ENABLE_DELAY);
174 for (s = 0; s < AO_TRNG_START_WAIT; s++) {
175 if (ao_trng_get_cooked(buffer[0]))
177 ao_delay(AO_MS_TO_TICKS(10));
180 /* Validate the hardware before enabling USB */
182 for (s = 0; s < AO_TRNG_START_CHECK; s++) {
183 if (!ao_trng_get_cooked(buffer[0])) {
185 ao_delay(AO_MS_TO_TICKS(10));
188 if (failed > AO_TRNG_START_CHECK / 4)
189 ao_panic(AO_PANIC_DMA);
192 ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
195 #ifdef AO_USB_START_DISABLED
200 ao_mutex_get(&random_mutex);
204 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
205 if (delay > TRNG_ENABLE_DELAY)
206 delay = TRNG_ENABLE_DELAY;
208 /* Delay long enough for the HV power supply
209 * to stabilize so that the first bits we read
210 * aren't of poor quality
215 #ifdef AO_LED_TRNG_COOKED
216 ao_led_on(AO_LED_TRNG_COOKED);
218 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
219 #ifdef AO_LED_TRNG_COOKED
220 ao_led_off(AO_LED_TRNG_COOKED);
222 ao_mutex_put(&random_mutex);
224 ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
225 usb_buf_id = 1-usb_buf_id;
229 ao_delay(AO_MS_TO_TICKS(10));
232 ao_panic(AO_PANIC_DMA);
238 #if AO_POWER_MANAGEMENT
240 static void ao_trng_suspend(void *arg)
243 #ifdef AO_TRNG_ENABLE_PORT
244 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
246 trng_running = FALSE;
249 static void ao_trng_resume(void *arg)
252 #ifdef AO_TRNG_ENABLE_PORT
253 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
255 trng_power_time = ao_time();
258 static struct ao_power ao_trng_power = {
259 .suspend = ao_trng_suspend,
260 .resume = ao_trng_resume
266 ao_trng_send_init(void)
268 #ifdef AO_TRNG_ENABLE_PORT
269 ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
270 ao_power_register(&ao_trng_power);
272 ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
273 ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");