comparison fteeprom/fteeprom-prog.c @ 0:11b8a30333b3

fteeprom: initial import from freecalypso-hwlab
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Sep 2023 18:08:22 +0000
parents
children 43b8e88dae02
comparison
equal deleted inserted replaced
-1:000000000000 0:11b8a30333b3
1 #include <sys/types.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <ftdi.h>
9
10 unsigned eeprom_size;
11 u_short eeprom[256];
12
13 read_eeprom_common(inf, filename_for_errs)
14 FILE *inf;
15 char *filename_for_errs;
16 {
17 char linebuf[1024], *cp;
18 int lineno, rc;
19 unsigned ptr = 0, input_off;
20
21 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
22 for (cp = linebuf; isspace(*cp); cp++)
23 ;
24 if (*cp == '\0' || *cp == '#')
25 continue;
26 if (ptr >= 256) {
27 fprintf(stderr,
28 "%s line %d: maximum EEPROM size exceeded\n",
29 filename_for_errs, lineno);
30 exit(1);
31 }
32 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
33 &input_off, eeprom + ptr, eeprom + ptr + 1,
34 eeprom + ptr + 2, eeprom + ptr + 3,
35 eeprom + ptr + 4, eeprom + ptr + 5,
36 eeprom + ptr + 6, eeprom + ptr + 7);
37 if (rc != 9 || input_off != ptr * 2) {
38 fprintf(stderr, "%s line %d: invalid input\n",
39 filename_for_errs, lineno);
40 exit(1);
41 }
42 ptr += 8;
43 }
44 if (ptr != 64 && ptr != 128 && ptr != 256) {
45 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
46 filename_for_errs);
47 exit(1);
48 }
49 eeprom_size = ptr;
50 }
51
52 read_eeprom_from_file(filename)
53 char *filename;
54 {
55 FILE *inf;
56
57 inf = fopen(filename, "r");
58 if (!inf) {
59 perror(filename);
60 exit(1);
61 }
62 read_eeprom_common(inf, filename);
63 fclose(inf);
64 }
65
66 read_eeprom_from_stdin()
67 {
68 read_eeprom_common(stdin, "stdin");
69 }
70
71 main(argc, argv)
72 char **argv;
73 {
74 struct ftdi_context ftdi;
75 u_short modem_status;
76 unsigned n;
77
78 if (argc < 2 || argc > 3) {
79 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n",
80 argv[0]);
81 exit(1);
82 }
83 if (argv[2])
84 read_eeprom_from_file(argv[2]);
85 else
86 read_eeprom_from_stdin();
87 ftdi_init(&ftdi);
88 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) {
89 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
90 exit(1);
91 }
92 /* magic sequence apparently required for FT232R */
93 if (ftdi_usb_reset(&ftdi) < 0) {
94 fprintf(stderr, "ftdi_usb_reset() failed: %s\n",
95 ftdi.error_str);
96 exit(1);
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);
117 }