altosuilib: Fill preload map on site or lat/lon change
[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;
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 #if AO_USB_HAS_IN2
34
35 static struct ao_task   ao_trng_send_raw_task;
36
37 static void
38 ao_trng_get_raw(uint16_t *buf)
39 {
40         uint16_t        i;
41         uint16_t        t;
42         uint16_t        v;
43
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++) {
46                 v = ao_adc_ring[t];
47                 *buf++ = v;
48                 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
49         }
50         ao_adc_ack(AO_USB_IN_SIZE>>1);
51 }
52
53 static void
54 ao_trng_send_raw(void)
55 {
56         static uint16_t *buffer[2];
57         int             usb_buf_id;
58
59         if (!buffer[0]) {
60                 buffer[0] = ao_usb_alloc();
61                 buffer[1] = ao_usb_alloc();
62                 if (!buffer[0])
63                         ao_exit();
64         }
65
66         usb_buf_id = 0;
67
68         for (;;) {
69                 ao_mutex_get(&random_mutex);
70                 if (!trng_running) {
71                         AO_TICK_TYPE    delay;
72
73                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
74                         if (delay > TRNG_ENABLE_DELAY)
75                                 delay = TRNG_ENABLE_DELAY;
76
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
80                          */
81                         ao_delay(delay);
82                         trng_running = TRUE;
83                 }
84 #ifdef AO_LED_TRNG_RAW
85                 ao_led_on(AO_LED_TRNG_RAW);
86 #endif
87                 ao_trng_get_raw(buffer[usb_buf_id]);
88 #ifdef AO_LED_TRNG_RAW
89                 ao_led_off(AO_LED_TRNG_RAW);
90 #endif
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;
94         }
95 }
96
97 #endif
98
99 /* Make sure there's at least 8 bits of variance in the samples */
100 #define MIN_VARIANCE            (128 * 128)
101
102 /* Make sure the signal is spread around a bit */
103 #define MAX_VARIANCE            (512 * 512)
104
105 #define ADD_STATS(value) do {                   \
106                 sum += (value);                 \
107                 sum2 += (value) * (value);      \
108         } while(0)
109
110 #define VARIANCE(n)     ((sum2 - (sum / (n) * sum)) / (n))
111
112 static int
113 ao_trng_get_cooked(uint16_t *buf)
114 {
115         uint16_t        i;
116         uint16_t        t;
117         uint32_t        *rnd = (uint32_t *) ao_adc_ring;
118         int32_t         sum, sum2, var;
119
120         sum = sum2 = 0;
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++) {
123                 uint32_t        v;
124                 uint16_t        v1, v2;
125
126                 /* Fetch two values in one operation */
127                 v = rnd[t];
128                 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
129
130                 *buf++ = ao_crc_in_32_out_16(v);
131
132                 v1 = v;
133                 v2 = v >> 16;
134
135                 ADD_STATS(v1);
136                 ADD_STATS(v2);
137         }
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;
141 }
142
143 #define AO_TRNG_START_WAIT      1024
144 #define AO_TRNG_START_CHECK     32
145
146 static void
147 ao_trng_send(void)
148 {
149         static uint16_t *buffer[2];
150         int     usb_buf_id;
151         int     good_bits;
152         int     failed;
153         int     s;
154
155         if (!buffer[0]) {
156                 buffer[0] = ao_usb_alloc();
157                 buffer[1] = ao_usb_alloc();
158                 if (!buffer[0])
159                         ao_exit();
160         }
161
162         usb_buf_id = 0;
163
164 #ifdef AO_TRNG_ENABLE_PORT
165         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
166 #endif
167         trng_power_time = ao_time();
168
169         ao_crc_reset();
170
171         ao_delay(TRNG_ENABLE_DELAY);
172
173         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
174                 if (ao_trng_get_cooked(buffer[0]))
175                         break;
176                 ao_delay(AO_MS_TO_TICKS(10));
177         }
178
179         /* Validate the hardware before enabling USB */
180         failed = 0;
181         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
182                 if (!ao_trng_get_cooked(buffer[0])) {
183                         failed++;
184                         ao_delay(AO_MS_TO_TICKS(10));
185                 }
186         }
187         if (failed > AO_TRNG_START_CHECK / 4)
188                 ao_panic(AO_PANIC_DMA);
189
190 #if AO_USB_HAS_IN2
191         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
192 #endif
193
194 #ifdef AO_USB_START_DISABLED
195         ao_usb_enable();
196 #endif
197
198         for (;;) {
199                 ao_mutex_get(&random_mutex);
200                 if (!trng_running) {
201                         AO_TICK_TYPE    delay;
202
203                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
204                         if (delay > TRNG_ENABLE_DELAY)
205                                 delay = TRNG_ENABLE_DELAY;
206
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
210                          */
211                         ao_delay(delay);
212                         trng_running = TRUE;
213                 }
214 #ifdef AO_LED_TRNG_COOKED
215                 ao_led_on(AO_LED_TRNG_COOKED);
216 #endif
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);
220 #endif
221                 ao_mutex_put(&random_mutex);
222                 if (good_bits) {
223                         ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
224                         usb_buf_id = 1-usb_buf_id;
225                         failed = 0;
226                 } else {
227                         failed++;
228                         ao_delay(AO_MS_TO_TICKS(10));
229                         if (failed > 10) {
230                                 ao_usb_disable();
231                                 ao_panic(AO_PANIC_DMA);
232                         }
233                 }
234         }
235 }
236
237 #if AO_POWER_MANAGEMENT
238
239 static void ao_trng_suspend(void *arg)
240 {
241         (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);
244 #endif
245         trng_running = FALSE;
246 }
247
248 static void ao_trng_resume(void *arg)
249 {
250         (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);
253 #endif
254         trng_power_time = ao_time();
255 }
256
257 static struct ao_power ao_trng_power = {
258         .suspend = ao_trng_suspend,
259         .resume = ao_trng_resume
260 };
261
262 #endif
263
264 void
265 ao_trng_send_init(void)
266 {
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);
270 #endif
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");
273 }