FreeCalypso > hg > freecalypso-hwlab
annotate ee2232/ee2232-prog.c @ 105:b1bf0ec6fff5
fc-simtool: pb-dump: first round of bugfixes
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 25 Jan 2021 05:59:33 +0000 |
parents | a85b6b7398bc |
children |
rev | line source |
---|---|
4
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <ctype.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <string.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <strings.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <stdio.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdlib.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <unistd.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <ftdi.h> |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 char *device_selector = "i:0x0403:0x6010"; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 unsigned eeprom_size = 64; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 u_short eeprom[256]; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 int erase; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 process_cmdline(argc, argv) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 char **argv; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 int c; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 extern char *optarg; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 while ((c = getopt(argc, argv, "d:et:")) != EOF) { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 switch (c) { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 case 'd': |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 device_selector = optarg; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 continue; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 case 'e': |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 erase = 1; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 continue; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 case 't': |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if (!strcmp(optarg, "46")) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 eeprom_size = 64; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 else if (!strcmp(optarg, "56")) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 eeprom_size = 128; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 else if (!strcmp(optarg, "66")) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 eeprom_size = 256; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 else { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 fprintf(stderr, |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 "error: -t option invalid value \"%s\"\n", |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 optarg); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 exit(1); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 continue; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 default: |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 /* error msg already printed */ |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 exit(1); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 read_eeprom_from_stdin() |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 unsigned n, off; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 int rc; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 for (n = 0; n < eeprom_size; n += 8) { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off, |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 eeprom + n, eeprom + n + 1, eeprom + n + 2, |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5, |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 eeprom + n + 6, eeprom + n + 7); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (rc != 9 || off != n * 2) { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fprintf(stderr, |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 "ee2232-prog error: invalid input on stdin\n"); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 exit(1); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 main(argc, argv) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 char **argv; |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 { |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 struct ftdi_context ftdi; |
5
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
72 unsigned n; |
4
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 process_cmdline(argc, argv); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (erase) |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 memset(eeprom, 0xFF, eeprom_size * 2); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 else |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 read_eeprom_from_stdin(); |
5
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
79 ftdi_init(&ftdi); |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
80 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) { |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
81 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
82 exit(1); |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
83 } |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
84 for (n = 0; n < eeprom_size; n++) { |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
85 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) { |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
86 fprintf(stderr, "EEPROM write error: %s\n", |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
87 ftdi.error_str); |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
88 exit(1); |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
89 } |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
90 } |
a85b6b7398bc
ee2232-prog: actual EEPROM write implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
4
diff
changeset
|
91 ftdi_usb_close(&ftdi); |
4
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 exit(0); |
02ea5dbdf84b
ee2232-prog: input parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 } |