Merge branch 'master' into micropeak-logging
[fw/altos] / src / micropeak / ao_log_micro.c
index 40a7a35dbe32561a620a6ffcde0407c7433d0ae4..d665efb5b56f94e1123afe8df306ad02dc53bbca 100644 (file)
@@ -49,6 +49,46 @@ ao_log_micro_data(void)
        }
 }
 
+#define POLY 0x8408
+
+static uint16_t
+ao_log_micro_crc(uint16_t crc, uint8_t byte)
+{
+       uint8_t i;
+
+       for (i = 0; i < 8; i++) {
+               if ((crc & 0x0001) ^ (byte & 0x0001))
+                       crc = (crc >> 1) ^ POLY;
+               else
+                       crc = crc >> 1;
+               byte >>= 1;
+       }
+       return crc;
+}
+
+static void
+ao_log_hex_nibble(uint8_t b)
+{
+       if (b < 10)
+               ao_async_byte('0' + b);
+       else
+               ao_async_byte('a' - 10 + b);
+}
+
+static void
+ao_log_hex(uint8_t b)
+{
+       ao_log_hex_nibble(b>>4);
+       ao_log_hex_nibble(b&0xf);
+}
+
+static void
+ao_log_newline(void)
+{
+       ao_async_byte('\r');
+       ao_async_byte('\n');
+}
+
 void
 ao_log_micro_dump(void)
 {
@@ -56,13 +96,26 @@ ao_log_micro_dump(void)
        uint16_t        nbytes;
        uint8_t         byte;
        uint16_t        b;
+       uint16_t        crc = 0xffff;
 
        ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
+       if (n_samples == 0xffff)
+               n_samples = 0;
        nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples;
+       ao_async_start();
        ao_async_byte('M');
        ao_async_byte('P');
        for (b = 0; b < nbytes; b++) {
+               if ((b & 0xf) == 0)
+                       ao_log_newline();
                ao_eeprom_read(b, &byte, 1);
-               ao_async_byte(byte);
+               ao_log_hex(byte);
+               crc = ao_log_micro_crc(crc, byte);
        }
+       ao_log_newline();
+       crc = ~crc;
+       ao_log_hex(crc >> 8);
+       ao_log_hex(crc);
+       ao_log_newline();
+       ao_async_stop();
 }