annotate fteeprom/fteeprom-read.c @ 40:d150d4704ff5

fteeprom: ftee-gen2232h program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 22 Apr 2019 00:05:32 +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 <string.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <strings.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <stdio.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdlib.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <unistd.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ftdi.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 char *device_selector;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 unsigned eeprom_size = 64;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 process_cmdline(argc, argv)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 char **argv;
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 int c;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern int optind;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern char *optarg;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 while ((c = getopt(argc, argv, "t:")) != EOF) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 switch (c) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 case 't':
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 if (!strcmp(optarg, "46"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 eeprom_size = 64;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 else if (!strcmp(optarg, "56"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 eeprom_size = 128;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 else if (!strcmp(optarg, "66"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 eeprom_size = 256;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 else {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 fprintf(stderr,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 "error: -t option invalid value \"%s\"\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 optarg);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 continue;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 default:
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /* error msg already printed */
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
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 if (argc != optind + 1) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 fprintf(stderr, "usage: %s [options] device-selector\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 argv[0]);
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 device_selector = argv[optind];
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
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 main(argc, argv)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 char **argv;
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 struct ftdi_context ftdi;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 u_short word;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 unsigned n, col;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 process_cmdline(argc, argv);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 ftdi_init(&ftdi);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 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
59 exit(1);
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 for (n = 0; n < eeprom_size; n++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 if (ftdi_read_eeprom_location(&ftdi, n, &word) < 0) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 fprintf(stderr, "EEPROM read error: %s\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 ftdi.error_str);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 col = n & 7;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (col == 0)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 printf("%02X:", n * 2);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 printf(" %04X", word);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (col == 7)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 putchar('\n');
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 ftdi_usb_close(&ftdi);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(0);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }