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