annotate cp2102/write_eeprom_main.c @ 100:29bff463402e

doc/CP2102-EEPROM-format: part number byte confirmed
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 05:39:44 +0000
parents 1cacc1ae56f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads a CP2102 EEPROM image from an Intel HEX file
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and writes it into a physical CP2102 USB device.
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <strings.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <unistd.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <usb.h>
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "../libuwrap/find_dev.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libuwrap/open_close.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "cp210x_defs.h"
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
17 extern struct usb_device *find_cp2102_device();
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
18
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 u_char eeprom[SIZE_EEPROM];
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 char *device_selector, *input_filename;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 int no_detach;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 process_cmdline(argc, argv)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 char **argv;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int c;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 extern int optind;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 extern char *optarg;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
30 while ((c = getopt(argc, argv, "d:n")) != EOF) {
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 switch (c) {
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
32 case 'd':
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
33 device_selector = optarg;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
34 continue;
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 case 'n':
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 no_detach = 1;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 default:
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
39 usage:
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
40 fprintf(stderr, "usage: %s [hw-options] eeprom-image\n",
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
41 argv[0]);
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
45 if (argc != optind + 1)
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
46 goto usage;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
47 input_filename = argv[optind];
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 main(argc, argv)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 char **argv;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 struct usb_device *dev;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 usb_dev_handle *usbh;
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 process_cmdline(argc, argv);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 read_intel_hex(input_filename);
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 96
diff changeset
58 dev = find_cp2102_device(device_selector);
96
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 usbh = usb_open(dev);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (!usbh) {
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 fprintf(stderr, "error: usb_open() failed\n");
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 exit(1);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 if (!no_detach)
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 usbwrap_claim_all_ifs(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 write_eeprom(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 usb_close(usbh);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 exit(0);
c6d04771bf6a cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }