altos: Share log code between telescience and telebt. Add telebt log
authorKeith Packard <keithp@keithp.com>
Sat, 27 Aug 2011 22:19:43 +0000 (15:19 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 27 Aug 2011 22:19:43 +0000 (15:19 -0700)
Telescience and telebt both log data in 32-byte chunks, so share some
code which manages that between the two products. Add simple telemetry
logging to telebt.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/avr/ao_spi_slave.c
src/core/ao.h
src/core/ao_log_telem.c
src/core/ao_log_telescience.c
src/core/ao_monitor.c
src/product/ao_telebt.c
src/product/ao_telescience.c
src/telebt-v0.1/Makefile
src/telescience-v0.1/Makefile

index 4dde09f3dbaff5f06ac3a0b7291079b72c8cb9d0..e4d866a823ab11501314cb7c6071b4e087a213fd 100644 (file)
@@ -74,9 +74,9 @@ static uint8_t ao_spi_slave_recv(void)
                return 0;
        }
 
-       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;
+       ao_log_single_write_data.telescience.tm_tick = ao_companion_command.tick;
+       if (ao_log_single_write_data.telescience.tm_state != ao_companion_command.flight_state) {
+               ao_log_single_write_data.telescience.tm_state = ao_companion_command.flight_state;
                return 1;
        }
        return 0;
@@ -93,11 +93,11 @@ ISR(PCINT0_vect)
                        cli();
                        changed = ao_spi_slave_recv();
                        sei();
