summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/ao_aprs.c104
-rw-r--r--src/drivers/ao_btm.c27
-rw-r--r--src/drivers/ao_cc1120.c16
-rw-r--r--src/drivers/ao_cc1200.c26
-rw-r--r--src/drivers/ao_cc1200.h32
-rw-r--r--src/drivers/ao_cc1200_CC1200.h9
-rw-r--r--src/drivers/ao_companion.c3
-rw-r--r--src/drivers/ao_hmc5883.c4
-rw-r--r--src/drivers/ao_lco.c437
-rw-r--r--src/drivers/ao_lco.h24
-rw-r--r--src/drivers/ao_packet.c4
-rw-r--r--src/drivers/ao_packet_master.c8
-rw-r--r--src/drivers/ao_packet_slave.c2
-rw-r--r--src/drivers/ao_pad.c14
-rw-r--r--src/drivers/ao_pad.h1
-rw-r--r--src/drivers/ao_trng.c79
-rw-r--r--src/drivers/ao_trng.h24
-rw-r--r--src/drivers/ao_trng_send.c65
-rw-r--r--src/drivers/ao_trng_send.h24
19 files changed, 836 insertions, 67 deletions
diff --git a/src/drivers/ao_aprs.c b/src/drivers/ao_aprs.c
index 19beb78f..a8016673 100644
--- a/src/drivers/ao_aprs.c
+++ b/src/drivers/ao_aprs.c
@@ -707,8 +707,7 @@ static int tncPositionPacket(void)
static int32_t latitude;
static int32_t longitude;
static int32_t altitude;
- int32_t lat, lon, alt;
- uint8_t *buf;
+ uint8_t *buf;
if (ao_gps_data.flags & AO_GPS_VALID) {
latitude = ao_gps_data.latitude;
@@ -719,28 +718,99 @@ static int tncPositionPacket(void)
}
buf = tncBuffer;
- *buf++ = '!';
- /* Symbol table ID */
- *buf++ = '/';
+#ifdef AO_APRS_TEST
+#define AO_APRS_FORMAT_COMPRESSED 0
+#define AO_APRS_FORMAT_UNCOMPRESSED 1
+ switch (AO_APRS_FORMAT_COMPRESSED) {
+#else
+ switch (ao_config.aprs_format) {
+#endif
+ case AO_APRS_FORMAT_COMPRESSED:
+ default:
+ {
+ int32_t lat, lon, alt;
+
+ *buf++ = '!';
+
+ /* Symbol table ID */
+ *buf++ = '/';
+
+ lat = ((uint64_t) 380926 * (900000000 - latitude)) / 10000000;
+ lon = ((uint64_t) 190463 * (1800000000 + longitude)) / 10000000;
+
+ alt = ao_aprs_encode_altitude(altitude);
- lat = ((uint64_t) 380926 * (900000000 - latitude)) / 10000000;
- lon = ((uint64_t) 190463 * (1800000000 + longitude)) / 10000000;
+ tncCompressInt(buf, lat, 4);
+ buf += 4;
+ tncCompressInt(buf, lon, 4);
+ buf += 4;
- alt = ao_aprs_encode_altitude(altitude);
+ /* Symbol code */
+ *buf++ = '\'';
- tncCompressInt(buf, lat, 4);
- buf += 4;
- tncCompressInt(buf, lon, 4);
- buf += 4;
+ tncCompressInt(buf, alt, 2);
+ buf += 2;
- /* Symbol code */
- *buf++ = '\'';
+ *buf++ = 33 + ((1 << 5) | (2 << 3));
- tncCompressInt(buf, alt, 2);
- buf += 2;
+ break;
+ }
+ case AO_APRS_FORMAT_UNCOMPRESSED:
+ {
+ char lat_sign = 'N', lon_sign = 'E';
+ int32_t lat = latitude;
+ int32_t lon = longitude;
+ int32_t alt = altitude;
+ uint16_t lat_deg;
+ uint16_t lon_deg;
+ uint16_t lat_min;
+ uint16_t lat_frac;
+ uint16_t lon_min;
+ uint16_t lon_frac;
+
+ if (lat < 0) {
+ lat_sign = 'S';
+ lat = -lat;
+ }
- *buf++ = 33 + ((1 << 5) | (2 << 3));
+ if (lon < 0) {
+ lon_sign = 'W';
+ lon = -lon;
+ }
+
+ /* Round latitude and longitude by 0.005 minutes */
+ lat = lat + 833;
+ if (lat > 900000000)
+ lat = 900000000;
+ lon = lon + 833;
+ if (lon > 1800000000)
+ lon = 1800000000;
+
+ lat_deg = lat / 10000000;
+ lat -= lat_deg * 10000000;
+ lat *= 60;
+ lat_min = lat / 10000000;
+ lat -= lat_min * 10000000;
+ lat_frac = lat / 100000;
+
+ lon_deg = lon / 10000000;
+ lon -= lon_deg * 10000000;
+ lon *= 60;
+ lon_min = lon / 10000000;
+ lon -= lon_min * 10000000;
+ lon_frac = lon / 100000;
+
+ /* Convert from meters to feet */
+ alt = (alt * 328 + 50) / 100;
+
+ buf += sprintf((char *) tncBuffer, "!%02u%02u.%02u%c/%03u%02u.%02u%c'/A=%06u ",
+ lat_deg, lat_min, lat_frac, lat_sign,
+ lon_deg, lon_min, lon_frac, lon_sign,
+ alt);
+ break;
+ }
+ }
buf += tncComment(buf);
diff --git a/src/drivers/ao_btm.c b/src/drivers/ao_btm.c
index e6b28688..8e7052cb 100644
--- a/src/drivers/ao_btm.c
+++ b/src/drivers/ao_btm.c
@@ -23,7 +23,7 @@
#ifndef ao_serial_btm_getchar
#define ao_serial_btm_putchar ao_serial1_putchar
#define _ao_serial_btm_pollchar _ao_serial1_pollchar
-#define _ao_serial_btm_sleep() ao_sleep((void *) &ao_serial1_rx_fifo)
+#define _ao_serial_btm_sleep_for(timeout) ao_sleep_for((void *) &ao_serial1_rx_fifo, timeout)
#define ao_serial_btm_set_speed ao_serial1_set_speed
#define ao_serial_btm_drain ao_serial1_drain
#endif
@@ -111,7 +111,7 @@ ao_btm_do_echo(void)
while (ao_btm_enable) {
ao_arch_block_interrupts();
while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN && ao_btm_enable)
- _ao_serial_btm_sleep();
+ _ao_serial_btm_sleep_for(0);
ao_arch_release_interrupts();
if (c != AO_READ_AGAIN) {
putchar(c);
@@ -166,9 +166,7 @@ ao_btm_getchar(void)
ao_arch_block_interrupts();
while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
- ao_alarm(AO_MS_TO_TICKS(10));
- c = _ao_serial_btm_sleep();
- ao_clear_alarm();
+ c = _ao_serial_btm_sleep_for(AO_MS_TO_TICKS(10));
if (c) {
c = AO_READ_AGAIN;
break;
@@ -265,6 +263,15 @@ uint8_t
ao_btm_cmd(__code char *cmd)
{
ao_btm_drain();
+
+#ifdef AO_BTM_INT_PORT
+ /* Trust that AltosDroid will eventually disconnect and let us
+ * get things set up. The BTM module doesn't appear to listen
+ * for +++, so we have no way to force a disconnect.
+ */
+ while (ao_btm_connected)
+ ao_sleep(&ao_btm_connected);
+#endif
ao_btm_string(cmd);
return ao_btm_wait_reply();
}
@@ -352,6 +359,10 @@ __xdata struct ao_task ao_btm_task;
void
ao_btm(void)
{
+#ifdef AO_BTM_INT_PORT
+ ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
+#endif
+
/*
* Wait for the bluetooth device to boot
*/
@@ -382,6 +393,8 @@ ao_btm(void)
/* Turn off status reporting */
ao_btm_cmd("ATQ1\r");
+ ao_btm_drain();
+
ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
ao_serial_btm_putchar,
NULL);
@@ -390,10 +403,6 @@ ao_btm(void)
/* Check current pin state */
ao_btm_check_link();
-#ifdef AO_BTM_INT_PORT
- ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
-#endif
-
for (;;) {
while (!ao_btm_connected)
ao_sleep(&ao_btm_connected);
diff --git a/src/drivers/ao_cc1120.c b/src/drivers/ao_cc1120.c
index 90d6cc75..5b814667 100644
--- a/src/drivers/ao_cc1120.c
+++ b/src/drivers/ao_cc1120.c
@@ -837,15 +837,11 @@ ao_radio_test_cmd(void)
static void
ao_radio_wait_isr(uint16_t timeout)
{
- if (timeout)
- ao_alarm(timeout);
ao_arch_block_interrupts();
while (!ao_radio_wake && !ao_radio_mcu_wake && !ao_radio_abort)
- if (ao_sleep(&ao_radio_wake))
+ if (ao_sleep_for(&ao_radio_wake, timeout))
ao_radio_abort = 1;
ao_arch_release_interrupts();
- if (timeout)
- ao_clear_alarm();
if (ao_radio_mcu_wake)
ao_radio_check_marc_status();
}
@@ -1060,19 +1056,17 @@ ao_radio_rx_isr(void)
static uint16_t
ao_radio_rx_wait(void)
{
- ao_alarm(AO_MS_TO_TICKS(100));
ao_arch_block_interrupts();
rx_waiting = 1;
while (rx_data_cur - rx_data_consumed < AO_FEC_DECODE_BLOCK &&
!ao_radio_abort &&
!ao_radio_mcu_wake)
{
- if (ao_sleep(&ao_radio_wake))
+ if (ao_sleep_for(&ao_radio_wake, AO_MS_TO_TICKS(100)))
ao_radio_abort = 1;
}
rx_waiting = 0;
ao_arch_release_interrupts();
- ao_clear_alarm();
if (ao_radio_abort || ao_radio_mcu_wake)
return 0;
rx_data_consumed += AO_FEC_DECODE_BLOCK;
@@ -1133,19 +1127,15 @@ ao_radio_recv(__xdata void *d, uint8_t size, uint8_t timeout)
ao_radio_strobe(CC1120_SRX);
- if (timeout)
- ao_alarm(timeout);
ao_arch_block_interrupts();
while (rx_starting && !ao_radio_abort) {
- if (ao_sleep(&ao_radio_wake))
+ if (ao_sleep_for(&ao_radio_wake, timeout))
ao_radio_abort = 1;
}
uint8_t rx_task_id_save = rx_task_id;
rx_task_id = 0;
rx_starting = 0;
ao_arch_release_interrupts();
- if (timeout)
- ao_clear_alarm();
if (ao_radio_abort) {
if (rx_task_id_save == 0)
diff --git a/src/drivers/ao_cc1200.c b/src/drivers/ao_cc1200.c
index 8546900e..6547be39 100644
--- a/src/drivers/ao_cc1200.c
+++ b/src/drivers/ao_cc1200.c
@@ -41,7 +41,11 @@ int8_t ao_radio_rssi; /* Last received RSSI value */
extern const uint32_t ao_radio_cal;
+#ifdef AO_CC1200_FOSC
+#define FOSC AO_CC1200_FOSC
+#else
#define FOSC 40000000
+#endif
#define ao_radio_select() ao_spi_get_mask(AO_CC1200_SPI_CS_PORT,(1 << AO_CC1200_SPI_CS_PIN),AO_CC1200_SPI_BUS,AO_SPI_SPEED_FAST)
#define ao_radio_deselect() ao_spi_put_mask(AO_CC1200_SPI_CS_PORT,(1 << AO_CC1200_SPI_CS_PIN),AO_CC1200_SPI_BUS)
@@ -301,20 +305,28 @@ ao_radio_idle(void)
* CHANBW = 5.0 (round to 9.5)
*/
+#if FOSC == 40000000
#define PACKET_SYMBOL_RATE_M 1013008
-
#define PACKET_SYMBOL_RATE_E_384 8
+#define PACKET_SYMBOL_RATE_E_96 6
+#define PACKET_SYMBOL_RATE_E_24 4
+#endif
+
+#if FOSC == 32000000
+#define PACKET_SYMBOL_RATE_M 239914
+#define PACKET_SYMBOL_RATE_E_384 9
+#define PACKET_SYMBOL_RATE_E_96 7
+#define PACKET_SYMBOL_RATE_E_24 5
+#endif
/* 200 / 2 = 100 */
#define PACKET_CHAN_BW_384 ((CC1200_CHAN_BW_ADC_CIC_DECFACT_12 << CC1200_CHAN_BW_ADC_CIC_DECFACT) | \
(16 << CC1200_CHAN_BW_BB_CIC_DECFACT))
-#define PACKET_SYMBOL_RATE_E_96 6
/* 200 / 10 = 20 */
#define PACKET_CHAN_BW_96 ((CC1200_CHAN_BW_ADC_CIC_DECFACT_48 << CC1200_CHAN_BW_ADC_CIC_DECFACT) | \
(16 << CC1200_CHAN_BW_BB_CIC_DECFACT))
-#define PACKET_SYMBOL_RATE_E_24 4
/* 200 / 25 = 8 */
#define PACKET_CHAN_BW_24 ((CC1200_CHAN_BW_ADC_CIC_DECFACT_48 << CC1200_CHAN_BW_ADC_CIC_DECFACT) | \
(44 << CC1200_CHAN_BW_BB_CIC_DECFACT))
@@ -715,17 +727,11 @@ ao_radio_show_state(char *where)
static void
ao_radio_wait_isr(uint16_t timeout)
{
- if (timeout)
- ao_alarm(timeout);
-
ao_arch_block_interrupts();
while (!ao_radio_wake && !ao_radio_abort)
- if (ao_sleep(&ao_radio_wake))
+ if (ao_sleep_for(&ao_radio_wake, timeout))
ao_radio_abort = 1;
ao_arch_release_interrupts();
-
- if (timeout)
- ao_clear_alarm();
}
static void
diff --git a/src/drivers/ao_cc1200.h b/src/drivers/ao_cc1200.h
index b04775fd..b2b63cde 100644
--- a/src/drivers/ao_cc1200.h
+++ b/src/drivers/ao_cc1200.h
@@ -438,6 +438,38 @@
#define CC1200_IF_MIX_CFG (CC1200_EXTENDED_BIT | 0x00)
#define CC1200_FREQOFF_CFG (CC1200_EXTENDED_BIT | 0x01)
#define CC1200_TOC_CFG (CC1200_EXTENDED_BIT | 0x02)
+
+#define CC1200_TOC_CFG_TOC_LIMIT 6
+#define CC1200_TOC_CFG_TOC_LIMIT_0_2 0
+#define CC1200_TOC_CFG_TOC_LIMIT_2 1
+#define CC1200_TOC_CFG_TOC_LIMIT_12 3
+
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN 3
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_8 0
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_16 1
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_32 2
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_64 3
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_128 4
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_256 5
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_8_16 0
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_6_16 1
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_2_16 2
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_1_16 3
+#define CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_1_16_SYNC 4
+
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN 0
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_8 0
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_16 1
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_32 2
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_64 3
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_128 4
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_256 5
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_FREEZE 0
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_6_32 1
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_2_32 2
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_1_32 3
+#define CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_1_32_SYNC 4
+
#define CC1200_MARC_SPARE (CC1200_EXTENDED_BIT | 0x03)
#define CC1200_ECG_CFG (CC1200_EXTENDED_BIT | 0x04)
#define CC1200_MDMCFG2 (CC1200_EXTENDED_BIT | 0x05)
diff --git a/src/drivers/ao_cc1200_CC1200.h b/src/drivers/ao_cc1200_CC1200.h
index 35673123..f0214c2a 100644
--- a/src/drivers/ao_cc1200_CC1200.h
+++ b/src/drivers/ao_cc1200_CC1200.h
@@ -101,6 +101,15 @@
(CC1200_MDMCFG2_SYMBOL_MAP_CFG_MODE_0 << CC1200_MDMCFG2_SYMBOL_MAP_CFG) |
(CC1200_MDMCFG2_UPSAMPLER_P_8 << CC1200_MDMCFG2_UPSAMPLER_P) |
(0 << CC1200_MDMCFG2_CFM_DATA_EN)),
+ CC1200_MDMCFG0, /* General Modem Parameter Configuration Reg. 0 */
+ ((0 << CC1200_MDMCFG0_TRANSPARENT_MODE_EN) |
+ (0 << CC1200_MDMCFG0_TRANSPARENT_INTFACT) |
+ (0 << CC1200_MDMCFG0_DATA_FILTER_EN) |
+ (1 << CC1200_MDMCFG0_VITERBI_EN)),
+ CC1200_TOC_CFG, /* Timing Offset Correction Configuration */
+ ((CC1200_TOC_CFG_TOC_LIMIT_2 << CC1200_TOC_CFG_TOC_LIMIT) |
+ (CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN_6_16 << CC1200_TOC_CFG_TOC_PRE_SYNC_BLOCKLEN)|
+ (CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN_2_32 << CC1200_TOC_CFG_TOC_POST_SYNC_BLOCKLEN)),
CC1200_FREQ2, 0x6c, /* Frequency Configuration [23:16] */
CC1200_FREQ1, 0xa3, /* Frequency Configuration [15:8] */
CC1200_FREQ0, 0x33, /* Frequency Configuration [7:0] */
diff --git a/src/drivers/ao_companion.c b/src/drivers/ao_companion.c
index 570b9e40..7e02939b 100644
--- a/src/drivers/ao_companion.c
+++ b/src/drivers/ao_companion.c
@@ -102,8 +102,7 @@ ao_companion(void)
break;
}
while (ao_companion_running) {
- ao_alarm(ao_companion_setup.update_period);
- if (ao_sleep(DATA_TO_XDATA(&ao_flight_state)))
+ if (ao_sleep_for(DATA_TO_XDATA(&ao_flight_state), ao_companion_setup.update_period))
ao_companion_get_data();
else
ao_companion_notify();
diff --git a/src/drivers/ao_hmc5883.c b/src/drivers/ao_hmc5883.c
index 2d217bcf..f761671a 100644
--- a/src/drivers/ao_hmc5883.c
+++ b/src/drivers/ao_hmc5883.c
@@ -75,13 +75,11 @@ ao_hmc5883_sample(struct ao_hmc5883_sample *sample)
ao_exti_enable(AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
ao_hmc5883_reg_write(HMC5883_MODE, HMC5883_MODE_SINGLE);
- ao_alarm(AO_MS_TO_TICKS(10));
ao_arch_block_interrupts();
while (!ao_hmc5883_done)
- if (ao_sleep(&ao_hmc5883_done))
+ if (ao_sleep_for(&ao_hmc5883_done, AO_MS_TO_TICKS(10)))
++ao_hmc5883_missed_irq;
ao_arch_release_interrupts();
- ao_clear_alarm();
ao_hmc5883_read(HMC5883_X_MSB, (uint8_t *) sample, sizeof (struct ao_hmc5883_sample));
#if __BYTE_ORDER == __LITTLE_ENDIAN
diff --git a/src/drivers/ao_lco.c b/src/drivers/ao_lco.c
new file mode 100644
index 00000000..b8698a80
--- /dev/null
+++ b/src/drivers/ao_lco.c
@@ -0,0 +1,437 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao.h>
+#include <ao_lco.h>
+#include <ao_event.h>
+#include <ao_seven_segment.h>
+#include <ao_quadrature.h>
+#include <ao_lco_func.h>
+#include <ao_radio_cmac.h>
+
+#define DEBUG 1
+
+#if DEBUG
+static uint8_t ao_lco_debug;
+#define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
+#else
+#define PRINTD(...)
+#endif
+
+#define AO_LCO_PAD_DIGIT 0
+#define AO_LCO_BOX_DIGIT_1 1
+#define AO_LCO_BOX_DIGIT_10 2
+
+static uint8_t ao_lco_min_box, ao_lco_max_box;
+static uint8_t ao_lco_pad;
+static uint8_t ao_lco_box;
+static uint8_t ao_lco_armed;
+static uint8_t ao_lco_firing;
+static uint8_t ao_lco_valid;
+static uint8_t ao_lco_got_channels;
+static uint16_t ao_lco_tick_offset;
+
+static struct ao_pad_query ao_pad_query;
+
+static void
+ao_lco_set_pad(uint8_t pad)
+{
+ ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad);
+}
+
+static void
+ao_lco_set_box(uint8_t box)
+{
+ ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10);
+ ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10);
+}
+
+static void
+ao_lco_set_voltage(uint16_t decivolts)
+{
+ uint8_t tens, ones, tenths;
+
+ tenths = decivolts % 10;
+ ones = (decivolts / 10) % 10;
+ tens = (decivolts / 100) % 10;
+ ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
+ ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
+ ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
+}
+
+static void
+ao_lco_set_display(void)
+{
+ if (ao_lco_pad == 0) {
+ ao_lco_set_voltage(ao_pad_query.battery);
+ } else {
+ ao_lco_set_pad(ao_lco_pad);
+ ao_lco_set_box(ao_lco_box);
+ }
+}
+
+#define MASK_SIZE(n) (((n) + 7) >> 3)
+#define MASK_ID(n) ((n) >> 3)
+#define MASK_SHIFT(n) ((n) & 7)
+
+static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
+
+static uint8_t
+ao_lco_box_present(uint8_t box)
+{
+ if (box >= AO_PAD_MAX_BOXES)
+ return 0;
+ return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
+}
+
+static uint8_t
+ao_lco_pad_present(uint8_t pad)
+{
+ if (!ao_lco_got_channels || !ao_pad_query.channels)
+ return pad == 0;
+ /* voltage measurement is always valid */
+ if (pad == 0)
+ return 1;
+ if (pad > AO_PAD_MAX_CHANNELS)
+ return 0;
+ return (ao_pad_query.channels >> (pad - 1)) & 1;
+}
+
+static uint8_t
+ao_lco_pad_first(void)
+{
+ uint8_t pad;
+
+ for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
+ if (ao_lco_pad_present(pad))
+ return pad;
+ return 0;
+}
+
+static void
+ao_lco_input(void)
+{
+ static struct ao_event event;
+ int8_t dir, new_box, new_pad;
+
+ ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
+ for (;;) {
+ ao_event_get(&event);
+ PRINTD("event type %d unit %d value %d\n",
+ event.type, event.unit, event.value);
+ switch (event.type) {
+ case AO_EVENT_QUADRATURE:
+ switch (event.unit) {
+ case AO_QUADRATURE_PAD:
+ if (!ao_lco_armed) {
+ dir = (int8_t) event.value;
+ new_pad = ao_lco_pad;
+ do {
+ new_pad += dir;
+ if (new_pad > AO_PAD_MAX_CHANNELS)
+ new_pad = 0;
+ if (new_pad < 0)
+ new_pad = AO_PAD_MAX_CHANNELS;
+ if (new_pad == ao_lco_pad)
+ break;
+ } while (!ao_lco_pad_present(new_pad));
+ if (new_pad != ao_lco_pad) {
+ ao_lco_pad = new_pad;
+ ao_lco_set_display();
+ }
+ }
+ break;
+ case AO_QUADRATURE_BOX:
+ if (!ao_lco_armed) {
+ dir = (int8_t) event.value;
+ new_box = ao_lco_box;
+ do {
+ new_box += dir;
+ if (new_box > ao_lco_max_box)
+ new_box = ao_lco_min_box;
+ else if (new_box < ao_lco_min_box)
+ new_box = ao_lco_max_box;
+ if (new_box == ao_lco_box)
+ break;
+ } while (!ao_lco_box_present(new_box));
+ if (ao_lco_box != new_box) {
+ ao_lco_box = new_box;
+ ao_lco_pad = 1;
+ ao_lco_got_channels = 0;
+ ao_lco_set_display();
+ }
+ }
+ break;
+ }
+ break;
+ case AO_EVENT_BUTTON:
+ switch (event.unit) {
+ case AO_BUTTON_ARM:
+ ao_lco_armed = event.value;
+ PRINTD("Armed %d\n", ao_lco_armed);
+ ao_wakeup(&ao_lco_armed);
+ break;
+ case AO_BUTTON_FIRE:
+ if (ao_lco_armed) {
+ ao_lco_firing = event.value;
+ PRINTD("Firing %d\n", ao_lco_firing);
+ ao_wakeup(&ao_lco_armed);
+ }
+ break;
+ }
+ break;
+ }
+ }
+}
+
+static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
+#ifdef AO_LED_CONTINUITY_0
+ AO_LED_CONTINUITY_0,
+#endif
+#ifdef AO_LED_CONTINUITY_1
+ AO_LED_CONTINUITY_1,
+#endif
+#ifdef AO_LED_CONTINUITY_2
+ AO_LED_CONTINUITY_2,
+#endif
+#ifdef AO_LED_CONTINUITY_3
+ AO_LED_CONTINUITY_3,
+#endif
+#ifdef AO_LED_CONTINUITY_4
+ AO_LED_CONTINUITY_4,
+#endif
+#ifdef AO_LED_CONTINUITY_5
+ AO_LED_CONTINUITY_5,
+#endif
+#ifdef AO_LED_CONTINUITY_6
+ AO_LED_CONTINUITY_6,
+#endif
+#ifdef AO_LED_CONTINUITY_7
+ AO_LED_CONTINUITY_7,
+#endif
+};
+
+static void
+ao_lco_update(void)
+{
+ int8_t r;
+ uint8_t c;
+
+ r = ao_lco_query(ao_lco_box, &ao_pad_query, &ao_lco_tick_offset);
+ if (r == AO_RADIO_CMAC_OK) {
+ c = ao_lco_got_channels;
+ ao_lco_got_channels = 1;
+ ao_lco_valid = 1;
+ if (!c) {
+ if (ao_lco_pad != 0)
+ ao_lco_pad = ao_lco_pad_first();
+ ao_lco_set_display();
+ }
+ if (ao_lco_pad == 0)
+ ao_lco_set_display();
+ } else
+ ao_lco_valid = 0;
+
+#if 0
+ PRINTD("lco_query success arm_status %d i0 %d i1 %d i2 %d i3 %d\n",
+ query.arm_status,
+ query.igniter_status[0],
+ query.igniter_status[1],
+ query.igniter_status[2],
+ query.igniter_status[3]);
+#endif
+ PRINTD("ao_lco_update valid %d\n", ao_lco_valid);
+ ao_wakeup(&ao_pad_query);
+}
+
+static void
+ao_lco_box_reset_present(void)
+{
+ ao_lco_min_box = 0xff;
+ ao_lco_max_box = 0x00;
+ memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
+}
+
+static void
+ao_lco_box_set_present(uint8_t box)
+{
+ if (box < ao_lco_min_box)
+ ao_lco_min_box = box;
+ if (box > ao_lco_max_box)
+ ao_lco_max_box = box;
+ if (box >= AO_PAD_MAX_BOXES)
+ return;
+ ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
+}
+
+static void
+ao_lco_search(void)
+{
+ uint16_t tick_offset;
+ int8_t r;
+ int8_t try;
+ uint8_t box;
+ uint8_t boxes = 0;
+
+ ao_lco_box_reset_present();
+ ao_lco_set_pad(0);
+ for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
+ if ((box % 10) == 0)
+ ao_lco_set_box(box);
+ for (try = 0; try < 3; try++) {
+ tick_offset = 0;
+ r = ao_lco_query(box, &ao_pad_query, &tick_offset);
+ PRINTD("box %d result %d\n", box, r);
+ if (r == AO_RADIO_CMAC_OK) {
+ ++boxes;
+ ao_lco_box_set_present(box);
+ ao_lco_set_pad(boxes % 10);
+ ao_delay(AO_MS_TO_TICKS(30));
+ break;
+ }
+ }
+ }
+ if (ao_lco_min_box <= ao_lco_max_box)
+ ao_lco_box = ao_lco_min_box;
+ else
+ ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
+ ao_lco_valid = 0;
+ ao_lco_got_channels = 0;
+ ao_lco_pad = 1;
+ ao_lco_set_display();
+}
+
+static void
+ao_lco_igniter_status(void)
+{
+ uint8_t c;
+
+ for (;;) {
+ ao_sleep(&ao_pad_query);
+ PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid);
+ if (!ao_lco_valid) {
+ ao_led_on(AO_LED_RED);
+ ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
+ continue;
+ }
+ if (ao_radio_cmac_rssi < -90) {
+ ao_led_on(AO_LED_AMBER);
+ ao_led_off(AO_LED_RED|AO_LED_GREEN);
+ } else {
+ ao_led_on(AO_LED_GREEN);
+ ao_led_off(AO_LED_RED|AO_LED_AMBER);
+ }
+ if (ao_pad_query.arm_status)
+ ao_led_on(AO_LED_REMOTE_ARM);
+ else
+ ao_led_off(AO_LED_REMOTE_ARM);
+ for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
+ uint8_t status;
+
+ if (ao_pad_query.channels & (1 << c))
+ status = ao_pad_query.igniter_status[c];
+ else
+ status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
+ if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
+ ao_led_on(continuity_led[c]);
+ else
+ ao_led_off(continuity_led[c]);
+ }
+ }
+}
+
+static void
+ao_lco_arm_warn(void)
+{
+ for (;;) {
+ while (!ao_lco_armed)
+ ao_sleep(&ao_lco_armed);
+ ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
+ ao_delay(AO_MS_TO_TICKS(200));
+ }
+}
+
+static struct ao_task ao_lco_input_task;
+static struct ao_task ao_lco_monitor_task;
+static struct ao_task ao_lco_arm_warn_task;
+static struct ao_task ao_lco_igniter_status_task;
+
+static void
+ao_lco_monitor(void)
+{
+ uint16_t delay;
+
+ ao_lco_search();
+ ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
+ ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
+ ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
+ for (;;) {
+ PRINTD("monitor armed %d firing %d offset %d\n",
+ ao_lco_armed, ao_lco_firing, ao_lco_tick_offset);
+
+ if (ao_lco_armed && ao_lco_firing) {
+ PRINTD("Firing box %d pad %d: valid %d\n",
+ ao_lco_box, ao_lco_pad, ao_lco_valid);
+ if (!ao_lco_valid)
+ ao_lco_update();
+ if (ao_lco_valid && ao_lco_pad)
+ ao_lco_ignite(ao_lco_box, 1 << (ao_lco_pad - 1), ao_lco_tick_offset);
+ } else if (ao_lco_armed) {
+ PRINTD("Arming box %d pad %d\n",
+ ao_lco_box, ao_lco_pad);
+ if (!ao_lco_valid)
+ ao_lco_update();
+ if (ao_lco_pad) {
+ ao_lco_arm(ao_lco_box, 1 << (ao_lco_pad - 1), ao_lco_tick_offset);
+ ao_delay(AO_MS_TO_TICKS(30));
+ ao_lco_update();
+ }
+ } else {
+ ao_lco_update();
+ }
+ if (ao_lco_armed && ao_lco_firing)
+ delay = AO_MS_TO_TICKS(100);
+ else
+ delay = AO_SEC_TO_TICKS(1);
+ ao_sleep_for(&ao_lco_armed, delay);
+ }
+}
+
+#if DEBUG
+void
+ao_lco_set_debug(void)
+{
+ ao_cmd_decimal();
+ if (ao_cmd_status == ao_cmd_success)
+ ao_lco_debug = ao_cmd_lex_i != 0;
+}
+
+__code struct ao_cmds ao_lco_cmds[] = {
+ { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
+ { ao_lco_search, "s\0Search for pad boxes" },
+ { 0, NULL }
+};
+#endif
+
+void
+ao_lco_init(void)
+{
+ ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
+#if DEBUG
+ ao_cmd_register(&ao_lco_cmds[0]);
+#endif
+}
diff --git a/src/drivers/ao_lco.h b/src/drivers/ao_lco.h
new file mode 100644
index 00000000..253f9702
--- /dev/null
+++ b/src/drivers/ao_lco.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_LCO_H_
+#define _AO_LCO_H_
+
+void
+ao_lco_init(void);
+
+#endif /* _AO_LCO_H_ */
diff --git a/src/drivers/ao_packet.c b/src/drivers/ao_packet.c
index 8cdf85a9..18330ead 100644
--- a/src/drivers/ao_packet.c
+++ b/src/drivers/ao_packet.c
@@ -54,14 +54,14 @@ ao_packet_send(void)
}
uint8_t
-ao_packet_recv(void)
+ao_packet_recv(uint16_t timeout)
{
uint8_t dma_done;
#ifdef AO_LED_GREEN
ao_led_on(AO_LED_GREEN);
#endif
- dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv), 0);
+ dma_done = ao_radio_recv(&ao_rx_packet, sizeof (struct ao_packet_recv), timeout);
#ifdef AO_LED_GREEN
ao_led_off(AO_LED_GREEN);
#endif
diff --git a/src/drivers/ao_packet_master.c b/src/drivers/ao_packet_master.c
index 42a4f5bf..5e440db0 100644
--- a/src/drivers/ao_packet_master.c
+++ b/src/drivers/ao_packet_master.c
@@ -97,9 +97,7 @@ ao_packet_master(void)
if (ao_tx_packet.len)
ao_packet_master_busy();
ao_packet_master_check_busy();
- ao_alarm(AO_PACKET_MASTER_RECV_DELAY);
- r = ao_packet_recv();
- ao_clear_alarm();
+ r = ao_packet_recv(AO_PACKET_MASTER_RECV_DELAY);
if (r) {
/* if we can transmit data, do so */
if (ao_packet_tx_used && ao_tx_packet.len == 0)
@@ -107,9 +105,7 @@ ao_packet_master(void)
if (ao_rx_packet.packet.len)
ao_packet_master_busy();
ao_packet_master_sleeping = 1;
- ao_alarm(ao_packet_master_delay);
- ao_sleep(&ao_packet_master_sleeping);
- ao_clear_alarm();
+ ao_sleep_for(&ao_packet_master_sleeping, ao_packet_master_delay);
ao_packet_master_sleeping = 0;
}
}
diff --git a/src/drivers/ao_packet_slave.c b/src/drivers/ao_packet_slave.c
index e75df0d6..0872682f 100644
--- a/src/drivers/ao_packet_slave.c
+++ b/src/drivers/ao_packet_slave.c
@@ -24,7 +24,7 @@ ao_packet_slave(void)
ao_tx_packet.len = AO_PACKET_SYN;
ao_packet_restart = 1;
while (ao_packet_enable) {
- if (ao_packet_recv()) {
+ if (ao_packet_recv(0)) {
ao_xmemcpy(&ao_tx_packet.callsign, &ao_rx_packet.packet.callsign, AO_MAX_CALLSIGN);
#if HAS_FLIGHT
ao_flight_force_idle = TRUE;
diff --git a/src/drivers/ao_pad.c b/src/drivers/ao_pad.c
index dc2c83fe..ffe46c68 100644
--- a/src/drivers/ao_pad.c
+++ b/src/drivers/ao_pad.c
@@ -29,6 +29,10 @@ static __pdata uint8_t ao_pad_box;
static __xdata uint8_t ao_pad_disabled;
static __pdata uint16_t ao_pad_packet_time;
+#ifndef AO_PAD_RSSI_MINIMUM
+#define AO_PAD_RSSI_MINIMUM -90
+#endif
+
#define DEBUG 1
#if DEBUG
@@ -36,8 +40,8 @@ static __pdata uint8_t ao_pad_debug;
#define PRINTD(...) (ao_pad_debug ? (printf(__VA_ARGS__), 0) : 0)
#define FLUSHD() (ao_pad_debug ? (flush(), 0) : 0)
#else
-#define PRINTD(...)
-#define FLUSHD()
+#define PRINTD(...)
+#define FLUSHD()
#endif
static void
@@ -123,6 +127,8 @@ ao_pad_monitor(void)
#define VOLTS_TO_PYRO(x) ((int16_t) ((x) * 27.0 / 127.0 / 3.3 * 32767.0))
+ /* convert ADC value to voltage in tenths, then add .2 for the diode drop */
+ query.battery = (packet->adc.batt + 96) / 192 + 2;
cur = 0;
if (pyro > VOLTS_TO_PYRO(10)) {
query.arm_status = AO_PAD_ARM_STATUS_ARMED;
@@ -138,7 +144,7 @@ ao_pad_monitor(void)
}
if ((ao_time() - ao_pad_packet_time) > AO_SEC_TO_TICKS(2))
cur |= AO_LED_RED;
- else if (ao_radio_cmac_rssi < -90)
+ else if (ao_radio_cmac_rssi < AO_PAD_RSSI_MINIMUM)
cur |= AO_LED_AMBER;
else
cur |= AO_LED_GREEN;
@@ -255,7 +261,7 @@ ao_pad(void)
if (ret != AO_RADIO_CMAC_OK)
continue;
ao_pad_packet_time = ao_time();
-
+
ao_pad_box = ao_pad_read_box();
PRINTD ("tick %d box %d (me %d) cmd %d channels %02x\n",
diff --git a/src/drivers/ao_pad.h b/src/drivers/ao_pad.h
index 23062899..d77d105a 100644
--- a/src/drivers/ao_pad.h
+++ b/src/drivers/ao_pad.h
@@ -39,6 +39,7 @@ struct ao_pad_query {
uint8_t channels; /* which chanels are present */
uint8_t armed; /* which channels are armed */
uint8_t arm_status; /* status of arming switch */
+ uint8_t battery; /* battery voltage in decivolts */
uint8_t igniter_status[AO_PAD_MAX_CHANNELS]; /* status for each igniter */
};
diff --git a/src/drivers/ao_trng.c b/src/drivers/ao_trng.c
new file mode 100644
index 00000000..e69cd30b
--- /dev/null
+++ b/src/drivers/ao_trng.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao.h>
+#include <ao_adc_fast.h>
+#include <ao_crc.h>
+#include <ao_trng.h>
+
+static void
+ao_trng_fetch(void)
+{
+ static uint16_t *buffer[2];
+ uint32_t kbytes = 1;
+ uint32_t count;
+ int usb_buf_id;
+ uint16_t i;
+ uint16_t *buf;
+ uint16_t t;
+ uint32_t *rnd = (uint32_t *) ao_adc_ring;
+
+ if (!buffer[0]) {
+ buffer[0] = ao_usb_alloc();
+ buffer[1] = ao_usb_alloc();
+ if (!buffer[0])
+ return;
+ }
+
+ ao_cmd_decimal();
+ if (ao_cmd_status == ao_cmd_success)
+ kbytes = ao_cmd_lex_u32;
+ else
+ ao_cmd_status = ao_cmd_success;
+ usb_buf_id = 0;
+ count = kbytes * (1024/AO_USB_IN_SIZE);
+
+ ao_crc_reset();
+
+ ao_led_on(AO_LED_TRNG_READ);
+ while (count--) {
+ t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
+ buf = buffer[usb_buf_id];
+ for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
+ *buf++ = ao_crc_in_32_out_16(rnd[t]);
+ t = (t + 1) & ((AO_ADC_RING_SIZE>>1) - 1);
+ }
+ ao_adc_ack(AO_USB_IN_SIZE);
+ ao_led_toggle(AO_LED_TRNG_READ|AO_LED_TRNG_WRITE);
+ ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
+ ao_led_toggle(AO_LED_TRNG_READ|AO_LED_TRNG_WRITE);
+ usb_buf_id = 1-usb_buf_id;
+ }
+ ao_led_off(AO_LED_TRNG_READ|AO_LED_TRNG_WRITE);
+ flush();
+}
+
+static const struct ao_cmds ao_trng_cmds[] = {
+ { ao_trng_fetch, "f <kbytes>\0Fetch a block of numbers" },
+ { 0, NULL },
+};
+
+void
+ao_trng_init(void)
+{
+ ao_cmd_register(ao_trng_cmds);
+}
diff --git a/src/drivers/ao_trng.h b/src/drivers/ao_trng.h
new file mode 100644
index 00000000..78577428
--- /dev/null
+++ b/src/drivers/ao_trng.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_TRNG_H_
+#define _AO_TRNG_H_
+
+void
+ao_trng_init(void);
+
+#endif /* _AO_TRNG_H_ */
diff --git a/src/drivers/ao_trng_send.c b/src/drivers/ao_trng_send.c
new file mode 100644
index 00000000..bac6035c
--- /dev/null
+++ b/src/drivers/ao_trng_send.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao.h>
+#include <ao_adc_fast.h>
+#include <ao_crc.h>
+#include <ao_trng_send.h>
+
+static void
+ao_trng_send(void)
+{
+ static uint16_t *buffer[2];
+ int usb_buf_id;
+ uint16_t i;
+ uint16_t *buf;
+ uint16_t t;
+ uint32_t *rnd = (uint32_t *) ao_adc_ring;
+
+ if (!buffer[0]) {
+ buffer[0] = ao_usb_alloc();
+ buffer[1] = ao_usb_alloc();
+ if (!buffer[0])
+ return;
+ }
+
+ usb_buf_id = 0;
+
+ ao_crc_reset();
+
+ for (;;) {
+ ao_led_on(AO_LED_TRNG_ACTIVE);
+ t = ao_adc_get(AO_USB_IN_SIZE) >> 1; /* one 16-bit value per output byte */
+ buf = buffer[usb_buf_id];
+ for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
+ *buf++ = ao_crc_in_32_out_16(rnd[t]);
+ t = (t + 1) & ((AO_ADC_RING_SIZE>>1) - 1);
+ }
+ ao_adc_ack(AO_USB_IN_SIZE);
+ ao_led_off(AO_LED_TRNG_ACTIVE);
+ ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
+ usb_buf_id = 1-usb_buf_id;
+ }
+}
+
+static struct ao_task ao_trng_send_task;
+
+void
+ao_trng_send_init(void)
+{
+ ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
+}
diff --git a/src/drivers/ao_trng_send.h b/src/drivers/ao_trng_send.h
new file mode 100644
index 00000000..83312d59
--- /dev/null
+++ b/src/drivers/ao_trng_send.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright © 2015 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_TRNG_SEND_H_
+#define _AO_TRNG_SEND_H_
+
+void
+ao_trng_send_init(void);
+
+#endif /* _AO_TRNG_SEND_H_ */