fixed EEPROM user-area space checks for FT232R and FT245R chips in ftdi_eeprom_build()
[libftdi] / examples / bitbang2.c
1 /* ftdi_out.c
2  *
3  * Output a (stream of) byte(s) in bitbang mode to the
4  * ftdi245 chip that is (hopefully) attached.
5  *
6  * We have a little board that has a FT245BM chip and
7  * the 8 outputs are connected to several different
8  * things that we can turn on and off with this program.
9  *
10  * If you have an idea about hardware that can easily
11  * interface onto an FTDI chip, I'd like to collect
12  * ideas. If I find it worthwhile to make, I'll consider
13  * making it, I'll even send you a prototype (against
14  * cost-of-material) if you want.
15  *
16  * At "harddisk-recovery.nl" they have a little board that
17  * controls the power to two harddrives and two fans.
18  *
19  * -- REW R.E.Wolff@BitWizard.nl
20  *
21  *
22  *
23  * This program was based on libftdi_example_bitbang2232.c
24  * which doesn't carry an author or attribution header.
25  *
26  *
27  * This program is distributed under the GPL, version 2.
28  * Millions copies of the GPL float around the internet.
29  */
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <ftdi.h>
36
37 void ftdi_fatal (struct ftdi_context *ftdi, char *str)
38 {
39     fprintf (stderr, "%s: %s\n",
40              str, ftdi_get_error_string (ftdi));
41     ftdi_free(ftdi);
42     exit (1);
43 }
44
45 int main(int argc, char **argv)
46 {
47     struct ftdi_context *ftdi;
48     int i, t;
49     unsigned char data;
50     int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */
51
52     while ((t = getopt (argc, argv, "d:")) != -1)
53     {
54         switch (t)
55         {
56             case 'd':
57                 delay = atoi (optarg);
58                 break;
59         }
60     }
61
62     if ((ftdi = ftdi_new()) == 0)
63     {
64         fprintf(stderr, "ftdi_bew failed\n");
65         return EXIT_FAILURE;
66     }
67
68     if (ftdi_usb_open(ftdi, 0x0403, 0x6001) < 0)
69         ftdi_fatal (ftdi, "Can't open ftdi device");
70
71     if (ftdi_set_bitmode(ftdi, 0xFF, BITMODE_BITBANG) < 0)
72         ftdi_fatal (ftdi, "Can't enable bitbang");
73
74     for (i=optind; i < argc ; i++)
75     {
76         sscanf (argv[i], "%x", &t);
77         data = t;
78         if (ftdi_write_data(ftdi, &data, 1) < 0)
79         {
80             fprintf(stderr,"write failed for 0x%x: %s\n",
81                     data, ftdi_get_error_string(ftdi));
82         }
83         usleep(delay);
84     }
85
86     ftdi_usb_close(ftdi);
87     ftdi_free(ftdi);
88     exit (0);
89 }