File: bitbang2.c

package info (click to toggle)
libftdi 0.20-4
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 2,076 kB
  • ctags: 598
  • sloc: sh: 11,006; ansic: 2,417; cpp: 594; makefile: 116
file content (90 lines) | stat: -rw-r--r-- 2,306 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* ftdi_out.c
 *
 * Output a (stream of) byte(s) in bitbang mode to the
 * ftdi245 chip that is (hopefully) attached.
 *
 * We have a little board that has a FT245BM chip and
 * the 8 outputs are connected to several different
 * things that we can turn on and off with this program.
 *
 * If you have an idea about hardware that can easily
 * interface onto an FTDI chip, I'd like to collect
 * ideas. If I find it worthwhile to make, I'll consider
 * making it, I'll even send you a prototype (against
 * cost-of-material) if you want.
 *
 * At "harddisk-recovery.nl" they have a little board that
 * controls the power to two harddrives and two fans.
 *
 * -- REW R.E.Wolff@BitWizard.nl
 *
 *
 *
 * This program was based on libftdi_example_bitbang2232.c
 * which doesn't carry an author or attribution header.
 *
 *
 * This program is distributed under the GPL, version 2.
 * Millions copies of the GPL float around the internet.
 */


#include <stdio.h>
#include <unistd.h>
#ifdef __WIN32__
#define usleep(x) Sleep((x+999)/1000)
#endif
#include <ftdi.h>

void ftdi_fatal (struct ftdi_context *ftdi, char *str)
{
    fprintf (stderr, "%s: %s\n",
             str, ftdi_get_error_string (ftdi));
    exit (1);
}

int main(int argc, char **argv)
{
    struct ftdi_context ftdic;
    int i, t;
    unsigned char data;
    int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */

    while ((t = getopt (argc, argv, "d:")) != -1)
    {
        switch (t)
        {
            case 'd':
                delay = atoi (optarg);
                break;
        }
    }

    if (ftdi_init(&ftdic) < 0)
    {
        fprintf(stderr, "ftdi_init failed\n");
        return EXIT_FAILURE;
    }

    if (ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0)
        ftdi_fatal (&ftdic, "Can't open ftdi device");

    if (ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG) < 0)
        ftdi_fatal (&ftdic, "Can't enable bitbang");

    for (i=optind; i < argc ; i++)
    {
        sscanf (argv[i], "%x", &t);
        data = t;
        if (ftdi_write_data(&ftdic, &data, 1) < 0)
        {
            fprintf(stderr,"write failed for 0x%x: %s\n",
                    data, ftdi_get_error_string(&ftdic));
        }
        usleep(delay);
    }

    ftdi_usb_close(&ftdic);
    ftdi_deinit(&ftdic);
    exit (0);
}