-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsb.cpp
333 lines (297 loc) · 7.6 KB
/
Usb.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright (C) 2017 Johan Bergkvist
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
#include <stdio.h>
#include <windows.h>
#include <WinUsb.h>
#include <Setupapi.h>
#include <Usb100.h>
DEFINE_GUID (GUID_PROG_DEVICE_INTERFACE_CLASS, 0xb35924d6, 0x3e16, 0x4a9e, 0x97, 0x82, 0x55, 0x24, 0xa4, 0xb7, 0x9b, 0xe0);
HANDLE handle;
WINUSB_INTERFACE_HANDLE usb_handle;
UCHAR out_pipe;
UCHAR in_pipe;
extern bool g_verbose;
bool g_print_txrx = false;
//===========================================================================
//
// Name : Open
//
// Desc : Opens communication with the PIC programmer over USB.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Open()
{
HDEVINFO hardware_device_info = SetupDiGetClassDevs (&GUID_PROG_DEVICE_INTERFACE_CLASS,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
if (hardware_device_info == INVALID_HANDLE_VALUE)
{
if (g_verbose)
{
printf ("*** SetupDiGetClassDevs failed\n");
}
return false;
}
SP_DEVICE_INTERFACE_DATA device_interface_data;
device_interface_data.cbSize = sizeof (SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces (hardware_device_info,
NULL,
&GUID_PROG_DEVICE_INTERFACE_CLASS,
0,
&device_interface_data))
{
if (g_verbose)
{
printf ("*** SetupDiEnumDeviceInterfaces failed\n");
}
else
{
printf ("*** Can't find the programmer. Is it connected?\n");
}
return false;
}
DWORD actual_length, length;
SetupDiGetDeviceInterfaceDetail (hardware_device_info,
&device_interface_data,
NULL,
0,
&actual_length,
NULL);
PSP_DEVICE_INTERFACE_DETAIL_DATA device_interface_detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc (actual_length);
device_interface_detail_data->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail (hardware_device_info,
&device_interface_data,
device_interface_detail_data,
actual_length,
&length,
NULL))
{
if (g_verbose)
{
printf ("*** SetupDiGetDeviceInterfaceDetail (2) failed\n");
}
return false;
}
SetupDiDestroyDeviceInfoList (hardware_device_info);
handle = CreateFile (device_interface_detail_data->DevicePath,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (handle == INVALID_HANDLE_VALUE)
{
if (g_verbose)
{
printf ("*** Can't open \"%s\"\n", device_interface_detail_data->DevicePath);
}
else
{
printf ("*** Can't find the programmer. Is it connected?\n");
}
return false;
}
if (!WinUsb_Initialize(handle, &usb_handle))
{
if (g_verbose)
{
printf ("*** WinUsb_Initialize failed\n");
}
return false;
}
USB_INTERFACE_DESCRIPTOR interface_descriptor;
if (!WinUsb_QueryInterfaceSettings(usb_handle, 0, &interface_descriptor))
{
if (g_verbose)
{
printf ("*** WinUsb_QueryInterfaceSettings failed\n");
}
return false;
}
for (int i = 0; i < interface_descriptor.bNumEndpoints; i++)
{
WINUSB_PIPE_INFORMATION pipe_information;
if (!WinUsb_QueryPipe(usb_handle, 0, (UCHAR)i, &pipe_information))
{
if (g_verbose)
{
printf ("*** WinUsb_QueryPipe failed\n");
}
return false;
}
if (pipe_information.PipeType == UsbdPipeTypeBulk
&& USB_ENDPOINT_DIRECTION_OUT(pipe_information.PipeId))
{
out_pipe = pipe_information.PipeId;
}
else if (pipe_information.PipeType == UsbdPipeTypeBulk
&& USB_ENDPOINT_DIRECTION_IN(pipe_information.PipeId))
{
in_pipe = pipe_information.PipeId;
}
else
{
if (g_verbose)
{
printf ("+++ Unexpected pipe %i\n", i);
}
}
}
free(device_interface_detail_data);
ULONG timeout_in_milliseconds = 1000;
if (!WinUsb_SetPipePolicy(usb_handle, out_pipe, PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout_in_milliseconds))
{
if (g_verbose)
{
printf ("*** WinUsb_SetPipePolicy(..., out_pipe, PIPE_TRANSFER_TIMEOUT, ...) failed\n");
}
return false;
}
if (!WinUsb_SetPipePolicy(usb_handle, in_pipe, PIPE_TRANSFER_TIMEOUT, sizeof(ULONG), &timeout_in_milliseconds))
{
if (g_verbose)
{
printf ("*** WinUsb_SetPipePolicy(..., in_pipe, PIPE_TRANSFER_TIMEOUT, ...) failed\n");
}
return false;
}
return true;
}
//===========================================================================
//
// Name : Close
//
// Desc : Closes the previously opened USB handle.
//
// Returns : Nothing.
//
//===========================================================================
void Close()
{
if (usb_handle != NULL)
{
WinUsb_Free(usb_handle);
usb_handle = NULL;
}
CloseHandle (handle);
}
//===========================================================================
//
// Name : Send
//
// Desc : Sends "length" bytes in "buffer" to the programmer over USB.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Send(unsigned char *buffer, int length)
{
if (g_print_txrx)
{
printf("TX: ");
for (int i = 0; i < length; i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
ULONG bytes_written;
if (!WinUsb_WritePipe(usb_handle, out_pipe, buffer, length, &bytes_written, NULL))
{
DWORD error = GetLastError();
switch(error)
{
case ERROR_SEM_TIMEOUT:
if (g_verbose)
{
printf("*** WinUsb_WritePipe failed with ERROR_SEM_TIMEOUT.\n");
}
break;
case ERROR_INVALID_HANDLE:
if (g_verbose)
{
printf("*** WinUsb_WritePipe failed with ERROR_INVALID_HANDLE.\n");
}
break;
case ERROR_IO_PENDING:
if (g_verbose)
{
printf("*** WinUsb_WritePipe failed with ERROR_IO_PENDING.\n");
}
break;
case ERROR_NOT_ENOUGH_MEMORY:
if (g_verbose)
{
printf("*** WinUsb_WritePipe failed with ERROR_NOT_ENOUGH_MEMORY.\n");
}
break;
default:
if (g_verbose)
{
printf("*** WinUsb_WritePipe failed with error code %d (0x%08x).\n", (int)error, (unsigned int)error);
}
break;
}
return false;
}
else if (bytes_written != (ULONG)length)
{
printf("*** WinUsb_WritePipe worked, but managed to send %i bytes rather then the %i expected.\n", (int)bytes_written, length);
return false;
}
return true;
}
//===========================================================================
//
// Name : Send
//
// Desc : Send s a single byte to the programmer over USB.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Send(unsigned char byte)
{
return Send(&byte, 1);
}
//===========================================================================
//
// Name : Receive
//
// Desc : Receives up to "*length" bytes into "buffer" from the programmer
// over USB. If successful, "*length" denotes the number of bytes
// actually received.
//
// Returns : True if successful, false otherwise.
//
//===========================================================================
bool Receive(unsigned char *buffer, int *length)
{
ULONG bytes_read;
memset(buffer, 0xcd, *length);
if (!WinUsb_ReadPipe(usb_handle, in_pipe, buffer, *length, &bytes_read, NULL)
&& GetLastError() != ERROR_SEM_TIMEOUT)
{
*length = 0;
return false;
}
*length = bytes_read;
if (g_print_txrx)
{
printf("RX: ");
for (int i = 0; i < *length; i++)
{
printf("%02x ", buffer[i]);
}
printf("\n");
}
return true;
}