Show a good example: Always check the return value of ftdi_init
[libftdi] / examples / bitbang_ft2232.c
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     if (ftdi_init(&ftdic) < 0)
23     {
24         fprintf(stderr, "ftdi_init failed\n");
25         return EXIT_FAILURE;
26     }
27
28     ftdi_set_interface(&ftdic, INTERFACE_A);
29     f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
30     if (f < 0 && f != -5)
31     {
32         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
33         exit(-1);
34     }
35     printf("ftdi open succeeded(channel 1): %d\n",f);
36
37     printf("enabling bitbang mode(channel 1)\n");
38     ftdi_enable_bitbang(&ftdic, 0xFF);
39
40     // Init 2. channel
41     if (ftdi_init(&ftdic2) < 0)
42     {
43         fprintf(stderr, "ftdi_init failed\n");
44         return EXIT_FAILURE;
45     }
46     ftdi_set_interface(&ftdic2, INTERFACE_B);
47     f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001);
48     if (f < 0 && f != -5)
49     {
50         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2));
51         exit(-1);
52     }
53     printf("ftdi open succeeded(channel 2): %d\n",f);
54
55     printf("enabling bitbang mode (channel 2)\n");
56     ftdi_enable_bitbang(&ftdic2, 0xFF);
57
58     // Write data
59     printf("startloop\n");
60     for (i = 0; i < 23; i++)
61     {
62         buf[0] =  0x1;
63         printf("porta: %02i: 0x%02x \n",i,buf[0]);
64         f = ftdi_write_data(&ftdic, buf, 1);
65         if (f < 0)
66             fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic));
67         sleep(1);
68
69         buf[0] =  0x2;
70         printf("porta: %02i: 0x%02x \n",i,buf[0]);
71         f = ftdi_write_data(&ftdic, buf, 1);
72         if (f < 0)
73             fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic));
74         sleep(1);
75
76         buf[0] =  0x1;
77         printf("portb: %02i: 0x%02x \n",i,buf[0]);
78         f = ftdi_write_data(&ftdic2, buf, 1);
79         if (f < 0)
80             fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2));
81         sleep(1);
82
83         buf[0] =  0x2;
84         printf("portb: %02i: 0x%02x \n",i,buf[0]);
85         f = ftdi_write_data(&ftdic2, buf, 1);
86         if (f < 0)
87             fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2));
88         sleep(1);
89     }
90     printf("\n");
91
92     printf("disabling bitbang mode(channel 1)\n");
93     ftdi_disable_bitbang(&ftdic);
94     ftdi_usb_close(&ftdic);
95     ftdi_deinit(&ftdic);
96
97     printf("disabling bitbang mode(channel 2)\n");
98     ftdi_disable_bitbang(&ftdic2);
99     ftdi_usb_close(&ftdic2);
100     ftdi_deinit(&ftdic2);
101 }