altos: Adapt micro log to MicroPeak V3
[fw/altos] / src / kernel / ao_log_micro.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_micropeak.h>
21 #include <ao_log_micro.h>
22 #ifndef ao_async_byte
23 #include <ao_async.h>
24 #else
25 #include <ao_serial.h>
26 #endif
27 #include <ao_storage.h>
28
29 static uint16_t ao_log_offset = STARTING_LOG_OFFSET;
30
31 #define AO_LOG_ID_SHIFT 12
32 #define AO_LOG_ID_MASK  ((1 << AO_LOG_ID_SHIFT) - 1)
33
34 void
35 ao_log_micro_save(void)
36 {
37         uint16_t        n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t);
38
39 #if AO_LOG_ID
40         n_samples |= AO_LOG_ID << AO_LOG_ID_SHIFT;
41 #endif
42         ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
43         ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
44         ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
45 }
46
47 void
48 ao_log_micro_restore(void)
49 {
50         ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
51         ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
52 }
53
54 void
55 ao_log_micro_data(void)
56 {
57         uint16_t        low_bits = pa;
58
59         if (ao_log_offset < MAX_LOG_OFFSET) {
60                 ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits));
61                 ao_log_offset += sizeof (low_bits);
62         }
63 }
64
65 #define POLY 0x8408
66
67 static uint16_t
68 ao_log_micro_crc(uint16_t crc, uint8_t byte)
69 {
70         uint8_t i;
71
72         for (i = 0; i < 8; i++) {
73                 if ((crc & 0x0001) ^ (byte & 0x0001))
74                         crc = (crc >> 1) ^ POLY;
75                 else
76                         crc = crc >> 1;
77                 byte >>= 1;
78         }
79         return crc;
80 }
81
82 static void
83 ao_log_hex_nibble(uint8_t b)
84 {
85         if (b < 10)
86                 ao_async_byte('0' + b);
87         else
88                 ao_async_byte('a' - 10 + b);
89 }
90
91 static void
92 ao_log_hex(uint8_t b)
93 {
94         ao_log_hex_nibble(b>>4);
95         ao_log_hex_nibble(b&0xf);
96 }
97
98 static void
99 ao_log_newline(void)
100 {
101         ao_async_byte('\r');
102         ao_async_byte('\n');
103 }
104
105 void
106 ao_log_micro_dump(void)
107 {
108         uint16_t        n_samples;
109         uint16_t        nbytes;
110         uint8_t         byte;
111         uint16_t        b;
112         uint16_t        crc = 0xffff;
113
114         ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
115
116         if (n_samples == 0xffff)
117                 n_samples = 0;
118 #if AO_LOG_ID
119         n_samples &= AO_LOG_ID_MASK;
120 #endif
121         nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples;
122         ao_async_start();
123         ao_async_byte('M');
124         ao_async_byte('P');
125         for (b = 0; b < nbytes; b++) {
126                 if ((b & 0xf) == 0)
127                         ao_log_newline();
128                 ao_eeprom_read(b, &byte, 1);
129                 ao_log_hex(byte);
130                 crc = ao_log_micro_crc(crc, byte);
131         }
132         ao_log_newline();
133         crc = ~crc;
134         ao_log_hex(crc >> 8);
135         ao_log_hex(crc);
136         ao_log_newline();
137         ao_async_stop();
138 }