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) |
- |
|
166 |
{ |
- |
|
167 |
printf(" Description Device is used by another process\n"); |
- |
|
168 |
} |
- |
|
169 |
else |
- |
|
170 |
{ |
165 |
printf(" Description \"%s\"\n", Description); |
171 |
printf(" Description \"%s\"\n", Description); |
166 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
172 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
167 |
//printf(" Flags 0x%x\n", Flags); |
173 |
//printf(" Flags 0x%x\n", Flags); |
168 |
//printf(" Type 0x%x\n", Type); |
174 |
//printf(" Type 0x%x\n", Type); |
169 |
//printf(" ID 0x%x\n", ID); |
175 |
//printf(" ID 0x%x\n", ID); |
170 |
printf(" Location 0x%x\n\n", LocId); |
176 |
printf(" Location 0x%x\n", LocId); |
- |
|
177 |
} |
- |
|
178 |
printf("\n"); |
171 |
} |
179 |
} |
172 |
} |
180 |
} |
173 |
|
181 |
|
174 |
// Select one Device and Open It |
182 |
// Select one Device and Open It |
175 |
unsigned int selectedDeviceIndex = 0; |
183 |
unsigned int selectedDeviceIndex = 0; |
176 |
if (findDeviceBy==0) |
184 |
if (findDeviceBy==0) |
177 |
{ |
185 |
{ |
178 |
// Select by Device Number |
186 |
// Select by Device Number |
179 |
selectedDeviceIndex = atoiEx(findDeviceByStr); |
187 |
selectedDeviceIndex = atoiEx(findDeviceByStr); |
180 |
if (numDevs<=selectedDeviceIndex) |
188 |
if (numDevs<=selectedDeviceIndex) |
181 |
{ |
189 |
{ |
182 |
fprintf(stderr, " There is no Device Number %d\n\n", selectedDeviceIndex); |
190 |
fprintf(stderr, " There is no Device Number %d\n\n", selectedDeviceIndex); |
183 |
return -1; |
191 |
return -1; |
184 |
} |
192 |
} |
185 |
// Open device |
193 |
// Open device |
186 |
ftStatus = FT_Open(selectedDeviceIndex, &ftHandle); |
194 |
ftStatus = FT_Open(selectedDeviceIndex, &ftHandle); |
187 |
} |
195 |
} |
188 |
else |
196 |
else |
189 |
{ |
197 |
{ |
190 |
// Select by Description / Serial Number / Location |
198 |
// Select by Description / Serial Number / Location |
191 |
if (findDeviceBy==FT_OPEN_BY_LOCATION) |
199 |
if (findDeviceBy==FT_OPEN_BY_LOCATION) |
192 |
{ |
200 |
{ |
193 |
// Open device (location is number, not string) |
201 |
// Open device (location is number, not string) |
194 |
int findDeviceByInt = atoiEx(findDeviceByStr); |
202 |
int findDeviceByInt = atoiEx(findDeviceByStr); |
195 |
ftStatus = FT_OpenEx((void*)findDeviceByInt, findDeviceBy, &ftHandle); |
203 |
ftStatus = FT_OpenEx((void*)findDeviceByInt, findDeviceBy, &ftHandle); |
196 |
} |
204 |
} |
197 |
else |
205 |
else |
198 |
{ |
206 |
{ |
199 |
ftStatus = FT_OpenEx(findDeviceByStr, findDeviceBy, &ftHandle); |
207 |
ftStatus = FT_OpenEx(findDeviceByStr, findDeviceBy, &ftHandle); |
200 |
} |
208 |
} |
201 |
} |
209 |
} |
202 |
|
210 |
|
203 |
// Check Status |
211 |
// Check Status |
204 |
if (ftStatus == FT_OK) |
212 |
if (ftStatus == FT_OK) |
205 |
{ |
213 |
{ |
206 |
ftHandleValid = true; |
214 |
ftHandleValid = true; |
207 |
//printf(" FTDI Device Opened\n"); |
215 |
//printf(" FTDI Device Opened\n"); |
208 |
} |
216 |
} |
209 |
else |
217 |
else |
210 |
{ |
218 |
{ |
211 |
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); |
212 |
return -1; |
220 |
return -1; |
213 |
} |
221 |
} |
214 |
|
222 |
|
215 |
// Selected Device |
223 |
// Selected Device |
216 |
ftStatus = FT_GetDeviceInfo(ftHandle, &Type, &ID, SerialNumber, Description, 0); |
224 |
ftStatus = FT_GetDeviceInfo(ftHandle, &Type, &ID, SerialNumber, Description, 0); |
217 |
if (ftStatus == FT_OK) |
225 |
if (ftStatus == FT_OK) |
218 |
{ |
226 |
{ |
219 |
printf("Selected Device\n"); |
227 |
printf("Selected Device\n"); |
220 |
printf(" Description \"%s\"\n", Description); |
228 |
printf(" Description \"%s\"\n", Description); |
221 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
229 |
printf(" SerialNumber \"%s\"\n", SerialNumber); |
222 |
//printf(" Type 0x%x\n", Type); |
230 |
//printf(" Type 0x%x\n", Type); |
223 |
//printf(" ID 0x%x\n", ID); |
231 |
//printf(" ID 0x%x\n", ID); |
224 |
} |
232 |
} |
225 |
|
233 |
|
226 |
// Get Driver Version |
234 |
// Get Driver Version |
227 |
DWORD dwDriverVer; |
235 |
DWORD dwDriverVer; |
228 |
ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); |
236 |
ftStatus = FT_GetDriverVersion(ftHandle, &dwDriverVer); |
229 |
if (ftStatus == FT_OK) |
237 |
if (ftStatus == FT_OK) |
230 |
{ |
238 |
{ |
231 |
printf(" Device Driver Ver 0x%x\n", dwDriverVer); |
239 |
printf(" Device Driver Ver 0x%x\n", dwDriverVer); |
232 |
} |
240 |
} |
233 |
else |
241 |
else |
234 |
{ |
242 |
{ |
235 |
fprintf(stderr, "FTDI: Error Reading Driver Version\n"); |
243 |
fprintf(stderr, "FTDI: Error Reading Driver Version\n"); |
236 |
} |
244 |
} |
237 |
|
245 |
|
238 |
// Set BitBang Mode |
246 |
// Set BitBang Mode |
239 |
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)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
240 |
if (ftStatus == FT_OK) |
248 |
if (ftStatus == FT_OK) |
241 |
{ |
249 |
{ |
242 |
// printf("Set BitBang Mode\n"); |
250 |
// printf("Set BitBang Mode\n"); |
243 |
} |
251 |
} |
244 |
else |
252 |
else |
245 |
{ |
253 |
{ |
246 |
fprintf(stderr, "FTDI: Set BitBang Mode Failed %d\n", ftStatus); |
254 |
fprintf(stderr, "FTDI: Set BitBang Mode Failed %d\n", ftStatus); |
247 |
} |
255 |
} |
248 |
|
256 |
|
249 |
// Set Baud Rate |
257 |
// Set Baud Rate |
250 |
ftStatus = FT_SetBaudRate(ftHandle, BAUD_RATE); |
258 |
ftStatus = FT_SetBaudRate(ftHandle, BAUD_RATE); |
251 |
if (ftStatus == FT_OK) |
259 |
if (ftStatus == FT_OK) |
252 |
{ |
260 |
{ |
253 |
printf(" Baud Rate %d\n", BAUD_RATE); |
261 |
printf(" Baud Rate %d\n", BAUD_RATE); |
254 |
} |
262 |
} |
255 |
else |
263 |
else |
256 |
{ |
264 |
{ |
257 |
fprintf(stderr, "FTDI: Set Baud Rate Failed %d\n", ftStatus); |
265 |
fprintf(stderr, "FTDI: Set Baud Rate Failed %d\n", ftStatus); |
258 |
} |
266 |
} |
259 |
|
267 |
|
260 |
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 |
261 |
if (ftStatus == FT_OK) |
269 |
if (ftStatus == FT_OK) |
262 |
{ |
270 |
{ |
263 |
// printf("Purge \n"); |
271 |
// printf("Purge \n"); |
264 |
} |
272 |
} |
265 |
else |
273 |
else |
266 |
{ |
274 |
{ |
267 |
fprintf(stderr, "FTDI: FT_Purge failed %d\n", ftStatus); |
275 |
fprintf(stderr, "FTDI: FT_Purge failed %d\n", ftStatus); |
268 |
} |
276 |
} |
269 |
|
277 |
|
270 |
ftStatus = FT_SetLatencyTimer(ftHandle, USB_LATENCY); // Latency in ms |
278 |
ftStatus = FT_SetLatencyTimer(ftHandle, USB_LATENCY); // Latency in ms |
271 |
if (ftStatus == FT_OK) |
279 |
if (ftStatus == FT_OK) |
272 |
{ |
280 |
{ |
273 |
printf(" USB Latency %d\n", USB_LATENCY); |
281 |
printf(" USB Latency %d\n", USB_LATENCY); |
274 |
} |
282 |
} |
275 |
else |
283 |
else |
276 |
{ |
284 |
{ |
277 |
fprintf(stderr, "FTDI: Set USB Latency Timer Failed %d\n", ftStatus); |
285 |
fprintf(stderr, "FTDI: Set USB Latency Timer Failed %d\n", ftStatus); |
278 |
} |
286 |
} |
279 |
|
287 |
|
280 |
printf("\n"); |
288 |
printf("\n"); |
281 |
return 0; |
289 |
return 0; |
282 |
} |
290 |
} |
283 |
|
291 |
|
284 |
|
292 |
|
285 |
// Enable or Disable Activity LED |
293 |
// Enable or Disable Activity LED |
286 |
void jtagSetLED(bool LedEnable) |
294 |
void jtagSetLED(bool LedEnable) |
287 |
{ |
295 |
{ |
288 |
|
296 |
|
289 |
// DBUS Connected LED (BitBang Mode) |
297 |
// DBUS Connected LED (BitBang Mode) |
290 |
LedMask = LedEnable ? PORT_LED & 0xFF : 0; // Set mask for jtagScan function |
298 |
LedMask = LedEnable ? PORT_LED & 0xFF : 0; // Set mask for jtagScan function |
291 |
if (PORT_LED & 0xFF) |
299 |
if (PORT_LED & 0xFF) |
292 |
{ |
300 |
{ |
293 |
// Set / Reset LED Pin |
301 |
// Set / Reset LED Pin |
294 |
DWORD BytesWritten; |
302 |
DWORD BytesWritten; |
295 |
DWORD BytesReceived; |
303 |
DWORD BytesReceived; |
296 |
unsigned char DataOut = LedMask | (PinStatus & ~PORT_LED); // Preserve PinStatus |
304 |
unsigned char DataOut = LedMask | (PinStatus & ~PORT_LED); // Preserve PinStatus |
297 |
unsigned char Dummy; |
305 |
unsigned char Dummy; |
298 |
FT_Write(ftHandle, &DataOut, 1, &BytesWritten ); // Send 1 byte |
306 |
FT_Write(ftHandle, &DataOut, 1, &BytesWritten ); // Send 1 byte |
299 |
FT_Read (ftHandle, &Dummy, 1, &BytesReceived); // Read 1 byte |
307 |
FT_Read (ftHandle, &Dummy, 1, &BytesReceived); // Read 1 byte |
300 |
//printf("[PinStatus %x DataOut %x]", PinStatus, DataOut); |
308 |
//printf("[PinStatus %x DataOut %x]", PinStatus, DataOut); |
301 |
} |
309 |
} |
302 |
|
310 |
|
303 |
// 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 |
304 |
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) ); |
305 |
const unsigned char Off = ( (((PORT_LED) & 0x0F00) >> 4) ); |
313 |
const unsigned char Off = ( (((PORT_LED) & 0x0F00) >> 4) ); |
306 |
|
314 |
|
307 |
if (On) |
315 |
if (On) |
308 |
{ |
316 |
{ |
309 |
FT_STATUS ftStatus; |
317 |
FT_STATUS ftStatus; |
310 |
|
318 |
|
311 |
// Set / Reset LED Pin |
319 |
// Set / Reset LED Pin |
312 |
ftStatus = FT_SetBitMode(ftHandle, LedEnable ? On : Off, FT_BITMODE_CBUS_BITBANG); |
320 |
ftStatus = FT_SetBitMode(ftHandle, LedEnable ? On : Off, FT_BITMODE_CBUS_BITBANG); |
313 |
|
321 |
|
314 |
// Return to used Mode |
322 |
// 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 |
323 |
ftStatus = FT_SetBitMode(ftHandle, (UCHAR)IO_OUTPUT_MASK, FT_BITMODE_SYNC_BITBANG); //FT_BITMODE_SYNC_BITBANG / FT_BITMODE_ASYNC_BITBANG |
316 |
} |
324 |
} |
317 |
} |
325 |
} |
318 |
|
326 |
|
319 |
|
327 |
|
320 |
// Set port to Idle state |
328 |
// Set port to Idle state |
321 |
void jtagSetIdle() |
329 |
void jtagSetIdle() |
322 |
{ |
330 |
{ |
323 |
char b = 0; // Idle State for JTAG pins |
331 |
char b = 0; // Idle State for JTAG pins |
324 |
DWORD BytesWritten; |
332 |
DWORD BytesWritten; |
325 |
DWORD BytesReceived; |
333 |
DWORD BytesReceived; |
326 |
|
334 |
|
327 |
// Write (idle state of pins) |
335 |
// Write (idle state of pins) |
328 |
FT_Write(ftHandle, &b, 1, &BytesWritten); |
336 |
FT_Write(ftHandle, &b, 1, &BytesWritten); |
329 |
// Read (not to left data in input fifo) |
337 |
// Read (not to left data in input fifo) |
330 |
FT_Read(ftHandle, &b, 1, &BytesReceived); |
338 |
FT_Read(ftHandle, &b, 1, &BytesReceived); |
331 |
} |
339 |
} |
332 |
|
340 |
|
333 |
|
341 |
|
334 |
// Close FTDI connection |
342 |
// Close FTDI connection |
335 |
int jtagClosePort() |
343 |
int jtagClosePort() |
336 |
{ |
344 |
{ |
337 |
if (ftHandleValid) |
345 |
if (ftHandleValid) |
338 |
{ |
346 |
{ |
339 |
jtagSetLED(false); |
347 |
jtagSetLED(false); |
340 |
// Switch Off the Outputs |
348 |
// Switch Off the Outputs |
341 |
FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
349 |
FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers |
342 |
FT_SetBitMode(ftHandle, 0, FT_BITMODE_SYNC_BITBANG); |
350 |
FT_SetBitMode(ftHandle, 0, FT_BITMODE_SYNC_BITBANG); |
343 |
// Close FTDI Lib |
351 |
// Close FTDI Lib |
344 |
FT_Close(ftHandle); |
352 |
FT_Close(ftHandle); |
345 |
ftHandleValid = false; |
353 |
ftHandleValid = false; |
346 |
} |
354 |
} |
347 |
return 0; |
355 |
return 0; |
348 |
} |
356 |
} |
349 |
|
357 |
|
350 |
|
358 |
|
351 |
// Send data to JTAG port and bring returned data |
359 |
// 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) |
360 |
int jtagScan(const unsigned char *TMS, const unsigned char *TDI, unsigned char *TDO, int bits) |
353 |
{ |
361 |
{ |
354 |
FT_STATUS ftStatus; |
362 |
FT_STATUS ftStatus; |
355 |
DWORD BytesWritten; |
363 |
DWORD BytesWritten; |
356 |
DWORD BytesReceived; |
364 |
DWORD BytesReceived; |
357 |
int r, t; |
365 |
int r, t; |
358 |
|
366 |
|
359 |
// Decompose TDI and TMS byte array to raw bitstream |
367 |
// Decompose TDI and TMS byte array to raw bitstream |
360 |
//(1 TDI bit + 1 TMS bit --> 1 byte + 1 byte with TCK) |
368 |
//(1 TDI bit + 1 TMS bit --> 1 byte + 1 byte with TCK) |
361 |
unsigned char buffer[16384]; |
369 |
unsigned char buffer[16384]; |
362 |
if (bits > sizeof(buffer)/2) |
370 |
if (bits > sizeof(buffer)/2) |
363 |
{ |
371 |
{ |
364 |
fprintf(stderr, "\n FTDI: Out of Buffer Space for %d bits\n", bits); |
372 |
fprintf(stderr, "\n FTDI: Out of Buffer Space for %d bits\n", bits); |
365 |
return -1; |
373 |
return -1; |
366 |
} |
374 |
} |
367 |
|
375 |
|
368 |
// Switch LED On |
376 |
// Switch LED On |
369 |
jtagSetLED(true); |
377 |
jtagSetLED(true); |
370 |
|
378 |
|
371 |
// Prepare transmit data to buffer |
379 |
// Prepare transmit data to buffer |
372 |
for (int i = 0; i < bits; ++i) |
380 |
for (int i = 0; i < bits; ++i) |
373 |
{ |
381 |
{ |
374 |
unsigned char v = 0 | LedMask; // LED On / Off (on DBUS) |
382 |
unsigned char v = 0 | LedMask; // LED On / Off (on DBUS) |
375 |
if (TMS[i/8] & (1<<(i&7))) |
383 |
if (TMS[i/8] & (1<<(i&7))) |
376 |
{ |
384 |
{ |
377 |
v |= (PORT_TMS); |
385 |
v |= (PORT_TMS); |
378 |
// printf("T"); |
386 |
// printf("T"); |
379 |
} |
387 |
} |
380 |
else |
388 |
else |
381 |
{ |
389 |
{ |
382 |
// printf("t"); |
390 |
// printf("t"); |
383 |
} |
391 |
} |
384 |
if (TDI[i/8] & (1<<(i&7))) |
392 |
if (TDI[i/8] & (1<<(i&7))) |
385 |
{ |
393 |
{ |
386 |
v |= (PORT_TDI); |
394 |
v |= (PORT_TDI); |
387 |
// printf("|"); |
395 |
// printf("|"); |
388 |
} |
396 |
} |
389 |
else |
397 |
else |
390 |
{ |
398 |
{ |
391 |
// printf("."); |
399 |
// printf("."); |
392 |
} |
400 |
} |
393 |
buffer[i * 2 + 0] = v; |
401 |
buffer[i * 2 + 0] = v; |
394 |
buffer[i * 2 + 1] = v | (PORT_TCK); |
402 |
buffer[i * 2 + 1] = v | (PORT_TCK); |
395 |
} |
403 |
} |
396 |
PinStatus = buffer[bits*2-1]; |
404 |
PinStatus = buffer[bits*2-1]; |
397 |
// printf("\n"); |
405 |
// printf("\n"); |
398 |
|
406 |
|
399 |
// Send data to FTDI |
407 |
// Send data to FTDI |
400 |
r = 0; |
408 |
r = 0; |
401 |
while (r < bits * 2) |
409 |
while (r < bits * 2) |
402 |
{ |
410 |
{ |
403 |
t = bits * 2 - r; |
411 |
t = bits * 2 - r; |
404 |
if (t > FTDI_MAX_WRITESIZE) |
412 |
if (t > FTDI_MAX_WRITESIZE) |
405 |
{ |
413 |
{ |
406 |
t = FTDI_MAX_WRITESIZE; |
414 |
t = FTDI_MAX_WRITESIZE; |
407 |
} |
415 |
} |
408 |
|
416 |
|
409 |
// printf("writing %d bytes to FTDI\n", t); |
417 |
// printf("writing %d bytes to FTDI\n", t); |
410 |
ftStatus = FT_Write(ftHandle, buffer+r, t, &BytesWritten); |
418 |
ftStatus = FT_Write(ftHandle, buffer+r, t, &BytesWritten); |
411 |
if (ftStatus != FT_OK) |
419 |
if (ftStatus != FT_OK) |
412 |
{ |
420 |
{ |
413 |
fprintf(stderr, "\n FTDI: Error Writing\n"); |
421 |
fprintf(stderr, "\n FTDI: Error Writing\n"); |
414 |
return -2; |
422 |
return -2; |
415 |
} |
423 |
} |
416 |
|
424 |
|
417 |
int i = 0; |
425 |
int i = 0; |
418 |
|
426 |
|
419 |
while (i < t) |
427 |
while (i < t) |
420 |
{ |
428 |
{ |
421 |
FT_SetTimeouts(ftHandle, 5000, 0); // timeout 5 sec |
429 |
FT_SetTimeouts(ftHandle, 5000, 0); // timeout 5 sec |
422 |
ftStatus = FT_Read(ftHandle, buffer+r+i, t-i, &BytesReceived); |
430 |
ftStatus = FT_Read(ftHandle, buffer+r+i, t-i, &BytesReceived); |
423 |
if (ftStatus == FT_OK) |
431 |
if (ftStatus == FT_OK) |
424 |
{ |
432 |
{ |
425 |
if (BytesReceived == t-i) |
433 |
if (BytesReceived == t-i) |
426 |
{ |
434 |
{ |
427 |
// FT_Read OK |
435 |
// FT_Read OK |
428 |
// printf("Read from FTDI %d bytes", BytesReceived); |
436 |
// printf("Read from FTDI %d bytes", BytesReceived); |
429 |
} |
437 |
} |
430 |
else |
438 |
else |
431 |
{ |
439 |
{ |
432 |
// FT_Read Timeout |
440 |
// FT_Read Timeout |
433 |
fprintf(stderr, "\n FTDI: Read Timeout\n"); |
441 |
fprintf(stderr, "\n FTDI: Read Timeout\n"); |
434 |
return -2; |
442 |
return -2; |
435 |
} |
443 |
} |
436 |
} |
444 |
} |
437 |
else |
445 |
else |
438 |
{ |
446 |
{ |
439 |
fprintf(stderr, "\n FTDI: Error Reading\n");// Error |
447 |
fprintf(stderr, "\n FTDI: Error Reading\n");// Error |
440 |
return -2; |
448 |
return -2; |
441 |
} |
449 |
} |
442 |
|
450 |
|
443 |
i += BytesReceived; |
451 |
i += BytesReceived; |
444 |
} |
452 |
} |
445 |
|
453 |
|
446 |
r += t; |
454 |
r += t; |
447 |
} |
455 |
} |
448 |
|
456 |
|
449 |
// Pack TDO bitstream from receive buffer to byte array |
457 |
// Pack TDO bitstream from receive buffer to byte array |
450 |
memset(TDO, 0, (bits + 7) / 8); |
458 |
memset(TDO, 0, (bits + 7) / 8); |
451 |
|
459 |
|
452 |
for (int i = 0; i < bits; ++i) |
460 |
for (int i = 0; i < bits; ++i) |
453 |
{ |
461 |
{ |
454 |
if (buffer[i * 2 + 1] & (PORT_TDO)) |
462 |
if (buffer[i * 2 + 1] & (PORT_TDO)) |
455 |
{ |
463 |
{ |
456 |
TDO[i/8] |= 1 << (i&7); |
464 |
TDO[i/8] |= 1 << (i&7); |
457 |
// printf("H"); |
465 |
// printf("H"); |
458 |
} |
466 |
} |
459 |
else |
467 |
else |
460 |
{ |
468 |
{ |
461 |
// printf("L"); |
469 |
// printf("L"); |
462 |
} |
470 |
} |
463 |
} |
471 |
} |
464 |
// printf("\n"); |
472 |
// printf("\n"); |
465 |
// printf(" Bits %d ", bit_counter); |
473 |
// printf(" Bits %d ", bit_counter); |
466 |
|
474 |
|
467 |
// Switch LED Off |
475 |
// Switch LED Off |
468 |
jtagSetLED(false); |
476 |
jtagSetLED(false); |
469 |
|
477 |
|
470 |
return 0; |
478 |
return 0; |
471 |
} |
479 |
} |