annotate fteeprom/test/fteeprom-prog-bad.c @ 16:1d76deae1e74

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