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