Fixed Context::open as get_strings() closes the device before. Thanks to Chris M...
[libftdi] / examples / bitbang_ft2232.c
... / ...
CommitLineData
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#ifdef __WIN32__
14#define sleep(x) _sleep(x)
15#endif
16#include <ftdi.h>
17
18int main(int argc, char **argv)
19{
20 struct ftdi_context ftdic, ftdic2;
21 char buf[1];
22 int f,i;
23
24 // Init 1. channel
25 if (ftdi_init(&ftdic) < 0)
26 {
27 fprintf(stderr, "ftdi_init failed\n");
28 return EXIT_FAILURE;
29 }
30
31 ftdi_set_interface(&ftdic, INTERFACE_A);
32 f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
33 if (f < 0 && f != -5)
34 {
35 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
36 exit(-1);
37 }
38 printf("ftdi open succeeded(channel 1): %d\n",f);
39
40 printf("enabling bitbang mode(channel 1)\n");
41 ftdi_enable_bitbang(&ftdic, 0xFF);
42
43 // Init 2. channel
44 if (ftdi_init(&ftdic2) < 0)
45 {
46 fprintf(stderr, "ftdi_init failed\n");
47 return EXIT_FAILURE;
48 }
49 ftdi_set_interface(&ftdic2, INTERFACE_B);
50 f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001);
51 if (f < 0 && f != -5)
52 {
53 fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2));
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_enable_bitbang(&ftdic2, 0xFF);
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(&ftdic, 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(&ftdic));
70 sleep(1);
71
72 buf[0] = 0x2;
73 printf("porta: %02i: 0x%02x \n",i,buf[0]);
74 f = ftdi_write_data(&ftdic, 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(&ftdic));
77 sleep(1);
78
79 buf[0] = 0x1;
80 printf("portb: %02i: 0x%02x \n",i,buf[0]);
81 f = ftdi_write_data(&ftdic2, 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(&ftdic2));
84 sleep(1);
85
86 buf[0] = 0x2;
87 printf("portb: %02i: 0x%02x \n",i,buf[0]);
88 f = ftdi_write_data(&ftdic2, 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(&ftdic2));
91 sleep(1);
92 }
93 printf("\n");
94
95 printf("disabling bitbang mode(channel 1)\n");
96 ftdi_disable_bitbang(&ftdic);
97 ftdi_usb_close(&ftdic);
98 ftdi_deinit(&ftdic);
99
100 printf("disabling bitbang mode(channel 2)\n");
101 ftdi_disable_bitbang(&ftdic2);
102 ftdi_usb_close(&ftdic2);
103 ftdi_deinit(&ftdic2);
104}