OpenCores
URL https://opencores.org/ocsvn/adv_debug_sys/adv_debug_sys/trunk

Subversion Repositories adv_debug_sys

[/] [adv_debug_sys/] [trunk/] [Software/] [adv_jtag_bridge/] [cable_usbblaster.c] - Blame information for rev 55

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 nyawn
/* cable_usbblaster.c - Altera USB Blaster driver for the Advanced JTAG Bridge
2 32 nyawn
   Copyright (C) 2008 - 2010 Nathan Yawn, nathan.yawn@opencores.org
3 4 nyawn
 
4
   This program is free software; you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation; either version 2 of the License, or
7
   (at your option) any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
   GNU General Public License for more details.
13
 
14
   You should have received a copy of the GNU General Public License
15
   along with this program; if not, write to the Free Software
16
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
 
18
 
19
#include <stdio.h>
20
#include <sys/types.h>
21
#include <unistd.h>  // for usleep()
22
#include <stdlib.h>  // for sleep()
23
#include <arpa/inet.h> // for htons()
24 32 nyawn
#include <string.h>  // for memcpy()
25 4 nyawn
 
26
#include "usb.h"  // libusb header
27 55 nyawn
 
28
#include "cable_usbblaster.h"
29 4 nyawn
#include "errcodes.h"
30
 
31
#define debug(...) //fprintf(stderr, __VA_ARGS__ )
32
 
33 55 nyawn
jtag_cable_t usbblaster_cable_driver = {
34
    .name = "usbblaster",
35
    .inout_func = cable_usbblaster_inout,
36
    .out_func = cable_usbblaster_out,
37
    .init_func =cable_usbblaster_init ,
38
    .opt_func = cable_usbblaster_opt,
39
    .bit_out_func = cable_common_write_bit,
40
    .bit_inout_func = cable_common_read_write_bit,
41
    .stream_out_func =  cable_usbblaster_write_stream,
42
    .stream_inout_func = cable_usbblaster_read_stream,
43
    .flush_func = NULL,
44
    .opts = "",
45
    .help = "no options\n",
46
    };
47
 
48 4 nyawn
// USB constants for the USB Blaster
49
// Valid endpoints: 0x81, 0x02, 0x06, 0x88
50
#define EP2        0x02
51
#define EP1        0x81
52
#define ALTERA_VID 0x09FB
53
#define ALTERA_PID 0x6001
54
 
55
//#define USB_TIMEOUT 500
56
#define USB_TIMEOUT 10000
57
 
58
 
59
// Bit meanings in the command byte sent to the USB-Blaster
60
#define USBBLASTER_CMD_TCK 0x01
61
#define USBBLASTER_CMD_TMS 0x02
62
#define USBBLASTER_CMD_nCE 0x04  /* should be left low */
63
#define USBBLASTER_CMD_nCS 0x08  /* must be set for byte-shift mode reads to work */
64
#define USBBLASTER_CMD_TDI 0x10
65
#define USBBLASTER_CMD_OE  0x20  /* appears necessary to set it to make everything work */
66
#define USBBLASTER_CMD_READ 0x40
67
#define USBBLASTER_CMD_BYTESHIFT 0x80
68
 
69
 
70
static struct usb_device *usbblaster_device;
71 32 nyawn
static usb_dev_handle *h_device;
72 4 nyawn
 
73 32 nyawn
// libusb seems to give an error if we ask for a transfer larger than 64 bytes
74
// USBBlaster also has a max. single transaction of 63 bytes.
75
// So, size the max read and write to create 64-byte USB packets (reads have 2 useless bytes prepended)
76
#define USBBLASTER_MAX_WRITE 63
77
static char data_out_scratchpad[USBBLASTER_MAX_WRITE+1];
78
#define USBBLASTER_MAX_READ  62
79
static char data_in_scratchpad[USBBLASTER_MAX_READ+2];
80 4 nyawn
 
81 32 nyawn
int cable_usbblaster_open_cable(void);
82
void cable_usbblaster_close_cable(void);
83
//int cable_usbblaster_reopen_cable(void);
84
 
