Rev Author Line No. Line
2697 miho 1 // Include FTDI library
2 #include "mlab_xvcd_port_FTDI.h"
3  
4  
5 // JTAG Output Pin Mask
6 #define IO_OUTPUT_MASK (PORT_TCK|PORT_TDI|PORT_TMS|PORT_LED) // Mask for all Output Pins
7  
8  
9 // Global Variables
10 FT_HANDLE ftHandle; // Handle for FTDI device
11 bool ftHandleValid = false; // Valid Handle
12 unsigned char PinStatus = 0; // Status of DBUS pins
13 unsigned char LedMask = 0; // LED Mask for DBUS data transfer
14  
15  
16 // Convert string to int (both decimal and hex string)
17 int atoiEx(char *s)
18 {
19 if (s[0]=='0' && (s[1]=='x' || s[1]=='X'))
20 {
21 // Hex Value
22 int i;
23 #pragma warning(disable: 4996) // Disable MS warning about scanf
24 sscanf(s, "%x", &i);
25 return i;
26 }
27 else
28 {
29 // Decimal Value
30 return atoi(s);
31 }
32 }
33  
34  
35 // Print FTDI Pin Names (from mask value)
36 void jtagPrintPinNames(int pinMask)
37 {
38 // 16bit (MSB is CBUS, LSB is DBUS)
39 int bit=15;
40 bool useDelimiter=false;
41  
42 do
43 {
44 int mask = 1 << bit;
45 if (pinMask & mask)
46 {
47 if (useDelimiter)
48 {
49 printf("+");
50 }
51 if (bit > 7)
52 {
53 printf("CBUS%c", '0' + bit - 8);
54 }
55 else
56 {
57 printf("DBUS%c", '0' + bit);
58 switch (mask)
59 {
60 case FTDI_TXD: printf("(TXD)"); break;
61 case FTDI_RXD: printf("(RXD)"); break;
62 case FTDI_RTS: printf("(RTS)"); break;
63 case FTDI_CTS: printf("(CTS)"); break;
64 case FTDI_DTR: printf("(DTR)"); break;
65 case FTDI_DSR: printf("(DSR)"); break;
66 case FTDI_DCD: printf("(DCD)"); break;
67 case FTDI_RI: printf("(RI) "); break;
68 }
69 }
70 useDelimiter = true;
71 }
72 }
73 while (bit-- > 0);
74 }
75  
76  
77 // Verify pin usage
78 void jtagCheckPinConfig()
79 {
80 // Check CBUS usage
81 if ( (PORT_TCK) > 0x00FF )
82 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK can't use CBUS signal"), exit(2);
83 if ( (PORT_TCK) == 0 )
84 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TCK not defined"), exit(2);
85  
86 if ( (PORT_TDI) > 0x00FF )
87 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI can't use CBUS signal"), exit(2);
88 if ( (PORT_TDI) == 0 )
89 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDI not defined"), exit(2);
90  
91 if ( (PORT_TDO) > 0x00FF )
92 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO can't use CBUS signal"), exit(2);
93 if ( (PORT_TDO) == 0 )
94 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TDO not defined"), exit(2);
95  
96 if ( (PORT_TMS) > 0x00FF)
97 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS can't use CBUS signal"), exit(2);
98 if ( (PORT_TMS) == 0 )
99 fprintf(stderr, "\nFTDI: INTERNAL ERROR: TMS not defined"), exit(2);
100  
101 if ( (PORT_LED) > 0x0FFF)
102 fprintf(stderr, "\nFTDI: INTERNAL ERROR: LED can't use CBUS signal > 3"), exit(2);
103 }
104  
105  
106 // Print JTAG Pin Assignment
107 void jtagPrintPinConfig()
108 {
109 // Print pin masks human readable
110 printf(" JTAG Port Pins "); printf("TCK->"); jtagPrintPinNames(PORT_TCK); printf("\n");
111 printf(" "); printf("TDI->"); jtagPrintPinNames(PORT_TDI); printf("\n");
112 printf(" "); printf("TDO->"); jtagPrintPinNames(PORT_TDO); printf("\n");
113 printf(" "); printf("TMS->"); jtagPrintPinNames(PORT_TMS); printf("\n");
114 printf(" "); printf("LED->"); jtagPrintPinNames(PORT_LED); printf("\n");
115 }
116  
117  
118 // Connect to FTDI driver
119 int jtagOpenPort(int findDeviceBy, char *findDeviceByStr)
120 {
121 // Enumerate FTDI Devices
122 // ----------------------
123  
124 FT_STATUS ftStatus;
125  
126 // Print Library Version
127 printf("FTDI Connect\n");
128 DWORD dwLibraryVer;
129 ftStatus = FT_GetLibraryVersion(&dwLibraryVer);
130 if (ftStatus == FT_OK)
131 printf(" Library Version 0x%x\n", dwLibraryVer);
132 else
133 fprintf(stderr, "\nFTDI: Error Reading Library Version\n");
134  
135 // Create Device Information List
136 DWORD numDevs = 0;
137 ftStatus = FT_CreateDeviceInfoList(&numDevs);
138 if (ftStatus == FT_OK)
139 printf(" Devices Found %d\n", numDevs);
140 else
141 printf(" No FTDI Device Found\n");
142  
143 if (numDevs==0)
144 return -1;
145  
146 // Print Config Info
147 jtagPrintPinConfig();
148 jtagCheckPinConfig();
149 printf("\n");
150  
151 // List All FTDI Devices
152 FT_HANDLE ftHandleTemp;
153 DWORD Flags;
154 DWORD ID;
155 DWORD Type;
156 DWORD LocId;
157 char SerialNumber[16];
158 char Description[64];
159 for (DWORD i=0; i<numDevs; i++)
160 {
161 ftStatus = FT_GetDeviceInfoDetail(i, &Flags, &Type, &ID, &LocId, SerialNumber, Description, &ftHandleTemp);
162 if (ftStatus == FT_OK)
163 {
164 printf("Device %d\n", i);
165 printf(" Description \"%s\"\n", Description);
166 printf(" SerialNumber \"%s\"\n", SerialNumber);
167 //printf(" Flags 0x%x\n", Flags);
168 //printf(" Type 0x%x\n", Type);
169 //printf(" ID 0x%x\n", ID);
170 printf(" Location 0x%x\n\n", LocId);
171 }
172 }
173  
174 // Select one Device and Open It
175 unsigned int selectedDeviceIndex = 0;
176 if (findDeviceBy==0)
177 {
178 // Select by Device Number
179 selectedDeviceIndex = atoiEx(findDeviceByStr);
180 if (numDevs<=selectedDeviceIndex)
181 {
182 fprintf(stderr, " There is no Device Number %d\n\n", selectedDeviceIndex);
183 return -1;
184 }
185 // Open device
186 ftStatus = FT_Open(selectedDeviceIndex, &ftHandle);
187 }
188 else
189 {
190 // Select by Description / Serial Number / Location
191 if (findDeviceBy==FT_OPEN_BY_LOCATION)
192 {
193 // Open device (location is number, not string)
194 int findDeviceByInt = atoiEx(findDeviceByStr);
195 ftStatus = FT_OpenEx((void*)findDeviceByInt, findDeviceBy, &ftHandle);
196 }
197 else
198 {
199 ftStatus = FT_OpenEx(findDeviceByStr, findDeviceBy, &ftHandle);
200 }
201 }
202  
203 // Check Status
204 if (ftStatus == FT_OK)
205 {
206 ftHandleValid = true;
207 //printf(" FTDI Device Opened\n");
208 }
209 else
210 {
211 fprintf(stderr, " Can't Open FTDI Device (error code %d)\n\n", ftStatus);
212 return -1;
213 }
214  
215 // Selected Device
216 ftStatus = FT_GetDeviceInfo(ftHandle, &Type, &ID, SerialNumber, Description, 0);
217 if (ftStatus == FT_OK)
218 {
219 printf("Selected Device\n");
220 printf(" Description \"%s\"\n", Description);
221 printf(" SerialNumber \"%s\"\n", SerialNumber);
222 //printf(" Type 0x%x\n", Type);
223 //printf(" ID 0x%x\n", ID);
224 }
225  
226 // Get Driver Version
227 DWORD dwDriverVer;
228 ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer);
229 if (ftStatus == FT_OK)
230 {
231 printf(" Device Driver Ver 0x%x\n", dwDriverVer);
232 }
233 else
234 {
235 fprintf(stderr, "FTDI: Error Reading Driver Version\n");
236 }
237  
238 // Set BitBang Mode
239 ftStatus = FT_SetBitMode(ftHandle, (UCHAR)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG
240 if (ftStatus == FT_OK)
241 {
242 // printf("Set BitBang Mode\n");
243 }
244 else
245 {
246 fprintf(stderr, "FTDI: Set BitBang Mode Failed %d\n", ftStatus);
247 }
248  
249 // Set Baud Rate
250 ftStatus = FT_SetBaudRate(ftHandle, BAUD_RATE);
251 if (ftStatus == FT_OK)
252 {
253 printf(" Baud Rate %d\n", BAUD_RATE);
254 }
255 else
256 {
257 fprintf(stderr, "FTDI: Set Baud Rate Failed %d\n", ftStatus);
258 }
259  
260 ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers
261 if (ftStatus == FT_OK)
262 {
263 // printf("Purge \n");
264 }
265 else
266 {
267 fprintf(stderr, "FTDI: FT_Purge failed %d\n", ftStatus);
268 }
269  
270 ftStatus = FT_SetLatencyTimer(ftHandle, USB_LATENCY); // Latency in ms
271 if (ftStatus == FT_OK)
272 {
273 printf(" USB Latency %d\n", USB_LATENCY);
274 }
275 else
276 {
277 fprintf(stderr, "FTDI: Set USB Latency Timer Failed %d\n", ftStatus);
278 }
279  
280 printf("\n");
281 return 0;
282 }
283  
284  
285 // Enable or Disable Activity LED
286 void jtagSetLED(bool LedEnable)
287 {
288  
289 // DBUS Connected LED (BitBang Mode)
290 LedMask = LedEnable ? PORT_LED & 0xFF : 0; // Set mask for jtagScan function
291 if (PORT_LED & 0xFF)
292 {
293 // Set / Reset LED Pin
294 DWORD BytesWritten;
295 DWORD BytesReceived;
296 unsigned char DataOut = LedMask | (PinStatus & ~PORT_LED); // Preserve PinStatus
297 unsigned char Dummy;
298 FT_Write(ftHandle, &DataOut, 1, &BytesWritten ); // Send 1 byte
299 FT_Read (ftHandle, &Dummy, 1, &BytesReceived); // Read 1 byte
300 //printf("[PinStatus %x DataOut %x]", PinStatus, DataOut);
301 }
302  
303 // CBUS Connected LED (BitBang Mode) 1 and 0 state of the port
304 const unsigned char On = ( (((PORT_LED) & 0x0F00) >> 4) | (((PORT_LED) & 0x0F00) >> 8) );
305 const unsigned char Off = ( (((PORT_LED) & 0x0F00) >> 4) );
306  
307 if (On)
308 {
309 FT_STATUS ftStatus;
310  
311 // Set / Reset LED Pin
312 ftStatus = FT_SetBitMode(ftHandle, LedEnable ? On : Off, FT_BITMODE_CBUS_BITBANG);
313  
314 // Return to used Mode
315 ftStatus = FT_SetBitMode(ftHandle, (UCHAR)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG
316 }
317 }
318  
319  
320 // Set port to Idle state
321 void jtagSetIdle()
322 {
323 char b = 0; // Idle State for JTAG pins
324 DWORD BytesWritten;
325 DWORD BytesReceived;
326  
327 // Write (idle state of pins)
328 FT_Write(ftHandle, &b, 1, &BytesWritten);
329 // Read (not to left data in input fifo)
330 FT_Read(ftHandle, &b, 1, &BytesReceived);
331 }
332  
333  
334 // Close FTDI connection
335 int jtagClosePort()
336 {
337 if (ftHandleValid)
338 {
339 jtagSetLED(false);
340 // Switch Off the Outputs
341 FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers
342 FT_SetBitMode(ftHandle, 0, FT_BITMODE_SYNC_BITBANG);
343 // Close FTDI Lib
344 FT_Close(ftHandle);
345 ftHandleValid = false;
346 }
347 return 0;
348 }
349  
350  
351 // Send data to JTAG port and bring returned data
352 int jtagScan(const unsigned char *TMS, const unsigned char *TDI, unsigned char *TDO, int bits)
353 {
354 FT_STATUS ftStatus;
355 DWORD BytesWritten;
356 DWORD BytesReceived;
357 int r, t;
358  
359 // Decompose TDI and TMS byte array to raw bitstream
360 //(1 TDI bit + 1 TMS bit --> 1 byte + 1 byte with TCK)
361 unsigned char buffer[16384];
362 if (bits > sizeof(buffer)/2)
363 {
364 fprintf(stderr, "\n FTDI: Out of Buffer Space for %d bits\n", bits);
365 return -1;
366 }
367  
368 // Switch LED On
369 jtagSetLED(true);
370  
371 // Prepare transmit data to buffer
372 for (int i = 0; i < bits; ++i)
373 {
374 unsigned char v = 0 | LedMask; // LED On / Off (on DBUS)
375 if (TMS[i/8] & (1<<(i&7)))
376 {
377 v |= (PORT_TMS);
378 // printf("T");
379 }
380 else
381 {
382 // printf("t");
383 }
384 if (TDI[i/8] & (1<<(i&7)))
385 {
386 v |= (PORT_TDI);
387 // printf("|");
388 }
389 else
390 {
391 // printf(".");
392 }
393 buffer[i * 2 + 0] = v;
394 buffer[i * 2 + 1] = v | (PORT_TCK);
395 }
396 PinStatus = buffer[bits*2-1];
397 // printf("\n");
398  
399 // Send data to FTDI
400 r = 0;
401 while (r < bits * 2)
402 {
403 t = bits * 2 - r;
404 if (t > FTDI_MAX_WRITESIZE)
405 {
406 t = FTDI_MAX_WRITESIZE;
407 }
408  
409 // printf("writing %d bytes to FTDI\n", t);
410 ftStatus = FT_Write(ftHandle, buffer+r, t, &BytesWritten);
411 if (ftStatus != FT_OK)
412 {
413 fprintf(stderr, "\n FTDI: Error Writing\n");
414 return -2;
415 }
416  
417 int i = 0;
418  
419 while (i < t)
420 {
421 FT_SetTimeouts(ftHandle, 5000, 0); // timeout 5 sec
422 ftStatus = FT_Read(ftHandle, buffer+r+i, t-i, &BytesReceived);
423 if (ftStatus == FT_OK)
424 {
425 if (BytesReceived == t-i)
426 {
427 // FT_Read OK
428 // printf("Read from FTDI %d bytes", BytesReceived);
429 }
430 else
431 {
432 // FT_Read Timeout
433 fprintf(stderr, "\n FTDI: Read Timeout\n");
434 return -2;
435 }
436 }
437 else
438 {
439 fprintf(stderr, "\n FTDI: Error Reading\n");// Error
440 return -2;
441 }
442  
443 i += BytesReceived;
444 }
445  
446 r += t;
447 }
448  
449 // Pack TDO bitstream from receive buffer to byte array
450 memset(TDO, 0, (bits + 7) / 8);
451  
452 for (int i = 0; i < bits; ++i)
453 {
454 if (buffer[i * 2 + 1] & (PORT_TDO))
455 {
456 TDO[i/8] |= 1 << (i&7);
457 // printf("H");
458 }
459 else
460 {
461 // printf("L");
462 }
463 }
464 // printf("\n");
465 // printf(" Bits %d ", bit_counter);
466  
467 // Switch LED Off
468 jtagSetLED(false);
469  
470 return 0;
471 }