source: trunk/FirmwareFX2/usbjtag.c@ 109

Last change on this file since 109 was 40, checked in by demin, 15 years ago

turn led off at startup

File size: 16.0 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 PINFLAGSAB = 0xFA; SYNCDELAY; // 1111_1010 => FLAGA = EMPTY flag for EP6; FLAGB = FULL flag for EP8
149
150
151 // LED turned off by default
152 IOD |= (1 << 4); SYNCDELAY;
153
154 // Put the system in high speed by default (REM: USB-Blaster is in full speed)
155 // This can be changed by vendor commands
156 CT1 &= ~0x02;
157}
158
159void OutputByte(BYTE d)
160{
161#ifdef USE_MOD256_OUTBUFFER
162 OutBuffer[FirstFreeInOutBuffer] = d;
163 FirstFreeInOutBuffer = ( FirstFreeInOutBuffer + 1 ) & 0xFF;
164#else
165 OutBuffer[FirstFreeInOutBuffer++] = d;
166 if(FirstFreeInOutBuffer >= OUTBUFFER_LEN) FirstFreeInOutBuffer = 0;
167#endif
168 Pending++;
169}
170
171//-----------------------------------------------------------------------------
172// usb_jtag_activity does most of the work. It now happens to behave just like
173// the combination of FT245BM and Altera-programmed EPM7064 CPLD in Altera's
174// USB-Blaster. The CPLD knows two major modes: Bit banging mode and Byte
175// shift mode. It starts in Bit banging mode. While bytes are received
176// from the host on EP2OUT, each byte B of them is processed as follows:
177//
178// Please note: nCE, nCS, LED pins and DATAOUT actually aren't supported here.
179// Support for these would be required for AS/PS mode and isn't too complicated,
180// but I haven't had the time yet.
181//
182// Bit banging mode:
183//
184// 1. Remember bit 6 (0x40) in B as the "Read bit".
185//
186// 2. If bit 7 (0x40) is set, switch to Byte shift mode for the coming
187// X bytes ( X := B & 0x3F ), and don't do anything else now.
188//
189// 3. Otherwise, set the JTAG signals as follows:
190// TCK/DCLK high if bit 0 was set (0x01), otherwise low
191// TMS/nCONFIG high if bit 1 was set (0x02), otherwise low
192// nCE high if bit 2 was set (0x04), otherwise low
193// nCS high if bit 3 was set (0x08), otherwise low
194// TDI/ASDI/DATA0 high if bit 4 was set (0x10), otherwise low
195// Output Enable/LED active if bit 5 was set (0x20), otherwise low
196//
197// 4. If "Read bit" (0x40) was set, record the state of TDO(CONF_DONE) and
198// DATAOUT(nSTATUS) pins and put it as a byte ((DATAOUT<<1)|TDO) in the
199// output FIFO _to_ the host (the code here reads TDO only and assumes
200// DATAOUT=1)
201//
202// Byte shift mode:
203//
204// 1. Load shift register with byte from host
205//
206// 2. Do 8 times (i.e. for each bit of the byte; implemented in shift.a51)
207// 2a) if nCS=1, set carry bit from TDO, else set carry bit from DATAOUT
208// 2b) Rotate shift register through carry bit
209// 2c) TDI := Carry bit
210// 2d) Raise TCK, then lower TCK.
211//
212// 3. If "Read bit" was set when switching into byte shift mode,
213// record the shift register content and put it into the FIFO
214// _to_ the host.
215//
216// Some more (minor) things to consider to emulate the FT245BM:
217//
218// a) The FT245BM seems to transmit just packets of no more than 64 bytes
219// (which perfectly matches the USB spec). Each packet starts with
220// two non-data bytes (I use 0x31,0x60 here). A USB sniffer on Windows
221// might show a number of packets to you as if it was a large transfer
222// because of the way that Windows understands it: it _is_ a large
223// transfer until terminated with an USB packet smaller than 64 byte.
224//
225// b) The Windows driver expects to get some data packets (with at least
226// the two leading bytes 0x31,0x60) immediately after "resetting" the
227// FT chip and then in regular intervals. Otherwise a blue screen may
228// appear... In the code below, I make sure that every 10ms there is
229// some packet.
230//
231// c) Vendor specific commands to configure the FT245 are mostly ignored
232// in my code. Only those for reading the EEPROM are processed. See
233// DR_GetStatus and DR_VendorCmd below for my implementation.
234//
235// All other TD_ and DR_ functions remain as provided with CY3681.
236//
237//-----------------------------------------------------------------------------
238
239void usb_jtag_activity(void) // Called repeatedly while the device is idle
240{
241 if(!Running) return;
242
243 ProgIO_Poll();
244
245 if(!(EP1INCS & bmEPBUSY))
246 {
247 if(Pending > 0)
248 {
249 BYTE o, n;
250
251 AUTOPTRH2 = MSB( EP1INBUF );
252 AUTOPTRL2 = LSB( EP1INBUF );
253
254 XAUTODAT2 = 0x31;
255 XAUTODAT2 = 0x60;
256
257 if(Pending > 0x3E) { n = 0x3E; Pending -= n; }
258 else { n = Pending; Pending = 0; };
259
260 o = n;
261
262#ifdef USE_MOD256_OUTBUFFER
263 APTR1H = MSB( OutBuffer );
264 APTR1L = FirstDataInOutBuffer;
265 while(n--)
266 {
267 XAUTODAT2 = XAUTODAT1;
268 APTR1H = MSB( OutBuffer ); // Stay within 256-Byte-Buffer
269 };
270 FirstDataInOutBuffer = APTR1L;
271#else
272 APTR1H = MSB( &(OutBuffer[FirstDataInOutBuffer]) );
273 APTR1L = LSB( &(OutBuffer[FirstDataInOutBuffer]) );
274 while(n--)
275 {
276 XAUTODAT2 = XAUTODAT1;
277
278 if(++FirstDataInOutBuffer >= OUTBUFFER_LEN)
279 {
280 FirstDataInOutBuffer = 0;
281 APTR1H = MSB( OutBuffer );
282 APTR1L = LSB( OutBuffer );
283 };
284 };
285#endif
286 SYNCDELAY;
287 EP1INBC = 2 + o;
288 TF2 = 1; // Make sure there will be a short transfer soon
289 }
290 else if(TF2)
291 {
292 EP1INBUF[0] = 0x31;
293 EP1INBUF[1] = 0x60;
294 SYNCDELAY;
295 EP1INBC = 2;
296 TF2 = 0;
297 };
298 };
299
300 if(!(EP2468STAT & bmEP2EMPTY) && (Pending < OUTBUFFER_LEN-0x3F))
301 {
302 WORD i, n = EP2BCL|EP2BCH<<8;
303
304 APTR1H = MSB( EP2FIFOBUF );
305 APTR1L = LSB( EP2FIFOBUF );
306
307 for(i=0;i<n;)
308 {
309 if(ClockBytes > 0)
310 {
311 WORD m;
312
313 m = n-i;
314 if(ClockBytes < m) m = ClockBytes;
315 ClockBytes -= m;
316 i += m;
317
318 /* Shift out 8 bits from d */
319
320 if(WriteOnly) /* Shift out 8 bits from d */
321 {
322 while(m--) ProgIO_ShiftOut(XAUTODAT1);
323 }
324 else /* Shift in 8 bits at the other end */
325 {
326 while(m--) OutputByte(ProgIO_ShiftInOut(XAUTODAT1));
327 }
328 }
329 else
330 {
331 BYTE d = XAUTODAT1;
332 WriteOnly = (d & bmBIT6) ? FALSE : TRUE;
333
334 if(d & bmBIT7)
335 {
336 /* Prepare byte transfer, do nothing else yet */
337
338 ClockBytes = d & 0x3F;
339 }
340 else
341 {
342 if(WriteOnly)
343 ProgIO_Set_State(d);
344 else
345 OutputByte(ProgIO_Set_Get_State(d));
346 };
347 i++;
348 };
349 };
350
351 SYNCDELAY;
352 EP2BCL = 0x80; // Re-arm endpoint 2
353 };
354}
355
356//-----------------------------------------------------------------------------
357// Handler for Vendor Requests (
358//-----------------------------------------------------------------------------
359
360unsigned char app_vendor_cmd(void)
361{
362 // because of fx2/usb_common.c, this code returns nonzero on success
363 // OUT requests. Pretend we handle them all...
364
365 if ((bRequestType & bmRT_DIR_MASK) == bmRT_DIR_OUT)
366 {
367 if(bRequest == RQ_GET_STATUS)
368 {
369 Running = 1;
370 }
371
372 if (bRequest == VEN_SPI_WR) // 0x99
373 {
374 // get EP0 data
375 EP0BCL = 0; // arm EP0 for OUT xfer. This sets the busy bit
376
377 while (EP0CS & bmEPBUSY) // wait for busy to clear
378 ;
379
380 // head_hi, head_l , format , address, *buf , len
381 return !spi_write (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, EP0BCL);
382 }
383
384
385 return 1;
386 }
387
388 // IN requests.
389
390 // change USB speed
391 if (bRequest == 0x91)
392 {
393 if (wIndexL == 0) // high speed
394 {
395 CT1 &= ~0x02;
396 fx2_renumerate(); // renumerate
397 }
398 else // full speed
399 {
400 CT1 |= 0x02;
401 fx2_renumerate(); // renumerate
402 }
403 }
404
405 // change synchronous/asynchronous mode
406 if (bRequest == 0x92)
407 {
408 if(IFCONFIG & bmASYNC)
409 {
410 IFCONFIG &= ~bmASYNC;
411 }
412 else
413 {
414 IFCONFIG |= bmASYNC;
415 }
416 }
417
418 if (bRequest == 0x93) // change to synchronous mode
419 {
420 IFCONFIG &= ~bmASYNC;
421 }
422
423 if (bRequest == VEN_SPI_EN) // 0x96
424 {
425 SPI_OE |= bmSPI_OE; // PA.0,1,3,7 output enable
426 init_spi();
427 EP0BUF[0] = 0;
428 EP0BUF[1] = 0;
429 }
430
431 if (bRequest == VEN_SPI_DIS) // 0x97
432 {
433 SPI_OE &= ~bmSPI_OE; // PA.0,1,3,7 output disable
434 EP0BUF[0] = 0x42;
435 EP0BUF[1] = 0x43;
436 EP0BUF[2] = 0x42;
437 EP0BUF[3] = 0x43;
438 EP0BCH = 0;
439 EP0BCL = wLengthL;
440 return 1;
441 }
442
443 if (bRequest == VEN_SPI_RD) // 0x98
444 {
445 // header_H,header_L, format, address, *buf , len
446 if (spi_read (wValueH, wValueL, wIndexH, wIndexL, EP0BUF, wLengthL))
447 return 0;
448
449 EP0BCH = 0;
450 EP0BCL = wLengthL;
451 return 1;
452 }
453
454 if(bRequest == 0x90)
455 {
456 BYTE addr = (wIndexL<<1) & 0x7F;
457 EP0BUF[0] = eeprom[addr];
458 EP0BUF[1] = eeprom[addr+1];
459 }
460 else
461 {
462 // dummy data
463 EP0BUF[0] = 0x36;
464 EP0BUF[1] = 0x83;
465 }
466
467 EP0BCH = 0;
468 EP0BCL = (wLengthL<2) ? wLengthL : 2; // Arm endpoint with # bytes to transfer
469
470 return 1;
471}
472
473//-----------------------------------------------------------------------------
474
475static void main_loop(void)
476{
477 while(1)
478 {
479 if(usb_setup_packet_avail()) usb_handle_setup_packet();
480 usb_jtag_activity();
481 }
482}
483
484//-----------------------------------------------------------------------------
485
486void main(void)
487{
488 EA = 0; // disable all interrupts
489
490 usb_jtag_init();
491 eeprom_init();
492 setup_autovectors();
493 usb_install_handlers();
494
495
496 EA = 1; // enable interrupts
497
498 fx2_renumerate(); // simulates disconnect / reconnect
499
500 main_loop();
501}
502
503
504
505
Note: See TracBrowser for help on using the repository browser.