1 /* Libftdi example for asynchronous read/write.
3 This program is distributed under the GPL, version 2
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
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.
15 * Result should be the same without any option or with either
28 int main(int argc, char **argv)
30 struct ftdi_context *ftdi;
35 if ((ftdi = ftdi_new()) == 0)
37 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
38 ftdi_get_error_string(ftdi));
42 while ((i = getopt(argc, argv, "brw")) != -1)
57 fprintf(stderr, "usage: %s [options]\n", *argv);
58 fprintf(stderr, "\t-b do synchronous read and write\n");
59 fprintf(stderr, "\t-r do synchronous read\n");
60 fprintf(stderr, "\t-w do synchronous write\n");
66 /* Select first free interface */
67 ftdi_set_interface(ftdi, INTERFACE_ANY);
69 struct ftdi_device_list *devlist;
71 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
73 fprintf(stderr, "No FTDI with default VID/PID found\n");
74 retval = EXIT_FAILURE;
80 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
83 fprintf(stderr, "Unable to open device %d: (%s)",
84 i, ftdi_get_error_string(ftdi));
91 fprintf(stderr, "No devices found\n");
95 ftdi_list_free(&devlist);
96 int err = ftdi_tcioflush(ftdi);
98 fprintf(stderr, "ftdi_tcioflush: %d: %s\n",
99 err, ftdi_get_error_string(ftdi));
103 /* Reset MPSSE controller. */
104 err = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
106 fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
107 err, ftdi_get_error_string(ftdi));
111 /* Enable MPSSE controller. Pin directions are set later.*/
112 err = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE);
114 fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
115 err, ftdi_get_error_string(ftdi));
118 #define DATA_TO_READ 3
119 uint8_t ftdi_init[] = {TCK_DIVISOR, 0x00, 0x00,
120 /* Set High byte to zero.*/
121 SET_BITS_HIGH, 0, 0xff,
123 /* Set High byte to 0x55.*/
124 SET_BITS_HIGH, 0x55, 0xff,
126 /* Set High byte to 0xaa.*/
127 SET_BITS_HIGH, 0xaa, 0xff,
129 /* Set back to high impedance.*/
130 SET_BITS_HIGH, 0x00, 0x00 };
131 struct ftdi_transfer_control *tc_read;
132 struct ftdi_transfer_control *tc_write;
135 tc_read = ftdi_read_data_submit(ftdi, data, DATA_TO_READ);
138 tc_write = ftdi_write_data_submit(ftdi, ftdi_init, sizeof(ftdi_init));
139 int transfer = ftdi_transfer_data_done(tc_write);
140 if (transfer != sizeof(ftdi_init)) {
141 printf("Async write failed : %d\n", transfer);
144 int written = ftdi_write_data(ftdi, ftdi_init, sizeof(ftdi_init));
145 if (written != sizeof(ftdi_init)) {
146 printf("Sync write failed: %d\n", written);
150 int transfer = ftdi_transfer_data_done(tc_read);
151 if (transfer != DATA_TO_READ) {
152 printf("Async Read failed:%d\n", transfer);
156 ftdi->usb_read_timeout = 1;
157 int i = 1000; /* Fail if read did not succeed in 1 second.*/
159 int res = ftdi_read_data(ftdi, data + index, 3 - index);
161 printf("Async read failure at %d\n", index);
170 printf("Async read unsuccessful\n");
173 printf("Read %02x %02x %02x\n", data[0], data[1], data[2]);
175 ftdi_usb_close(ftdi);