comparison fteeprom/test/fteeprom-prog-bad.c @ 1:ab2bb12ec959

fteeprom/test: import from freecalypso-hwlab
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Sep 2023 18:09:28 +0000
parents
children
comparison
equal deleted inserted replaced
0:11b8a30333b3 1:ab2bb12ec959
1 /*
2 * This "bad" version of fteeprom-prog is being presented in order to show
3 * what happens on FT232R chips when a certain magic sequence is omitted.
4 */
5
6 #include <sys/types.h>
7 #include <ctype.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <ftdi.h>
14
15 unsigned eeprom_size;
16 u_short eeprom[256];
17
18 read_eeprom_common(inf, filename_for_errs)
19 FILE *inf;
20 char *filename_for_errs;
21 {
22 char linebuf[1024], *cp;
23 int lineno, rc;
24 unsigned ptr = 0, input_off;
25
26 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
27 for (cp = linebuf; isspace(*cp); cp++)
28 ;
29 if (*cp == '\0' || *cp == '#')
30 continue;
31 if (ptr >= 256) {
32 fprintf(stderr,
33 "%s line %d: maximum EEPROM size exceeded\n",
34 filename_for_errs, lineno);
35 exit(1);
36 }
37 rc = sscanf(linebuf, "%x: %hx %hx %hx %hx %hx %hx %hx %hx",
38 &input_off, eeprom + ptr, eeprom + ptr + 1,
39 eeprom + ptr + 2, eeprom + ptr + 3,
40 eeprom + ptr + 4, eeprom + ptr + 5,
41 eeprom + ptr + 6, eeprom + ptr + 7);
42 if (rc != 9 || input_off != ptr * 2) {
43 fprintf(stderr, "%s line %d: invalid input\n",
44 filename_for_errs, lineno);
45 exit(1);
46 }
47 ptr += 8;
48 }
49 if (ptr != 64 && ptr != 128 && ptr != 256) {
50 fprintf(stderr, "%s: not an EEPROM image of recognized size\n",
51 filename_for_errs);
52 exit(1);
53 }
54 eeprom_size = ptr;
55 }
56
57 read_eeprom_from_file(filename)
58 char *filename;
59 {
60 FILE *inf;
61
62 inf = fopen(filename, "r");
63 if (!inf) {
64 perror(filename);
65 exit(1);
66 }
67 read_eeprom_common(inf, filename);
68 fclose(inf);
69 }
70
71 read_eeprom_from_stdin()
72 {
73 read_eeprom_common(stdin, "stdin");
74 }
75
76 main(argc, argv)
77 char **argv;
78 {
79 struct ftdi_context ftdi;
80 u_short modem_status;
81 unsigned n;
82
83 if (argc < 2 || argc > 3) {
84 fprintf(stderr, "usage: %s device-selector [eeprom-image]\n",
85 argv[0]);
86 exit(1);
87 }
88 if (argv[2])
89 read_eeprom_from_file(argv[2]);
90 else
91 read_eeprom_from_stdin();
92 ftdi_init(&ftdi);
93 if (ftdi_usb_open_string(&ftdi, argv[1]) < 0) {
94 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str);
95 exit(1);
96 }
97 #if 0 /* FT232R magic sequence omitted! */
98 if (ftdi_usb_reset(&ftdi) < 0) {
99 fprintf(stderr, "ftdi_usb_reset() failed: %s\n",
100 ftdi.error_str);
101 exit(1);
102 }
103 if (ftdi_poll_modem_status(&ftdi, &modem_status) < 0) {
104 fprintf(stderr, "ftdi_poll_modem_status() failed: %s\n",
105 ftdi.error_str);
106 exit(1);
107 }
108 if (ftdi_set_latency_timer(&ftdi, 0x77) < 0) {
109 fprintf(stderr, "ftdi_set_latency_timer() failed: %s\n",
110 ftdi.error_str);
111 exit(1);
112 }
113 #endif
114 for (n = 0; n < eeprom_size; n++) {
115 if (ftdi_write_eeprom_location(&ftdi, n, eeprom[n]) < 0) {
116 fprintf(stderr, "EEPROM write error: %s\n",
117 ftdi.error_str);
118 exit(1);
119 }
120 }
121 ftdi_usb_close(&ftdi);
122 exit(0);
123 }