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
26 int main(int argc, char **argv)
28 struct ftdi_context *ftdi;
31 int i, f, retval = EXIT_SUCCESS;
33 if ((ftdi = ftdi_new()) == 0)
35 fprintf(stderr, "Failed to allocate ftdi structure :%s \n",
36 ftdi_get_error_string(ftdi));
40 while ((i = getopt(argc, argv, "brw")) != -1)
55 fprintf(stderr, "usage: %s [options]\n", *argv);
56 fprintf(stderr, "\t-b do synchronous read and write\n");
57 fprintf(stderr, "\t-r do synchronous read\n");
58 fprintf(stderr, "\t-w do synchronous write\n");
64 /* Select first free interface */
65 ftdi_set_interface(ftdi, INTERFACE_ANY);
67 struct ftdi_device_list *devlist;
69 if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0)
71 fprintf(stderr, "No FTDI with default VID/PID found\n");
72 retval = EXIT_FAILURE;
78 f = ftdi_usb_open_dev(ftdi, devlist[0].dev);
81 fprintf(stderr, "Unable to open device %d: (%s)",
82 i, ftdi_get_error_string(ftdi));
89 fprintf(stderr, "No devices found\n");
93 ftdi_list_free(&devlist);
94 int err = ftdi_tcioflush(ftdi);
96 fprintf(stderr, "ftdi_tcioflush: %d: %s\n",
97 err, ftdi_get_error_string(ftdi));
101 /* Reset MPSSE controller. */
102 err = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
104 fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
105 err, ftdi_get_error_string(ftdi));
109 /* Enable MPSSE controller. Pin directions are set later.*/
110 err = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE);
112 fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
113 err, ftdi_get_error_string(ftdi));
116 #define DATA_TO_READ 3
117 uint8_t ftdi_init[] = {TCK_DIVISOR, 0x00, 0x00,
118 /* Set High byte to zero.*/
119 SET_BITS_HIGH, 0, 0xff,
121 /* Set High byte to 0x55.*/
122 SET_BITS_HIGH, 0x55, 0xff,
124 /* Set High byte to 0xaa.*/
125 SET_BITS_HIGH, 0xaa, 0xff,
127 /* Set back to high impedance.*/
128 SET_BITS_HIGH, 0x00, 0x00 };
129 struct ftdi_transfer_control *tc_read;
130 struct ftdi_transfer_control *tc_write;
133 tc_read = ftdi_read_data_submit(ftdi, data, DATA_TO_READ);
136 tc_write = ftdi_write_data_submit(ftdi, ftdi_init, sizeof(ftdi_init));
137 int transfer = ftdi_transfer_data_done(tc_write);
138 if (transfer != sizeof(ftdi_init)) {
139 printf("Async write failed : %d\n", transfer);
142 int written = ftdi_write_data(ftdi, ftdi_init, sizeof(ftdi_init));
143 if (written != sizeof(ftdi_init)) {
144 printf("Sync write failed: %d\n", written);
148 int transfer = ftdi_transfer_data_done(tc_read);
149 if (transfer != DATA_TO_READ) {
150 printf("Async Read failed:%d\n", transfer);
154 ftdi->usb_read_timeout = 1;
155 int i = 1000; /* Fail if read did not succeed in 1 second.*/
157 int res = ftdi_read_data(ftdi, data + index, 3 - index);
159 printf("Async read failure at %d\n", index);
168 printf("Async read unsuccessful\n");
171 printf("Read %02x %02x %02x\n", data[0], data[1], data[2]);
173 ftdi_usb_close(ftdi);