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

Subversion Repositories openrisc

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

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
          Status = FT_OpenEx((PVOID)lpDeviceName, FT_OPEN_BY_DESCRIPTION, &ftHandle);
450
          // Linux das not return the LocationID
451
          // Status = FT_OpenEx((PVOID)dwLocationID, FT_OPEN_BY_LOCATION, &ftHandle);
452
 
453
          if (Status == FTC_SUCCESS)
454
          {
455
            *pftHandle = (DWORD)ftHandle;
456
 
457
            FTC_InsertDeviceHandle(lpDeviceName, dwLocationID, *pftHandle);
458
          }
459
 
460
        }
461
 
462
      }
463
      else
464
        Status = FTC_DEVICE_IN_USE;
465
    }
466
  }
467
  else
468
    Status = FTC_NULL_DEVICE_NAME_BUFFER_POINTER;
469
 
470
  return Status;
471
}
472
 
473
FTC_STATUS FT2232c::FTC_OpenDevice(FTC_HANDLE *pftHandle)
474
{
475
  FTC_STATUS Status = FTC_SUCCESS;
476
  DWORD dwNumDevices = 0;
477
  FT2232CDeviceIndexes FT2232CIndexes;
478
  char szDeviceName[DEVICE_STRING_BUFF_SIZE + 1];
479
  DWORD dwLocationID;
480
 
481
  Status = FTC_GetNumDevices(&dwNumDevices, &FT2232CIndexes);
482
 
483
  if (Status == FTC_SUCCESS)
484
  {
485
    if (dwNumDevices == 1)
486
    {
487
      Status = FTC_GetDeviceNameLocationID(FT2232CIndexes[0], szDeviceName, (DEVICE_STRING_BUFF_SIZE + 1), &dwLocationID);
488
 
489
      if (Status == FTC_SUCCESS)
490
        Status = FTC_OpenSpecifiedDevice(szDeviceName, dwLocationID, pftHandle);
491
    }
492
    else
493
    {
494
      if (dwNumDevices == 0)
495
        Status = FTC_DEVICE_NOT_FOUND;
496
      else
497
        Status = FTC_TOO_MANY_DEVICES;
498
    }
499
  }
500
 
501
  return Status;
502
}
503
 
504
FTC_STATUS FT2232c::FTC_CloseDevice(FTC_HANDLE ftHandle)
505
{
506
  FTC_STATUS Status = FTC_SUCCESS;
507
 
508
  Status = FTC_IsDeviceHandleValid(ftHandle);
509
 
510
  if (Status == FTC_SUCCESS)
511
  {
512
    Status = FT_Close((FT_HANDLE)ftHandle);
513
 
514
    FTC_RemoveDeviceHandle(ftHandle);
515
  }
516
 
517
  return Status;
518
}
519
 
520
void FT2232c::FTC_GetClockFrequencyValues(DWORD dwClockFrequencyValue, LPDWORD lpdwClockFrequencyHz)
521
{
522
  *lpdwClockFrequencyHz = (BASE_CLOCK_FREQUENCY_12_MHZ / ((1 + dwClockFrequencyValue) * 2));
523
}
524
 
525
FTC_STATUS FT2232c::FTC_SetDeviceLoopbackState(FTC_HANDLE ftHandle, BOOL bLoopbackState)
526
{
527
  FTC_STATUS Status = FTC_SUCCESS;
528
 
529
  Status = FTC_IsDeviceHandleValid(ftHandle);
530
 
531
  if (Status == FTC_SUCCESS)
532
  {
533
    if (bLoopbackState == false)
534
      // turn off loopback
535
      FTC_AddByteToOutputBuffer(TURN_OFF_LOOPBACK_CMD, true);
536
    else
537
      // turn on loopback
538
      FTC_AddByteToOutputBuffer(TURN_ON_LOOPBACK_CMD, true);
539
 
540
    FTC_SendBytesToDevice(ftHandle);
541
  }
542
 
543
  return Status;
544
}
545
 
