5e4f61e6f9560e09c93fe00c5af5bbc8824878f9
[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 <usb.h>
13 #include <getopt.h>
14 #include <stdint.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 //GPIO_READ
40 //ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x00c2, 0, gpio, 1);
41 //type c0
42 //value c2
43 //index 0
44 //buffer 1 bytes
45 //length 1
46
47
48 /* Get current GPIO register from PL2303 */
49 char gpio_read_reg(usb_dev_handle *h)
50 {
51 char buf;
52 int bytes = usb_control_msg(
53 h, // handle obtained with usb_open()
54 0xc0, // bRequestType
55 0xff, // bRequest
56 0xc2, // wValue
57 0, // wIndex
58 &buf, // pointer to destination buffer
59 1, // wLength
60 100
61 );
62 handle_error(bytes);
63 return buf;
64 }
65
66 // GPIO_WRITE
67 //request 0xff
68 //type 0x40
69 //value 0x37e1
70 //index gpioreg
71 //data NULL
72 //len 0
73 void gpio_write_reg(usb_dev_handle *h, uint16_t reg)
74 {
75 int bytes = usb_control_msg(
76 h, // handle obtained with usb_open()
77 0x40, // bRequestType
78 0xff, // bRequest
79 0x37e1, // wValue
80 reg, // wIndex
81 0, // pointer to destination buffer
82 0, // wLength
83 6000
84 );
85 handle_error(bytes);
86 }
87
88
89 void gpio_out(usb_dev_handle *h, int gnum, int value)
90 {
91
92 uint16_t gpio = 0;
93 switch (gnum) {
94 case 0:
95 gpio |= GPIO_0;
96 if (value)
97 gpio |= (GPIO_0 << 8);
98 break;
99 case 1:
100 gpio |= GPIO_1;
101 if (value)
102 gpio |= (GPIO_1 << 8);
103 break;
104 case 2:
105 gpio |= GPIO_2;
106 if (value)
107 gpio |= (GPIO_2 << 8);
108 break;
109 case 3:
110 gpio |= GPIO_3;
111 if (value)
112 gpio |= (GPIO_3 << 8);
113 break;
114 }
115 gpio_write_reg(h, gpio);
116 }
117
118 void gpio_in(usb_dev_handle *h, int gpio, int pullup)
119 {
120 printf("FixMe: don't know how to make pins input on cp2103\n");
121 }
122
123 int gpio_read(usb_dev_handle *h, int gpio)
124 {
125 printf("FixMe: don't know how to read pins on cp2103\n");
126 }
127
128
129 extern usb_dev_handle *nc_usb_open(int vendor, int product,
130 const char *vendor_name, const char *product_name, const char *serial);
131 void check_handle(usb_dev_handle **h, const char* manuf, const char* product, const char* serial)
132 {
133 if (*h)
134 return;
135
136 *h = nc_usb_open(I_VENDOR_NUM, I_PRODUCT_NUM, manuf, product, serial);
137 if (!(*h)) {
138 fprintf(stderr, "No CP2103 USB device %04x:%04x found ;(\n", I_VENDOR_NUM, I_PRODUCT_NUM);
139 exit(1);
140 }
141 }