2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_micropeak.h>
20 #include <ao_log_micro.h>
23 static uint16_t ao_log_offset = STARTING_LOG_OFFSET;
25 #define AO_LOG_ID_SHIFT 12
26 #define AO_LOG_ID_MASK ((1 << AO_LOG_ID_SHIFT) - 1)
29 ao_log_micro_save(void)
31 uint16_t n_samples = (ao_log_offset - STARTING_LOG_OFFSET) / sizeof (uint16_t);
34 n_samples |= AO_LOG_ID << AO_LOG_ID_SHIFT;
36 ao_eeprom_write(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
37 ao_eeprom_write(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
38 ao_eeprom_write(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
42 ao_log_micro_restore(void)
44 ao_eeprom_read(PA_GROUND_OFFSET, &pa_ground, sizeof (pa_ground));
45 ao_eeprom_read(PA_MIN_OFFSET, &pa_min, sizeof (pa_min));
49 ao_log_micro_data(void)
51 uint16_t low_bits = pa;
53 if (ao_log_offset < MAX_LOG_OFFSET) {
54 ao_eeprom_write(ao_log_offset, &low_bits, sizeof (low_bits));
55 ao_log_offset += sizeof (low_bits);
62 ao_log_micro_crc(uint16_t crc, uint8_t byte)
66 for (i = 0; i < 8; i++) {
67 if ((crc & 0x0001) ^ (byte & 0x0001))
68 crc = (crc >> 1) ^ POLY;
77 ao_log_hex_nibble(uint8_t b)
80 ao_async_byte('0' + b);
82 ao_async_byte('a' - 10 + b);
88 ao_log_hex_nibble(b>>4);
89 ao_log_hex_nibble(b&0xf);
100 ao_log_micro_dump(void)
106 uint16_t crc = 0xffff;
108 ao_eeprom_read(N_SAMPLES_OFFSET, &n_samples, sizeof (n_samples));
110 if (n_samples == 0xffff)
113 n_samples &= AO_LOG_ID_MASK;
115 nbytes = STARTING_LOG_OFFSET + sizeof (uint16_t) * n_samples;
119 for (b = 0; b < nbytes; b++) {
122 ao_eeprom_read(b, &byte, 1);
124 crc = ao_log_micro_crc(crc, byte);
128 ao_log_hex(crc >> 8);