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_common.c] - Blame information for rev 51

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

Line No. Rev Author Line
1 21 nyawn
/* cable_common.c -- Interface to the low-level cable drivers
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
   Copyright (C) 2004 György Jeney, nog@sdf.lonestar.org
4 32 nyawn
   Copyright (C) 2008 - 2010 Nathan Yawn, nathan.yawn@opencores.org
5 21 nyawn
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
 
21
 
22
#include <stdio.h>
23
#include <string.h>
24
 
25
 
26
#include "cable_common.h"
27
#include "cable_parallel.h"
28
#include "cable_sim.h"
29
#include "cable_usbblaster.h"
30
#include "cable_ft2232.h"
31
#include "cable_xpc_dlc9.h"
32
#include "errcodes.h"
33
 
34
#define debug(...)   //fprintf(stderr, __VA_ARGS__ )
35
 
36
static struct jtag_cable {
37
  const char *name;
38
  int (*inout_func)(uint8_t, uint8_t *);
39
  int (*out_func)(uint8_t);
40
  int (*init_func)();
41
  void (*wait_func)();
42
  int (*opt_func)(int, char *);
43
  int (*bit_out_func)(uint8_t);
44
  int (*bit_inout_func)(uint8_t, uint8_t *);
45
  int (*stream_out_func)(uint32_t *, int, int);
46
  int (*stream_inout_func)(uint32_t *, uint32_t *, int, int);
47
  int (*flush_func)();
48
  const char *opts;
49
  const char *help;
50
} jtag_cables[] = {
51
  { "rtl_sim",
52
    cable_rtl_sim_inout,
53
    cable_rtl_sim_out,
54
    cable_rtl_sim_init,
55
    NULL,
56
    cable_rtl_sim_opt,
57
    cable_common_write_bit,
58
    cable_common_read_write_bit,
59
    cable_common_write_stream,
60
    cable_common_read_stream,
61
    NULL,
62
    "d:",
63
    "-d [directory] Directory in which gdb_in.dat/gdb_out.dat may be found\n" },
64
  { "vpi",
65
    cable_vpi_inout,
66
    cable_vpi_out,
67
    cable_vpi_init,
68
    cable_vpi_wait,
69
    cable_vpi_opt,
70
    cable_common_write_bit,
71
    cable_common_read_write_bit,
72
    cable_common_write_stream,
73
    cable_common_read_stream,
74
    NULL,
75
    "s:p:",
76
    "-p [port] Port number that the VPI module is listening on\n\t-s [server] Server that the VPI module is running on\n" },
77 51 nyawn
#ifdef __SUPPORT_PARALLEL_CABLES__
78 21 nyawn
  { "xpc3",
79
    cable_xpc3_inout,
80
    cable_xpc3_out,
81
    cable_parallel_init,
82
    cable_parallel_phys_wait,
83
    cable_parallel_opt,
84
    cable_common_write_bit,
85
    cable_common_read_write_bit,
86
    cable_common_write_stream,
87
    cable_common_read_stream,
88
    NULL,
89
    "p:",
90
    "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n" },
91
  { "xess",
92
    cable_xess_inout,
93
    cable_xess_out,
94
    cable_parallel_init,
95
    cable_parallel_phys_wait,
96
    cable_parallel_opt,
97
    cable_common_write_bit,
98
    cable_common_read_write_bit,
99
    cable_common_write_stream,
100
    cable_common_read_stream,
101
    NULL,
102
    "p:",
103
    "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n" },
104 51 nyawn
#endif
105 21 nyawn
#ifdef __SUPPORT_USB_CABLES__
106
  { "usbblaster",
107
    cable_usbblaster_inout,
108
    cable_usbblaster_out,
109
    cable_usbblaster_init,
110
    NULL,
111
    cable_usbblaster_opt,
112
    cable_common_write_bit,
113
    cable_common_read_write_bit,
114
    cable_usbblaster_write_stream,
115
    cable_usbblaster_read_stream,
116
    NULL,
117
    "",
118
    "no options\n" },
119
  { "xpc_usb",
120
    cable_xpcusb_inout,
121
    cable_xpcusb_out,
122
    cable_xpcusb_init,
123
    NULL,
124
    cable_xpcusb_opt,
125
    cable_common_write_bit,
126
    cable_xpcusb_read_write_bit,
127
    cable_common_write_stream,
128
    cable_common_read_stream,
129
    NULL,
130
    "",
131
    "no options\n" },
132
#ifdef __SUPPORT_FTDI_CABLES__
133
  { "ft2232",
134
    NULL,
135
    NULL,
136
    cable_ftdi_init,
137
    NULL,
138
    cable_ftdi_opt,
139
    cable_ftdi_write_bit,
140
    cable_ftdi_read_write_bit,
141
    cable_ftdi_write_stream,
142
    cable_ftdi_read_stream,
143
    cable_ftdi_flush,
144
    "",
145
    "no options\n" },
146
#endif  // __SUPPORT_FTDI_CABLES__
147
#endif  // __SUPPORT_USB_CABLES__
148
  { NULL, NULL, NULL, NULL,
149
    NULL, NULL, NULL, NULL,
150
    NULL, NULL, NULL, NULL, NULL } };
151
 
152
static struct jtag_cable *jtag_cable_in_use = NULL; /* The currently selected cable */
153
 
