FreeCalypso > hg > fc-usbser-tools
annotate cp2102/write_eeprom_main.c @ 96:c6d04771bf6a
cp2102-write-eeprom program written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 28 Sep 2023 02:38:59 +0000 |
parents | |
children | 8d35346f1d46 |
rev | line source |
---|---|
96
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This program reads a CP2102 EEPROM image from an Intel HEX file |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * and writes it into a physical CP2102 USB device. |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <string.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <strings.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <unistd.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <usb.h> |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include "../libuwrap/find_dev.h" |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include "../libuwrap/open_close.h" |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include "cp210x_defs.h" |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 u_char eeprom[SIZE_EEPROM]; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 char *device_selector, *input_filename; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 int no_detach; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 process_cmdline(argc, argv) |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 char **argv; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 int c; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 extern int optind; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 extern char *optarg; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 while ((c = getopt(argc, argv, "n")) != EOF) { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 switch (c) { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 case 'n': |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 no_detach = 1; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 continue; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 default: |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 /* error msg already printed */ |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 exit(1); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 if (argc != optind + 2) { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 fprintf(stderr, |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 "usage: %s [options] device-selector eeprom-image\n", |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 argv[0]); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 exit(1); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 device_selector = argv[optind]; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 input_filename = argv[optind + 1]; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 main(argc, argv) |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 char **argv; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 struct usb_device *dev; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 usb_dev_handle *usbh; |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 process_cmdline(argc, argv); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 read_intel_hex(input_filename); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 dev = find_usbdev_by_desc_string(device_selector); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 if (!dev) { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 fprintf(stderr, "error: specified USB device not found\n"); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 exit(1); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 usbh = usb_open(dev); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (!usbh) { |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 fprintf(stderr, "error: usb_open() failed\n"); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 exit(1); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 if (!no_detach) |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 usbwrap_claim_all_ifs(usbh); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 write_eeprom(usbh); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 usb_close(usbh); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 exit(0); |
c6d04771bf6a
cp2102-write-eeprom program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 } |