comparison fteeprom/fteeprom-prog.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 <ctype.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <ftdi.h>
9
10 char *device_selector;
11 unsigned eeprom_size = 64;
12 u_short eeprom[256];
13 int erase;
14
15 process_cmdline(argc, argv)
16 char **argv;
17 {
18 int c;
19 extern int optind;
20 extern char *optarg;
21
22 while ((c = getopt(argc, argv, "et:")) != EOF) {
23 switch (c) {
24 case 'e':
25 erase = 1;
26 continue;
27 case 't':
28 if (!strcmp(optarg, "46"))
29 eeprom_size = 64;
30 else if (!strcmp(optarg, "56"))
31 eeprom_size = 128;
32 else if (!strcmp(optarg, "66"))
33 eeprom_size = 256;
34 else {
35 fprintf(stderr,
36 "error: -t option invalid value \"%s\"\n",
37 optarg);
38 exit(1);
39 }
40 continue;
41 default:
42 /* error msg already printed */
43 exit(1);
44 }
45 }
46 if (argc != optind + 1) {
47 fprintf(stderr, "usage: %s [options] device-selector\n",
48 argv[0]);
49 exit(1);
50 }
51 device_selector = argv[optind];
52 }
53
54 read_eeprom_from_stdin()
55 {
56 unsigned n, off;
57 int rc;
58
59 for (n = 0; n < eeprom_size; n += 8) {
60 rc = scanf("%x: %hx %hx %hx %hx %hx %hx %hx %hx", &off,
61 eeprom + n, eeprom + n + 1, eeprom + n + 2,
62 eeprom + n + 3, eeprom + n + 4, eeprom + n + 5,
63 eeprom + n + 6, eeprom + n + 7);
64 if (rc != 9 || off != n * 2) {
65 fprintf(stderr,
66 "ee2232-prog error: invalid input on stdin\n");
67 exit(1);
68 }
69 }
70 }
71
72 main(argc, argv)
73 char **argv;
74 {
75 struct ftdi_context ftdi;
76 unsigned n;
77
78 process_cmdline(argc, argv);
79 if (erase)
80 memset(eeprom, 0xFF, eeprom_size * 2);
81 else
82 read_eeprom_from_stdin();
83 ftdi_init(&ftdi);
84 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) {
85 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
86 exit(1);
87 }
88 for (n = 0; n < eeprom_size; n++) {
89 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
90 fprintf(stderr, "EEPROM write error: %s\n",
91 ftdi.error_str);
92 exit(1);
93 }
94 }
95 ftdi_usb_close(&ftdi);
96 exit(0);
97 }