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