2 * Copyright © 2014 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.
21 #define AO_TRNG_SPI_BUS 1
23 static uint32_t spi_speed = AO_SPI_SPEED_4MHz;
25 #define AO_TRNG_SPI_BUF 1024
29 static uint8_t *spi_buf;
30 static uint8_t *spi_head, *spi_tail;
31 static uint8_t spi_wakeup;
40 spi_buf = ao_usb_alloc(AO_TRNG_SPI_BUF);
42 ao_spi_get(AO_TRNG_SPI_BUS, spi_speed);
45 ao_spi_recv_ring_start(spi_buf, AO_TRNG_SPI_BUF, &spi_head, &spi_tail, &spi_wakeup, AO_TRNG_SPI_BUS);
46 while (!ao_usb_out_avail) {
47 ao_arch_block_interrupts();
48 while (spi_head == spi_tail) {
50 ao_sleep(&spi_wakeup);
52 ao_arch_release_interrupts();
53 if (spi_tail > spi_head)
54 end = spi_buf + AO_TRNG_SPI_BUF;
57 this_time = end - spi_tail;
60 ao_usb_write(spi_tail, this_time);
61 spi_tail += this_time;
62 if (spi_tail == spi_buf + AO_TRNG_SPI_BUF)
65 ao_spi_put(AO_TRNG_SPI_BUS);
73 static uint8_t random[32];
76 ao_spi_get(AO_TRNG_SPI_BUS, spi_speed);
77 ao_spi_recv(random, sizeof (random), AO_TRNG_SPI_BUS);
78 ao_spi_put(AO_TRNG_SPI_BUS);
79 for (i = 0; i < sizeof (random); i++)
80 printf (" %02x", random[i]);
85 #define ADC_RING_SIZE 512
87 static uint8_t *adc_ring;
88 static uint16_t adc_head, adc_tail;
89 static uint16_t adc_wake;
91 void lpc_adc_isr(void)
97 next = (this + 1) & (ADC_RING_SIZE - 1);
98 if (next == adc_tail) {
102 adc_ring[this] = lpc_adc.dr[6] >> 8;
105 /* If there are enough entries, wake up any waiters
107 avail = (next - adc_tail) & (ADC_RING_SIZE - 1);
108 if (avail >= adc_wake) {
110 ao_wakeup(&adc_wake);
114 #define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 450000)
117 ao_trng_adc_init(void)
119 adc_ring = ao_usb_alloc(ADC_RING_SIZE);
121 lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
122 lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
124 /* Enable interrupt when AO_ADC_6 is complete */
127 lpc_nvic_set_enable(LPC_ISR_ADC_POS);
128 lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
131 ao_enable_analog(0, 11, 0);
134 ao_enable_analog(0, 12, 1);
137 ao_enable_analog(0, 13, 2);
140 ao_enable_analog(0, 14, 3);
143 ao_enable_analog(0, 15, 4);
146 ao_enable_analog(0, 16, 5);
149 ao_enable_analog(0, 22, 6);
152 ao_enable_analog(0, 23, 7);
155 lpc_adc.cr = ((1 << (LPC_ADC_CR_SEL + 6)) |
156 (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
157 (1 << LPC_ADC_CR_BURST) |
158 (LPC_ADC_CR_CLKS_9 << LPC_ADC_CR_CLKS));
162 ao_trng_adc_dump(void)
166 while (((adc_head - adc_tail) & (ADC_RING_SIZE - 1)) < 16) {
167 lpc_adc.inten = (1 << (LPC_ADC_INTEN_ADINTEN + 6));
171 printf("adc_head %d tail %d\n", adc_head, adc_tail);
173 for (i = 0; i < 16; i++) {
174 printf(" %4d", adc_ring[adc_tail]);
175 adc_tail = (adc_tail + 1) & (ADC_RING_SIZE - 1);
187 while (!ao_usb_out_avail) {
188 ao_arch_block_interrupts();
189 while (((adc_head - adc_tail) & (ADC_RING_SIZE - 1)) < 64) {
190 lpc_adc.inten = (1 << (LPC_ADC_INTEN_ADINTEN + 6));
194 ao_arch_release_interrupts();
196 this_time = ADC_RING_SIZE - adc_tail;
199 ao_usb_write(&adc_ring[adc_tail], this_time);
200 adc_tail = (adc_tail + this_time) & (ADC_RING_SIZE - 1);
210 if (ao_cmd_lex_u32 == 0 || ao_cmd_status != ao_cmd_success) {
211 ao_cmd_status = ao_cmd_success;
212 printf ("Current spi speed %d\n", spi_speed);
214 spi_speed = ao_cmd_lex_u32;
218 static const struct ao_cmds ao_trng_cmds[] = {
219 // { ao_trng_test, "R\0Dump some random numbers" },
220 { ao_trng_run, "s\0Send random bits until char" },
221 { ao_trng_speed, "S <speed>\0Set SPI speed (48MHz/speed)" },
222 { ao_trng_adc_dump, "a\0Dump ADC data" },
240 ao_led_init(LEDS_AVAILABLE);
244 ao_cmd_register(ao_trng_cmds);
246 ao_start_scheduler();