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 | ||
c84c9109 TJ |
26 | The input is standard "0x" hex notation. |
27 | A carriage return terminates the program. | |
28 | ||
cb2d4d24 TJ |
29 | This program is distributed under the GPL, version 2 |
30 | */ | |
31 | ||
32 | #include <stdio.h> | |
33 | #include <unistd.h> | |
c84c9109 | 34 | #include <stdlib.h> |
cb2d4d24 TJ |
35 | #include <ftdi.h> |
36 | ||
6ac169ea | 37 | int main(void) |
cb2d4d24 | 38 | { |
cd2ead2f | 39 | struct ftdi_context *ftdi; |
6ac169ea | 40 | int f; |
c84c9109 | 41 | unsigned char buf[1]; |
cb2d4d24 | 42 | unsigned char bitmask; |
579b006f | 43 | char input[10]; |
cb2d4d24 | 44 | |
cd2ead2f | 45 | if ((ftdi = ftdi_new()) == 0) |
6ac169ea | 46 | { |
cd2ead2f | 47 | fprintf(stderr, "ftdi_new failed\n"); |
6ac169ea TJ |
48 | return EXIT_FAILURE; |
49 | } | |
cb2d4d24 | 50 | |
cd2ead2f | 51 | f = ftdi_usb_open(ftdi, 0x0403, 0x6001); |
22d12cda TJ |
52 | if (f < 0 && f != -5) |
53 | { | |
cd2ead2f UB |
54 | fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(ftdi)); |
55 | ftdi_deinit(ftdi); | |
cb2d4d24 TJ |
56 | exit(-1); |
57 | } | |
58 | printf("ftdi open succeeded: %d\n",f); | |
59 | ||
22d12cda TJ |
60 | while (1) |
61 | { | |
c84c9109 TJ |
62 | // Set bitmask from input |
63 | fgets(input, sizeof(input) - 1, stdin); | |
64 | if (input[0] == '\n') break; | |
65 | bitmask = strtol(input, NULL, 0); | |
66 | printf("Using bitmask 0x%02x\n", bitmask); | |
cd2ead2f | 67 | f = ftdi_set_bitmode(ftdi, bitmask, BITMODE_CBUS); |
22d12cda TJ |
68 | if (f < 0) |
69 | { | |
cd2ead2f UB |
70 | fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask, f, ftdi_get_error_string(ftdi)); |
71 | ftdi_usb_close(ftdi); | |
72 | ftdi_deinit(ftdi); | |
025db2c2 | 73 | exit(-1); |
c84c9109 | 74 | } |
cb2d4d24 | 75 | |
c84c9109 | 76 | // read CBUS |
cd2ead2f | 77 | f = ftdi_read_pins(ftdi, &buf[0]); |
22d12cda TJ |
78 | if (f < 0) |
79 | { | |
cd2ead2f UB |
80 | fprintf(stderr, "read_pins failed, error %d (%s)\n", f, ftdi_get_error_string(ftdi)); |
81 | ftdi_usb_close(ftdi); | |
82 | ftdi_deinit(ftdi); | |
c84c9109 TJ |
83 | exit(-1); |
84 | } | |
85 | printf("Read returned 0x%01x\n", buf[0] & 0x0f); | |
cb2d4d24 | 86 | } |
cb2d4d24 | 87 | printf("disabling bitbang mode\n"); |
cd2ead2f | 88 | ftdi_disable_bitbang(ftdi); |
cb2d4d24 | 89 | |
cd2ead2f UB |
90 | ftdi_usb_close(ftdi); |
91 | ftdi_free(ftdi); | |
579b006f JZ |
92 | |
93 | return 0; | |
cb2d4d24 | 94 | } |