154
 
155
/////////////////////////////////////////////////////////////////////////////////////
156
// Cable subsystem / init functions
157
 
158
 
159
/* Selects a cable for use */
160
int cable_select(const char *cable)
161
{
162
  int i;
163
 
164
  for(i = 0; jtag_cables[i].name; i++) {
165
    if(!strcmp(cable, jtag_cables[i].name)) {
166
      jtag_cable_in_use = &jtag_cables[i];
167
      return APP_ERR_NONE;
168
    }
169
  }
170
 
171
  return APP_ERR_CABLE_INVALID;
172
}
173
 
174
/* Calls the init function of the cable
175
 */
176
int cable_init()
177
{
178
  return jtag_cable_in_use->init_func();
179
}
180
 
181
/* Parses command-line options specific to the selected cable */
182
int cable_parse_opt(int c, char *str)
183
{
184
  return jtag_cable_in_use->opt_func(c, str);
185
}
186
 
187
const char *cable_get_args()
188
{
189
  if(jtag_cable_in_use != NULL)
190
    return jtag_cable_in_use->opts;
191
  else
192
    return NULL;
193
}
194
 
195
/* Prints a (short) useage message for each available cable */
196
void cable_print_help()
197
{
198
  int i;
199
  printf("Available cables: ");
200
 
201
  for(i = 0; jtag_cables[i].name; i++) {
202
    if(i)
203
      printf(", ");
204
    printf("%s", jtag_cables[i].name);
205
  }
206
 
207
  printf("\n\nOptions availible for the cables:\n");
208
  for(i = 0; jtag_cables[i].name; i++) {
209
    if(!jtag_cables[i].help)
210
      continue;
211
    printf("  %s:\n    %s", jtag_cables[i].name, jtag_cables[i].help);
212
  }
213
}
214
 
215
 
216
/////////////////////////////////////////////////////////////////////////////////
217
// Cable API Functions
218
 
219
int cable_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
220
  return jtag_cable_in_use->stream_out_func(stream, len_bits, set_last_bit);
221
}
222
 
223
int cable_read_write_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
224
  return jtag_cable_in_use->stream_inout_func(outstream, instream, len_bits, set_last_bit);
225
}
226
 
227
int cable_write_bit(uint8_t packet) {
228
  return jtag_cable_in_use->bit_out_func(packet);
229
}
230
 
231
int cable_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
232
  return jtag_cable_in_use->bit_inout_func(packet_out, bit_in);
233
}
234
 
235
int cable_flush(void) {
236
  if(jtag_cable_in_use->flush_func != NULL)
237
    return jtag_cable_in_use->flush_func();
238
  return APP_ERR_NONE;
239
}
240
 
241
 
242
/////////////////////////////////////////////////////////////////////////////////////
243
// Common functions which may or may not be used by individual drivers
244
 
245
 
