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;
break;
}
}
- printf("Logging will start at %ld\n", ao_log_current_pos); flush();
}
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.
/* 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));
}
}
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[] = {
#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,
.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;
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;
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 */