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 55

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 55 nyawn
#include "cable_sim.h"
28
 
29
#ifdef __SUPPORT_PARALLEL_CABLES__
30 21 nyawn
#include "cable_parallel.h"
31 55 nyawn
#endif
32
 
33
#ifdef __SUPPORT_USB_CABLES__ 
34 21 nyawn
#include "cable_usbblaster.h"
35
#include "cable_xpc_dlc9.h"
36 55 nyawn
 
37
 #ifdef __SUPPORT_FTDI_CABLES__ 
38
 #include "cable_ft245.h"
39
 #include "cable_ft2232.h"
40
 #endif
41
 
42
#endif
43
 
44 21 nyawn
#include "errcodes.h"
45
 
46
#define debug(...)   //fprintf(stderr, __VA_ARGS__ )
47
 
48 55 nyawn
#define JTAG_MAX_CABLES 16
49
jtag_cable_t *jtag_cables[JTAG_MAX_CABLES];
50 21 nyawn
 
51 55 nyawn
static jtag_cable_t *jtag_cable_in_use = NULL; /* The currently selected cable */
52 21 nyawn
 
53
 
54
/////////////////////////////////////////////////////////////////////////////////////
55
// Cable subsystem / init functions
56
 
57 55 nyawn
void cable_setup(void)
58
{
59
  int i = 0;
60 21 nyawn
 
61 55 nyawn
  memset(jtag_cables, 0, sizeof(jtag_cables));
62
 
63
  jtag_cables[i++] = cable_rtl_get_driver();
64
  jtag_cables[i++] = cable_vpi_get_driver();
65
 
66
#ifdef __SUPPORT_PARALLEL_CABLES__
67
  jtag_cables[i++] = cable_xpc3_get_driver();
68
  jtag_cables[i++] = cable_xess_get_driver();
69
#endif
70
 
71
#ifdef  __SUPPORT_USB_CABLES__
72
  jtag_cables[i++] = cable_usbblaster_get_driver();
73
  jtag_cables[i++] = cable_xpcusb_get_driver();
74
 #ifdef __SUPPORT_FTDI_CABLES__
75
  jtag_cables[i++] = cable_ftdi_get_driver();
76
  jtag_cables[i++] = cable_ft245_get_driver();
77
 #endif
78
#endif
79
}
80
 
81 21 nyawn
/* Selects a cable for use */
82
int cable_select(const char *cable)
83
{
84
  int i;
85
 
86 55 nyawn
  for(i = 0; jtag_cables[i] != NULL; i++) {
87
    if(!strcmp(cable, jtag_cables[i]->name)) {
88
      jtag_cable_in_use = jtag_cables[i];
89 21 nyawn
      return APP_ERR_NONE;
90
    }
91
  }
92
 
93
  return APP_ERR_CABLE_INVALID;
94
}
95
 
96
/* Calls the init function of the cable
97
 */
98
int cable_init()
99
{
100
  return jtag_cable_in_use->init_func();
101
}
102
 
103
/* Parses command-line options specific to the selected cable */
104
int cable_parse_opt(int c, char *str)
105
{
106
  return jtag_cable_in_use->opt_func(c, str);
107
}
108
 
109
const char *cable_get_args()
110
{
111
  if(jtag_cable_in_use != NULL)
112
    return jtag_cable_in_use->opts;
113
  else
114
    return NULL;
115
}
116
 
117
/* Prints a (short) useage message for each available cable */
118
void cable_print_help()
119
{
120
  int i;
121
  printf("Available cables: ");
122
 
123 55 nyawn
  for(i = 0; jtag_cables[i]; i++) {
124 21 nyawn
    if(i)
125
      printf(", ");
126 55 nyawn
    printf("%s", jtag_cables[i]->name);
127 21 nyawn
  }
128
 
129
  printf("\n\nOptions availible for the cables:\n");
130 55 nyawn
  for(i = 0; jtag_cables[i]; i++) {
131
    if(!jtag_cables[i]->help)
132 21 nyawn
      continue;
133 55 nyawn
    printf("  %s:\n    %s", jtag_cables[i]->name, jtag_cables[i]->help);
134 21 nyawn
  }
135
}
136
 
137
 
138
/////////////////////////////////////////////////////////////////////////////////
139
// Cable API Functions
140
 
141
int cable_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
142
  return jtag_cable_in_use->stream_out_func(stream, len_bits, set_last_bit);
143
}
144
 
145
int cable_read_write_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
146
  return jtag_cable_in_use->stream_inout_func(outstream, instream, len_bits, set_last_bit);
147
}
148
 
149
int cable_write_bit(uint8_t packet) {
150
  return jtag_cable_in_use->bit_out_func(packet);
151
}
152
 
153
int cable_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
154
  return jtag_cable_in_use->bit_inout_func(packet_out, bit_in);
155
}
156
 
