Debian Codegraph @

examples/async.c

raw symbols SCIP
1 /* Libftdi example for asynchronous read/write.
2
3 This program is distributed under the GPL, version 2
4 */
5
6 /* This program switches to MPSSE mode, and sets and then reads back
7 * the high byte 3 times with three different values.
8 * The expected read values are hard coded in ftdi_init
9 * with 0x00, 0x55 and 0xaa
10 *
11 * Make sure that that nothing else drives some bit of the high byte
12 * or expect a collision for a very short time and some differences
13 * in the data read back.
14 *
15 * Result should be the same without any option or with either
16 * -r or -w or -b.
17 */
18
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <ftdi.h>cxx . . $ `<file>/src/ftdi.h`/
25
26 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv)
27 {
28 struct ftdi_contextcxx . . $ ftdi_context# *ftdi;
29 int do_read = 0;
30 int do_write = 0;
31 int i, f, retval = EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!;
32
33 if ((ftdi = ftdi_newcxx . . $ ftdi_new(207ff50971a1b30).()) == 0)
34 {
35 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Failed to allocate ftdi structure :%s \n",
36 ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
37 return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
38 }
39
40 while ((i = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "brw")) != -1)
41 {
42 switch (i)
43 {
44 case 'b':
45 do_read = 1;
46 do_write = 1;
47 break;
48 case 'r':
49 do_read = 1;
50 break;
51 case 'w':
52 do_write = 1;
53 break;
54 default:
55 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "usage: %s [options]\n", *argv);
56 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-b do synchronous read and write\n");
57 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-r do synchronous read\n");
58 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "\t-w do synchronous write\n");
59 retval = -1;
60 goto done;
61 }
62 }
63
64 /* Select first free interface */
65 ftdi_set_interfacecxx . . $ ftdi_set_interface(73d9956508f3a98f).(ftdi, INTERFACE_ANYcxx . . $ INTERFACE_ANY.);
66
67 struct ftdi_device_listcxx . . $ ftdi_device_list# *devlist;
68 int res;
69 if ((res = ftdi_usb_find_allcxx . . $ ftdi_usb_find_all(c44df5d38c831626).(ftdi, &devlist, 0, 0)) < 0)
70 {
71 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "No FTDI with default VID/PID found\n");
72 retval = EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!;
73 goto do_deinit;
74 }
75 if (res > 0)
76 {
77 int i = 1;
78 f = ftdi_usb_open_devcxx . . $ ftdi_usb_open_dev(adb36a6e32e3138e).(ftdi, devlist[0].devcxx . . $ ftdi_device_list#dev.);
79 if (f < 0)
80 {
81 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "Unable to open device %d: (%s)",
82 i, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
83 retval = -1;
84 goto do_deinit;
85 }
86 }
87 else
88 {
89 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "No devices found\n");
90 retval = -1;
91 goto do_deinit;
92 }
93 ftdi_list_freecxx . . $ ftdi_list_free(1a862d998d78d7c5).(&devlist);
94 int err = ftdi_tcioflushcxx . . $ ftdi_tcioflush(e7991dd2c21df3a2).(ftdi);
95 if (err != 0) {
96 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_tcioflush: %d: %s\n",
97 err, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
98 retval = -1;
99 goto do_deinit;
100 }
101 /* Reset MPSSE controller. */
102 err = ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0, BITMODE_RESETcxx . . $ BITMODE_RESET.);
103 if (err != 0) {
104 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_set_bitmode: %d: %s\n",
105 err, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
106 retval = -1;
107 goto do_deinit;
108 }
109 /* Enable MPSSE controller. Pin directions are set later.*/
110 err = ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0, BITMODE_MPSSEcxx . . $ BITMODE_MPSSE.);
111 if (err != 0) {
112 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "ftdi_set_bitmode: %d: %s\n",
113 err, ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi));
114 return -1;
115 }
116 #define DATA_TO_READcxx . . $ `examples/async.c:116:9`! 3
117 uint8_tcxx . . $ uint8_t# ftdi_init[] = {TCK_DIVISORcxx . . $ `src/ftdi.h:119:9`!, 0x00, 0x00,
118 /* Set High byte to zero.*/
119 SET_BITS_HIGHcxx . . $ `src/ftdi.h:112:9`!, 0, 0xff,
120 GET_BITS_HIGHcxx . . $ `src/ftdi.h:116:9`!,
121 /* Set High byte to 0x55.*/
122 SET_BITS_HIGHcxx . . $ `src/ftdi.h:112:9`!, 0x55, 0xff,
123 GET_BITS_HIGHcxx . . $ `src/ftdi.h:116:9`!,
124 /* Set High byte to 0xaa.*/
125 SET_BITS_HIGHcxx . . $ `src/ftdi.h:112:9`!, 0xaa, 0xff,
126 GET_BITS_HIGHcxx . . $ `src/ftdi.h:116:9`!,
127 /* Set back to high impedance.*/
128 SET_BITS_HIGHcxx . . $ `src/ftdi.h:112:9`!, 0x00, 0x00 };
129 struct ftdi_transfer_controlcxx . . $ ftdi_transfer_control# *tc_read;
130 struct ftdi_transfer_controlcxx . . $ ftdi_transfer_control# *tc_write;
131 uint8_tcxx . . $ uint8_t# data[3];
132 if (do_read) {
133 tc_read = ftdi_read_data_submitcxx . . $ ftdi_read_data_submit(3ebd176bc7ae4a39).(ftdi, data, DATA_TO_READcxx . . $ `examples/async.c:116:9`!);
134 }
135 if (do_write) {
136 tc_write = ftdi_write_data_submitcxx . . $ ftdi_write_data_submit(3ebd176bc7ae4a39).(ftdi, ftdi_init, sizeof(ftdi_init));
137 int transfer = ftdi_transfer_data_donecxx . . $ ftdi_transfer_data_done(c120ea6f3818ac34).(tc_write);
138 if (transfer != sizeof(ftdi_init)) {
139 printfcxx . . $ printf(581fddd72a8db8fc).("Async write failed : %d\n", transfer);
140 }
141 } else {
142 int written = ftdi_write_datacxx . . $ ftdi_write_data(8bb9860f18916ed8).(ftdi, ftdi_init, sizeof(ftdi_init));
143 if (written != sizeof(ftdi_init)) {
144 printfcxx . . $ printf(581fddd72a8db8fc).("Sync write failed: %d\n", written);
145 }
146 }
147 if (do_read) {
148 int transfer = ftdi_transfer_data_donecxx . . $ ftdi_transfer_data_done(c120ea6f3818ac34).(tc_read);
149 if (transfer != DATA_TO_READcxx . . $ `examples/async.c:116:9`!) {
150 printfcxx . . $ printf(581fddd72a8db8fc).("Async Read failed:%d\n", transfer);
151 }
152 } else {
153 int index = 0;
154 ftdi->usb_read_timeoutcxx . . $ ftdi_context#usb_read_timeout. = 1;
155 int i = 1000; /* Fail if read did not succeed in 1 second.*/
156 while (i--) {
157 int res = ftdi_read_datacxx . . $ ftdi_read_data(d0399952b505fffc).(ftdi, data + index, 3 - index);
158 if (res < 0) {
159 printfcxx . . $ printf(581fddd72a8db8fc).("Async read failure at %d\n", index);
160 } else {
161 index += res;
162 }
163 if (res == 3) {
164 break;
165 }
166 }
167 if (i < 1) {
168 printfcxx . . $ printf(581fddd72a8db8fc).("Async read unsuccessful\n");
169 }
170 }
171 printfcxx . . $ printf(581fddd72a8db8fc).("Read %02x %02x %02x\n", data[0], data[1], data[2]);
172 done:
173 ftdi_usb_closecxx . . $ ftdi_usb_close(e7991dd2c21df3a2).(ftdi);
174 do_deinit:
175 ftdi_freecxx . . $ ftdi_free(a4aec54891bac95c).(ftdi);
176 return retval;
177 }