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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_adc_fast.h>
21 #include <ao_trng_send.h>
25 static struct ao_task ao_trng_send_task;
26 static uint8_t trng_running;
27 static AO_TICK_TYPE trng_power_time;
29 #define TRNG_ENABLE_DELAY AO_MS_TO_TICKS(100)
31 static uint8_t random_mutex;
35 static struct ao_task ao_trng_send_raw_task;
38 ao_trng_get_raw(uint16_t *buf)
44 t = ao_adc_get(AO_USB_IN_SIZE>>1); /* one 16-bit value per two output bytes */
45 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
48 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
50 ao_adc_ack(AO_USB_IN_SIZE>>1);
54 ao_trng_send_raw(void)
56 static uint16_t *buffer[2];
60 buffer[0] = ao_usb_alloc();
61 buffer[1] = ao_usb_alloc();
69 ao_mutex_get(&random_mutex);
73 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
74 if (delay > TRNG_ENABLE_DELAY)
75 delay = TRNG_ENABLE_DELAY;
77 /* Delay long enough for the HV power supply
78 * to stabilize so that the first bits we read
79 * aren't of poor quality
84 #ifdef AO_LED_TRNG_RAW
85 ao_led_on(AO_LED_TRNG_RAW);
87 ao_trng_get_raw(buffer[usb_buf_id]);
88 #ifdef AO_LED_TRNG_RAW
89 ao_led_off(AO_LED_TRNG_RAW);
91 ao_mutex_put(&random_mutex);
92 ao_usb_write2(buffer[usb_buf_id], AO_USB_IN_SIZE);
93 usb_buf_id = 1-usb_buf_id;
99 /* Make sure there's at least 8 bits of variance in the samples */
100 #define MIN_VARIANCE (128 * 128)
102 /* Make sure the signal is spread around a bit */
103 #define MAX_VARIANCE (512 * 512)
105 #define ADD_STATS(value) do { \
107 sum2 += (value) * (value); \
110 #define VARIANCE(n) ((sum2 - (sum / (n) * sum)) / (n))
113 ao_trng_get_cooked(uint16_t *buf)
117 uint32_t *rnd = (uint32_t *) ao_adc_ring;
118 int32_t sum, sum2, var;
121 t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
122 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
126 /* Fetch two values in one operation */
128 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
130 *buf++ = ao_crc_in_32_out_16(v);
138 ao_adc_ack(AO_USB_IN_SIZE);
139 var = VARIANCE(2 * AO_USB_IN_SIZE / sizeof (uint16_t));
140 return var >= MIN_VARIANCE && var <= MAX_VARIANCE;
143 #define AO_TRNG_START_WAIT 1024
144 #define AO_TRNG_START_CHECK 32
149 static uint16_t *buffer[2];
156 buffer[0] = ao_usb_alloc();
157 buffer[1] = ao_usb_alloc();
164 #ifdef AO_TRNG_ENABLE_PORT
165 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
167 trng_power_time = ao_time();
171 ao_delay(TRNG_ENABLE_DELAY);
173 for (s = 0; s < AO_TRNG_START_WAIT; s++) {
174 if (ao_trng_get_cooked(buffer[0]))
176 ao_delay(AO_MS_TO_TICKS(10));
179 /* Validate the hardware before enabling USB */
181 for (s = 0; s < AO_TRNG_START_CHECK; s++) {
182 if (!ao_trng_get_cooked(buffer[0])) {
184 ao_delay(AO_MS_TO_TICKS(10));
187 if (failed > AO_TRNG_START_CHECK / 4)
188 ao_panic(AO_PANIC_DMA);
191 ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
194 #ifdef AO_USB_START_DISABLED
199 ao_mutex_get(&random_mutex);
203 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
204 if (delay > TRNG_ENABLE_DELAY)
205 delay = TRNG_ENABLE_DELAY;
207 /* Delay long enough for the HV power supply
208 * to stabilize so that the first bits we read
209 * aren't of poor quality
214 #ifdef AO_LED_TRNG_COOKED
215 ao_led_on(AO_LED_TRNG_COOKED);
217 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
218 #ifdef AO_LED_TRNG_COOKED
219 ao_led_off(AO_LED_TRNG_COOKED);
221 ao_mutex_put(&random_mutex);
223 ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
224 usb_buf_id = 1-usb_buf_id;
228 ao_delay(AO_MS_TO_TICKS(10));
231 ao_panic(AO_PANIC_DMA);
237 #if AO_POWER_MANAGEMENT
239 static void ao_trng_suspend(void *arg)
242 #ifdef AO_TRNG_ENABLE_PORT
243 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
245 trng_running = FALSE;
248 static void ao_trng_resume(void *arg)
251 #ifdef AO_TRNG_ENABLE_PORT
252 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
254 trng_power_time = ao_time();
257 static struct ao_power ao_trng_power = {
258 .suspend = ao_trng_suspend,
259 .resume = ao_trng_resume
265 ao_trng_send_init(void)
267 #ifdef AO_TRNG_ENABLE_PORT
268 ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
269 ao_power_register(&ao_trng_power);
271 ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
272 ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");