157
int cable_flush(void) {
158
  if(jtag_cable_in_use->flush_func != NULL)
159
    return jtag_cable_in_use->flush_func();
160
  return APP_ERR_NONE;
161
}
162
 
163
 
164
/////////////////////////////////////////////////////////////////////////////////////
165
// Common functions which may or may not be used by individual drivers
166
 
167
 
168
/* Note that these make no assumption as to the starting state of the clock,
169
 * and they leave the clock HIGH.  But, these need to interface with other routines (like
170
 * the byte-shift mode in the USB-Blaster), which begin by assuming that a new
171
 * data bit is available at TDO, which only happens after a FALLING edge of TCK.
172
 * So, routines which assume new data is available will need to start by dropping
173
 * the clock.
174
 */
175
int cable_common_write_bit(uint8_t packet) {
176
  uint8_t data = TRST_BIT;  // TRST is active low, don't clear unless /set/ in 'packet'
177
  int err = APP_ERR_NONE;
178
 
179
  /* Write data, drop clock */
180
  if(packet & TDO) data |= TDI_BIT;
181
  if(packet & TMS) data |= TMS_BIT;
182
  if(packet & TRST) data &= ~TRST_BIT;
183
 
184
  err |= jtag_cable_in_use->out_func(data);
185
 
186
  /* raise clock, to do write */
187
  err |= jtag_cable_in_use->out_func(data | TCLK_BIT);
188
 
189
  return err;
190
}
191
 
192
int cable_common_read_write_bit(uint8_t packet_out, uint8_t *bit_in) {
193
  uint8_t data = TRST_BIT;  //  TRST is active low, don't clear unless /set/ in 'packet'
194
  int err = APP_ERR_NONE;
195
 
196
  /* Write data, drop clock */
197
  if(packet_out & TDO) data |= TDI_BIT;
198
  if(packet_out & TMS) data |= TMS_BIT;
199
  if(packet_out & TRST) data &= ~TRST_BIT;
200
 
201
  err |= jtag_cable_in_use->out_func(data);  // drop the clock to make data available, set the out data
202
  err |= jtag_cable_in_use->inout_func((data | TCLK_BIT), bit_in);  // read in bit, clock high for out bit.
203
 
204
  return err;
205
}
206
 
207
 
208
/* Writes bitstream via bit-bang. Can be used by any driver which does not have a high-speed transfer function.
209
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
210
 */
211
int cable_common_write_stream(uint32_t *stream, int len_bits, int set_last_bit) {
212
  int i;
213
  int index = 0;
214
  int bits_this_index = 0;
215
  uint8_t out;
216
  int err = APP_ERR_NONE;
217
 
218 55 nyawn
  debug("writeStrm%d(", len_bits);
219 21 nyawn
  for(i = 0; i < len_bits - 1; i++) {
220
    out = (stream[index] >> bits_this_index) & 1;
221
    err |= cable_write_bit(out);
222
    debug("%i", out);
223
    bits_this_index++;
224
    if(bits_this_index >= 32) {
225
      index++;
226
      bits_this_index = 0;
227
    }
228
  }
229
 
230
  out = (stream[index] >>(len_bits - 1)) & 0x1;
231
  if(set_last_bit) out |= TMS;
232
  err |= cable_write_bit(out);
233
  debug("%i)\n", out);
234
  return err;
235
}
236
 
237
/* Gets bitstream via bit-bang.  Can be used by any driver which does not have a high-speed transfer function.
238
 * Transfers LSB to MSB of stream[0], then LSB to MSB of stream[1], etc.
239
 */
240
int cable_common_read_stream(uint32_t *outstream, uint32_t *instream, int len_bits, int set_last_bit) {
241
  int i;
242
  int index = 0;
243
  int bits_this_index = 0;
244
  uint8_t inval, outval;
245
  int err = APP_ERR_NONE;
246
 
247
  instream[0] = 0;
248
 
249
  debug("readStrm%d(", len_bits);
250
  for(i = 0; i < (len_bits - 1); i++) {
251
    outval = (outstream[index] >> bits_this_index) & 0x1;
252
    err |= cable_read_write_bit(outval, &inval);
253
    debug("%i", inval);
254
    instream[index] |= (inval << bits_this_index);
255
    bits_this_index++;
256
    if(bits_this_index >= 32) {
257
      index++;
258
      bits_this_index = 0;
259
      instream[index] = 0;  // It's safe to do this, because there's always at least one more bit
260
    }
261
  }
262
 
263
  if (set_last_bit)
264
    outval = ((outstream[index] >> (len_bits - 1)) & 1) | TMS;
265
  else
266
    outval = (outstream[index] >> (len_bits - 1)) & 1;
267
 
268
  err |= cable_read_write_bit(outval, &inval);
269
  debug("%i", inval);
270
  instream[index] |= (inval << bits_this_index);
271
 
272
  debug(") = 0x%lX\n", instream[0]);
273
 
274
  return err;
275
}
276
 
277
 
278
 

powered by: WebSVN 2.1.0

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