85 4 nyawn
///////////////////////////////////////////////////////////////////////////////
86
/*-------------------------------------[ USB Blaster specific functions ]---*/
87
/////////////////////////////////////////////////////////////////////////////
88
 
89
 
90
static int usbblaster_start_interface(struct usb_dev_handle *xpcu)
91
{
92
  // Need to send a VENDOR request OUT, request = GET_STATUS
93
  // Other parameters are ignored
94
  if(usb_control_msg(xpcu, (USB_ENDPOINT_OUT | USB_TYPE_VENDOR), USB_REQ_GET_STATUS,
95
                     0, 0, NULL, 0, 1000)<0)
96
    {
97
      perror("usb_control_msg(start interface)");
98
      return APP_ERR_USB;
99
    }
100
 
101
  return APP_ERR_NONE;
102
}
103
 
104
 
105
static int usbblaster_read_firmware_version(struct usb_dev_handle *xpcu, uint16_t *buf)
106
{
107
  if(usb_control_msg(xpcu, 0xC0, 0x90, 0, 3, (char*)buf, 2, USB_TIMEOUT)<0)
108
    {
109
      perror("usb_control_msg(0x90.0) (read_firmware_version)");
110
      return APP_ERR_USB;
111
    }
112
 
113
  // Swap endian
114
  *buf = htons(*buf);
115
  //*buf = (*buf << 8) | (*buf >> 8);
116
 
117
  return APP_ERR_NONE;
118
}
119
 
120
 
121
 
122
static int usbblaster_enumerate_bus(void)
123
{
124
  int             flag;  // for USB bus scanning stop condition
125
  struct usb_bus *bus;   // pointer on the USB bus
126
 
127
  // board detection
128
  usb_init();
129
  usb_find_busses();
130
  usb_find_devices();
131
 
132
  flag = 0;
133
 
134
  for (bus = usb_get_busses(); bus; bus = bus->next)
135
  {
136
    for (usbblaster_device = bus->devices; usbblaster_device; usbblaster_device = usbblaster_device->next)
137
    {
138
      if (usbblaster_device->descriptor.idVendor  == ALTERA_VID &&
139
          usbblaster_device->descriptor.idProduct == ALTERA_PID)
140
      {
141
              flag = 1;
142
              fprintf(stderr, "Found Altera USB-Blaster\n");
143
              return APP_ERR_NONE;
144
      }
145
    }
146
    if (flag)
147
      break;
148
  }
149
 
150
  fprintf(stderr, "Failed to find USB-Blaster\n");
151
  return APP_ERR_CABLENOTFOUND;
152
}
153
 
154
 