-                       if (changed && ao_flight_boost <= ao_log_store.tm_state) {
-                               if (ao_log_store.tm_state < ao_flight_landed)
-                                       ao_log_start();
+                       if (changed && ao_flight_boost <= ao_log_single_write_data.telescience.tm_state) {
+                               if (ao_log_single_write_data.telescience.tm_state < ao_flight_landed)
+                                       ao_log_single_start();
                                else
-                                       ao_log_stop();
+                                       ao_log_single_stop();
                        }
                }
        } else {
index a5bbb6f1fce51433ffb985a651a2670fca43cadf..8b978272349607e8c9dc493fcd29cfa1d81fe9c3 100644 (file)
@@ -505,23 +505,6 @@ extern __pdata uint32_t ao_log_start_pos;
 extern __xdata uint8_t ao_log_running;
 extern __pdata enum flight_state ao_log_state;
 
-#define AO_LOG_TELESCIENCE_START       ((uint8_t) 's')
-#define AO_LOG_TELESCIENCE_DATA                ((uint8_t) 'd')
-
-#define AO_LOG_TELESCIENCE_NUM_ADC     12
-
-struct ao_log_telescience {
-       uint8_t         type;
-       uint8_t         csum;
-       uint16_t        tick;
-       uint16_t        tm_tick;
-       uint8_t         tm_state;
-       uint8_t         unused;
-       uint16_t        adc[AO_LOG_TELESCIENCE_NUM_ADC];
-};
-
-extern struct ao_log_telescience ao_log_store;
-
 /* required functions from the underlying log system */
 
 #define AO_LOG_FORMAT_UNKNOWN          0       /* unknown; altosui will have to guess */
@@ -1365,6 +1348,20 @@ ao_radio_init(void);
 
 extern const char const * const ao_state_names[];
 
+#define AO_MONITOR_RING        8
+
+union ao_monitor {
+               struct ao_telemetry_raw_recv    raw;
+               struct ao_telemetry_orig_recv   orig;
+               struct ao_telemetry_tiny_recv   tiny;
+};
+
+extern __xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
+
+#define ao_monitor_ring_next(n)        (((n) + 1) & (AO_MONITOR_RING - 1))
+
+extern __data uint8_t ao_monitor_head;
+
 void
 ao_monitor(void);
 
@@ -1724,4 +1721,70 @@ struct ao_launch_query {
 void
 ao_launch_init(void);
 
+/*
+ * ao_log_single.c
+ */
+
+#define AO_LOG_TELESCIENCE_START       ((uint8_t) 's')
+#define AO_LOG_TELESCIENCE_DATA                ((uint8_t) 'd')
+
+#define AO_LOG_TELESCIENCE_NUM_ADC     12
+
+struct ao_log_telescience {
+       uint8_t         type;
+       uint8_t         csum;
+       uint16_t        tick;
+       uint16_t        tm_tick;
+       uint8_t         tm_state;
+       uint8_t         unused;
+       uint16_t        adc[AO_LOG_TELESCIENCE_NUM_ADC];
+};
+
+#define AO_LOG_SINGLE_SIZE             32
+
+union ao_log_single {
+       struct ao_log_telescience       telescience;
+       union ao_telemetry_all          telemetry;
+       uint8_t                         bytes[AO_LOG_SINGLE_SIZE];
+};
+
+extern __xdata union ao_log_single     ao_log_single_write_data;
+extern __xdata union ao_log_single     ao_log_single_read_data;
+
+void
+ao_log_single_extra_query(void);
+
+void
+ao_log_single_list(void);
+
+void
+ao_log_single_main(void);
+
+uint8_t
+ao_log_single_write(void);
+
+uint8_t
+ao_log_single_read(uint32_t pos);
+
+void
+ao_log_single_start(void);
+
+void
+ao_log_single_stop(void);
+
+void
+ao_log_single_restart(void);
+
+void
+ao_log_single_set(void);
+
+void
+ao_log_single_delete(void);
+
+void
+ao_log_single_init(void);
+
+void
+ao_log_single(void);
+
 #endif /* _AO_H_ */
index 1b472efe38614a7d2d52cb2dd6b896b3c836a1dc..34abe879b210bf20637f108afe6d0be3c49998f7 100644 (file)
 
 #include "ao.h"
 
+__code uint8_t ao_log_format = AO_LOG_FORMAT_TELEMETRY;
+
+static __data uint8_t  ao_log_monitor_pos;
+
+void
+ao_log_single(void)
+{
+       ao_storage_setup();
+
+       /* This can take a while, so let the rest
+        * of the system finish booting before we start
+        */
+       ao_delay(AO_SEC_TO_TICKS(2));
+
+       ao_log_single_restart();
+       for (;;) {
+               while (!ao_log_running)
+                       ao_sleep(&ao_log_running);
+
+               ao_log_monitor_pos = ao_monitor_head;
+               while (ao_log_running) {
+                       /* Write samples to EEPROM */
+                       while (ao_log_monitor_pos != ao_monitor_head) {
+                               memcpy(&ao_log_single_write_data.telemetry,
+                                      &ao_monitor_ring[ao_log_monitor_pos],
+                                      AO_LOG_SINGLE_SIZE);
+                               ao_log_single_write();
+                               ao_log_monitor_pos = ao_monitor_ring_next(ao_log_monitor_pos);
+                       }
+                       /* Wait for more ADC data to arrive */
+                       ao_sleep(DATA_TO_XDATA(&ao_monitor_head));
+               }
+       }
+}
+
 void
-ao_log_write_erase(uint8_t pos)
+ao_log_single_list(void)
 {
-       (void) pos;
+       if (ao_log_current_pos != 0)
+               printf("flight 1 start %x end %x\n",
+                      0,
+                      (uint16_t) ((ao_log_current_pos + 0xff) >> 8));
+       printf ("done\n");
 }
 
-uint8_t
-ao_log_present(void)
+void
+ao_log_single_extra_query(void)
 {
-       return 0;
 }
index aac780fad58fb653d82c0f5c9385fcd82765acfc..31eda381d775a9d393c3c5a591816ce09858e56c 100644 (file)
 #include "ao.h"
 #include "ao_product.h"
 
-static struct ao_task ao_log_task;
-//static struct ao_task ao_spi_task;
-
-uint8_t        ao_log_running;
-uint8_t                ao_log_mutex;
-uint32_t       ao_log_start_pos;
-uint32_t       ao_log_end_pos;
-uint32_t       ao_log_current_pos;
-
-#define AO_LOG_TELESCIENCE_START       ((uint8_t) 's')
-#define AO_LOG_TELESCIENCE_DATA                ((uint8_t) 'd')
-
-struct ao_log_telescience ao_log_store;
-struct ao_log_telescience ao_log_fetch;
-
 static uint8_t ao_log_adc_pos;
 
 __code uint8_t ao_log_format = AO_LOG_FORMAT_TELESCIENCE;
 
-static uint8_t
-ao_log_csum(__xdata uint8_t *b) __reentrant
+static void
+ao_log_telescience_csum(void) __reentrant
 {
+       __xdata uint8_t *b = ao_log_single_write_data.bytes;
        uint8_t sum = 0x5a;
        uint8_t i;
 
+       ao_log_single_write_data.telescience.csum = 0;
        for (i = 0; i < sizeof (struct ao_log_telescience); i++)
                sum += *b++;
-       return -sum;
-}
-
-static uint8_t
-ao_log_telescience_write(void)
-{
-       uint8_t wrote = 0;
-
-       ao_log_store.csum = 0;
-       ao_log_store.csum = ao_log_csum((__xdata uint8_t *) &ao_log_store);
-       ao_mutex_get(&ao_log_mutex); {
-               if (ao_log_current_pos >= ao_log_end_pos && ao_log_running)
-                       ao_log_stop();
-               if (ao_log_running) {
-                       wrote = 1;
-                       ao_storage_write(ao_log_current_pos,
-                                        (__xdata uint8_t *) &ao_log_store,
-                                        sizeof (struct ao_log_telescience));
-                       ao_log_current_pos += sizeof (struct ao_log_telescience);
-               }
-       } ao_mutex_put(&ao_log_mutex);
-       return wrote;
-}
-
-static uint8_t
-ao_log_valid(struct ao_log_telescience *log)
-{
-       uint8_t *d;
-       uint8_t i;
-       d = (uint8_t *) log;
-       for (i = 0; i < sizeof (struct ao_log_telescience); i++)
-               if (d[i] != 0xff)
-                       return 1;
-       return 0;
-}
-
-static uint8_t
-ao_log_telescience_read(uint32_t pos)
-{
-       if (!ao_storage_read(pos, &ao_log_fetch, sizeof (struct ao_log_telescience)))
-               return 0;
-       return ao_log_valid(&ao_log_fetch);
-}
-
-void
-ao_log_start(void)
-{
-       if (!ao_log_running) {
-               ao_log_running = 1;
-               ao_wakeup(&ao_log_running);
-       }
-}
-
-void
-ao_log_stop(void)
-{
-       if (ao_log_running) {
-               ao_log_running = 0;
-       }
-}
-
-void
-ao_log_restart(void)
-{
-       /* 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)
-       {
-               if (!ao_log_telescience_read(ao_log_current_pos))
-                       break;
-       }
-       if (ao_log_current_pos > 0) {
-               ao_log_current_pos -= ao_storage_block;
-               for (; ao_log_current_pos < ao_storage_config;
-                    ao_log_current_pos += sizeof (struct ao_log_telescience))
-               {
-                       if (!ao_log_telescience_read(ao_log_current_pos))
-                               break;
-               }
-       }
+       ao_log_single_write_data.telescience.csum = -sum;
 }
 
 void
-ao_log_telescience(void)
+ao_log_single(void)
 {
        ao_storage_setup();
 
@@ -139,57 +45,42 @@ ao_log_telescience(void)
         */
        ao_delay(AO_SEC_TO_TICKS(10));
 
-       ao_log_restart();
+       ao_log_single_restart();
        for (;;) {
                while (!ao_log_running)
                        ao_sleep(&ao_log_running);
 
                ao_log_start_pos = ao_log_current_pos;
-               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();
+               ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_START;
+               ao_log_single_write_data.telescience.tick = ao_time();
+               ao_log_single_write_data.telescience.adc[0] = ao_companion_command.serial;
+               ao_log_single_write_data.telescience.adc[1] = ao_companion_command.flight;
+               ao_log_telescience_csum();
+               ao_log_single_write();
                /* Write the whole contents of the ring to the log
                 * when starting up.
                 */
                ao_log_adc_pos = ao_adc_ring_next(ao_adc_head);
-               ao_log_store.type = AO_LOG_TELESCIENCE_DATA;
+               ao_log_single_write_data.telescience.type = AO_LOG_TELESCIENCE_DATA;
                while (ao_log_running) {
                        /* Write samples to EEPROM */
                        while (ao_log_adc_pos != ao_adc_head) {
-                               ao_log_store.tick = ao_adc_ring[ao_log_adc_pos].tick;
-                               memcpy(&ao_log_store.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
+                               ao_log_single_write_data.telescience.tick = ao_adc_ring[ao_log_adc_pos].tick;
+                               memcpy(&ao_log_single_write_data.telescience.adc, (void *) ao_adc_ring[ao_log_adc_pos].adc,
                                       AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
-                               ao_log_telescience_write();
+                               ao_log_telescience_csum();
+                               ao_log_single_write();
                                ao_log_adc_pos = ao_adc_ring_next(ao_log_adc_pos);
                        }
                        /* Wait for more ADC data to arrive */
                        ao_sleep((void *) &ao_adc_head);
                }
-               memset(&ao_log_store.adc, '\0', sizeof (ao_log_store.adc));
+               memset(&ao_log_single_write_data.telescience.adc, '\0', sizeof (ao_log_single_write_data.telescience.adc));
        }
 }
 
 void
-ao_log_set(void)
-{
-       printf("Logging currently %s\n", ao_log_running ? "on" : "off");
-       ao_cmd_hex();
-       if (ao_cmd_status == ao_cmd_success) {
-               if (ao_cmd_lex_i) {
-                       printf("Logging from %ld to %ld\n", ao_log_current_pos, ao_log_end_pos);
-                       ao_log_start();
-               } else {
-                       printf ("Log stopped at %ld\n", ao_log_current_pos);
-                       ao_log_stop();
-               }
-       }
-       ao_cmd_status = ao_cmd_success;
-}
-
-void
-ao_log_list(void)
+ao_log_single_list(void)
 {
        uint32_t        pos;
        uint32_t        start = 0;
@@ -197,8 +88,8 @@ ao_log_list(void)
 
        for (pos = 0; ; pos += sizeof (struct ao_log_telescience)) {
                if (pos >= ao_storage_config ||
-                   !ao_log_telescience_read(pos) ||
-                   ao_log_fetch.type == AO_LOG_TELESCIENCE_START)
+                   !ao_log_single_read(pos) ||
+                   ao_log_single_read_data.telescience.type == AO_LOG_TELESCIENCE_START)
                {
                        if (pos != start) {
                                printf("flight %d start %x end %x\n",
@@ -206,7 +97,7 @@ ao_log_list(void)
                                       (uint16_t) (start >> 8),
                                       (uint16_t) ((pos + 0xff) >> 8)); flush();
                        }
-                       if (ao_log_fetch.type != AO_LOG_TELESCIENCE_START)
+                       if (ao_log_single_read_data.telescience.type != AO_LOG_TELESCIENCE_START)
                                break;
                        start = pos;
                        flight++;
@@ -216,59 +107,11 @@ ao_log_list(void)
 }
 
 void
-ao_log_delete(void)
+ao_log_single_extra_query(void)
 {
-       uint32_t        pos;
-
-       ao_cmd_hex();
-       if (ao_cmd_status != ao_cmd_success)
-               return;
-       if (ao_cmd_lex_i != 1) {
-               ao_cmd_status = ao_cmd_syntax_error;
-               printf("No such flight: %d\n", ao_cmd_lex_i);
-               return;
-       }
-       ao_log_stop();
-       for (pos = 0; pos < ao_storage_config; pos += ao_storage_block) {
-               if (!ao_log_telescience_read(pos))
-                       break;
-               ao_storage_erase(pos);
-       }
-       ao_log_current_pos = ao_log_start_pos = 0;
-       if (pos == 0)
-               printf("No such flight: %d\n", ao_cmd_lex_i);
-       else
-               printf ("Erased\n");
-}
-
-static void
-ao_log_query(void)
-{
-       printf("Logging enabled: %d\n", ao_log_running);
-       printf("Log start: %ld\n", ao_log_start_pos);
-       printf("Log cur: %ld\n", ao_log_current_pos);
-       printf("Log end: %ld\n", ao_log_end_pos);
-       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("log data tick: %04x\n", ao_log_single_write_data.telescience.tick);
+       printf("TM data tick: %04x\n", ao_log_single_write_data.telescience.tm_tick);
+       printf("TM state: %d\n", ao_log_single_write_data.telescience.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[] = {
-       { ao_log_set,   "L <0 off, 1 on>\0Set logging mode" },
-       { ao_log_list,  "l\0List stored flight logs" },
-       { ao_log_delete, "d 1\0Delete all stored flights" },
-       { ao_log_query, "q\0Query log status" },
-       { 0,    NULL },
-};
-
-void
-ao_log_init(void)
-{
-       ao_log_running = 0;
-
-       ao_cmd_register(&ao_log_cmds[0]);
-
-       ao_add_task(&ao_log_task, ao_log_telescience, "log");
-}
index 69eb58e85f8ac1373b9576b66573ecedfefd8019..6f2d9dbb3d3bac0a626491b3d217bb00d3213c61 100644 (file)
 __xdata uint8_t ao_monitoring;
 __pdata uint8_t ao_monitor_led;
 
-#define AO_MONITOR_RING        8
-
-__xdata union ao_monitor {
-               struct ao_telemetry_raw_recv    raw;
-               struct ao_telemetry_orig_recv   orig;
-               struct ao_telemetry_tiny_recv   tiny;
-} ao_monitor_ring[AO_MONITOR_RING];
-
-#define ao_monitor_ring_next(n)        (((n) + 1) & (AO_MONITOR_RING - 1))
+__xdata union ao_monitor ao_monitor_ring[AO_MONITOR_RING];
 
 __data uint8_t ao_monitor_head;
 
index 5bbbf71b2ace93853f619a51186d6a093ef14f8f..cb23f39100731c52cdb41845a058a079ef5c4f97 100644 (file)
@@ -42,6 +42,9 @@ main(void)
        ao_radio_init();
        ao_packet_master_init();
        ao_btm_init();
+#if HAS_LOG
+       ao_log_single_init();
+#endif
 #if HAS_DBG
        ao_dbg_init();
 #endif
index 4dec3a184adad922c301b3e47e8896731c87b4c1..45b6d40e0c406ca1af29c1fadd69c5e65453bf33 100644 (file)
@@ -33,7 +33,7 @@ main(void)
        ao_storage_init();
        ao_usb_init();
        ao_adc_init();
-       ao_log_init();
+       ao_log_single_init();
        ao_start_scheduler();
        return 0;
 }
index a34e89126e2f754dde0afd805476bec99e90e204..01fbaf5205b471ca1820010b98601fb833411386 100644 (file)
@@ -10,6 +10,7 @@ TELEBT_INC = \
 
 TELEBT_SRC = \
        ao_beep.c \
+       ao_log_single.c \
        ao_log_telem.c \
        ao_spi.c \
        ao_storage.c \
index 3ccbb787479cafda4ea131e8a7b3a1e65ae2286b..282829e7c476520f8e5d5a0d00695acacd17ecd3 100644 (file)
@@ -46,6 +46,7 @@ ALTOS_SRC = \
        ao_usb_avr.c \
        ao_adc_avr.c \
        ao_spi_slave.c \
+       ao_log_single.c \
        ao_log_telescience.c \
        $(TELESCIENCE_STORAGE)