From: Peter Henn Date: Mon, 17 Jul 2017 01:23:08 +0000 (+0000) Subject: Add Support for FT232R X-Git-Tag: v0.1~7 X-Git-Url: http://git.linex4red.de/pub/pl2303-ft232-gpio.git/commitdiff_plain/7cde69e0d8f5bd8e32e34b6f6db4372f473b5a05 Add Support for FT232R Signed-off-by: Peter Henn --- diff --git a/10-pl2303_cp210x_userspace.rules b/10-pl2303_cp210x_userspace.rules index b4a35c9..16af484 100755 --- a/10-pl2303_cp210x_userspace.rules +++ b/10-pl2303_cp210x_userspace.rules @@ -3,3 +3,4 @@ SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="067b", ATTR{idProduct}=="2303", GROUP="users", MODE="0666" SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="10c4", ATTR{idProduct}=="ea60", GROUP="users", MODE="0666" +SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="0403", ATTR{idProduct}=="6001", GROUP="users", MODE="0666" diff --git a/Makefile b/Makefile index 075fe8f..44b6fa7 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LDFLAGS = $(shell pkg-config --libs libusb-1.0) PREFIX?=/usr/local -all: pl2303gpio cp2103gpio +all: pl2303gpio cp2103gpio ft232rgpio OBJS=usb.c main.c pl2303gpio: $(OBJS) pl2303.o @@ -12,12 +12,17 @@ pl2303gpio: $(OBJS) pl2303.o cp2103gpio: $(OBJS) cp2103.o $(CC) $(CFLAGS) -Wall -Werror -I"../include" -o $(@) $(^) $(LDFLAGS) +ft232rgpio: $(OBJS) ft232r.o + $(CC) $(CFLAGS) -Wall -Werror -I"../include" -o $(@) $(^) $(LDFLAGS) + clean: - -rm pl2303gpio cp2103gpio + -rm -f pl2303gpio cp2103gpio ft232rgpio + -rm -f pl2303.o cp2103.o ft232r.o -install: pl2303gpio cp2103gpio - cp pl2303gpio $(PREFIX)/bin - cp cp2103gpio $(PREFIX)/bin +install: pl2303gpio cp2103gpio ft232rgpio + install pl2303gpio $(PREFIX)/bin + install cp2103gpio $(PREFIX)/bin + install ft232rgpio $(PREFIX)/bin install-rules: cp 10-pl2303_cp210x_userspace.rules /etc/udev/rules.d diff --git a/ft232r.c b/ft232r.c new file mode 100644 index 0000000..e8a558f --- /dev/null +++ b/ft232r.c @@ -0,0 +1,114 @@ +#include +#include +/* According to POSIX.1-2001 */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define _GNU_SOURCE +#include + + +#define I_VENDOR_NUM 0x0403 +#define I_PRODUCT_NUM 0x6001 +#define BITMODE_CBUS 0x20 +#define BITMODE_RESET 0x00 + +#define FTDI_SIO_SET_BITMODE 11 /* Set the bitmode */ +#define FTDI_SIO_READ_PINS 12 /* Read pins in bitmode */ + +#define FTDI_SIO_SET_BITMODE_REQUEST FTDI_SIO_SET_BITMODE +#define FTDI_SIO_SET_BITMODE_REQUEST_TYPE 0x40 + +#define FTDI_SIO_READ_PINS_REQUEST FTDI_SIO_READ_PINS +#define FTDI_SIO_READ_PINS_REQUEST_TYPE 0xC0 + +int get_device_vid() +{ + return I_VENDOR_NUM; +} + +int get_device_pid() +{ + return I_PRODUCT_NUM; +} + +/* Get current GPIO register from PL2303 */ +unsigned char gpio_read_reg(libusb_device_handle *h) +{ + unsigned char buf; + int bytes = libusb_control_transfer( + h, // handle obtained with usb_open() + FTDI_SIO_READ_PINS_REQUEST_TYPE, // bRequestType + FTDI_SIO_READ_PINS_REQUEST, // bRequest + 0, // wValue + 0, // wIndex + &buf, // pointer to destination buffer + 1, // wLength + 1000 + ); + handle_error(bytes); + return buf; +} + +void gpio_write_reg(libusb_device_handle *h, unsigned short reg) +{ + int bytes = libusb_control_transfer( + h, // handle obtained with usb_open() + FTDI_SIO_SET_BITMODE_REQUEST_TYPE, //bRequestType + FTDI_SIO_SET_BITMODE_REQUEST, // bRequest + reg, // wValue + 0, // wIndex + 0, // pointer to destination buffer + 0, // wLength + 1000 + ); + handle_error(bytes); + +} + +void gpio_out(libusb_device_handle *h, int gpio, int value) +{ + unsigned char cbus_mask; + + if ((gpio < 0) || (gpio > 3)) + return; + + cbus_mask = gpio_read_reg(h); + cbus_mask |= ((1 << gpio) << 4); + if (value) + cbus_mask |= (1 << gpio); + else + cbus_mask &= ~(1 << gpio); + gpio_write_reg(h, (BITMODE_CBUS << 8) | cbus_mask); +} + +void gpio_in(libusb_device_handle *h, int gpio, int pullup) +{ + unsigned char cbus_mask; + + if ((gpio < 0) || (gpio > 3)) + return; + + cbus_mask = gpio_read_reg(h); + cbus_mask &= ~((1 << gpio) << 4); + gpio_write_reg(h, (BITMODE_CBUS << 8) | cbus_mask); +} + +int gpio_read(libusb_device_handle *h, int gpio) +{ + unsigned char r = gpio_read_reg(h); + + if ((gpio < 0) || (gpio > 3)) + return 0; + return (r & (1 << gpio)) ? 1 : 0; +} +