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> | |
13 | #include <ftdi.h> | |
14 | ||
15 | int main(int argc, char **argv) | |
16 | { | |
08cb09bf TJ |
17 | struct ftdi_context ftdic, ftdic2; |
18 | char buf[1]; | |
ad397a4b TJ |
19 | int f,i; |
20 | ||
08cb09bf | 21 | // Init 1. channel |
6ac169ea TJ |
22 | if (ftdi_init(&ftdic) < 0) |
23 | { | |
24 | fprintf(stderr, "ftdi_init failed\n"); | |
25 | return EXIT_FAILURE; | |
26 | } | |
27 | ||
08cb09bf | 28 | ftdi_set_interface(&ftdic, INTERFACE_A); |
ad397a4b | 29 | f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); |
22d12cda TJ |
30 | if (f < 0 && f != -5) |
31 | { | |
ad397a4b TJ |
32 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); |
33 | exit(-1); | |
34 | } | |
08cb09bf | 35 | printf("ftdi open succeeded(channel 1): %d\n",f); |
ad397a4b | 36 | |
08cb09bf | 37 | printf("enabling bitbang mode(channel 1)\n"); |
ad397a4b TJ |
38 | ftdi_enable_bitbang(&ftdic, 0xFF); |
39 | ||
08cb09bf | 40 | // Init 2. channel |
6ac169ea TJ |
41 | if (ftdi_init(&ftdic2) < 0) |
42 | { | |
43 | fprintf(stderr, "ftdi_init failed\n"); | |
44 | return EXIT_FAILURE; | |
45 | } | |
08cb09bf | 46 | ftdi_set_interface(&ftdic2, INTERFACE_B); |
ad397a4b | 47 | f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001); |
22d12cda TJ |
48 | if (f < 0 && f != -5) |
49 | { | |
08cb09bf | 50 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2)); |
ad397a4b TJ |
51 | exit(-1); |
52 | } | |
08cb09bf | 53 | printf("ftdi open succeeded(channel 2): %d\n",f); |
ad397a4b | 54 | |
08cb09bf | 55 | printf("enabling bitbang mode (channel 2)\n"); |
ad397a4b TJ |
56 | ftdi_enable_bitbang(&ftdic2, 0xFF); |
57 | ||
08cb09bf TJ |
58 | // Write data |
59 | printf("startloop\n"); | |
22d12cda TJ |
60 | for (i = 0; i < 23; i++) |
61 | { | |
08cb09bf TJ |
62 | buf[0] = 0x1; |
63 | printf("porta: %02i: 0x%02x \n",i,buf[0]); | |
64 | f = ftdi_write_data(&ftdic, buf, 1); | |
22d12cda | 65 | if (f < 0) |
08cb09bf TJ |
66 | fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); |
67 | sleep(1); | |
ad397a4b | 68 | |
08cb09bf TJ |
69 | buf[0] = 0x2; |
70 | printf("porta: %02i: 0x%02x \n",i,buf[0]); | |
71 | f = ftdi_write_data(&ftdic, buf, 1); | |
22d12cda | 72 | if (f < 0) |
08cb09bf TJ |
73 | fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); |
74 | sleep(1); | |
ad397a4b | 75 | |
08cb09bf TJ |
76 | buf[0] = 0x1; |
77 | printf("portb: %02i: 0x%02x \n",i,buf[0]); | |
78 | f = ftdi_write_data(&ftdic2, buf, 1); | |
22d12cda | 79 | if (f < 0) |
08cb09bf TJ |
80 | fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); |
81 | sleep(1); | |
ad397a4b | 82 | |
08cb09bf TJ |
83 | buf[0] = 0x2; |
84 | printf("portb: %02i: 0x%02x \n",i,buf[0]); | |
85 | f = ftdi_write_data(&ftdic2, buf, 1); | |
22d12cda | 86 | if (f < 0) |
08cb09bf | 87 | fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2)); |
ad397a4b TJ |
88 | sleep(1); |
89 | } | |
ad397a4b TJ |
90 | printf("\n"); |
91 | ||
08cb09bf | 92 | printf("disabling bitbang mode(channel 1)\n"); |
ad397a4b | 93 | ftdi_disable_bitbang(&ftdic); |
ad397a4b TJ |
94 | ftdi_usb_close(&ftdic); |
95 | ftdi_deinit(&ftdic); | |
08cb09bf TJ |
96 | |
97 | printf("disabling bitbang mode(channel 2)\n"); | |
98 | ftdi_disable_bitbang(&ftdic2); | |
99 | ftdi_usb_close(&ftdic2); | |
100 | ftdi_deinit(&ftdic2); | |
ad397a4b | 101 | } |