52 int main(
int argc,
char **argv)
54 const int vendor_id = 0x0403;
55 const int product_id = 0x6001;
61 ecl::CmdLine cmd_line(
"This is used to write 'roch' on an ftdi device.",
' ',
"0.1");
63 "s",
"serial",
"Identify the device via the old serial id (if there are multiple attached) ['unspecified'].",
false,
64 "unspecified",
"string");
66 "f",
"filename",
"Name of the eeprom binary. ['eeprom.new'].",
false,
67 "eeprom.new",
"string");
68 cmd_line.
add(serial_arg);
69 cmd_line.
add(filename_arg);
70 cmd_line.
parse(argc, argv);
71 bool using_serial_id =
false;
72 std::string serial_id;
73 if (serial_arg.
getValue() !=
"unspecified")
75 using_serial_id =
true;
78 std::string filename = filename_arg.
getValue();
83 std::cout <<
"Input Information" << std::endl;
86 std::cout <<
" Serial id: unused (first device found.)" << std::endl;
90 std::cout <<
" Serial id: " << serial_id << std::endl;
96 struct ftdi_context ftdi;
97 if (ftdi_init(&ftdi) < 0)
99 std::cerr <<
"ftdi_init failed" << std::endl;
102 if (!using_serial_id)
104 if (ftdi_usb_open(&ftdi, vendor_id, product_id) != 0)
106 std::cerr <<
"Couldn't find/open an ftdi device [" << ftdi.error_str <<
"]" << std::endl;
112 if (ftdi_usb_open_desc(&ftdi, vendor_id, product_id, NULL, serial_id.c_str()) < 0)
114 std::cerr <<
"Couldn't open the device with serial id string: " << serial_id << std::endl;
123 std::cout <<
"Load eeprom binary" << std::endl;
124 unsigned char eeprom_binary[512];
125 FILE *fp = fopen(filename.c_str(),
"rb");
127 std::cerr <<
"Error: could not read the eeprom binary file." << std::endl;
130 size_t n = fread(eeprom_binary,1,512,fp);
131 if ( ( n != 512 ) && ( !feof(fp) ) ) {
132 std::cerr <<
"Error: failed read from eeoprom binary file" << std::endl;
134 std::cout <<
" Size: " << n <<
" bytes" << std::endl;
141 std::cout <<
"Erasing eeprom" << std::endl;
142 result = ftdi_erase_eeprom(&ftdi);
143 if ( result == -1 ) {
144 std::cerr <<
"Error: erase failed." << std::endl;
146 }
else if ( result == -2 ) {
147 std::cerr <<
"Error: usb device unavailable." << std::endl;
150 std::cout <<
" Ok" << std::endl;
156 std::cout <<
"Flashing eeprom" << std::endl;
157 result = ftdi_write_eeprom(&ftdi, eeprom_binary);
160 std::cerr <<
"Error : write failed [" << ftdi_get_error_string(&ftdi) <<
"]" << std::endl;
163 std::cout <<
" Ok" << std::endl;
169 std::cout <<
"Done." << std::endl;
int main(int argc, char **argv)
void parse(int argc, char **argv)