1 | //---------------------------------------------------------------------------
|
2 |
|
3 | #include <vcl.h>
|
4 | #pragma hdrstop
|
5 |
|
6 | #include "Unit1.h"
|
7 | #include "stdafx.h"
|
8 | #include <windows.h>
|
9 | #include "FTCSPI.h"
|
10 |
|
11 | #define MAX_SPI_93LC56B_CHIP_SIZE_IN_WORDS 128
|
12 |
|
13 | // To communicate with the 93LC56(2048 word) EEPROM, the maximum frequency the clock can be set is 1MHz
|
14 | #define MAX_FREQ_93LC56B_CLOCK_DIVISOR 29 // defualt is 29 equivalent to 1MHz
|
15 |
|
16 | const BYTE SPI_EWEN_CMD = '\x9F'; // set up write enable command
|
17 | const BYTE SPI_EWDS_CMD = '\x87'; // set up write disable command
|
18 | const BYTE SPI_ERAL_CMD = '\x97'; // set up erase all command
|
19 |
|
20 | #define NUM_93LC56B_CMD_CONTOL_BITS 11 //Number of bits required for 93C56 Control operations
|
21 | #define NUM_93LC56B_CMD_CONTOL_BYTES 2
|
22 |
|
23 | #define NUM_93LC56B_CMD_DATA_BITS 16 // Number of bits required for 93C56 Data operations
|
24 | #define NUM_93LC56B_CMD_DATA_BYTES 2
|
25 |
|
26 | #define MAX_READ_DATA_WORDS_BUFFER_SIZE 65536 // Maximum Number of Data words
|
27 |
|
28 | typedef WORD ReadDataWordBuffer[MAX_READ_DATA_WORDS_BUFFER_SIZE];
|
29 | typedef ReadDataWordBuffer *PReadDataWordBuffer;
|
30 |
|
31 | #define DEV_DESC "Dual RS232-HS B" //"FT2232H MiniModule A"
|
32 |
|
33 | #define BUFSIZE (4*1024*1024) // 4MB - huge buffer to hold all the commands that are sent down the USB to the FT2232H
|
34 | #define BLOCKSIZE (8*1024) // We read/write chunks of 8kb to the cart
|
35 | #define NBLOCKS ((512*1024) / BLOCKSIZE)
|
36 | #define N_WAIT_CYCLES 96 // How many wait cycles for byte programming?
|
37 |
|
38 | #define FLASH_RESET 0
|
39 | #define FLASH_MANUFACTURER 1
|
40 | #define FLASH_DEVICECODE 2
|
41 | #define FLASH_WRITEBYTE 3
|
42 | #define FLASH_CHIPERASE 4
|
43 | #define FLASH_SECERASE 5
|
44 | #define FLASH_DEVICECODEEX 6
|
45 |
|
46 |
|
47 | // Command buffer
|
48 | BYTE cmdBuffer[BUFSIZE];
|
49 | int PosCmdBuf=0;
|
50 |
|
51 | // Data buffers
|
52 | BYTE datBuffer[BLOCKSIZE];
|
53 | BYTE verBuffer[BLOCKSIZE];
|
54 |
|
55 | // clock divider
|
56 | DWORD clock_divider=0;
|
57 |
|
58 | void CS_Low(void);
|
59 | void CS_High(void);
|
60 | void Power_Off(void);
|
61 | int Sync_To_MPSSE(void);
|
62 | int OpenDevice(void);
|
63 | void WriteCart(DWORD addr, BYTE dat, BYTE waitcycles);
|
64 | BYTE ReadCart(DWORD addr, DWORD nBytes);
|
65 | void FlushInBuffer(void);
|
66 | void FlushOutBuffer(void);
|
67 | void SetSpeed(BYTE speed);
|
68 | BYTE FlashProcess(int command, DWORD addr, BYTE data);
|
69 | BYTE ConvByt(BYTE byt);
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | //---------------------------------------------------------------------------
|
75 | #pragma package(smart_init)
|
76 | #pragma resource "*.dfm"
|
77 | TForm1 *Form1;
|
78 | //---------------------------------------------------------------------------
|
79 | __fastcall TForm1::TForm1(TComponent* Owner)
|
80 | : TForm(Owner)
|
81 | {
|
82 | }
|
83 | //---------------------------------------------------------------------------
|
84 | void __fastcall TForm1::Button1Click(TObject *Sender)
|
85 | {
|
86 | FTC_STATUS Status = FTC_SUCCESS;
|
87 | DWORD dwNumHiSpeedDevices = 0;
|
88 | char szDeviceName[100];
|
89 | char szChannel[5];
|
90 | DWORD dwLocationID = 0;
|
91 | DWORD dwHiSpeedDeviceType = 0;
|
92 | DWORD dwHiSpeedDeviceIndex = 0;
|
93 | FTC_HANDLE ftHandle = 0;
|
94 | char szDeviceDetails[150];
|
95 | BYTE timerValue = 0;
|
96 | DWORD dwClockFrequencyHz = 0;
|
97 | BOOL bPerformCommandSequence = false;
|
98 | FTC_CHIP_SELECT_PINS ChipSelectsDisableStates;
|
99 | FTH_INPUT_OUTPUT_PINS HighInputOutputPins;
|
100 | FTH_LOW_HIGH_PINS HighPinsInputData;
|
101 | FTC_INIT_CONDITION WriteStartCondition;
|
102 | WriteControlByteBuffer WriteControlBuffer;
|
103 | WriteDataByteBuffer WriteDataBuffer;
|
104 | DWORD dwNumDataBytesToWrite = 0;
|
105 | FTC_WAIT_DATA_WRITE WaitDataWriteComplete;
|
106 | FTH_HIGHER_OUTPUT_PINS HighPinsWriteActiveStates;
|
107 | FTC_CLOSE_FINAL_STATE_PINS CloseFinalStatePinsData;
|
108 | DWORD dwDataWordInitValue = 0;
|
109 | DWORD dwDataWordValue = 0;
|
110 | DWORD dwWriteDataWordAddress = 0;
|
111 | DWORD dwControlLocAddress1 = 0;
|
112 | DWORD dwControlLocAddress2 = 0;
|
113 | FTC_INIT_CONDITION ReadStartCondition;
|
114 | DWORD dwReadDataWordAddress = 0;
|
115 | DWORD dwReadWordValue = 0;
|
116 | ReadDataWordBuffer ReadWordData;
|
117 | DWORD dwNumDataBytesReturned = 0;
|
118 | DWORD dwDataWordWritten = 0;
|
119 | DWORD dwCharCntr = 0;
|
120 | ReadDataByteBuffer ReadDataBuffer;
|
121 | DWORD dwReadDataIndex = 0;
|
122 | ReadCmdSequenceDataByteBuffer ReadCmdSequenceDataBuffer;
|
123 | int MsgBoxKeyPressed = 0;
|
124 | DWORD dwLoopCntr = 0;
|
125 |
|
126 | char szDllVersion[10];
|
127 | char szTitleErrorMessage[100];
|
128 | char szStatusErrorMessage[100];
|
129 | char szErrorMessage[200];
|
130 | char szMismatchMessage[100];
|
131 |
|
132 | //char channel[1];
|
133 | char tmp_Channel[5];
|
134 | unsigned char i;
|
135 |
|
136 | //if(RadioButton1->Checked) channel = "A";
|
137 | //if(RadioButton2->Checked) channel = "B";
|
138 |
|
139 | for (dwCharCntr = 0; (dwCharCntr < 100); dwCharCntr++)
|
140 | szMismatchMessage[dwCharCntr] = '\0';
|
141 |
|
142 | for (i = 0; (i < 5); i++)
|
143 | tmp_Channel[i] = '\0';
|
144 |
|
145 | // Get version of DLL file
|
146 | Status = SPI_GetDllVersion(szDllVersion, 10);
|
147 | if(Status==FTC_SUCCESS) Edit1->Text = ((AnsiString)szDllVersion);
|
148 |
|
149 | Status = SPI_GetNumHiSpeedDevices(&dwNumHiSpeedDevices);
|
150 | if(Status==FTC_SUCCESS) Edit2->Text = IntToStr((int)dwNumHiSpeedDevices);
|
151 | //ShowMessage("Qty of HiSpeedDevices : " + IntToStr((int)dwNumHiSpeedDevices));
|
152 |
|
153 | if ((Status == FTC_SUCCESS) && (dwNumHiSpeedDevices > 0))
|
154 | {
|
155 | do
|
156 | {
|
157 |
|
158 | // This gets the name and location identifier for any FTDI HiSpeed device connected to the host.
|
159 |
|
160 | Status = SPI_GetHiSpeedDeviceNameLocIDChannel(dwHiSpeedDeviceIndex, szDeviceName, 100, &dwLocationID, szChannel, 5, &dwHiSpeedDeviceType);
|
161 | tmp_Channel[0] = szChannel[0];
|
162 | if(tmp_Channel[0] == 'A') Edit3->Text = szDeviceName;
|
163 | if(tmp_Channel[0] == 'B') Edit4->Text = szDeviceName;
|
164 | dwHiSpeedDeviceIndex = dwHiSpeedDeviceIndex + 1; // Increment to look for other HiSpeed device connected to host.
|
165 |
|
166 | }
|
167 | while ((Status == FTC_SUCCESS) && (dwHiSpeedDeviceIndex < dwNumHiSpeedDevices) && (strcmp(szChannel, "B") != 0));
|
168 |
|
169 | if (Status == FTC_SUCCESS)
|
170 | {
|
171 | if (strcmp(szChannel, "B") != 0)
|
172 | Status = FTC_DEVICE_IN_USE;
|
173 | }
|
174 |
|
175 | if (Status == FTC_SUCCESS) {
|
176 |
|
177 | if (ftHandle != 0)
|
178 | {
|
179 | SPI_Close(ftHandle);
|
180 | ftHandle = 0;
|
181 | Status == FTC_SUCCESS;
|
182 | }
|
183 |
|
184 | // Opens a handle to the Hi Speed device and initializes the device to default state.
|
185 |
|
186 | Status = SPI_OpenHiSpeedDevice(szDeviceName, dwLocationID, szChannel, &ftHandle);
|
187 |
|
188 | if (Status == FTC_DEVICE_IN_USE)
|
189 | {
|
190 |
|
191 | if (ftHandle != 0)
|
192 | {
|
193 | SPI_Close(ftHandle);
|
194 | ftHandle = 0;
|
195 | Status == FTC_SUCCESS;
|
196 | }
|
197 | }
|
198 |
|
199 | if (Status == FTC_SUCCESS) {
|
200 |
|
201 | // Returns the device type, either FT2232H or FT4232H.
|
202 |
|
203 | Status = SPI_GetHiSpeedDeviceType(ftHandle, &dwHiSpeedDeviceType);
|
204 | if (Status == FTC_SUCCESS) {
|
205 |
|
206 | strcpy(szDeviceDetails, "Type = ");
|
207 |
|
208 | if (dwHiSpeedDeviceType == FT4232H_DEVICE_TYPE) // Is the device a FT4232H ?
|
209 | strcat(szDeviceDetails, "FT4232H");
|
210 |
|
211 | else if (dwHiSpeedDeviceType == FT2232H_DEVICE_TYPE) // Is the device a FT222H ?
|
212 | strcat(szDeviceDetails, "FT2232H");
|
213 |
|
214 | strcat(szDeviceDetails, ", Name = ");
|
215 | strcat(szDeviceDetails, szDeviceName);
|
216 |
|
217 | ShowMessage(szDeviceDetails);
|
218 |
|
219 | }
|
220 | }
|
221 | }
|
222 | }
|
223 |
|
224 | hier gehts weiter.....
|