annotate fteeprom/fteeprom-prog.c @ 41:79cdd210abe1

eeproms/usb-com232-plus2: read out of an FTDI USB-COM232-PLUS2 board
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Apr 2019 00:19:13 +0000
parents 16b625911e19
children c4b9026c8875
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 extern char *optarg;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 while ((c = getopt(argc, argv, "et:")) != EOF) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 switch (c) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 case 'e':
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 erase = 1;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 continue;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 case 't':
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (!strcmp(optarg, "46"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 eeprom_size = 64;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 else if (!strcmp(optarg, "56"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 eeprom_size = 128;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 else if (!strcmp(optarg, "66"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 eeprom_size = 256;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 else {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 fprintf(stderr,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 "error: -t option invalid value \"%s\"\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 optarg);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 continue;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 default:
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 /* error msg already printed */
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
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 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (argc != optind + 1) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 fprintf(stderr, "usage: %s [options] device-selector\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 argv[0]);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 device_selector = argv[optind];
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 read_eeprom_from_stdin()
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 unsigned n, off;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 int rc;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 for (n = 0; n < eeprom_size; n += 8) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 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
61 eeprom + n, eeprom + n + 1, eeprom + n + 2,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 eeprom + n + 6, eeprom + n + 7);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (rc != 9 || off != n * 2) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 fprintf(stderr,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 "ee2232-prog error: invalid input on stdin\n");
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 exit(1);
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 }
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
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 main(argc, argv)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 char **argv;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 struct ftdi_context ftdi;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 unsigned n;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 process_cmdline(argc, argv);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (erase)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 memset(eeprom, 0xFF, eeprom_size * 2);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 else
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 read_eeprom_from_stdin();
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 ftdi_init(&ftdi);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 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
86 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 for (n = 0; n < eeprom_size; n++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 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
90 fprintf(stderr, "EEPROM write error: %s\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 ftdi.error_str);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 ftdi_usb_close(&ftdi);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(0);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }