1 |
// Include FTDI library |
1 |
// Include FTDI library |
2 |
#include "mlab_xvcd_port_FTDI.h" |
2 |
#include "mlab_xvcd_port_FTDI.h" |
3 |
|
3 |
|
4 |
|
4 |
|
5 |
// JTAG Output Pin Mask |
5 |
// JTAG Output Pin Mask |
6 |
#define IO_OUTPUT_MASK (PORT_TCK|PORT_TDI|PORT_TMS|PORT_LED) // Mask for all Output Pins |
6 |
#define IO_OUTPUT_MASK (PORT_TCK|PORT_TDI|PORT_TMS|PORT_LED) // Mask for all Output Pins |
7 |
|
7 |
|
8 |
|
8 |
|
9 |
// Global Variables |
9 |
// Global Variables |
10 |
FT_HANDLE ftHandle; // Handle for FTDI device |
10 |
FT_HANDLE ftHandle; // Handle for FTDI device |
11 |
bool ftHandleValid = false; // Valid Handle |
11 |
bool ftHandleValid = false; // Valid Handle |
12 |
unsigned char PinStatus = 0; // Status of DBUS pins |
12 |
unsigned char PinStatus = 0; // Status of DBUS pins |
13 |
unsigned char LedMask = 0; // LED Mask for DBUS data transfer |
13 |
unsigned char LedMask = 0; // LED Mask for DBUS data transfer |
14 |
|
14 |
|
15 |
|
15 |
|
16 |
// Convert string to int (both decimal and hex string) |
16 |
// Convert string to int (both decimal and hex string) |
17 |
int atoiEx(char *s) |
17 |
int atoiEx(char *s) |
18 |
{ |
18 |
{ |
19 |
if (s[0]=='0' && (s[1]=='x' || s[1]=='X')) |
19 |
if (s[0]=='0' && (s[1]=='x' || s[1]=='X')) |
20 |
{ |
20 |
{ |
21 |
// Hex Value |
21 |
// Hex Value |
22 |
int i; |
22 |
int i; |
23 |
#pragma warning(disable: 4996) // Disable MS warning about scanf |
23 |
#pragma warning(disable: 4996) // Disable MS warning about scanf |
24 |
sscanf(s, "%x", &i); |
24 |
sscanf(s, "%x", &i); |
25 |
return i; |
25 |
return i; |
26 |
} |
26 |
} |
27 |
else |
27 |
else |
28 |
{ |
28 |
{ |
29 |
// Decimal Value |
29 |
// Decimal Value |
30 |
return atoi(s); |
30 |
return atoi(s); |
31 |
} |
31 |
} |
32 |
} |
32 |
} |
33 |
|
33 |
|
34 |
|
34 |
|
35 |
// Print FTDI Pin Names (from mask value) |
35 |
// Print FTDI Pin Names (from mask value) |
36 |
void jtagPrintPinNames(int pinMask) |
36 |
void jtagPrintPinNames(int pinMask) |
37 |
{ |
37 |
{ |
38 |
// 16bit (MSB is CBUS, LSB is DBUS) |
38 |
// 16bit (MSB is CBUS, LSB is DBUS) |
39 |
int bit=15; |
39 |
int bit=15; |
40 |
bool useDelimiter=false; |
40 |
bool useDelimiter=false; |
41 |
|
41 |
|
42 |
do |
42 |
do |
43 |
{ |
43 |
{ |
44 |
int mask = 1 << bit; |
44 |
int mask = 1 << bit; |
45 |
if (pinMask & mask) |
45 |
if (pinMask & mask) |
46 |
{ |
46 |
{ |
47 |
if (useDelimiter) |
47 |
if (useDelimiter) |
48 |
{ |
48 |
{ |
49 |
printf("+"); |
49 |
printf("+"); |
50 |
} |
50 |
} |
51 |
if (bit > 7) |
51 |
if (bit > 7) |
52 |
{ |
52 |
{ |
53 |
printf("CBUS%c", '0' + bit - 8); |
53 |
printf("CBUS%c", '0' + bit - 8); |
54 |
} |
54 |
} |
55 |
else |
55 |
else |
56 |
{ |
56 |
{ |
57 |
printf("DBUS%c", '0' + bit); |
57 |
printf("DBUS%c", '0' + bit); |
58 |
switch (mask) |
58 |
switch (mask) |
59 |
{ |
59 |
{ |
60 |
case FTDI_TXD: printf("(TXD)"); break; |
60 |
case FTDI_TXD: printf("(TXD)"); break; |
61 |
case FTDI_RXD: printf("(RXD)"); break; |
61 |
case FTDI_RXD: printf("(RXD)"); break; |
62 |
case FTDI_RTS: printf("(RTS)"); break; |
62 |
case FTDI_RTS: printf("(RTS)"); break; |
63 |
case FTDI_CTS: printf("(CTS)"); break; |
63 |
case FTDI_CTS: printf("(CTS)"); break; |
64 |
case FTDI_DTR: printf("(DTR)"); break; |
64 |
case FTDI_DTR: printf("(DTR)"); break; |
65 |
case FTDI_DSR: printf("(DSR)"); break; |
65 |
case FTDI_DSR: printf("(DSR)"); break; |
66 |
case FTDI_DCD: printf("(DCD)"); break; |
66 |
case FTDI_DCD: printf("(DCD)"); break; |
67 |
case FTDI_RI: printf("(RI) "); break; |
67 |
case FTDI_RI: printf("(RI) "); break; |
68 |
} |
68 |
} |
69 |
} |
69 |
} |
70 |
useDelimiter = true; |
70 |
useDelimiter = true; |
71 |
} |
71 |
} |
72 |
} |
72 |
} |
73 |
while (bit-- > 0); |
73 |
while (bit-- > 0); |
74 |
} |
74 |
} |
75 |
|
75 |
|
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 |
107 |
void jtagPrintPinConfig() |
107 |
void jtagPrintPinConfig() |
108 |
{ |
108 |
{ |
109 |
// Print pin masks human readable |
109 |
// Print pin masks human readable |
110 |
printf(" JTAG Port Pins "); printf("TCK->"); jtagPrintPinNames(PORT_TCK); printf("\n"); |
110 |
printf(" JTAG Port Pins "); printf("TCK->"); jtagPrintPinNames(PORT_TCK); printf("\n"); |
111 |
printf(" "); printf("TDI->"); jtagPrintPinNames(PORT_TDI); printf("\n"); |
111 |
printf(" "); printf("TDI->"); jtagPrintPinNames(PORT_TDI); printf("\n"); |
112 |
printf(" "); printf("TDO->"); jtagPrintPinNames(PORT_TDO); printf("\n"); |
112 |
printf(" "); printf("TDO->"); jtagPrintPinNames(PORT_TDO); printf("\n"); |
113 |
printf(" "); printf("TMS->"); jtagPrintPinNames(PORT_TMS); printf("\n"); |
113 |
printf(" "); printf("TMS->"); jtagPrintPinNames(PORT_TMS); printf("\n"); |
114 |
printf(" "); printf("LED->"); jtagPrintPinNames(PORT_LED); printf("\n"); |
114 |
printf(" "); printf("LED->"); jtagPrintPinNames(PORT_LED); printf("\n"); |
115 |
} |
115 |
} |
116 |
|
116 |
|
117 |
|
117 |
|
118 |
// Connect to FTDI driver |
118 |
// Connect to FTDI driver |
119 |
int jtagOpenPort(int findDeviceBy, char *findDeviceByStr) |
119 |
int jtagOpenPort(int findDeviceBy, char *findDeviceByStr) |
120 |
{ |
120 |
{ |
121 |
// Enumerate FTDI Devices |
121 |
// Enumerate FTDI Devices |
122 |
// ---------------------- |
122 |
// ---------------------- |
123 |
|
123 |
|
124 |
FT_STATUS ftStatus; |
124 |
FT_STATUS ftStatus; |
125 |
|
125 |
|
126 |
// Print Library Version |
126 |
// Print Library Version |
127 |
printf("FTDI Connect\n"); |
127 |
printf("FTDI Connect\n"); |
128 |
DWORD dwLibraryVer; |
128 |
DWORD dwLibraryVer; |
129 |
ftStatus = FT_GetLibraryVersion(&dwLibraryVer); |
129 |
ftStatus = FT_GetLibraryVersion(&dwLibraryVer); |
130 |
if (ftStatus == FT_OK) |
130 |
if (ftStatus == FT_OK) |
131 |
printf(" Library Version 0x%x\n", dwLibraryVer); |
131 |
printf(" Library Version 0x%x\n", dwLibraryVer); |
132 |
else |
132 |
else |
133 |
fprintf(stderr, "\nFTDI: Error Reading Library Version\n"); |
133 |
fprintf(stderr, "\nFTDI: Error Reading Library Version\n"); |
134 |
|
134 |
|
135 |
// Create Device Information List |
135 |
// Create Device Information List |
136 |
DWORD numDevs = 0; |
136 |
DWORD numDevs = 0; |
137 |
ftStatus = FT_CreateDeviceInfoList(&numDevs); |
137 |
ftStatus = FT_CreateDeviceInfoList(&numDevs); |
138 |
if (ftStatus == FT_OK) |
138 |
if (ftStatus == FT_OK) |
139 |
printf(" Devices Found %d\n", numDevs); |
139 |
printf(" Devices Found %d\n", numDevs); |
140 |
else |
140 |
else |
141 |
printf(" No FTDI Device Found\n"); |
141 |
printf(" No FTDI Device Found\n"); |
142 |
|
142 |
|
143 |
if (numDevs==0) |
143 |
if (numDevs==0) |
144 |
return -1; |
144 |
return -1; |
145 |
|
145 |
|
146 |
// Print Config Info |
146 |
// Print Config Info |
147 |
jtagPrintPinConfig(); |
147 |
jtagPrintPinConfig(); |
148 |
jtagCheckPinConfig(); |
148 |
jtagCheckPinConfig(); |
149 |
printf("\n"); |
149 |
printf("\n"); |
150 |
|
150 |
|
151 |
// List All FTDI Devices |
151 |
// List All FTDI Devices |
152 |
FT_HANDLE ftHandleTemp; |
152 |
FT_HANDLE ftHandleTemp; |
153 |
DWORD Flags; |
153 |
DWORD Flags; |
154 |
DWORD ID; |
154 |
DWORD ID; |
155 |
DWORD Type; |
155 |
DWORD Type; |
156 |
DWORD LocId; |
156 |
DWORD LocId; |
157 |
char SerialNumber[16]; |
157 |
char SerialNumber[16]; |
158 |
char Description[64]; |
158 |
char Description[64]; |
159 |
for (DWORD i=0; i<numDevs; i++) |
159 |
for (DWORD i=0; i<numDevs; i++) |
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 |
{ |
171 |
printf(" Description \"%s\"\n", Description); |
171 |
printf(" Description \"%s\"\n", Description); |
172 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
172 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
173 |
//printf(" Flags 0x%x\n", Flags); |
173 |
//printf(" Flags 0x%x\n", Flags); |
174 |
//printf(" Type 0x%x\n", Type); |
174 |
//printf(" Type 0x%x\n", Type); |
175 |
//printf(" ID 0x%x\n", ID); |
175 |
//printf(" ID 0x%x\n", ID); |
176 |
printf(" Location 0x%x\n", LocId); |
176 |
printf(" Location 0x%x\n", LocId); |
177 |
} |
177 |
} |
178 |
printf("\n"); |
178 |
printf("\n"); |
179 |
} |
179 |
} |
180 |
} |
180 |
} |
181 |
|
181 |
|
182 |
// Select one Device and Open It |
182 |
// Select one Device and Open It |
183 |
unsigned int selectedDeviceIndex = 0; |
183 |
unsigned int selectedDeviceIndex = 0; |
184 |
if (findDeviceBy==0) |
184 |
if (findDeviceBy==0) |
185 |
{ |
185 |
{ |
186 |
// Select by Device Number |
186 |
// Select by Device Number |
187 |
selectedDeviceIndex = atoiEx(findDeviceByStr); |
187 |
selectedDeviceIndex = atoiEx(findDeviceByStr); |
188 |
if (numDevs<=selectedDeviceIndex) |
188 |
if (numDevs<=selectedDeviceIndex) |
189 |
{ |
189 |
{ |
190 |
fprintf(stderr, " There is no Device Number %d\n\n", selectedDeviceIndex); |
190 |
fprintf(stderr, " There is no Device Number %d\n\n", selectedDeviceIndex); |
191 |
return -1; |
191 |
return -1; |
192 |
} |
192 |
} |
193 |
// Open device |
193 |
// Open device |
194 |
ftStatus = FT_Open(selectedDeviceIndex, &ftHandle); |
194 |
ftStatus = FT_Open(selectedDeviceIndex, &ftHandle); |
195 |
} |
195 |
} |
196 |
else |
196 |
else |
197 |
{ |
197 |
{ |
198 |
// Select by Description / Serial Number / Location |
198 |
// Select by Description / Serial Number / Location |
199 |
if (findDeviceBy==FT_OPEN_BY_LOCATION) |
199 |
if (findDeviceBy==FT_OPEN_BY_LOCATION) |
200 |
{ |
200 |
{ |
201 |
// Open device (location is number, not string) |
201 |
// Open device (location is number, not string) |
202 |
int findDeviceByInt = atoiEx(findDeviceByStr); |
202 |
int findDeviceByInt = atoiEx(findDeviceByStr); |
203 |
ftStatus = FT_OpenEx((void*)findDeviceByInt, findDeviceBy, &ftHandle); |
203 |
ftStatus = FT_OpenEx((void*)findDeviceByInt, findDeviceBy, &ftHandle); |
204 |
} |
204 |
} |
205 |
else |
205 |
else |
206 |
{ |
206 |
{ |
207 |
ftStatus = FT_OpenEx(findDeviceByStr, findDeviceBy, &ftHandle); |
207 |
ftStatus = FT_OpenEx(findDeviceByStr, findDeviceBy, &ftHandle); |
208 |
} |
208 |
} |
209 |
} |
209 |
} |
210 |
|
210 |
|
211 |
// Check Status |
211 |
// Check Status |
212 |
if (ftStatus == FT_OK) |
212 |
if (ftStatus == FT_OK) |
213 |
{ |
213 |
{ |
214 |
ftHandleValid = true; |
214 |
ftHandleValid = true; |
215 |
//printf(" FTDI Device Opened\n"); |
215 |
//printf(" FTDI Device Opened\n"); |
216 |
} |
216 |
} |
217 |
else |
217 |
else |
218 |
{ |
218 |
{ |
219 |
fprintf(stderr, " Can't Open FTDI Device (error code %d)\n\n", ftStatus); |
219 |
fprintf(stderr, " Can't Open FTDI Device (error code %d)\n\n", ftStatus); |
220 |
return -1; |
220 |
return -1; |
221 |
} |
221 |
} |
222 |
|
222 |
|
223 |
// Selected Device |
223 |
// Selected Device |
224 |
ftStatus = FT_GetDeviceInfo(ftHandle, &Type, &ID, SerialNumber, Description, 0); |
224 |
ftStatus = FT_GetDeviceInfo(ftHandle, &Type, &ID, SerialNumber, Description, 0); |
225 |
if (ftStatus == FT_OK) |
225 |
if (ftStatus == FT_OK) |
226 |
{ |
226 |
{ |
227 |
printf("Selected Device\n"); |
227 |
printf("Selected Device\n"); |
228 |
printf(" Description \"%s\"\n", Description); |
228 |
printf(" Description \"%s\"\n", Description); |
229 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
229 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
230 |
//printf(" Type 0x%x\n", Type); |
230 |
//printf(" Type 0x%x\n", Type); |
231 |
//printf(" ID 0x%x\n", ID); |
231 |
//printf(" ID 0x%x\n", ID); |
232 |
} |
232 |
} |
233 |
|
233 |
|
234 |
// Get Driver Version |
234 |
// Get Driver Version |
235 |
DWORD dwDriverVer; |
235 |
DWORD dwDriverVer; |
236 |
ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); |
236 |
ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); |
237 |
if (ftStatus == FT_OK) |
237 |
if (ftStatus == FT_OK) |
238 |
{ |
238 |
{ |
239 |
printf(" Device Driver Ver 0x%x\n", dwDriverVer); |
239 |
printf(" Device Driver Ver 0x%x\n", dwDriverVer); |
240 |
} |
240 |
} |
241 |
else |
241 |
else |
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 |
253 |
{ |
253 |
{ |
254 |
fprintf(stderr, "FTDI: Set BitBang Mode Failed %d\n", ftStatus); |
254 |
fprintf(stderr, "FTDI: Set BitBang Mode Failed %d\n", ftStatus); |
255 |
} |
255 |
} |
256 |
|
256 |
|
257 |
// Set Baud Rate |
257 |
// Set Baud Rate |
258 |
ftStatus = FT_SetBaudRate(ftHandle, BAUD_RATE); |
258 |
ftStatus = FT_SetBaudRate(ftHandle, BAUD_RATE); |
259 |
if (ftStatus == FT_OK) |
259 |
if (ftStatus == FT_OK) |
260 |
{ |
260 |
{ |
261 |
printf(" Baud Rate %d\n", BAUD_RATE); |
261 |
printf(" Baud Rate %d\n", BAUD_RATE); |
262 |
} |
262 |
} |
263 |
else |
263 |
else |
264 |
{ |
264 |
{ |
265 |
fprintf(stderr, "FTDI: Set Baud Rate Failed %d\n", ftStatus); |
265 |
fprintf(stderr, "FTDI: Set Baud Rate Failed %d\n", ftStatus); |
266 |
} |
266 |
} |
267 |
|
267 |
|
268 |
ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
268 |
ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
269 |
if (ftStatus == FT_OK) |
269 |
if (ftStatus == FT_OK) |
270 |
{ |
270 |
{ |
271 |
// printf("Purge \n"); |
271 |
// printf("Purge \n"); |
272 |
} |
272 |
} |
273 |
else |
273 |
else |
274 |
{ |
274 |
{ |
275 |
fprintf(stderr, "FTDI: FT_Purge failed %d\n", ftStatus); |
275 |
fprintf(stderr, "FTDI: FT_Purge failed %d\n", ftStatus); |
276 |
} |
276 |
} |
277 |
|
277 |
|
278 |
ftStatus = FT_SetLatencyTimer(ftHandle, USB_LATENCY); // Latency in ms |
278 |
ftStatus = FT_SetLatencyTimer(ftHandle, USB_LATENCY); // Latency in ms |
279 |
if (ftStatus == FT_OK) |
279 |
if (ftStatus == FT_OK) |
280 |
{ |
280 |
{ |
281 |
printf(" USB Latency %d\n", USB_LATENCY); |
281 |
printf(" USB Latency %d\n", USB_LATENCY); |
282 |
} |
282 |
} |
283 |
else |
283 |
else |
284 |
{ |
284 |
{ |
285 |
fprintf(stderr, "FTDI: Set USB Latency Timer Failed %d\n", ftStatus); |
285 |
fprintf(stderr, "FTDI: Set USB Latency Timer Failed %d\n", ftStatus); |
286 |
} |
286 |
} |
287 |
|
287 |
|
288 |
printf("\n"); |
288 |
printf("\n"); |
289 |
return 0; |
289 |
return 0; |
290 |
} |
290 |
} |
291 |
|
291 |
|
292 |
|
292 |
|
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; |
304 |
unsigned char DataOut = LedMask | (PinStatus & ~PORT_LED); // Preserve PinStatus |
304 |
unsigned char DataOut = LedMask | (PinStatus & ~PORT_LED); // Preserve PinStatus |
305 |
unsigned char Dummy; |
305 |
unsigned char Dummy; |
306 |
FT_Write(ftHandle, &DataOut, 1, &BytesWritten ); // Send 1 byte |
306 |
FT_Write(ftHandle, &DataOut, 1, &BytesWritten ); // Send 1 byte |
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 |
329 |
void jtagSetIdle() |
327 |
void jtagSetIdle() |
330 |
{ |
328 |
{ |
331 |
char b = 0; // Idle State for JTAG pins |
329 |
char b = 0; // Idle State for JTAG pins |
332 |
DWORD BytesWritten; |
330 |
DWORD BytesWritten; |
333 |
DWORD BytesReceived; |
331 |
DWORD BytesReceived; |
334 |
|
332 |
|
335 |
// Write (idle state of pins) |
333 |
// Write (idle state of pins) |
336 |
FT_Write(ftHandle, &b, 1, &BytesWritten); |
334 |
FT_Write(ftHandle, &b, 1, &BytesWritten); |
337 |
// Read (not to left data in input fifo) |
335 |
// Read (not to left data in input fifo) |
338 |
FT_Read(ftHandle, &b, 1, &BytesReceived); |
336 |
FT_Read(ftHandle, &b, 1, &BytesReceived); |
339 |
} |
337 |
} |
340 |
|
338 |
|
341 |
|
339 |
|
342 |
// Close FTDI connection |
340 |
// Close FTDI connection |
343 |
int jtagClosePort() |
341 |
int jtagClosePort() |
344 |
{ |
342 |
{ |
345 |
if (ftHandleValid) |
343 |
if (ftHandleValid) |
346 |
{ |
344 |
{ |
347 |
jtagSetLED(false); |
345 |
jtagSetLED(false); |
348 |
// Switch Off the Outputs |
346 |
// Switch Off the Outputs |
349 |
FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
347 |
FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
350 |
FT_SetBitMode(ftHandle, 0, FT_BITMODE_SYNC_BITBANG); |
348 |
FT_SetBitMode(ftHandle, 0, FT_BITMODE_SYNC_BITBANG); |
351 |
// Close FTDI Lib |
349 |
// Close FTDI Lib |
352 |
FT_Close(ftHandle); |
350 |
FT_Close(ftHandle); |
353 |
ftHandleValid = false; |
351 |
ftHandleValid = false; |
354 |
} |
352 |
} |
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) |
371 |
{ |
369 |
{ |
372 |
fprintf(stderr, "\n FTDI: Out of Buffer Space for %d bits\n", bits); |
370 |
fprintf(stderr, "\n FTDI: Out of Buffer Space for %d bits\n", bits); |
373 |
return -1; |
371 |
return -1; |
374 |
} |
372 |
} |
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 |
408 |
r = 0; |
406 |
r = 0; |
409 |
while (r < bits * 2) |
407 |
while (r < bits * 2) |
410 |
{ |
408 |
{ |
411 |
t = bits * 2 - r; |
409 |
t = bits * 2 - r; |
412 |
if (t > FTDI_MAX_WRITESIZE) |
410 |
if (t > FTDI_MAX_WRITESIZE) |
413 |
{ |
411 |
{ |
414 |
t = FTDI_MAX_WRITESIZE; |
412 |
t = FTDI_MAX_WRITESIZE; |
415 |
} |
413 |
} |
416 |
|
414 |
|
417 |
// printf("writing %d bytes to FTDI\n", t); |
415 |
// printf("writing %d bytes to FTDI\n", t); |
418 |
ftStatus = FT_Write(ftHandle, buffer+r, t, &BytesWritten); |
416 |
ftStatus = FT_Write(ftHandle, buffer+r, t, &BytesWritten); |
419 |
if (ftStatus != FT_OK) |
417 |
if (ftStatus != FT_OK) |
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); |
431 |
if (ftStatus == FT_OK) |
429 |
if (ftStatus == FT_OK) |
432 |
{ |
430 |
{ |
433 |
if (BytesReceived == t-i) |
431 |
if (BytesReceived == t-i) |
434 |
{ |
432 |
{ |
435 |
// FT_Read OK |
433 |
// FT_Read OK |
436 |
// printf("Read from FTDI %d bytes", BytesReceived); |
434 |
// printf("Read from FTDI %d bytes", BytesReceived); |
437 |
} |
435 |
} |
438 |
else |
436 |
else |
439 |
{ |
437 |
{ |
440 |
// FT_Read Timeout |
438 |
// FT_Read Timeout |
441 |
fprintf(stderr, "\n FTDI: Read Timeout\n"); |
439 |
fprintf(stderr, "\n FTDI: Read Timeout\n"); |
442 |
return -2; |
440 |
return -2; |
443 |
} |
441 |
} |
444 |
} |
442 |
} |
445 |
else |
443 |
else |
446 |
{ |
444 |
{ |
447 |
fprintf(stderr, "\n FTDI: Error Reading\n");// Error |
445 |
fprintf(stderr, "\n FTDI: Error Reading\n");// Error |
448 |
return -2; |
446 |
return -2; |
449 |
} |
447 |
} |
450 |
|
448 |
|
451 |
i += BytesReceived; |
449 |
i += BytesReceived; |
452 |
} |
450 |
} |
453 |
|
451 |
|
454 |
r += t; |
452 |
r += t; |
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 |
468 |
{ |
466 |
{ |
469 |
// printf("L"); |
467 |
// printf("L"); |
470 |
} |
468 |
} |
471 |
} |
469 |
} |
472 |
// printf("\n"); |
470 |
// printf("\n"); |
473 |
// printf(" Bits %d ", bit_counter); |
471 |
// printf(" Bits %d ", bit_counter); |
474 |
|
472 |
|
475 |
// Switch LED Off |
473 |
// Switch LED Off |
476 |
jtagSetLED(false); |
474 |
jtagSetLED(false); |
477 |
|
475 |
|
478 |
return 0; |
476 |
return 0; |
479 |
} |
477 |
} |
480 |
|
478 |
|
481 |
// Check if Cable is still connected and accesible |
479 |
// Check if Cable is still connected and accesible |
482 |
// True is o.k. |
480 |
// True is o.k. |
483 |
bool CheckCable() |
481 |
bool CheckCable() |
484 |
{ |
482 |
{ |
485 |
FT_STATUS ftStatus; |
483 |
FT_STATUS ftStatus; |
486 |
|
484 |
|
487 |
DWORD lpdwAmountInRxQueue, lpdwAmountInTxQueue, lpdwEventStatus; |
485 |
DWORD lpdwAmountInRxQueue, lpdwAmountInTxQueue, lpdwEventStatus; |
488 |
|
486 |
|
489 |
ftStatus = FT_GetStatus(ftHandle, &lpdwAmountInRxQueue, &lpdwAmountInTxQueue, &lpdwEventStatus); |
487 |
ftStatus = FT_GetStatus(ftHandle, &lpdwAmountInRxQueue, &lpdwAmountInTxQueue, &lpdwEventStatus); |
490 |
return (ftStatus==FT_OK); |
488 |
return (ftStatus==FT_OK); |
491 |
} |
489 |
} |