546
void FT2232c::FTC_InsertDeviceHandle(LPSTR lpDeviceName, DWORD dwLocationID, FTC_HANDLE ftHandle)
547
{
548
  DWORD dwProcessId = 0;
549
  INT iDeviceCntr = 0;
550
  BOOLEAN bDeviceftHandleInserted = false;
551
 
552
  if (uiNumOpenedDevices < MAX_NUM_DEVICES)
553
  {
554
    //dwProcessId = GetCurrentProcessId();
555
    dwProcessId = getpid(); //Changed windows call to linux\s getpid()
556
 
557
    for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDeviceftHandleInserted); iDeviceCntr++)
558
    {
559
      if (OpenedDevices[iDeviceCntr].dwProcessId == 0)
560
      {
561
        OpenedDevices[iDeviceCntr].dwProcessId = dwProcessId;
562
        strcpy(OpenedDevices[iDeviceCntr].szDeviceName, lpDeviceName);
563
        OpenedDevices[iDeviceCntr].dwLocationID = dwLocationID;
564
        OpenedDevices[iDeviceCntr].hDevice = ftHandle;
565
 
566
        uiNumOpenedDevices = uiNumOpenedDevices + 1;
567
 
568
        bDeviceftHandleInserted = true;
569
      }
570
    }
571
  }
572
}
573
 
574
FTC_STATUS FT2232c::FTC_IsDeviceHandleValid(FTC_HANDLE ftHandle)
575
{
576
  FTC_STATUS Status = FTC_SUCCESS;
577
  DWORD dwProcessId = 0;
578
  INT iDeviceCntr = 0;
579
  BOOLEAN bDevicempHandleFound = false;
580
 
581
  if ((uiNumOpenedDevices > 0) && (ftHandle > 0))
582
  {
583
    //dwProcessId = GetCurrentProcessId();
584
    dwProcessId = getpid(); //Changed windows call to linux\s getpid()
585
 
586
    for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDevicempHandleFound); iDeviceCntr++)
587
    {
588
      if (OpenedDevices[iDeviceCntr].dwProcessId == dwProcessId)
589
      {
590
        if (OpenedDevices[iDeviceCntr].hDevice == ftHandle)
591
          bDevicempHandleFound = true;
592
      }
593
    }
594
 
595
    if (!bDevicempHandleFound)
596
      Status = FTC_INVALID_HANDLE;
597
  }
598
  else
599
    Status = FTC_INVALID_HANDLE;
600
 
601
  return Status;
602
}
603
 
604
 
605
void FT2232c::FTC_RemoveDeviceHandle(FTC_HANDLE ftHandle)
606
{
607
  DWORD dwProcessId = 0;
608
  INT iDeviceCntr = 0;
609
  BOOLEAN bDevicempHandleFound = false;
610
 
611
  if (uiNumOpenedDevices > 0)
612
  {
613
    //dwProcessId = GetCurrentProcessId();
614
    dwProcessId = getpid(); //Changed windows call to linux\s getpid()
615
 
616
    for (iDeviceCntr = 0; ((iDeviceCntr < MAX_NUM_DEVICES) && !bDevicempHandleFound); iDeviceCntr++)
617
    {
618
      if (OpenedDevices[iDeviceCntr].dwProcessId == dwProcessId)
619
      {
620
        if (OpenedDevices[iDeviceCntr].hDevice == ftHandle)
621
        {
622
          OpenedDevices[iDeviceCntr].dwProcessId = 0;
623
          strcpy(OpenedDevices[iDeviceCntr].szDeviceName, "");
624
          OpenedDevices[iDeviceCntr].dwLocationID = 0;
625
          OpenedDevices[iDeviceCntr].hDevice = 0;
626
 
627
          uiNumOpenedDevices = uiNumOpenedDevices - 1;
628
 
629
          bDevicempHandleFound = true;
630
        }
631
      }
632
    }
633
  }
634
}
635
 
