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

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [soc/] [sw/] [adv_jtag_bridge/] [cable_common.c] - Blame information for rev 12

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

Line No. Rev Author Line
1 12 xianfeng
/* 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
   Copyright (C) 2008 Nathan Yawn, nathan.yawn@opencores.org
5
 
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
  { "xpc3",
78
    cable_xpc3_inout,
79
    cable_xpc3_out,
80
    cable_parallel_init,
81
    cable_parallel_phys_wait,
82
    cable_parallel_opt,
83
    cable_common_write_bit,
84
    cable_common_read_write_bit,
85
    cable_common_write_stream,
86
    cable_common_read_stream,
87
    NULL,
88
    "p:",
89
    "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n" },
90
  { "xess",
91
    cable_xess_inout,
92
    cable_xess_out,
93
    cable_parallel_init,
94
    cable_parallel_phys_wait,
95
    cable_parallel_opt,
96
    cable_common_write_bit,
97
    cable_common_read_write_bit,
98
    cable_common_write_stream,
99
    cable_common_read_stream,
100
    NULL,
101
    "p:",
102
    "-p [port] Which port to use when communicating with the parport hardware (eg. 0x378)\n" },
103
#ifdef __SUPPORT_USB_CABLES__
104
  { "usbblaster",
105
    cable_usbblaster_inout,
106
    cable_usbblaster_out,
107
    cable_usbblaster_init,
108
    NULL,
109
    cable_usbblaster_opt,
110
    cable_common_write_bit,
111
    cable_common_read_write_bit,
112
    cable_usbblaster_write_stream,
113
    cable_usbblaster_read_stream,
114
    NULL,
115
    "",
116
    "no options\n" },
117
#ifdef __SUPPORT_FTDI_CABLES__
118
  { "usbblaster_ftdi",
119
    ftdi_usb_blaster_inout,
120
    ftdi_usb_blaster_out,
121
    ftdi_usb_blaster_init,
122
    NULL,
123
    cable_usbblaster_opt,
124
    cable_common_write_bit,
125
    cable_common_read_write_bit,
126
    cable_common_write_stream,
127
    cable_common_read_stream,
128
    NULL,
129
    "",
130
    "no options\n" },
131
#endif
132
  { "xpc_usb",
133
    cable_xpcusb_inout,
134
    cable_xpcusb_out,
135
    cable_xpcusb_init,
136
    NULL,
137
    cable_xpcusb_opt,
138
    cable_common_write_bit,
139
    cable_xpcusb_read_write_bit,
140
    cable_common_write_stream,
141
    cable_common_read_stream,
142
    NULL,
143
    "",
144
    "no options\n" },
145
#ifdef __SUPPORT_FTDI_CABLES__
146
  { "ft2232",
147
    NULL,
148
    NULL,
149
    cable_ftdi_init,
150
    NULL,
151
    cable_ftdi_opt,
152
    cable_ftdi_write_bit,
153
    cable_ftdi_read_write_bit,
154
    cable_ftdi_write_stream,
155
    cable_ftdi_read_stream,
156
    cable_ftdi_flush,
157
    "",
158
    "no options\n" },
159
#endif  // __SUPPORT_FTDI_CABLES__
160
#endif  // __SUPPORT_USB_CABLES__
161
  { NULL, NULL, NULL, NULL,
162
    NULL, NULL, NULL, NULL,
163
    NULL, NULL, NULL, NULL, NULL } };
164
 
165
static struct jtag_cable *jtag_cable_in_use = NULL; /* The currently selected cable */
166
 
167
 
168
/////////////////////////////////////////////////////////////////////////////////////
169
// Cable subsystem / init functions
170
 
171
 
172
/* Selects a cable for use */
173
int cable_select(const char *cable)
174
{
175
  int i;
176
 
177
  for(i = 0; jtag_cables[i].name; i++) {
178
    if(!strcmp(cable, jtag_cables[i].name)) {
179
      jtag_cable_in_use = &jtag_cables[i];
180
      return APP_ERR_NONE;
181
    }
182
  }
183
 
184
  return APP_ERR_CABLE_INVALID;
185
}
186
 
187
/* Calls the init function of the cable
188
 */
189
int cable_init()
190
{
191
  return jtag_cable_in_use->init_func();
192
}
193
 
194
/* Parses command-line options specific to the selected cable */
195
int cable_parse_opt(int c, char *str)
196
{
197
  return jtag_cable_in_use->opt_func(c, str);
198
}
199
 
200
const char *cable_get_args()
201
{
202
  if(jtag_cable_in_use != NULL)
203
    return jtag_cable_in_use->opts;
204
  else
205
    return NULL;
206
}
207
 
208
/* Prints a (short) useage message for each available cable */
209
void cable_print_help()
210
{
211
  int i;
212
  printf("Available cables: ");
213
 
214
  for(i = 0; jtag_cables[i].name; i++) {
215
    if(i)
216
      printf(", ");
217
    printf("%s", jtag_cables[i].name);
218
  }
219
 
220
  printf("\n\nOptions availible for the cables:\n");
221
  for(i = 0; jtag_cables[i].name; i++) {
222
    if(!jtag_cables[i].help)
223
      continue;
224
    printf("  %s:\n    %s", jtag_cables[i].name, jtag_cables[i].help);
225
  }
226
}
227
 
228
 
229
/////////////////////////////////////////////////////////////////////////////////
230
// Cable API Functions
231
 
