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