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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.61/] [tools/] [fx2/] [src/] [main.c] - Blame information for rev 26

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 wfjm
/* $Id: main.c 472 2013-01-06 14:39:10Z mueller $ */
2 17 wfjm
/*
3 18 wfjm
 * Copyright 2011-2013 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4 17 wfjm
 * Code was forked from ixo-jtag.svn.sourceforge.net on 2011-07-17
5
 *
6
 * - original copyright and licence disclaimer --------------------------------
7
 * - Code that turns a Cypress FX2 USB Controller into an USB JTAG adapter
8
 * - Copyright (C) 2005..2007 Kolja Waschk, ixo.de
9
 * - This code is part of usbjtag. usbjtag is free software;
10
 *-----------------------------------------------------------------------------
11
 *
12
 * This program is free software; you may redistribute and/or modify it under
13
 * the terms of the GNU General Public License as published by the Free
14
 * Software Foundation, either version 2, or at your option any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful, but
17
 * WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
18
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19
 * for complete details.
20
 *
21
 *-----------------------------------------------------------------------------
22
 *
23
 * EZ-USB FX2 controller main program
24
 *
25
 * Revision History:
26
 *
27
 * Date         Rev Version  Comment
28 18 wfjm
 * 2013-01-05   472   1.1.1  BUGFIX: explicitly set FIFOPINPOLAR=0
29 17 wfjm
 * 2011-07-23   397   1.1    factor out usb_fifo_init() code
30
 * 2011-07-17   394   1.0    Initial version (from ixo-jtag/usb_jtag Rev 204)
31
 *
32
 *-----------------------------------------------------------------------------
33
 */
34
 
35
#include "isr.h"
36
#include "timer.h"
37
#include "delay.h"
38
#include "fx2regs.h"
39
#include "fx2utils.h"
40
#include "usb_common.h"
41
#include "usb_descriptors.h"
42
#include "usb_requests.h"
43
 
44
#include "syncdelay.h"
45
 
46
#include "eeprom.h"
47
#include "hardware.h"
48
 
49
//-----------------------------------------------------------------------------
50
// Define USE_MOD256_OUTBUFFER:
51
// Saves about 256 bytes in code size, improves speed a little.
52
// A further optimization could be not to use an extra output buffer at 
53
// all, but to write directly into EP1INBUF. Not implemented yet. When 
54
// downloading large amounts of data _to_ the target, there is no output
55
// and thus the output buffer isn't used at all and doesn't slow down things.
56
 
57
#define USE_MOD256_OUTBUFFER 1
58
 
59
//-----------------------------------------------------------------------------
60
// Global data
61
 
62
typedef bit BOOL;
63
#define FALSE 0
64
#define TRUE  1
65
static BOOL Running;
66
static BOOL WriteOnly;
67
 
68
static BYTE ClockBytes;
69
static WORD Pending;
70
 
71
#ifdef USE_MOD256_OUTBUFFER
72
  static BYTE FirstDataInOutBuffer;
73
  static BYTE FirstFreeInOutBuffer;
74
#else
75
  static WORD FirstDataInOutBuffer;
76
  static WORD FirstFreeInOutBuffer;
77
#endif
78
 
79
#ifdef USE_MOD256_OUTBUFFER
80
  /* Size of output buffer must be exactly 256 */
81
  #define OUTBUFFER_LEN 0x100
82
  /* Output buffer must begin at some address with lower 8 bits all zero */
83
  xdata at 0xE000 BYTE OutBuffer[OUTBUFFER_LEN];
84
#else
85
  #define OUTBUFFER_LEN 0x200
86
  static xdata BYTE OutBuffer[OUTBUFFER_LEN];
87
#endif
88
 
89
//-----------------------------------------------------------------------------
90
 
91
void usb_jtag_init(void)              // Called once at startup
92
{
93
   WORD tmp;
94
 
95
   Running = FALSE;
96
   ClockBytes = 0;
97
   Pending = 0;
98
   WriteOnly = TRUE;
99
   FirstDataInOutBuffer = 0;
100
   FirstFreeInOutBuffer = 0;
101
 
102
   ProgIO_Init();
103
 
104
   ProgIO_Enable();
105
 
106
   // Make Timer2 reload at 100 Hz to trigger Keepalive packets
107
 
108
   tmp = 65536 - ( 48000000 / 12 / 100 );
109
   RCAP2H = tmp >> 8;
110
   RCAP2L = tmp & 0xFF;
111
   CKCON = 0; // Default Clock
112
   T2CON = 0x04; // Auto-reload mode using internal clock, no baud clock.
113
 
114
   // Enable Autopointer
115
 
116
   EXTACC = 1;  // Enable
117
   APTR1FZ = 1; // Don't freeze
118
   APTR2FZ = 1; // Don't freeze
119
}
120
 
121
void OutputByte(BYTE d)
122
{
123
#ifdef USE_MOD256_OUTBUFFER
124
   OutBuffer[FirstFreeInOutBuffer] = d;
125
   FirstFreeInOutBuffer = ( FirstFreeInOutBuffer + 1 ) & 0xFF;
126
#else
127
   OutBuffer[FirstFreeInOutBuffer++] = d;
128
   if(FirstFreeInOutBuffer >= OUTBUFFER_LEN) FirstFreeInOutBuffer = 0;
129
#endif
130
   Pending++;
131
}
132
 
