APM:Libraries
eedump_apparam.c
Go to the documentation of this file.
1 /*
2  * Simple tool to dump the AP_Param contents from an EEPROM dump
3  * Andrew Tridgell February 2012
4  */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 
9 uint8_t eeprom[0x1000];
10 
11 struct EEPROM_header {
12  uint8_t magic[2];
13  uint8_t revision;
14  uint8_t spare;
15 };
16 
17 static const uint16_t k_EEPROM_magic0 = 0x50;
18 static const uint16_t k_EEPROM_magic1 = 0x41;
19 static const uint16_t k_EEPROM_revision = 6;
20 
31 };
32 
33 static const char *type_names[8] = {
34  "NONE", "INT8", "INT16", "INT32", "FLOAT", "VECTOR3F", "MATRIX6F", "GROUP"
35 };
36 
37 struct Param_header {
38  uint32_t key : 8;
39  uint32_t type : 6;
40  uint32_t group_element : 18;
41 };
42 
43 
44 static const uint8_t _sentinal_key = 0xFF;
45 static const uint8_t _sentinal_type = 0xFF;
46 static const uint8_t _sentinal_group = 0xFF;
47 
48 static uint8_t type_size(enum ap_var_type type)
49 {
50  switch (type) {
51  case AP_PARAM_NONE:
52  case AP_PARAM_GROUP:
53  return 0;
54  case AP_PARAM_INT8:
55  return 1;
56  case AP_PARAM_INT16:
57  return 2;
58  case AP_PARAM_INT32:
59  return 4;
60  case AP_PARAM_FLOAT:
61  return 4;
62  case AP_PARAM_VECTOR3F:
63  return 3*4;
64  case AP_PARAM_VECTOR6F:
65  return 6*4;
66  case AP_PARAM_MATRIX3F:
67  return 3*3*4;
68  }
69  printf("unknown type %u\n", (unsigned int)type);
70  return 0;
71 }
72 
73 static void
74 fail(const char *why)
75 {
76  fprintf(stderr, "ERROR: %s\n", why);
77  exit(1);
78 }
79 
80 int
81 main(int argc, char *argv[])
82 {
83  FILE *fp;
84  struct EEPROM_header *header;
85  struct Param_header *var;
86  unsigned index;
87  unsigned i;
88 
89  if (argc != 2) {
90  fail("missing EEPROM file name");
91  }
92  if (NULL == (fp = fopen(argv[1], "rb"))) {
93  fail("can't open EEPROM file");
94  }
95  if (1 != fread(eeprom, sizeof(eeprom), 1, fp)) {
96  fail("can't read EEPROM file");
97  }
98  fclose(fp);
99 
100  header = (struct EEPROM_header *)&eeprom[0];
101  if (header->magic[0] != k_EEPROM_magic0 ||
102  header->magic[1] != k_EEPROM_magic1) {
103  fail("bad magic in EEPROM file");
104  }
105  if (header->revision != k_EEPROM_revision) {
106  fail("unsupported EEPROM format revision");
107  }
108  printf("Header OK\n");
109 
110  index = sizeof(*header);
111  for (;; ) {
112  uint8_t size;
113  var = (struct Param_header *)&eeprom[index];
114  if (var->key == _sentinal_key ||
115  var->group_element == _sentinal_group ||
116  var->type == _sentinal_type) {
117  printf("end sentinel at %u\n", index);
118  break;
119  }
120  size = type_size(var->type);
121  printf("%04x: type %u (%s) key %u group_element %u size %d value ",
122  index, var->type, type_names[var->type], var->key, var->group_element, size);
123  index += sizeof(*var);
124  switch (var->type) {
125  case AP_PARAM_INT8:
126  printf("%d\n", (int)*(int8_t *)&eeprom[index]);
127  break;
128  case AP_PARAM_INT16:
129  printf("%d\n", (int)*(int16_t *)&eeprom[index]);
130  break;
131  case AP_PARAM_INT32:
132  printf("%d\n", (int)*(int32_t *)&eeprom[index]);
133  break;
134  case AP_PARAM_FLOAT:
135  printf("%f\n", *(float *)&eeprom[index]);
136  break;
137  case AP_PARAM_VECTOR3F:
138  printf("%f %f %f\n",
139  *(float *)&eeprom[index],
140  *(float *)&eeprom[index+4],
141  *(float *)&eeprom[index+8]);
142  break;
143  case AP_PARAM_VECTOR6F:
144  printf("%f %f %f %f %f %f\n",
145  *(float *)&eeprom[index],
146  *(float *)&eeprom[index+4],
147  *(float *)&eeprom[index+8],
148  *(float *)&eeprom[index+12],
149  *(float *)&eeprom[index+16],
150  *(float *)&eeprom[index+20]);
151  break;
152  case AP_PARAM_MATRIX3F:
153  printf("%f %f %f %f %f %f %f %f %f\n",
154  *(float *)&eeprom[index],
155  *(float *)&eeprom[index+4],
156  *(float *)&eeprom[index+8],
157  *(float *)&eeprom[index+12],
158  *(float *)&eeprom[index+16],
159  *(float *)&eeprom[index+20],
160  *(float *)&eeprom[index+24],
161  *(float *)&eeprom[index+28],
162  *(float *)&eeprom[index+32]);
163  break;
164  default:
165  printf("NONE\n");
166  break;
167  }
168  for (i = 0; i < size; i++) {
169  printf(" %02x", eeprom[index + i]);
170  }
171  printf("\n");
172  index += size;
173  if (index >= sizeof(eeprom)) {
174  fflush(stdout);
175  fail("missing end sentinel");
176  }
177  }
178  return 0;
179 }
static const char * type_names[8]
int printf(const char *fmt,...)
Definition: stdio.c:113
uint32_t group_element
uint16_t magic
Definition: eedump.c:11
ap_var_type
Definition: AP_Param.h:124
uint32_t key
static const uint8_t _sentinal_key
static const uint8_t _sentinal_type
static void fail(const char *why)
static const uint16_t k_EEPROM_magic1
#define NULL
Definition: hal_types.h:59
uint8_t spare
Definition: eedump.c:13
int main(int argc, char *argv[])
static const uint8_t _sentinal_group
FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:772
static uint8_t type_size(enum ap_var_type type)
int fprintf(FILE *fp, const char *fmt,...)
fprintf character write function
Definition: posix.c:2539
uint8_t revision
Definition: eedump.c:12
uint32_t type
uint8_t eeprom[0x1000]
Definition: eedump_apparam.c:9
static const uint16_t k_EEPROM_magic0
static const uint16_t k_EEPROM_revision