annotate duart28/main.c @ 32:bb1d0956b0a8

fteeprom-read: we can do it without kicking off ftdi_sio driver
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 09 Sep 2023 19:56:11 +0000
parents 530ec3792de8
children df284688d0c8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 }
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
35 usbh = usbwrap_open_dev(dev, 1);
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
36 read_eeprom(usbh);
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
37 usbwrap_close_dev(usbh);
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
38 analyze_eeprom();
a7393d00996a fc-duart28-conf: implement check-eeprom
Mychaela Falconia <falcon@freecalypso.org>
parents: 27
diff changeset
39 return 0;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 oper_program(newconf)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 {
31
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
44 struct usb_device *dev;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
45 usb_dev_handle *usbh;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
46 int prev;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
47
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
48 dev = find_duart28_usbdev();
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
49 if (!dev) {
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
50 fprintf(stderr, "error: no DUART28 adapter found\n");
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
51 exit(1);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
52 }
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
53 usbh = usbwrap_open_dev(dev, 1);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
54 read_eeprom(usbh);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
55 prev = analyze_eeprom();
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
56 if (prev == newconf) {
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
57 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
58 usbwrap_close_dev(usbh);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
59 return 0;
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
60 }
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
61 printf("Reprogramming to DUART28%c configuration\n", newconf);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
62 update_eeprom(usbh, newconf);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
63 usbwrap_close_dev(usbh);
530ec3792de8 fc-duart28-conf: implement set command
Mychaela Falconia <falcon@freecalypso.org>
parents: 29
diff changeset
64 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
65 return 0;
27
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 main(argc, argv)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 char **argv;
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 if (argc < 2)
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 return oper_find();
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (!strcmp(argv[1], "find")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (argc != 2) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 fprintf(stderr,
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 "error: %s find command takes no arguments\n",
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 return oper_find();
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (!strcmp(argv[1], "check-eeprom")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (argc != 2) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 fprintf(stderr,
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 "error: %s check-eeprom command takes no arguments\n",
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 return oper_check_eeprom();
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 if (!strcmp(argv[1], "set")) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 if (argc != 3) {
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 fprintf(stderr,
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 "error: %s set command takes 1 argument\n",
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 if (!strcmp(argv[2], "C") || !strcmp(argv[2], "S"))
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 return oper_program(argv[2][0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (!strcmp(argv[2], "c") || !strcmp(argv[2], "s"))
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 return oper_program(toupper(argv[2][0]));
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 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
103 argv[0]);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 exit(1);
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 fprintf(stderr, "%s error: non-understood command \"%s\"\n", argv[0],
2413a54a1bfc fc-duart28-conf started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 argv[1]);
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 }