Commit | Line | Data |
---|---|---|
cb2d4d24 TJ |
1 | /* bitbang_cbus.c |
2 | ||
3 | Example to use CBUS bitbang mode of newer chipsets. | |
4 | You must enable CBUS bitbang mode in the EEPROM first. | |
5 | ||
6 | Thanks to Steve Brown <sbrown@ewol.com> for the | |
7 | the information how to do it. | |
8 | ||
9 | The top nibble controls input/output and the bottom nibble | |
10 | controls the state of the lines set to output. The datasheet isn't clear | |
11 | what happens if you set a bit in the output register when that line is | |
12 | conditioned for input. This is described in more detail | |
13 | in the FT232R bitbang app note. | |
14 | ||
15 | BITMASK | |
16 | CBUS Bits | |
17 | 3210 3210 | |
18 | xxxx xxxx | |
19 | | |------ Output Control 0->LO, 1->HI | |
20 | |----------- Input/Output 0->Input, 1->Output | |
21 | ||
22 | Example: | |
23 | All pins to output with 0 bit high: 0xF1 (11110001) | |
24 | Bits 0 and 1 to input, 2 and 3 to output and masked high: 0xCC (11001100) | |
25 | ||
26 | This program is distributed under the GPL, version 2 | |
27 | */ | |
28 | ||
29 | #include <stdio.h> | |
30 | #include <unistd.h> | |
31 | #include <ftdi.h> | |
32 | ||
33 | int main(int argc, char **argv) | |
34 | { | |
35 | struct ftdi_context ftdic; | |
36 | int f,i; | |
37 | char buf[1]; | |
38 | unsigned char bitmask; | |
39 | ||
40 | ftdi_init(&ftdic); | |
41 | ||
42 | f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); | |
43 | if(f < 0 && f != -5) { | |
44 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); | |
45 | exit(-1); | |
46 | } | |
47 | printf("ftdi open succeeded: %d\n",f); | |
48 | ||
49 | // enable CBUS | |
50 | bitmask = 0x71; | |
51 | printf("enabling CBUS mode\n"); | |
52 | f = ftdi_set_bitmode(&ftdic, bitmask, 0x20); // cbus[0] as input (high), cbus[1-3] as output | |
53 | if (f < 0) { | |
54 | fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask, f, ftdi_get_error_string(&ftdic)); | |
55 | exit(-1); | |
56 | } | |
57 | ||
58 | // write to CBUS | |
59 | buf[0] = 0xFF; | |
60 | f = ftdi_write_data(&ftdic, buf, 1); | |
61 | if(f < 0) { | |
62 | fprintf(stderr,"write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); | |
63 | exit(-1); | |
64 | } | |
65 | ||
66 | // read CBUS | |
67 | f = ftdi_read_pins(&ftdic, &buf[0]); | |
68 | if (f < 0) { | |
69 | fprintf(stderr, "read_pins failed, error %d (%s)\n", f, ftdi_get_error_string(&ftdic)); | |
70 | exit(-1); | |
71 | } | |
72 | ||
73 | printf("disabling bitbang mode\n"); | |
74 | ftdi_disable_bitbang(&ftdic); | |
75 | ||
76 | ftdi_usb_close(&ftdic); | |
77 | ftdi_deinit(&ftdic); | |
78 | } |