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