annotate cp2102/read_partno.c @ 102:4f72e09fb698

doc/CP2102-EEPROM-tools: complete for now
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 21:13:33 +0000
parents 1cacc1ae56f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program locates a presumed-CP2102 device via libusb and reads its
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * vendor-specific part number register, seeking to distinguish between
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * the original CP2102 and the later CP2102N.
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <unistd.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <usb.h>
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libuwrap/find_dev.h"
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "cp210x_defs.h"
a5c4a82d01ab cp2102-read-partno 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: 51
diff changeset
17 extern struct usb_device *find_cp2102_device();
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
18 char *device_selector;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
19
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
20 process_cmdline(argc, argv)
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
21 char **argv;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
22 {
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
23 int c;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
24 extern int optind;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
25 extern char *optarg;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
26
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
27 while ((c = getopt(argc, argv, "d:")) != EOF) {
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
28 switch (c) {
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
29 case 'd':
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
30 device_selector = optarg;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
31 continue;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
32 default:
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
33 usage:
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
34 fprintf(stderr, "usage: %s [-d device-selector]\n",
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
35 argv[0]);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
36 exit(1);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
37 }
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
38 }
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
39 if (argc != optind)
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
40 goto usage;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
41 }
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 51
diff changeset
42
50
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 main(argc, argv)
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 char **argv;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 struct usb_device *dev;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 usb_dev_handle *usbh;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 u_char partno;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 int rc;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
51 process_cmdline(argc, argv);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
52 dev = find_cp2102_device(device_selector);
50
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 usbh = usb_open(dev);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (!usbh) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 fprintf(stderr, "error: usb_open() failed\n");
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
51
9a15eb300ab0 cp2102-read-partno works
Mychaela Falconia <falcon@freecalypso.org>
parents: 50
diff changeset
58 rc = usb_control_msg(usbh, DEVICE_IN_REQTYPE, CP210x_CONFIG,
9a15eb300ab0 cp2102-read-partno works
Mychaela Falconia <falcon@freecalypso.org>
parents: 50
diff changeset
59 REG_PART_NUMBER, 0, (char *) &partno, 1,
50
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 USB_READ_TIMEOUT);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (rc != 1) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 fprintf(stderr,
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 "REG_PART_NUMBER read error: usb_control_msg() returned %d\n",
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 rc);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 usb_close(usbh);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 printf("0x%02X\n", partno);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 exit(0);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }