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;
38 ao_mutex_get(&random_mutex);
42 delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
43 if (delay > TRNG_ENABLE_DELAY)
44 delay = TRNG_ENABLE_DELAY;
46 /* Delay long enough for the HV power supply
47 * to stabilize so that the first bits we read
48 * aren't of poor quality
53 ao_mutex_put(&random_mutex);
59 static struct ao_task ao_trng_send_raw_task;
62 ao_trng_get_raw(uint16_t *buf)
68 t = ao_adc_get(AO_USB_IN_SIZE>>1); /* one 16-bit value per two output bytes */
69 for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
72 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
74 ao_adc_ack(AO_USB_IN_SIZE>>1);
78 ao_trng_send_raw(void)
83 usb_buf_id = ao_usb_alloc2(buffer);
87 #ifdef AO_LED_TRNG_RAW
88 ao_led_on(AO_LED_TRNG_RAW);
90 ao_trng_get_raw(buffer[usb_buf_id]);
91 #ifdef AO_LED_TRNG_RAW
92 ao_led_off(AO_LED_TRNG_RAW);
94 usb_buf_id = ao_usb_write2(AO_USB_IN_SIZE);
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 = 1;
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
140 usb_buf_id = ao_usb_alloc(buffer);
142 #ifdef AO_TRNG_ENABLE_PORT
143 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, 1);
145 trng_power_time = ao_time();
149 for (s = 0; s < AO_TRNG_START_WAIT; s++) {
152 uint16_t buf[AO_USB_IN_SIZE>>1];
154 ao_trng_get_raw(buf);
156 for (i = 1; i < (AO_USB_IN_SIZE>>1); i++) {
158 if (v < min) min = v;
159 if (v > max) max = v;
161 /* Wait for at least 10 bits of range */
162 if ((uint16_t) (max - min) >= 1024)
164 ao_delay(AO_MS_TO_TICKS(10));
167 /* Validate the hardware before enabling USB */
169 for (s = 0; s < AO_TRNG_START_CHECK; s++) {
170 if (!ao_trng_get_cooked(buffer[0])) {
172 ao_delay(AO_MS_TO_TICKS(10));
175 if (failed > AO_TRNG_START_CHECK / 4)
176 ao_panic(AO_PANIC_DMA);
179 ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
182 #ifdef AO_USB_START_DISABLED
188 #ifdef AO_LED_TRNG_COOKED
189 ao_led_on(AO_LED_TRNG_COOKED);
191 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
192 #ifdef AO_LED_TRNG_COOKED
193 ao_led_off(AO_LED_TRNG_COOKED);
196 usb_buf_id = ao_usb_write(AO_USB_IN_SIZE);
202 ao_panic(AO_PANIC_DMA);
208 #if AO_POWER_MANAGEMENT
210 static void ao_trng_suspend(void *arg)
213 #ifdef AO_TRNG_ENABLE_PORT
214 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, 0);
216 trng_running = false;
219 static void ao_trng_resume(void *arg)
222 #ifdef AO_TRNG_ENABLE_PORT
223 ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, 1);
225 trng_power_time = ao_time();
228 static struct ao_power ao_trng_power = {
229 .suspend = ao_trng_suspend,
230 .resume = ao_trng_resume
236 ao_trng_send_init(void)
238 #ifdef AO_TRNG_ENABLE_PORT
239 ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, 0);
240 ao_power_register(&ao_trng_power);
242 ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
243 ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");