-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch340.cpp
206 lines (175 loc) · 6.28 KB
/
ch340.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "ch340.h"
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iterator>
int CH340::init(int vendor, int product, int baudrate) {
int err;
err = init_usb(vendor, product);
if (err != 0) {
std::cerr << "Error initializing usb: " << libusb_error_name(err) << std::endl;
return err;
}
err = handshake();
if (err != 0) {
std::cerr << "Error during handshake: " << libusb_error_name(err) << std::endl;
return err;
}
err = set_baud(baudrate);
if (err != 0) {
std::cerr << "Error setting baudrate: " << libusb_error_name(err) << std::endl;
return err;
}
err = up();
if (err != 0) {
std::cerr << "Error during up: " << libusb_error_name(err) << std::endl;
return err;
}
return err;
}
int CH340::init_usb(int vendor, int product) {
int err;
err = libusb_init(&ctx);
if (err != 0) {
std::cerr << "Error initializing libusb: " << libusb_error_name(err) << std::endl;
return err;
}
libusb_device **dev_list = NULL;
ssize_t num_devices = libusb_get_device_list(ctx, &dev_list);
if (num_devices < 0) {
std::cerr << "Error getting device list." << std::endl;
libusb_exit(ctx);
return -1;
}
bool deviceFound = false;
for (ssize_t i = 0; i < num_devices; i++) {
libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev_list[i], &desc) == 0) {
if (debug) {
std::cout << "Product ID: " << desc.idProduct << std::endl;
std::cout << "Vendor: " << desc.idVendor << std::endl;
std::cout << "Manufacturer: " << desc.iManufacturer << std::endl;
std::cout << "serial Num: " << desc.iSerialNumber << std::endl;
std::cout << "Product: " << desc.iProduct << std::endl;
}
if (desc.idVendor == vendor && desc.idProduct == product) {
dev_handle = libusb_open_device_with_vid_pid(ctx, desc.idVendor, desc.idProduct);
if (dev_handle) {
libusb_config_descriptor *config;
libusb_get_active_config_descriptor(dev_list[i], &config);
int interfaceNum = config->interface[0].altsetting[0].bInterfaceNumber;
if (libusb_kernel_driver_active(dev_handle, interfaceNum)) {
err = libusb_detach_kernel_driver(dev_handle, interfaceNum);
if (err != 0) {
std::cerr << "Error detaching kernel driver: " << libusb_error_name(err) << std::endl;
libusb_close(dev_handle);
libusb_exit(ctx);
return err;
}
}
err = libusb_claim_interface(dev_handle, interfaceNum);
libusb_free_config_descriptor(config);
if (err == 0) {
deviceFound = true;
break;
} else {
std::cerr << "Error claiming interface: " << libusb_error_name(err) << std::endl;
libusb_close(dev_handle);
dev_handle = NULL;
return err;
}
}
break;
}
}
}
if (dev_handle == NULL) {
std::cerr << "Could not find device." << std::endl;
return -1;
}
return err;
}
int CH340::handshake() {
if (dev_handle == NULL) {
std::cerr << "dev_handle not defined." << std::endl;
return -1;
}
int err = 0;
// 1
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0xa1, 0, 0, NULL, 0, 1000);
if (err < 0)
{
std::cerr << "Error during handshake: " << libusb_error_name(err) << std::endl;
return err;
}
// 2
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0x9a, 0x2518, 0x0050, NULL, 0, 1000);
if (err < 0)
{
std::cerr << "Error during handshake: " << libusb_error_name(err) << std::endl;
return err;
}
// 3
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0xa1, 0x501f, 0xd90a, NULL, 0, 1000);
if (err < 0)
{
std::cerr << "Error during handshake: " << libusb_error_name(err) << std::endl;
return err;
}
return 0;
}
int CH340::set_baud(int baudrate) {
static int baud[] = {
2400, 0xd901, 0x0038,
4800, 0x6402, 0x001f,
9600, 0xb202, 0x0013,
19200, 0xd902, 0x000d,
38400, 0x6403, 0x000a,
115200, 0xcc03, 0x0008
};
int err = 0;
for (int i = 0; i < sizeof(baud)/sizeof(int) / 3; i++) {
if (baud[i * 3] == baudrate) {
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0x9a, 0x1312, baud[i * 3 + 1], NULL, 0, 1000);
if (err < 0) {
printf("could not set baudrate \n");
return err;
}
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0x9a, 0x0f2c, baud[i * 3 + 2], NULL, 0, 1000);
if (err < 0) {
printf("could not set baudrate \n");
return err;
}
}
}
return err;
}
int CH340::up() {
int err = 0;
err = libusb_control_transfer(dev_handle, CTRL_OUT, 0xa4, ~((dtr ? 1 << 5 : 0) | (rts ? 1 << 6 : 0)), 0, NULL, 0, 1000);
if (err < 0) {
std::cerr << "Error during up: " << libusb_error_name(err) << std::endl;
return err;
}
return err;
}
int CH340::bulk_write(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout) {
int err = 0;
int transferred = 0;
err = libusb_bulk_transfer(dev_handle, endpoint, data, length, &transferred, timeout);
if (err < 0) {
std::cerr << "Error during bulk write: " << libusb_error_name(err) << std::endl;
return err;
}
return err;
}
int CH340::bulk_read(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout) {
int err = 0;
int transferred = 0;
err = libusb_bulk_transfer(dev_handle, endpoint, data, length, &transferred, timeout);
if (err < 0) {
std::cerr << "Error during bulk read: " << libusb_error_name(err) << std::endl;
return err;
}
return transferred;
}