Commit | Line | Data |
---|---|---|
08cb09bf TJ |
1 | /* bitbang_ft2232.c |
2 | ||
3 | Output some flickering in bitbang mode to the FT2232 | |
4 | ||
5 | Thanks to max@koeln.ccc.de for fixing and extending | |
6 | the example for the second channel. | |
7 | ||
8 | This program is distributed under the GPL, version 2 | |
9 | */ | |
ad397a4b TJ |
10 | |
11 | #include <stdio.h> | |
12 | #include <unistd.h> | |
7b5c5624 UB |
13 | #ifdef __WIN32__ |
14 | #define sleep(x) _sleep(x) | |
15 | #endif | |
ad397a4b TJ |
16 | #include <ftdi.h> |
17 | ||
18 | int main(int argc, char **argv) | |
19 | { | |
08cb09bf TJ |
20 | struct ftdi_context ftdic, ftdic2; |
21 | char buf[1]; | |
ad397a4b TJ |
22 | int f,i; |
23 | ||
08cb09bf | 24 | // Init 1. channel |
6ac169ea TJ |
25 | if (ftdi_init(&ftdic) < 0) |
26 | { | |
27 | fprintf(stderr, "ftdi_init failed\n"); | |
28 | return EXIT_FAILURE; | |
29 | } | |
30 | ||
08cb09bf | 31 | ftdi_set_interface(&ftdic, INTERFACE_A); |
ad397a4b | 32 | f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); |
22d12cda TJ |
33 | if (f < 0 && f != -5) |
34 | { | |
ad397a4b TJ |
35 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); |
36 | exit(-1); | |
37 | } | |
08cb09bf | 38 | printf("ftdi open succeeded(channel 1): %d\n",f); |
ad397a4b | 39 | |
08cb09bf | 40 | printf("enabling bitbang mode(channel 1)\n"); |
5484786d | 41 | ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG); |
ad397a4b | 42 | |
08cb09bf | 43 | // Init 2. channel |
6ac169ea TJ |
44 | if (ftdi_init(&ftdic2) < 0) |
45 | { | |
46 | fprintf(stderr, "ftdi_init failed\n"); | |
47 | return EXIT_FAILURE; | |
48 | } | |
08cb09bf | 49 | ftdi_set_interface(&ftdic2, INTERFACE_B); |
ad397a4b | 50 | f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001); |
22d12cda TJ |
51 | if (f < 0 && f != -5) |
52 | { | |
08cb09bf | 53 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2)); |
ad397a4b TJ |
54 | exit(-1); |
55 | } | |
08cb09bf | 56 | printf("ftdi open succeeded(channel 2): %d\n",f); |
ad397a4b | 57 | |
08cb09bf | 58 | printf("enabling bitbang mode (channel 2)\n"); |
5484786d | 59 | ftdi_set_bitmode(&ftdic2, 0xFF, BITMODE_BITBANG); |
ad397a4b | 60 | |
08cb09bf TJ |
61 | // Write data |
62 | printf("startloop\n"); | |
22d12cda TJ |
63 | for (i = 0; i < 23; i++) |
64 | { | |
08cb09bf TJ |
65 | buf[0] = 0x1; |
66 | printf("porta: %02i: 0x%02x \n",i,buf[0]); | |
67 | f = ftdi_write_data(&ftdic, buf, 1); | |
22d12cda | 68 | if (f < 0) |
08cb09bf TJ |
69 | fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); |
70 | sleep(1); | |
ad397a4b | 71 | |
08cb09bf TJ |
72 | buf[0] = 0x2; |
73 | printf("porta: %02i: 0x%02x \n",i,buf[0]); | |
74 | f = ftdi_write_data(&ftdic, buf, 1); | |
22d12cda | 75 | if (f < 0) |
08cb09bf TJ |
76 | fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); |
77 | sleep(1); | |
ad397a4b | 78 | |
08cb09bf TJ |
79 | buf[0] = 0x1; |
80 | printf("portb: %02i: 0x%02x \n",i,buf[0]); | |
81 | f = ftdi_write_data(&ftdic2, buf, 1); | |
22d12cda | 82 | if (f < 0) |
08cb09bf TJ |
83 | fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); |
84 | sleep(1); | |
ad397a4b | 85 | |
08cb09bf TJ |
86 | buf[0] = 0x2; |
87 | printf("portb: %02i: 0x%02x \n",i,buf[0]); | |
88 | f = ftdi_write_data(&ftdic2, buf, 1); | |
22d12cda | 89 | if (f < 0) |
08cb09bf | 90 | fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); |
ad397a4b TJ |
91 | sleep(1); |
92 | } | |
ad397a4b TJ |
93 | printf("\n"); |
94 | ||
08cb09bf | 95 | printf("disabling bitbang mode(channel 1)\n"); |
ad397a4b | 96 | ftdi_disable_bitbang(&ftdic); |
ad397a4b TJ |
97 | ftdi_usb_close(&ftdic); |
98 | ftdi_deinit(&ftdic); | |
08cb09bf TJ |
99 | |
100 | printf("disabling bitbang mode(channel 2)\n"); | |
101 | ftdi_disable_bitbang(&ftdic2); | |
102 | ftdi_usb_close(&ftdic2); | |
103 | ftdi_deinit(&ftdic2); | |
ad397a4b | 104 | } |