Simply read high/low 1-pin (input only) with USB GPIO?

1 reply [Last post]
SvenHazejager
Offline
Newbie
Joined: 12 Oct 2009
Posts:

Hi,

I'm currently using a simple parallel port interface to read a high/low
signal and I was wondering if the USB GPIO module would allow me to do the
same via USB? O/S is FreeBSD 7.

The circuit that I'm reading out is simple: it's a high/low "toggle"
triggered by a pulse coming from a home electricity meter. My current C code
simply reads the parallel port every 5ms and that works perfectly. Based on
http://offog.org/code/electricity.html.

How would this work with the USB GPIO module? Is it simply a matter of reading out the serial port that FreeBSD will assign to the module?

Thanks
Sven

dwolpoff
dwolpoff's picture
Offline
Forum Administrator
Joined: 17 Apr 2007
Posts:
Yep.

Sven,
Yes, this one of the intended purposes for this module. With the USB GPIO module, you have a few programming options for accessing incoming data. The method I most frequently use is the libftdi api. On my Ubuntu Linux development station, libftdi is included in the package management system-- while I don't have a FreeBSD system in front of me, I believe libftdi is accessible there as well.
I won't attempt to replicate the example code that ships with libftdi, but if you look at, pay attention to bitbang.c

Here's a version I modified to read a single bit:

/* This program is distributed under the GPL, version 2 */

#include
#include
#include

int main(int argc, char **argv)
{
struct ftdi_context ftdic;
int f,i;
char buf[1];

ftdi_init(&ftdic);

f = ftdi_usb_open(&ftdic, 0xdeaf, 0xdeed);

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);

printf("enabling bitbang mode\n");
ftdi_enable_bitbang(&ftdic, 0x00);

sleep(3);

buf[0] = 0xFF;

while(1) {
f = ftdi_read_pins(&ftdic, buf);
if(buf[0] & 0x08)
{
printf(".");
}
fflush(stdout);
if(f < 0) {
fprintf(stderr,"Read failed for 0x%x, error %d (%s)\n",buf[0],f, ftdi_get_error_string(&ftdic));
}
}

printf("\n");

printf("disabling bitbang mode\n");
ftdi_disable_bitbang(&ftdic);

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