Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1702 | - | 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 | #pragma pack(1) |
||
12 | |||
13 | struct EEPROM_header { |
||
14 | uint8_t magic[2]; |
||
15 | uint8_t revision; |
||
16 | uint8_t spare; |
||
17 | }; |
||
18 | |||
19 | static const uint16_t k_EEPROM_magic0 = 0x50; |
||
20 | static const uint16_t k_EEPROM_magic1 = 0x41; |
||
21 | static const uint16_t k_EEPROM_revision = 5; |
||
22 | |||
23 | enum ap_var_type { |
||
24 | AP_PARAM_NONE = 0, |
||
25 | AP_PARAM_INT8, |
||
26 | AP_PARAM_INT16, |
||
27 | AP_PARAM_INT32, |
||
28 | AP_PARAM_FLOAT, |
||
29 | AP_PARAM_VECTOR3F, |
||
30 | AP_PARAM_VECTOR6F, |
||
31 | AP_PARAM_MATRIX3F, |
||
32 | AP_PARAM_GROUP |
||
33 | }; |
||
34 | |||
35 | static const char *type_names[8] = { |
||
36 | "NONE", "INT8", "INT16", "INT32", "FLOAT", "VECTOR3F", "MATRIX6F", "GROUP" |
||
37 | }; |
||
38 | |||
39 | struct Param_header { |
||
40 | uint8_t key; |
||
41 | uint8_t group_element; |
||
42 | uint8_t type; |
||
43 | }; |
||
44 | |||
45 | |||
46 | static const uint8_t _sentinal_key = 0xFF; |
||
47 | static const uint8_t _sentinal_type = 0xFF; |
||
48 | static const uint8_t _sentinal_group = 0xFF; |
||
49 | |||
50 | static uint8_t type_size(enum ap_var_type type) |
||
51 | { |
||
52 | switch (type) { |
||
53 | case AP_PARAM_NONE: |
||
54 | case AP_PARAM_GROUP: |
||
55 | return 0; |
||
56 | case AP_PARAM_INT8: |
||
57 | return 1; |
||
58 | case AP_PARAM_INT16: |
||
59 | return 2; |
||
60 | case AP_PARAM_INT32: |
||
61 | return 4; |
||
62 | case AP_PARAM_FLOAT: |
||
63 | return 4; |
||
64 | case AP_PARAM_VECTOR3F: |
||
65 | return 3*4; |
||
66 | case AP_PARAM_VECTOR6F: |
||
67 | return 6*4; |
||
68 | case AP_PARAM_MATRIX3F: |
||
69 | return 3*3*4; |
||
70 | } |
||
71 | printf("unknown type %u\n", type); |
||
72 | return 0; |
||
73 | } |
||
74 | |||
75 | static void |
||
76 | fail(const char *why) |
||
77 | { |
||
78 | fprintf(stderr, "ERROR: %s\n", why); |
||
79 | exit(1); |
||
80 | } |
||
81 | |||
82 | int |
||
83 | main(int argc, char *argv[]) |
||
84 | { |
||
85 | FILE *fp; |
||
86 | struct EEPROM_header *header; |
||
87 | struct Param_header *var; |
||
88 | unsigned index; |
||
89 | unsigned i; |
||
90 | |||
91 | if (argc != 2) { |
||
92 | fail("missing EEPROM file name"); |
||
93 | } |
||
94 | if (NULL == (fp = fopen(argv[1], "rb"))) { |
||
95 | fail("can't open EEPROM file"); |
||
96 | } |
||
97 | if (1 != fread(eeprom, sizeof(eeprom), 1, fp)) { |
||
98 | fail("can't read EEPROM file"); |
||
99 | } |
||
100 | fclose(fp); |
||
101 | |||
102 | header = (struct EEPROM_header *)&eeprom[0]; |
||
103 | if (header->magic[0] != k_EEPROM_magic0 || |
||
104 | header->magic[1] != k_EEPROM_magic1) { |
||
105 | fail("bad magic in EEPROM file"); |
||
106 | } |
||
107 | if (header->revision != k_EEPROM_revision) { |
||
108 | fail("unsupported EEPROM format revision"); |
||
109 | } |
||
110 | printf("Header OK\n"); |
||
111 | |||
112 | index = sizeof(*header); |
||
113 | for (;;) { |
||
114 | uint8_t size; |
||
115 | var = (struct Param_header *)&eeprom[index]; |
||
116 | if (var->key == _sentinal_key || |
||
117 | var->group_element == _sentinal_group || |
||
118 | var->type == _sentinal_type) { |
||
119 | printf("end sentinel at %u\n", index); |
||
120 | break; |
||
121 | } |
||
122 | size = type_size(var->type); |
||
123 | printf("%04x: type %u (%s) key %u group_element %u size %d value ", |
||
124 | index, var->type, type_names[var->type], var->key, var->group_element, size); |
||
125 | index += sizeof(*var); |
||
126 | switch (var->type) { |
||
127 | case AP_PARAM_INT8: |
||
128 | printf("%d\n", (int)*(int8_t *)&eeprom[index]); |
||
129 | break; |
||
130 | case AP_PARAM_INT16: |
||
131 | printf("%d\n", (int)*(int16_t *)&eeprom[index]); |
||
132 | break; |
||
133 | case AP_PARAM_INT32: |
||
134 | printf("%d\n", (int)*(int32_t *)&eeprom[index]); |
||
135 | break; |
||
136 | case AP_PARAM_FLOAT: |
||
137 | printf("%f\n", *(float *)&eeprom[index]); |
||
138 | break; |
||
139 | case AP_PARAM_VECTOR3F: |
||
140 | printf("%f %f %f\n", |
||
141 | *(float *)&eeprom[index], |
||
142 | *(float *)&eeprom[index+4], |
||
143 | *(float *)&eeprom[index+8]); |
||
144 | break; |
||
145 | case AP_PARAM_VECTOR6F: |
||
146 | printf("%f %f %f %f %f %f\n", |
||
147 | *(float *)&eeprom[index], |
||
148 | *(float *)&eeprom[index+4], |
||
149 | *(float *)&eeprom[index+8], |
||
150 | *(float *)&eeprom[index+12], |
||
151 | *(float *)&eeprom[index+16], |
||
152 | *(float *)&eeprom[index+20]); |
||
153 | break; |
||
154 | case AP_PARAM_MATRIX3F: |
||
155 | printf("%f %f %f %f %f %f %f %f %f\n", |
||
156 | *(float *)&eeprom[index], |
||
157 | *(float *)&eeprom[index+4], |
||
158 | *(float *)&eeprom[index+8], |
||
159 | *(float *)&eeprom[index+12], |
||
160 | *(float *)&eeprom[index+16], |
||
161 | *(float *)&eeprom[index+20], |
||
162 | *(float *)&eeprom[index+24], |
||
163 | *(float *)&eeprom[index+28], |
||
164 | *(float *)&eeprom[index+32]); |
||
165 | break; |
||
166 | default: |
||
167 | printf("NONE\n"); |
||
168 | break; |
||
169 | } |
||
170 | for (i = 0; i < size; i++) { |
||
171 | printf(" %02x", eeprom[index + i]); |
||
172 | } |
||
173 | printf("\n"); |
||
174 | index += size; |
||
175 | if (index >= sizeof(eeprom)) { |
||
176 | fflush(stdout); |
||
177 | fail("missing end sentinel"); |
||
178 | } |
||
179 | } |
||
180 | return 0; |
||
181 | } |