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