155
int cable_usbblaster_init(){
156
  int err = APP_ERR_NONE;
157
 
158
  // Process to reset the usb blaster
159
  if(err |= usbblaster_enumerate_bus()) {
160
    return err;
161
  }
162
 
163 32 nyawn
  h_device = usb_open(usbblaster_device);
164 4 nyawn
 
165
  if(h_device == NULL)
166
    {
167
      fprintf(stderr, "Init failed to open USB device for reset\n");
168
      return APP_ERR_USB;
169
    }
170
 
171
  if(usb_reset(h_device) != APP_ERR_NONE)
172
    fprintf(stderr, "Failed to reset USB Blaster\n");
173
 
174
  usb_close(h_device);
175
 
176
  // Wait for reset!!!
177
  sleep(1);
178
 
179
  // Do device initialization
180
  if(err |= usbblaster_enumerate_bus())
181
    return err;
182
 
183 32 nyawn
  err = cable_usbblaster_open_cable();
184
  if(err != APP_ERR_NONE) return err;
185 4 nyawn
 
186
  //usb_clear_halt(h_device, EP1);
187
  //usb_clear_halt(h_device, EP2);
188
 
189
  // IMPORTANT:  DO NOT SEND A REQUEST TYPE "CLASS" OR TYPE "RESERVED".  This may stall the EP.
190
 
191
  // Some clones need this before they will start processing IN/OUT requests
192
  if(usbblaster_start_interface(h_device) != APP_ERR_NONE)
193
    fprintf(stderr, "Failed to start remote interface\n");
194
 
195
  uint16_t buf;
196
  if(err |= usbblaster_read_firmware_version(h_device, &buf))
197
    {
198
      usb_close(h_device);
199
      fprintf(stderr, "Failed to read firmware version\n");
200
      return err;
201
    }
202
  else
203
    {
204
      printf("firmware version = 0x%04X (%u)\n", buf, buf);
205
    }
206
 
207
 
208
  // USB blaster is expecting us to read 2 bytes, which are useless to us...
209
  char ret[2];
210
  int rv = usb_bulk_read(h_device, EP1, ret, 2, USB_TIMEOUT);
211
  if (rv < 0){  // But if we fail, who cares?
212
    fprintf(stderr, "\nWarning: Failed to read post-init bytes from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
213
  }
214 32 nyawn
 
215 4 nyawn
  return APP_ERR_NONE;
216
}
217
 
218
 
219
int cable_usbblaster_out(uint8_t value)
220
{
221
  int             rv;                  // to catch return values of functions
222 14 nyawn
  char out;
223 4 nyawn
  int err = APP_ERR_NONE;
224
 
225 32 nyawn
  // open the device, if necessary
226
  if(h_device == NULL) {
227
    rv = cable_usbblaster_open_cable();
228
    if(rv != APP_ERR_NONE) return rv;
229 4 nyawn
  }
230
 
231
  out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS);  // Set output enable (appears necessary) and nCS (necessary for byte-shift reads)
232
 
233
  // Translate to USB blaster protocol
234
  // USB-Blaster has no TRST pin
235
  if(value & TCLK_BIT)
236
    out |= USBBLASTER_CMD_TCK;
237
  if(value & TDI_BIT)
238
    out |= USBBLASTER_CMD_TDI;
239
  if(value & TMS_BIT)
240
    out |= USBBLASTER_CMD_TMS;
241
 
242
 
243
  rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
244
  if (rv != 1){
245
    fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
246
    err |= APP_ERR_USB;
247
  }
248
 
249
  return err;
250
}
251
 
252
 