636
FTC_STATUS FT2232c::FTC_ResetUSBDevicePurgeUSBInputBuffer(FTC_HANDLE ftHandle)
637
{
638
  FTC_STATUS Status = FTC_SUCCESS;
639
  InputByteBuffer InputBuffer;
640
  DWORD dwNumBytesRead = 0;
641
  DWORD dwNumBytesDeviceInputBuffer;
642
 
643
  Status = FT_ResetDevice((FT_HANDLE)ftHandle);
644
 
645
  if (Status == FTC_SUCCESS)
646
  {
647
    // Get the number of bytes in the device input buffer
648
    Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
649
 
650
    if (Status == FTC_SUCCESS)
651
    {
652
      if (dwNumBytesDeviceInputBuffer > 0)
653
        FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
654
    }
655
  }
656
 
657
  return Status;
658
}
659
 
660
FTC_STATUS FT2232c::FTC_SetDeviceUSBBufferSizes(FTC_HANDLE ftHandle, DWORD InputBufferSize, DWORD OutputBufferSize)
661
{
662
  return FT_SetUSBParameters((FT_HANDLE)ftHandle, InputBufferSize, OutputBufferSize);
663
}
664
 
665
FTC_STATUS FT2232c::FTC_SetDeviceSpecialCharacters(FTC_HANDLE ftHandle, BOOLEAN bEventEnabled, UCHAR EventCharacter,
666
                                                   BOOLEAN bErrorEnabled, UCHAR ErrorCharacter)
667
{
668
        UCHAR EventCharEnabled = UCHAR(bEventEnabled);
669
        UCHAR ErrorCharEnabled = UCHAR(bErrorEnabled);
670
 
671
  // Set the special characters for the device. disable event and error characters
672
  return FT_SetChars((FT_HANDLE)ftHandle, EventCharacter, EventCharEnabled, ErrorCharacter, ErrorCharEnabled);
673
}
674
 
675
FTC_STATUS FT2232c::FTC_SetReadWriteDeviceTimeouts(FTC_HANDLE ftHandle, DWORD dwReadTimeoutmSec, DWORD dwWriteTimeoutmSec)
676
{
677
  // Sets the read and write timeouts in milli-seconds for the device
678
  return FT_SetTimeouts((FT_HANDLE)ftHandle, dwReadTimeoutmSec, dwWriteTimeoutmSec);
679
}
680
 
681
FTC_STATUS FT2232c::FTC_SetDeviceLatencyTimer(FTC_HANDLE ftHandle, BYTE LatencyTimermSec)
682
{
683
  // Set the device latency timer to a number of milliseconds
684
  return FT_SetLatencyTimer((FT_HANDLE)ftHandle, LatencyTimermSec);
685
}
686
 
687
FTC_STATUS FT2232c::FTC_ResetMPSSEInterface(FTC_HANDLE ftHandle)
688
{
689
  return FT_SetBitMode((FT_HANDLE)ftHandle, MPSSE_INTERFACE_MASK, RESET_MPSSE_INTERFACE);
690
}
691
 
692
FTC_STATUS FT2232c::FTC_EnableMPSSEInterface(FTC_HANDLE ftHandle)
693
{
694
  return FT_SetBitMode((FT_HANDLE)ftHandle, MPSSE_INTERFACE_MASK, ENABLE_MPSSE_INTERFACE);
695
}
696
 
