X-Git-Url: http://developer.intra2net.com/git/?p=libftdi;a=blobdiff_plain;f=examples%2Fbitbang_cbus.c;h=17d27ebf969ca300decc39ebe7683744136722f9;hp=6163af8836566414197744ff7b0238076975efd0;hb=579b006f4873fa72c73d372660d158061497bcc9;hpb=cb2d4d24e31a8423349a2930fb99adfb1390a90a diff --git a/examples/bitbang_cbus.c b/examples/bitbang_cbus.c index 6163af8..17d27eb 100644 --- a/examples/bitbang_cbus.c +++ b/examples/bitbang_cbus.c @@ -23,56 +23,67 @@ All pins to output with 0 bit high: 0xF1 (11110001) Bits 0 and 1 to input, 2 and 3 to output and masked high: 0xCC (11001100) + The input is standard "0x" hex notation. + A carriage return terminates the program. + This program is distributed under the GPL, version 2 */ #include #include +#include #include -int main(int argc, char **argv) +int main(void) { struct ftdi_context ftdic; - int f,i; - char buf[1]; + int f; + unsigned char buf[1]; unsigned char bitmask; + char input[10]; - ftdi_init(&ftdic); + if (ftdi_init(&ftdic) < 0) + { + fprintf(stderr, "ftdi_init failed\n"); + return EXIT_FAILURE; + } f = ftdi_usb_open(&ftdic, 0x0403, 0x6001); - if(f < 0 && f != -5) { + if (f < 0 && f != -5) + { fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic)); exit(-1); } printf("ftdi open succeeded: %d\n",f); - // enable CBUS - bitmask = 0x71; - printf("enabling CBUS mode\n"); - f = ftdi_set_bitmode(&ftdic, bitmask, 0x20); // cbus[0] as input (high), cbus[1-3] as output - if (f < 0) { - fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask, f, ftdi_get_error_string(&ftdic)); - exit(-1); - } - - // write to CBUS - buf[0] = 0xFF; - f = ftdi_write_data(&ftdic, buf, 1); - if(f < 0) { - fprintf(stderr,"write failed for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic)); - exit(-1); - } + while (1) + { + // Set bitmask from input + fgets(input, sizeof(input) - 1, stdin); + if (input[0] == '\n') break; + bitmask = strtol(input, NULL, 0); + printf("Using bitmask 0x%02x\n", bitmask); + f = ftdi_set_bitmode(&ftdic, bitmask, BITMODE_CBUS); + if (f < 0) + { + fprintf(stderr, "set_bitmode failed for 0x%x, error %d (%s)\n", bitmask, f, ftdi_get_error_string(&ftdic)); + exit(-1); + } - // read CBUS - f = ftdi_read_pins(&ftdic, &buf[0]); - if (f < 0) { - fprintf(stderr, "read_pins failed, error %d (%s)\n", f, ftdi_get_error_string(&ftdic)); - exit(-1); + // read CBUS + f = ftdi_read_pins(&ftdic, &buf[0]); + if (f < 0) + { + fprintf(stderr, "read_pins failed, error %d (%s)\n", f, ftdi_get_error_string(&ftdic)); + exit(-1); + } + printf("Read returned 0x%01x\n", buf[0] & 0x0f); } - printf("disabling bitbang mode\n"); ftdi_disable_bitbang(&ftdic); ftdi_usb_close(&ftdic); ftdi_deinit(&ftdic); + + return 0; }