altos-avr: Include serial/flight in companion commands
authorKeith Packard <keithp@keithp.com>
Sun, 14 Aug 2011 01:27:02 +0000 (18:27 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 14 Aug 2011 01:27:02 +0000 (18:27 -0700)
This lets TeleScience log these values so the log can be matched with
a suitable TeleMetrum log.

Signed-off-by: Keith Packard <keithp@keithp.com>
src-avr/ao.h
src-avr/ao_cmd.c
src-avr/ao_log_telescience.c
src-avr/ao_product.c
src-avr/ao_spi_slave.c

index c5032d16fe8ad5fc51d092a8b6ad5c233320c3e4..73725488fb699ac100ec6a131ae3df38eb67c56a 100644 (file)
@@ -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;
index c36b9d944aded56f30fd97b5d89ec9e47dfe3c20..16a05472af4596788b356377954455fbfa6c2dc6 100644 (file)
@@ -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
index 18d836d0733cabca39c09b42ef07c478ce834f5f..2aa309b2b458771e18d0ead597453344014fc358 100644 (file)
@@ -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[] = {
index 5cf365d5bf7ff8e04066146af174f18e37a78234..b78ac1aa56d72e4780532bf9635b7e1a68353d18 100644 (file)
@@ -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))
 
index d44e4d0e0bafa81b0029b1475ed3185eaa77da3c..9ef30ead7d19cab604b35ee8ff1e02e02ac5fcf0 100644 (file)
@@ -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 */