697
FTC_STATUS FT2232c::FTC_SendReceiveCommandFromMPSSEInterface(FTC_HANDLE ftHandle, BOOLEAN bSendEchoCommandContinuouslyOnce, BYTE EchoCommand, LPBOOL lpbCommandEchod)
698
{
699
  FTC_STATUS Status = FTC_SUCCESS;
700
  DWORD dwNumBytesDeviceInputBuffer = 0;
701
  //SYSTEMTIME StartTime;
702
  InputByteBuffer InputBuffer;
703
  DWORD dwNumBytesRead = 0;
704
  DWORD dwByteCntr = 0;
705
  BOOL bBadCommandResponse = false;
706
 
707
  // New timeout detection variables for linux build -- Julius
708
  struct timeval tv;
709
  time_t time_start; // to store number of seconds since unix epoch
710
 
711
 
712
  *lpbCommandEchod = false;
713
 
714
  if (!bSendEchoCommandContinuouslyOnce)
715
  {
716
    // Causes the device to echo back the command character and wait in command mode
717
    FTC_AddByteToOutputBuffer(EchoCommand, true);
718
    FTC_SendBytesToDevice(ftHandle);
719
  }
720
 
721
  // Windows only time functions - removed for linux library build -- Julius
722
  //GetLocalTime(&StartTime);
723
  gettimeofday(&tv, NULL);
724
  time_start = tv.tv_sec;
725
 
726
  do
727
  {
728
    // Send the echo command every time round the loop
729
    if (bSendEchoCommandContinuouslyOnce)
730
    {
731
      // Causes the device to echo back the command character and wait in command mode
732
      FTC_AddByteToOutputBuffer(EchoCommand, true);
733
      FTC_SendBytesToDevice(ftHandle);
734
    }
735
 
736
    // Get the number of bytes in the device input buffer
737
    Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
738
 
739
    if (Status == FTC_SUCCESS)
740
    {
741
      //Sleep(0);  // give up timeslice - doesn't work this way in linux, instead do a do a sched_yield():
742
      sched_yield();
743
      // This basically checks if the time we took from the first GetLocalTime was bigger than
744
      // MAX_COMMAND_TIMEOUT_PERIOD
745
      //if (FTC_Timeout(StartTime, MAX_COMMAND_TIMEOUT_PERIOD))
746
      gettimeofday(&tv, NULL);
747
      if ((tv.tv_sec - time_start) > (MAX_COMMAND_TIMEOUT_PERIOD/1000))
748
        Status = FTC_FAILED_TO_COMPLETE_COMMAND;
749
 
750
 
751
 
752
    }
753
 
754
    if (Status == FTC_SUCCESS)
755
    {
756
      if (dwNumBytesDeviceInputBuffer > 0)
757
      {
758
        FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
759
 
760
        if (dwNumBytesRead > 0)
761
        {
762
                  dwByteCntr = 0;
763
 
764
          do
765
          {
766
                        if (dwByteCntr <= (dwNumBytesRead - 1))
767
            {
768
              if (InputBuffer[dwByteCntr] == BAD_COMMAND_RESPONSE)
769
                bBadCommandResponse = true;
770
              else
771
              {
772
                if (bBadCommandResponse == TRUE)
773
                {
774
                  if (InputBuffer[dwByteCntr] == EchoCommand)
775
                    *lpbCommandEchod = true;
776
                }
777
 
778
                bBadCommandResponse = false;
779
              }
780
            }
781
 
782
            dwByteCntr = dwByteCntr + 1;
783
          }
784
          while ((dwByteCntr < dwNumBytesRead) && (*lpbCommandEchod == false));
785
        }
786
      }
787
    }
788
  }
789
  while ((*lpbCommandEchod == false) && (Status == FTC_SUCCESS));
790
 
791
  return Status;
792
}
793
 
794
FTC_STATUS FT2232c::FTC_SynchronizeMPSSEInterface(FTC_HANDLE ftHandle)
795
{
796
  FTC_STATUS Status = FTC_SUCCESS;
797
  DWORD dwNumBytesDeviceInputBuffer = 0;
798
  InputByteBuffer InputBuffer;
799
  DWORD dwNumBytesRead = 0;
800
  BOOL bCommandEchod = false;
801
 
802
  // Get the number of bytes in the device input buffer
803
  Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, &dwNumBytesDeviceInputBuffer);
804
 
805
  if (Status == FTC_SUCCESS)