253
int cable_usbblaster_inout(uint8_t value, uint8_t *in_bit)
254
{
255
  int             rv;                  // to catch return values of functions
256
  char ret[3] = {0,0,0};               // Two useless bytes (0x31,0x60) always precede the useful byte
257 14 nyawn
  char out;
258 4 nyawn
 
259
  out = (USBBLASTER_CMD_OE | USBBLASTER_CMD_nCS);  // Set output enable (?) and nCS (necessary for byte-shift reads)
260
  out |=  USBBLASTER_CMD_READ;
261
 
262
  // Translate to USB blaster protocol
263
  // USB-Blaster has no TRST pin
264
  if(value & TCLK_BIT)
265
    out |= USBBLASTER_CMD_TCK;
266
  if(value & TDI_BIT)
267
    out |= USBBLASTER_CMD_TDI;
268
  if(value & TMS_BIT)
269
    out |= USBBLASTER_CMD_TMS;
270
 
271 32 nyawn
  // open the device, if necessary
272
  if(h_device == NULL) {
273
    rv = cable_usbblaster_open_cable();
274
    if(rv != APP_ERR_NONE) return rv;
275 4 nyawn
  }
276
 
277
  // Send a read request
278
  rv = usb_bulk_write(h_device, EP2, &out, 1, USB_TIMEOUT);
279
  if (rv != 1){
280
    fprintf(stderr, "\nFailed to write a read request to the EP2 FIFO:\n%s", usb_strerror());
281 32 nyawn
    cable_usbblaster_close_cable();
282 4 nyawn
    return APP_ERR_USB;
283
  }
284
 
285
 
286
  // receive the response
287
  // Sometimes, we do a read but just get the useless 0x31,0x60 chars...
288
  // retry until we get a 3rd byte (with real data), for a reasonable number of retries.
289
  int retries = 0;
290
  do {
291
    rv = usb_bulk_read(h_device, EP1, ret, 3, USB_TIMEOUT);
292
    if (rv < 0){
293
      fprintf(stderr, "\nFailed to read from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
294 32 nyawn
      cable_usbblaster_close_cable();
295 4 nyawn
      return APP_ERR_USB;
296
    }
297
 
298
    // fprintf(stderr, "Read %i bytes: 0x%X, 0x%X, 0x%X\n", rv, ret[0], ret[1], ret[2]);
299
    retries++;
300
  }
301
  while((rv < 3) && (retries < 20));
302
 
303
  *in_bit = (ret[2] & 0x01); /* TDO is bit 0.  USB-Blaster may also set bit 1. */
304
  return APP_ERR_NONE;
305
}
306
 
307
 
308
// The usbblaster transfers the bits in the stream in the following order:
309
// bit 0 of the first byte received ... bit 7 of the first byte received
310
// bit 0 of second byte received ... etc.
311
int cable_usbblaster_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
312
  int             rv;                  // to catch return values of functions
313
  unsigned int bytes_to_transfer, leftover_bit_length;
314
  uint32_t leftover_bits;
315 32 nyawn
  int bytes_remaining;
316
  char *xfer_ptr;
317 4 nyawn
  int err = APP_ERR_NONE;
318
 
319 32 nyawn
  debug("cable_usbblaster_write_stream(0x%X, %d, %i)\n", stream, len, set_last_bit);
320 4 nyawn
 
321
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
322
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
323
  // plus one bit to send along with TMS.
324
  bytes_to_transfer = len_bits / 8;
325
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
326
 
327
  if((!leftover_bit_length) && set_last_bit) {
328
    bytes_to_transfer -= 1;
329
    leftover_bit_length += 8;
330
  }
331
 
332 32 nyawn
  debug("bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
333 4 nyawn
 
334
  // Not enough bits for high-speed transfer. bit-bang.
335
  if(bytes_to_transfer == 0) {
336
    return cable_common_write_stream(stream, len_bits, set_last_bit);
337
  }
338
 
339
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
340 32 nyawn
  err |= cable_usbblaster_out(0);  // Lower the clock.
341 4 nyawn
 
342
  // Set leftover bits
343
  leftover_bits = (stream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
344
 
345 32 nyawn
  debug("leftover_bits: 0x%X, LSB_first_xfer = %d\n", leftover_bits, LSB_first_xfer);
346 4 nyawn
 
347 32 nyawn
  // open the device, if necessary
348
  if(h_device == NULL) {
349
    rv = cable_usbblaster_open_cable();
350
    if(rv != APP_ERR_NONE) return rv;
351 4 nyawn
  }
352
 
353 32 nyawn
  bytes_remaining = bytes_to_transfer;
354
  xfer_ptr = (char *) stream;
355
  while(bytes_remaining > 0)
356
    {
357
      int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_WRITE) ? USBBLASTER_MAX_WRITE:bytes_remaining;
358 4 nyawn
 
359 32 nyawn
      data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | (bytes_this_xfer & 0x3F);
360
      memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
361
 
362
      /* printf("Data packet: ");
363
         for(i = 0; i <= bytes_to_transfer; i++)
364
         printf("0x%X ", out[i]);
365
         printf("\n"); */
366
 
367
      rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_this_xfer+1, USB_TIMEOUT);
368
      if (rv != (bytes_this_xfer+1)){
369
        fprintf(stderr, "\nFailed to write to the FIFO (rv = %d):\n%s", rv, usb_strerror());
370
        err |= APP_ERR_USB;
371
        break;
372
      }
373 4 nyawn
 
374 32 nyawn
      bytes_remaining -= bytes_this_xfer;
375
      xfer_ptr += bytes_this_xfer;
376 4 nyawn
    }
377
 
378
  // if we have a number of bits not divisible by 8, or we need to set TMS...
379
  if(leftover_bit_length != 0) {
380
    //printf("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
381
    return cable_common_write_stream(&leftover_bits, leftover_bit_length, set_last_bit);
382
  }
383
 
384
  return err;
385
}
386
 
387
 
388
int cable_usbblaster_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
389
  int             rv;                  // to catch return values of functions
390 32 nyawn
  unsigned int bytes_received = 0, total_bytes_received = 0;
391 4 nyawn
  unsigned int bytes_to_transfer, leftover_bit_length;
392
  uint32_t leftover_bits, leftovers_received = 0;