133
//-----------------------------------------------------------------------------
134
// usb_jtag_activity does most of the work. It now happens to behave just like
135
// the combination of FT245BM and Altera-programmed EPM7064 CPLD in Altera's
136
// USB-Blaster. The CPLD knows two major modes: Bit banging mode and Byte
137
// shift mode. It starts in Bit banging mode. While bytes are received
138
// from the host on EP2OUT, each byte B of them is processed as follows:
139
//
140
// Please note: nCE, nCS, LED pins and DATAOUT actually aren't supported here.
141
// Support for these would be required for AS/PS mode and isn't too complicated,
142
// but I haven't had the time yet.
143
//
144
// Bit banging mode:
145
// 
146
//   1. Remember bit 6 (0x40) in B as the "Read bit".
147
//
148
//   2. If bit 7 (0x40) is set, switch to Byte shift mode for the coming
149
//      X bytes ( X := B & 0x3F ), and don't do anything else now.
150
//
151
//    3. Otherwise, set the JTAG signals as follows:
152
//        TCK/DCLK high if bit 0 was set (0x01), otherwise low
153
//        TMS/nCONFIG high if bit 1 was set (0x02), otherwise low
154
//        nCE high if bit 2 was set (0x04), otherwise low
155
//        nCS high if bit 3 was set (0x08), otherwise low
156
//        TDI/ASDI/DATA0 high if bit 4 was set (0x10), otherwise low
157
//        Output Enable/LED active if bit 5 was set (0x20), otherwise low
158
//
159
//    4. If "Read bit" (0x40) was set, record the state of TDO(CONF_DONE) and
160
//        DATAOUT(nSTATUS) pins and put it as a byte ((DATAOUT<<1)|TDO) in the
161
//        output FIFO _to_ the host (the code here reads TDO only and assumes
162
//        DATAOUT=1)
163
//
164
// Byte shift mode:
165
//
166
//   1. Load shift register with byte from host
167
//
168
//   2. Do 8 times (i.e. for each bit of the byte; implemented in shift.a51)
169
//      2a) if nCS=1, set carry bit from TDO, else set carry bit from DATAOUT
170
//      2b) Rotate shift register through carry bit
171
//      2c) TDI := Carry bit
172
//      2d) Raise TCK, then lower TCK.
173
//
174
//   3. If "Read bit" was set when switching into byte shift mode,
175
//      record the shift register content and put it into the FIFO
176
//      _to_ the host.
177
//
178
// Some more (minor) things to consider to emulate the FT245BM:
179
//
180
//   a) The FT245BM seems to transmit just packets of no more than 64 bytes
181
//      (which perfectly matches the USB spec). Each packet starts with
182
//      two non-data bytes (I use 0x31,0x60 here). A USB sniffer on Windows
183
//      might show a number of packets to you as if it was a large transfer
184
//      because of the way that Windows understands it: it _is_ a large
185
//      transfer until terminated with an USB packet smaller than 64 byte.
186
//
187
//   b) The Windows driver expects to get some data packets (with at least
188
//      the two leading bytes 0x31,0x60) immediately after "resetting" the
189
//      FT chip and then in regular intervals. Otherwise a blue screen may
190
//      appear... In the code below, I make sure that every 10ms there is
191
//      some packet.
192
//
193
//   c) Vendor specific commands to configure the FT245 are mostly ignored
194
//      in my code. Only those for reading the EEPROM are processed. See
195
//      DR_GetStatus and DR_VendorCmd below for my implementation.
196
//
197
//   All other TD_ and DR_ functions remain as provided with CY3681.
198
//
199
//-----------------------------------------------------------------------------
200
 
