altos: mark gps date written only after it gets into eeprom
[fw/altos] / src / ao_log.c
index 19bfdfb89a13fe62234ac099e7dd05b910f6bc27..18bdb8c8d4f63cdca7b370d6c40cf0445322ffa7 100644 (file)
@@ -23,7 +23,7 @@ static __xdata uint8_t        ao_log_running;
 static __xdata uint8_t ao_log_mutex;
 
 static uint8_t
-ao_log_csum(uint8_t *b)
+ao_log_csum(__xdata uint8_t *b) __reentrant
 {
        uint8_t sum = 0x5a;
        uint8_t i;
@@ -33,14 +33,16 @@ ao_log_csum(uint8_t *b)
        return -sum;
 }
 
-void
-ao_log_data(struct ao_log_record *log)
+uint8_t
+ao_log_data(__xdata struct ao_log_record *log) __reentrant
 {
+       uint8_t wrote = 0;
        /* set checksum */
        log->csum = 0;
-       log->csum = ao_log_csum((uint8_t *) log);
+       log->csum = ao_log_csum((__xdata uint8_t *) log);
        ao_mutex_get(&ao_log_mutex); {
                if (ao_log_running) {
+                       wrote = 1;
                        ao_ee_write(ao_log_current_pos,
                                    (uint8_t *) log,
                                    sizeof (struct ao_log_record));
@@ -51,6 +53,7 @@ ao_log_data(struct ao_log_record *log)
                                ao_log_running = 0;
                }
        } ao_mutex_put(&ao_log_mutex);
+       return wrote;
 }
 
 void
@@ -59,62 +62,42 @@ ao_log_flush(void)
        ao_ee_flush();
 }
 
-__xdata struct ao_log_record ao_log_dump;
-static __xdata uint16_t ao_log_dump_flight;
-static __xdata uint32_t ao_log_dump_pos;
+__xdata struct ao_log_record log;
+__xdata uint16_t ao_flight_number;
 
 static uint8_t
 ao_log_dump_check_data(void)
 {
-       if (ao_log_csum((uint8_t *) &ao_log_dump) != 0)
+       if (ao_log_csum((uint8_t *) &log) != 0)
                return 0;
        return 1;
 }
 
-static uint8_t
-ao_log_dump_scan(void)
+static void
+ao_log_scan(void)
 {
-       if (!ao_ee_read(0, (uint8_t *) &ao_log_dump, sizeof (struct ao_log_record)))
+       if (!ao_ee_read(0, (uint8_t *) &log, sizeof (struct ao_log_record)))
                ao_panic(AO_PANIC_LOG);
-       if (ao_log_dump_check_data() && ao_log_dump.type == AO_LOG_FLIGHT) {
-               ao_log_dump_flight = ao_log_dump.u.flight.flight;
-               return 1;
+       if (ao_log_dump_check_data() && log.type == AO_LOG_FLIGHT) {
+               ao_flight_number = log.u.flight.flight + 1;
+               if (ao_flight_number == 0)
+                       ao_flight_number = 1;
        } else {
-               ao_log_dump_flight = 0;
-               return 0;
+               ao_flight_number = 1;
        }
-}
-
-uint8_t
-ao_log_dump_first(void)
-{
-       ao_log_dump_pos = 0;
-       if (!ao_log_dump_scan())
-               return 0;
-       return 1;
-}
-
-uint8_t
-ao_log_dump_next(void)
-{
-       ao_log_dump_pos += sizeof (struct ao_log_record);
-       if (ao_log_dump_pos >= AO_EE_DEVICE_SIZE)
-               return 0;
-       if (!ao_ee_read(ao_log_dump_pos, (uint8_t *) &ao_log_dump,
-                       sizeof (struct ao_log_record)))
-               return 0;
-       return ao_log_dump_check_data();
+       ao_wakeup(&ao_flight_number);
 }
 
 __xdata uint8_t        ao_log_adc_pos;
 __xdata enum flight_state ao_log_state;
 
+/* a hack to make sure that ao_log_records fill the eeprom block in even units */
+typedef uint8_t check_log_size[1-(256 % sizeof(struct ao_log_record))] ;
+
 void
 ao_log(void)
 {
-       static __xdata struct ao_log_record     log;
-
-       ao_log_dump_scan();
+       ao_log_scan();
 
        while (!ao_log_running)
                ao_sleep(&ao_log_running);
@@ -122,7 +105,7 @@ ao_log(void)
        log.type = AO_LOG_FLIGHT;
        log.tick = ao_flight_tick;
        log.u.flight.ground_accel = ao_ground_accel;
-       log.u.flight.flight = ao_log_dump_flight + 1;
+       log.u.flight.flight = ao_flight_number;
        ao_log_data(&log);
 
        /* Write the whole contents of the ring to the log
@@ -188,29 +171,6 @@ ao_log_stop(void)
        ao_log_flush();
 }
 
-static void
-dump_log(void)
-{
-       uint8_t more;
-
-       for (more = ao_log_dump_first(); more; more = ao_log_dump_next()) {
-               printf("%c %4x %4x %4x\n",
-                      ao_log_dump.type,
-                      ao_log_dump.tick,
-                      ao_log_dump.u.anon.d0,
-                      ao_log_dump.u.anon.d1);
-               if (ao_log_dump.type == AO_LOG_STATE &&
-                   ao_log_dump.u.state.state == ao_flight_landed)
-                       break;
-       }
-       printf("end\n");
-}
-
-__code struct ao_cmds ao_log_cmds[] = {
-       { 'l',  dump_log,               "l                                  Dump last flight log" },
-       { 0, dump_log, NULL },
-};
-
 static __xdata struct ao_task ao_log_task;
 
 void
@@ -225,5 +185,4 @@ ao_log_init(void)
 
        /* Create a task to log events to eeprom */
        ao_add_task(&ao_log_task, ao_log, "log");
-       ao_cmd_register(&ao_log_cmds[0]);
 }