1 |
1779 |
julius |
/*++
|
2 |
|
|
|
3 |
|
|
FTC MPSSE Interface DLLs - Copyright © FTDI Ltd. 2009
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
The source code to the FTCI2C, FTCJTAG and FTCSPI DLLs is provided as-is and no warranty is made as to its function or reliability.
|
7 |
|
|
|
8 |
|
|
This source code may be freely modified and redistributed, provided that the FTDI Ltd. copyright notice remains intact.
|
9 |
|
|
|
10 |
|
|
Copyright (c) 2005 Future Technology Devices International Ltd.
|
11 |
|
|
|
12 |
|
|
Module Name:
|
13 |
|
|
|
14 |
|
|
FT2232c.cpp
|
15 |
|
|
|
16 |
|
|
Abstract:
|
17 |
|
|
|
18 |
|
|
FT2232C Dual Type Devices Base Class Implementation.
|
19 |
|
|
|
20 |
|
|
Environment:
|
21 |
|
|
|
22 |
|
|
kernel & user mode
|
23 |
|
|
|
24 |
|
|
Revision History:
|
25 |
|
|
|
26 |
|
|
07/02/05 kra Created.
|
27 |
|
|
25/08/05 kra Windows 2000 Professional always sets the USB buffer size to 4K ie 4096
|
28 |
|
|
19/11/08 Rene Baumann Port FTCJTAG to Linux.
|
29 |
|
|
|
30 |
|
|
--*/
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
#include <unistd.h>
|
34 |
|
|
#include <stdio.h>
|
35 |
|
|
#include <stdlib.h>
|
36 |
|
|
//#include <linux/unistd.h> // -- Possibly not needed, trying without -jb
|
37 |
|
|
#include <errno.h>
|
38 |
|
|
#include <sys/types.h>
|
39 |
|
|
#include <sys/stat.h>
|
40 |
|
|
#include <fcntl.h>
|
41 |
|
|
#include <sys/ioctl.h>
|
42 |
|
|
#include <string.h>
|
43 |
|
|
|
44 |
|
|
#include <ctype.h>
|
45 |
|
|
#include <time.h>
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
#include "FT2232c.h"
|
49 |
|
|
#include "WinTypes.h"
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
//couple of simple string functions
|
53 |
|
|
char* strupr(char *string);
|
54 |
|
|
char* strlwr(char *string);
|
55 |
|
|
|
56 |
|
|
static UINT uiNumOpenedDevices = 0; // Removed from class FT2232c in FT2232c.h to avoid segmentation fault. Rene
|
57 |
|
|
static FTC_DEVICE_DATA OpenedDevices[MAX_NUM_DEVICES]; // Removed from class FT2232c in FT2232c.h to avoid segmentation fault. Rene
|
58 |
|
|
static OutputByteBuffer OutputBuffer; // Removed from class FT2232c in FT2232c.h to avoid segmentation fault. Rene
|
59 |
|
|
static DWORD dwNumBytesToSend = 0; // Removed from class FT2232c in FT2232c.h to avoid segmentation fault. Rene
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
BOOLEAN FT2232c::FTC_DeviceInUse(LPSTR lpDeviceName, DWORD dwLocationID)
|
63 |
|
|
{
|
64 |
|
|
BOOLEAN bDeviceInUse = false;
|
65 |
|
|
DWORD dwProcessId = 0;
|
66 |
|
|
BOOLEAN bLocationIDFound = false;
|
67 |
|
|
INT iDeviceCntr = 0;
|
68 |
|
|
|
69 |
|
|
if (uiNumOpenedDevices > 0)
|
70 |
|
|
{
|
71 |
|
|
//dwProcessId = GetCurrentProcessId();
|
72 |
|
|
dwProcessId = getpid(); //Changed windows call to linux\s getpid()
|
73 |
|
|
|
74 |
|
|
for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bLocationIDFound); iDeviceCntr++)
|
75 |
|
|
{
|
76 |
|
|
// Only check device name and location id not the current application
|
77 |
|
|
if (OpenedDevices[iDeviceCntr].dwProcessId != dwProcessId)
|
78 |
|
|
{
|
79 |
|
|
if (strcmp(OpenedDevices[iDeviceCntr].szDeviceName, lpDeviceName) == 0)
|
80 |
|
|
{
|
81 |
|
|
if (OpenedDevices[iDeviceCntr].dwLocationID == dwLocationID)
|
82 |
|
|
bLocationIDFound = true;
|
83 |
|
|
}
|
84 |
|
|
}
|
85 |
|
|
}
|
86 |
|
|
|
87 |
|
|
if (bLocationIDFound)
|
88 |
|
|
bDeviceInUse = true;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
return bDeviceInUse;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
BOOLEAN FT2232c::FTC_DeviceOpened(LPSTR lpDeviceName, DWORD dwLocationID, FTC_HANDLE *pftHandle)
|
95 |
|
|
{
|
96 |
|
|
BOOLEAN bDeviceOpen = false;
|
97 |
|
|
DWORD dwProcessId = 0;
|
98 |
|
|
BOOLEAN bLocationIDFound = false;
|
99 |
|
|
INT iDeviceCntr = 0;
|
100 |
|
|
|
101 |
|
|
if (uiNumOpenedDevices > 0)
|
102 |
|
|
{
|
103 |
|
|
//dwProcessId = GetCurrentProcessId();
|
104 |
|
|
dwProcessId = getpid(); //Changed windows call to linux one
|
105 |
|
|
|
106 |
|
|
for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bLocationIDFound); iDeviceCntr++)
|
107 |
|
|
{
|
108 |
|
|
if (OpenedDevices[iDeviceCntr].dwProcessId == dwProcessId)
|
109 |
|
|
{
|
110 |
|
|
if (strcmp(OpenedDevices[iDeviceCntr].szDeviceName, lpDeviceName) == 0)
|
111 |
|
|
{
|
112 |
|
|
if (OpenedDevices[iDeviceCntr].dwLocationID == dwLocationID)
|
113 |
|
|
{
|
114 |
|
|
// Device has already been opened by this application, so just return the handle to the device
|
115 |
|
|
*pftHandle = OpenedDevices[iDeviceCntr].hDevice;
|
116 |
|
|
bLocationIDFound = true;
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
if (bLocationIDFound)
|
123 |
|
|
bDeviceOpen = true;
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
return bDeviceOpen;
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
FTC_STATUS FT2232c::FTC_IsDeviceNameLocationIDValid(LPSTR lpDeviceName, DWORD dwLocationID)
|
130 |
|
|
{
|
131 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
132 |
|
|
DWORD dwNumOfDevices = 0;
|
133 |
|
|
FT2232CDeviceIndexes FT2232CIndexes;
|
134 |
|
|
DWORD dwFlags = 0;
|
135 |
|
|
DWORD dwDeviceType = 0;
|
136 |
|
|
DWORD dwProductVendorID = 0;
|
137 |
|
|
DWORD dwLocID = 0;
|
138 |
|
|
SerialNumber szSerialNumber;
|
139 |
|
|
char szDeviceNameBuffer[DEVICE_STRING_BUFF_SIZE + 1];
|
140 |
|
|
FT_HANDLE ftHandle;
|
141 |
|
|
BOOL bDeviceNameFound = false;
|
142 |
|
|
DWORD dwDeviceIndex = 0;
|
143 |
|
|
|
144 |
|
|
// Get the number of devices connected to the system
|
145 |
|
|
Status = FTC_GetNumDevices(&dwNumOfDevices, &FT2232CIndexes);
|
146 |
|
|
|
147 |
|
|
if (Status == FTC_SUCCESS)
|
148 |
|
|
{
|
149 |
|
|
if (dwNumOfDevices > 0)
|
150 |
|
|
{
|
151 |
|
|
do
|
152 |
|
|
{
|
153 |
|
|
Status = FT_GetDeviceInfoDetail(FT2232CIndexes[dwDeviceIndex], &dwFlags, &dwDeviceType, &dwProductVendorID,
|
154 |
|
|
&dwLocID, szSerialNumber, szDeviceNameBuffer, &ftHandle);
|
155 |
|
|
|
156 |
|
|
if (Status == FTC_SUCCESS)
|
157 |
|
|
{
|
158 |
|
|
if (strcmp(szDeviceNameBuffer, lpDeviceName) == 0)
|
159 |
|
|
bDeviceNameFound = true;
|
160 |
|
|
}
|
161 |
|
|
dwDeviceIndex++;
|
162 |
|
|
}
|
163 |
|
|
while ((dwDeviceIndex < dwNumOfDevices) && (Status == FTC_SUCCESS) && (bDeviceNameFound == false));
|
164 |
|
|
|
165 |
|
|
if (bDeviceNameFound == TRUE)
|
166 |
|
|
{
|
167 |
|
|
if (dwLocID != dwLocationID)
|
168 |
|
|
Status = FTC_INVALID_LOCATION_ID;
|
169 |
|
|
}
|
170 |
|
|
else
|
171 |
|
|
Status = FTC_INVALID_DEVICE_NAME;
|
172 |
|
|
}
|
173 |
|
|
else
|
174 |
|
|
Status = FTC_DEVICE_NOT_FOUND;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
return Status;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
FTC_STATUS FT2232c::FTC_IsDeviceFT2232CType(LPSTR lpDeviceName, LPBOOL lpbFT2232CTypeDevice)
|
181 |
|
|
{
|
182 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
183 |
|
|
LPSTR pszStringSearch;
|
184 |
|
|
|
185 |
|
|
*lpbFT2232CTypeDevice = false;
|
186 |
|
|
|
187 |
|
|
// Search for the last occurrence of the channel string ie ' A'
|
188 |
|
|
if ((pszStringSearch = strstr(strupr(lpDeviceName), DEVICE_CHANNEL_A)) != NULL)
|
189 |
|
|
{
|
190 |
|
|
// Ensure the last two characters of the device name is ' A' ie channel A
|
191 |
|
|
if (strlen(pszStringSearch) == 2)
|
192 |
|
|
*lpbFT2232CTypeDevice = true;
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
return Status;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
FT2232c::FT2232c(void)
|
199 |
|
|
{
|
200 |
|
|
INT iDeviceCntr = 0;
|
201 |
|
|
|
202 |
|
|
uiNumOpenedDevices = 0;
|
203 |
|
|
|
204 |
|
|
for (iDeviceCntr = 0; (iDeviceCntr < MAX_NUM_DEVICES); iDeviceCntr++)
|
205 |
|
|
OpenedDevices[iDeviceCntr].dwProcessId = 0;
|
206 |
|
|
|
207 |
|
|
dwNumBytesToSend = 0;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
FT2232c::~FT2232c(void)
|
211 |
|
|
{
|
212 |
|
|
|
213 |
|
|
}
|
214 |
|
|
|
215 |
|
|
FTC_STATUS FT2232c::FTC_GetNumDevices(LPDWORD lpdwNumDevices, FT2232CDeviceIndexes *FT2232CIndexes)
|
216 |
|
|
{
|
217 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
218 |
|
|
DWORD dwNumOfDevices = 0;
|
219 |
|
|
DWORD dwDeviceIndex = 0;
|
220 |
|
|
DWORD dwFlags = 0;
|
221 |
|
|
DWORD dwDeviceType = 0;
|
222 |
|
|
DWORD dwProductVendorID = 0;
|
223 |
|
|
SerialNumber szSerialNumber;
|
224 |
|
|
char szDeviceNameBuffer[DEVICE_STRING_BUFF_SIZE + 1];
|
225 |
|
|
FT_HANDLE ftHandle;
|
226 |
|
|
DWORD dwLocationID;
|
227 |
|
|
DWORD i;
|
228 |
|
|
BOOL bFT2232CTypeDevice = false;
|
229 |
|
|
|
230 |
|
|
*lpdwNumDevices = 0;
|
231 |
|
|
|
232 |
|
|
// Get the number of devices connected to the system
|
233 |
|
|
Status = FT_CreateDeviceInfoList(&dwNumOfDevices);
|
234 |
|
|
|
235 |
|
|
if(DEBUG_LIST_DEVICE){
|
236 |
|
|
FT_STATUS ftStatus;
|
237 |
|
|
FT_DEVICE_LIST_INFO_NODE *devInfo;
|
238 |
|
|
DWORD numDevs;
|
239 |
|
|
// create the device information list
|
240 |
|
|
ftStatus = FT_CreateDeviceInfoList(&numDevs);
|
241 |
|
|
if (ftStatus == FT_OK) {
|
242 |
|
|
printf("Number of devices is %ld\n",numDevs);
|
243 |
|
|
}
|
244 |
|
|
if (numDevs > 0) {
|
245 |
|
|
// allocate storage for list based on numDevs
|
246 |
|
|
devInfo =
|
247 |
|
|
(FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);
|
248 |
|
|
// get the device information list
|
249 |
|
|
ftStatus = FT_GetDeviceInfoList(devInfo,&numDevs);
|
250 |
|
|
if (ftStatus == FT_OK) {
|
251 |
|
|
for ( i = 0; i < numDevs; i++) {
|
252 |
|
|
printf("Dev %ld:\n",i);
|
253 |
|
|
printf(" Flags=0x%lx\n",devInfo[i].Flags);
|
254 |
|
|
printf(" Type=0x%lx\n",devInfo[i].Type);
|
255 |
|
|
printf(" ID=0x%lx\n",devInfo[i].ID);
|
256 |
|
|
printf(" LocId=0x%lx\n",devInfo[i].LocId);
|
257 |
|
|
printf(" SerialNumber=%s\n",devInfo[i].SerialNumber);
|
258 |
|
|
printf(" Description=%s\n",devInfo[i].Description);
|
259 |
|
|
printf(" ftHandle=0x%lx\n",(unsigned long int)devInfo[i].ftHandle);
|
260 |
|
|
}
|
261 |
|
|
}
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
if (Status == FTC_SUCCESS)
|
270 |
|
|
{
|
271 |
|
|
if (dwNumOfDevices > 0)
|
272 |
|
|
{
|
273 |
|
|
do
|
274 |
|
|
{
|
275 |
|
|
// Devices previously opened by another application, results in 0 being returned for dwflags, dwDeviceType,
|
276 |
|
|
// dwProductVendorID, dwLocationID and ftHandle and empty being returned in szSerialNumber and
|
277 |
|
|
// szDeviceNameBuffer. This problem is in the FTD2XX.DLL.
|
278 |
|
|
Status = FT_GetDeviceInfoDetail(dwDeviceIndex, &dwFlags, &dwDeviceType, &dwProductVendorID,
|
279 |
|
|
&dwLocationID, szSerialNumber, szDeviceNameBuffer, &ftHandle);
|
280 |
|
|
// The flag value is a 4-byte bit map containing miscellaneous data as defined in the - Type Definitions.
|
281 |
|
|
// Bit 0 (least significant bit) of this number indicates if the port is open (1) or closed (0).
|
282 |
|
|
// Bit 1 indicates if the device is enumerated as a high-speed USB device (2) or a full-speed USB device (0).
|
283 |
|
|
// The remaining bits (2 - 31) are reserved.
|
284 |
|
|
|
285 |
|
|
// dwDeviceType = 4 --> FT_DEVICE_2232C see FT_DEVICE in ftd2xx.h
|
286 |
|
|
if (Status == FTC_SUCCESS)
|
287 |
|
|
{
|
288 |
|
|
Status = FTC_IsDeviceFT2232CType(szDeviceNameBuffer, &bFT2232CTypeDevice);
|
289 |
|
|
|
290 |
|
|
if (Status == FTC_SUCCESS)
|
291 |
|
|
{
|
292 |
|
|
if (bFT2232CTypeDevice == TRUE)
|
293 |
|
|
{
|
294 |
|
|
// The number of devices returned is, not opened devices ie channel A plus devices opened by the
|
295 |
|
|
// calling application. Devices previously opened by another application are not included in this
|
296 |
|
|
// number.
|
297 |
|
|
(*FT2232CIndexes)[*lpdwNumDevices] = dwDeviceIndex;
|
298 |
|
|
|
299 |
|
|
*lpdwNumDevices = *lpdwNumDevices + 1;
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
}
|
303 |
|
|
else
|
304 |
|
|
{
|
305 |
|
|
// if a device has already been opened by another application, the FT_GetDeviceInfoDetail call will
|
306 |
|
|
// return an INVALID_HANDLE error code, ignore this error code and continue
|
307 |
|
|
if (Status == FTC_INVALID_HANDLE)
|
308 |
|
|
Status = FTC_SUCCESS;
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
dwDeviceIndex++;
|
312 |
|
|
}
|
313 |
|
|
while ((dwDeviceIndex < dwNumOfDevices) && (Status == FTC_SUCCESS));
|
314 |
|
|
}
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
return Status;
|
318 |
|
|
}
|
319 |
|
|
|
320 |
|
|
FTC_STATUS FT2232c::FTC_GetNumNotOpenedDevices(LPDWORD lpdwNumNotOpenedDevices, FT2232CDeviceIndexes *FT2232CIndexes)
|
321 |
|
|
{
|
322 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
323 |
|
|
DWORD dwNumOfDevices = 0;
|
324 |
|
|
DWORD dwDeviceIndex = 0;
|
325 |
|
|
DWORD dwFlags = 0;
|
326 |
|
|
DWORD dwDeviceType = 0;
|
327 |
|
|
DWORD dwProductVendorID = 0;
|
328 |
|
|
SerialNumber szSerialNumber;
|
329 |
|
|
char szDeviceNameBuffer[DEVICE_STRING_BUFF_SIZE + 1];
|
330 |
|
|
FT_HANDLE ftHandle;
|
331 |
|
|
DWORD dwLocationID;
|
332 |
|
|
BOOL bFT2232CTypeDevice = false;
|
333 |
|
|
|
334 |
|
|
*lpdwNumNotOpenedDevices = 0;
|
335 |
|
|
|
336 |
|
|
// Get the number of devices connected to the system
|
337 |
|
|
Status = FT_CreateDeviceInfoList(&dwNumOfDevices);
|
338 |
|
|
|
339 |
|
|
if (Status == FTC_SUCCESS)
|
340 |
|
|
{
|
341 |
|
|
if (dwNumOfDevices > 0)
|
342 |
|
|
{
|
343 |
|
|
do
|
344 |
|
|
{
|
345 |
|
|
Status = FT_GetDeviceInfoDetail(dwDeviceIndex, &dwFlags, &dwDeviceType, &dwProductVendorID,
|
346 |
|
|
&dwLocationID, szSerialNumber, szDeviceNameBuffer, &ftHandle);
|
347 |
|
|
|
348 |
|
|
if (Status == FTC_SUCCESS)
|
349 |
|
|
{
|
350 |
|
|
Status = FTC_IsDeviceFT2232CType(szDeviceNameBuffer, &bFT2232CTypeDevice);
|
351 |
|
|
|
352 |
|
|
if (Status == FTC_SUCCESS)
|
353 |
|
|
{
|
354 |
|
|
if ((bFT2232CTypeDevice == TRUE) && ((dwFlags & DEVICE_OPENED_FLAG) == 0))
|
355 |
|
|
{
|
356 |
|
|
(*FT2232CIndexes)[*lpdwNumNotOpenedDevices] = dwDeviceIndex;
|
357 |
|
|
|
358 |
|
|
*lpdwNumNotOpenedDevices = *lpdwNumNotOpenedDevices + 1;
|
359 |
|
|
}
|
360 |
|
|
}
|
361 |
|
|
}
|
362 |
|
|
else
|
363 |
|
|
{
|
364 |
|
|
// if a device has already been opened by another application, the FT_GetDeviceInfoDetail call will
|
365 |
|
|
// return an INVALID_HANDLE error code, ignore this error code and continue
|
366 |
|
|
if (Status == FTC_INVALID_HANDLE)
|
367 |
|
|
Status = FTC_SUCCESS;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
dwDeviceIndex++;
|
371 |
|
|
}
|
372 |
|
|
while ((dwDeviceIndex < dwNumOfDevices) && (Status == FTC_SUCCESS));
|
373 |
|
|
}
|
374 |
|
|
}
|
375 |
|
|
|
376 |
|
|
return Status;
|
377 |
|
|
}
|
378 |
|
|
|
379 |
|
|
FTC_STATUS FT2232c::FTC_GetDeviceNameLocationID(DWORD dwDeviceIndex, LPSTR lpDeviceName, DWORD dwBufferSize, LPDWORD lpdwLocationID)
|
380 |
|
|
{
|
381 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
382 |
|
|
DWORD dwNumDevices = 0;
|
383 |
|
|
FT2232CDeviceIndexes FT2232CIndexes;
|
384 |
|
|
DWORD dwFlags = 0;
|
385 |
|
|
DWORD dwDeviceType = 0;
|
386 |
|
|
DWORD dwProductVendorID = 0;
|
387 |
|
|
SerialNumber szSerialNumber;
|
388 |
|
|
char szDeviceNameBuffer[DEVICE_STRING_BUFF_SIZE + 1];
|
389 |
|
|
FT_HANDLE ftHandle;
|
390 |
|
|
|
391 |
|
|
if (lpDeviceName != NULL)
|
392 |
|
|
{
|
393 |
|
|
Status = FTC_GetNumDevices(&dwNumDevices, &FT2232CIndexes);
|
394 |
|
|
|
395 |
|
|
if (Status == FTC_SUCCESS)
|
396 |
|
|
{
|
397 |
|
|
if (dwNumDevices > 0)
|
398 |
|
|
{
|
399 |
|
|
if (dwDeviceIndex < dwNumDevices)
|
400 |
|
|
{
|
401 |
|
|
Status = FT_GetDeviceInfoDetail(FT2232CIndexes[dwDeviceIndex], &dwFlags, &dwDeviceType, &dwProductVendorID,
|
402 |
|
|
lpdwLocationID, szSerialNumber, szDeviceNameBuffer, &ftHandle);
|
403 |
|
|
|
404 |
|
|
if (Status == FTC_SUCCESS)
|
405 |
|
|
{
|
406 |
|
|
if (strlen(szDeviceNameBuffer) <= dwBufferSize)
|
407 |
|
|
strcpy(lpDeviceName, szDeviceNameBuffer);
|
408 |
|
|
else
|
409 |
|
|
Status = FTC_DEVICE_NAME_BUFFER_TOO_SMALL;
|
410 |
|
|
}
|
411 |
|
|
}
|
412 |
|
|
else
|
413 |
|
|
Status = FTC_INVALID_DEVICE_NAME_INDEX;
|
414 |
|
|
}
|
415 |
|
|
else
|
416 |
|
|
Status = FTC_DEVICE_NOT_FOUND;
|
417 |
|
|
}
|
418 |
|
|
}
|
419 |
|
|
else
|
420 |
|
|
Status = FTC_NULL_DEVICE_NAME_BUFFER_POINTER;
|
421 |
|
|
|
422 |
|
|
return Status;
|
423 |
|
|
}
|
424 |
|
|
|
425 |
|
|
FTC_STATUS FT2232c::FTC_OpenSpecifiedDevice(LPSTR lpDeviceName, DWORD dwLocationID, FTC_HANDLE *pftHandle)
|
426 |
|
|
{
|
427 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
428 |
|
|
FT_HANDLE ftHandle;
|
429 |
|
|
|
430 |
|
|
if (lpDeviceName != NULL)
|
431 |
|
|
{
|
432 |
|
|
|
433 |
|
|
// Caused driver to remove both devices loaded by ftdi_sio in /dev/
|
434 |
|
|
// We wish the second, /dev/ttyUSB1 to remain to connect a terminal
|
435 |
|
|
// program to it. - jb 090301
|
436 |
|
|
//Status = FTC_IsDeviceNameLocationIDValid(lpDeviceName, dwLocationID);
|
437 |
|
|
|
438 |
|
|
if (Status == FTC_SUCCESS)
|
439 |
|
|
{
|
440 |
|
|
|
441 |
|
|
if (!FTC_DeviceInUse(lpDeviceName, dwLocationID))
|
442 |
|
|
{
|
443 |
|
|
|
444 |
|
|
if (!FTC_DeviceOpened(lpDeviceName, dwLocationID, pftHandle))
|
445 |
|
|
{
|
446 |
|
|
|
447 |
|
|
// To open a device in Linux the FT_OPEN_BY_DESCRIPTION has to be used. Rene
|
448 |
|
|
Status = FT_OpenEx((PVOID)lpDeviceName, FT_OPEN_BY_DESCRIPTION, &ftHandle);
|
449 |
|
|
// Linux das not return the LocationID
|
450 |
|
|
// Status = FT_OpenEx((PVOID)dwLocationID, FT_OPEN_BY_LOCATION, &ftHandle);
|
451 |
|
|
|
452 |
|
|
if (Status == FTC_SUCCESS)
|
453 |
|
|
{
|
454 |
|
|
*pftHandle = (DWORD)ftHandle;
|
455 |
|
|
|
456 |
|
|
FTC_InsertDeviceHandle(lpDeviceName, dwLocationID, *pftHandle);
|
457 |
|
|
}
|
458 |
|
|
|
459 |
|
|
}
|
460 |
|
|
|
461 |
|
|
}
|
462 |
|
|
else
|
463 |
|
|
Status = FTC_DEVICE_IN_USE;
|
464 |
|
|
}
|
465 |
|
|
}
|
466 |
|
|
else
|
467 |
|
|
Status = FTC_NULL_DEVICE_NAME_BUFFER_POINTER;
|
468 |
|
|
|
469 |
|
|
return Status;
|
470 |
|
|
}
|
471 |
|
|
|
472 |
|
|
FTC_STATUS FT2232c::FTC_OpenDevice(FTC_HANDLE *pftHandle)
|
473 |
|
|
{
|
474 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
475 |
|
|
DWORD dwNumDevices = 0;
|
476 |
|
|
FT2232CDeviceIndexes FT2232CIndexes;
|
477 |
|
|
char szDeviceName[DEVICE_STRING_BUFF_SIZE + 1];
|
478 |
|
|
DWORD dwLocationID;
|
479 |
|
|
|
480 |
|
|
Status = FTC_GetNumDevices(&dwNumDevices, &FT2232CIndexes);
|
481 |
|
|
|
482 |
|
|
if (Status == FTC_SUCCESS)
|
483 |
|
|
{
|
484 |
|
|
if (dwNumDevices == 1)
|
485 |
|
|
{
|
486 |
|
|
Status = FTC_GetDeviceNameLocationID(FT2232CIndexes[0], szDeviceName, (DEVICE_STRING_BUFF_SIZE + 1), &dwLocationID);
|
487 |
|
|
|
488 |
|
|
if (Status == FTC_SUCCESS)
|
489 |
|
|
Status = FTC_OpenSpecifiedDevice(szDeviceName, dwLocationID, pftHandle);
|
490 |
|
|
}
|
491 |
|
|
else
|
492 |
|
|
{
|
493 |
|
|
if (dwNumDevices == 0)
|
494 |
|
|
Status = FTC_DEVICE_NOT_FOUND;
|
495 |
|
|
else
|
496 |
|
|
Status = FTC_TOO_MANY_DEVICES;
|
497 |
|
|
}
|
498 |
|
|
}
|
499 |
|
|
|
500 |
|
|
return Status;
|
501 |
|
|
}
|
502 |
|
|
|
503 |
|
|
FTC_STATUS FT2232c::FTC_CloseDevice(FTC_HANDLE ftHandle)
|
504 |
|
|
{
|
505 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
506 |
|
|
|
507 |
|
|
Status = FTC_IsDeviceHandleValid(ftHandle);
|
508 |
|
|
|
509 |
|
|
if (Status == FTC_SUCCESS)
|
510 |
|
|
{
|
511 |
|
|
Status = FT_Close((FT_HANDLE)ftHandle);
|
512 |
|
|
|
513 |
|
|
FTC_RemoveDeviceHandle(ftHandle);
|
514 |
|
|
}
|
515 |
|
|
|
516 |
|
|
return Status;
|
517 |
|
|
}
|
518 |
|
|
|
519 |
|
|
void FT2232c::FTC_GetClockFrequencyValues(DWORD dwClockFrequencyValue, LPDWORD lpdwClockFrequencyHz)
|
520 |
|
|
{
|
521 |
|
|
*lpdwClockFrequencyHz = (BASE_CLOCK_FREQUENCY_12_MHZ / ((1 + dwClockFrequencyValue) * 2));
|
522 |
|
|
}
|
523 |
|
|
|
524 |
|
|
FTC_STATUS FT2232c::FTC_SetDeviceLoopbackState(FTC_HANDLE ftHandle, BOOL bLoopbackState)
|
525 |
|
|
{
|
526 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
527 |
|
|
|
528 |
|
|
Status = FTC_IsDeviceHandleValid(ftHandle);
|
529 |
|
|
|
530 |
|
|
if (Status == FTC_SUCCESS)
|
531 |
|
|
{
|
532 |
|
|
if (bLoopbackState == false)
|
533 |
|
|
// turn off loopback
|
534 |
|
|
FTC_AddByteToOutputBuffer(TURN_OFF_LOOPBACK_CMD, true);
|
535 |
|
|
else
|
536 |
|
|
// turn on loopback
|
537 |
|
|
FTC_AddByteToOutputBuffer(TURN_ON_LOOPBACK_CMD, true);
|
538 |
|
|
|
539 |
|
|
FTC_SendBytesToDevice(ftHandle);
|
540 |
|
|
}
|
541 |
|
|
|
542 |
|
|
return Status;
|
543 |
|
|
}
|
544 |
|
|
|
545 |
|
|
void FT2232c::FTC_InsertDeviceHandle(LPSTR lpDeviceName, DWORD dwLocationID, FTC_HANDLE ftHandle)
|
546 |
|
|
{
|
547 |
|
|
DWORD dwProcessId = 0;
|
548 |
|
|
INT iDeviceCntr = 0;
|
549 |
|
|
BOOLEAN bDeviceftHandleInserted = false;
|
550 |
|
|
|
551 |
|
|
if (uiNumOpenedDevices < MAX_NUM_DEVICES)
|
552 |
|
|
{
|
553 |
|
|
//dwProcessId = GetCurrentProcessId();
|
554 |
|
|
dwProcessId = getpid(); //Changed windows call to linux\s getpid()
|
555 |
|
|
|
556 |
|
|
for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDeviceftHandleInserted); iDeviceCntr++)
|
557 |
|
|
{
|
558 |
|
|
if (OpenedDevices[iDeviceCntr].dwProcessId == 0)
|
559 |
|
|
{
|
560 |
|
|
OpenedDevices[iDeviceCntr].dwProcessId = dwProcessId;
|
561 |
|
|
strcpy(OpenedDevices[iDeviceCntr].szDeviceName, lpDeviceName);
|
562 |
|
|
OpenedDevices[iDeviceCntr].dwLocationID = dwLocationID;
|
563 |
|
|
OpenedDevices[iDeviceCntr].hDevice = ftHandle;
|
564 |
|
|
|
565 |
|
|
uiNumOpenedDevices = uiNumOpenedDevices + 1;
|
566 |
|
|
|
567 |
|
|
bDeviceftHandleInserted = true;
|
568 |
|
|
}
|
569 |
|
|
}
|
570 |
|
|
}
|
571 |
|
|
}
|
572 |
|
|
|
573 |
|
|
FTC_STATUS FT2232c::FTC_IsDeviceHandleValid(FTC_HANDLE ftHandle)
|
574 |
|
|
{
|
575 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
576 |
|
|
DWORD dwProcessId = 0;
|
577 |
|
|
INT iDeviceCntr = 0;
|
578 |
|
|
BOOLEAN bDevicempHandleFound = false;
|
579 |
|
|
|
580 |
|
|
if ((uiNumOpenedDevices > 0) && (ftHandle > 0))
|
581 |
|
|
{
|
582 |
|
|
//dwProcessId = GetCurrentProcessId();
|
583 |
|
|
dwProcessId = getpid(); //Changed windows call to linux\s getpid()
|
584 |
|
|
|
585 |
|
|
for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDevicempHandleFound); iDeviceCntr++)
|
586 |
|
|
{
|
587 |
|
|
if (OpenedDevices[iDeviceCntr].dwProcessId == dwProcessId)
|
588 |
|
|
{
|
589 |
|
|
if (OpenedDevices[iDeviceCntr].hDevice == ftHandle)
|
590 |
|
|
bDevicempHandleFound = true;
|
591 |
|
|
}
|
592 |
|
|
}
|
593 |
|
|
|
594 |
|
|
if (!bDevicempHandleFound)
|
595 |
|
|
Status = FTC_INVALID_HANDLE;
|
596 |
|
|
}
|
597 |
|
|
else
|
598 |
|
|
Status = FTC_INVALID_HANDLE;
|
599 |
|
|
|
600 |
|
|
return Status;
|
601 |
|
|
}
|
602 |
|
|
|
603 |
|
|
|
604 |
|
|
void FT2232c::FTC_RemoveDeviceHandle(FTC_HANDLE ftHandle)
|
605 |
|
|
{
|
606 |
|
|
DWORD dwProcessId = 0;
|
607 |
|
|
INT iDeviceCntr = 0;
|
608 |
|
|
BOOLEAN bDevicempHandleFound = false;
|
609 |
|
|
|
610 |
|
|
if (uiNumOpenedDevices > 0)
|
611 |
|
|
{
|
612 |
|
|
//dwProcessId = GetCurrentProcessId();
|
613 |
|
|
dwProcessId = getpid(); //Changed windows call to linux\s getpid()
|
614 |
|
|
|
615 |
|
|
for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDevicempHandleFound); iDeviceCntr++)
|
616 |
|
|
{
|
617 |
|
|
if (OpenedDevices[iDeviceCntr].dwProcessId == dwProcessId)
|
618 |
|
|
{
|
619 |
|
|
if (OpenedDevices[iDeviceCntr].hDevice == ftHandle)
|
620 |
|
|
{
|
621 |
|
|
OpenedDevices[iDeviceCntr].dwProcessId = 0;
|
622 |
|
|
strcpy(OpenedDevices[iDeviceCntr].szDeviceName, "");
|
623 |
|
|
OpenedDevices[iDeviceCntr].dwLocationID = 0;
|
624 |
|
|
OpenedDevices[iDeviceCntr].hDevice = 0;
|
625 |
|
|
|
626 |
|
|
uiNumOpenedDevices = uiNumOpenedDevices - 1;
|
627 |
|
|
|
628 |
|
|
bDevicempHandleFound = true;
|
629 |
|
|
}
|
630 |
|
|
}
|
631 |
|
|
}
|
632 |
|
|
}
|
633 |
|
|
}
|
634 |
|
|
|
635 |
|
|
FTC_STATUS FT2232c::FTC_ResetUSBDevicePurgeUSBInputBuffer(FTC_HANDLE ftHandle)
|
636 |
|
|
{
|
637 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
638 |
|
|
InputByteBuffer InputBuffer;
|
639 |
|
|
DWORD dwNumBytesRead = 0;
|
640 |
|
|
DWORD dwNumBytesDeviceInputBuffer;
|
641 |
|
|
|
642 |
|
|
Status = FT_ResetDevice((FT_HANDLE)ftHandle);
|
643 |
|
|
|
644 |
|
|
if (Status == FTC_SUCCESS)
|
645 |
|
|
{
|
646 |
|
|
// Get the number of bytes in the device input buffer
|
647 |
|
|
Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
|
648 |
|
|
|
649 |
|
|
if (Status == FTC_SUCCESS)
|
650 |
|
|
{
|
651 |
|
|
if (dwNumBytesDeviceInputBuffer > 0)
|
652 |
|
|
FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
|
653 |
|
|
}
|
654 |
|
|
}
|
655 |
|
|
|
656 |
|
|
return Status;
|
657 |
|
|
}
|
658 |
|
|
|
659 |
|
|
FTC_STATUS FT2232c::FTC_SetDeviceUSBBufferSizes(FTC_HANDLE ftHandle, DWORD InputBufferSize, DWORD OutputBufferSize)
|
660 |
|
|
{
|
661 |
|
|
return FT_SetUSBParameters((FT_HANDLE)ftHandle, InputBufferSize, OutputBufferSize);
|
662 |
|
|
}
|
663 |
|
|
|
664 |
|
|
FTC_STATUS FT2232c::FTC_SetDeviceSpecialCharacters(FTC_HANDLE ftHandle, BOOLEAN bEventEnabled, UCHAR EventCharacter,
|
665 |
|
|
BOOLEAN bErrorEnabled, UCHAR ErrorCharacter)
|
666 |
|
|
{
|
667 |
|
|
UCHAR EventCharEnabled = UCHAR(bEventEnabled);
|
668 |
|
|
UCHAR ErrorCharEnabled = UCHAR(bErrorEnabled);
|
669 |
|
|
|
670 |
|
|
// Set the special characters for the device. disable event and error characters
|
671 |
|
|
return FT_SetChars((FT_HANDLE)ftHandle, EventCharacter, EventCharEnabled, ErrorCharacter, ErrorCharEnabled);
|
672 |
|
|
}
|
673 |
|
|
|
674 |
|
|
FTC_STATUS FT2232c::FTC_SetReadWriteDeviceTimeouts(FTC_HANDLE ftHandle, DWORD dwReadTimeoutmSec, DWORD dwWriteTimeoutmSec)
|
675 |
|
|
{
|
676 |
|
|
// Sets the read and write timeouts in milli-seconds for the device
|
677 |
|
|
return FT_SetTimeouts((FT_HANDLE)ftHandle, dwReadTimeoutmSec, dwWriteTimeoutmSec);
|
678 |
|
|
}
|
679 |
|
|
|
680 |
|
|
FTC_STATUS FT2232c::FTC_SetDeviceLatencyTimer(FTC_HANDLE ftHandle, BYTE LatencyTimermSec)
|
681 |
|
|
{
|
682 |
|
|
// Set the device latency timer to a number of milliseconds
|
683 |
|
|
return FT_SetLatencyTimer((FT_HANDLE)ftHandle, LatencyTimermSec);
|
684 |
|
|
}
|
685 |
|
|
|
686 |
|
|
FTC_STATUS FT2232c::FTC_ResetMPSSEInterface(FTC_HANDLE ftHandle)
|
687 |
|
|
{
|
688 |
|
|
return FT_SetBitMode((FT_HANDLE)ftHandle, MPSSE_INTERFACE_MASK, RESET_MPSSE_INTERFACE);
|
689 |
|
|
}
|
690 |
|
|
|
691 |
|
|
FTC_STATUS FT2232c::FTC_EnableMPSSEInterface(FTC_HANDLE ftHandle)
|
692 |
|
|
{
|
693 |
|
|
return FT_SetBitMode((FT_HANDLE)ftHandle, MPSSE_INTERFACE_MASK, ENABLE_MPSSE_INTERFACE);
|
694 |
|
|
}
|
695 |
|
|
|
696 |
|
|
FTC_STATUS FT2232c::FTC_SendReceiveCommandFromMPSSEInterface(FTC_HANDLE ftHandle, BOOLEAN bSendEchoCommandContinuouslyOnce, BYTE EchoCommand, LPBOOL lpbCommandEchod)
|
697 |
|
|
{
|
698 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
699 |
|
|
DWORD dwNumBytesDeviceInputBuffer = 0;
|
700 |
|
|
//SYSTEMTIME StartTime;
|
701 |
|
|
InputByteBuffer InputBuffer;
|
702 |
|
|
DWORD dwNumBytesRead = 0;
|
703 |
|
|
DWORD dwByteCntr = 0;
|
704 |
|
|
BOOL bBadCommandResponse = false;
|
705 |
|
|
|
706 |
|
|
// New timeout detection variables for linux build -- Julius
|
707 |
|
|
struct timeval tv;
|
708 |
|
|
time_t time_start; // to store number of seconds since unix epoch
|
709 |
|
|
|
710 |
|
|
|
711 |
|
|
*lpbCommandEchod = false;
|
712 |
|
|
|
713 |
|
|
if (!bSendEchoCommandContinuouslyOnce)
|
714 |
|
|
{
|
715 |
|
|
// Causes the device to echo back the command character and wait in command mode
|
716 |
|
|
FTC_AddByteToOutputBuffer(EchoCommand, true);
|
717 |
|
|
FTC_SendBytesToDevice(ftHandle);
|
718 |
|
|
}
|
719 |
|
|
|
720 |
|
|
// Windows only time functions - removed for linux library build -- Julius
|
721 |
|
|
//GetLocalTime(&StartTime);
|
722 |
|
|
gettimeofday(&tv, NULL);
|
723 |
|
|
time_start = tv.tv_sec;
|
724 |
|
|
|
725 |
|
|
do
|
726 |
|
|
{
|
727 |
|
|
// Send the echo command every time round the loop
|
728 |
|
|
if (bSendEchoCommandContinuouslyOnce)
|
729 |
|
|
{
|
730 |
|
|
// Causes the device to echo back the command character and wait in command mode
|
731 |
|
|
FTC_AddByteToOutputBuffer(EchoCommand, true);
|
732 |
|
|
FTC_SendBytesToDevice(ftHandle);
|
733 |
|
|
}
|
734 |
|
|
|
735 |
|
|
// Get the number of bytes in the device input buffer
|
736 |
|
|
Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
|
737 |
|
|
|
738 |
|
|
if (Status == FTC_SUCCESS)
|
739 |
|
|
{
|
740 |
|
|
//Sleep(0); // give up timeslice - not exist in linux!
|
741 |
|
|
// perhaps #include <sched.h> and do a sched_yield();
|
742 |
|
|
// This basically checks if the time we took from the first GetLocalTime was bigger than
|
743 |
|
|
// MAX_COMMAND_TIMEOUT_PERIOD
|
744 |
|
|
//if (FTC_Timeout(StartTime, MAX_COMMAND_TIMEOUT_PERIOD))
|
745 |
|
|
gettimeofday(&tv, NULL);
|
746 |
|
|
if ((tv.tv_sec - time_start) > (MAX_COMMAND_TIMEOUT_PERIOD/1000))
|
747 |
|
|
Status = FTC_FAILED_TO_COMPLETE_COMMAND;
|
748 |
|
|
|
749 |
|
|
|
750 |
|
|
|
751 |
|
|
}
|
752 |
|
|
|
753 |
|
|
if (Status == FTC_SUCCESS)
|
754 |
|
|
{
|
755 |
|
|
if (dwNumBytesDeviceInputBuffer > 0)
|
756 |
|
|
{
|
757 |
|
|
FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
|
758 |
|
|
|
759 |
|
|
if (dwNumBytesRead > 0)
|
760 |
|
|
{
|
761 |
|
|
dwByteCntr = 0;
|
762 |
|
|
|
763 |
|
|
do
|
764 |
|
|
{
|
765 |
|
|
if (dwByteCntr <= (dwNumBytesRead - 1))
|
766 |
|
|
{
|
767 |
|
|
if (InputBuffer[dwByteCntr] == BAD_COMMAND_RESPONSE)
|
768 |
|
|
bBadCommandResponse = true;
|
769 |
|
|
else
|
770 |
|
|
{
|
771 |
|
|
if (bBadCommandResponse == TRUE)
|
772 |
|
|
{
|
773 |
|
|
if (InputBuffer[dwByteCntr] == EchoCommand)
|
774 |
|
|
*lpbCommandEchod = true;
|
775 |
|
|
}
|
776 |
|
|
|
777 |
|
|
bBadCommandResponse = false;
|
778 |
|
|
}
|
779 |
|
|
}
|
780 |
|
|
|
781 |
|
|
dwByteCntr = dwByteCntr + 1;
|
782 |
|
|
}
|
783 |
|
|
while ((dwByteCntr < dwNumBytesRead) && (*lpbCommandEchod == false));
|
784 |
|
|
}
|
785 |
|
|
}
|
786 |
|
|
}
|
787 |
|
|
}
|
788 |
|
|
while ((*lpbCommandEchod == false) && (Status == FTC_SUCCESS));
|
789 |
|
|
|
790 |
|
|
return Status;
|
791 |
|
|
}
|
792 |
|
|
|
793 |
|
|
FTC_STATUS FT2232c::FTC_SynchronizeMPSSEInterface(FTC_HANDLE ftHandle)
|
794 |
|
|
{
|
795 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
796 |
|
|
DWORD dwNumBytesDeviceInputBuffer = 0;
|
797 |
|
|
InputByteBuffer InputBuffer;
|
798 |
|
|
DWORD dwNumBytesRead = 0;
|
799 |
|
|
BOOL bCommandEchod = false;
|
800 |
|
|
|
801 |
|
|
// Get the number of bytes in the device input buffer
|
802 |
|
|
Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
|
803 |
|
|
|
804 |
|
|
if (Status == FTC_SUCCESS)
|
805 |
|
|
{
|
806 |
|
|
if (dwNumBytesDeviceInputBuffer > 0)
|
807 |
|
|
FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
|
808 |
|
|
|
809 |
|
|
Status = FTC_SendReceiveCommandFromMPSSEInterface(ftHandle, TRUE, AA_ECHO_CMD_1, &bCommandEchod);
|
810 |
|
|
|
811 |
|
|
if (Status == FTC_SUCCESS)
|
812 |
|
|
{
|
813 |
|
|
if (bCommandEchod == TRUE)
|
814 |
|
|
{
|
815 |
|
|
Status = FTC_SendReceiveCommandFromMPSSEInterface(ftHandle, FALSE, AB_ECHO_CMD_2, &bCommandEchod);
|
816 |
|
|
|
817 |
|
|
if (Status == FTC_SUCCESS)
|
818 |
|
|
{
|
819 |
|
|
if (bCommandEchod == false)
|
820 |
|
|
Status = FTC_FAILED_TO_SYNCHRONIZE_DEVICE_MPSSE;
|
821 |
|
|
}
|
822 |
|
|
}
|
823 |
|
|
else
|
824 |
|
|
Status = FTC_FAILED_TO_SYNCHRONIZE_DEVICE_MPSSE;
|
825 |
|
|
}
|
826 |
|
|
}
|
827 |
|
|
|
828 |
|
|
return Status;
|
829 |
|
|
}
|
830 |
|
|
|
831 |
|
|
/*
|
832 |
|
|
BOOLEAN FT2232c::FTC_Timeout(SYSTEMTIME StartSystemTime, DWORD dwTimeoutmSecs)
|
833 |
|
|
{
|
834 |
|
|
BOOLEAN bTimoutExpired = false;
|
835 |
|
|
FILETIME StartFileTime;
|
836 |
|
|
ULARGE_INTEGER StartTime;
|
837 |
|
|
SYSTEMTIME EndSystemTime;
|
838 |
|
|
FILETIME EndFileTime;
|
839 |
|
|
ULARGE_INTEGER EndTime;
|
840 |
|
|
ULONGLONG ulTimeoutmSecs = dwTimeoutmSecs * CONVERT_1MS_TO_100NS; //10000;
|
841 |
|
|
|
842 |
|
|
GetLocalTime(&EndSystemTime);
|
843 |
|
|
|
844 |
|
|
SystemTimeToFileTime(&StartSystemTime, &StartFileTime);
|
845 |
|
|
|
846 |
|
|
StartTime.LowPart = StartFileTime.dwLowDateTime;
|
847 |
|
|
StartTime.HighPart = StartFileTime.dwHighDateTime;
|
848 |
|
|
|
849 |
|
|
SystemTimeToFileTime(&EndSystemTime, &EndFileTime);
|
850 |
|
|
|
851 |
|
|
EndTime.LowPart = EndFileTime.dwLowDateTime;
|
852 |
|
|
EndTime.HighPart = EndFileTime.dwHighDateTime;
|
853 |
|
|
|
854 |
|
|
if ((EndTime.QuadPart - StartTime.QuadPart) > ulTimeoutmSecs)
|
855 |
|
|
bTimoutExpired = true;
|
856 |
|
|
|
857 |
|
|
return bTimoutExpired;
|
858 |
|
|
}*/
|
859 |
|
|
|
860 |
|
|
FTC_STATUS FT2232c::FTC_GetNumberBytesFromDeviceInputBuffer(FTC_HANDLE ftHandle, LPDWORD lpdwNumBytesDeviceInputBuffer)
|
861 |
|
|
{
|
862 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
863 |
|
|
//SYSTEMTIME StartTime;
|
864 |
|
|
|
865 |
|
|
// New timeout detection variables for linux build -- Julius
|
866 |
|
|
struct timeval tv;
|
867 |
|
|
time_t time_start; // to store number of seconds since unix epoch
|
868 |
|
|
|
869 |
|
|
|
870 |
|
|
// Windows only time functions - removed for linux library build -- Julius
|
871 |
|
|
//GetLocalTime(&StartTime);
|
872 |
|
|
gettimeofday(&tv, NULL);
|
873 |
|
|
time_start = tv.tv_sec;
|
874 |
|
|
|
875 |
|
|
|
876 |
|
|
do
|
877 |
|
|
{
|
878 |
|
|
// Get the number of bytes in the device input buffer
|
879 |
|
|
Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, lpdwNumBytesDeviceInputBuffer);
|
880 |
|
|
|
881 |
|
|
if (Status == FTC_SUCCESS)
|
882 |
|
|
{
|
883 |
|
|
//Sleep(0); // give up timeslice - not exist in linux!
|
884 |
|
|
// perhaps #include <sched.h> and do a sched_yield();
|
885 |
|
|
// This basically checks if the time we took from the first GetLocalTime was bigger than
|
886 |
|
|
// MAX_COMMAND_TIMEOUT_PERIOD
|
887 |
|
|
//if (FTC_Timeout(StartTime, MAX_COMMAND_TIMEOUT_PERIOD))
|
888 |
|
|
gettimeofday(&tv, NULL);
|
889 |
|
|
if ((tv.tv_sec - time_start) > (MAX_COMMAND_TIMEOUT_PERIOD / 1000))
|
890 |
|
|
Status = FTC_FAILED_TO_COMPLETE_COMMAND;
|
891 |
|
|
}
|
892 |
|
|
}
|
893 |
|
|
while ((*lpdwNumBytesDeviceInputBuffer == 0) && (Status == FTC_SUCCESS));
|
894 |
|
|
|
895 |
|
|
return Status;
|
896 |
|
|
}
|
897 |
|
|
|
898 |
|
|
void FT2232c::FTC_ClearOutputBuffer(void)
|
899 |
|
|
{
|
900 |
|
|
dwNumBytesToSend = 0;
|
901 |
|
|
}
|
902 |
|
|
|
903 |
|
|
void FT2232c::FTC_AddByteToOutputBuffer(DWORD dwOutputByte, BOOL bClearOutputBuffer)
|
904 |
|
|
{
|
905 |
|
|
if (bClearOutputBuffer == TRUE)
|
906 |
|
|
dwNumBytesToSend = 0;
|
907 |
|
|
|
908 |
|
|
OutputBuffer[dwNumBytesToSend] = (dwOutputByte & '\xFF');
|
909 |
|
|
|
910 |
|
|
dwNumBytesToSend = dwNumBytesToSend + 1;
|
911 |
|
|
}
|
912 |
|
|
|
913 |
|
|
DWORD FT2232c::FTC_GetNumBytesInOutputBuffer(void)
|
914 |
|
|
{
|
915 |
|
|
return dwNumBytesToSend;
|
916 |
|
|
}
|
917 |
|
|
|
918 |
|
|
FTC_STATUS FT2232c::FTC_SendBytesToDevice(FTC_HANDLE ftHandle)
|
919 |
|
|
{
|
920 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
921 |
|
|
DWORD dwNumDataBytesToSend = 0;
|
922 |
|
|
DWORD dwNumBytesSent = 0;
|
923 |
|
|
DWORD dwTotalNumBytesSent = 0;
|
924 |
|
|
|
925 |
|
|
if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE)
|
926 |
|
|
{
|
927 |
|
|
do
|
928 |
|
|
{
|
929 |
|
|
// 25/08/05 - Can only use 4096 byte block as Windows 2000 Professional does not allow you to alter the USB buffer size
|
930 |
|
|
// 25/08/05 - Windows 2000 Professional always sets the USB buffer size to 4K ie 4096
|
931 |
|
|
if ((dwTotalNumBytesSent + MAX_NUM_BYTES_USB_WRITE) <= dwNumBytesToSend)
|
932 |
|
|
dwNumDataBytesToSend = MAX_NUM_BYTES_USB_WRITE;
|
933 |
|
|
else
|
934 |
|
|
dwNumDataBytesToSend = (dwNumBytesToSend - dwTotalNumBytesSent);
|
935 |
|
|
|
936 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
937 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
938 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
939 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwTotalNumBytesSent], dwNumDataBytesToSend, &dwNumBytesSent);
|
940 |
|
|
|
941 |
|
|
dwTotalNumBytesSent = dwTotalNumBytesSent + dwNumBytesSent;
|
942 |
|
|
}
|
943 |
|
|
while ((dwTotalNumBytesSent < dwNumBytesToSend) && (Status == FTC_SUCCESS));
|
944 |
|
|
}
|
945 |
|
|
else
|
946 |
|
|
{
|
947 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
948 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
949 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
950 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
|
951 |
|
|
}
|
952 |
|
|
|
953 |
|
|
|
954 |
|
|
dwNumBytesToSend = 0;
|
955 |
|
|
|
956 |
|
|
return Status;
|
957 |
|
|
}
|
958 |
|
|
|
959 |
|
|
FTC_STATUS FT2232c::FTC_ReadBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
|
960 |
|
|
DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
|
961 |
|
|
{
|
962 |
|
|
// This function reads data from a FT2232C dual type device. The dwNumBytesToRead variable specifies the maximum
|
963 |
|
|
// number of bytes to be read from a FT2232C dual type device. The lpdwNumBytesRead variable contains the actual
|
964 |
|
|
// number of bytes read from a FT2232C dual type device, which may range from zero to the actual number of bytes
|
965 |
|
|
// requested, depending on how many have been received at the time of the request + the read timeout value.
|
966 |
|
|
// The bytes read from a FT2232C dual type device, will be returned in the input buffer.
|
967 |
|
|
return FT_Read((FT_HANDLE)ftHandle, InputBuffer, dwNumBytesToRead, lpdwNumBytesRead);
|
968 |
|
|
}
|
969 |
|
|
|
970 |
|
|
FTC_STATUS FT2232c::FTC_ReadFixedNumBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
|
971 |
|
|
DWORD dwNumBytesToRead, LPDWORD lpdwNumDataBytesRead)
|
972 |
|
|
{
|
973 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
974 |
|
|
DWORD dwNumBytesDeviceInputBuffer = 0;
|
975 |
|
|
InputByteBuffer TmpInputByteBuffer;
|
976 |
|
|
DWORD dwNumBytesRead = 0;
|
977 |
|
|
DWORD dwBytesReadIndex = 0;
|
978 |
|
|
|
979 |
|
|
do
|
980 |
|
|
{
|
981 |
|
|
Status = FTC_GetNumberBytesFromDeviceInputBuffer(ftHandle, &dwNumBytesDeviceInputBuffer);
|
982 |
|
|
|
983 |
|
|
if ((Status == FTC_SUCCESS) && (dwNumBytesDeviceInputBuffer > 0))
|
984 |
|
|
{
|
985 |
|
|
Status = FTC_ReadBytesFromDevice(ftHandle, &TmpInputByteBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
|
986 |
|
|
|
987 |
|
|
if (Status == FTC_SUCCESS)
|
988 |
|
|
{
|
989 |
|
|
for (dwBytesReadIndex = 0 ; dwBytesReadIndex < dwNumBytesRead; dwBytesReadIndex++)
|
990 |
|
|
{
|
991 |
|
|
(*InputBuffer)[*lpdwNumDataBytesRead] = TmpInputByteBuffer[dwBytesReadIndex];
|
992 |
|
|
*lpdwNumDataBytesRead = (*lpdwNumDataBytesRead + 1);
|
993 |
|
|
}
|
994 |
|
|
}
|
995 |
|
|
}
|
996 |
|
|
}
|
997 |
|
|
while ((*lpdwNumDataBytesRead < dwNumBytesToRead) && (Status == FTC_SUCCESS));
|
998 |
|
|
|
999 |
|
|
return Status;
|
1000 |
|
|
}
|
1001 |
|
|
|
1002 |
|
|
FTC_STATUS FT2232c::FTC_SendReadBytesToFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
|
1003 |
|
|
DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
|
1004 |
|
|
{
|
1005 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
1006 |
|
|
DWORD dwNumBytesSent = 0;
|
1007 |
|
|
DWORD dwNumControlSendBytes = 0;
|
1008 |
|
|
DWORD dwNumDataBytesRead = 0;
|
1009 |
|
|
|
1010 |
|
|
if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE_READ)
|
1011 |
|
|
{
|
1012 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
1013 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
1014 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
1015 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, MAX_NUM_BYTES_USB_WRITE_READ, &dwNumBytesSent);
|
1016 |
|
|
|
1017 |
|
|
if (Status == FTC_SUCCESS)
|
1018 |
|
|
{
|
1019 |
|
|
dwNumControlSendBytes = (dwNumBytesToSend - dwNumBytesToRead);
|
1020 |
|
|
|
1021 |
|
|
Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, (MAX_NUM_BYTES_USB_WRITE_READ - dwNumControlSendBytes), &dwNumDataBytesRead);
|
1022 |
|
|
|
1023 |
|
|
if (Status == FTC_SUCCESS)
|
1024 |
|
|
{
|
1025 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwNumBytesSent], (dwNumBytesToSend - dwNumBytesSent), &dwNumBytesSent);
|
1026 |
|
|
|
1027 |
|
|
if (Status == FTC_SUCCESS)
|
1028 |
|
|
{
|
1029 |
|
|
Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, (dwNumBytesToRead - dwNumDataBytesRead), &dwNumDataBytesRead);
|
1030 |
|
|
|
1031 |
|
|
if (Status == FTC_SUCCESS)
|
1032 |
|
|
*lpdwNumBytesRead = dwNumDataBytesRead;
|
1033 |
|
|
}
|
1034 |
|
|
}
|
1035 |
|
|
}
|
1036 |
|
|
}
|
1037 |
|
|
else
|
1038 |
|
|
{
|
1039 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
1040 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
1041 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
1042 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
|
1043 |
|
|
|
1044 |
|
|
if (Status == FTC_SUCCESS)
|
1045 |
|
|
{
|
1046 |
|
|
Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumBytesToRead, &dwNumDataBytesRead);
|
1047 |
|
|
|
1048 |
|
|
if (Status == FTC_SUCCESS)
|
1049 |
|
|
*lpdwNumBytesRead = dwNumDataBytesRead;
|
1050 |
|
|
}
|
1051 |
|
|
}
|
1052 |
|
|
|
1053 |
|
|
dwNumBytesToSend = 0;
|
1054 |
|
|
|
1055 |
|
|
return Status;
|
1056 |
|
|
}
|
1057 |
|
|
|
1058 |
|
|
FTC_STATUS FT2232c::FTC_SendCommandsSequenceToDevice(FTC_HANDLE ftHandle)
|
1059 |
|
|
{
|
1060 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
1061 |
|
|
DWORD dwNumDataBytesToSend = 0;
|
1062 |
|
|
DWORD dwNumBytesSent = 0;
|
1063 |
|
|
DWORD dwTotalNumBytesSent = 0;
|
1064 |
|
|
|
1065 |
|
|
if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE)
|
1066 |
|
|
{
|
1067 |
|
|
do
|
1068 |
|
|
{
|
1069 |
|
|
if ((dwTotalNumBytesSent + MAX_NUM_BYTES_USB_WRITE) <= dwNumBytesToSend)
|
1070 |
|
|
dwNumDataBytesToSend = MAX_NUM_BYTES_USB_WRITE;
|
1071 |
|
|
else
|
1072 |
|
|
dwNumDataBytesToSend = (dwNumBytesToSend - dwTotalNumBytesSent);
|
1073 |
|
|
|
1074 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
1075 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
1076 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
1077 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwTotalNumBytesSent], dwNumDataBytesToSend, &dwNumBytesSent);
|
1078 |
|
|
|
1079 |
|
|
dwTotalNumBytesSent = dwTotalNumBytesSent + dwNumBytesSent;
|
1080 |
|
|
}
|
1081 |
|
|
while ((dwTotalNumBytesSent < dwNumBytesToSend) && (Status == FTC_SUCCESS));
|
1082 |
|
|
}
|
1083 |
|
|
else
|
1084 |
|
|
{
|
1085 |
|
|
// This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
|
1086 |
|
|
// bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
|
1087 |
|
|
// the actual number of bytes sent to a FT2232C dual type device.
|
1088 |
|
|
Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
|
1089 |
|
|
}
|
1090 |
|
|
|
1091 |
|
|
dwNumBytesToSend = 0;
|
1092 |
|
|
|
1093 |
|
|
return Status;
|
1094 |
|
|
}
|
1095 |
|
|
|
1096 |
|
|
FTC_STATUS FT2232c::FTC_ReadCommandsSequenceBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
|
1097 |
|
|
DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
|
1098 |
|
|
{
|
1099 |
|
|
FTC_STATUS Status = FTC_SUCCESS;
|
1100 |
|
|
DWORD dwNumDataBytesToRead = 0;
|
1101 |
|
|
DWORD dwNumBytesRead = 0;
|
1102 |
|
|
DWORD dwTotalNumBytesRead = 0;
|
1103 |
|
|
|
1104 |
|
|
if (dwNumBytesToRead > MAX_NUM_BYTES_USB_READ)
|
1105 |
|
|
{
|
1106 |
|
|
do
|
1107 |
|
|
{
|
1108 |
|
|
if ((dwTotalNumBytesRead + MAX_NUM_BYTES_USB_WRITE_READ) <= dwNumBytesToRead)
|
1109 |
|
|
dwNumDataBytesToRead = MAX_NUM_BYTES_USB_WRITE_READ;
|
1110 |
|
|
else
|
1111 |
|
|
dwNumDataBytesToRead = (dwNumBytesToRead - dwTotalNumBytesRead);
|
1112 |
|
|
|
1113 |
|
|
Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumDataBytesToRead, &dwNumBytesRead);
|
1114 |
|
|
|
1115 |
|
|
dwTotalNumBytesRead = dwTotalNumBytesRead + dwNumBytesRead;
|
1116 |
|
|
}
|
1117 |
|
|
while ((dwTotalNumBytesRead < dwNumBytesToRead) && (Status == FTC_SUCCESS));
|
1118 |
|
|
|
1119 |
|
|
*lpdwNumBytesRead = dwTotalNumBytesRead;
|
1120 |
|
|
}
|
1121 |
|
|
else
|
1122 |
|
|
Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumBytesToRead, lpdwNumBytesRead);
|
1123 |
|
|
|
1124 |
|
|
return Status;
|
1125 |
|
|
}
|
1126 |
|
|
|
1127 |
|
|
|
1128 |
|
|
// Define strupr and strlwr - not included in standard gcc libraries
|
1129 |
|
|
|
1130 |
|
|
|
1131 |
|
|
//#if defined(__cplusplus) && __cplusplus
|
1132 |
|
|
// extern "C" {
|
1133 |
|
|
//#endif
|
1134 |
|
|
|
1135 |
|
|
char* strupr(char *string)
|
1136 |
|
|
{
|
1137 |
|
|
char *s;
|
1138 |
|
|
|
1139 |
|
|
if (string)
|
1140 |
|
|
{
|
1141 |
|
|
for (s = string; *s; ++s)
|
1142 |
|
|
*s = toupper(*s);
|
1143 |
|
|
}
|
1144 |
|
|
return string;
|
1145 |
|
|
}
|
1146 |
|
|
|
1147 |
|
|
char* strlwr(char *string)
|
1148 |
|
|
{
|
1149 |
|
|
char *s;
|
1150 |
|
|
|
1151 |
|
|
if (string)
|
1152 |
|
|
{
|
1153 |
|
|
for (s = string; *s; ++s)
|
1154 |
|
|
*s = tolower(*s);
|
1155 |
|
|
}
|
1156 |
|
|
return string;
|
1157 |
|
|
}
|
1158 |
|
|
|
1159 |
|
|
//#if defined(__cplusplus) && __cplusplus
|
1160 |
|
|
//}
|
1161 |
|
|
//#endif
|