-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFTDI_LOW_LEVEL.c
220 lines (193 loc) · 4.21 KB
/
FTDI_LOW_LEVEL.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "../ftd2xx.h"
#include "FTDI_LOW_LEVEL.h"
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
void FT_Find_all_Connected_Devices(void)
{
FT_DEVICE_LIST_INFO_NODE *devInfo;
DWORD numDevs;
DWORD devIndex = 0;
// create the device information list
ftStatus = FT_CreateDeviceInfoList(&numDevs);
if (ftStatus == FT_OK)
{
printf("Number of connected devices is %d\n",numDevs);
}
if (numDevs > 0)
{
//allocate storage for list based on numDevs
devInfo = (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs);
//get the device informtion list
ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs);
if (ftStatus == FT_OK)
{
for (DWORD i = 0; i < numDevs; i++)
{
printf(" Dev %d:\n",i);
printf(" Flags=0x%x\n",devInfo[i].Flags);
printf(" Type=0x%x\n",devInfo[i].Type);
printf(" ID=0x%x\n",devInfo[i].ID);
printf(" LocId=0x%x\n",devInfo[i].LocId);
printf(" SerialNumber=%s\n",devInfo[i].SerialNumber);
printf(" Description=%s\n",devInfo[i].Description);
printf(" ftHandle=0x%x\n",devInfo[i].ftHandle);
}
}
}
}
bool FT_Open_By_SN(bool FT_ShowDebugInformation)
{
ftStatus = FT_OpenEx("FT26FFDLA",FT_OPEN_BY_SERIAL_NUMBER, &ftHandle);
if (ftStatus != FT_OK)
{
if (FT_ShowDebugInformation == TRUE)
{
printf("FT open failed\n");
printf("Can't finde FTDI device with specified SN\n");
}
return true;
}
else
{
if (FT_ShowDebugInformation == TRUE)
{
printf("FTDI for FPGA is found\n");
}
return false;
}
}
//calling this function configure FTDI. Full functionality is not implemented yet
bool FT_SetConfiguration(UCHAR Mask, UCHAR FT_MODE, bool FT_ShowDebugInformation)
{
//configure FTDI for using in Syncronous mode, up to 480Mbps
ftStatus = FT_SetBitMode(ftHandle, Mask, FT_MODE);
if (ftStatus == FT_OK)
{
if (FT_ShowDebugInformation == true)
{
printf("FTDI is configured SYNC FIFO\n");
}
return true;
}
else
{
return false;
}
}
//Get information of selected FTDI configuration information. CPU,Syn FIFI, Async FIFO
UCHAR FT_GetConfiguration(bool FT_ShowDebugInformation)
{
UCHAR BitMode;
ftStatus = FT_GetBitMode(ftHandle, &BitMode);
if (ftStatus == FT_OK)
{
if (FT_ShowDebugInformation == true)
{
printf("BitMode=%x\n",BitMode);
}
return BitMode;
}
else
{
return 0xAA;
}
}
// This function writes to FTDI device only one byte
bool FT_SendByte(UCHAR cValue, bool FT_ShowDebugInformation)
{
DWORD BytesWritten;
ftStatus = FT_Write(ftHandle, &cValue, 1, &BytesWritten);
if (ftStatus == FT_OK)
{
if (FT_ShowDebugInformation == true)
{
printf(" FT_Write Ok, sent byte 0x%x\n", cValue);
}
return true;
}
else
{
if (FT_ShowDebugInformation == true)
{
printf("FT_Write Failed\n");
}
return false;
}
}
bool FT_SendBuffer(UCHAR* pTxBuffer, int nPageSize, int nBufferSize)
{
// DWORD nBufferSize = 32;
DWORD BytesSent;
int nTxBuffer = 0;
while(nPageSize !=0)
{
ftStatus = FT_Write(ftHandle, (char*)((int)pTxBuffer+nTxBuffer), nPageSize, &BytesSent);
if (ftStatus == FT_OK)
{
nTxBuffer += BytesSent;
// printf("BytesSend %d\n", BytesSent);
// printf("nTxBuffer value %d\n",nTxBuffer);
}
else
{
printf("FT Write Failed\n");
return false;
}
if ((nBufferSize - nTxBuffer) < nPageSize)
{
nPageSize = (nBufferSize - nTxBuffer);
}
}
return true;
}
UCHAR FT_ReadByte()
{
DWORD BytesReceived;
UCHAR FT_Byte;
ftStatus = FT_Read(ftHandle, &FT_Byte, 1, &BytesReceived);
if (ftStatus == FT_OK)
{
return FT_Byte;
}
else
{
printf("FT_Read failed\n");
return 0;
}
}
bool FT_ReadBuffer(UCHAR* pRxBuffer, int nPageSize, int nBMBufferSize)
{
DWORD nBufferSize = nBMBufferSize;
DWORD BytesReceived;
int nRxBuffer = 0;
while(nPageSize != 0)
{
ftStatus = FT_Read(ftHandle, (UCHAR*)((int)pRxBuffer+nRxBuffer), nPageSize, &BytesReceived);
if (ftStatus == FT_OK)
{
if (BytesReceived == nPageSize)
{
nRxBuffer += BytesReceived;
}
else
{
nRxBuffer += BytesReceived;
}
}
else
{
printf("FT_Read Failed!\n");
return false;
}
if ((nBufferSize - nRxBuffer) < nPageSize)
{
nPageSize = (nBufferSize - nRxBuffer);
}
}
return true;
}