Skip to content

Instantly share code, notes, and snippets.

@vdudouyt
Last active August 29, 2015 14:26
Show Gist options
  • Save vdudouyt/9118bc9c79bdcb3f91fd to your computer and use it in GitHub Desktop.
Save vdudouyt/9118bc9c79bdcb3f91fd to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ftdi.h>
#include <assert.h>
#include <math.h>
/*
* Input: RxD
* OUT0: CTS
* OUT1: DTR
* OUT2: DSR
* OUT3: DCD
*/
static void print_help_and_exit()
{
fprintf(stderr, "Usage: ftdi_testlogic <bits_count>\n");
exit(-1);
}
static void print_binary(int arg, int bits_count)
{
int i;
for(i = bits_count-1; i >= 0; i--)
{
printf("%d", (arg & (1 << i)) >> i);
}
}
int main(int argc, char **argv)
{
if(argc != 2) print_help_and_exit();
int bits_count = atoi(argv[1]);
struct ftdi_context *ftdi = ftdi_new();
assert(ftdi != NULL);
ftdi_set_interface(ftdi, INTERFACE_A);
assert(ftdi_usb_open(ftdi, 0x0403, 0x6001) >= 0);
assert(ftdi_set_bitmode(ftdi, 0xfc, BITMODE_BITBANG) == 0);
int i;
char buf[2];
for(i = 0; i < pow(2, bits_count); i++)
{
buf[0] = i << 3; // Bypass TXD (output in normal mode), RXD (needed for input) and RTS (not easily available on FOCA breakout)
ftdi_write_data(ftdi, buf, 1);
usleep(10000);
ftdi_read_pins(ftdi, buf);
print_binary(i, bits_count);
printf(" => %d\n", (buf[0] & 2) >> 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment