ftdi_write_eeprom.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Yujin Robot.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Yujin Robot nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
40 /*****************************************************************************
41  ** Includes
42  *****************************************************************************/
43 
44 #include <ftdi.h>
45 #include <cstring>
46 #include <ecl/command_line.hpp>
47 
48 /*****************************************************************************
49  ** Main
50  *****************************************************************************/
51 
52 int main(int argc, char **argv)
53 {
54  const int vendor_id = 0x0403;
55  const int product_id = 0x6001;
56  int result;
57 
58  /*********************
59  ** Parse Command Line
60  **********************/
61  ecl::CmdLine cmd_line("This is used to write 'kobuki' on an ftdi device.", ' ', "0.1");
62  ecl::ValueArg<std::string> serial_arg(
63  "s", "serial", "Identify the device via the old serial id (if there are multiple attached) ['unspecified'].", false,
64  "unspecified", "string");
65  ecl::ValueArg<std::string> filename_arg(
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")
74  {
75  using_serial_id = true;
76  serial_id = serial_arg.getValue();
77  }
78  std::string filename = filename_arg.getValue();
79 
80  /*********************
81  ** Debug output
82  **********************/
83  std::cout << "Input Information" << std::endl;
84  if (!using_serial_id)
85  {
86  std::cout << " Serial id: unused (first device found.)" << std::endl;
87  }
88  else
89  {
90  std::cout << " Serial id: " << serial_id << std::endl;
91  }
92 
93  /*********************
94  ** Open a context
95  **********************/
96  struct ftdi_context ftdi;
97  if (ftdi_init(&ftdi) < 0)
98  {
99  std::cerr << "ftdi_init failed" << std::endl;
100  return EXIT_FAILURE;
101  }
102  if (!using_serial_id)
103  { // simply open up the first found.
104  if (ftdi_usb_open(&ftdi, vendor_id, product_id) != 0)
105  {
106  std::cerr << "Couldn't find/open an ftdi device [" << ftdi.error_str << "]" << std::endl;
107  return EXIT_FAILURE;
108  }
109  }
110  else
111  {
112  if (ftdi_usb_open_desc(&ftdi, vendor_id, product_id, NULL, serial_id.c_str()) < 0)
113  {
114  std::cerr << "Couldn't open the device with serial id string: " << serial_id << std::endl;
115  return EXIT_FAILURE;
116  }
117  }
118 
119 
120  /******************************************
121  ** Load Eeeprom Binary
122  *******************************************/
123  std::cout << "Load eeprom binary" << std::endl;
124  unsigned char eeprom_binary[512];
125  FILE *fp = fopen(filename.c_str(), "rb");
126  if ( fp == NULL ) {
127  std::cerr << "Error: could not read the eeprom binary file." << std::endl;
128  return EXIT_FAILURE;
129  }
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;
133  } else {
134  std::cout << " Size: " << n << " bytes" << std::endl;
135  }
136  fclose (fp);
137 
138  /*********************
139  ** Erasing eeprom
140  **********************/
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;
145  return EXIT_FAILURE;
146  } else if ( result == -2 ) {
147  std::cerr << "Error: usb device unavailable." << std::endl;
148  return EXIT_FAILURE;
149  } else {
150  std::cout << " Ok" << std::endl;
151  }
152 
153  /*********************
154  ** Flashing Eeprom
155  **********************/
156  std::cout << "Flashing eeprom" << std::endl;
157  result = ftdi_write_eeprom(&ftdi, eeprom_binary);
158  if (result < 0)
159  {
160  std::cerr << "Error : write failed [" << ftdi_get_error_string(&ftdi) << "]" << std::endl;
161  return EXIT_FAILURE;
162  } else {
163  std::cout << " Ok" << std::endl;
164  }
165 
166  /*********************
167  ** Cleanup
168  **********************/
169  std::cout << "Done." << std::endl;
170 
171  return 0;
172 }
173 
command_line.hpp
ecl::CmdLine
ecl::ValueArg::getValue
T & getValue()
ecl::CmdLine::add
void add(Arg &a)
main
int main(int argc, char **argv)
Definition: ftdi_write_eeprom.cpp:52
ecl::ValueArg
ecl::CmdLine::parse
void parse(int argc, char **argv)


kobuki_ftdi
Author(s): Younghun Ju
autogenerated on Wed Mar 2 2022 00:26:19