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