First support PL2303-HXD GPIO 2 & 3
[pub/pl2303-ft232-gpio.git] / cp2103.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 /* According to POSIX.1-2001 */
4 #include <sys/select.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9 #include <termios.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <stdint.h>
14 #include <libusb.h>
15
16 #define _GNU_SOURCE
17 #include <getopt.h>
18
19
20 #define I_VENDOR_NUM 0x10c4
21 #define I_PRODUCT_NUM 0xea60
22
23
24 #define VENDOR_READ_REQUEST_TYPE 0xc0
25 #define VENDOR_READ_REQUEST 0x01
26
27 #define VENDOR_WRITE_REQUEST_TYPE 0x40
28 #define VENDOR_WRITE_REQUEST 0x01
29
30 /* CP2103 GPIO */
31 #define GPIO_0 0x01
32 #define GPIO_1 0x02
33 #define GPIO_2 0x04
34 #define GPIO_3 0x08
35 #define GPIO_MASK (GPIO_0|GPIO_1|GPIO_2|GPIO_3)
36
37
38
39
40 int get_device_vid()
41 {
42 return I_VENDOR_NUM;
43 }
44
45 int get_device_pid()
46 {
47 return I_PRODUCT_NUM;
48 }
49
50 //GPIO_READ
51 //ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x00c2, 0, gpio, 1);
52 //type c0
53 //value c2
54 //index 0
55 //buffer 1 bytes
56 //length 1
57
58
59 /* Get current GPIO register from PL2303 */
60 char gpio_read_reg(libusb_device_handle *h)
61 {
62 char buf;
63 int bytes = libusb_control_transfer(
64 h, // handle obtained with usb_open()
65 0xc0, // bRequestType
66 0xff, // bRequest
67 0xc2, // wValue
68 0, // wIndex
69 &buf, // pointer to destination buffer
70 1, // wLength
71 100
72 );
73 handle_error(bytes);
74 return buf;
75 }
76
77 // GPIO_WRITE
78 //request 0xff
79 //type 0x40
80 //value 0x37e1
81 //index gpioreg
82 //data NULL
83 //len 0
84 void gpio_write_reg(libusb_device_handle *h, uint16_t reg)
85 {
86 int bytes = libusb_control_transfer(
87 h, // handle obtained with usb_open()
88 0x40, // bRequestType
89 0xff, // bRequest
90 0x37e1, // wValue
91 reg, // wIndex
92 0, // pointer to destination buffer
93 0, // wLength
94 1000
95 );
96 handle_error(bytes);
97 }
98
99
100 void gpio_out(libusb_device_handle *h, int gnum, int value)
101 {
102
103 uint16_t gpio = 0;
104 switch (gnum) {
105 case 0:
106 gpio |= GPIO_0;
107 if (value)
108 gpio |= (GPIO_0 << 8);
109 break;
110 case 1:
111 gpio |= GPIO_1;
112 if (value)
113 gpio |= (GPIO_1 << 8);
114 break;
115 case 2:
116 gpio |= GPIO_2;
117 if (value)
118 gpio |= (GPIO_2 << 8);
119 break;
120 case 3:
121 gpio |= GPIO_3;
122 if (value)
123 gpio |= (GPIO_3 << 8);
124 break;
125 }
126 gpio_write_reg(h, gpio);
127 }
128
129 void gpio_in(libusb_device_handle *h, int gpio, int pullup)
130 {
131 printf("FixMe: don't know how to make pins input on cp2103\n");
132 }
133
134 int gpio_read(libusb_device_handle *h, int gpio)
135 {
136 printf("FixMe: don't know how to read pins on cp2103\n");
137 }
138