806
  {
807
    if (dwNumBytesDeviceInputBuffer > 0)
808
      FTC_ReadBytesFromDevice(ftHandle, &InputBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
809
 
810
    Status = FTC_SendReceiveCommandFromMPSSEInterface(ftHandle, TRUE, AA_ECHO_CMD_1, &bCommandEchod);
811
 
812
    if (Status == FTC_SUCCESS)
813
    {
814
      if (bCommandEchod == TRUE)
815
      {
816
        Status = FTC_SendReceiveCommandFromMPSSEInterface(ftHandle, FALSE, AB_ECHO_CMD_2, &bCommandEchod);
817
 
818
        if (Status == FTC_SUCCESS)
819
        {
820
          if (bCommandEchod == false)
821
            Status = FTC_FAILED_TO_SYNCHRONIZE_DEVICE_MPSSE;
822
        }
823
      }
824
      else
825
        Status = FTC_FAILED_TO_SYNCHRONIZE_DEVICE_MPSSE;
826
    }
827
  }
828
 
829
  return Status;
830
}
831
 
832
/*
833
BOOLEAN FT2232c::FTC_Timeout(SYSTEMTIME StartSystemTime, DWORD dwTimeoutmSecs)
834
{
835
  BOOLEAN bTimoutExpired = false;
836
  FILETIME StartFileTime;
837
  ULARGE_INTEGER StartTime;
838
  SYSTEMTIME EndSystemTime;
839
  FILETIME EndFileTime;
840
  ULARGE_INTEGER EndTime;
841
  ULONGLONG ulTimeoutmSecs = dwTimeoutmSecs * CONVERT_1MS_TO_100NS; //10000;
842
 
843
  GetLocalTime(&EndSystemTime);
844
 
845
  SystemTimeToFileTime(&StartSystemTime, &StartFileTime);
846
 
847
  StartTime.LowPart = StartFileTime.dwLowDateTime;
848
  StartTime.HighPart = StartFileTime.dwHighDateTime;
849
 
850
  SystemTimeToFileTime(&EndSystemTime, &EndFileTime);
851
 
852
  EndTime.LowPart = EndFileTime.dwLowDateTime;
853
  EndTime.HighPart = EndFileTime.dwHighDateTime;
854
 
855
  if ((EndTime.QuadPart - StartTime.QuadPart) > ulTimeoutmSecs)
856
    bTimoutExpired = true;
857
 
858
  return bTimoutExpired;
859
  }*/
860
 
861
FTC_STATUS FT2232c::FTC_GetNumberBytesFromDeviceInputBuffer(FTC_HANDLE ftHandle, LPDWORD lpdwNumBytesDeviceInputBuffer)
862
{
863
  FTC_STATUS Status = FTC_SUCCESS;
864
  //SYSTEMTIME StartTime;
865
 
866
  // New timeout detection variables for linux build -- Julius
867
  struct timeval tv;
868
  time_t time_start; // to store number of seconds since unix epoch
869
 
870
 
871
  // Windows only time functions - removed for linux library build -- Julius
872
  //GetLocalTime(&StartTime);
873
  gettimeofday(&tv, NULL);
874
  time_start = tv.tv_sec;
875
 
876
 
877
  do
878
  {
879
    // Get the number of bytes in the device input buffer
880
    Status = FT_GetQueueStatus((FT_HANDLE)ftHandle, lpdwNumBytesDeviceInputBuffer);
881
 
882
    if (Status == FTC_SUCCESS)
883
    {
884
      //Sleep(0);  // give up timeslice - doesn't work this way in linux, instead do a do a sched_yield():
885
      sched_yield();
886
      // This basically checks if the time we took from the first GetLocalTime was bigger than
887
      // MAX_COMMAND_TIMEOUT_PERIOD
888
      //if (FTC_Timeout(StartTime, MAX_COMMAND_TIMEOUT_PERIOD))
889
      gettimeofday(&tv, NULL);
890
      if ((tv.tv_sec - time_start) > (MAX_COMMAND_TIMEOUT_PERIOD / 1000))
891
        Status = FTC_FAILED_TO_COMPLETE_COMMAND;
892
    }
893
  }
894
  while ((*lpdwNumBytesDeviceInputBuffer == 0) && (Status == FTC_SUCCESS));
895
 
896
  return Status;
897
}
898
 
