Debian Codegraph @

examples/serial_test.c

raw symbols SCIP
1 /* serial_test.c
2
3 Read/write data via serial I/O
4
5 This program is distributed under the GPL, version 2
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <getopt.h>
12 #include <signal.h>
13 #include <ftdi.h>cxx . . $ `<file>/src/ftdi.h`/
14
15 static int exitRequestedcxx . . $ exitRequested. = 0;
16 /*
17 * sigintHandler --
18 *
19 * SIGINT handler, so we can gracefully exit when the user hits ctrl-C.
20 */
21 static void
22 sigintHandlercxx . . $ sigintHandler(d4f767463ce0a6b3).(int signum)
23 {
24 exitRequestedcxx . . $ exitRequested. = 1;
25 }
26
27 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
28 {
29 struct ftdi_contextcxx . . $ ftdi_context# *ftdi;
30 unsigned char buf[1024];
31 int f = 0, i;
32 int vid = 0x403;
33 int pid = 0;
34 int baudrate = 115200;
35 int interface = INTERFACE_ANYcxx . . $ INTERFACE_ANY.;
36 int do_write = 0;
37 unsigned int pattern = 0xffff;
38 int retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
39
40 while ((i = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "i:v:p:b:w::")) != -1)
41 {
42 switch (i)
43 {
44 case 'i': // 0=ANY, 1=A, 2=B, 3=C, 4=D
45 interface = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0);
46 break;
47 case 'v':
48 vid = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0);
49 break;
50 case 'p':
51 pid = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0);
52 break;
53 case 'b':
54 baudrate = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0);
55 break;
56 case 'w':
57 do_write = 1;
58 if (optargcxx . . $ optarg.)
59 pattern = strtoulcxx . . $ strtoul(31746e8d6ec49860).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0);
60 if (pattern > 0xff)
61 {
62 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Please provide a 8 bit pattern\n");
63 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
64 }
65 break;
66 default:
67 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "usage: %s [-i interface] [-v vid] [-p pid] [-b baudrate] [-w [pattern]]\n", *argv);
68 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
69 }
70 }
71
72 // Init
73 if ((ftdi = ftdi_newcxx . . $ ftdi_new(207ff50971a1b30).()) == 0)
74 {
75 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_new failed\n");
76 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
77 }
78
79 if (!vid && !pid && (interface == INTERFACE_ANYcxx . . $ INTERFACE_ANY.))
80 {
81 ftdi_set_interfacecxx . . $ ftdi_set_interface(73d9956508f3a98f).(ftdi, INTERFACE_ANYcxx . . $ INTERFACE_ANY.);
82 struct ftdi_device_listcxx . . $ ftdi_device_list# *devlist;
83 int res;
84 if ((res = ftdi_usb_find_allcxx . . $ ftdi_usb_find_all(c44df5d38c831626).(ftdi, &devlist, 0, 0)) < 0)
85 {
86 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "No FTDI with default VID/PID found\n");
87 goto do_deinit;
88 }
89 if (res == 1)
90 {
91 f = ftdi_usb_open_devcxx . . $ ftdi_usb_open_dev(adb36a6e32e3138e).(ftdi, devlist[0].devcxx . . $ ftdi_device_list#dev.);
92 if (f<0)
93 {
94 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Unable to open device %d: (%s)",
95 i, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
96 }
97 }
98 ftdi_list_freecxx . . $ ftdi_list_free(1a862d998d78d7c5).(&devlist);
99 if (res > 1)
100 {
101 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "%d Devices found, please select Device with VID/PID\n", res);
102 /* TODO: List Devices*/
103 goto do_deinit;
104 }
105 if (res == 0)
106 {
107 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "No Devices found with default VID/PID\n");
108 goto do_deinit;
109 }
110 }
111 else
112 {
113 // Select interface
114 ftdi_set_interfacecxx . . $ ftdi_set_interface(73d9956508f3a98f).(ftdi, interface);
115
116 // Open device
117 f = ftdi_usb_opencxx . . $ ftdi_usb_open(cfc3cf95535913f).(ftdi, vid, pid);
118 }
119 if (f < 0)
120 {
121 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
122 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
123 }
124
125 // Set baudrate
126 f = ftdi_set_baudratecxx . . $ ftdi_set_baudrate(b8d6b357325709d2).(ftdi, baudrate);
127 if (f < 0)
128 {
129 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "unable to set baudrate: %d (%s)\n", f, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
130 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
131 }
132
133 /* Set line parameters
134 *
135 * TODO: Make these parameters settable from the command line
136 *
137 * Parameters are chosen that sending a continuous stream of 0x55
138 * should give a square wave
139 *
140 */
141 f = ftdi_set_line_propertycxx . . $ ftdi_set_line_property(1a8de5025239d250).(ftdi, 8, STOP_BIT_1cxx . . $ STOP_BIT_1., NONEcxx . . $ NONE.);
142 if (f < 0)
143 {
144 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "unable to set line parameters: %d (%s)\n", f, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
145 exitcxx . . $ exit(3ca292c338df9bb3).(-1);
146 }
147
148 if (do_write)
149 for(i=0; i<1024; i++)
150 buf[i] = pattern;
151
152 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, sigintHandlercxx . . $ sigintHandler(d4f767463ce0a6b3).);
153 while (!exitRequestedcxx . . $ exitRequested.)
154 {
155 if (do_write)
156 f = ftdi_write_datacxx . . $ ftdi_write_data(8bb9860f18916ed8).(ftdi, buf,
157 (baudrate/512 >sizeof(buf))?sizeof(buf):
158 (baudrate/512)?baudrate/512:1);
159 else
160 f = ftdi_read_datacxx . . $ ftdi_read_data(d0399952b505fffc).(ftdi, buf, sizeof(buf));
161 if (f<0)
162 usleepcxx . . $ usleep(4c679297bf94b9eb).(1 * 1000000);
163 else if(f> 0 && !do_write)
164 {
165 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "read %d bytes\n", f);
166 fwritecxx . . $ fwrite(ab0609fa07c47d6e).(buf, f, 1, stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
167 fflushcxx . . $ fflush(89bc1efcdac0ca22).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!);
168 fflushcxx . . $ fflush(89bc1efcdac0ca22).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!);
169 }
170 }
171 signalcxx . . $ signal(c2d516773f91faf8).(SIGINTcxx . . $ `<external>/11773118241360591791/signum-generic.h:48:9`!, SIG_DFLcxx . . $ `<external>/11773118241360591791/signum-generic.h:29:9`!);
172 retval = EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!;
173
174 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
175 do_deinit:
176 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
177
178 return retval;
179 }