source: trunk/FirmwareFX2/usbjtag.c@ 23

Last change on this file since 23 was 21, checked in by demin, 15 years ago

cleanup vendor commands

File size: 15.9 KB
Line 
1/*-----------------------------------------------------------------------------
2 * Code that turns a Cypress FX2 USB Controller into an USB JTAG adapter
3 *-----------------------------------------------------------------------------
4 * Copyright (C) 2005..2007 Kolja Waschk, ixo.de
5 *-----------------------------------------------------------------------------
6 * Check hardware.h/.c if it matches your hardware configuration (e.g. pinout).
7 * Changes regarding USB identification should be made in product.inc!
8 *-----------------------------------------------------------------------------
9 * This code is part of usbjtag. usbjtag is free software; you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version. usbjtag is distributed in the hope
13 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details. You should have received a
16 * copy of the GNU General Public License along with this program in the file
17 * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin
18 * St, Fifth Floor, Boston, MA 02110-1301 USA
19 *-----------------------------------------------------------------------------
20 */
21
22#include "isr.h"
23#include "timer.h"
24#include "delay.h"
25#include "fx2regs.h"
26#include "fx2utils.h"
27#include "usb_common.h"
28#include "usb_descriptors.h"
29#include "usb_requests.h"
30
31#include "syncdelay.h"
32
33#include "eeprom.h"
34#include "hardware.h"
35
36#include "spi.h"
37
38//-----------------------------------------------------------------------------
39// Define USE_MOD256_OUTBUFFER:
40// Saves about 256 bytes in code size, improves speed a little.
41// A further optimization could be not to use an extra output buffer at
42// all, but to write directly into EP1INBUF. Not implemented yet. When
43// downloading large amounts of data _to_ the target, there is no output
44// and thus the output buffer isn't used at all and doesn't slow down things.
45
46#define USE_MOD256_OUTBUFFER 1
47
48//-----------------------------------------------------------------------------
49// Global data
50
51typedef bit BOOL;
52#define FALSE 0
53#define TRUE 1
54static BOOL Running;
55static BOOL WriteOnly;
56
57static BYTE ClockBytes;
58static WORD Pending;
59
60#ifdef USE_MOD256_OUTBUFFER
61 static BYTE FirstDataInOutBuffer;
62 static BYTE FirstFreeInOutBuffer;
63#else
64 static WORD FirstDataInOutBuffer;
65 static WORD FirstFreeInOutBuffer;
66#endif
67
68#ifdef USE_MOD256_OUTBUFFER
69 /* Size of output buffer must be exactly 256 */
70 #define OUTBUFFER_LEN 0x100
71 /* Output buffer must begin at some address with lower 8 bits all zero */
72 xdata at 0xE000 BYTE OutBuffer[OUTBUFFER_LEN];
73#else
74 #define OUTBUFFER_LEN 0x200
75 static xdata BYTE OutBuffer[OUTBUFFER_LEN];
76#endif
77
78//-----------------------------------------------------------------------------
79
80void usb_jtag_init(void) // Called once at startup
81{
82 WORD tmp;
83
84 Running = FALSE;
85 ClockBytes = 0;
86 Pending = 0;
87 WriteOnly = TRUE;
88 FirstDataInOutBuffer = 0;
89 FirstFreeInOutBuffer = 0;
90
91 ProgIO_Init();
92
93 ProgIO_Enable();
94
95 // Make Timer2 reload at 100 Hz to trigger Keepalive packets
96
97 tmp = 65536 - ( 48000000 / 12 / 100 );
98 RCAP2H = tmp >> 8;
99 RCAP2L = tmp & 0xFF;
100 CKCON = 0; // Default Clock
101 T2CON = 0x04; // Auto-reload mode using internal clock, no baud clock.
102
103 // Enable Autopointer
104
105 EXTACC = 1; // Enable
106 APTR1FZ = 1; // Don't freeze
107 APTR2FZ = 1; // Don't freeze
108
109 // define endpoint configuration
110
111 REVCTL = 3; SYNCDELAY; // Allow FW access to FIFO buffer
112 FIFORESET = 0x80; SYNCDELAY; // From now on, NAK all, reset all FIFOS
113 FIFORESET = 0x02; SYNCDELAY; // Reset FIFO 2
114 FIFORESET = 0x04; SYNCDELAY; // Reset FIFO 4
115 FIFORESET = 0x06; SYNCDELAY; // Reset FIFO 6
116 FIFORESET = 0x08; SYNCDELAY; // Reset FIFO 8
117 FIFORESET = 0x00; SYNCDELAY; // Restore normal behaviour
118
119 EP1OUTCFG = 0xA0; SYNCDELAY; // Endpoint 1 Type Bulk
120 EP1INCFG = 0xA0; SYNCDELAY; // Endpoint 1 Type Bulk
121
122 EP2FIFOCFG = 0x00; SYNCDELAY; // Endpoint 2
123 EP2CFG = 0xA2; SYNCDELAY; // Endpoint 2 Valid, Out, Type Bulk, Double buffered
124
125 EP4FIFOCFG = 0x00; SYNCDELAY; // Endpoint 4 not used
126 EP4CFG = 0xA0; SYNCDELAY; // Endpoint 4 not used
127
128 REVCTL = 0; SYNCDELAY; // Reset FW access to FIFO buffer, enable auto-arming when AUTOOUT is switched to 1
129
130 EP6CFG = 0xA2; SYNCDELAY; // Out endpoint, Bulk, Double buffering
131 EP6FIFOCFG = 0x00; SYNCDELAY; // Firmware has to see a rising edge on auto bit to enable auto arming
132 EP6FIFOCFG = bmAUTOOUT; SYNCDELAY; // Endpoint 6 used for user communicationn, auto commitment, 8 bits data bus
133
134 EP8CFG = 0xE0; SYNCDELAY; // In endpoint, Bulk
135 EP8FIFOCFG = 0x00; SYNCDELAY; // Firmware has to see a rising edge on auto bit to enable auto arming
136 EP8FIFOCFG = bmAUTOIN; SYNCDELAY; // Endpoint 8 used for user communication, auto commitment, 8 bits data bus
137
138 EP8AUTOINLENH = 0x00; SYNCDELAY; // Size in bytes of the IN data automatically commited (64 bytes here, but changed dynamically depending on the connection)
139 EP8AUTOINLENL = 0x40; SYNCDELAY; // Can use signal PKTEND if you want to commit a shorter packet
140
141 // Out endpoints do not come up armed
142 // Since the defaults are double buffered we must write dummy byte counts twice
143 EP2BCL = 0x80; SYNCDELAY; // Arm EP2OUT by writing byte count w/skip.=
144 EP4BCL = 0x80; SYNCDELAY;
145 EP2BCL = 0x80; SYNCDELAY; // Arm EP4OUT by writing byte count w/skip.=
146 EP4BCL = 0x80; SYNCDELAY;
147
148 // Put the system in high speed by default (REM: USB-Blaster is in full speed)
149 // This can be changed by vendor commands
150 CT1 &= ~0x02;
151}
152
153void OutputByte(BYTE d)
154{
155#ifdef USE_MOD256_OUTBUFFER
156 OutBuffer[FirstFreeInOutBuffer] = d;
157 FirstFreeInOutBuffer = ( FirstFreeInOutBuffer + 1 ) & 0xFF;
158#else
159 OutBuffer[FirstFreeInOutBuffer++] = d;
160 if(FirstFreeInOutBuffer >= OUTBUFFER_LEN) FirstFreeInOutBuffer = 0;
161#endif
162 Pending++;
163}
164
165//-----------------------------------------------------------------------------
166// usb_jtag_activity does most of the work. It now happens to behave just like
167// the combination of FT245BM and Altera-programmed EPM7064 CPLD in Altera's
168// USB-Blaster. The CPLD knows two major modes: Bit banging mode and Byte
169// shift mode. It starts in Bit banging mode. While bytes are received
170// from the host on EP2OUT, each byte B of them is processed as follows:
171//
172// Please note: nCE, nCS, LED pins and DATAOUT actually aren't supported here.
173// Support for these would be required for AS/PS mode and isn't too complicated,
174// but I haven't had the time yet.
175//
176// Bit banging mode:
177//
178// 1. Remember bit 6 (0x40) in B as the "Read bit".
179//
180// 2. If bit 7 (0x40) is set, switch to Byte shift mode for the coming
181// X bytes ( X := B & 0x3F ), and don't do anything else now.
182//
183// 3. Otherwise, set the JTAG signals as follows:
184// TCK/DCLK high if bit 0 was set (0x01), otherwise low
185// TMS/nCONFIG high if bit 1 was set (0x02), otherwise low
186// nCE high if bit 2 was set (0x04), otherwise low
187// nCS high if bit 3 was set (0x08), otherwise low
188// TDI/ASDI/DATA0 high if bit 4 was set (0x10), otherwise low
189// Output Enable/LED active if bit 5 was set (0x20), otherwise low
190//
191// 4. If "Read bit" (0x40) was set, record the state of TDO(CONF_DONE) and
192// DATAOUT(nSTATUS) pins and put it as a byte ((DATAOUT<<1)|TDO) in the
193// output FIFO _to_ the host (the code here reads TDO only and assumes
194// DATAOUT=1)
195//
196// Byte shift mode:
197//
198// 1. Load shift register with byte from host
199//
200// 2. Do 8 times (i.e. for each bit of the byte; implemented in shift.a51)
201// 2a) if nCS=1, set carry bit from TDO, else set carry bit from DATAOUT
202// 2b) Rotate shift register through carry bit
203// 2c) TDI := Carry bit
204// 2d) Raise TCK, then lower TCK.
205//
206// 3. If "Read bit" was set when switching into byte shift mode,
207// record the shift register content and put it into the FIFO
208// _to_ the host.
209//
210// Some more (minor) things to consider to emulate the FT245BM:
211//
212// a) The FT245BM seems to transmit just packets of no more than 64 bytes
213// (which perfectly matches the USB spec). Each packet starts with
214// two non-data bytes (I use 0x31,0x60 here). A USB sniffer on Windows
215// might show a number of packets to you as if it was a large transfer
216// because of the way that Windows understands it: it _is_ a large
217// transfer until terminated with an USB packet smaller than 64 byte.
218//
219// b) The Windows driver expects to get some data packets (with at least
220// the two leading bytes 0x31,0x60) immediately after "resetting" the
221// FT chip and then in regular intervals. Otherwise a blue screen may
222// appear... In the code below, I make sure that every 10ms there is
223// some packet.
224//
225// c) Vendor specific commands to configure the FT245 are mostly ignored
226// in my code. Only those for reading the EEPROM are processed. See
227// DR_GetStatus and DR_VendorCmd below for my implementation.
228//
229// All other TD_ and DR_ functions remain as provided with CY3681.
230//
231//-----------------------------------------------------------------------------
232
233void usb_jtag_activity(void) // Called repeatedly while the device is idle
234{
235 if(!Running) return;
236
237 ProgIO_Poll();
238
239 if(!(EP1INCS & bmEPBUSY))
240 {
241 if(Pending > 0)
242 {
243 BYTE o, n;
244
245 AUTOPTRH2 = MSB( EP1INBUF );
246 AUTOPTRL2 = LSB( EP1INBUF );
247
248 XAUTODAT2 = 0x31;
249 XAUTODAT2 = 0x60;
250
251 if(Pending > 0x3E) { n = 0x3E; Pending -= n; }
252 else { n = Pending; Pending = 0; };
253
254 o = n;
255
256#ifdef USE_MOD256_OUTBUFFER
257 APTR1H = MSB( OutBuffer );
258 APTR1L = FirstDataInOutBuffer;
259 while(n--)
260 {
261 XAUTODAT2 = XAUTODAT1;
262 APTR1H = MSB( OutBuffer ); // Stay within 256-Byte-Buffer
263 };
264 FirstDataInOutBuffer = APTR1L;
265#else
266 APTR1H = MSB( &(OutBuffer[FirstDataInOutBuffer]) );
267 APTR1L = LSB( &(OutBuffer[FirstDataInOutBuffer]) );
268 while(n--)
269 {
270 XAUTODAT2 = XAUTODAT1;
271
272 if(++FirstDataInOutBuffer >= OUTBUFFER_LEN)
273 {
274 FirstDataInOutBuffer = 0;
275 APTR1H = MSB( OutBuffer );
276 APTR1L = LSB( OutBuffer );
277 };
278 };
279#endif
280 SYNCDELAY;
281 EP1INBC = 2 + o;
282 TF2 = 1; // Make sure there will be a short transfer soon
283 }
284 else if(TF2)
285 {
286 EP1INBUF[0] = 0x31;
287 EP1INBUF[1] = 0x60;
288 SYNCDELAY;
289 EP1INBC = 2;
290 TF2 = 0;
291 };
292 };
293
294 if(!(EP2468STAT & bmEP2EMPTY) && (Pending < OUTBUFFER_LEN-0x3F))
295 {
296 WORD i, n = EP2BCL|EP2BCH<<8;
297
298 APTR1H = MSB( EP2FIFOBUF );
299 APTR1L = LSB( EP2FIFOBUF );
300
301 for(i=0;i<n;)
302 {
303 if(ClockBytes > 0)
304 {
305 WORD m;
306
307 m = n-i;
308 if(ClockBytes < m) m = ClockBytes;
309 ClockBytes -= m;
310 i += m;
311
312 /* Shift out 8 bits from d */
313
314 if(WriteOnly) /* Shift out 8 bits from d */
315 {
316 while(m--) ProgIO_ShiftOut(XAUTODAT1);
317 }
318 else /* Shift in 8 bits at the other end */
319 {
320 while(m--) OutputByte(ProgIO_ShiftInOut(XAUTODAT1));
321 }
322 }
323 else
324 {
325 BYTE d = XAUTODAT1;
326 WriteOnly = (d & bmBIT6) ? FALSE : TRUE;
327
328 if(d & bmBIT7)
329 {
330 /* Prepare byte transfer, do nothing else yet */
331
332 ClockBytes = d & 0x3F;
333 }
334 else
335 {
336 if(WriteOnly)
337 ProgIO_Set_State(d);
338 else
339 OutputByte(ProgIO_Set_Get_State(d));
340 };
341 i++;
342 };
343 };
344
345 SYNCDELAY;
346 EP2BCL = 0x80; // Re-arm endpoint 2
347 };
348}
349
350//-----------------------------------------------------------------------------
351// Handler for Vendor Requests (
352//-----------------------------------------------------------------------------
353
354unsigned char app_vendor_cmd(void)
355{
356 // because of fx2/usb_common.c, this code returns nonzero on success
357 // OUT requests. Pretend we handle them all...
358
359 if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_OUT)
360 {
361 if(bRequest == RQ_GET_STATUS)
362 {
363 Running = 1;
364 }
365
366 if (bRequest == VEN_SPI_WR) // 0x99
367 {
368 // get EP0 data
369 EP0BCL = 0; // arm EP0 for OUT xfer. This sets the busy bit
370
371 while (EP0CS & bmEPBUSY) // wait for busy to clear
372 ;
373
374 // head_hi, head_l , format , address, *buf , len
375 return !spi_write (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, EP0BCL);
376 }
377
378
379 return 1;
380 }
381
382 // IN requests.
383
384 // change USB speed
385 if (bRequest == 0x91)
386 {
387 if (wIndexL == 0) // high speed
388 {
389 CT1 &= ~0x02;
390 fx2_renumerate(); // renumerate
391 }
392 else // full speed
393 {
394 CT1 |= 0x02;
395 fx2_renumerate(); // renumerate
396 }
397 }
398
399 // change synchronous/asynchronous mode
400 if (bRequest == 0x92)
401 {
402 if(IFCONFIG & bmASYNC)
403 {
404 IFCONFIG &= ~bmASYNC;
405 }
406 else
407 {
408 IFCONFIG |= bmASYNC;
409 }
410 }
411
412 if (bRequest == 0x93) // change to synchronous mode
413 {
414 IFCONFIG &= ~bmASYNC;
415 }
416
417 if (bRequest == VEN_SPI_EN) // 0x96
418 {
419 SPI_OE |= bmSPI_OE; // PA.0,1,3,7 output enable
420 init_spi();
421 EP0BUF[0] = 0;
422 EP0BUF[1] = 0;
423 }
424
425 if (bRequest == VEN_SPI_DIS) // 0x97
426 {
427 SPI_OE &= ~bmSPI_OE; // PA.0,1,3,7 output disable
428 EP0BUF[0] = 0x42;
429 EP0BUF[1] = 0x43;
430 EP0BUF[2] = 0x42;
431 EP0BUF[3] = 0x43;
432 EP0BCH = 0;
433 EP0BCL = wLengthL;
434 return 1;
435 }
436
437 if (bRequest == VEN_SPI_RD) // 0x98
438 {
439 // header_H,header_L, format, address, *buf , len
440 if (spi_read (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, wLengthL))
441 return 0;
442
443 EP0BCH = 0;
444 EP0BCL = wLengthL;
445 return 1;
446 }
447
448 if(bRequest == 0x90)
449 {
450 BYTE addr = (wIndexL<<1) & 0x7F;
451 EP0BUF[0] = eeprom[addr];
452 EP0BUF[1] = eeprom[addr+1];
453 }
454 else
455 {
456 // dummy data
457 EP0BUF[0] = 0x36;
458 EP0BUF[1] = 0x83;
459 }
460
461 EP0BCH = 0;
462 EP0BCL = (wLengthL<2) ? wLengthL : 2; // Arm endpoint with # bytes to transfer
463
464 return 1;
465}
466
467//-----------------------------------------------------------------------------
468
469static void main_loop(void)
470{
471 while(1)
472 {
473 if(usb_setup_packet_avail()) usb_handle_setup_packet();
474 usb_jtag_activity();
475 }
476}
477
478//-----------------------------------------------------------------------------
479
480void main(void)
481{
482 EA = 0; // disable all interrupts
483
484 usb_jtag_init();
485 eeprom_init();
486 setup_autovectors();
487 usb_install_handlers();
488
489
490 EA = 1; // enable interrupts
491
492 fx2_renumerate(); // simulates disconnect / reconnect
493
494 main_loop();
495}
496
497
498
499
Note: See TracBrowser for help on using the repository browser.