libftdi Archives

Subject: libFTDI based program does not read properly pin state of FT245

From: mackowiakp <mackowiakp@xxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 3 Oct 2016 22:29:32 -0700 (MST)
I use Asus router (based on ARMv7 proc) with Advanced Tomato installed
on it as my ARMv7 developer platform. I install compiler (gcc - 5.4.0-1)
plus dependencies and libFTDI (libftdi1 - 1.3-1) from OpenWRT Linux
repo.

I wrote a program on Linux x86_64 platform, using `ftd2xx` library taken
from chip manufacturer producer. It works properly. But now I want to port
it on ARMv7 platform, using Open Source `libFTDI` library.

I use ft245 chip based board. Pins number 0 to 3 acts as relay outputs while
pins 4-7 as optoisolated inputs. My goal is, that running program (named
arco) with parameter 0 to 15 turns on/off appropriate relays. Additionally,
after such running, program returns actual state of all pins in hex. Running
program without parameter, simply returns state of all 8 pins as hex number.
My problem is that when I run program using syntax for example `arco 7` it
turn on relays number 0 to 2 and turn off relay number 3. So it is correct.
But `ftdi_read_data` returns not actual state of pins but previous. When I
run `arco` for the second time (without parameters) it returns correct
value.

What I am doing wrong?

Below source of this small program:

    #include <stdio.h>
    #include<stdlib.h>
    #include <ftdi.h>
    
    int main(int argc, char *argv[])
    {
        unsigned char c = 0;
        unsigned char pkod = 0;
    int     parametr = 0;
    int         stat = 0;
    int read = 0;
    struct ftdi_context ftdic;

        if(argc > 1) { sscanf(argv[1], "%d", &parametr); }
        else { read = 1; }

        if (parametr > 15 || parametr < 0) {
                fprintf (stderr,"Niepoprawny parametr wywolania\n");
                exit(1); }

    /* Initialize context for subsequent function calls */
    ftdi_init(&ftdic);

    /* Open FTDI device based on FT245R vendor & product IDs */
    if(ftdi_usb_open(&ftdic, 0x5555, 0x0001) < 0) {
        fprintf(stderr,"ARCO not avaliable\n");
        exit(2);
    }

    /* Enable bitbang mode with a single output line */
        ftdi_set_bitmode(&ftdic, 0x0f, BITMODE_BITBANG);

        if(read != 1) {
        pkod=(unsigned char)parametr;
        ftdi_write_data(&ftdic, &pkod, 1);}

        ftdi_read_data(&ftdic, &c, 1);
        exit(stat); 
    }



--
View this message in context: 
http://libftdi.141977.n3.nabble.com/libFTDI-based-program-does-not-read-properly-pin-state-of-FT245-tp4026120.html
Sent from the libFTDI mailing list archive at Nabble.com.

--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx   

Current Thread