1 /* This program is distributed under the GPL, version 2 */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <ftdi.h>
7
main(int argc,char ** argv)8 int main(int argc, char **argv)
9 {
10 struct ftdi_context *ftdi;
11 int f,i;
12 unsigned char buf[1];
13 int retval = 0;
14
15 if ((ftdi = ftdi_new()) == 0)
16 {
17 fprintf(stderr, "ftdi_new failed\n");
18 return EXIT_FAILURE;
19 }
20
21 f = ftdi_usb_open(ftdi, 0x0403, 0x6001);
22
23 if (f < 0 && f != -5)
24 {
25 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi));
26 retval = 1;
27 goto done;
28 }
29
30 printf("ftdi open succeeded: %d\n",f);
31
32 printf("enabling bitbang mode\n");
33 ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG);
34
35 usleep(3 * 1000000);
36
37 buf[0] = 0x0;
38 printf("turning everything on\n");
39 f = ftdi_write_data(ftdi, buf, 1);
40 if (f < 0)
41 {
42 fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(ftdi));
43 }
44
45 usleep(3 * 1000000);
46
47 buf[0] = 0xFF;
48 printf("turning everything off\n");
49 f = ftdi_write_data(ftdi, buf, 1);
50 if (f < 0)
51 {
52 fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(ftdi));
53 }
54
55 usleep(3 * 1000000);
56
57 for (i = 0; i < 32; i++)
58 {
59 buf[0] = 0 | (0xFF ^ 1 << (i % 8));
60 if ( i > 0 && (i % 8) == 0)
61 {
62 printf("\n");
63 }
64 printf("%02hhx ",buf[0]);
65 fflush(stdout);
66 f = ftdi_write_data(ftdi, buf, 1);
67 if (f < 0)
68 {
69 fprintf(stderr,"write failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(ftdi));
70 }
71 usleep(1 * 1000000);
72 }
73
74 printf("\n");
75
76 printf("disabling bitbang mode\n");
77 ftdi_disable_bitbang(ftdi);
78
79 ftdi_usb_close(ftdi);
80 done:
81 ftdi_free(ftdi);
82
83 return retval;
84 }
85