246
/* Note that these make no assumption as to the starting state of the clock,
247
 * and they leave the clock HIGH.  But, these need to interface with other routines (like
248
 * the byte-shift mode in the USB-Blaster), which begin by assuming that a new
249
 * data bit is available at TDO, which only happens after a FALLING edge of TCK.
250
 * So, routines which assume new data is available will need to start by dropping
251
 * the clock.
252
 */
253
int cable_common_write_bit(uint8_t packet) {
254
  uint8_t data = TRST_BIT;  // TRST is active low, don't clear unless /set/ in 'packet'
255
  int err = APP_ERR_NONE;
256
 
257
  /* Write data, drop clock */
258
  if(packet & TDO) data |= TDI_BIT;
259
  if(packet & TMS) data |= TMS_BIT;
260
  if(packet & TRST) data &= ~TRST_BIT;
261
 
262
  err |= jtag_cable_in_use->out_func(data);
263
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
264
 
265
  /* raise clock, to do write */
266
  err |= jtag_cable_in_use->out_func(data | TCLK_BIT);
267
   if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
268
 
269
  return err;
270
}
271
 
272
int cable_common_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
273
  uint8_t data = TRST_BIT;  //  TRST is active low, don't clear unless /set/ in 'packet'
274
  int err = APP_ERR_NONE;
275
 
276
  /* Write data, drop clock */
277
  if(packet_out & TDO) data |= TDI_BIT;
278
  if(packet_out & TMS) data |= TMS_BIT;
279
  if(packet_out & TRST) data &= ~TRST_BIT;
280
 
281
  err |= jtag_cable_in_use->out_func(data);  // drop the clock to make data available, set the out data
282
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
283
  err |= jtag_cable_in_use->inout_func((data | TCLK_BIT), bit_in);  // read in bit, clock high for out bit.
284
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
285
 
286
  return err;
287
}
288
 
289
 
290
/* Writes bitstream via bit-bang. Can be used by any driver which does not have a high-speed transfer function.
291
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
292
 */
293
int cable_common_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
294
  int i;
295
  int index = 0;
296
  int bits_this_index = 0;
297
  uint8_t out;
298
  int err = APP_ERR_NONE;
299
 
300
  debug("writeSrrm%d(", len_bits);
301
  for(i = 0; i < len_bits - 1; i++) {
302
    out = (stream[index] >> bits_this_index) & 1;
303
    err |= cable_write_bit(out);
304
    debug("%i", out);
305
    bits_this_index++;
306
    if(bits_this_index >= 32) {
307
      index++;
308
      bits_this_index = 0;
309
    }
310
  }
311
 
312
  out = (stream[index] >>(len_bits - 1)) & 0x1;
313
  if(set_last_bit) out |= TMS;
314
  err |= cable_write_bit(out);
315
  debug("%i)\n", out);
316
  return err;
317
}
318
 
319
/* Gets bitstream via bit-bang.  Can be used by any driver which does not have a high-speed transfer function.
320
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
321
 */
322
int cable_common_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
323
  int i;
324
  int index = 0;
325
  int bits_this_index = 0;
326
  uint8_t inval, outval;
327
  int err = APP_ERR_NONE;
328
 
329
  instream[0] = 0;
330
 
331
  debug("readStrm%d(", len_bits);
332
  for(i = 0; i < (len_bits - 1); i++) {
333
    outval = (outstream[index] >> bits_this_index) & 0x1;
334
    err |= cable_read_write_bit(outval, &inval);
335
    debug("%i", inval);
336
    instream[index] |= (inval << bits_this_index);
337
    bits_this_index++;
338
    if(bits_this_index >= 32) {
339
      index++;
340
      bits_this_index = 0;
341
      instream[index] = 0;  // It's safe to do this, because there's always at least one more bit
342
    }
343
  }
344
 
345
  if (set_last_bit)
346
    outval = ((outstream[index] >> (len_bits - 1)) & 1) | TMS;
347
  else
348
    outval = (outstream[index] >> (len_bits - 1)) & 1;
349
 
350
  err |= cable_read_write_bit(outval, &inval);
351
  debug("%i", inval);
352
  instream[index] |= (inval << bits_this_index);
353
 
354
  debug(") = 0x%lX\n", instream[0]);
355
 
356
  return err;
357
}
358
 
359
 
360
 

powered by: WebSVN 2.1.0

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