annotate cp2102/read_partno.c @ 65:225dc1d9f2f1

ftee-decode program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 12 Sep 2023 22:23:30 +0000
parents 9a15eb300ab0
children 8d35346f1d46
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
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 main(argc, argv)
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 char **argv;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 struct usb_device *dev;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 usb_dev_handle *usbh;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 u_char partno;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int rc;
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 if (argc != 2) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 fprintf(stderr, "usage: %s device-selector\n", argv[0]);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 dev = find_usbdev_by_desc_string(argv[1]);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (!dev) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fprintf(stderr, "error: specified USB device not found\n");
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 usbh = usb_open(dev);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (!usbh) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 fprintf(stderr, "error: usb_open() failed\n");
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
51
9a15eb300ab0 cp2102-read-partno works
Mychaela Falconia <falcon@freecalypso.org>
parents: 50
diff changeset
39 rc = usb_control_msg(usbh, DEVICE_IN_REQTYPE, CP210x_CONFIG,
9a15eb300ab0 cp2102-read-partno works
Mychaela Falconia <falcon@freecalypso.org>
parents: 50
diff changeset
40 REG_PART_NUMBER, 0, (char *) &partno, 1,
50
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 USB_READ_TIMEOUT);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (rc != 1) {
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 fprintf(stderr,
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 "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
45 rc);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 exit(1);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 usb_close(usbh);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 printf("0x%02X\n", partno);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 exit(0);
a5c4a82d01ab cp2102-read-partno program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 }