OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [FT2232c.cpp] - Blame information for rev 646

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.