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