OpenCores
URL https://opencores.org/ocsvn/usb_ft232h_avalon-mm_interface/usb_ft232h_avalon-mm_interface/trunk

Subversion Repositories usb_ft232h_avalon-mm_interface

[/] [usb_ft232h_avalon-mm_interface/] [trunk/] [testbench/] [desktop/] [main.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 melman701
#include <iostream>
2
#include <fstream>
3
#include <types.h>
4
#include <malloc.h>
5
#include <sysinfoapi.h>
6
#include "ftd2xx.h"
7
 
8
#define MS_DEV_TYPE     8
9
#define MS_DEV_VID      0x0403
10
#define MS_DEV_PID      0x75D8
11
#define MS_DEV_ID       ((MS_DEV_VID <<16) | MS_DEV_PID)
12
 
13
#define SEND_SIZE       524288
14
#define GET_SIZE        524288
15
 
16
using namespace std;
17
 
18
int main()
19
{
20
    cout << "Hello World!" << endl;
21
 
22
    FT_HANDLE ftHandle;
23
    FT_STATUS ftStatus;
24
    FT_DEVICE_LIST_INFO_NODE* ftDevInfo;
25
    unsigned long dcount;
26
    unsigned char mask=0xff, mode=0x0, latency=16;
27
 
28
    ftStatus = FT_CreateDeviceInfoList(&dcount);
29
    ftDevInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*dcount);
30
    FT_GetDeviceInfoList(ftDevInfo, &dcount);
31
 
32
    for(int i=0; i<dcount; i++) {
33
        if((ftDevInfo[i].Type == MS_DEV_TYPE) && (ftDevInfo[i].ID == MS_DEV_ID)) {
34
//            printf("Open device\n");
35
            cout << "Open device" << endl;
36
            ftStatus = FT_Open (i, &ftHandle);
37
            if (!FT_SUCCESS(ftStatus))
38
            {
39
//                printf("Unable to open USB device\n");
40
                cout << "Unable to open USB device" << endl;
41
                return 0;
42
            }
43
            break;
44
        }
45
    }
46
    if(ftHandle == NULL) {
47
//        printf("No device found\n");
48
        cout << "No device found" << endl;
49
        return 0;
50
    }
51
 
52
    mode = 0x0; //reset mode
53
    ftStatus = FT_SetBitMode(ftHandle, mask, mode);
54
    Sleep(1000);
55
    mode = 0x40;
56
    ftStatus = FT_SetBitMode(ftHandle, mask, mode);
57
    if(ftStatus != FT_OK) {
58
        printf("Set bit mode error %d\n", ftStatus);
59
        return -2;
60
    }
61
    FT_SetLatencyTimer(ftHandle, 2);
62
    FT_SetUSBParameters(ftHandle, 0x10000, 0x10000);
63
    FT_SetFlowControl(ftHandle, FT_FLOW_RTS_CTS, 0x0, 0x0);
64
    FT_Purge(ftHandle, /*FT_PURGE_RX |*/ FT_PURGE_TX);
65
 
66
    uint8_t rxbuff[524288];
67
    uint32_t rxbuffCount = 0;
68
    uint8_t txbuff[40000];
69
 
70
    for(int i=0; i<sizeof(txbuff); i++)
71
        txbuff[i] = i & 0xFF;
72
 
73
    unsigned long getSize, sendSize;
74
    unsigned long event, rxCount, txCount;
75
    unsigned long bytesWrite, bytesRead;
76
 
77
    unsigned long start, last, current, prev;
78
 
79
    getSize = 0;
80
    sendSize = 0;
81
    event = 0;
82
    rxCount = 0;
83
    txCount = 0;
84
    bytesWrite = 0;
85
    bytesRead = 0;
86
 
87
    ofstream rxfile;
88
    rxfile.open("e:\\rxlog.txt", ios_base::out | ios_base::binary | ios_base::trunc);
89
    if(!rxfile.is_open()) {
90
        cout << "Cant open file" << endl;
91
        return -1;
92
    }
93
 
94
    start = GetTickCount();
95
    while((sendSize < SEND_SIZE) || (getSize < GET_SIZE)) {
96
        cout << getSize << " - " << sendSize << endl;
97
        if(FT_GetStatus(ftHandle, &rxCount, &txCount, &event) == FT_OK) {
98
            if(sendSize < SEND_SIZE) {
99
                if(FT_Write(ftHandle, txbuff, 4096, &bytesWrite) == FT_OK) {
100
                    cout << "Send OK " << bytesWrite << endl;
101
                    sendSize += bytesWrite;
102
                } else {
103
                    cout << "Write error " << endl;
104
//                    return -2;
105
                }
106
            }
107
//            if((getSize < GET_SIZE) || (rxCount > 0)) {
108
                if(rxCount) {
109
                    if (FT_Read (ftHandle, rxbuff, rxCount, &bytesRead) == FT_OK) {
110
                        if(bytesRead > 0) {
111
                            cout << "Read OK " << bytesRead << " " << txCount << endl;
112
                            getSize += bytesRead;
113
                            for(int i=0; i<bytesRead; i++)
114
                                rxfile << rxbuff[i];
115
                        }
116
                    } else {
117
                        cout << "read error" << endl;
118
                    }
119
                }
120
//            }
121
        } else {
122
            cout << "get dstatus error" << endl;
123
        }
124
 
125
        current = GetTickCount();
126
        if((current - prev) >= 500) {
127
            prev = current;
128
            cout << "Alive " << getSize << "(" << rxCount << ")\t " << sendSize << "(" << txCount << ")" << endl;
129
            rxfile.flush();
130
        }
131
    }
132
 
133
    /*do {
134
        FT_GetStatus(ftHandle, &rxCount, &txCount, &event);
135
    } while(rxCount || txCount);*/
136
 
137
    cout << "Complete" << endl;
138
    FT_GetStatus(ftHandle, &rxCount, &txCount, &event);
139
     cout << "Alive " << getSize << "(" << rxCount << ")\t " << sendSize << "(" << txCount << ")" << endl;
140
    last = GetTickCount();
141
 
142
    double kbsec = (double) ((double) (getSize + sendSize) / 1000) / ((double) (last - start) / 1000);
143
    int timepassed = (last - start) / 1000;
144
 
145
    printf("Time:%d Size:%dB\/%dB Rate:%.1fKB/s       \r", timepassed, getSize, sendSize, kbsec);
146
//    cout << "Time: " << timepassed << "\t Get size: " << getSize << "\t Send size: " << sendSize << "\t Rate: " << kbsec << endl;
147
 
148
    rxfile.close();
149
 
150
    FT_SetBitMode(ftHandle, 0, 0);
151
    FT_Close(ftHandle);
152
 
153
    return 0;
154
}
155
 

powered by: WebSVN 2.1.0

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