393 32 nyawn
  unsigned char i;
394
  int bytes_remaining;
395
  char *xfer_ptr;
396 4 nyawn
  int retval = APP_ERR_NONE;
397
 
398
  debug("cable_usbblaster_read_stream(0x%X, %d, %i)\n", outstream[0], len_bits, set_last_bit);
399
 
400
  // This routine must transfer at least 8 bits.  Additionally, TMS (the last bit)
401
  // cannot be set by 'byte shift mode'.  So we need at least 8 bits to transfer,
402
  // plus one bit to send along with TMS.
403
  bytes_to_transfer = len_bits / 8;
404
  leftover_bit_length = len_bits - (bytes_to_transfer * 8);
405
 
406
  if((!leftover_bit_length) && set_last_bit) {
407
    bytes_to_transfer -= 1;
408
    leftover_bit_length += 8;
409
  }
410
 
411 32 nyawn
  debug("RD bytes_to_transfer: %d. leftover_bit_length: %d\n", bytes_to_transfer, leftover_bit_length);
412 4 nyawn
 
413
  // Not enough bits for high-speed transfer. bit-bang.
414
  if(bytes_to_transfer == 0) {
415
    return cable_common_read_stream(outstream, instream, len_bits, set_last_bit);
416
  }
417
 
418
  // Bitbang functions leave clock high.  USBBlaster assumes clock low at the start of a burst.
419
  // Lower the clock.
420
  retval |= cable_usbblaster_out(0);
421
 
422
  // Zero the input, since we add new data by logical-OR
423 32 nyawn
  for(i = 0; i < (len_bits/32); i++)  instream[i] = 0;
424
  if(len_bits % 32)                   instream[i] = 0;
425 4 nyawn
 
426
  // Set leftover bits
427
  leftover_bits = (outstream[bytes_to_transfer>>2] >> ((bytes_to_transfer & 0x3) * 8)) & 0xFF;
428
  debug("leftover_bits: 0x%X\n", leftover_bits);
429
 
430 32 nyawn
  // open the device, if necessary
431
  if(h_device == NULL) {
432
    rv = cable_usbblaster_open_cable();
433
    if(rv != APP_ERR_NONE) return rv;
434 4 nyawn
  }
435
 
436 32 nyawn
  // Transfer the data.  USBBlaster has a max transfer size of 64 bytes.
437
  bytes_remaining = bytes_to_transfer;
438
  xfer_ptr = (char *) outstream;
439
  total_bytes_received = 0;
440
  while(bytes_remaining > 0)
