FreeCalypso > hg > fc-usbser-tools
annotate duart28/main.c @ 36:3794ad47a97f
doc: on second thought, let's not get into Unbuffered-FT2232x-JTAG
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 10 Sep 2023 01:31:05 +0000 |
parents | df284688d0c8 |
children | b9ecfa54fe2b |
rev | line source |
---|---|
27
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * Main module for fc-duart28-conf utility. |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <ctype.h> |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <string.h> |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <strings.h> |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <usb.h> |
29
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
11 #include "../libuwrap/open_close.h" |
27
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 extern struct usb_device *find_duart28_usbdev(); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 oper_find() |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 struct usb_device *dev; |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 dev = find_duart28_usbdev(); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 if (!dev) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 printf("No DUART28 adapter found\n"); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 return 0; |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 oper_check_eeprom() |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 { |
29
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
27 struct usb_device *dev; |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
28 usb_dev_handle *usbh; |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
29 |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
30 dev = find_duart28_usbdev(); |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
31 if (!dev) { |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
32 fprintf(stderr, "error: no DUART28 adapter found\n"); |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
33 exit(1); |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
34 } |
33
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
35 usbh = usb_open(dev); |
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
36 if (!usbh) { |
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
37 fprintf(stderr, "error: usb_open() failed\n"); |
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
38 exit(1); |
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
39 } |
29
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
40 read_eeprom(usbh); |
33
df284688d0c8
fc-duart28-conf check-eeprom: same change as in fteeprom-read
Mychaela Falconia <falcon@freecalypso.org>
parents:
31
diff
changeset
|
41 usb_close(usbh); |
29
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
42 analyze_eeprom(); |
a7393d00996a
fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
43 return 0; |
27
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 oper_program(newconf) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 { |
31
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
48 struct usb_device *dev; |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
49 usb_dev_handle *usbh; |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
50 int prev; |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
51 |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
52 dev = find_duart28_usbdev(); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
53 if (!dev) { |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
54 fprintf(stderr, "error: no DUART28 adapter found\n"); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
55 exit(1); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
56 } |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
57 usbh = usbwrap_open_dev(dev, 1); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
58 read_eeprom(usbh); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
59 prev = analyze_eeprom(); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
60 if (prev == newconf) { |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
61 printf("EEPROM is already in the right state, nothing to do\n"); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
62 usbwrap_close_dev(usbh); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
63 return 0; |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
64 } |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
65 printf("Reprogramming to DUART28%c configuration\n", newconf); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
66 update_eeprom(usbh, newconf); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
67 usbwrap_close_dev(usbh); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
68 printf("EEPROM changed, replug the USB device to take effect\n"); |
530ec3792de8
fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents:
29
diff
changeset
|
69 return 0; |
27
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 main(argc, argv) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 char **argv; |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (argc < 2) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 return oper_find(); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (!strcmp(argv[1], "find")) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 if (argc != 2) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 fprintf(stderr, |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 "error: %s find command takes no arguments\n", |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 argv[0]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 exit(1); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 return oper_find(); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 if (!strcmp(argv[1], "check-eeprom")) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 if (argc != 2) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 fprintf(stderr, |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 "error: %s check-eeprom command takes no arguments\n", |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 argv[0]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 exit(1); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 return oper_check_eeprom(); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 if (!strcmp(argv[1], "set")) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 if (argc != 3) { |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 fprintf(stderr, |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 "error: %s set command takes 1 argument\n", |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 argv[0]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 exit(1); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 if (!strcmp(argv[2], "C") || !strcmp(argv[2], "S")) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 return oper_program(argv[2][0]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 if (!strcmp(argv[2], "c") || !strcmp(argv[2], "s")) |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 return oper_program(toupper(argv[2][0])); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 fprintf(stderr, "error: %s set argument must be C or S\n", |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 argv[0]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 exit(1); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 } |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
110 fprintf(stderr, "%s error: non-understood command \"%s\"\n", argv[0], |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 argv[1]); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
112 exit(1); |
2413a54a1bfc
fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
113 } |