-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftdi_dev.cpp
331 lines (262 loc) · 7.71 KB
/
ftdi_dev.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Implementation of FTDIDEV class
Copyright (C) 2017 Alamy Liu <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include <iostream> /* cout */
#include <iomanip> /* setw, setfill, ... */
#include <assert.h> /* assert */
#include "ftdi_dev.hpp"
/* -------------------- Constructor / Destructor -------------------- */
#if 0
FTDIDEV::FTDIDEV( int vid, int pid, enum ftdi_chip_type type )
: FTDIDEV( vid, pid )
{
if (ftdi) {
ftdi->type = type;
}
}
#endif
FTDIDEV::FTDIDEV( Options *opt )
{
int rc;
string err_string;
/* Already created */
if (ftdi != NULL) {
return;
}
if ((ftdi = ftdi_new()) == NULL) {
err_string = "Failed to new FTDI!";
goto err_new;
}
/* Not really accessing USB device's EEPROM. i.e.: file */
if (
(opt == NULL)
|| ( !(opt->isInFTDIDEV() || opt->isOutFTDIDEV()) )
) {
return;
}
assert( opt->isBusDefined() || opt->isIdDefined() );
/* Open by bus:dev - ftdi_usb_open_bus_addr */
if ( opt->isBusDefined() ) {
rc = ftdi_usb_open_bus_addr(ftdi,
opt->getBus(), opt->getDev());
if (rc < 0) {
goto err_open;
}
}
/* Open by pid:vid - ftdi_usb_open */
if ( opt->isIdDefined() ) {
rc = ftdi_usb_open(ftdi,
opt->getVid(), opt->getPid());
if (rc < 0) {
goto err_open;
}
}
/* ToDo: Use ftdi_usb_open_desc() to specify _description_ & _serial_ of
* the device
*/
/* IMPORTANT: Perform a EEPROM read to get eeprom size */
if ((rc = read_eeprom()) < 0) {
goto err_open;
}
return;
err_open:
err_string = ftdi_get_error_string(ftdi);
ftdi_free( ftdi );
ftdi = NULL;
err_new:
throw std::runtime_error( err_string );
// cerr << err_string << endl;
// throw rc;
}
FTDIDEV::~FTDIDEV()
{
if (ftdi) {
ftdi_usb_close( ftdi );
ftdi_free( ftdi );
ftdi = NULL;
}
}
/* ------------------------------------------------------------------ */
int FTDIDEV::read_file(string path)
{
int rc;
unsigned int buf_size;
ifstream ifs( path, ios::in | ios::binary);
/* if output is EEPROM, eeprom_buf_size[I] would be EEPROM size
* see iSize, oSize in main() function.
*/
buf_size = eeprom_buf_size[I];
/* TODO: handle ERROR */
ifs.read( reinterpret_cast<char*>(file_buf), buf_size);
ifs.close();
/* Copy data to EEPROM buffer */
// buffer size had been set by set_buffer_sizes()
rc = ftdi_set_eeprom_buf(ftdi, file_buf, buf_size);
if ( rc != 0 ) {
cerr << "Fail to set EEPROM buffer" << endl;
}
/* CAUTION: Hacking libftdi to enable this feature */
ftdi_set_eeprom_value(ftdi, CHIP_SIZE, buf_size);
return 0;
}
int FTDIDEV::write_file(string path)
{
int rc;
unsigned int buf_size;
ofstream ofs( path, ios::out | ios::trunc | ios::binary);
/* Copy data from EEPROM buffer */
buf_size = eeprom_buf_size[O];
rc = ftdi_get_eeprom_buf(ftdi, file_buf, buf_size);
if (rc < 0) {
cerr << "Fail to get EEPROM buffer" << endl;
return rc;
}
/* output size depends on input (EEPROM or file) */
ofs.write( reinterpret_cast<const char*>(file_buf), buf_size);
ofs.close();
return 0;
}
int FTDIDEV::read_eeprom()
{
int rc;
if ( !ftdi ) return -ENODEV;
if ((rc = ftdi_read_eeprom(ftdi)) < 0) {
cerr << "Fail to Read EEPROM: " << rc
<< "(" << ftdi_get_error_string(ftdi) << ")" << endl;
}
/* size will also be set to -1 in the case of Blank EEPROM */
eeprom_blank = (get_eeprom_size() == -1);
return rc;
}
int FTDIDEV::write_eeprom()
{
int rc;
if ( !ftdi ) return -ENODEV;
if ((rc = ftdi_write_eeprom(ftdi)) == 0) {
cout << "Replug device to see the result!" << endl;
} else {
cerr << "Fail to Write EEPROM: " << rc
<< "(" << ftdi_get_error_string(ftdi) << ")" << endl;
}
return rc;
}
/* ------------------------------------------------------------------ */
int FTDIDEV::read(bool isInFTDIDEV, string fName, bool verboseMode)
{
int rc;
if ( isInFTDIDEV ) {
rc = read_eeprom();
/* data had been directly read into FTDI buffer */
} else {
rc = read_file(fName);
}
if ( verboseMode ) {
dump( eeprom_buf_size[I] );
}
return rc;
}
int FTDIDEV::write(bool isOutFTDIDEV, string fName, bool verboseMode)
{
int rc;
if ( verboseMode ) {
dump( eeprom_buf_size[O] );
}
if ( isOutFTDIDEV ) {
rc = write_eeprom();
/* data had been directly read into FTDI buffer */
} else {
rc = write_file(fName);
}
return rc;
}
/* ------------------------------------------------------------------ */
int FTDIDEV::decode(int verbose)
{
int rc;
if ( !ftdi ) return -ENODEV;
if ((rc = ftdi_eeprom_decode(ftdi, verbose)) < 0) {
cerr << "Fail to Decode: " << rc << endl;
if ( ftdi ) {
cerr << "(" << ftdi_get_error_string(ftdi) << ")" << endl;
}
}
return rc;
}
void FTDIDEV::show_info( void )
{
char *ChipType[] = {
(char *)"AM",
(char *)"BM",
(char *)"2232C",
(char *)"R",
(char *)"2232H",
(char *)"4232H",
(char *)"232H",
(char *)"230X",
};
if ( !ftdi ) {
cerr << __func__ << ": FTDI device not available!" << endl;
return;
}
cout << "Chip type: " << ChipType[ftdi->type] << endl;
cout << "EEPROM size: " << get_eeprom_size() << endl;
}
void FTDIDEV::dump(unsigned int buf_size)
{
unsigned int offset, i;
char ch;
unsigned char *buf = file_buf;
if (is_EEPROM_blank()) {
buf_size = FTDI_MAX_EEPROM_SIZE;
cout << "EEPROM is empty, use maximum size: " << buf_size << endl;
}
/* The data might already be in the *file_buf*
* i.e.: Input from file_buf
* Just copy it for other cases, don't bother to check
*/
/* Copy data from EEPROM buffer */
if (ftdi_get_eeprom_buf(ftdi, buf, buf_size) < 0) {
cerr << "Fail to get EEPROM buffer" << endl;
return;
}
cout << hex << setfill('0');
for(offset = 0; offset < buf_size; offset += 16) {
/* show the offset */
cout << setw(8) << offset;
/* show the hex codes */
for (i = 0; i < 16; i++) {
if (i % 8 == 0) cout << ' ';
if (offset + i < buf_size)
cout << ' ' << setw(2) << (unsigned)(buf[offset + i]);
else
cout << " ";
}
/* show the ASCII */
cout << " ";
for (i = 0; i < 16; i++) {
if (offset + i < buf_size) {
ch = buf[offset + i];
ch = ( ((ch >= 0x20) && (ch <= 0x7E)) ? ch : '.' );
} else {
ch = '.';
}
cout << ch;
}
cout << endl;
}
cout.unsetf(ios::hex);
cout << endl;
}