FreeCalypso > hg > freecalypso-hwlab
annotate fteeprom/fteeprom-prog.c @ 42:c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 22 Apr 2019 01:17:07 +0000 |
parents | 16b625911e19 |
children | 2c092eb1621b |
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 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 char *device_selector; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 unsigned eeprom_size = 64; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 u_short eeprom[256]; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 int erase; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 process_cmdline(argc, argv) |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 char **argv; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 int c; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 extern int optind; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
42
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
21 while ((c = getopt(argc, argv, "bBe")) != EOF) { |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 switch (c) { |
42
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
23 case 'b': |
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
24 eeprom_size = 128; |
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
25 continue; |
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
26 case 'B': |
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
27 eeprom_size = 256; |
c4b9026c8875
fteeprom-{prog,read}: -t option replaced with -b and -B
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
28 continue; |
31
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 case 'e': |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 erase = 1; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 continue; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 default: |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 /* error msg already printed */ |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 if (argc != optind + 1) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 fprintf(stderr, "usage: %s [options] device-selector\n", |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 argv[0]); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 device_selector = argv[optind]; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 read_eeprom_from_stdin() |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 unsigned n, off; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 int rc; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 for (n = 0; n < eeprom_size; n += 8) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off, |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 eeprom + n, eeprom + n + 1, eeprom + n + 2, |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5, |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 eeprom + n + 6, eeprom + n + 7); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 if (rc != 9 || off != n * 2) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 fprintf(stderr, |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 "ee2232-prog error: invalid input on stdin\n"); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 main(argc, argv) |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 char **argv; |
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 struct ftdi_context ftdi; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 unsigned n; |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 process_cmdline(argc, argv); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 if (erase) |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 memset(eeprom, 0xFF, eeprom_size * 2); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 else |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 read_eeprom_from_stdin(); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 ftdi_init(&ftdi); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 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
|
77 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 for (n = 0; n < eeprom_size; n++) { |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 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
|
81 fprintf(stderr, "EEPROM write error: %s\n", |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 ftdi.error_str); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 exit(1); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 ftdi_usb_close(&ftdi); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 exit(0); |
16b625911e19
fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 } |