899
void FT2232c::FTC_ClearOutputBuffer(void)
900
{
901
  dwNumBytesToSend = 0;
902
}
903
 
904
void FT2232c::FTC_AddByteToOutputBuffer(DWORD dwOutputByte, BOOL bClearOutputBuffer)
905
{
906
  if (bClearOutputBuffer == TRUE)
907
    dwNumBytesToSend = 0;
908
 
909
  OutputBuffer[dwNumBytesToSend] = (dwOutputByte & '\xFF');
910
 
911
  dwNumBytesToSend = dwNumBytesToSend + 1;
912
}
913
 
914
DWORD FT2232c::FTC_GetNumBytesInOutputBuffer(void)
915
{
916
  return dwNumBytesToSend;
917
}
918
 
919
FTC_STATUS FT2232c::FTC_SendBytesToDevice(FTC_HANDLE ftHandle)
920
{
921
  FTC_STATUS Status = FTC_SUCCESS;
922
  DWORD dwNumDataBytesToSend = 0;
923
  DWORD dwNumBytesSent = 0;
924
  DWORD dwTotalNumBytesSent = 0;
925
 
926
  if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE)
927
  {
928
    do
929
    {
930
      // 25/08/05 - Can only use 4096 byte block as Windows 2000 Professional does not allow you to alter the USB buffer size
931
      // 25/08/05 - Windows 2000 Professional always sets the USB buffer size to 4K ie 4096
932
      if ((dwTotalNumBytesSent + MAX_NUM_BYTES_USB_WRITE) <= dwNumBytesToSend)
933
        dwNumDataBytesToSend = MAX_NUM_BYTES_USB_WRITE;
934
      else
935
        dwNumDataBytesToSend = (dwNumBytesToSend - dwTotalNumBytesSent);
936
 
937
      // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
938
      // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
939
      // the actual number of bytes sent to a FT2232C dual type device.
940
      Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwTotalNumBytesSent], dwNumDataBytesToSend, &dwNumBytesSent);
941
 
942
      dwTotalNumBytesSent = dwTotalNumBytesSent + dwNumBytesSent;
943
    }
944
    while ((dwTotalNumBytesSent < dwNumBytesToSend) && (Status == FTC_SUCCESS));
945
  }
946
  else
947
  {
948
    // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
949
    // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
950
    // the actual number of bytes sent to a FT2232C dual type device.
951
    Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
952
  }
953
 
954
 
955
  dwNumBytesToSend = 0;
956
 
957
  return Status;
958
}
959
 
960
FTC_STATUS FT2232c::FTC_ReadBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
961
                                            DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
962
{
963
  // This function reads data from a FT2232C dual type device. The dwNumBytesToRead variable specifies the maximum
964
  // number of bytes to be read from a FT2232C dual type device. The lpdwNumBytesRead variable contains the actual
965
  // number of bytes read from a FT2232C dual type device, which may range from zero to the actual number of bytes
966
  // requested, depending on how many have been received at the time of the request + the read timeout value.
967
  // The bytes read from a FT2232C dual type device, will be returned in the input buffer.
968
  return FT_Read((FT_HANDLE)ftHandle, InputBuffer, dwNumBytesToRead, lpdwNumBytesRead);
969
}
970
 
971
FTC_STATUS FT2232c::FTC_ReadFixedNumBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
972
                                                    DWORD dwNumBytesToRead, LPDWORD lpdwNumDataBytesRead)
