-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmosdfu.cpp
executable file
·425 lines (350 loc) · 11.3 KB
/
xmosdfu.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "libusb.h"
/* the device's vendor and product id */
// #define XMOS_VID 0x20b1
#define XMOS_L1_AUDIO2_PID 0x0002
// #define XMOS_L1_AUDIO1_PID 0x0003
#define XMOS_L2_AUDIO2_PID 0x0004
#define XMOS_SU1_AUDIO2_PID 0x0008
// vid and pid of respeaker mic array
#define XMOS_VID 0x2886
#define XMOS_L1_AUDIO1_PID 0x0007
unsigned int XMOS_DFU_IF = 0;
#define DFU_REQUEST_TO_DEV 0x21
#define DFU_REQUEST_FROM_DEV 0xa1
// Standard DFU requests
#define DFU_DETACH 0
#define DFU_DNLOAD 1
#define DFU_UPLOAD 2
#define DFU_GETSTATUS 3
#define DFU_CLRSTATUS 4
#define DFU_GETSTATE 5
#define DFU_ABORT 6
// XMOS alternate setting requests
#define XMOS_DFU_RESETDEVICE 0xf0
#define XMOS_DFU_REVERTFACTORY 0xf1
#define XMOS_DFU_RESETINTODFU 0xf2
#define XMOS_DFU_RESETFROMDFU 0xf3
#define XMOS_DFU_SAVESTATE 0xf5
#define XMOS_DFU_RESTORESTATE 0xf6
static libusb_device_handle *devh = NULL;
static int find_xmos_device(unsigned int id)
{
libusb_device *dev;
libusb_device **devs;
int i = 0;
int found = 0;
libusb_get_device_list(NULL, &devs);
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(dev, &desc);
printf("VID = 0x%x, PID = 0x%x\n", desc.idVendor, desc.idProduct);
if (desc.idVendor == XMOS_VID &&
((desc.idProduct == XMOS_L1_AUDIO1_PID) ||
(desc.idProduct == XMOS_L1_AUDIO2_PID) ||
(desc.idProduct == XMOS_SU1_AUDIO2_PID) ||
(desc.idProduct == XMOS_L2_AUDIO2_PID)))
{
if (found == id)
{
if (libusb_open(dev, &devh) < 0)
{
return -1;
}
else
{
libusb_config_descriptor *config_desc = NULL;
libusb_get_active_config_descriptor(dev, &config_desc);
if (config_desc != NULL)
{
for (int j = 0; j < config_desc->bNumInterfaces; j++)
{
const libusb_interface_descriptor *inter_desc = ((libusb_interface *)&config_desc->interface[j])->altsetting;
if (inter_desc->bInterfaceClass == 0xFE && inter_desc->bInterfaceSubClass == 0x1)
{
XMOS_DFU_IF = j;
}
}
}
else
{
XMOS_DFU_IF = 0;
}
}
break;
}
found++;
}
}
libusb_free_device_list(devs, 1);
return devh ? 0 : -1;
}
int xmos_dfu_resetdevice(void) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETDEVICE, 0, 0, NULL, 0, 0);
}
int xmos_dfu_revertfactory(void) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_REVERTFACTORY, 0, 0, NULL, 0, 0);
}
int xmos_dfu_resetintodfu(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETINTODFU, 0, interface, NULL, 0, 0);
}
int xmos_dfu_resetfromdfu(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESETFROMDFU, 0, interface, NULL, 0, 0);
}
int dfu_detach(unsigned int interface, unsigned int timeout) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DETACH, timeout, interface, NULL, 0, 0);
return 0;
}
int dfu_getState(unsigned int interface, unsigned char *state) {
libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATE, 0, interface, state, 1, 0);
return 0;
}
int dfu_getStatus(unsigned int interface, unsigned char *state, unsigned int *timeout,
unsigned char *nextState, unsigned char *strIndex) {
unsigned int data[2];
libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_GETSTATUS, 0, interface, (unsigned char *)data, 6, 0);
*state = data[0] & 0xff;
*timeout = (data[0] >> 8) & 0xffffff;
*nextState = data[1] & 0xff;
*strIndex = (data[1] >> 8) & 0xff;
return 0;
}
int dfu_clrStatus(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_CLRSTATUS, 0, interface, NULL, 0, 0);
return 0;
}
int dfu_abort(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_ABORT, 0, interface, NULL, 0, 0);
return 0;
}
int xmos_dfu_save_state(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_SAVESTATE, 0, interface, NULL, 0, 0);
printf("Save state command sent\n");
return 0;
}
int xmos_dfu_restore_state(unsigned int interface) {
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, XMOS_DFU_RESTORESTATE, 0, interface, NULL, 0, 0);
printf("Restore state command sent\n");
return 0;
}
int dfu_download(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char *data) {
//printf("... Downloading block number %d size %d\r", block_num, size);
libusb_control_transfer(devh, DFU_REQUEST_TO_DEV, DFU_DNLOAD, block_num, interface, data, size, 0);
return 0;
}
int dfu_upload(unsigned int interface, unsigned int block_num, unsigned int size, unsigned char*data) {
unsigned int numBytes = 0;
numBytes = libusb_control_transfer(devh, DFU_REQUEST_FROM_DEV, DFU_UPLOAD, block_num, interface, (unsigned char *)data, size, 0);
return numBytes;
}
int write_dfu_image(char *file) {
int i = 0;
FILE* inFile = NULL;
int image_size = 0;
unsigned int num_blocks = 0;
unsigned int block_size = 64;
unsigned int remainder = 0;
unsigned char block_data[256];
unsigned char dfuState = 0;
unsigned char nextDfuState = 0;
unsigned int timeout = 0;
unsigned char strIndex = 0;
unsigned int dfuBlockCount = 0;
inFile = fopen( file, "rb" );
if( inFile == NULL ) {
fprintf(stderr,"Error: Failed to open input data file.\n");
return -1;
}
/* Discover the size of the image. */
if( 0 != fseek( inFile, 0, SEEK_END ) ) {
fprintf(stderr,"Error: Failed to discover input data file size.\n");
return -1;
}
image_size = (int)ftell( inFile );
if( 0 != fseek( inFile, 0, SEEK_SET ) ) {
fprintf(stderr,"Error: Failed to input file pointer.\n");
return -1;
}
num_blocks = image_size/block_size;
remainder = image_size - (num_blocks * block_size);
printf("... Downloading image (%s) to device\n", file);
dfuBlockCount = 0;
for (i = 0; i < num_blocks; i++) {
memset(block_data, 0x0, block_size);
fread(block_data, 1, block_size, inFile);
dfu_download(0, dfuBlockCount, block_size, block_data);
dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex);
dfuBlockCount++;
}
if (remainder) {
memset(block_data, 0x0, block_size);
fread(block_data, 1, remainder, inFile);
dfu_download(0, dfuBlockCount, block_size, block_data);
dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex);
}
// 0 length download terminates
dfu_download(0, 0, 0, NULL);
dfu_getStatus(0, &dfuState, &timeout, &nextDfuState, &strIndex);
printf("... Download complete\n");
return 0;
}
int read_dfu_image(char *file) {
FILE *outFile = NULL;
unsigned int block_count = 0;
unsigned int block_size = 64;
unsigned char block_data[64];
outFile = fopen( file, "wb" );
if( outFile == NULL ) {
fprintf(stderr,"Error: Failed to open output data file.\n");
return -1;
}
printf("... Uploading image (%s) from device\n", file);
while (1) {
unsigned int numBytes = 0;
numBytes = dfu_upload(0, block_count, 64, block_data);
if (numBytes == 0)
break;
fwrite(block_data, 1, block_size, outFile);
block_count++;
}
fclose(outFile);
}
int main(int argc, char **argv) {
int r = 1;
unsigned char dfuState = 0;
unsigned char nextDfuState = 0;
unsigned int timeout = 0;
unsigned char strIndex = 0;
unsigned int download = 0;
unsigned int upload = 0;
unsigned int revert = 0;
unsigned int save = 0;
unsigned int restore = 0;
char *firmware_filename = NULL;
if (argc < 2) {
fprintf(stderr, "No options passed to dfu application\n");
return -1;
}
if (strcmp(argv[1], "--download") == 0) {
if (argv[2]) {
firmware_filename = argv[2];
} else {
fprintf(stderr, "No filename specified for download option\n");
return -1;
}
download = 1;
} else if (strcmp(argv[1], "--upload") == 0) {
if (argv[2]) {
firmware_filename = argv[2];
} else {
fprintf(stderr, "No filename specified for upload option\n");
return -1;
}
upload = 1;
} else if (strcmp(argv[1], "--revertfactory") == 0) {
revert = 1;
}
else if(strcmp(argv[1], "--savecustomstate") == 0)
{
save = 1;
}
else if(strcmp(argv[1], "--restorecustomstate") == 0)
{
restore = 1;
}
else {
fprintf(stderr, "Invalid option passed to dfu application\n");
return -1;
}
r = libusb_init(NULL);
if (r < 0) {
fprintf(stderr, "failed to initialise libusb\n");
return -1;
}
r = find_xmos_device(0);
if (r < 0) {
fprintf(stderr, "Could not find/open device\n");
return -1;
}
r = libusb_claim_interface(devh, XMOS_DFU_IF);
if (r < 0) {
fprintf(stderr, "Error claiming interface %d %d\n", XMOS_DFU_IF, r);
return -1;
}
printf("XMOS DFU application started - Interface %d claimed\n", XMOS_DFU_IF);
/* Dont go into DFU mode for save/restore */
if(save)
{
xmos_dfu_save_state(XMOS_DFU_IF);
}
else if(restore)
{
xmos_dfu_restore_state(XMOS_DFU_IF);
}
else
{
printf("Detaching device from application mode.\n");
xmos_dfu_resetintodfu(XMOS_DFU_IF);
libusb_release_interface(devh, XMOS_DFU_IF);
libusb_close(devh);
printf("Waiting for device to restart and enter DFU mode...\n");
// Wait for device to enter dfu mode and restart
system("sleep 20");
// NOW IN DFU APPLICATION MODE
r = find_xmos_device(0);
if (r < 0) {
fprintf(stderr, "Could not find/open device\n");
return -1;
}
r = libusb_claim_interface(devh, 0);
if (r != 0)
{
fprintf(stderr, "Error claiming interface 0\n");
switch(r)
{
case LIBUSB_ERROR_NOT_FOUND:
printf("The requested interface does not exist'n");
break;
case LIBUSB_ERROR_BUSY:
printf("Another program or driver has claimed the interface\n");
break;
case LIBUSB_ERROR_NO_DEVICE:
printf("The device has been disconnected\n");
break;
case LIBUSB_ERROR_ACCESS:
printf("Access denied\n");
break;
default:
printf("Unknown error code: %d\n", r);
break;
}
return -1;
}
printf("... DFU firmware upgrade device opened\n");
if (download) {
write_dfu_image(firmware_filename);
xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} else if (upload) {
read_dfu_image(firmware_filename);
xmos_dfu_resetfromdfu(XMOS_DFU_IF);
} else if (revert) {
printf("... Reverting device to factory image\n");
xmos_dfu_revertfactory();
// Give device time to revert firmware
system("sleep 2");
xmos_dfu_resetfromdfu(XMOS_DFU_IF);
}
else{
xmos_dfu_resetfromdfu(XMOS_DFU_IF);
}
printf("... Returning device to application mode\n");
}
// END OF DFU APPLICATION MODE
libusb_release_interface(devh, 0);
libusb_close(devh);
libusb_exit(NULL);
return true;
}