forked from brand7n/FTDIconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
313 lines (268 loc) · 12.1 KB
/
Program.cs
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// adapted from FTDI Loopback example by Brandin Claar <brandin@remodulate.com>
using System;
using System.Threading;
using FTD2XX_NET;
using lpcprog;
using System.Configuration;
namespace LoopBack
{
class Program
{
static void attention()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
}
static void prompt()
{
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.Black;
}
static void subdue()
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.BackgroundColor = ConsoleColor.Black;
}
static void program()
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.BackgroundColor = ConsoleColor.DarkBlue;
}
static void Main(string[] args)
{
program();
Console.WriteLine("This program contains a software component made available under the GNU Lesser General Public License. The source code of this component is available here:");
Console.WriteLine("https://github.com/brand7n/lpc21isp");
// Create new instance of the FTDI device class
FTDI myFtdiDevice = new FTDI();
attention();
Console.WriteLine("ATTEMPTING TO CONNECT...");
subdue();
while (true)
{
try
{
open(myFtdiDevice);
Thread.Sleep(1000);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
attention();
Console.WriteLine("DISCONNECTED. RESTART OR REATTACH DEVICE.");
subdue();
Console.WriteLine(ex.Message);
}
finally
{
myFtdiDevice.CyclePort();
}
}
}
static int myStrDelegate(int n, String m)
{
Console.Write(m);
return 0;
}
static void open(FTDI myFtdiDevice)
{
UInt32 ftdiDeviceCount = 0;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
// Determine the number of FTDI devices connected to the machine
ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
// Check status
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
return;
}
// If no devices available, return
if (ftdiDeviceCount == 0) return;
Console.WriteLine("Number of FTDI devices: " + ftdiDeviceCount.ToString());
// Allocate storage for device info list
FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
// Populate our device list
ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
int oIdx=-1;
if (ftStatus == FTDI.FT_STATUS.FT_OK)
{
for (Int32 i = 0; i < ftdiDeviceCount; i++)
{
if (ftdiDeviceList[i].Type == FTDI.FT_DEVICE.FT_DEVICE_X_SERIES)
{
oIdx = i;
}
}
if (oIdx < 0)
{
Console.WriteLine("no matching device");
return;
}
}
Console.WriteLine("Device Index: " + oIdx.ToString());
Console.WriteLine("Flags: " + String.Format("{0:x}", ftdiDeviceList[oIdx].Flags));
Console.WriteLine("Type: " + ftdiDeviceList[oIdx].Type.ToString());
Console.WriteLine("ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].ID));
Console.WriteLine("Location ID: " + String.Format("{0:x}", ftdiDeviceList[oIdx].LocId));
Console.WriteLine("Serial Number: " + ftdiDeviceList[oIdx].SerialNumber.ToString());
Console.WriteLine("Description: " + ftdiDeviceList[oIdx].Description.ToString());
myFtdiDevice.SetDTR(false);
myFtdiDevice.SetRTS(false);
// Open first device in our list by serial number
ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[oIdx].SerialNumber);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to open device (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
return;
}
myFtdiDevice.SetDTR(false);
myFtdiDevice.SetRTS(false);
// Set up device data parameters
uint baud = 115200;
if (ConfigurationManager.AppSettings["BaudRate"] != null)
{
baud = UInt32.Parse(ConfigurationManager.AppSettings["BaudRate"]);
}
ftStatus = myFtdiDevice.SetBaudRate(baud);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to set Baud rate (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
return;
}
// Set data characteristics - Data bits, Stop bits, Parity
ftStatus = myFtdiDevice.SetDataCharacteristics(FTDI.FT_DATA_BITS.FT_BITS_8, FTDI.FT_STOP_BITS.FT_STOP_BITS_1, FTDI.FT_PARITY.FT_PARITY_NONE);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to set data characteristics (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
return;
}
// Set flow control - set RTS/CTS flow control
ftStatus = myFtdiDevice.SetFlowControl(FTDI.FT_FLOW_CONTROL.FT_FLOW_NONE, 0x11, 0x13);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to set flow control (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
return;
}
// times out at 100ms
ftStatus = myFtdiDevice.SetTimeouts(100, 100);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
Console.WriteLine("Failed to set timeouts (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
return;
}
attention();
Console.WriteLine("CONNECTED. PRESS ANY KEY TO START.");
Console.WriteLine("CTRL-R: RESET");
Console.WriteLine("CTRL-P: PROGRAM");
Console.WriteLine("CTRL-T: SET TIME");
prompt();
myFtdiDevice.SetDTR(true);
myFtdiDevice.SetRTS(true);
while (true)
{
UInt32 numBytes = 0;
byte[] buf = new byte[1024];
while (Console.KeyAvailable)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
//Console.WriteLine("{0} (character '{1}')", cki.Key, cki.KeyChar);
if ((cki.Modifiers & ConsoleModifiers.Control) != 0)
{
if (cki.Key.ToString().ToUpper().EndsWith("R"))
{
attention();
Console.WriteLine("\nRESET!");
prompt();
myFtdiDevice.SetRTS(false);
myFtdiDevice.SetDTR(true);
Thread.Sleep(100);
myFtdiDevice.SetDTR(false);
continue;
}
if (cki.Key.ToString().ToUpper().EndsWith("P"))
{
attention();
Console.WriteLine("\nPROGRAM MODE:");
myFtdiDevice.Close();
program();
string firmwareName = ConfigurationManager.AppSettings["FirmwareName"];
LpcProgrammer p = new LpcProgrammer(new StringOutDelegate(myStrDelegate));
//p.Prepare();
int r = p.Program(firmwareName);
attention();
Console.WriteLine("\nRETURN VALUE: {0}", r);
subdue();
throw new Exception("Leaving program mode");
}
if (cki.Key.ToString().ToUpper().EndsWith("T"))
{
// Time: 10:11:0
// Date: 2827.5.9
string datePatt = @"yyyy.M.d";
string timePatt = @"HH:mm:ss";
DateTime dispDt = DateTime.Now;
string dtString = "set_date " + dispDt.ToString(datePatt) + "\r";
string tmString = "set_time " + dispDt.ToString(timePatt) + "\r";
//Console.WriteLine(dtString);
//Console.WriteLine(tmString);
System.Text.ASCIIEncoding ASCII = new System.Text.ASCIIEncoding();
Byte[] cmdBytes = ASCII.GetBytes(dtString);
myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes);
Thread.Sleep(200);
cmdBytes = ASCII.GetBytes(tmString);
myFtdiDevice.Write(cmdBytes, cmdBytes.Length, ref numBytes);
continue;
}
}
buf[0] = (byte)(cki.KeyChar & 0xff);
if (cki.Key == ConsoleKey.Enter)
buf[0] = (byte)'\n';
numBytes = 0;
ftStatus = myFtdiDevice.Write(buf, 1, ref numBytes);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
throw new Exception("Failed to write to device (error " + ftStatus.ToString() + ")");
//return;
}
}
numBytes = 0;
ftStatus = myFtdiDevice.GetRxBytesAvailable(ref numBytes);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
throw new Exception("Failed to get number of bytes available to read (error " + ftStatus.ToString() + ")");
//return;
}
if (numBytes > 0)
{
UInt32 numBytesRead = 0;
byte[] buffer = new byte[1024];
// Note that the Read method is overloaded, so can read string or byte array data
//ftStatus = myFtdiDevice.Read(out readData, 1024, ref numBytesRead);
ftStatus = myFtdiDevice.Read(buffer, 1024, ref numBytesRead);
if (ftStatus != FTDI.FT_STATUS.FT_OK)
{
// Wait for a key press
throw new Exception("Failed to read data (error " + ftStatus.ToString() + ")");
//Console.ReadKey();
//return;
}
string readData = System.Text.Encoding.ASCII.GetString(buffer, 0, (int)numBytesRead);
Console.Write(readData);
}
else
{
Thread.Sleep(10);
}
}
}
}
}