973
{
974
  FTC_STATUS Status = FTC_SUCCESS;
975
  DWORD dwNumBytesDeviceInputBuffer = 0;
976
  InputByteBuffer TmpInputByteBuffer;
977
  DWORD dwNumBytesRead = 0;
978
  DWORD dwBytesReadIndex = 0;
979
 
980
  do
981
  {
982
    Status = FTC_GetNumberBytesFromDeviceInputBuffer(ftHandle, &dwNumBytesDeviceInputBuffer);
983
 
984
    if ((Status == FTC_SUCCESS) && (dwNumBytesDeviceInputBuffer > 0))
985
    {
986
      Status = FTC_ReadBytesFromDevice(ftHandle, &TmpInputByteBuffer, dwNumBytesDeviceInputBuffer, &dwNumBytesRead);
987
 
988
      if (Status == FTC_SUCCESS)
989
      {
990
        for (dwBytesReadIndex = 0 ; dwBytesReadIndex < dwNumBytesRead; dwBytesReadIndex++)
991
        {
992
          (*InputBuffer)[*lpdwNumDataBytesRead] = TmpInputByteBuffer[dwBytesReadIndex];
993
          *lpdwNumDataBytesRead = (*lpdwNumDataBytesRead + 1);
994
        }
995
      }
996
    }
997
  }
998
  while ((*lpdwNumDataBytesRead < dwNumBytesToRead) && (Status == FTC_SUCCESS));
999
 
1000
  return Status;
1001
}
1002
 
1003
FTC_STATUS FT2232c::FTC_SendReadBytesToFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
1004
                                                  DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
1005
{
1006
  FTC_STATUS Status = FTC_SUCCESS;
1007
  DWORD dwNumBytesSent = 0;
1008
  DWORD dwNumControlSendBytes = 0;
1009
  DWORD dwNumDataBytesRead = 0;
1010
 
1011
  if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE_READ)
1012
  {
1013
    // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
1014
    // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
1015
    // the actual number of bytes sent to a FT2232C dual type device.
1016
    Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, MAX_NUM_BYTES_USB_WRITE_READ, &dwNumBytesSent);
1017
 
1018
    if (Status == FTC_SUCCESS)
1019
    {
1020
      dwNumControlSendBytes = (dwNumBytesToSend - dwNumBytesToRead);
1021
 
1022
      Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, (MAX_NUM_BYTES_USB_WRITE_READ - dwNumControlSendBytes), &dwNumDataBytesRead);
1023
 
1024
      if (Status == FTC_SUCCESS)
1025
      {
1026
        Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwNumBytesSent], (dwNumBytesToSend - dwNumBytesSent), &dwNumBytesSent);
1027
 
1028
        if (Status == FTC_SUCCESS)
1029
        {
1030
          Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, (dwNumBytesToRead - dwNumDataBytesRead), &dwNumDataBytesRead);
1031
 
1032
          if (Status == FTC_SUCCESS)
1033
            *lpdwNumBytesRead = dwNumDataBytesRead;
1034
        }
1035
      }
1036
    }
1037
  }
1038
  else
1039
  {
1040
    // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
1041
    // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
1042
    // the actual number of bytes sent to a FT2232C dual type device.
1043
    Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
1044
 
1045
    if (Status == FTC_SUCCESS)
1046
    {
1047
      Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumBytesToRead, &dwNumDataBytesRead);
1048
 
1049
      if (Status == FTC_SUCCESS)
1050
        *lpdwNumBytesRead = dwNumDataBytesRead;
1051
    }
1052
  }
1053
 
1054
  dwNumBytesToSend = 0;
1055
 
1056
  return Status;
1057
}
1058
 
