X-Git-Url: http://git.linex4red.de/pub/pl2303-ft232-gpio.git/blobdiff_plain/13516fc96d4f88897f182212e834869259e6d783..HEAD:/main.c diff --git a/main.c b/main.c index f24e40f..6dbb9d0 100644 --- a/main.c +++ b/main.c @@ -9,23 +9,19 @@ #include #include #include -#include +#include #include #define _GNU_SOURCE #include - -#define I_VENDOR_NUM 0x67b -#define I_PRODUCT_NUM 0x2303 - - -#define VENDOR_READ_REQUEST_TYPE 0xc0 -#define VENDOR_READ_REQUEST 0x01 - -#define VENDOR_WRITE_REQUEST_TYPE 0x40 -#define VENDOR_WRITE_REQUEST 0x01 +int get_device_vid(); +int get_device_pid(); +void check_handle(libusb_device_handle **h, int vid, int pid, const char* manuf, const char* product, const char* serial); +int gpio_read(libusb_device_handle *h, int gpio); +void gpio_in(libusb_device_handle *h, int gpio, int pullup); +void gpio_out(libusb_device_handle *h, int gpio, int value); void handle_error(int ret) @@ -37,111 +33,35 @@ void handle_error(int ret) } } - -/* Get current GPIO register from PL2303 */ -char gpio_read_reg(usb_dev_handle *h) -{ - char buf; - int bytes = usb_control_msg( - h, // handle obtained with usb_open() - VENDOR_READ_REQUEST_TYPE, // bRequestType - VENDOR_READ_REQUEST, // bRequest - 0x0081, // wValue - 0, // wIndex - &buf, // pointer to destination buffer - 1, // wLength - 100 - ); - handle_error(bytes); - return buf; -} - -void gpio_write_reg(usb_dev_handle *h, unsigned char reg) -{ - int bytes = usb_control_msg( - h, // handle obtained with usb_open() - VENDOR_WRITE_REQUEST_TYPE, // bRequestType - VENDOR_WRITE_REQUEST, // bRequest - 1, // wValue - reg, // wIndex - 0, // pointer to destination buffer - 0, // wLength - 6000 - ); - handle_error(bytes); - -} - -int gpio_dir_shift(int gpio) { - if (gpio == 0) - return 4; - if (gpio == 1) - return 5; - return 4; /* default to 0 */ -} - -int gpio_val_shift(int gpio) { - if (gpio == 0) - return 6; - if (gpio == 1) - return 7; - return 6; /* default to 0 */ -} - - -void gpio_out(usb_dev_handle *h, int gpio, int value) -{ - int shift_dir = gpio_dir_shift(gpio); - int shift_val = gpio_val_shift(gpio); - unsigned char reg = gpio_read_reg(h); - reg |= (1 << shift_dir); - reg &= ~(1 << shift_val); - reg |= (value << shift_val); - gpio_write_reg(h, reg); -} - -void gpio_in(usb_dev_handle *h, int gpio, int pullup) -{ - int shift_dir = gpio_dir_shift(gpio); - int shift_val = gpio_val_shift(gpio); - - unsigned char reg = gpio_read_reg(h); - reg &= ~(1 << shift_dir); - reg &= ~(1 << shift_val); - reg |= (pullup << shift_val); - gpio_write_reg(h, reg); -} - -int gpio_read(usb_dev_handle *h, int gpio) -{ - unsigned char r = gpio_read_reg(h); - int shift = gpio_val_shift(gpio); - return (r & (1<