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 static uint32_t previous[AO_USB_IN_SIZE / sizeof (uint16_t)];
103 ao_trng_get_cooked(uint16_t *buf)
107 uint32_t *rnd = (uint32_t *) (void *) ao_adc_ring;
108 uint8_t mismatch = 0;
110 t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
111 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
114 /* Fetch two values in one operation */
116 if (v != previous[i]) {
120 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
122 *buf++ = ao_crc_in_32_out_16(v);
124 ao_adc_ack(AO_USB_IN_SIZE);
128 #define AO_TRNG_START_WAIT 1024
129 #define AO_TRNG_START_CHECK 32
134 static uint16_t *buffer[2];
141 buffer[0] = ao_usb_alloc();
142 buffer[1] = ao_usb_alloc();
149 #ifdef AO_TRNG_ENABLE_PORT
150 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
152 trng_power_time = ao_time();
156 ao_delay(TRNG_ENABLE_DELAY);
158 for (s = 0; s < AO_TRNG_START_WAIT; s++) {
159 if (ao_trng_get_cooked(buffer[0]))
161 ao_delay(AO_MS_TO_TICKS(10));
164 /* Validate the hardware before enabling USB */
166 for (s = 0; s < AO_TRNG_START_CHECK; s++) {
167 if (!ao_trng_get_cooked(buffer[0])) {
169 ao_delay(AO_MS_TO_TICKS(10));
172 if (failed > AO_TRNG_START_CHECK / 4)
173 ao_panic(AO_PANIC_DMA);
176 ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
179 #ifdef AO_USB_START_DISABLED
184 ao_mutex_get(&random_mutex);
188 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
189 if (delay > TRNG_ENABLE_DELAY)
190 delay = TRNG_ENABLE_DELAY;
192 /* Delay long enough for the HV power supply
193 * to stabilize so that the first bits we read
194 * aren't of poor quality
199 #ifdef AO_LED_TRNG_COOKED
200 ao_led_on(AO_LED_TRNG_COOKED);
202 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
203 #ifdef AO_LED_TRNG_COOKED
204 ao_led_off(AO_LED_TRNG_COOKED);
206 ao_mutex_put(&random_mutex);
208 ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
209 usb_buf_id = 1-usb_buf_id;
213 ao_delay(AO_MS_TO_TICKS(10));
216 ao_panic(AO_PANIC_DMA);
222 #if AO_POWER_MANAGEMENT
224 static void ao_trng_suspend(void *arg)
227 #ifdef AO_TRNG_ENABLE_PORT
228 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
230 trng_running = FALSE;
233 static void ao_trng_resume(void *arg)
236 #ifdef AO_TRNG_ENABLE_PORT
237 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
239 trng_power_time = ao_time();
242 static struct ao_power ao_trng_power = {
243 .suspend = ao_trng_suspend,
244 .resume = ao_trng_resume
250 ao_trng_send_init(void)
252 #ifdef AO_TRNG_ENABLE_PORT
253 ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
254 ao_power_register(&ao_trng_power);
256 ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
257 ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");