3 /* According to POSIX.1-2001 */
4 #include <sys/select.h>
20 void check_handle(usb_dev_handle
**h
, const char* manuf
, const char* product
, const char* serial
);
21 int gpio_read(usb_dev_handle
*h
, int gpio
);
22 void gpio_in(usb_dev_handle
*h
, int gpio
, int pullup
);
23 void gpio_out(usb_dev_handle
*h
, int gpio
, int value
);
26 void handle_error(int ret
)
29 perror("Failed to write to PL2303 device");
30 fprintf(stderr
, "Have you installed the correct udev rules?\n");
35 static struct option long_options
[] =
37 /* These options set a flag. */
38 {"help", no_argument
, 0, 'h'},
39 {"gpio", required_argument
, 0, 'g'},
40 {"in", optional_argument
, 0, 'i'},
41 {"out", required_argument
, 0, 'o'},
42 {"sleep", required_argument
, 0, 's'},
43 {"read", no_argument
, 0, 'r'},
44 {"product", required_argument
, 0, 'p'},
45 {"manuf", required_argument
, 0, 'm'},
46 {"serial", required_argument
, 0, 'n'},
50 void usage(const char *self
)
52 printf("PL2303HXA/CP2103 userspace GPIO control tool\n"
53 "(c) Andrew 'Necromant' Andrianov 2014, License: GPLv3\n"
54 "Usage: %s [action1] [action2] ...\n"
56 "\t --product=blah - Use device with this 'product' string\n"
57 "\t --serial=blah - Use device with this 'serial' string\n"
58 "\t --manuf=blah - Use device with this 'manufacturer' string\n"
59 "\t -g/--gpio n - select GPIO, n=0, 1\n"
60 "\t -i/--in - configure GPIO as input\n"
61 "\t -o/--out v - configure GPIO as output with value v\n"
62 "\t -r/--read v - Read current GPIO value\n\n"
63 "\t -s/--sleep v - Delay for v ms\n\n"
65 "\t%s --gpio=1 --out 1\n"
66 "\t%s --gpio=0 --out 0 --gpio=1 --in\n\n"
67 "All arguments are executed from left to right, you can add \n"
68 "delays using --sleep v option. e.g. \n"
69 "\t%s --gpio=0 --out 0 --sleep 1000 --gpio=0 --out 1\n"
70 "\n", self
, self
, self
, self
);
73 int main(int argc
, char* argv
[])
76 usb_dev_handle
*h
= NULL
;
78 const char *product
= NULL
;
79 const char *manuf
= NULL
;
80 const char *serial
= NULL
;
89 c
= getopt_long (argc
, argv
, "hg:i:o:r:s:",
90 long_options
, &option_index
);
92 /* Detect the end of the options. */
117 check_handle(&h
, manuf
, product
, serial
);
126 check_handle(&h
, manuf
, product
, serial
);
129 gpio_out(h
, gpio
, v
);
134 unsigned long n
= atoi(optarg
);
140 check_handle(&h
, manuf
, product
, serial
);
141 printf("%d\n", gpio_read(h
, gpio
) ?
1 : 0);