1059
FTC_STATUS FT2232c::FTC_SendCommandsSequenceToDevice(FTC_HANDLE ftHandle)
1060
{
1061
  FTC_STATUS Status = FTC_SUCCESS;
1062
  DWORD dwNumDataBytesToSend = 0;
1063
  DWORD dwNumBytesSent = 0;
1064
  DWORD dwTotalNumBytesSent = 0;
1065
 
1066
  if (dwNumBytesToSend > MAX_NUM_BYTES_USB_WRITE)
1067
  {
1068
    do
1069
    {
1070
      if ((dwTotalNumBytesSent + MAX_NUM_BYTES_USB_WRITE) <= dwNumBytesToSend)
1071
        dwNumDataBytesToSend = MAX_NUM_BYTES_USB_WRITE;
1072
      else
1073
        dwNumDataBytesToSend = (dwNumBytesToSend - dwTotalNumBytesSent);
1074
 
1075
      // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
1076
      // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
1077
      // the actual number of bytes sent to a FT2232C dual type device.
1078
      Status = FT_Write((FT_HANDLE)ftHandle, &OutputBuffer[dwTotalNumBytesSent], dwNumDataBytesToSend, &dwNumBytesSent);
1079
 
1080
      dwTotalNumBytesSent = dwTotalNumBytesSent + dwNumBytesSent;
1081
    }
1082
    while ((dwTotalNumBytesSent < dwNumBytesToSend) && (Status == FTC_SUCCESS));
1083
  }
1084
  else
1085
  {
1086
    // This function sends data to a FT2232C dual type device. The dwNumBytesToSend variable specifies the number of
1087
    // bytes in the output buffer to be sent to a FT2232C dual type device. The dwNumBytesSent variable contains
1088
    // the actual number of bytes sent to a FT2232C dual type device.
1089
    Status = FT_Write((FT_HANDLE)ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
1090
  }
1091
 
1092
  dwNumBytesToSend = 0;
1093
 
1094
  return Status;
1095
}
1096
 
1097
FTC_STATUS FT2232c::FTC_ReadCommandsSequenceBytesFromDevice(FTC_HANDLE ftHandle, PInputByteBuffer InputBuffer,
1098
                                                            DWORD dwNumBytesToRead, LPDWORD lpdwNumBytesRead)
1099
{
1100
  FTC_STATUS Status = FTC_SUCCESS;
1101
  DWORD dwNumDataBytesToRead = 0;
1102
  DWORD dwNumBytesRead = 0;
1103
  DWORD dwTotalNumBytesRead = 0;
1104
 
1105
  if (dwNumBytesToRead > MAX_NUM_BYTES_USB_READ)
1106
  {
1107
    do
1108
    {
1109
      if ((dwTotalNumBytesRead + MAX_NUM_BYTES_USB_WRITE_READ) <= dwNumBytesToRead)
1110
        dwNumDataBytesToRead = MAX_NUM_BYTES_USB_WRITE_READ;
1111
      else
1112
        dwNumDataBytesToRead = (dwNumBytesToRead - dwTotalNumBytesRead);
1113
 
1114
      Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumDataBytesToRead, &dwNumBytesRead);
1115
 
1116
      dwTotalNumBytesRead = dwTotalNumBytesRead + dwNumBytesRead;
1117
    }
1118
    while ((dwTotalNumBytesRead < dwNumBytesToRead) && (Status == FTC_SUCCESS));
1119
 
1120
    *lpdwNumBytesRead = dwTotalNumBytesRead;
1121
  }
1122
  else
1123
    Status = FTC_ReadFixedNumBytesFromDevice(ftHandle, InputBuffer, dwNumBytesToRead, lpdwNumBytesRead);
1124
 
1125
  return Status;
1126
}
1127
 
1128
 
1129
// Define strupr and strlwr - not included in standard gcc libraries
1130
 
1131
 
1132
//#if defined(__cplusplus) && __cplusplus
1133
// extern "C" {
1134
//#endif
1135
 
1136
char* strupr(char *string)
1137
{
1138
      char *s;
1139
 
1140
      if (string)
1141
      {
1142
            for (s = string; *s; ++s)
1143
                  *s = toupper(*s);
1144
      }
1145
      return string;
1146
}
1147
 
1148
char* strlwr(char *string)
1149
{
1150
      char *s;
1151
 
1152
      if (string)
1153
      {
1154
            for (s = string; *s; ++s)
1155
                  *s = tolower(*s);
1156
      }
1157
      return string;
1158
}
1159
 
1160
//#if defined(__cplusplus) && __cplusplus
1161
//}
1162
//#endif

powered by: WebSVN 2.1.0

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