Merge branch 'master' into micropeak-logging
[fw/altos] / src / micropeak / 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_micropeak.h>
20 #include <ao_log_micro.h>
21 #include <ao_async.h>
22
23 static uint16_t ao_log_offset = STARTING_LOG_OFFSET;
24
25 void
26 ao_log_micro_save(void)
27 {
28         uint16_t        n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t);
29         ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
30         ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
31         ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
32 }
33
34 void
35 ao_log_micro_restore(void)
36 {
37         ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
38         ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
39 }
40
41 void
42 ao_log_micro_data(void)
43 {
44         uint16_t        low_bits = pa;
45
46         if (ao_log_offset < MAX_LOG_OFFSET) {
47                 ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits));
48                 ao_log_offset += sizeof (low_bits);
49         }
50 }
51
52 #define POLY 0x8408
53
54 static uint16_t
55 ao_log_micro_crc(uint16_t crc, uint8_t byte)
56 {
57         uint8_t i;
58
59         for (i = 0; i < 8; i++) {
60                 if ((crc & 0x0001) ^ (byte & 0x0001))
61                         crc = (crc >> 1) ^ POLY;
62                 else
63                         crc = crc >> 1;
64                 byte >>= 1;
65         }
66         return crc;
67 }
68
69 static void
70 ao_log_hex_nibble(uint8_t b)
71 {
72         if (b < 10)
73                 ao_async_byte('0' + b);
74         else
75                 ao_async_byte('a' - 10 + b);
76 }
77
78 static void
79 ao_log_hex(uint8_t b)
80 {
81         ao_log_hex_nibble(b>>4);
82         ao_log_hex_nibble(b&0xf);
83 }
84
85 static void
86 ao_log_newline(void)
87 {
88         ao_async_byte('\r');
89         ao_async_byte('\n');
90 }
91
92 void
93 ao_log_micro_dump(void)
94 {
95         uint16_t        n_samples;
96         uint16_t        nbytes;
97         uint8_t         byte;
98         uint16_t        b;
99         uint16_t        crc = 0xffff;
100
101         ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
102         if (n_samples == 0xffff)
103                 n_samples = 0;
104         nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples;
105         ao_async_start();
106         ao_async_byte('M');
107         ao_async_byte('P');
108         for (b = 0; b < nbytes; b++) {
109                 if ((b & 0xf) == 0)
110                         ao_log_newline();
111                 ao_eeprom_read(b, &byte, 1);
112                 ao_log_hex(byte);
113                 crc = ao_log_micro_crc(crc, byte);
114         }
115         ao_log_newline();
116         crc = ~crc;
117         ao_log_hex(crc >> 8);
118         ao_log_hex(crc);
119         ao_log_newline();
120         ao_async_stop();
121 }