441
    {
442
      int bytes_this_xfer = (bytes_remaining > USBBLASTER_MAX_READ) ? USBBLASTER_MAX_READ:bytes_remaining;
443
      data_out_scratchpad[0] = USBBLASTER_CMD_BYTESHIFT | USBBLASTER_CMD_READ | (bytes_this_xfer & 0x3F);
444
      memcpy(&data_out_scratchpad[1], xfer_ptr, bytes_this_xfer);
445 4 nyawn
 
446 32 nyawn
      /* debug("Data packet: ");
447
         for(i = 0; i <= bytes_to_transfer; i++) debug("0x%X ", data_out_scratchpad[i]);
448
         debug("\n"); */
449 4 nyawn
 
450 32 nyawn
      rv = usb_bulk_write(h_device, EP2, data_out_scratchpad, bytes_this_xfer+1, USB_TIMEOUT);
451
      if (rv != (bytes_this_xfer+1)){
452
        fprintf(stderr, "\nFailed to write to the EP2 FIFO (rv = %d):\n%s", rv, usb_strerror());
453
        cable_usbblaster_close_cable();
454
        return APP_ERR_USB;
455
      }
456 4 nyawn
 
457 32 nyawn
      // receive the response
458
      // Sometimes, we do a read but just get the useless 0x31,0x60 chars...
459
      // retry until we get at least 3 bytes (with real data), for a reasonable number of retries.
460
      int retries = 0;
461
      bytes_received = 0;
462
      do {
463
        debug("stream read, bytes_this_xfer = %i, bytes_received = %i\n", bytes_this_xfer, bytes_received);
464
        rv = usb_bulk_read(h_device, EP1, data_in_scratchpad, (bytes_this_xfer-bytes_received)+2, USB_TIMEOUT);
465
        if (rv < 0){
466
          fprintf(stderr, "\nFailed to read stream from the EP1 FIFO (%i):\n%s", rv, usb_strerror());
467
          cable_usbblaster_close_cable();
468
          return APP_ERR_USB;
469
        }
470 4 nyawn
 
471 32 nyawn
        /* debug("Read %i bytes: ", rv);
472
           for(i = 0; i < rv; i++)
473
           debug("0x%X ", data_in_scratchpad[i]);
474
           debug("\n"); */
475
 
476
        if(rv > 2) retries = 0;
477
        else retries++;
478
 
479
        /* Put the received bytes into the return stream.  Works for either endian. */
480
        for(i = 0; i < (rv-2); i++) {
481
          // Do size/type promotion before shift.  Must cast to unsigned, else the value may be
482
          // sign-extended through the upper 16 bits of the uint32_t.
483
          uint32_t tmp = (unsigned char) data_in_scratchpad[2+i];
484
          instream[(total_bytes_received+i)>>2] |= (tmp << ((i & 0x3)*8));
485
        }
486 4 nyawn
 
487 32 nyawn
        bytes_received += (rv-2);
488
        total_bytes_received += (rv-2);
489
      }
490
      while((bytes_received < bytes_this_xfer) && (retries < 15));
491 4 nyawn
 
492 32 nyawn
      bytes_remaining -= bytes_this_xfer;
493
      xfer_ptr += bytes_this_xfer;
494 4 nyawn
    }
495
 
496
  // if we have a number of bits not divisible by 8
497
  if(leftover_bit_length != 0) {
498
    debug("Doing leftovers: (0x%X, %d, %d)\n", leftover_bits, leftover_bit_length, set_last_bit);
499
    retval |= cable_common_read_stream(&leftover_bits, &leftovers_received, leftover_bit_length, set_last_bit);
500
    instream[bytes_to_transfer>>2] |= (leftovers_received & 0xFF) << (8*(bytes_to_transfer & 0x3));
501
  }
502
 
503
  return retval;
504
}
505
 
506 55 nyawn
jtag_cable_t *cable_usbblaster_get_driver(void)
507
{
508
  return &usbblaster_cable_driver;
509
}
510 4 nyawn
 
511
int cable_usbblaster_opt(int c, char *str)
512
{
513
  fprintf(stderr, "Unknown parameter '%c'\n", c);
514
  return APP_ERR_BAD_PARAM;
515
}
516
 
517
 
518 32 nyawn
int cable_usbblaster_open_cable(void)
519
{
520
  // open the device
521
  h_device = usb_open(usbblaster_device);
522
  if (h_device == NULL){
523
        fprintf(stderr, "USBBlaster driver failed to open device\n");
524
        return APP_ERR_USB;
525
  }
526
 
527
  // set the configuration
528
  if (usb_set_configuration(h_device, usbblaster_device->config->bConfigurationValue))
529
  {
530
        usb_close(h_device);
531
        h_device = NULL;
532
        fprintf(stderr, "USBBlaster driver failed to set configuration\n");
533
        return APP_ERR_USB;
534
  }
535
 
536
  // wait until device is ready
537
  // *** TODO: add timeout
538
  while (usb_claim_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber));
539
 
540
  return APP_ERR_NONE;
541
}
542
 
543
 
544
void cable_usbblaster_close_cable(void)
545
{
546
  if(h_device != NULL) {
547
    // release the interface cleanly
548
    if (usb_release_interface(h_device, usbblaster_device->config->interface->altsetting->bInterfaceNumber)){
549
      fprintf(stderr, "Warning: failed to release usb interface\n");
550
    }
551
 
552
    // close the device
553
    usb_close(h_device);
554
    h_device = NULL;
555
  }
556
 
557
  return;
558
}
559
 
560
/*
561
int cable_usbblaster_reopen_cable(void)
562
{
563
  cable_usbblaster_close_cable();
564
  return cable_usbblaster_open_cable();
565
}
566
*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.