Debian Codegraph @

examples/purge_test.c

raw symbols SCIP
1 /* purge_test.c
2 *
3 * Test for purge TX/RX functions.
4 *
5 * The chip must be wired to loop TX data to RX data (loopback).
6 *
7 * This program works with "standard" linux drivers and the FTDI1 library.
8 *
9 * Usage: purge_test [-b baud] [-i interface] [-n msg-size] [-N note] device-specifier
10 * See usage below for more information on command usage.
11 *
12 * This program works well with the FT4231H which is newer and has large
13 * FIFOs. This program does not work well with FT232, either pre or post
14 * switching the SIO_RESET_PURGE_TX/SIO_RESET_PURGE_RX values.
15 *
16 * This needs testing with other devices, which I do not have.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <signal.h>
25 #include <errno.h>
26 /* Prevent deprecated messages when building library */
27 #define _FTDI_DISABLE_DEPRECATEDcxx . . $ `examples/purge_test.c:27:9`!
28 #include <ftdi.h>cxx . . $ `<file>/src/ftdi.h`/
29
30 #include <termios.h> // For baudcodes & linux UARTs
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34
35
36 static struct ftdi_contextcxx . . $ ftdi_context# *ftdicxx . . $ ftdi. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
37 static int dev_fdcxx . . $ dev_fd. = -1;
38 static char * dev_stringcxx . . $ dev_string. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
39 static int latency_specifiedcxx . . $ latency_specified. = 0;
40 static int latencycxx . . $ latency. = 5;
41 static int baudcxx . . $ baud. = 9600;
42 static int baud_codecxx . . $ baud_code. = -1;
43 static enum ftdi_interfacecxx . . $ ftdi_interface# interfacecxx . . $ interface. = INTERFACE_Acxx . . $ INTERFACE_A.;
44 static int msg_sizecxx . . $ msg_size. = 80;
45 static int broken_purge_testcxx . . $ broken_purge_test. = 0;
46
47 static const int latency_mincxx . . $ latency_min. = 2;
48 static const int latency_maxcxx . . $ latency_max. = 255;
49
50 static volatile long long usec_test_startcxx . . $ usec_test_start.;
51
52
53
54 static int ascii2intcxx . . $ ascii2int(5ae29d726ae57f88).(const char * str, const char * pgm_name);
55 static int baud_2_baud_codecxx . . $ baud_2_baud_code(7864480464b09eea).(int baud);
56 static long int char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(int char_count);
57 static long int draincxx . . $ drain(2ebcc2597f7e891d).();
58 static int flushcxx . . $ flush(7864480464b09eea).(int queue_selector);
59 static long long int get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).();
60
61 static const int flushQueueSelectorcxx . . $ flushQueueSelector.[] = {
62 TCIFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:76:9`!, TCOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:77:9`!, TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`! }; /* See /usr/include/bits/termios.h */
63 static const char * flushTestNamecxx . . $ flushTestName.[] = {
64 "Input-only", "Output-only", "Input+Output" };
65 static const char * expectedcxx . . $ expected.[] = {
66 "last portion of message",
67 "first portion of message",
68 "mid-message characters",
69 };
70
71
72 static const char * chip_typescxx . . $ chip_types.[] = {
73 "am",
74 "bm",
75 "2232C",
76 "R",
77 "2232H",
78 "4232H",
79 "232H",
80 "230X",
81 };
82
83 #ifndef ARRAY_SIZE
84 # define ARRAY_SIZEcxx . . $ `examples/purge_test.c:84:11`!(x) (sizeof(x)/sizeof(x[0]))
85 #endif
86
87
88
89 /**********************************************************************
90 */
91 static void
92 usagecxx . . $ usage(85c52e162fed56f9).(const char *argv0)
93 {
94 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,
95 "Usage: %s [options...] device-specifier\n"
96 "Flush test for UARTS.\n"
97 " with loopback connector\n"
98 " [-b baud] baud rate (e.g., 300, 600, 1200, ...230400)\n"
99 " [-i {a|b|c|d}] FTDI interface for chips which have multiple UARTS\n"
100 " [-l latency] Latency (%d..%d)\n"
101 " [-n msg-size] Number of bytes in test message\n"
102 " [-N note] Note for the output\n"
103 " [-P] Use broken libftdi1 purge methods (over new flush)\n"
104 "\n"
105 " device-specifier String specifying the UART. If the first character\n"
106 " is the '/' character, the program assumes a Linux UART\n"
107 " is to be tested and the string would be something like\n"
108 " '/dev/ttyS0' or '/dev/ttyUSB0'. Otherwise, the program\n"
109 " assumes an FTDI device is being tested with the FTDI1\n"
110 " library. The device-specifier must be a string\n"
111 " accepted by the ftdi_usb_open_string function. An\n"
112 " example would be 'i:0x0403:0x6011[:index]'.\n"
113 "\n"
114 "NOTE: To function correctly, this program requires a loopback connector\n"
115 " attached to the UART under test.\n"
116 "\n"
117 "Adapted from stream_test.c 2018. Eric Schott <[email protected]>\n"
118 "Copyright (C) 2009 Micah Dowty <[email protected]>\n"
119 "Adapted for use with libftdi (C) 2010 Uwe Bonnes <[email protected]>\n",
120 argv0, latency_mincxx . . $ latency_min., latency_maxcxx . . $ latency_max.);
121 exitcxx . . $ exit(3ca292c338df9bb3).(1);
122 }
123
124
125 /**********************************************************************
126 */
127 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
128 {
129 int c, i;
130 int option_index;
131 int test;
132 unsigned char * msg;
133 unsigned char * retMsg;
134 char * note = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
135 char * note_default = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
136 size_t retMsgSize;
137 long int msg_xmit_time_us;
138 static struct optioncxx . . $ option# long_options[] = {{NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!},};
139
140 while ((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "n:b:i:l:N:P", long_options, &option_index)) !=- 1)
141 switch (c)
142 {
143 case -1:
144 break;
145 case 'b':
146 baudcxx . . $ baud. = ascii2intcxx . . $ ascii2int(5ae29d726ae57f88).(optargcxx . . $ optarg., argv[0]);
147 break;
148 case 'i':
149 if (optargcxx . . $ optarg. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || strlencxx . . $ strlen(751346f8406fd082).(optargcxx . . $ optarg.) != 1)
150 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
151 switch (optargcxx . . $ optarg.[0])
152 {
153 case 'a':
154 case 'A':
155 interfacecxx . . $ interface. = INTERFACE_Acxx . . $ INTERFACE_A.;
156 break;
157
158 case 'b':
159 case 'B':
160 interfacecxx . . $ interface. = INTERFACE_Bcxx . . $ INTERFACE_B.;
161 break;
162
163 case 'c':
164 case 'C':
165 interfacecxx . . $ interface. = INTERFACE_Ccxx . . $ INTERFACE_C.;
166 break;
167
168 case 'd':
169 case 'D':
170 interfacecxx . . $ interface. = INTERFACE_Dcxx . . $ INTERFACE_D.;
171 break;
172
173 default:
174 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
175 }
176 break;
177 case 'l':
178 latencycxx . . $ latency. = ascii2intcxx . . $ ascii2int(5ae29d726ae57f88).(optargcxx . . $ optarg., argv[0]);
179 if (latencycxx . . $ latency. < latency_mincxx . . $ latency_min. || latencycxx . . $ latency. > latency_maxcxx . . $ latency_max.)
180 {
181 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "latency [-l] must be an integer in the range %d..%d\n",
182 latency_mincxx . . $ latency_min., latency_maxcxx . . $ latency_max.);
183 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
184 }
185 latency_specifiedcxx . . $ latency_specified. = 1;
186 break;
187 case 'n':
188 msg_sizecxx . . $ msg_size. = ascii2intcxx . . $ ascii2int(5ae29d726ae57f88).(optargcxx . . $ optarg., argv[0]);
189 if (msg_sizecxx . . $ msg_size. < 1)
190 {
191 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "msg-size [-n] must be an integer greater than 0\n");
192 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
193 }
194 break;
195 case 'N':
196 note = optargcxx . . $ optarg.;
197 break;
198 case 'P':
199 broken_purge_testcxx . . $ broken_purge_test. = 1;
200 break;
201 default:
202 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
203 }
204
205 if (optindcxx . . $ optind. == argc)
206 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
207
208 if (optindcxx . . $ optind. == argc - 1)
209 {
210 // Exactly one extra argument- a dump file
211 dev_stringcxx . . $ dev_string. = argv[optindcxx . . $ optind.];
212 }
213 else if (optindcxx . . $ optind. < argc)
214 {
215 // Too many extra args
216 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
217 }
218
219 baud_codecxx . . $ baud_code. = baud_2_baud_codecxx . . $ baud_2_baud_code(7864480464b09eea).(baudcxx . . $ baud.);
220 if (baud_codecxx . . $ baud_code. < 1)
221 {
222 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Invalid baud [-b]\n");
223 usagecxx . . $ usage(85c52e162fed56f9).(argv[0]);
224 }
225
226 if (dev_stringcxx . . $ dev_string.[0] == '/')
227 {
228 struct termioscxx . . $ termios# termios;
229
230 if (latency_specifiedcxx . . $ latency_specified.) {
231 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Latency (-l) option not support on this device; ignored\n");
232 }
233
234 if (broken_purge_testcxx . . $ broken_purge_test.) {
235 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Broken-purge (-P) option not support with Linux kernel driver\n");
236 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
237 }
238
239 dev_fdcxx . . $ dev_fd. = opencxx . . $ open(e156e3aa1e5c4ad1).(dev_stringcxx . . $ dev_string., O_NOCTTYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:53:10`! | O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!);
240 if (dev_fdcxx . . $ dev_fd. < 0)
241 {
242 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Error opening Linux device \"%s\": %s\n",
243 dev_stringcxx . . $ dev_string., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
244 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
245 }
246
247 if (! isattycxx . . $ isatty(7864480464b09eea).(dev_fdcxx . . $ dev_fd.))
248 {
249 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Not a TTY device: \"%s\"\n", dev_stringcxx . . $ dev_string.);
250 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
251 }
252
253 if (tcgetattrcxx . . $ tcgetattr(7da5892ac34fc53f).(dev_fdcxx . . $ dev_fd., &termios) == -1)
254 {
255 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Error getting TTY attributes for \"%s\": %s\n",
256 dev_stringcxx . . $ dev_string., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
257 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
258 }
259
260 note_default = "Linux kernel driver";
261
262 cfmakerawcxx . . $ cfmakeraw(15ae79ea0163f307).(&termios);
263
264 termios.c_cflagcxx . . $ termios#c_cflag. &=
265 ~(CSTOPBcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:29:9`! | CRTSCTScxx . . $ `<external>/934398879100599903/termios-c_cflag.h:39:10`!);
266
267 termios.c_cflagcxx . . $ termios#c_cflag. &= ~CSIZEcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:24:9`!;
268 termios.c_cflagcxx . . $ termios#c_cflag. |= CS8cxx . . $ `<external>/934398879100599903/termios-c_cflag.h:28:11`!;
269
270 cfsetspeedcxx . . $ cfsetspeed(2c6f1e9388fa56a).(&termios, baud_codecxx . . $ baud_code.);
271
272 termios.c_cflagcxx . . $ termios#c_cflag. |=
273 CLOCALcxx . . $ `<external>/934398879100599903/termios-c_cflag.h:34:9`!;
274
275 termios.c_cccxx . . $ termios#c_cc.[VMINcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:30:9`!] = 1; // Character at a time input
276 termios.c_cccxx . . $ termios#c_cc.[VTIMEcxx . . $ `<external>/5505922754558120548/termios-c_cc.h:29:9`!] = 0; // with blocking
277
278 if (tcsetattrcxx . . $ tcsetattr(4c59c899411d127a).(dev_fdcxx . . $ dev_fd., TCSAFLUSHcxx . . $ `<external>/7740429199341890421/termios-tcflow.h:26:9`!, &termios) == -1) {
279 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Error setting TTY attributes for \"%s\": %s\n",
280 dev_stringcxx . . $ dev_string., strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!));
281 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
282 }
283 }
284 else
285 {
286
287 if ((ftdicxx . . $ ftdi. = ftdi_newcxx . . $ ftdi_new(207ff50971a1b30).()) == 0)
288 {
289 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_new failed\n");
290 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
291 }
292
293 if (ftdi_set_interfacecxx . . $ ftdi_set_interface(73d9956508f3a98f).(ftdicxx . . $ ftdi., interfacecxx . . $ interface.) < 0)
294 {
295 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_set_interface failed\n");
296 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
297 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
298 }
299
300 if (ftdi_usb_open_stringcxx . . $ ftdi_usb_open_string(4cc73c67245670e5).(ftdicxx . . $ ftdi., dev_stringcxx . . $ dev_string.) < 0)
301 {
302 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Error opening ftdi device \"%s\": %s\n", dev_stringcxx . . $ dev_string.,
303 ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
304 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
305 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
306 }
307
308 if(ftdi_set_latency_timercxx . . $ ftdi_set_latency_timer(2eccf738de101a49).(ftdicxx . . $ ftdi., (unsigned char) latencycxx . . $ latency.))
309 {
310 if (latency_specifiedcxx . . $ latency_specified. &&
311 (ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. == TYPE_AMcxx . . $ TYPE_AM. || ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. == TYPE_232Hcxx . . $ TYPE_232H.)) {
312 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Latency (-l) option not support on this device; ignored\n");
313 } else if (ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. != TYPE_AMcxx . . $ TYPE_AM. && ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. != TYPE_232Hcxx . . $ TYPE_232H.) {
314 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Error setting latency for ftdi device \"%s\" (%d): %s\n",
315 dev_stringcxx . . $ dev_string., ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type., ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
316 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
317 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
318 }
319 }
320
321 if (ftdi_set_line_property2cxx . . $ ftdi_set_line_property2(1641e47cc6dfb378).(ftdicxx . . $ ftdi., BITS_8cxx . . $ BITS_8., STOP_BIT_1cxx . . $ STOP_BIT_1., NONEcxx . . $ NONE., BREAK_OFFcxx . . $ BREAK_OFF.) < 0)
322 {
323 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Error setting line properties ftdi device \"%s\": %s\n", dev_stringcxx . . $ dev_string.,
324 ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
325 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
326 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
327 }
328
329 if (ftdi_set_baudratecxx . . $ ftdi_set_baudrate(b8d6b357325709d2).(ftdicxx . . $ ftdi., baudcxx . . $ baud.) < 0)
330 {
331 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Error setting baud rate for ftdi device \"%s\": %s\n", dev_stringcxx . . $ dev_string.,
332 ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
333 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
334 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
335 }
336
337 if (ftdi_setflowctrlcxx . . $ ftdi_setflowctrl(b8d6b357325709d2).(ftdicxx . . $ ftdi., SIO_DISABLE_FLOW_CTRLcxx . . $ `src/ftdi.h:225:9`!))
338 {
339 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Error setting flow control for ftdi device \"%s\": %s\n", dev_stringcxx . . $ dev_string.,
340 ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
341 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
342 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
343 }
344
345 if (broken_purge_testcxx . . $ broken_purge_test.)
346 note_default = "libftdi w/ deprecated purge";
347 else
348 note_default = "libftdi w/ new flush methods";
349 }
350
351 printfcxx . . $ printf(581fddd72a8db8fc).("Purge (tcflush) test for device %s\n", dev_stringcxx . . $ dev_string.);
352 printfcxx . . $ printf(581fddd72a8db8fc).("Note: %s\n", (note ? note : note_default));
353
354 if (dev_fdcxx . . $ dev_fd. < 0)
355 {
356 if (ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. >0 && ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type. < ARRAY_SIZEcxx . . $ `examples/purge_test.c:84:11`!(chip_types))
357 printfcxx . . $ printf(581fddd72a8db8fc).("FTDI chip type is %d (%s)\n",
358 ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type., chip_typescxx . . $ chip_types.[ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type.]);
359 else
360 printfcxx . . $ printf(581fddd72a8db8fc).("FTDI chip type is %d (unknown)\n", ftdicxx . . $ ftdi.->typecxx . . $ ftdi_context#type.);
361 }
362
363 printfcxx . . $ printf(581fddd72a8db8fc).("# purge_test" );
364 for (c = 1; c < argc; ++c)
365 {
366 const char *p = argv[c];
367 while (*p != '\0')
368 {
369 if (*p == ' ')
370 break;
371 ++p;
372 }
373 if (*p == ' ')
374 printfcxx . . $ printf(581fddd72a8db8fc).(" '%s'", argv[c]);
375 else
376 printfcxx . . $ printf(581fddd72a8db8fc).(" %s", argv[c]);
377 }
378 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
379
380 msg_xmit_time_us = char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(msg_sizecxx . . $ msg_size.);
381 printfcxx . . $ printf(581fddd72a8db8fc).("%d chars at %d baud takes about %.0f ms to transmit\n", msg_sizecxx . . $ msg_size.,
382 baudcxx . . $ baud., msg_xmit_time_us * .001);
383
384 msg = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(msg_sizecxx . . $ msg_size. + 1);
385 if (msg == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
386 {
387 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Could not allocate send message buffer\n");
388 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
389 }
390
391 {
392 char dataChar = '0' + ((get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() / 1000) % 31);
393 char next = 'A';
394 for (i = 0; i < msg_sizecxx . . $ msg_size.; ++i) {
395 if (dataChar == '`')
396 {
397 msg[i] = next++;
398 ++dataChar;
399 }
400 else
401 msg[i] = dataChar++;
402
403 if (dataChar > 'z') {
404 dataChar = '`';
405 }
406 }
407 msg[msg_sizecxx . . $ msg_size.] = '\0';
408 }
409
410 printfcxx . . $ printf(581fddd72a8db8fc).("TX Message is \"%s\"\n", msg);
411
412 retMsgSize = 2 * msg_sizecxx . . $ msg_size.;
413 retMsg = malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(retMsgSize);
414 if (retMsg == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
415 {
416 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Could not allocate received message buffer\n");
417 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
418 }
419
420 flushcxx . . $ flush(7864480464b09eea).(TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!);
421
422 for (test = 0; test <= 2; ++test)
423 {
424 long long usec_delay;
425 long long usec_to_now;
426 int rc;
427
428 printfcxx . . $ printf(581fddd72a8db8fc).("\n******** Test purge %s; expect %s ********\n"
429 " -- Flushing UART\n",
430 flushTestNamecxx . . $ flushTestName.[test], expectedcxx . . $ expected.[test]);
431 flushcxx . . $ flush(7864480464b09eea).(TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!);
432 usleepcxx . . $ usleep(4c679297bf94b9eb).(msg_xmit_time_us);
433 flushcxx . . $ flush(7864480464b09eea).(TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!);
434 usleepcxx . . $ usleep(4c679297bf94b9eb).(100000);
435
436 usec_test_startcxx . . $ usec_test_start. = get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).();
437 if (dev_fdcxx . . $ dev_fd. >= 0)
438 rc = writecxx . . $ write(41184a59d38b4f65).(dev_fdcxx . . $ dev_fd., msg, msg_sizecxx . . $ msg_size.);
439 else
440 rc = ftdi_write_datacxx . . $ ftdi_write_data(8bb9860f18916ed8).(ftdicxx . . $ ftdi., msg, msg_sizecxx . . $ msg_size.);
441
442 if (rc != msg_sizecxx . . $ msg_size.)
443 {
444 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Data write was short: %d: %s\n",
445 rc, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.));
446 exitcxx . . $ exit(3ca292c338df9bb3).(1);
447 }
448 usec_to_now = get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.;
449 usec_delay = msg_xmit_time_us / 2 - usec_to_now;
450 if (usec_delay < 0)
451 usec_delay = 0;
452 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Write completes; delaying to TX midpoint (%.1f ms)\n",
453 usec_to_now * .001, usec_delay * .001);
454 if (usec_delay > 0)
455 usleepcxx . . $ usleep(4c679297bf94b9eb).(usec_delay);
456
457 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Issuing %s flush (purge)\n",
458 (get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.) * .001,
459 flushTestNamecxx . . $ flushTestName.[test]);
460 flushcxx . . $ flush(7864480464b09eea).(flushQueueSelectorcxx . . $ flushQueueSelector.[test]);
461
462 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Calling drain to wait for transmit to complete\n",
463 (get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.) * .001);
464 draincxx . . $ drain(2ebcc2597f7e891d).();
465
466 usec_to_now = get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.;
467
468 /* If input only flush, check drain time. */
469 if (flushQueueSelectorcxx . . $ flushQueueSelector.[test] == TCIFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:76:9`! &&
470 usec_to_now < (msg_xmit_time_us * 90ll) / 100ll)
471 {
472 usec_delay = (msg_xmit_time_us * 110ll) / 100ll - usec_to_now;
473 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Drain() completed too early; expected at least %.1f ms\n"
474 " Delaying for %.1f ms\n",
475 usec_to_now * .001,
476 ((msg_xmit_time_us * 90ll) / 100ll) * .001,
477 usec_delay * .001);
478 usleepcxx . . $ usleep(4c679297bf94b9eb).(usec_delay);
479 }
480 else
481 {
482 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Drain() reports completed; timing OK; delaying for 4 bytes\n",
483 (get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.) * .001);
484 usleepcxx . . $ usleep(4c679297bf94b9eb).(char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(4));
485 }
486
487 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Reading data.\n",
488 (get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.) * .001);
489 if (dev_fdcxx . . $ dev_fd. >= 0)
490 rc = readcxx . . $ read(7e34d0a36f355389).(dev_fdcxx . . $ dev_fd., retMsg, retMsgSize);
491 else
492 rc = ftdi_read_datacxx . . $ ftdi_read_data(d0399952b505fffc).(ftdicxx . . $ ftdi., retMsg, retMsgSize - 1);
493
494 usec_to_now = get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - usec_test_startcxx . . $ usec_test_start.;
495 if (rc < 0)
496 {
497 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, " -- %9.1f ms Read returned error %s\n",
498 usec_to_now * .001,
499 (dev_fdcxx . . $ dev_fd. >= 0 ? strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!) : ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdicxx . . $ ftdi.)));
500 exitcxx . . $ exit(3ca292c338df9bb3).(1);
501 }
502 retMsg[rc] = '\0';
503 printfcxx . . $ printf(581fddd72a8db8fc).(" -- %9.1f ms Read returns %d bytes; msg: \"%s\"\n",
504 usec_to_now * .001, rc, retMsg);
505
506 usleepcxx . . $ usleep(4c679297bf94b9eb).(char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(10));
507
508 }
509
510
511 if (dev_fdcxx . . $ dev_fd. >= 0)
512 {
513 closecxx . . $ close(7864480464b09eea).(dev_fdcxx . . $ dev_fd.);
514 }
515 else
516 {
517 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
518 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdicxx . . $ ftdi.);
519 }
520
521 exitcxx . . $ exit(3ca292c338df9bb3). (0);
522 }
523
524 /**********************************************************************
525 */
526 static int ascii2intcxx . . $ ascii2int(5ae29d726ae57f88).(const char * str, const char * pgm_name)
527 {
528 int rc;
529 char * endptr;
530 if (str == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || strlencxx . . $ strlen(751346f8406fd082).(str) == 0)
531 usagecxx . . $ usage(85c52e162fed56f9).(pgm_name);
532 rc = strtolcxx . . $ strtol(44bbb87609b212fc).(str, &endptr, 10);
533 if (endptr == str || *endptr != '\0')
534 usagecxx . . $ usage(85c52e162fed56f9).(pgm_name);
535 return rc;
536 }
537
538
539 /**********************************************************************
540 */
541 static struct Baud_Tablecxx . . $ Baud_Table# {
542 int32_tcxx . . $ int32_t# baudcxx . . $ Baud_Table#baud., baud_codecxx . . $ Baud_Table#baud_code.;
543 } baud_tablecxx . . $ baud_table. [] =
544 {
545 { 50, B50cxx . . $ `<external>/14258770537406596697/termios-baud.h:25:9`! },
546 { 75, B75cxx . . $ `<external>/14258770537406596697/termios-baud.h:26:9`! },
547 { 110, B110cxx . . $ `<external>/14258770537406596697/termios-baud.h:27:9`! },
548 { 134, B134cxx . . $ `<external>/14258770537406596697/termios-baud.h:28:9`! },
549 { 150, B150cxx . . $ `<external>/14258770537406596697/termios-baud.h:29:9`! },
550 { 200, B200cxx . . $ `<external>/14258770537406596697/termios-baud.h:30:9`! },
551 { 300, B300cxx . . $ `<external>/14258770537406596697/termios-baud.h:31:9`! },
552 { 600, B600cxx . . $ `<external>/14258770537406596697/termios-baud.h:32:9`! },
553 { 1200, B1200cxx . . $ `<external>/14258770537406596697/termios-baud.h:33:9`! },
554 { 1800, B1800cxx . . $ `<external>/14258770537406596697/termios-baud.h:34:9`! },
555 { 2400, B2400cxx . . $ `<external>/14258770537406596697/termios-baud.h:35:9`! },
556 { 4800, B4800cxx . . $ `<external>/14258770537406596697/termios-baud.h:36:9`! },
557 { 9600, B9600cxx . . $ `<external>/14258770537406596697/termios-baud.h:37:9`! },
558 { 19200, B19200cxx . . $ `<external>/14258770537406596697/termios-baud.h:38:9`! },
559 { 38400, B38400cxx . . $ `<external>/14258770537406596697/termios-baud.h:39:9`! },
560 { 57600, B57600cxx . . $ `<external>/14258770537406596697/termios-baud.h:50:9`! },
561 { 115200, B115200cxx . . $ `<external>/14258770537406596697/termios-baud.h:52:9`! },
562 { 230400, B230400cxx . . $ `<external>/14258770537406596697/termios-baud.h:54:9`! },
563 { -1, -1, }
564 };
565
566 /**********************************************************************
567 */
568 static int baud_2_baud_codecxx . . $ baud_2_baud_code(7864480464b09eea).(int baud)
569 {
570 struct Baud_Tablecxx . . $ Baud_Table# *p;
571
572 for (p = baud_tablecxx . . $ baud_table. ; p->baudcxx . . $ Baud_Table#baud. != -1; ++p) {
573 if (p->baudcxx . . $ Baud_Table#baud. == baud)
574 break;
575 }
576 return p->baud_codecxx . . $ Baud_Table#baud_code.;
577 }
578
579
580 static long int char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(int char_count)
581 {
582 long long bits = 8 + 1 + 1; /* Number of bits in each character */
583 bits *= (char_count == 0 ? 1 : char_count); /* Total number of bits */
584 bits *= 1000000; /* Convert to us */
585 lldiv_tcxx . . $ lldiv_t# parts = lldivcxx . . $ lldiv(46f10bbce55eaa25).(bits, baudcxx . . $ baud.); /* Number of us for message */
586 return (parts.quotcxx . . $ $anonymous_type_b31a80a31ad213b2_2#quot. + 1);
587 }
588
589
590 static long int draincxx . . $ drain(2ebcc2597f7e891d).()
591 {
592 long int rc = 0;
593 long long start_time = get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).();
594 if (dev_fdcxx . . $ dev_fd. >= 0)
595 rc = tcdraincxx . . $ tcdrain(7864480464b09eea).(dev_fdcxx . . $ dev_fd.);
596 else
597 {
598 long int sleep_interval = char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(10);
599 while (1) {
600 unsigned short modem_status = 0;
601 int rc = ftdi_poll_modem_statuscxx . . $ ftdi_poll_modem_status(ff746d63fa604f39).(ftdicxx . . $ ftdi., &modem_status);
602 if (rc < 0)
603 return -1;
604 if (modem_status & (1 << (6 + 8))) {
605 break;
606 }
607 usleepcxx . . $ usleep(4c679297bf94b9eb).(sleep_interval);
608 }
609 }
610 if (rc < 0)
611 return rc;
612 usleepcxx . . $ usleep(4c679297bf94b9eb).(char_cnt_2_useccxx . . $ char_cnt_2_usec(dc1f09c09fbff20f).(2));
613 return get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).() - start_time;
614 }
615
616
617 static int flushcxx . . $ flush(7864480464b09eea).(int queue_selector)
618 {
619 int rc;
620 if (dev_fdcxx . . $ dev_fd. >= 0)
621 rc = tcflushcxx . . $ tcflush(9b79fb6aee4c0440).(dev_fdcxx . . $ dev_fd., queue_selector);
622 else if (! broken_purge_testcxx . . $ broken_purge_test.)
623 {
624 switch (queue_selector) {
625
626 case TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!:
627 rc = ftdi_tcioflushcxx . . $ ftdi_tcioflush(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
628 break;
629
630 case TCIFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:76:9`!:
631 rc = ftdi_tciflushcxx . . $ ftdi_tciflush(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
632 break;
633
634 case TCOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:77:9`!:
635 rc = ftdi_tcoflushcxx . . $ ftdi_tcoflush(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
636 break;
637
638 default:
639 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
640 return -1;
641 }
642 }
643 else
644 {
645 switch (queue_selector) {
646
647 case TCIOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:78:9`!:
648 rc = ftdi_usb_purge_bufferscxx . . $ ftdi_usb_purge_buffers(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
649 break;
650
651 case TCIFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:76:9`!:
652 rc = ftdi_usb_purge_rx_buffercxx . . $ ftdi_usb_purge_rx_buffer(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
653 break;
654
655 case TCOFLUSHcxx . . $ `<external>/17989485081294502337/termios.h:77:9`!:
656 rc = ftdi_usb_purge_tx_buffercxx . . $ ftdi_usb_purge_tx_buffer(e7991dd2c21df3a2).(ftdicxx . . $ ftdi.);
657 break;
658
659 default:
660 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!;
661 return -1;
662 }
663 }
664
665 return rc;
666 }
667
668
669 static long long int get_time_useccxx . . $ get_time_usec(3028bef6af4f6cac).()
670 {
671 struct timevalcxx . . $ timeval# tv;
672 gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&tv, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!);
673 return tv.tv_seccxx . . $ timeval#tv_sec. * 1000000ll + tv.tv_useccxx . . $ timeval#tv_usec.;
674 }