-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch340.h
38 lines (32 loc) · 1.08 KB
/
ch340.h
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
#include <libusb-1.0/libusb.h>
#include <vector>
#include <string>
#include <cstdlib>
#define BAUDRATE 115200
#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
#define EP_DATA_OUT (0x2 | LIBUSB_ENDPOINT_OUT)
bool debug = false;
void checkDebugEnv() {
const char* debug_env = std::getenv("DEBUG");
if (debug_env != nullptr && std::string(debug_env) == "1") {
debug = true;
}
}
class CH340 {
private:
uint8_t dtr = 0;
uint8_t rts = 0;
libusb_context *ctx = NULL;
libusb_device_handle *dev_handle = NULL;
public:
// USB driver
bool connected;
int init(int vendor, int product, int baudrate=BAUDRATE);
int init_usb(int vendor, int product);
int handshake();
int set_baud(int baudrate);
int up();
int bulk_write(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout);
int bulk_read(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout);
};