Line 76... |
Line 76... |
76 |
|
76 |
|
77 |
// Verify pin usage |
77 |
// Verify pin usage |
78 |
void jtagCheckPinConfig() |
78 |
void jtagCheckPinConfig() |
79 |
{ |
79 |
{ |
80 |
// Check CBUS usage |
80 |
// Check CBUS usage |
81 |
if ( (PORT_TCK) > 0x00FF ) |
81 |
if ( PORT_TCK > 0x00FF ) |
82 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK can't use CBUS signal"), exit(2); |
82 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK can't use CBUS signal"), exit(2); |
83 |
if ( (PORT_TCK) == 0 ) |
83 |
if ( PORT_TCK == 0 ) |
84 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK not defined"), exit(2); |
84 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK not defined"), exit(2); |
85 |
|
85 |
|
86 |
if ( (PORT_TDI) > 0x00FF ) |
86 |
if ( PORT_TDI > 0x00FF ) |
87 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI can't use CBUS signal"), exit(2); |
87 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI can't use CBUS signal"), exit(2); |
88 |
if ( (PORT_TDI) == 0 ) |
88 |
if ( PORT_TDI == 0 ) |
89 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI not defined"), exit(2); |
89 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI not defined"), exit(2); |
90 |
|
90 |
|
91 |
if ( (PORT_TDO) > 0x00FF ) |
91 |
if ( PORT_TDO > 0x00FF ) |
92 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO can't use CBUS signal"), exit(2); |
92 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO can't use CBUS signal"), exit(2); |
93 |
if ( (PORT_TDO) == 0 ) |
93 |
if ( PORT_TDO == 0 ) |
94 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO not defined"), exit(2); |
94 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO not defined"), exit(2); |
95 |
|
95 |
|
96 |
if ( (PORT_TMS) > 0x00FF) |
96 |
if ( PORT_TMS > 0x00FF) |
97 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS can't use CBUS signal"), exit(2); |
97 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS can't use CBUS signal"), exit(2); |
98 |
if ( (PORT_TMS) == 0 ) |
98 |
if ( PORT_TMS == 0 ) |
99 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS not defined"), exit(2); |
99 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS not defined"), exit(2); |
100 |
|
100 |
|
101 |
if ( (PORT_LED) > 0x0FFF) |
101 |
if ( PORT_LED > 0x0FFF) |
102 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: LED can't use CBUS signal > 3"), exit(2); |
102 |
fprintf(stderr, "\nFTDI: INTERNAL ERROR: LED can't use CBUS signal > 3"), exit(2); |
103 |
} |
103 |
} |
104 |
|
104 |
|
105 |
|
105 |
|
106 |
// Print JTAG Pin Assignment |
106 |
// Print JTAG Pin Assignment |
Line 160... |
Line 160... |
160 |
{ |
160 |
{ |
161 |
ftStatus = FT_GetDeviceInfoDetail(i, &Flags, &Type, &ID, &LocId, SerialNumber, Description, &ftHandleTemp); |
161 |
ftStatus = FT_GetDeviceInfoDetail(i, &Flags, &Type, &ID, &LocId, SerialNumber, Description, &ftHandleTemp); |
162 |
if (ftStatus == FT_OK) |
162 |
if (ftStatus == FT_OK) |
163 |
{ |
163 |
{ |
164 |
printf("Device %d\n", i); |
164 |
printf("Device %d\n", i); |
165 |
if (Flags && FT_FLAGS_OPENED) |
165 |
if (Flags & FT_FLAGS_OPENED) |
166 |
{ |
166 |
{ |
167 |
printf(" Description Device is used by another process\n"); |
167 |
printf(" Description Device is used by another process\n"); |
168 |
} |
168 |
} |
169 |
else |
169 |
else |
170 |
{ |
170 |
{ |
Line 242... |
Line 242... |
242 |
{ |
242 |
{ |
243 |
fprintf(stderr, "FTDI: Error Reading Driver Version\n"); |
243 |
fprintf(stderr, "FTDI: Error Reading Driver Version\n"); |
244 |
} |
244 |
} |
245 |
|
245 |
|
246 |
// Set BitBang Mode |
246 |
// Set BitBang Mode |
247 |
ftStatus = FT_SetBitMode(ftHandle, (UCHAR)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
247 |
ftStatus = FT_SetBitMode(ftHandle, (UCHAR)(0xFF & IO_OUTPUT_MASK), FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
248 |
if (ftStatus == FT_OK) |
248 |
if (ftStatus == FT_OK) |
249 |
{ |
249 |
{ |
250 |
// printf("Set BitBang Mode\n"); |
250 |
// printf("Set BitBang Mode\n"); |
251 |
} |
251 |
} |
252 |
else |
252 |
else |
Line 293... |
Line 293... |
293 |
// Enable or Disable Activity LED |
293 |
// Enable or Disable Activity LED |
294 |
void jtagSetLED(bool LedEnable) |
294 |
void jtagSetLED(bool LedEnable) |
295 |
{ |
295 |
{ |
296 |
|
296 |
|
297 |
// DBUS Connected LED (BitBang Mode) |
297 |
// DBUS Connected LED (BitBang Mode) |
298 |
LedMask = LedEnable ? PORT_LED & 0xFF : 0; // Set mask for jtagScan function |
298 |
LedMask = LedEnable ? (PORT_LED & 0xFF) : 0; // Set mask for jtagScan function |
299 |
if (PORT_LED & 0xFF) |
299 |
if (PORT_LED & 0xFF) |
300 |
{ |
300 |
{ |
301 |
// Set / Reset LED Pin |
301 |
// Set / Reset LED Pin |
302 |
DWORD BytesWritten; |
302 |
DWORD BytesWritten; |
303 |
DWORD BytesReceived; |
303 |
DWORD BytesReceived; |
Line 307... |
Line 307... |
307 |
FT_Read (ftHandle, &Dummy, 1, &BytesReceived); // Read 1 byte |
307 |
FT_Read (ftHandle, &Dummy, 1, &BytesReceived); // Read 1 byte |
308 |
//printf("[PinStatus %x DataOut %x]", PinStatus, DataOut); |
308 |
//printf("[PinStatus %x DataOut %x]", PinStatus, DataOut); |
309 |
} |
309 |
} |
310 |
|
310 |
|
311 |
// CBUS Connected LED (BitBang Mode) 1 and 0 state of the port |
311 |
// CBUS Connected LED (BitBang Mode) 1 and 0 state of the port |
312 |
const unsigned char On = ( (((PORT_LED) & 0x0F00) >> 4) | (((PORT_LED) & 0x0F00) >> 8) ); |
312 |
const unsigned char On = ( ((PORT_LED & 0x0F00) >> 4) | ((PORT_LED & 0x0F00) >> 8) ); |
313 |
const unsigned char Off = ( (((PORT_LED) & 0x0F00) >> 4) ); |
313 |
const unsigned char Off = ( ((PORT_LED & 0x0F00) >> 4) ); |
314 |
|
314 |
|
315 |
if (On) |
315 |
if (On) |
316 |
{ |
316 |
{ |
317 |
FT_STATUS ftStatus; |
- |
|
318 |
|
- |
|
319 |
// Set / Reset LED Pin |
317 |
// Set / Reset LED Pin |
320 |
ftStatus = FT_SetBitMode(ftHandle, LedEnable ? On : Off, FT_BITMODE_CBUS_BITBANG); |
318 |
FT_SetBitMode(ftHandle, LedEnable ? On : Off, FT_BITMODE_CBUS_BITBANG); |
321 |
|
319 |
|
322 |
// Return to used Mode |
320 |
// Return to used Mode |
323 |
ftStatus = FT_SetBitMode(ftHandle, (UCHAR)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
321 |
FT_SetBitMode(ftHandle, (UCHAR)(0xFF & IO_OUTPUT_MASK), FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
324 |
} |
322 |
} |
325 |
} |
323 |
} |
326 |
|
324 |
|
327 |
|
325 |
|
328 |
// Set port to Idle state |
326 |
// Set port to Idle state |
Line 355... |
Line 353... |
355 |
return 0; |
353 |
return 0; |
356 |
} |
354 |
} |
357 |
|
355 |
|
358 |
|
356 |
|
359 |
// Send data to JTAG port and bring returned data |
357 |
// Send data to JTAG port and bring returned data |
360 |
int jtagScan(const unsigned char *TMS, const unsigned char *TDI, unsigned char *TDO, int bits) |
358 |
int jtagScan(const unsigned char *TMS, const unsigned char *TDI, unsigned char *TDO, unsigned int bits) |
361 |
{ |
359 |
{ |
362 |
FT_STATUS ftStatus; |
360 |
FT_STATUS ftStatus; |
363 |
DWORD BytesWritten; |
361 |
DWORD BytesWritten; |
364 |
DWORD BytesReceived; |
362 |
DWORD BytesReceived; |
365 |
int r, t; |
363 |
unsigned int r, t; |
366 |
|
364 |
|
367 |
// Decompose TDI and TMS byte array to raw bitstream |
365 |
// Decompose TDI and TMS byte array to raw bitstream |
368 |
//(1 TDI bit + 1 TMS bit --> 1 byte + 1 byte with TCK) |
366 |
//(1 TDI bit + 1 TMS bit --> 1 byte + 1 byte with TCK) |
369 |
unsigned char buffer[16384]; |
367 |
unsigned char buffer[16384]; |
370 |
if (bits > sizeof(buffer)/2) |
368 |
if (bits > sizeof(buffer)/2) |
Line 375... |
Line 373... |
375 |
|
373 |
|
376 |
// Switch LED On |
374 |
// Switch LED On |
377 |
jtagSetLED(true); |
375 |
jtagSetLED(true); |
378 |
|
376 |
|
379 |
// Prepare transmit data to buffer |
377 |
// Prepare transmit data to buffer |
380 |
for (int i = 0; i < bits; ++i) |
378 |
for (unsigned int i = 0; i < bits; ++i) |
381 |
{ |
379 |
{ |
382 |
unsigned char v = 0 | LedMask; // LED On / Off (on DBUS) |
380 |
unsigned char v = 0 | LedMask; // LED On / Off (on DBUS) |
383 |
if (TMS[i/8] & (1<<(i&7))) |
381 |
if (TMS[i/8] & (1<<(i&7))) |
384 |
{ |
382 |
{ |
385 |
v |= (PORT_TMS); |
383 |
v |= PORT_TMS; |
386 |
// printf("T"); |
384 |
// printf("T"); |
387 |
} |
385 |
} |
388 |
else |
386 |
else |
389 |
{ |
387 |
{ |
390 |
// printf("t"); |
388 |
// printf("t"); |
391 |
} |
389 |
} |
392 |
if (TDI[i/8] & (1<<(i&7))) |
390 |
if (TDI[i/8] & (1<<(i&7))) |
393 |
{ |
391 |
{ |
394 |
v |= (PORT_TDI); |
392 |
v |= PORT_TDI; |
395 |
// printf("|"); |
393 |
// printf("|"); |
396 |
} |
394 |
} |
397 |
else |
395 |
else |
398 |
{ |
396 |
{ |
399 |
// printf("."); |
397 |
// printf("."); |
400 |
} |
398 |
} |
401 |
buffer[i * 2 + 0] = v; |
399 |
buffer[i * 2 + 0] = v; |
402 |
buffer[i * 2 + 1] = v | (PORT_TCK); |
400 |
buffer[i * 2 + 1] = v | PORT_TCK; |
403 |
} |
401 |
} |
404 |
PinStatus = buffer[bits*2-1]; |
402 |
PinStatus = buffer[bits*2-1]; |
405 |
// printf("\n"); |
403 |
// printf("\n"); |
406 |
|
404 |
|
407 |
// Send data to FTDI |
405 |
// Send data to FTDI |
Line 420... |
Line 418... |
420 |
{ |
418 |
{ |
421 |
fprintf(stderr, "\n FTDI: Error Writing\n"); |
419 |
fprintf(stderr, "\n FTDI: Error Writing\n"); |
422 |
return -2; |
420 |
return -2; |
423 |
} |
421 |
} |
424 |
|
422 |
|
425 |
int i = 0; |
423 |
unsigned int i = 0; |
426 |
|
424 |
|
427 |
while (i < t) |
425 |
while (i < t) |
428 |
{ |
426 |
{ |
429 |
FT_SetTimeouts(ftHandle, 5000, 0); // timeout 5 sec |
427 |
FT_SetTimeouts(ftHandle, 5000, 0); // timeout 5 sec |
430 |
ftStatus = FT_Read(ftHandle, buffer+r+i, t-i, &BytesReceived); |
428 |
ftStatus = FT_Read(ftHandle, buffer+r+i, t-i, &BytesReceived); |
Line 455... |
Line 453... |
455 |
} |
453 |
} |
456 |
|
454 |
|
457 |
// Pack TDO bitstream from receive buffer to byte array |
455 |
// Pack TDO bitstream from receive buffer to byte array |
458 |
memset(TDO, 0, (bits + 7) / 8); |
456 |
memset(TDO, 0, (bits + 7) / 8); |
459 |
|
457 |
|
460 |
for (int i = 0; i < bits; ++i) |
458 |
for (unsigned int i = 0; i < bits; ++i) |
461 |
{ |
459 |
{ |
462 |
if (buffer[i * 2 + 1] & (PORT_TDO)) |
460 |
if (buffer[i * 2 + 1] & PORT_TDO) |
463 |
{ |
461 |
{ |
464 |
TDO[i/8] |= 1 << (i&7); |
462 |
TDO[i/8] |= 1 << (i&7); |
465 |
// printf("H"); |
463 |
// printf("H"); |
466 |
} |
464 |
} |
467 |
else |
465 |
else |