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, ao_trng_send_raw_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;
34 ao_trng_get_raw(uint16_t *buf)
40 t = ao_adc_get(AO_USB_IN_SIZE>>1); /* one 16-bit value per two output bytes */
41 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
44 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
46 ao_adc_ack(AO_USB_IN_SIZE>>1);
50 ao_trng_send_raw(void)
52 static uint16_t *buffer[2];
56 buffer[0] = ao_usb_alloc();
57 buffer[1] = ao_usb_alloc();
65 ao_mutex_get(&random_mutex);
69 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
70 if (delay > TRNG_ENABLE_DELAY)
71 delay = TRNG_ENABLE_DELAY;
73 /* Delay long enough for the HV power supply
74 * to stabilize so that the first bits we read
75 * aren't of poor quality
80 #ifdef AO_LED_TRNG_RAW
81 ao_led_on(AO_LED_TRNG_RAW);
83 ao_trng_get_raw(buffer[usb_buf_id]);
84 #ifdef AO_LED_TRNG_RAW
85 ao_led_off(AO_LED_TRNG_RAW);
87 ao_mutex_put(&random_mutex);
88 ao_usb_write2(buffer[usb_buf_id], AO_USB_IN_SIZE);
89 usb_buf_id = 1-usb_buf_id;
93 /* Make sure there's at least 8 bits of variance in the samples */
94 #define MIN_VARIANCE (128 * 128)
96 /* Make sure the signal is spread around a bit */
97 #define MAX_VARIANCE (512 * 512)
99 #define ADD_STATS(value) do { \
101 sum2 += (value) * (value); \
104 #define VARIANCE(n) ((sum2 - (sum / (n) * sum)) / (n))
107 ao_trng_get_cooked(uint16_t *buf)
111 uint32_t *rnd = (uint32_t *) ao_adc_ring;
112 int32_t sum, sum2, var;
115 t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
116 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
120 /* Fetch two values in one operation */
122 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
124 *buf++ = ao_crc_in_32_out_16(v);
132 ao_adc_ack(AO_USB_IN_SIZE);
133 var = VARIANCE(2 * AO_USB_IN_SIZE / sizeof (uint16_t));
134 return var >= MIN_VARIANCE && var <= MAX_VARIANCE;
137 #define AO_TRNG_START_WAIT 1024
138 #define AO_TRNG_START_CHECK 32
143 static uint16_t *buffer[2];
150 buffer[0] = ao_usb_alloc();
151 buffer[1] = ao_usb_alloc();
158 #ifdef AO_TRNG_ENABLE_PORT
159 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
161 trng_power_time = ao_time();
165 ao_delay(TRNG_ENABLE_DELAY);
167 for (s = 0; s < AO_TRNG_START_WAIT; s++) {
168 if (ao_trng_get_cooked(buffer[0]))
170 ao_delay(AO_MS_TO_TICKS(10));
173 /* Validate the hardware before enabling USB */
175 for (s = 0; s < AO_TRNG_START_CHECK; s++) {
176 if (!ao_trng_get_cooked(buffer[0])) {
178 ao_delay(AO_MS_TO_TICKS(10));
181 if (failed > AO_TRNG_START_CHECK / 4)
182 ao_panic(AO_PANIC_DMA);
184 ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
186 #ifdef AO_USB_START_DISABLED
191 ao_mutex_get(&random_mutex);
195 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
196 if (delay > TRNG_ENABLE_DELAY)
197 delay = TRNG_ENABLE_DELAY;
199 /* Delay long enough for the HV power supply
200 * to stabilize so that the first bits we read
201 * aren't of poor quality
206 #ifdef AO_LED_TRNG_COOKED
207 ao_led_on(AO_LED_TRNG_COOKED);
209 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
210 #ifdef AO_LED_TRNG_COOKED
211 ao_led_off(AO_LED_TRNG_COOKED);
213 ao_mutex_put(&random_mutex);
215 ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
216 usb_buf_id = 1-usb_buf_id;
220 ao_delay(AO_MS_TO_TICKS(10));
223 ao_panic(AO_PANIC_DMA);
229 #if AO_POWER_MANAGEMENT
231 static void ao_trng_suspend(void *arg)
234 #ifdef AO_TRNG_ENABLE_PORT
235 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
237 trng_running = FALSE;
240 static void ao_trng_resume(void *arg)
243 #ifdef AO_TRNG_ENABLE_PORT
244 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
246 trng_power_time = ao_time();
249 static struct ao_power ao_trng_power = {
250 .suspend = ao_trng_suspend,
251 .resume = ao_trng_resume
257 ao_trng_send_init(void)
259 #ifdef AO_TRNG_ENABLE_PORT
260 ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
261 ao_power_register(&ao_trng_power);
263 ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
264 ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");