-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathruncontrol.cpp
177 lines (126 loc) · 3.83 KB
/
runcontrol.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
#include "runcontrol.h"
#include <stdio.h>
//#define DEBUG_RC
RunControl::RunControl(void) {
initok = false;
devh = NULL;
}
void RunControl::init(libusb_device_handle **d) {
devh = *d;
initok = true;
}
bool RunControl::read_reg(uint16_t addr, uint16_t &value) {
uint16_t pkt_wr[2];
uint8_t *buf_wr;
uint8_t buf_rd[4];
uint16_t *pkt_rd;
uint8_t seq;
int retval, actual;
seq = addr % 0xF;
pkt_wr[0] = (OPC_RD << 12) | seq;
pkt_wr[1] = addr;
buf_wr = reinterpret_cast<uint8_t*>(pkt_wr);
#ifdef DEBUG_RC
printf("buf_wr[0] = %.2X\n", buf_wr[0]);
printf("buf_wr[1] = %.2X\n", buf_wr[1]);
printf("buf_wr[2] = %.2X\n", buf_wr[2]);
printf("buf_wr[3] = %.2X\n", buf_wr[3]);
#endif
retval = libusb_bulk_transfer(devh, EP_RCWR, buf_wr, 4, &actual, USB_TIMEOUT);
if (retval == 0 && actual > 0) { // we write successfully
retval = libusb_bulk_transfer(devh, EP_RCRD, buf_rd, 4, &actual, USB_TIMEOUT);
if (retval == 0 && actual > 0) { // we read successfully
if(buf_rd[0] == seq) {
if(buf_rd[1] == (OPC_RD << 4)) {
pkt_rd = reinterpret_cast<uint16_t*>(buf_rd);
#ifdef DEBUG_RC
printf("actual = %d\n", actual);
printf("pkt_rd[0] = %.4X\n", pkt_rd[0]);
printf("pkt_rd[1] = %.4X\n", pkt_rd[1]);
#endif
value = pkt_rd[1];
return true;
} else {
printf("usb_rc_regread: read error - error code received\n");
return false;
}
} else {
#ifdef DEBUG_RC
printf("usb_rc_regread: response out of sequence error\n");
#endif
return false;
}
}
} else {
#ifdef DEBUG_RC
printf("usb_rc_regread: request write error\n");
#endif
return false;
}
#ifdef DEBUG_RC
printf("buf_rd[0] = %.2X\n", buf_rd[0]);
printf("buf_rd[1] = %.2X\n", buf_rd[1]);
printf("buf_rd[2] = %.2X\n", buf_rd[2]);
printf("buf_rd[3] = %.2X\n", buf_rd[3]);
#endif
pkt_rd = reinterpret_cast<uint16_t*>(buf_rd);
#ifdef DEBUG_RC
printf("pkt_rd[0] = %.4X\n", pkt_rd[0]);
printf("pkt_rd[1] = %.4X\n", pkt_rd[1]);
#endif
value = pkt_rd[1];
return true;
}
bool RunControl::write_reg(uint16_t addr, uint16_t value) {
uint16_t pkt_wr[3];
uint8_t *buf_wr;
uint8_t buf_rd[2];
uint8_t seq;
int retval, actual;
seq = addr % 0xF;
pkt_wr[0] = (OPC_WR << 12) | seq;
pkt_wr[1] = addr;
pkt_wr[2] = value;
buf_wr = reinterpret_cast<uint8_t*>(pkt_wr);
retval = libusb_bulk_transfer(devh, EP_RCWR, buf_wr, 6, &actual, USB_TIMEOUT);
#ifdef DEBUG_RC
printf("actual = %d\n", actual);
printf("buf_wr[0] = %.2X\n", buf_wr[0]);
printf("buf_wr[1] = %.2X\n", buf_wr[1]);
printf("buf_wr[2] = %.2X\n", buf_wr[2]);
printf("buf_wr[3] = %.2X\n", buf_wr[3]);
printf("buf_wr[4] = %.2X\n", buf_wr[4]);
printf("buf_wr[5] = %.2X\n", buf_wr[5]);
#endif
if (retval == 0 && actual > 0) { // we write successfully
retval = libusb_bulk_transfer(devh, EP_RCRD, buf_rd, 2, &actual, USB_TIMEOUT);
if (retval == 0 && actual > 0) { // we read successfully
if(buf_rd[0] == seq) {
if(buf_rd[1] == (OPC_WR << 4)) {
return true;
} else {
#ifdef DEBUG_RC
printf("usb_rc_regwrite: write error - error code received\n");
#endif
return false;
}
} else {
#ifdef DEBUG_RC
printf("usb_rc_regwrite: write error - out of sequence\n");
#endif
return false;
}
} else {
#ifdef DEBUG_RC
printf("usb_rc_regwrite: read (step 2) error code received\n");
#endif
return false;
}
} else {
#ifdef DEBUG_RC
printf("usb_rc_regwrite: write (step 1) error code received\n");
#endif
return false;
}
return true;
}