From 0beebf5b8659014c5e8ba194e42e5dcd9be1a2a6 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 13 Aug 2011 18:27:02 -0700 Subject: [PATCH] altos-avr: Include serial/flight in companion commands This lets TeleScience log these values so the log can be matched with a suitable TeleMetrum log. Signed-off-by: Keith Packard --- src-avr/ao.h | 5 +++ src-avr/ao_cmd.c | 3 +- src-avr/ao_log_telescience.c | 12 +++---- src-avr/ao_product.c | 1 + src-avr/ao_spi_slave.c | 69 ++++++++++++++++++++++++------------ 5 files changed, 59 insertions(+), 31 deletions(-) diff --git a/src-avr/ao.h b/src-avr/ao.h index c5032d16..73725488 100644 --- a/src-avr/ao.h +++ b/src-avr/ao.h @@ -1348,6 +1348,7 @@ ao_rssi_init(uint8_t rssi_led); extern const char ao_version[]; extern const char ao_manufacturer[]; extern const char ao_product[]; +extern const int ao_serial_number; /* * Fifos @@ -1474,8 +1475,12 @@ struct ao_companion_command { uint8_t command; uint8_t flight_state; uint16_t tick; + uint16_t serial; + uint16_t flight; }; +extern struct ao_companion_command ao_companion_command; + struct ao_companion_setup { uint16_t board_id; uint16_t board_id_inverse; diff --git a/src-avr/ao_cmd.c b/src-avr/ao_cmd.c index c36b9d94..16a05472 100644 --- a/src-avr/ao_cmd.c +++ b/src-avr/ao_cmd.c @@ -217,12 +217,13 @@ ao_reboot(void) ao_panic(AO_PANIC_REBOOT); } + static void version(void) { printf("manufacturer %s\n", ao_manufacturer); printf("product %s\n", ao_product); -// printf("serial-number %u\n", ao_romconfig.serial_number); + printf("serial-number %u\n", ao_serial_number); #if HAS_LOG printf("log-format %u\n", ao_log_format); #endif diff --git a/src-avr/ao_log_telescience.c b/src-avr/ao_log_telescience.c index 18d836d0..2aa309b2 100644 --- a/src-avr/ao_log_telescience.c +++ b/src-avr/ao_log_telescience.c @@ -109,18 +109,15 @@ ao_log_stop(void) void ao_log_restart(void) { - printf("Finding end of current data...\n"); flush(); /* Find end of data */ ao_log_end_pos = ao_storage_config; for (ao_log_current_pos = 0; ao_log_current_pos < ao_storage_config; ao_log_current_pos += ao_storage_block) { - printf("reading %ld\n", ao_log_current_pos); flush(); if (!ao_log_telescience_read(ao_log_current_pos)) break; } - printf("last block is at %ld\n", ao_log_current_pos); flush(); if (ao_log_current_pos > 0) { ao_log_current_pos -= ao_storage_block; for (; ao_log_current_pos < ao_storage_config; @@ -130,7 +127,6 @@ ao_log_restart(void) break; } } - printf("Logging will start at %ld\n", ao_log_current_pos); flush(); } void @@ -149,10 +145,10 @@ ao_log_telescience(void) ao_sleep(&ao_log_running); ao_log_start_pos = ao_log_current_pos; - printf("Start logging at %ld state %d\n", - ao_log_current_pos, ao_log_store.tm_state); flush(); ao_log_store.type = AO_LOG_TELESCIENCE_START; ao_log_store.tick = ao_time(); + ao_log_store.adc[0] = ao_companion_command.serial; + ao_log_store.adc[1] = ao_companion_command.flight; ao_log_telescience_write(); /* Write the whole contents of the ring to the log * when starting up. @@ -171,8 +167,6 @@ ao_log_telescience(void) /* Wait for more ADC data to arrive */ ao_sleep((void *) &ao_adc_head); } - printf("Stop logging at %ld state %d\n", - ao_log_current_pos, ao_log_store.tm_state); flush(); memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc)); } } @@ -257,6 +251,8 @@ ao_log_query(void) printf("log data tick: %04x\n", ao_log_store.tick); printf("TM data tick: %04x\n", ao_log_store.tm_tick); printf("TM state: %d\n", ao_log_store.tm_state); + printf("TM serial: %d\n", ao_companion_command.serial); + printf("TM flight: %d\n", ao_companion_command.flight); } const struct ao_cmds ao_log_cmds[] = { diff --git a/src-avr/ao_product.c b/src-avr/ao_product.c index 5cf365d5..b78ac1aa 100644 --- a/src-avr/ao_product.c +++ b/src-avr/ao_product.c @@ -23,6 +23,7 @@ const char ao_version[] = AO_iVersion_STRING; const char ao_manufacturer[] = AO_iManufacturer_STRING; const char ao_product[] = AO_iProduct_STRING; +const int ao_serial_number = 1; #define LE_WORD(x) ((x)&0xFF),((uint8_t) (((uint16_t) (x))>>8)) diff --git a/src-avr/ao_spi_slave.c b/src-avr/ao_spi_slave.c index d44e4d0e..9ef30ead 100644 --- a/src-avr/ao_spi_slave.c +++ b/src-avr/ao_spi_slave.c @@ -18,7 +18,8 @@ #include "ao.h" #include "ao_product.h" -static struct ao_companion_command ao_companion_command; +struct ao_companion_command ao_companion_command; + static const struct ao_companion_setup ao_companion_setup = { .board_id = AO_idProduct_NUMBER, .board_id_inverse = ~AO_idProduct_NUMBER, @@ -26,29 +27,46 @@ static const struct ao_companion_setup ao_companion_setup = { .channels = NUM_ADC }; -static uint8_t ao_spi_slave_recv(void) +static uint8_t +ao_spi_read(uint8_t *buf, uint8_t len) { - uint8_t *buf; - uint8_t len; - - len = sizeof (ao_companion_command); - buf = (uint8_t *) &ao_companion_command; while (len--) { while (!(SPSR & (1 << SPIF))) if ((PINB & (1 << PINB0))) return 0; *buf++ = SPDR; } + return 1; +} + +static void +ao_spi_write(uint8_t *buf, uint8_t len) +{ + while (len--) { + SPDR = *buf++; + while (!(SPSR & (1 << SPIF))) + if ((PINB & (1 << PINB0))) + return; + } + /* Clear pending SPIF bit by reading */ + (void) SPDR; +} + +static uint8_t ao_spi_slave_recv(void) +{ + if (!ao_spi_read((uint8_t *) &ao_companion_command, + sizeof (ao_companion_command))) + return 0; /* Figure out the outbound data */ switch (ao_companion_command.command) { case AO_COMPANION_SETUP: - buf = (uint8_t *) &ao_companion_setup; - len = sizeof (ao_companion_setup); + ao_spi_write((uint8_t *) &ao_companion_setup, + sizeof (ao_companion_setup)); break; case AO_COMPANION_FETCH: - buf = (uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc; - len = NUM_ADC * sizeof (uint16_t); + ao_spi_write((uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc, + NUM_ADC * sizeof (uint16_t)); break; case AO_COMPANION_STATE: break; @@ -56,16 +74,6 @@ static uint8_t ao_spi_slave_recv(void) return 0; } - if (len) { - /* Send the outbound data */ - while (len--) { - SPDR = *buf++; - while (!(SPSR & (1 << SPIF))) - if ((PINB & (1 << PINB0))) - return 0; - } - (void) SPDR; - } ao_log_store.tm_tick = ao_companion_command.tick; if (ao_log_store.tm_state != ao_companion_command.flight_state) { ao_log_store.tm_state = ao_companion_command.flight_state; @@ -107,7 +115,24 @@ ao_spi_slave_init(void) PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 pin change */ PCICR |= (1 << PCIE0); /* Enable pin change interrupt */ - DDRB = (DDRB & 0xf0) | (1 << 3); + DDRB = ((DDRB & 0xf0) | + (1 << 3) | /* MISO, output */ + (0 << 2) | /* MOSI, input */ + (0 << 1) | /* SCK, input */ + (0 << 0)); /* SS, input */ + + /* We'd like to have a pull-up on SS so that disconnecting the + * TM would cause any SPI transaction to abort. However, when + * I tried that, SPI transactions would spontaneously abort, + * making me assume that we needed a less aggressive pull-up + * than is offered inside the AVR + */ + PORTB = ((PORTB & 0xf0) | + (1 << 3) | /* MISO, output */ + (0 << 2) | /* MOSI, no pull-up */ + (0 << 1) | /* SCK, no pull-up */ + (0 << 0)); /* SS, no pull-up */ + SPCR = (0 << SPIE) | /* Disable SPI interrupts */ (1 << SPE) | /* Enable SPI */ (0 << DORD) | /* MSB first */ -- 2.30.2