2 * Copyright © 2017 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.
18 #include "ao_eeprom_read.h"
24 ao_eeprom_add_u8(struct ao_eeprom *ao_eeprom, uint8_t byte)
26 if (ao_eeprom->len == ao_eeprom->size) {
27 uint32_t nsize = ao_eeprom->size * 2;
28 uint8_t *ndata = realloc(ao_eeprom->data, nsize);
31 ao_eeprom->data = ndata;
32 ao_eeprom->size = nsize;
34 ao_eeprom->data[ao_eeprom->len++] = byte;
39 ao_eeprom_add_u16(struct ao_eeprom *ao_eeprom, uint16_t u16)
41 if (!ao_eeprom_add_u8(ao_eeprom, u16 & 0xff))
44 return ao_eeprom_add_u8(ao_eeprom, u16 >> 8);
48 ao_eeprom_read_old(FILE *file)
50 struct ao_eeprom *ao_eeprom;
54 ao_eeprom = calloc(1, sizeof (struct ao_eeprom));
58 ao_eeprom->log_format = -1;
60 ao_eeprom->data = malloc(ao_eeprom->size);
61 if (!ao_eeprom->data) {
65 while (fgets(line, sizeof(line), file) != NULL) {
70 for (nword = 0; nword < 64; nword++) {
71 words[nword] = strtok_r(l, " \t\n", &saveptr);
73 if (words[nword] == NULL)
76 if (((ao_eeprom->log_format == AO_LOG_FORMAT_TELEMEGA_OLD || ao_eeprom->log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) ||
77 ((ao_eeprom->log_format == AO_LOG_FORMAT_EASYMINI1 || ao_eeprom->log_format == AO_LOG_FORMAT_EASYMINI2) && nword == 14 && strlen(words[0]) == 1) ||
78 (ao_eeprom->log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1))
85 tick = strtoul(words[1], NULL, 16);
86 ao_eeprom_add_u8(ao_eeprom, type);
87 ao_eeprom_add_u8(ao_eeprom, 0); /* checksum */
88 ao_eeprom_add_u16(ao_eeprom, tick);
89 for (i = 2; i < nword; i++)
90 ao_eeprom_add_u8(ao_eeprom, strtoul(words[i], NULL, 16));
92 else if (nword == 4 && strlen(words[0]) == 1) {
96 tick = strtoul(words[1], NULL, 16);
97 a = strtoul(words[2], NULL, 16);
98 b = strtoul(words[3], NULL, 16);
101 ao_eeprom_add_u8(ao_eeprom, type);
102 ao_eeprom_add_u8(ao_eeprom, 0); /* checksum */
103 ao_eeprom_add_u16(ao_eeprom, tick);
104 ao_eeprom_add_u16(ao_eeprom, a);
105 ao_eeprom_add_u16(ao_eeprom, b);
107 else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
108 if (strcmp(words[1], "reserved:") == 0)
109 ao_eeprom->ms5607_prom.reserved = strtoul(words[2], NULL, 10);
110 else if (strcmp(words[1], "sens:") == 0)
111 ao_eeprom->ms5607_prom.sens = strtoul(words[2], NULL, 10);
112 else if (strcmp(words[1], "off:") == 0)
113 ao_eeprom->ms5607_prom.off = strtoul(words[2], NULL, 10);
114 else if (strcmp(words[1], "tcs:") == 0)
115 ao_eeprom->ms5607_prom.tcs = strtoul(words[2], NULL, 10);
116 else if (strcmp(words[1], "tco:") == 0)
117 ao_eeprom->ms5607_prom.tco = strtoul(words[2], NULL, 10);
118 else if (strcmp(words[1], "tref:") == 0)
119 ao_eeprom->ms5607_prom.tref = strtoul(words[2], NULL, 10);
120 else if (strcmp(words[1], "tempsens:") == 0)
121 ao_eeprom->ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
122 else if (strcmp(words[1], "crc:") == 0)
123 ao_eeprom->ms5607_prom.crc = strtoul(words[2], NULL, 10);
127 else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
128 int p = strtoul(words[1], NULL, 10);
130 struct ao_pyro *pyro = &ao_eeprom->config.pyro[p];
132 for (i = 2; i < nword; i++) {
133 for (j = 0; j < NUM_PYRO_VALUES; j++)
134 if (!strcmp (words[i], ao_pyro_values[j].name))
136 if (j == NUM_PYRO_VALUES)
138 pyro->flags |= ao_pyro_values[j].flag;
139 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
140 int16_t val = strtoul(words[++i], NULL, 10);
141 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
142 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
147 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
148 ao_eeprom->log_format = strtoul(words[1], NULL, 10);
149 } else if (nword == 2 && strcmp(words[0], "serial-number") == 0) {
150 ao_eeprom->serial_number = strtoul(words[1], NULL, 10);
151 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
152 ao_eeprom->config.accel_plus_g = atoi(words[3]);
153 ao_eeprom->config.accel_minus_g = atoi(words[5]);
155 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
156 ao_eeprom->config.accel_zero_along = atoi(words[3]);
157 ao_eeprom->config.accel_zero_across = atoi(words[5]);
158 ao_eeprom->config.accel_zero_through = atoi(words[7]);
160 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
161 ao_eeprom->config.main_deploy = atoi(words[2]);
162 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
163 strcmp(words[1], "lockout:") == 0) {
164 ao_eeprom->config.apogee_lockout = atoi(words[2]);
165 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
166 strcmp(words[1], "orientation:") == 0) {
167 ao_eeprom->config.pad_orientation = atoi(words[2]);