-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftx_prog.c
1337 lines (1186 loc) · 36.3 KB
/
ftx_prog.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This is a Linux command-line alternative to the FTDI MProg/FTProg
* utilities for FTDI's FT-X series.
*
* Modified for the FT-X series by Richard Meadows 2012.
*
* Based upon:
* ft232r_prog.c by Mark Lord. Copyright 2010-2012.
*
* 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, 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; see the file LICENSE.txt. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ftdi.h>
#include <stdbool.h>
#include <unistd.h>
#define MYVERSION "0.3"
#define CBUS_COUNT 7
static struct ftdi_context ftdi;
static int verbose = 0;
static int erase_eeprom = 0;
static int ignore_crc_error = 0;
static bool use_8b_strings = false;
static const char *save_path = NULL, *restore_path = NULL;
/* ------------ Bit Definitions for EEPROM Decoding ------------ */
enum cbus_mode {
cbus_tristate = 0,
cbus_rxled = 1,
cbus_txled = 2,
cbus_txrxled = 3,
cbus_pwren = 4,
cbus_sleep = 5,
cbus_drive0 = 6,
cbus_drive1 = 7,
cbus_gpio = 8,
cbus_txden = 9,
cbus_clk24 = 10,
cbus_clk12 = 11,
cbus_clk6 = 12,
cbus_bcd_det = 13,
cbus_bcd_det_i = 14,
cbus_i2c_txe = 15,
cbus_i2c_rxf = 16,
cbus_vbus_sense = 17,
cbus_bitbang_wr = 18,
cbus_bitbang_rd = 19,
cbus_timestamp = 20,
cbus_keep_awake = 21,
_cbus_mode_end
};
enum misc_config {
bcd_enable = 0x01,
force_power_enable = 0x02,
deactivate_sleep = 0x04,
rs485_echo_suppress = 0x08,
ext_osc = 0x10,
ext_osc_feedback_en = 0x20,
vbus_sense_alloc = 0x40,
load_vcp = 0x80,
};
enum power_config {
remote_wakeup = 0x20,
self_powered = 0x40,
};
enum dbus_cbus_config {
dbus_drive_strength = 0x03,
dbus_slow_slew = 0x04,
dbus_schmitt = 0x08,
cbus_drive_strength = 0x30,
cbus_slow_slew = 0x40,
cbus_schmitt = 0x80,
};
enum peripheral_config {
suspend_pull_down = 0x04,
serial_number_avail = 0x08,
ft1248_cpol = 0x10,
ft1248_bord = 0x20,
ft1248_flow_control = 0x40,
disable_i2c_schmitt = 0x80,
invert_txd = 0x01,
invert_rxd = 0x02,
invert_rts = 0x04,
invert_cts = 0x08,
invert_dtr = 0x10,
invert_dsr = 0x20,
invert_dcd = 0x40,
invert_ri = 0x80,
};
/* ------------ Command Line Arguments ------------ */
enum arg_type {
arg_help,
arg_dump,
arg_verbose,
arg_save,
arg_restore,
arg_8b_strings,
arg_cbus,
arg_manufacturer,
arg_product,
arg_old_serno,
arg_new_serno,
arg_max_bus_power,
arg_suspend_pull_down,
arg_load_vcp,
arg_remote_wakeup,
arg_ft1248_cpol,
arg_ft1248_bord,
arg_ft1248_flow_control,
arg_i2c_schmitt,
arg_i2c_slave_address,
arg_i2c_device_id,
arg_rs485_echo_suppression,
arg_old_vid,
arg_old_pid,
arg_new_vid,
arg_new_pid,
arg_invert,
arg_noinvert,
arg_self_powered,
arg_ignore_crc_error,
arg_erase_eeprom,
arg_dbus_config,
arg_cbus_config
};
struct args_required_t
{
enum arg_type t;
int number;
};
const struct args_required_t req_info[] =
{
{arg_help, 0},
{arg_dump, 0},
{arg_verbose, 0},
{arg_save, 1},
{arg_restore, 1},
{arg_8b_strings, 0},
{arg_cbus, 2},
{arg_manufacturer, 1},
{arg_product, 1},
{arg_old_serno, 1},
{arg_new_serno, 1},
{arg_max_bus_power, 1},
{arg_suspend_pull_down, 1},
{arg_load_vcp, 1},
{arg_remote_wakeup, 1},
{arg_ft1248_cpol, 1},
{arg_ft1248_bord, 1},
{arg_ft1248_flow_control, 1},
{arg_i2c_schmitt, 1},
{arg_i2c_slave_address, 1},
{arg_i2c_device_id, 1},
{arg_rs485_echo_suppression, 1},
{arg_old_vid, 1},
{arg_old_pid, 1},
{arg_new_vid, 1},
{arg_new_pid, 1},
{arg_invert, 1},
{arg_noinvert, 1},
{arg_self_powered, 1},
{arg_ignore_crc_error, 0},
{arg_erase_eeprom, 0},
{arg_cbus_config,1},
};
/* ------------ Strings for argument parsing ------------ */
static const char* arg_type_strings[] = {
"--help",
"--dump",
"--verbose",
"--save",
"--restore",
"--8bit-strings",
"--cbus",
"--manufacturer",
"--product",
"--old-serial-number",
"--new-serial-number",
"--max-bus-power",
"--suspend-pull-down",
"--load-vcp",
"--remote-wakeup",
"--ft1248-cpol",
"--ft1248-bord",
"--ft1248-flow-control",
"--i2c-schmitt",
"--i2c-slave-address",
"--i2c-device-id",
"--rs485-echo-supp",
"--old-vid",
"--old-pid",
"--new-vid",
"--new-pid",
"--invert",
"--noinvert",
"--self-powered",
"--ignore-crc-error",
"--erase-eeprom",
"--dbus-config",
"--cbus-config",
NULL
};
static const char* rs232_strings[] = {
"txd",
"rxd",
"rts",
"cts",
"dtr",
"dsr",
"dcd",
"ri",
NULL
};
static const char* cbus_strings[] = {
"0",
"1",
"2",
"3",
"4",
"5",
"6",
NULL
};
static const char* cbus_mode_strings[] = {
"Tristate",
"RxLED",
"TxLED",
"TxRxLED",
"PWREN",
"SLEEP",
"Drive_0",
"Drive_1",
"GPIO",
"TXDEN",
"CLK24MHz",
"CLK12MHz",
"CLK6MHz",
"BCD_Charger",
"BCD_Charger#",
"I2C_TXE",
"I2C_RXF",
"VBUS_Sense",
"BitBang_WR",
"BitBang_RD",
"Time_Stamp",
"Keep_Awake",
NULL
};
static const char *d_cbus_config_strings [] = {
"4ma",
"8ma",
"12ma",
"16ma",
"slow_slew",
"fast_slew",
"normal",
"schmitt",
NULL
};
static const char *arg_type_help[] = {
" # (show this help text)",
" # (dump eeprom settings to stdout)",
" # (show debug info and raw eeprom contents)",
" <file> # (save original eeprom contents to file)",
" <file> # (restore initial eeprom contents from file)",
" # (byte strings)",
"[cbus]",
" <string> # (new USB manufacturer string)",
" <string> # (new USB product name string)",
" <string> # (current serial number of device to be reprogrammed)",
" <string> # (new USB serial number string)",
" <number> # (max bus current in milli-amperes)",
" [on|off] # (force I/O pins into logic low state on suspend)",
" [on|off] # (controls if the VCP drivers are loaded)",
" [on|off] # (allows the interface to be woken up by something other than USB)",
" [high|low] # (set the clock polarity on the FT1248 interface to active high or active low)",
" [msb|lsb] # (set the bit order on the FT1248 interface to msb first or lsb first)",
" [on|off] # (flow control for FT1248 interface)",
" [on|off] # (schmitt trigger on I2C interface)",
" <number> # (I2C slave address)",
" <number> # (I2C device ID)",
" [on|off] # (enable echo supression on the RS485 bus)",
" <number> # (current vendor id of device to be reprogrammed, eg. 0x0403)",
" <number> # (current product id of device to be reprogrammed, eg. 0x6001)",
" <number> # (new/custom vendor id to be programmed)",
" <number> # (new/custom product id be programmed)",
"[invert]",
"[noinvert]",
" [on|off] # (specify if chip is bus-powered or self-powered)",
" # Ignore CRC errors and continue ",
" # Erase the EEPROM and exit",
"dbus_cfg",
"cbus_cfg",
};
static const char *bool_strings[] = {
"False",
"True",
"false",
"true",
"Off",
"On",
"off",
"on",
"0",
"1",
"No",
"Yes",
"no",
"yes",
"disable",
"enable",
"low",
"high",
"msb",
"lsb",
};
struct eeprom_fields {
/* Misc Config */
unsigned char bcd_enable;
unsigned char force_power_enable;
unsigned char deactivate_sleep;
unsigned char rs485_echo_suppress;
unsigned char ext_osc;
unsigned char ext_osc_feedback_en;
unsigned char vbus_sense_alloc;
unsigned char load_vcp;
/* USB VID/PID */
unsigned short usb_vid;
unsigned short usb_pid;
/* USB Release Number */
unsigned short usb_release_major;
unsigned short usb_release_minor;
/* Max Power and Config */
unsigned char remote_wakeup;
unsigned char self_powered;
unsigned char max_power; /* Units of 2mA */
/* Device and perhiperal control */
unsigned char suspend_pull_down;
unsigned char serial_number_avail;
unsigned char ft1248_cpol;
unsigned char ft1248_bord;
unsigned char ft1248_flow_control;
unsigned char disable_i2c_schmitt;
unsigned char invert_txd;
unsigned char invert_rxd;
unsigned char invert_rts;
unsigned char invert_cts;
unsigned char invert_dtr;
unsigned char invert_dsr;
unsigned char invert_dcd;
unsigned char invert_ri;
/* DBUS & CBUS Control */
unsigned char dbus_drive_strength;
unsigned char dbus_slow_slew;
unsigned char dbus_schmitt;
unsigned char cbus_drive_strength;
unsigned char cbus_slow_slew;
unsigned char cbus_schmitt;
/* Manufacturer, Product and Serial Number string */
char* manufacturer_string;
char* product_string;
char* serial_string;
/* I2C */
unsigned short i2c_slave_addr;
unsigned int i2c_device_id;
/* CBUS */
enum cbus_mode cbus[CBUS_COUNT];
/* Other memory areas */
unsigned char user_mem[92]; /* user memory space */
unsigned char factory_config[32]; /* factory configuration values */
/* These are not actually eeprom values; here for convenience */
unsigned short old_vid;
unsigned short old_pid;
const char *old_serno;
};
/* ------------ libftdi helpers ------------ */
static void do_deinit (void)
{
ftdi_deinit(&ftdi);
}
static void do_close (void)
{
ftdi_usb_close(&ftdi);
}
/* ------------ Printing ------------ */
/**
* Prints a hex dump of a block of memory
*/
static void dumpmem (const char *msg, void *addr, int len)
{
char *data = addr, hex[3 * 16 + 1], ascii[17];
unsigned int i, offset = 0;
if (msg)
printf("%s:\n", msg);
for (i = 0; i < len;) {
unsigned int i16 = i % 16;
unsigned char c = data[i];
sprintf(hex + (3 * i16), " %02x", c);
ascii[i16] = (c < ' ' || c > '~') ? '.' : c;
if (++i == len || i16 == 15) {
ascii[i16 + 1] = '\0';
for (; i16 != 15; ++i16)
strcat(hex, " ");
printf("%04x:%s %s\n", offset, hex, ascii);
offset = i;
}
}
}
/**
* A helper that prints either true or false
*/
const char* print_bool(char value)
{
if (value) return bool_strings[1];
return bool_strings[0];
}
/**
* Prints out the current FT-X EEPROM Configuration
*/
static void ee_dump (struct eeprom_fields *ee)
{
unsigned int c;
/* Misc Config */
printf(" Battery Charge Detect (BCD) Enabled = %s\n",
print_bool(ee->bcd_enable));
printf(" Force Power Enable Signal on CBUS = %s\n",
print_bool(ee->force_power_enable));
printf(" Deactivate Sleep in Battery Charge Mode = %s\n",
print_bool(ee->deactivate_sleep));
printf(" External Oscillator Enabled = %s\n", print_bool(ee->ext_osc));
printf(" External Oscillator Feedback Resistor Enabled = %s\n",
print_bool(ee->ext_osc_feedback_en));
printf(" CBUS pin allocated to VBUS Sense Mode = %s\n",
print_bool(ee->vbus_sense_alloc));
printf(" Load Virtual COM Port (VCP) Drivers = %s\n",
print_bool(ee->load_vcp));
/* USB VID/PID */
printf(" Vendor ID (VID) = 0x%04x\n", ee->usb_vid);
printf(" Product ID (PID) = 0x%04x\n", ee->usb_pid);
/* USB Release Number */
printf(" USB Version = USB%d.%d\n", ee->usb_release_major,
ee->usb_release_minor);
/* Max Power and Config */
printf(" Remote Wakeup by something other than USB = %s\n",
print_bool(ee->remote_wakeup));
printf(" Self Powered = %s\n", print_bool(ee->self_powered));
printf(" Maximum Current Supported from USB = %dmA\n",
2 * ee->max_power); /* units of 2mA */
/* Device and perhiperal control */
printf(" Pins Pulled Down on USB Suspend = %s\n",
print_bool(ee->suspend_pull_down));
printf(" Indicate USB Serial Number Available = %s\n",
print_bool(ee->serial_number_avail));
printf(" FT1248\n");
printf("-------\n");
printf(" FT1248 Clock Polarity = %s\n",
ee->ft1248_cpol ? "Active High":"Active Low");
printf(" FT1248 Bit Order = %s\n",
ee->ft1248_bord ? "LSB to MSB":"MSB to LSB");
printf(" FT1248 Flow Control Enabled = %s\n",
print_bool(ee->ft1248_flow_control));
printf(" RS232\n");
printf("-------\n");
printf(" Invert TXD = %s\n", print_bool(ee->invert_txd));
printf(" Invert RXD = %s\n", print_bool(ee->invert_rxd));
printf(" Invert RTS = %s\n", print_bool(ee->invert_rts));
printf(" Invert CTS = %s\n", print_bool(ee->invert_cts));
printf(" Invert DTR = %s\n", print_bool(ee->invert_dtr));
printf(" Invert DSR = %s\n", print_bool(ee->invert_dsr));
printf(" Invert DCD = %s\n", print_bool(ee->invert_dcd));
printf(" Invert RI = %s\n", print_bool(ee->invert_ri));
printf(" RS485\n");
printf("-------\n");
printf(" RS485 Echo Suppression Enabled = %s\n",
print_bool(ee->rs485_echo_suppress));
/* DBUS & CBUS Control */
printf(" DBUS Drive Strength = %dmA\n", 4 * (ee->dbus_drive_strength+1));
printf(" DBUS Slow Slew Mode = %u\n", ee->dbus_slow_slew);
printf(" DBUS Schmitt Trigger = %u\n", ee->dbus_schmitt);
printf(" CBUS Drive Strength = %dmA\n", 4 * (ee->cbus_drive_strength+1));
printf(" CBUS Slow Slew Mode = %u\n", ee->cbus_slow_slew);
printf(" CBUS Schmitt Trigger = %u\n", ee->cbus_schmitt);
/* Manufacturer, Product and Serial Number string */
printf(" Manufacturer = %s\n", ee->manufacturer_string);
printf(" Product = %s\n", ee->product_string);
printf(" Serial Number = %s\n", ee->serial_string);
/* I2C */
printf(" I2C\n");
printf("-------\n");
printf(" I2C Slave Address = %d \n", ee->i2c_slave_addr);
printf(" I2C Device ID = %d \n", ee->i2c_device_id);
printf(" I2C Schmitt Triggers Disabled = %s\n",
print_bool(ee->disable_i2c_schmitt));
/* CBUS */
printf(" CBUS\n");
printf("-------\n");
for (c = 0; c < CBUS_COUNT; ++c) {
/* Check this is a valid cbus mode */
if (ee->cbus[c] < _cbus_mode_end) {
printf(" CBUS%u = %s\n", c, cbus_mode_strings[ee->cbus[c]]);
} else {
printf(" CBUS%u = %d\n", c, ee->cbus[c]);
}
}
}
/* ------------ Cyclic Redundancy Check ------------ */
static unsigned short calc_crc_ftx (void *addr)
{
unsigned int i;
unsigned short crc = 0xaaaa;
unsigned char* d8 = addr;
/* Word Addresses 0x0 - 0x11 inclusive */
for (i = 0; i < 0x12*2; i += 2) {
crc ^= d8[i] | (d8[i+1] << 8);
crc = (crc << 1) | (crc >> 15);
}
/* Word Addresses 0x12 - 0x39 are ignored */
/* Word Addresses 0x40 - 0x7E inclusive */
for (i = 0x40*2; i < 0x7F*2; i += 2) {
crc ^= d8[i] | (d8[i+1] << 8);
crc = (crc << 1) | (crc >> 15);
}
/* Word Address 0x7E is ignored */
/* Word Address 0x7F is the checksum */
return crc;
}
static unsigned short verify_crc (void *addr, int len)
{
unsigned short crc = calc_crc_ftx(addr);
unsigned char *d8 = addr;
unsigned short actual = d8[len-2] | (d8[len-1] << 8);
if (crc != actual) {
fprintf(stderr, "Bad CRC: crc=0x%04x, actual=0x%04x\n", crc, actual);
if (ignore_crc_error == 0) {
exit(EINVAL);
} else {
fprintf(stderr, "Ignoring CRC error\n");
}
}
if (verbose) { printf("CRC: Okay (0x%04x)\n", crc); }
return crc;
}
static unsigned short update_crc (void *addr, int len)
{
unsigned short crc = calc_crc_ftx(addr);
unsigned char *d8 = addr;
d8[len-2] = crc;
d8[len-1] = crc >> 8;
return crc;
}
/* ------------ EEPROM Encoding and Decoding ------------ */
/**
* Checks that the strings aren't too big to fit in the string
* descriptors memory.
*/
static int ee_check_strings(char* man, char* prod, char* ser)
{
/* if the strings are too long */
if (strlen(man) + strlen(prod) + strlen(ser) > 96) return 1;
return 0;
}
/**
* Inserts a string into a buffer to be written out to the eeprom
*/
static void ee_encode_string(char* str, unsigned char *ptr_field,
unsigned char* len_field, unsigned char* eeprom,
unsigned char* string_addr)
{
int original_length = strlen(str), length;
if (use_8b_strings) {
length = original_length;
} else {
length = strlen(str)*2 + 2;
char* ftstr = malloc(length);
/* Encode a FT Prog compatible string */
ftstr[0] = length;
ftstr[1] = 3;
int in, out;
for (in = 0, out = 2; out < length; in++, out += 2) {
ftstr[out] = str[in];
ftstr[out+1] = 0;
}
str = ftstr;
}
/* Copy the strings to the string area */
memcpy(eeprom + *string_addr, str, length);
/* Write the the two metadata fields */
*ptr_field = *string_addr;
*len_field = length;
/* Move the string area address forward */
*string_addr += *len_field;
}
/**
* Encodes an eeprom_fields object into a buffer ready to be written
* out to the eeprom.
*/
static unsigned short ee_encode (unsigned char *eeprom, int len,
struct eeprom_fields *ee)
{
int c; unsigned char string_desc_addr = 0xA0;
memset(eeprom, 0, len);
/* Misc Config */
if (ee->bcd_enable) eeprom[0x00] |= bcd_enable;
if (ee->force_power_enable) eeprom[0x00] |= force_power_enable;
if (ee->deactivate_sleep) eeprom[0x00] |= deactivate_sleep;
if (ee->rs485_echo_suppress) eeprom[0x00] |= rs485_echo_suppress;
if (ee->ext_osc) eeprom[0x00] |= ext_osc;
if (ee->ext_osc_feedback_en) eeprom[0x00] |= ext_osc_feedback_en;
if (ee->vbus_sense_alloc) eeprom[0x00] |= vbus_sense_alloc;
if (ee->load_vcp) eeprom[0x00] |= load_vcp;
/* USB VID/PID */
eeprom[0x02] = ee->usb_vid & 0xFF;
eeprom[0x03] = (ee->usb_vid >> 8) & 0xFF;
eeprom[0x04] = ee->usb_pid & 0xFF;
eeprom[0x05] = (ee->usb_pid >> 8) & 0xFF;
/* USB Release Number */
eeprom[0x07] = ee->usb_release_major;
eeprom[0x06] = ee->usb_release_minor;
/* Max Power and Config */
if (ee->remote_wakeup) eeprom[0x08] |= remote_wakeup;
if (ee->self_powered) eeprom[0x08] |= self_powered;
eeprom[0x08] |= 0x80; /* This is a reserved bit! */
eeprom[0x09] = ee->max_power; /* Units of 2mA */
/* Device and perhiperal control */
if (ee->suspend_pull_down) eeprom[0x0A] |= suspend_pull_down;
if (ee->serial_number_avail) eeprom[0x0A] |= serial_number_avail;
if (ee->ft1248_cpol) eeprom[0x0A] |= ft1248_cpol;
if (ee->ft1248_bord) eeprom[0x0A] |= ft1248_bord;
if (ee->ft1248_flow_control) eeprom[0x0A] |= ft1248_flow_control;
if (ee->disable_i2c_schmitt) eeprom[0x0A] |= disable_i2c_schmitt;
if (ee->invert_txd) eeprom[0x0B] |= invert_txd;
if (ee->invert_rxd) eeprom[0x0B] |= invert_rxd;
if (ee->invert_rts) eeprom[0x0B] |= invert_rts;
if (ee->invert_cts) eeprom[0x0B] |= invert_cts;
if (ee->invert_dtr) eeprom[0x0B] |= invert_dtr;
if (ee->invert_dsr) eeprom[0x0B] |= invert_dsr;
if (ee->invert_dcd) eeprom[0x0B] |= invert_dcd;
if (ee->invert_ri) eeprom[0x0B] |= invert_ri;
/* DBUS & CBUS Control */
eeprom[0x0C] |= (ee->dbus_drive_strength & dbus_drive_strength);
if (ee->dbus_slow_slew) eeprom[0x0C] |= dbus_slow_slew;
if (ee->dbus_schmitt) eeprom[0x0C] |= dbus_schmitt;
eeprom[0x0C] |= (ee->cbus_drive_strength << 4) & cbus_drive_strength;
if (ee->cbus_slow_slew) eeprom[0x0C] |= cbus_slow_slew;
if (ee->cbus_schmitt) eeprom[0x0C] |= cbus_schmitt;
/* eeprom[0x0D] is unused */
/* Manufacturer, Product and Serial Number string */
if (ee_check_strings(ee->manufacturer_string, ee->product_string,
ee->serial_string)) {
fprintf(stderr,
"Failed to encode, strings too long to fit in string memory area!\n");
exit(EINVAL);
}
ee_encode_string(ee->manufacturer_string, &eeprom[0x0E], &eeprom[0x0F],
eeprom, &string_desc_addr);
ee_encode_string(ee->product_string, &eeprom[0x10], &eeprom[0x11],
eeprom, &string_desc_addr);
ee_encode_string(ee->serial_string, &eeprom[0x12], &eeprom[0x13],
eeprom, &string_desc_addr);
/* I2C */
eeprom[0x14] = ee->i2c_slave_addr & 0xFF;
eeprom[0x15] = (ee->i2c_slave_addr >> 8) & 0xFF;
eeprom[0x16] = ee->i2c_device_id & 0xFF;
eeprom[0x17] = (ee->i2c_device_id >> 8) & 0xFF;
eeprom[0x18] = (ee->i2c_device_id >> 16) & 0xFF;
/* CBUS */
for (c = 0; c < CBUS_COUNT; c++) {
eeprom[0x1A + c] = ee->cbus[c];
}
/* User Memory Space */
memcpy(&eeprom[0x24], ee->user_mem, 92);
/* Factory Configuration Values */
memcpy(&eeprom[0x80], ee->factory_config, 32);
return update_crc(eeprom, len);
}
/**
* Extracts a string from the a buffer read from eeprom
*/
static char* ee_decode_string(unsigned char *eeprom, unsigned char* ptr,
unsigned char len)
{
char* str = malloc(len+1);
if (str != NULL) {
/* Copy the string from the EEPROM memory */
memcpy(str, ptr, len);
/* Decode strings written by FT Prog correctly */
if (use_8b_strings) {
str[len] = '\0';
} else {
/* Pick the actual ASCII characters out of the FT Prog encoded string */
int in, out;
for (in = 2, out = 0; in < len; in += 2, out++) {
str[out] = str[in];
}
str[out] = '\0';
}
}
return str;
}
/*
* Populates an eeprom_fields object from a buffer read from eeprom
*/
static void ee_decode (unsigned char *eeprom, int len, struct eeprom_fields *ee)
{
int c;
/* Misc Config */
ee->bcd_enable = (eeprom[0x00] & bcd_enable);
ee->force_power_enable = (eeprom[0x00] & force_power_enable);
ee->deactivate_sleep = (eeprom[0x00] & deactivate_sleep);
ee->rs485_echo_suppress = (eeprom[0x00] & rs485_echo_suppress);
ee->ext_osc = (eeprom[0x00] & ext_osc);
ee->ext_osc_feedback_en = (eeprom[0x00] & ext_osc_feedback_en);
ee->vbus_sense_alloc = (eeprom[0x00] & vbus_sense_alloc);
ee->load_vcp = (eeprom[0x00] & load_vcp);
/* USB VID/PID */
ee->usb_vid = eeprom[0x02] | (eeprom[0x03] << 8);
ee->usb_pid = eeprom[0x04] | (eeprom[0x05] << 8);
/* USB Release Number */
ee->usb_release_major = eeprom[0x07];
ee->usb_release_minor = eeprom[0x06];
/* Max Power and Config */
ee->remote_wakeup = (eeprom[0x08] & remote_wakeup);
ee->self_powered = (eeprom[0x08] & self_powered);
ee->max_power = eeprom[0x09]; /* Units of 2mA */
/* Device and perhiperal control */
ee->suspend_pull_down = (eeprom[0x0A] & suspend_pull_down);
ee->serial_number_avail = (eeprom[0x0A] & serial_number_avail);
ee->ft1248_cpol = (eeprom[0x0A] & ft1248_cpol);
ee->ft1248_bord = (eeprom[0x0A] & ft1248_bord);
ee->ft1248_flow_control = (eeprom[0x0A] & ft1248_flow_control);
ee->disable_i2c_schmitt = (eeprom[0x0A] & disable_i2c_schmitt);
ee->invert_txd = (eeprom[0x0B] & invert_txd);
ee->invert_rxd = (eeprom[0x0B] & invert_rxd);
ee->invert_rts = (eeprom[0x0B] & invert_rts);
ee->invert_cts = (eeprom[0x0B] & invert_cts);
ee->invert_dtr = (eeprom[0x0B] & invert_dtr);
ee->invert_dsr = (eeprom[0x0B] & invert_dsr);
ee->invert_dcd = (eeprom[0x0B] & invert_dcd);
ee->invert_ri = (eeprom[0x0B] & invert_ri);
/* DBUS & CBUS Control */
ee->dbus_drive_strength = (eeprom[0x0C] & dbus_drive_strength);
ee->dbus_slow_slew = (eeprom[0x0C] & dbus_slow_slew);
ee->dbus_schmitt = (eeprom[0x0C] & dbus_schmitt);
ee->cbus_drive_strength = (eeprom[0x0C] & cbus_drive_strength) >> 4;
ee->cbus_slow_slew = (eeprom[0x0C] & cbus_slow_slew);
ee->cbus_schmitt = (eeprom[0x0C] & cbus_schmitt);
/* eeprom[0x0D] is unused */
/* Manufacturer, Product and Serial Number string */
ee->manufacturer_string = ee_decode_string(eeprom,
eeprom+eeprom[0x0E], eeprom[0x0F]);
ee->product_string = ee_decode_string(eeprom,
eeprom+eeprom[0x10], eeprom[0x11]);
ee->serial_string = ee_decode_string(eeprom,
eeprom+eeprom[0x12], eeprom[0x13]);
/* I2C */
ee->i2c_slave_addr = eeprom[0x14] | (eeprom[0x15] << 8);
ee->i2c_device_id = eeprom[0x16] | (eeprom[0x17] << 8) | (eeprom[0x18] << 16);
/* CBUS */
for (c = 0; c < CBUS_COUNT; c++) {
ee->cbus[c] = eeprom[0x1A + c];
}
/* User Memory Space */
memcpy(ee->user_mem, &eeprom[0x24], 92);
/* Factory Configuration Values */
memcpy(ee->factory_config, &eeprom[0x80], 32);
}
/* ------------ Help ------------ */
static const char *myname;
/**
* Prints a human-readable expression showing all the possibilities
* for an option.
*/
static void print_options(FILE *fp, const char** options)
{
int j;
fprintf(fp, " [");
for (j = 0; options[j];) {
fprintf(fp, "%s", options[j]);
if (options[++j])
fprintf(fp, "|");
}
fprintf(fp, "]");
}
/*
* Prints a human readable help text.
*/
static void show_help (FILE *fp)
{
int i;
fprintf(fp, "\nUsage: %s [<arg> <val>]..\n", myname);
fprintf(fp, "\nwhere <arg> must be any of:\n");
for (i = 0; arg_type_strings[i]; ++i) { /* For each argument */
/* Get the help string */
const char *val = arg_type_help[i];
/* Print its name */
fprintf(fp, " %s", arg_type_strings[i]);
if (val) { /* If there is a help string */
if (strcmp(val, "[cbus]") == 0) {
fprintf(fp, " [1..%d]", CBUS_COUNT);
print_options(fp, cbus_mode_strings);
} else if (strcmp(val, "[invert]") == 0) {
print_options(fp, rs232_strings);
} else if (strcmp(val, "[noinvert]") == 0) {
print_options(fp, rs232_strings);
} else if (strcmp(val, "dbus_cfg") == 0) {
print_options(fp, d_cbus_config_strings);
} else if (strcmp(val, "cbus_cfg") == 0) {
print_options(fp, d_cbus_config_strings);
} else {
fprintf(fp, " %s", val);
}
}
fputc('\n', fp);
}
fputc('\n', fp);
}
/* ------------ EEPROM Reading and Writing ------------ */
static int ee_prepare_write(void)
{
unsigned short status;
int ret;
/* These commands were traced while running MProg */
if ((ret = ftdi_usb_reset(&ftdi)) != 0) { return ret; }
if ((ret = ftdi_poll_modem_status(&ftdi, &status)) != 0) { return ret; }
if ((ret = ftdi_set_latency_timer(&ftdi, 0x77)) != 0) { return ret; }
return 0;
}
static int ee_write(unsigned char *eeprom, int len)
{
int i;
if (ee_prepare_write()) {
fprintf(stderr, "ee_prepare_write() failed: %s\n",
ftdi_get_error_string(&ftdi));
exit(EIO);
}
for (i = 0; i < len/2; i++) {
if (ftdi_write_eeprom_location(&ftdi, i,
eeprom[i*2] | (eeprom[(i*2)+1] << 8))) {
fprintf(stderr, "ftdi_write_eeprom_location() failed: %s\n",
ftdi_get_error_string(&ftdi));
exit(EIO);
}
}
return 0;
}
static unsigned short ee_read_and_verify (unsigned char *eeprom, int len)
{
int i;
for (i = 0; i < len/2; i++) {
if (ftdi_read_eeprom_location(&ftdi, i, (void*)(eeprom + (i*2)))) {
fprintf(stderr, "ftdi_read_eeprom_location() failed: %s\n",
ftdi_get_error_string(&ftdi));
exit(EIO);
}
}
return verify_crc(eeprom, len);
}
/* ------------ Parsing Command Line ------------ */
static int match_arg (const char *arg, const char **possibles)
{
int i;
for (i = 0; possibles[i]; ++i) {
if (0 == strcasecmp(possibles[i], arg))
return i;
}
fprintf(stderr, "unrecognized arg: \"%s\"\n", arg);
exit(EINVAL);
return -1; /* never reached */
}