altos/chaoskey: Add another USB endpoint to read raw data
[fw/altos] / src / drivers / ao_trng_send.c
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_adc_fast.h>
20 #include <ao_crc.h>
21 #include <ao_trng_send.h>
22 #include <ao_exti.h>
23 #include <ao_power.h>
24
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;
28
29 #define TRNG_ENABLE_DELAY       AO_MS_TO_TICKS(100)
30
31 static uint8_t          random_mutex;
32
33 static void
34 ao_trng_get_raw(uint16_t *buf)
35 {
36         uint16_t        i;
37         uint16_t        t;
38         uint16_t        v;
39
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++) {
42                 v = ao_adc_ring[t];
43                 *buf++ = v;
44                 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
45         }
46         ao_adc_ack(AO_USB_IN_SIZE>>1);
47 }
48
49 static void
50 ao_trng_send_raw(void)
51 {
52         static uint16_t *buffer[2];
53         int             usb_buf_id;
54
55         if (!buffer[0]) {
56                 buffer[0] = ao_usb_alloc();
57                 buffer[1] = ao_usb_alloc();
58                 if (!buffer[0])
59                         ao_exit();
60         }
61
62         usb_buf_id = 0;
63
64         for (;;) {
65                 ao_mutex_get(&random_mutex);
66                 if (!trng_running) {
67                         AO_TICK_TYPE    delay;
68
69                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
70                         if (delay > TRNG_ENABLE_DELAY)
71                                 delay = TRNG_ENABLE_DELAY;
72
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
76                          */
77                         ao_delay(delay);
78                         trng_running = TRUE;
79                 }
80 #ifdef AO_LED_TRNG_RAW
81                 ao_led_on(AO_LED_TRNG_RAW);
82 #endif
83                 ao_trng_get_raw(buffer[usb_buf_id]);
84 #ifdef AO_LED_TRNG_RAW
85                 ao_led_off(AO_LED_TRNG_RAW);
86 #endif
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;
90         }
91 }
92
93 /* Make sure there's at least 8 bits of variance in the samples */
94 #define MIN_VARIANCE            (128 * 128)
95
96 /* Make sure the signal is spread around a bit */
97 #define MAX_VARIANCE            (512 * 512)
98
99 #define ADD_STATS(value) do {                   \
100                 sum += (value);                 \
101                 sum2 += (value) * (value);      \
102         } while(0)
103
104 #define VARIANCE(n)     ((sum2 - (sum / (n) * sum)) / (n))
105
106 static int
107 ao_trng_get_cooked(uint16_t *buf)
108 {
109         uint16_t        i;
110         uint16_t        t;
111         uint32_t        *rnd = (uint32_t *) ao_adc_ring;
112         int32_t         sum, sum2, var;
113
114         sum = sum2 = 0;
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++) {
117                 uint32_t        v;
118                 uint16_t        v1, v2;
119
120                 /* Fetch two values in one operation */
121                 v = rnd[t];
122                 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
123
124                 *buf++ = ao_crc_in_32_out_16(v);
125
126                 v1 = v;
127                 v2 = v >> 16;
128
129                 ADD_STATS(v1);
130                 ADD_STATS(v2);
131         }
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;
135 }
136
137 #define AO_TRNG_START_WAIT      1024
138 #define AO_TRNG_START_CHECK     32
139
140 static void
141 ao_trng_send(void)
142 {
143         static uint16_t *buffer[2];
144         int     usb_buf_id;
145         int     good_bits;
146         int     failed;
147         int     s;
148
149         if (!buffer[0]) {
150                 buffer[0] = ao_usb_alloc();
151                 buffer[1] = ao_usb_alloc();
152                 if (!buffer[0])
153                         ao_exit();
154         }
155
156         usb_buf_id = 0;
157
158 #ifdef AO_TRNG_ENABLE_PORT
159         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
160 #endif
161         trng_power_time = ao_time();
162
163         ao_crc_reset();
164
165         ao_delay(TRNG_ENABLE_DELAY);
166
167         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
168                 if (ao_trng_get_cooked(buffer[0]))
169                         break;
170                 ao_delay(AO_MS_TO_TICKS(10));
171         }
172
173         /* Validate the hardware before enabling USB */
174         failed = 0;
175         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
176                 if (!ao_trng_get_cooked(buffer[0])) {
177                         failed++;
178                         ao_delay(AO_MS_TO_TICKS(10));
179                 }
180         }
181         if (failed > AO_TRNG_START_CHECK / 4)
182                 ao_panic(AO_PANIC_DMA);
183
184         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
185
186 #ifdef AO_USB_START_DISABLED
187         ao_usb_enable();
188 #endif
189
190         for (;;) {
191                 ao_mutex_get(&random_mutex);
192                 if (!trng_running) {
193                         AO_TICK_TYPE    delay;
194
195                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
196                         if (delay > TRNG_ENABLE_DELAY)
197                                 delay = TRNG_ENABLE_DELAY;
198
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
202                          */
203                         ao_delay(delay);
204                         trng_running = TRUE;
205                 }
206 #ifdef AO_LED_TRNG_COOKED
207                 ao_led_on(AO_LED_TRNG_COOKED);
208 #endif
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);
212 #endif
213                 ao_mutex_put(&random_mutex);
214                 if (good_bits) {
215                         ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
216                         usb_buf_id = 1-usb_buf_id;
217                         failed = 0;
218                 } else {
219                         failed++;
220                         ao_delay(AO_MS_TO_TICKS(10));
221                         if (failed > 10) {
222                                 ao_usb_disable();
223                                 ao_panic(AO_PANIC_DMA);
224                         }
225                 }
226         }
227 }
228
229 #if AO_POWER_MANAGEMENT
230
231 static void ao_trng_suspend(void *arg)
232 {
233         (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);
236 #endif
237         trng_running = FALSE;
238 }
239
240 static void ao_trng_resume(void *arg)
241 {
242         (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);
245 #endif
246         trng_power_time = ao_time();
247 }
248
249 static struct ao_power ao_trng_power = {
250         .suspend = ao_trng_suspend,
251         .resume = ao_trng_resume
252 };
253
254 #endif
255
256 void
257 ao_trng_send_init(void)
258 {
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);
262 #endif
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");
265 }