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 58

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

powered by: WebSVN 2.1.0

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