comparison fteeprom/fteeprom-read.c @ 31:16b625911e19

fteeprom: generalization of previous ee2232 tools
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Apr 2019 04:14:00 +0000
parents
children c4b9026c8875
comparison
equal deleted inserted replaced
30:56780c191b58 31:16b625911e19
1 #include <sys/types.h>
2 #include <string.h>
3 #include <strings.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <ftdi.h>
8
9 char *device_selector;
10 unsigned eeprom_size = 64;
11
12 process_cmdline(argc, argv)
13 char **argv;
14 {
15 int c;
16 extern int optind;
17 extern char *optarg;
18
19 while ((c = getopt(argc, argv, "t:")) != EOF) {
20 switch (c) {
21 case 't':
22 if (!strcmp(optarg, "46"))
23 eeprom_size = 64;
24 else if (!strcmp(optarg, "56"))
25 eeprom_size = 128;
26 else if (!strcmp(optarg, "66"))
27 eeprom_size = 256;
28 else {
29 fprintf(stderr,
30 "error: -t option invalid value \"%s\"\n",
31 optarg);
32 exit(1);
33 }
34 continue;
35 default:
36 /* error msg already printed */
37 exit(1);
38 }
39 }
40 if (argc != optind + 1) {
41 fprintf(stderr, "usage: %s [options] device-selector\n",
42 argv[0]);
43 exit(1);
44 }
45 device_selector = argv[optind];
46 }
47
48 main(argc, argv)
49 char **argv;
50 {
51 struct ftdi_context ftdi;
52 u_short word;
53 unsigned n, col;
54
55 process_cmdline(argc, argv);
56 ftdi_init(&ftdi);
57 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
58 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
59 exit(1);
60 }
61 for (n = 0; n < eeprom_size; n++) {
62 if (ftdi_read_eeprom_location(&ftdi, n, &word) < 0) {
63 fprintf(stderr, "EEPROM read error: %s\n",
64 ftdi.error_str);
65 exit(1);
66 }
67 col = n & 7;
68 if (col == 0)
69 printf("%02X:", n * 2);
70 printf(" %04X", word);
71 if (col == 7)
72 putchar('\n');
73 }
74 ftdi_usb_close(&ftdi);
75 exit(0);
76 }