annotate cp2102/read_eeprom_main.c @ 101:ba741a1af0e3

doc/CP2102-EEPROM-tools: article started
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 06:35:39 +0000
parents 1cacc1ae56f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program locates a CP2102 device via libusb, reads its internal
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
3 * 1024-byte EEPROM and writes (into a file) an image of this EEPROM
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * in the same Intel HEX format as used by cp210x-program-1.0,
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * the Python-language tool from 2014.
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <unistd.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <usb.h>
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../libuwrap/find_dev.h"
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include "cp210x_defs.h"
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
18 extern struct usb_device *find_cp2102_device();
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
19
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 u_char eeprom[SIZE_EEPROM];
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
21 char *device_selector, *outfilename;
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 process_cmdline(argc, argv)
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
24 char **argv;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
25 {
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
26 int c;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
27 extern int optind;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
28 extern char *optarg;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
29
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:")) != EOF) {
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
31 switch (c) {
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;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
35 default:
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
36 usage:
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
37 fprintf(stderr,
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
38 "usage: %s [-d device-selector] output-hex-file\n",
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
39 argv[0]);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
40 exit(1);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
41 }
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
42 }
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
43 if (argc != optind + 1)
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
44 goto usage;
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
45 outfilename = argv[optind];
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
46 }
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 main(argc, argv)
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 char **argv;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 struct usb_device *dev;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 usb_dev_handle *usbh;
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
53 FILE *outf;
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
55 process_cmdline(argc, argv);
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
56 dev = find_cp2102_device(device_selector);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 usbh = usb_open(dev);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (!usbh) {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 fprintf(stderr, "error: usb_open() failed\n");
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 read_eeprom(usbh);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 usb_close(usbh);
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
64 outf = fopen(outfilename, "w");
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
65 if (!outf) {
98
1cacc1ae56f0 cp2102 tools: convert to -d option for non-default device
Mychaela Falconia <falcon@freecalypso.org>
parents: 97
diff changeset
66 perror(outfilename);
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
67 exit(1);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
68 }
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
69 intel_hex_out(eeprom, outf);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
70 fclose(outf);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 exit(0);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }