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 21

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

powered by: WebSVN 2.1.0

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