comparison fteeprom/fteeprom-prog.c @ 20:43b8e88dae02

fteeprom-prog: convert to new local libs
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 08 Sep 2023 22:40:14 +0000
parents 11b8a30333b3
children 7e6dcceb5ee8
comparison
equal deleted inserted replaced
19:d420375243c9 20:43b8e88dae02
1 /*
2 * This program connects to an FTDI chip via libusb and programs its EEPROM
3 * with a raw hex image read from stdin or from a backup file.
4 *
5 * The present version has been converted to use our local libraries
6 * (libftmini and libuwrap) instead of libftdi - thus the external dependency
7 * is only libusb.
8 */
9
1 #include <sys/types.h> 10 #include <sys/types.h>
2 #include <ctype.h> 11 #include <ctype.h>
3 #include <string.h> 12 #include <string.h>
4 #include <strings.h> 13 #include <strings.h>
5 #include <stdio.h> 14 #include <stdio.h>
6 #include <stdlib.h> 15 #include <stdlib.h>
7 #include <unistd.h> 16 #include <unistd.h>
8 #include <ftdi.h> 17 #include <usb.h>
18 #include "../libuwrap/find_dev.h"
19 #include "../libuwrap/open_close.h"
20 #include "../libftmini/eeprom_func.h"
9 21
10 unsigned eeprom_size; 22 unsigned eeprom_size;
11 u_short eeprom[256]; 23 u_short eeprom[256];
12 24
13 read_eeprom_common(inf, filename_for_errs) 25 read_eeprom_common(inf, filename_for_errs)
69 } 81 }
70 82
71 main(argc, argv) 83 main(argc, argv)
72 char **argv; 84 char **argv;
73 { 85 {
74 struct ftdi_context ftdi; 86 struct usb_device *dev;
75 u_short modem_status; 87 usb_dev_handle *usbh;
76 unsigned n; 88 unsigned n;
77 89
78 if (argc < 2 || argc > 3) { 90 if (argc < 2 || argc > 3) {
79 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n", 91 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n",
80 argv[0]); 92 argv[0]);
82 } 94 }
83 if (argv[2]) 95 if (argv[2])
84 read_eeprom_from_file(argv[2]); 96 read_eeprom_from_file(argv[2]);
85 else 97 else
86 read_eeprom_from_stdin(); 98 read_eeprom_from_stdin();
87 ftdi_init(&ftdi); 99 dev = find_usbdev_by_desc_string(argv[1]);
88 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) { 100 if (!dev) {
89 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); 101 fprintf(stderr, "error: specified USB device not found\n");
90 exit(1); 102 exit(1);
91 } 103 }
92 /* magic sequence apparently required for FT232R */ 104 usbh = usbwrap_open_dev(dev, 1);
93 if (ftdi_usb_reset(&ftdi) < 0) { 105 /* FIXME: reimplement FT232R magic sequence */
94 fprintf(stderr, "ftdi_usb_reset() failed: %s\n", 106 for (n = 0; n < eeprom_size; n++)
95 ftdi.error_str); 107 ftmini_write_eeprom_loc(usbh, n, eeprom[n]);
96 exit(1); 108 usbwrap_close_dev(usbh);
97 }
98 if (ftdi_poll_modem_status(&ftdi, &modem_status) < 0) {
99 fprintf(stderr, "ftdi_poll_modem_status() failed: %s\n",
100 ftdi.error_str);
101 exit(1);
102 }
103 if (ftdi_set_latency_timer(&ftdi, 0x77) < 0) {
104 fprintf(stderr, "ftdi_set_latency_timer() failed: %s\n",
105 ftdi.error_str);
106 exit(1);
107 }
108 for (n = 0; n < eeprom_size; n++) {
109 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
110 fprintf(stderr, "EEPROM write error: %s\n",
111 ftdi.error_str);
112 exit(1);
113 }
114 }
115 ftdi_usb_close(&ftdi);
116 exit(0); 109 exit(0);
117 } 110 }