FreeCalypso > hg > freecalypso-hwlab
annotate fteeprom/fteeprom-prog.c @ 52:ace3ed1d5ddf
Unbuffered FT2232x JTAG article written
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 23 Apr 2019 01:22:41 +0000 |
parents | 2c092eb1621b |
children | 47dbfd66bd58 |
rev | line source |
---|---|
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <ctype.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <string.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <strings.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdio.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdlib.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <unistd.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <ftdi.h> |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
10 unsigned eeprom_size; |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 u_short eeprom[256]; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
13 read_eeprom_common(inf, filename_for_errs) |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
14 FILE *inf; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
15 char *filename_for_errs; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
16 { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
17 char linebuf[1024], *cp; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
18 int lineno, rc; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
19 unsigned ptr = 0, input_off; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
20 |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
21 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
22 for (cp = linebuf; isspace(*cp); cp++) |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
23 ; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
24 if (*cp == '\0' || *cp == '#') |
42
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
25 continue; |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
26 if (ptr >= 256) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
27 fprintf(stderr, |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
28 "%s line %d: maximum EEPROM size exceeded\n", |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
29 filename_for_errs, lineno); |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
32 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx", |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
33 &input_off, eeprom + ptr, eeprom + ptr + 1, |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
34 eeprom + ptr + 2, eeprom + ptr + 3, |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
35 eeprom + ptr + 4, eeprom + ptr + 5, |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
36 eeprom + ptr + 6, eeprom + ptr + 7); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
37 if (rc != 9 || input_off != ptr * 2) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
38 fprintf(stderr, "%s line %d: invalid input\n", |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
39 filename_for_errs, lineno); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
40 exit(1); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
41 } |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
42 ptr += 8; |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
44 if (ptr != 64 && ptr != 128 && ptr != 256) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
45 fprintf(stderr, "%s: not an EEPROM image of recognized size\n", |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
46 filename_for_errs); |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
49 eeprom_size = ptr; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
50 } |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
51 |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
52 read_eeprom_from_file(filename) |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
53 char *filename; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
54 { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
55 FILE *inf; |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
56 |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
57 inf = fopen(filename, "r"); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
58 if (!inf) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
59 perror(filename); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
60 exit(1); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
61 } |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
62 read_eeprom_common(inf, filename); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
63 fclose(inf); |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 read_eeprom_from_stdin() |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 { |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
68 read_eeprom_common(stdin, "stdin"); |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 main(argc, argv) |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 char **argv; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 struct ftdi_context ftdi; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 unsigned n; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
77 if (argc < 2 || argc > 3) { |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
78 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n", |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
79 argv[0]); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
80 exit(1); |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
81 } |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
82 if (argv[2]) |
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
83 read_eeprom_from_file(argv[2]); |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 else |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 read_eeprom_from_stdin(); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 ftdi_init(&ftdi); |
47
2c092eb1621b
fteeprom-prog revamped: read from file or stdin, comments allowed,
Mychaela Falconia <falcon@freecalypso.org>
parents:
42
diff
changeset
|
87 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) { |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 for (n = 0; n < eeprom_size; n++) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 fprintf(stderr, "EEPROM write error: %s\n", |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 ftdi.error_str); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 ftdi_usb_close(&ftdi); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 exit(0); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 } |