Commit | Line | Data |
---|---|---|
26fc55b0 UB |
1 | /* LIBFTDI EEPROM access example |
2 | ||
3 | This program is distributed under the GPL, version 2 | |
4 | */ | |
5 | ||
6 | #include <stdio.h> | |
7 | #include <stdlib.h> | |
8 | #include <string.h> | |
9 | #include <ctype.h> | |
10 | #include <unistd.h> | |
11 | #include <getopt.h> | |
12 | #include <ftdi.h> | |
13 | ||
14 | int main(int argc, char **argv) | |
15 | { | |
16 | struct ftdi_context *ftdi; | |
17 | int do_read = 0; | |
18 | int do_write = 0; | |
19 | int i, f, retval; | |
20 | ||
21 | if ((ftdi = ftdi_new()) == 0) | |
22 | { | |
23 | fprintf(stderr, "Failed to allocate ftdi structure :%s \n", | |
24 | ftdi_get_error_string(ftdi)); | |
25 | return EXIT_FAILURE; | |
26 | } | |
27 | ||
28 | while ((i = getopt(argc, argv, "brw")) != -1) | |
29 | { | |
30 | switch (i) | |
31 | { | |
32 | case 'b': | |
33 | do_read = 1; | |
34 | do_write = 1; | |
35 | break; | |
36 | case 'r': | |
37 | do_read = 1; | |
38 | break; | |
39 | case 'w': | |
40 | do_write = 1; | |
41 | break; | |
42 | default: | |
43 | fprintf(stderr, "usage: %s [options]\n", *argv); | |
44 | fprintf(stderr, "\t-b do synchronous read and write\n"); | |
45 | fprintf(stderr, "\t-r do synchronous read\n"); | |
46 | fprintf(stderr, "\t-w do synchronous write\n"); | |
47 | retval = -1; | |
48 | goto done; | |
49 | } | |
50 | } | |
51 | ||
52 | // Select first interface | |
53 | ftdi_set_interface(ftdi, INTERFACE_ANY); | |
54 | ||
55 | struct ftdi_device_list *devlist; | |
56 | int res; | |
57 | if ((res = ftdi_usb_find_all(ftdi, &devlist, 0, 0)) < 0) | |
58 | { | |
59 | fprintf(stderr, "No FTDI with default VID/PID found\n"); | |
60 | retval = EXIT_FAILURE; | |
61 | goto do_deinit; | |
62 | } | |
63 | if (res > 0) | |
64 | { | |
65 | int i = 1; | |
66 | f = ftdi_usb_open_dev(ftdi, devlist[0].dev); | |
67 | if (f < 0) | |
68 | { | |
69 | fprintf(stderr, "Unable to open device %d: (%s)", | |
70 | i, ftdi_get_error_string(ftdi)); | |
71 | retval = -1; | |
72 | goto do_deinit; | |
73 | } | |
74 | } | |
75 | else | |
76 | { | |
77 | fprintf(stderr, "No devices found\n"); | |
78 | retval = -1; | |
79 | goto do_deinit; | |
80 | } | |
81 | ftdi_list_free(&devlist); | |
82 | int err = ftdi_usb_purge_buffers(ftdi); | |
83 | if (err != 0) { | |
84 | fprintf(stderr, "ftdi_usb_purge_buffer: %d: %s\n", | |
85 | err, ftdi_get_error_string(ftdi)); | |
86 | retval = -1; | |
87 | goto do_deinit; | |
88 | } | |
89 | /* Reset MPSSE controller. */ | |
90 | err = ftdi_set_bitmode(ftdi, 0, BITMODE_RESET); | |
91 | if (err != 0) { | |
92 | fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", | |
93 | err, ftdi_get_error_string(ftdi)); | |
94 | retval = -1; | |
95 | goto do_deinit; | |
96 | } | |
97 | /* Enable MPSSE controller. Pin directions are set later.*/ | |
98 | err = ftdi_set_bitmode(ftdi, 0, BITMODE_MPSSE); | |
99 | if (err != 0) { | |
100 | fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", | |
101 | err, ftdi_get_error_string(ftdi)); | |
102 | return -1; | |
103 | } | |
104 | uint8_t ftdi_init[15] = {TCK_DIVISOR, 0x00, 0x00, | |
105 | SET_BITS_HIGH, 0, 0xff, | |
106 | GET_BITS_HIGH, | |
107 | SET_BITS_HIGH, 0x55, 0xff, | |
108 | GET_BITS_HIGH, | |
109 | SET_BITS_HIGH, 0xaa, 0xff, | |
110 | GET_BITS_HIGH}; | |
111 | struct ftdi_transfer_control *tc_read; | |
112 | struct ftdi_transfer_control *tc_write; | |
113 | uint8_t data[3]; | |
114 | if (do_read) { | |
115 | tc_read = ftdi_read_data_submit(ftdi, data, 3); | |
116 | } | |
117 | if (do_write) { | |
118 | tc_write = ftdi_write_data_submit(ftdi, ftdi_init, 15); | |
119 | int transfer = ftdi_transfer_data_done(tc_write); | |
120 | printf("Async write %d\n", transfer); | |
121 | } else { | |
122 | int written = ftdi_write_data(ftdi, ftdi_init, 15); | |
123 | if (written != 15) { | |
124 | printf("Sync write failed\n"); | |
125 | } | |
126 | } | |
127 | if (do_read) { | |
128 | int transfer = ftdi_transfer_data_done(tc_read); | |
129 | printf("Async Read %d\n", transfer); | |
130 | } else { | |
131 | int index = 0; | |
132 | while (index < 3) { | |
133 | int res = ftdi_read_data(ftdi, data + index, 3 - index); | |
134 | if (res < 0) { | |
135 | printf("Async read failure at %d\n", index); | |
136 | } else { | |
137 | index += res; | |
138 | } | |
139 | } | |
140 | } | |
141 | printf("Read %02x %02x %02x\n", data[0], data[1], data[2]); | |
142 | done: | |
143 | ftdi_usb_close(ftdi); | |
144 | do_deinit: | |
145 | ftdi_free(ftdi); | |
146 | return retval; | |
147 | } |