annotate cp2102/read_eeprom_main.c @ 95:a378bf95c26c

cp2102-read-eeprom: write the Intel HEX image into a file
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 28 Sep 2023 02:19:39 +0000
parents d4d3531d342a
children 8d35346f1d46
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
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 u_char eeprom[SIZE_EEPROM];
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 main(argc, argv)
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 char **argv;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 struct usb_device *dev;
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 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
25 FILE *outf;
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
95
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
27 if (argc != 3) {
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
28 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
29 argv[0]);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 dev = find_usbdev_by_desc_string(argv[1]);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (!dev) {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 fprintf(stderr, "error: specified USB device not found\n");
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 usbh = usb_open(dev);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (!usbh) {
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 fprintf(stderr, "error: usb_open() failed\n");
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 exit(1);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 read_eeprom(usbh);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 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
44 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
45 if (!outf) {
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
46 perror(argv[2]);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
47 exit(1);
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
48 }
a378bf95c26c cp2102-read-eeprom: write the Intel HEX image into a file
Mychaela Falconia <falcon@freecalypso.org>
parents: 53
diff changeset
49 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
50 fclose(outf);
53
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(0);
d4d3531d342a cp2102-read-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }