annotate cp2102/read_eeprom_main.c @ 97:8d35346f1d46

cp2102 tools: accept "default" as device-selector
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 03:26:11 +0000
parents a378bf95c26c
children 1cacc1ae56f0
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];
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 main(argc, argv)
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 char **argv;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 struct usb_device *dev;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 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
27 FILE *outf;
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
29 if (argc != 3) {
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
30 fprintf(stderr, "usage: %s device-selector output-hex-file\n",
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
31 argv[0]);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 }
97
8d35346f1d46 cp2102 tools: accept "default" as device-selector
Mychaela Falconia <falcon@freecalypso.org>
parents: 95
diff changeset
34 dev = find_cp2102_device(argv[1]);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 usbh = usb_open(dev);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (!usbh) {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "error: usb_open() failed\n");
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 read_eeprom(usbh);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 usb_close(usbh);
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
42 outf = fopen(argv[2], "w");
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
43 if (!outf) {
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
44 perror(argv[2]);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
45 exit(1);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
46 }
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
47 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
48 fclose(outf);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 exit(0);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }