annotate fteeprom/fteeprom-prog.c @ 40:f5847be43d35

fc-duart28-conf: bump off both ttyUSB devices on set operation
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 10 Sep 2023 21:13:58 +0000
parents 7e6dcceb5ee8
children 940cde8a99b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
1 /*
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
2 * This program connects to an FTDI chip via libusb and programs its EEPROM
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
3 * with a raw hex image read from stdin or from a backup file.
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
4 *
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
5 * The present version has been converted to use our local libraries
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
6 * (libftmini and libuwrap) instead of libftdi - thus the external dependency
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
7 * is only libusb.
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
8 */
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
9
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/types.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <ctype.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdio.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <stdlib.h>
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <unistd.h>
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
17 #include <usb.h>
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
18 #include "../libuwrap/find_dev.h"
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
19 #include "../libuwrap/open_close.h"
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
20 #include "../libftmini/eeprom_func.h"
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 unsigned eeprom_size;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 u_short eeprom[256];
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 read_eeprom_common(inf, filename_for_errs)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 FILE *inf;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 char *filename_for_errs;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 char linebuf[1024], *cp;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 int lineno, rc;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 unsigned ptr = 0, input_off;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 for (cp = linebuf; isspace(*cp); cp++)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 ;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (*cp == '\0' || *cp == '#')
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (ptr >= 256) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 fprintf(stderr,
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 "%s line %d: maximum EEPROM size exceeded\n",
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 filename_for_errs, lineno);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 &input_off, eeprom + ptr, eeprom + ptr + 1,
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 eeprom + ptr + 2, eeprom + ptr + 3,
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 eeprom + ptr + 4, eeprom + ptr + 5,
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 eeprom + ptr + 6, eeprom + ptr + 7);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (rc != 9 || input_off != ptr * 2) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 fprintf(stderr, "%s line %d: invalid input\n",
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 filename_for_errs, lineno);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 ptr += 8;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (ptr != 64 && ptr != 128 && ptr != 256) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 filename_for_errs);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 eeprom_size = ptr;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 read_eeprom_from_file(filename)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 char *filename;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 FILE *inf;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 inf = fopen(filename, "r");
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (!inf) {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 perror(filename);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 read_eeprom_common(inf, filename);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fclose(inf);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 read_eeprom_from_stdin()
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 {
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 read_eeprom_common(stdin, "stdin");
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 main(argc, argv)
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 char **argv;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 {
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
86 char **ap, *devsel;
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
87 struct usb_device *dev;
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
88 usb_dev_handle *usbh;
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
89 int ft232r_mode = 0;
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 unsigned n;
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
92 if (argc < 2) {
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
93 usage: fprintf(stderr,
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
94 "usage: %s [-r] device-selector [eeprom-image]\n",
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 argv[0]);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
98 ap = argv + 1;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
99 if (!strcmp(*ap, "-r")) {
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
100 ft232r_mode = 1;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
101 ap++;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
102 }
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
103 if (!*ap)
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
104 goto usage;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
105 devsel = *ap++;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
106 if (*ap) {
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
107 if (ap[1])
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
108 goto usage;
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
109 read_eeprom_from_file(*ap);
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
110 } else
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 read_eeprom_from_stdin();
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
112 dev = find_usbdev_by_desc_string(devsel);
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
113 if (!dev) {
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
114 fprintf(stderr, "error: specified USB device not found\n");
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 exit(1);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 }
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
117 usbh = usbwrap_open_dev(dev, 1);
23
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
118 if (ft232r_mode)
7e6dcceb5ee8 fteeprom-prog: implement -r option for FT232R mode
Mychaela Falconia <falcon@freecalypso.org>
parents: 20
diff changeset
119 ft232r_eeprom_magic(usbh);
20
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
120 for (n = 0; n < eeprom_size; n++)
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
121 ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
43b8e88dae02 fteeprom-prog: convert to new local libs
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
122 usbwrap_close_dev(usbh);
0
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 exit(0);
11b8a30333b3 fteeprom: initial import from freecalypso-hwlab
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 }