201
void usb_jtag_activity(void) // Called repeatedly while the device is idle
202
{
203
   if(!Running) return;
204
 
205
   ProgIO_Poll();
206
 
207
   if(!(EP1INCS & bmEPBUSY)) {
208
      if(Pending > 0) {
209
         BYTE o, n;
210
 
211
         AUTOPTRH2 = MSB( EP1INBUF );
212
         AUTOPTRL2 = LSB( EP1INBUF );
213
 
214
         XAUTODAT2 = 0x31;
215
         XAUTODAT2 = 0x60;
216
 
217
         if(Pending > 0x3E) { n = 0x3E; Pending -= n; }
218
                     else { n = Pending; Pending = 0; };
219
 
220
         o = n;
221
 
222
#ifdef USE_MOD256_OUTBUFFER
223
         APTR1H = MSB( OutBuffer );
224
         APTR1L = FirstDataInOutBuffer;
225
         while(n--) {
226
            XAUTODAT2 = XAUTODAT1;
227
            APTR1H = MSB( OutBuffer ); // Stay within 256-Byte-Buffer
228
         }
229
         FirstDataInOutBuffer = APTR1L;
230
#else
231
         APTR1H = MSB( &(OutBuffer[FirstDataInOutBuffer]) );
232
         APTR1L = LSB( &(OutBuffer[FirstDataInOutBuffer]) );
233
         while(n--) {
234
            XAUTODAT2 = XAUTODAT1;
235
 
236
            if(++FirstDataInOutBuffer >= OUTBUFFER_LEN) {
237
               FirstDataInOutBuffer = 0;
238
               APTR1H = MSB( OutBuffer );
239
               APTR1L = LSB( OutBuffer );
240
            }
241
         }
242
#endif
243
         SYNCDELAY;
244
         EP1INBC = 2 + o;
245
         TF2 = 1; // Make sure there will be a short transfer soon
246
      } else if(TF2) {
247
         EP1INBUF[0] = 0x31;
248
         EP1INBUF[1] = 0x60;
249
         SYNCDELAY;
250
         EP1INBC = 2;
251
         TF2 = 0;
252
      }
253
   }
254
 
255
   if(!(EP2468STAT & bmEP2EMPTY) && (Pending < OUTBUFFER_LEN-0x3F)) {
256
   //BYTE i, n = EP2BCL; // bugfix by Sune Mai (Oct 2008, 
257
   // https://sourceforge.net/projects/urjtag/forums/forum/682993/topic/2312452)
258
      WORD i, n = EP2BCL|EP2BCH<<8;
259
 
260
      APTR1H = MSB( EP2FIFOBUF );
261
      APTR1L = LSB( EP2FIFOBUF );
262
 
263
      for(i=0;i<n;) {
264
         if(ClockBytes > 0) {
265
            //BYTE m; // bugfix by Sune Mai, see above
266
            WORD m;
267
 
268
            m = n-i;
269
            if(ClockBytes < m) m = ClockBytes;
270
            ClockBytes -= m;
271
            i += m;
272
 
273
            /* Shift out 8 bits from d */
274
 
275
            if(WriteOnly) {      /* Shift out 8 bits from d */
276
               while(m--) ProgIO_ShiftOut(XAUTODAT1);
277
            } else {             /* Shift in 8 bits at the other end  */
278
               while(m--) OutputByte(ProgIO_ShiftInOut(XAUTODAT1));
279
            }
280
        } else {
281
            BYTE d = XAUTODAT1;
282
            WriteOnly = (d & bmBIT6) ? FALSE : TRUE;
283
 
284
            if(d & bmBIT7) {
285
               /* Prepare byte transfer, do nothing else yet */
286
 
287
               ClockBytes = d & 0x3F;
288
            } else {
289
               if(WriteOnly)
290
                   ProgIO_Set_State(d);
291
               else
292
                   OutputByte(ProgIO_Set_Get_State(d));
293
            }
294
            i++;
295
         }
296
      }
297
 
298
      SYNCDELAY;
299
      EP2BCL = 0x80; // Re-arm endpoint 2
300
   };
301
}
302
 
303
//-----------------------------------------------------------------------------
304
// Handler for Vendor Requests (
305
//-----------------------------------------------------------------------------
306
 
307
unsigned char app_vendor_cmd(void)
308
{
309
  // OUT requests. Pretend we handle them all...
310
 
311
  if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_OUT) {
312
    if(bRequest == RQ_GET_STATUS) {
313
      Running = 1;
314
    }
315
    return 1;
316
  }
317
 
318
  // IN requests.
319
 
320
  if(bRequest == 0x90) {
321
    BYTE addr = (wIndexL<<1) & 0x7F;
322
    EP0BUF[0] = eeprom[addr];
323
    EP0BUF[1] = eeprom[addr+1];
324
  } else {
325
    // dummy data
326
    EP0BUF[0] = 0x36;
327
    EP0BUF[1] = 0x83;
328
  }
329
 
330
  EP0BCH = 0;
331
  EP0BCL = (wLengthL<2) ? wLengthL : 2; // Arm endpoint with # bytes to transfer
332
 
333
  return 1;
334
}
335
 
336
//-----------------------------------------------------------------------------
337
 
338
static void main_loop(void)
339
{
340
  while(1) {
341
    if(usb_setup_packet_avail()) usb_handle_setup_packet();
342
    usb_jtag_activity();
343
  }
344
}
345
 
346
//-----------------------------------------------------------------------------
347
 
348
extern void usb_fifo_init(void);
349
 
350
void main(void)
351
{
352
  EA = 0; // disable all interrupts
353 18 wfjm
 
354
  // Digilent nexys3 and atlys boards change FIFOPINPOLAR such that
355
  // EE and FF are active high. In nexys2 boards they are active low
356
  // All config regs should be set (even when power on defaults are
357
  // use, but this one especially....
358
  FIFOPINPOLAR = 0;
359 17 wfjm
 
360
  usb_jtag_init();
361
  usb_fifo_init();
362
  eeprom_init();
363
  setup_autovectors ();
364
  usb_install_handlers ();
365
 
366
 
367
  EA = 1; // enable interrupts
368
 
369
  fx2_renumerate(); // simulates disconnect / reconnect
370
 
371
  main_loop();
372
}
373
 
374
 
375
 
376
 

powered by: WebSVN 2.1.0

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