232
int cable_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
233
  return jtag_cable_in_use->stream_out_func(stream, len_bits, set_last_bit);
234
}
235
 
236
int cable_read_write_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
237
  return jtag_cable_in_use->stream_inout_func(outstream, instream, len_bits, set_last_bit);
238
}
239
 
240
int cable_write_bit(uint8_t packet) {
241
  return jtag_cable_in_use->bit_out_func(packet);
242
}
243
 
244
int cable_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
245
  return jtag_cable_in_use->bit_inout_func(packet_out, bit_in);
246
}
247
 
248
int cable_flush(void) {
249
  if(jtag_cable_in_use->flush_func != NULL)
250
    return jtag_cable_in_use->flush_func();
251
  return APP_ERR_NONE;
252
}
253
 
254
 
255
/////////////////////////////////////////////////////////////////////////////////////
256
// Common functions which may or may not be used by individual drivers
257
 
258
 
259
/* Note that these make no assumption as to the starting state of the clock,
260
 * and they leave the clock HIGH.  But, these need to interface with other routines (like
261
 * the byte-shift mode in the USB-Blaster), which begin by assuming that a new
262
 * data bit is available at TDO, which only happens after a FALLING edge of TCK.
263
 * So, routines which assume new data is available will need to start by dropping
264
 * the clock.
265
 */
266
int cable_common_write_bit(uint8_t packet) {
267
  uint8_t data = TRST_BIT;  // TRST is active low, don't clear unless /set/ in 'packet'
268
  int err = APP_ERR_NONE;
269
 
270
  /* Write data, drop clock */
271
  if(packet & TDO) data |= TDI_BIT;
272
  if(packet & TMS) data |= TMS_BIT;
273
  if(packet & TRST) data &= ~TRST_BIT;
274
 
275
  err |= jtag_cable_in_use->out_func(data);
276
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
277
 
278
  /* raise clock, to do write */
279
  err |= jtag_cable_in_use->out_func(data | TCLK_BIT);
280
   if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
281
 
282
  return err;
283
}
284
 
285
int cable_common_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
286
  uint8_t data = TRST_BIT;  //  TRST is active low, don't clear unless /set/ in 'packet'
287
  int err = APP_ERR_NONE;
288
 
289
  /* Write data, drop clock */
290
  if(packet_out & TDO) data |= TDI_BIT;
291
  if(packet_out & TMS) data |= TMS_BIT;
292
  if(packet_out & TRST) data &= ~TRST_BIT;
293
 
294
  err |= jtag_cable_in_use->out_func(data);  // drop the clock to make data available, set the out data
295
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
296
  err |= jtag_cable_in_use->inout_func((data | TCLK_BIT), bit_in);  // read in bit, clock high for out bit.
297
  if (jtag_cable_in_use->wait_func != NULL) jtag_cable_in_use->wait_func();
298
 
299
  return err;
300
}
301
 
302
 
303
/* Writes bitstream via bit-bang. Can be used by any driver which does not have a high-speed transfer function.
304
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
305
 */
306
int cable_common_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
307
  int i;
308
  int index = 0;
309
  int bits_this_index = 0;
310
  uint8_t out;
311
  int err = APP_ERR_NONE;
312
 
313
  debug("writeSrrm%d(", len_bits);
314
  for(i = 0; i < len_bits - 1; i++) {
315
    out = (stream[index] >> bits_this_index) & 1;
316
    err |= cable_write_bit(out);
317
    debug("%i", out);
318
    bits_this_index++;
319
    if(bits_this_index >= 32) {
320
      index++;
321
      bits_this_index = 0;
322
    }
323
  }
324
 
325
  out = (stream[index] >>(len_bits - 1)) & 0x1;
326
  if(set_last_bit) out |= TMS;
327
  err |= cable_write_bit(out);
328
  debug("%i)\n", out);
329
  return err;
330
}
331
 
332
/* Gets bitstream via bit-bang.  Can be used by any driver which does not have a high-speed transfer function.
333
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
334
 */
335
int cable_common_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
336
  int i;
337
  int index = 0;
338
  int bits_this_index = 0;
339
  uint8_t inval, outval;
340
  int err = APP_ERR_NONE;
341
 
342
  instream[0] = 0;
343
 
344
  debug("readStrm%d(", len_bits);
345
  for(i = 0; i < (len_bits - 1); i++) {
346
    outval = (outstream[index] >> bits_this_index) & 0x1;
347
    err |= cable_read_write_bit(outval, &inval);
348
    debug("%i", inval);
349
    instream[index] |= (inval << bits_this_index);
350
    bits_this_index++;
351
    if(bits_this_index >= 32) {
352
      index++;
353
      bits_this_index = 0;
354
      instream[index] = 0;  // It's safe to do this, because there's always at least one more bit
355
    }
356
  }
357
 
358
  if (set_last_bit)
359
    outval = ((outstream[index] >> (len_bits - 1)) & 1) | TMS;
360
  else
361
    outval = (outstream[index] >> (len_bits - 1)) & 1;
362
 
363
  err |= cable_read_write_bit(outval, &inval);
364
  debug("%i", inval);
365
  instream[index] |= (inval << bits_this_index);
366
 
367
  debug(") = 0x%lX\n", instream[0]);
368
 
369
  return err;
370
}
371
 
372
 
373
 

powered by: WebSVN 2.1.0

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