ftdi_read_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  */
41 /*****************************************************************************
42  ** Includes
43  *****************************************************************************/
44 
45 #include <ftdi.h>
46 #include <cstring>
47 #include <ecl/command_line.hpp>
48 
49 /*****************************************************************************
50 ** Preprocessor
51 *****************************************************************************/
52 
53 #define REQUEST_EEPROM_SIZE 0
54 
55 /*****************************************************************************
56  ** Using
57  *****************************************************************************/
58 
59 using ecl::CmdLine;
61 using ecl::ValueArg;
62 using ecl::SwitchArg;
63 using std::string;
64 
65 /*****************************************************************************
66 ** Functions
67 *****************************************************************************/
68 
69 bool decode(unsigned char* eeprom_binary, const int &size) {
70  ftdi_eeprom eeprom;
71  std::cout << " Decoding into eeprom structure." << std::endl;
72  // put the binary into an eeprom structure
73  if ( ftdi_eeprom_decode(&eeprom, eeprom_binary, size) != 0 ) {
74  return false;
75  }
76  std::cout << "Eeprom:" << std::endl;
77  std::cout << " Manufacturer: " << eeprom.manufacturer << std::endl;
78  std::cout << " Product : " << eeprom.product << std::endl;
79  std::cout << " Vendor Id : " << eeprom.vendor_id << std::endl;
80  std::cout << " Product Id : " << eeprom.product_id << std::endl;
81  std::cout << " Serial Id : " << eeprom.serial << std::endl;
82  std::cout << " Self Powered: " << eeprom.self_powered << std::endl;
83  std::cout << " Remote Wake : " << eeprom.remote_wakeup << std::endl;
84  std::cout << " Use Serial : " << eeprom.use_serial << std::endl;
85  std::cout << " In Iso : " << eeprom.in_is_isochronous << std::endl;
86  std::cout << " Out Iso : " << eeprom.out_is_isochronous << std::endl;
87  std::cout << " Suspend : " << eeprom.suspend_pull_downs << std::endl;
88  std::cout << " Max Power : " << eeprom.max_power << std::endl;
89  std::cout << " Chip Type : " << eeprom.chip_type << std::endl;
90  return true;
91 }
92 /*****************************************************************************
93  ** Main
94  *****************************************************************************/
95 
96 int main(int argc, char **argv)
97 {
98  const int vendor_id = 0x0403;
99  const int product_id = 0x6001;
100 
101  /*********************
102  ** Parse Command Line
103  **********************/
104  CmdLine cmd_line("This is used to read an eeprom on an ftdi device.", ' ', "0.1");
105  ValueArg<std::string> serial_arg(
106  "s", "serial", "Identify the device via the old serial id (if there are multiple attached) ['unspecified'].", false,
107  "unspecified", "string");
108  cmd_line.add(serial_arg);
109  cmd_line.parse(argc, argv);
110  bool using_serial_id = false;
111  string serial_id;
112  if (serial_arg.getValue() != "unspecified")
113  {
114  using_serial_id = true;
115  serial_id = serial_arg.getValue();
116  }
117 
118  /*********************
119  ** Debug output
120  **********************/
121  std::cout << "Input Information" << std::endl;
122  if (!using_serial_id)
123  {
124  std::cout << " Serial id: unused (first device found.)" << std::endl;
125  }
126  else
127  {
128  std::cout << " Serial id: " << serial_id << std::endl;
129  }
130 
131  /*********************
132  ** Open a context
133  **********************/
134  struct ftdi_context ftdi;
135  if (ftdi_init(&ftdi) < 0)
136  {
137  std::cerr << "ftdi_init failed" << std::endl;
138  return EXIT_FAILURE;
139  }
140  if (!using_serial_id)
141  { // simply open up the first found.
142  if (ftdi_usb_open(&ftdi, vendor_id, product_id) != 0)
143  {
144  std::cerr << "Couldn't find/open an ftdi device [" << ftdi.error_str << "]" << std::endl;
145  return EXIT_FAILURE;
146  }
147  }
148  else
149  {
150  if (ftdi_usb_open_desc(&ftdi, vendor_id, product_id, NULL, serial_id.c_str()) < 0)
151  {
152  std::cerr << "Couldn't open the device with serial id string: " << serial_id << std::endl;
153  return EXIT_FAILURE;
154  }
155  }
156  /******************************************
157  ** Eeeprom Binary (Requested size)
158  *******************************************/
159  std::cout << "Eeprom Binary" << std::endl;
160  unsigned char eeprom_binary[512];
161  int size = ftdi_read_eeprom_getsize(&ftdi, eeprom_binary, 512);
162  if (size < 0)
163  {
164  std::cerr << "Error: Could not read the eeprom from the requested device." << std::endl;
165  return EXIT_FAILURE;
166  }
167  std::cout << " Read binary [" << size << " bytes]." << std::endl;
168  std::cout << " Write binary [eeprom.req]." << std::endl;
169  FILE *fp = fopen ("eeprom.req", "wb");
170  fwrite (&eeprom_binary, 1, size, fp);
171  fclose (fp);
172 
173  if ( !decode(eeprom_binary, size) ) {
174  std::cerr << "Error: Could not write raw binary eeprom into the eeprom structure." << std::endl;
175  return EXIT_FAILURE;
176  }
177 
178 #if REQUEST_EEPROM_SIZE
179  /******************************************
180  ** Eeeprom Binary (Fixed size)
181  *******************************************/
182  // this is what the official example does.
183  size = 128;
184  int result = ftdi_read_eeprom(&ftdi,eeprom_binary);
185  if ( result < 0 ) {
186  std::cerr << "Error: Could not read the eeprom from the requested device." << std::endl;
187  return EXIT_FAILURE;
188  }
189  fp = fopen ("eeprom.fix", "wb");
190  fwrite (&eeprom_binary, 1, size, fp);
191  fclose (fp);
192  std::cout << " Write binary [eeprom.fix]." << std::endl;
193 
194  if ( !decode(eeprom_binary, size) ) {
195  std::cerr << "Error: Could not write raw binary eeprom into the eeprom structure." << std::endl;
196  return EXIT_FAILURE;
197  }
198 #endif
199  std::cout << "Done." << std::endl;
200 
201  return 0;
202 }
203 
void add(Arg &a)
int main(int argc, char **argv)
void parse(int argc, char **argv)
bool decode(unsigned char *eeprom_binary, const int &size)


kobuki_ftdi
Author(s): Younghun Ju
autogenerated on Mon Feb 28 2022 22:37:20