C8051F32x firmware infrastructure
Sign in or create your account | Project List | Help
C8051F32x firmware infrastructure Commit Details
Date: | 2010-11-07 05:52:19 (12 years 10 months ago) |
---|---|
Author: | Werner Almesberger |
Commit: | 88fa3ad057bf23094182d01e2947b8078e81e7db |
Message: | Improved chip identification, added C8051F34x support, some cleanup. - f32x/f32x.c (identify_chip, identify, main): identify the chip and print its name - f32x/f32x.c (identify): exit if communication is unstable already during chip identification - f32x/f32x.c (chips): added the C8051F34x - f32x/f32x.c (determine_layout, main): determine memory layout based on flash size - f32x/f32x.c (usage, main): new option -k flash_kb to set/override the flash size - f32x/f32x.c (LOCK_BYTE, protect, need_layout, main): use memory layout determined at run-time instead of hard-coded layout - f32x/f32x.c (main): throw an error of -p is used without flashing - f32x/f32x.c: added section titles and rearranged the order of some functions |
Files: |
f32x/f32x.c (8 diffs) |
Change Details
f32x/f32x.c | ||
---|---|---|
23 | 23 | #include "boundary.h" |
24 | 24 | |
25 | 25 | |
26 | #define LOCK_BYTE 0x3dff | |
26 | struct chip { | |
27 | const char *name; /* NULL for end of list */ | |
28 | uint8_t dev_id; | |
29 | int max_flash_kb; /* maximum flash size, in kB */ | |
30 | ||
31 | }; | |
32 | ||
33 | struct mem_layout { | |
34 | int flash_kb; /* real flash size, in kB. 0 for end of list */ | |
35 | uint16_t lock_byte; /* location of lock byte */ | |
36 | }; | |
37 | ||
38 | static struct chip chips[] = { | |
39 | { "C8051F320/1", 0x09, 16 }, | |
40 | { "C8051F326/7", 0x0d, 16 }, | |
41 | { "C8051F34x", 0x0f, 64 }, | |
42 | { NULL, } | |
43 | }; | |
44 | ||
45 | static struct mem_layout mem_layouts[] = { | |
46 | { 16, 0x3dff }, | |
47 | { 32, 0x7fff }, | |
48 | { 64, 0xfbff }, | |
49 | { 0, } | |
50 | }; | |
51 | ||
52 | static const struct chip *chip = NULL; | |
53 | static const struct mem_layout *layout = NULL; | |
27 | 54 | |
28 | 55 | |
29 | 56 | static size_t file_size; |
30 | 57 | |
31 | 58 | |
59 | /* ----- flash ------------------------------------------------------------- */ | |
60 | ||
61 | ||
32 | 62 | static void dump(const char *title, void *data, size_t size) |
33 | 63 | { |
34 | 64 | int i, j; |
... | ... | |
80 | 110 | } |
81 | 111 | |
82 | 112 | |
113 | static void do_flash(const char *name) | |
114 | { | |
115 | FILE *file; | |
116 | uint8_t code[16384]; | |
117 | ||
118 | file = fopen(name, "r"); | |
119 | if (!file) { | |
120 | perror(name); | |
121 | exit(1); | |
122 | } | |
123 | file_size = fread(code, 1, sizeof(code), file); | |
124 | (void) fclose(file); | |
125 | flash_device(code, file_size); | |
126 | } | |
127 | ||
128 | ||
129 | /* ----- protect ----------------------------------------------------------- */ | |
130 | ||
131 | ||
132 | static void protect(void) | |
133 | { | |
134 | uint8_t pages, lock_byte; | |
135 | ||
136 | pages = (file_size+511) >> 9; | |
137 | printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); | |
138 | lock_byte = ~pages; | |
139 | flash_block_write(layout->lock_byte, &lock_byte, 1); | |
140 | } | |
141 | ||
142 | ||
143 | /* ----- erase ------------------------------------------------------------- */ | |
144 | ||
145 | ||
83 | 146 | static void erase_flash(void) |
84 | 147 | { |
85 | 148 | flash_init(); |
... | ... | |
87 | 150 | } |
88 | 151 | |
89 | 152 | |
153 | /* ----- dump -------------------------------------------------------------- */ | |
154 | ||
155 | ||
90 | 156 | static void dump_flash(size_t size) |
91 | 157 | { |
92 | 158 | int i, j; |
... | ... | |
119 | 185 | } |
120 | 186 | |
121 | 187 | |
188 | /* ----- identify ---------------------------------------------------------- */ | |
189 | ||
190 | ||
191 | static void identify_chip(uint8_t dev) | |
192 | { | |
193 | const struct chip *p; | |
194 | ||
195 | for (p = chips; p->name; p++) | |
196 | if (p->dev_id == dev) { | |
197 | chip = p; | |
198 | return; | |
199 | } | |
200 | } | |
201 | ||
202 | ||
122 | 203 | static void identify(void) |
123 | 204 | { |
124 | int i; | |
205 | int i, same = 1; | |
206 | uint8_t dev, same_dev; | |
125 | 207 | |
126 | 208 | c2_addr_write(0); |
127 | printf("Dev"); | |
128 | for (i = 0; i != 10; i++) | |
129 | printf(" 0x%02x", c2_data_read(1)); | |
209 | fprintf(stderr, "Dev"); | |
210 | for (i = 0; i != 10; i++) { | |
211 | dev = c2_data_read(1); | |
212 | fprintf(stderr, " 0x%02x", dev); | |
213 | if (!i) | |
214 | same_dev = dev; | |
215 | else { | |
216 | if (same_dev != dev) | |
217 | same = 0; | |
218 | } | |
219 | } | |
220 | if (!same) { | |
221 | fprintf(stderr, "\ncommunication error\n"); | |
222 | exit(1); | |
223 | } | |
224 | identify_chip(same_dev); | |
225 | ||
130 | 226 | c2_addr_write(1); |
131 | printf("\nRev"); | |
227 | fprintf(stderr, "\nRev"); | |
132 | 228 | for (i = 0; i != 10; i++) |
133 | printf(" 0x%02x", c2_data_read(1)); | |
134 | printf("\n"); | |
229 | fprintf(stderr, " 0x%02x", c2_data_read(1)); | |
230 | fprintf(stderr, "\n"); | |
135 | 231 | } |
136 | 232 | |
137 | 233 | |
138 | static void do_flash(const char *name) | |
234 | static void determine_layout(int kb) | |
139 | 235 | { |
140 | FILE *file; | |
141 | uint8_t code[16384]; | |
236 | if (kb) { | |
237 | if (chip && kb > chip->max_flash_kb) { | |
238 | fprintf(stderr, | |
239 | "chip has only %d kB of flash\n", | |
240 | chip->max_flash_kb); | |
241 | exit(1); | |
242 | } | |
243 | } else { | |
244 | if (chip) | |
245 | kb = chip->max_flash_kb; | |
246 | } | |
142 | 247 | |
143 | file = fopen(name, "r"); | |
144 | if (!file) { | |
145 | perror(name); | |
248 | if (!kb) | |
249 | return; | |
250 | for (layout = mem_layouts; layout->flash_kb; layout++) | |
251 | if (layout->flash_kb == kb) | |
252 | break; | |
253 | if (!layout->flash_kb) { | |
254 | fprintf(stderr, "no memory layout found for %d kB\n", kb); | |
146 | 255 | exit(1); |
147 | 256 | } |
148 | file_size = fread(code, 1, sizeof(code), file); | |
149 | (void) fclose(file); | |
150 | flash_device(code, file_size); | |
151 | 257 | } |
152 | 258 | |
153 | 259 | |
154 | static void protect(void) | |
155 | { | |
156 | uint8_t pages, lock_byte; | |
157 | ||
158 | pages = (file_size+511) >> 9; | |
159 | printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); | |
160 | lock_byte = ~pages; | |
161 | flash_block_write(LOCK_BYTE, &lock_byte, 1); | |
162 | } | |
260 | /* ----- main -------------------------------------------------------------- */ | |
163 | 261 | |
164 | 262 | |
165 | 263 | static void usage(const char *name) |
166 | 264 | { |
167 | 265 | fprintf(stderr, |
168 | "usage: %s [-n] [-p] file\n" | |
169 | " %s [-n] -d\n" | |
266 | "usage: %s [-n] [-p] [-k flash_kb] file\n" | |
267 | " %s [-n] [-k flash_kb] -d\n" | |
170 | 268 | " %s [-n] -e\n" |
171 | 269 | " %s [-n] -b pin_setup\n" |
172 | 270 | " %s [-n]\n\n" |
... | ... | |
176 | 274 | " Pins can be set to 0, 1, or R (pull-up). Dots can be used to structure\n" |
177 | 275 | " the bit string. Prints what the pins read back (0 or 1) in the same\n" |
178 | 276 | " order, with a dot between P0 and P2.\n" |
179 | " -d dump Flash content\n" | |
180 | " -e erase whole Flash\n" | |
181 | " -n do not provide power to the target (default: do provide power)\n" | |
182 | " -p protect the data after writing\n" | |
277 | " -d dump Flash content\n" | |
278 | " -e erase whole Flash\n" | |
279 | " -k flash_kb set flash size (16/32/64; default: auto-select)\n" | |
280 | " -n do not provide power to the target (default: do provide power)\n" | |
281 | " -p protect the data after writing\n" | |
183 | 282 | "Invocation without argument resets the F32x.\n" |
184 | 283 | , name, name, name, name, name); |
185 | 284 | exit(1); |
186 | 285 | } |
187 | 286 | |
188 | 287 | |
288 | static void need_layout(void) | |
289 | { | |
290 | if (layout) | |
291 | return; | |
292 | fprintf(stderr, "please specify flash size with -k flash_kB\n"); | |
293 | exit(1); | |
294 | } | |
295 | ||
296 | ||
189 | 297 | int main(int argc, char **argv) |
190 | 298 | { |
191 | 299 | enum { |
... | ... | |
195 | 303 | mode_erase, |
196 | 304 | mode_scan, |
197 | 305 | } mode = mode_default; |
198 | int do_protect = 0, power = 1; | |
306 | int do_protect = 0, power = 1, kb = 0; | |
199 | 307 | int c; |
200 | 308 | |
201 | while ((c = getopt(argc, argv, "bdenp")) != EOF) | |
309 | while ((c = getopt(argc, argv, "bdek:np")) != EOF) | |
202 | 310 | switch (c) { |
203 | 311 | case 'd': |
204 | 312 | if (mode) |
... | ... | |
215 | 323 | usage(*argv); |
216 | 324 | mode = mode_scan;; |
217 | 325 | break; |
326 | case 'k': | |
327 | kb = atoi(optarg); | |
328 | break; | |
218 | 329 | case 'n': |
219 | 330 | power = 0; |
220 | 331 | break; |
... | ... | |
246 | 357 | usage(*argv); |
247 | 358 | break; |
248 | 359 | } |
360 | if (do_protect && mode != mode_flash) | |
361 | usage(*argv); | |
249 | 362 | |
250 | 363 | c2_init(power); |
251 | 364 | identify(); |
365 | determine_layout(kb); | |
366 | ||
367 | if (chip) | |
368 | fprintf(stderr, "%s, ", chip->name); | |
369 | else | |
370 | fprintf(stderr, "unknown chip, "); | |
371 | if (layout) | |
372 | fprintf(stderr, "flash size: %d kB\n", layout->flash_kb); | |
373 | else | |
374 | fprintf(stderr, "flash size unknown\n"); | |
252 | 375 | |
253 | 376 | switch (mode) { |
254 | 377 | case mode_default: |
255 | 378 | /* just reset */ |
256 | 379 | break; |
257 | 380 | case mode_dump: |
258 | dump_flash(0x4000); | |
381 | need_layout(); | |
382 | dump_flash(layout->lock_byte+2); | |
259 | 383 | break; |
260 | 384 | case mode_erase: |
261 | 385 | erase_flash(); |
262 | 386 | break; |
263 | 387 | case mode_flash: |
388 | if (do_protect) | |
389 | need_layout(); | |
264 | 390 | do_flash(argv[optind]); |
265 | 391 | if (do_protect) |
266 | 392 | protect(); |
Branches:
master