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