Quantcast

libFTDI based program does not read properly pin state of FT245

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

libFTDI based program does not read properly pin state of FT245

mackowiakp
This post was updated on .
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", ¶metr); }
        else { read = 1; }

        if (parametr > 15 || parametr < 0) {
                fprintf (stderr,"Bad call parameter\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);
    }
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Thomas Jarosch
On Monday, 3. October 2016 22:29:32 mackowiakp wrote:
>         pkod=(unsigned char)parametr;
>         ftdi_write_data(&ftdic, &pkod, 1);}
>
>         ftdi_read_data(&ftdic, &c, 1);

what happens if you add a "sleep(1);"
between ftdi_write_data() and ftdi_read_data()?

It might take some time until the receiving end
actually acts on the "command".

Cheers,
Thomas


--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to [hidden email]  

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [SPAM] Re: libFTDI based program does not read properly pin state of FT245

mackowiakp
No. It does not help. It is not time relayted problem. Look at the
output from command line below. Initial state of relay outputs - off

root@asus:/opt# ./arco 7;./arco
f0
f7

That is, acro program wa run twice, without any pause

W dniu 04.10.2016 o 10:04, Thomas Jarosch [via libFTDI] pisze:

> On Monday, 3. October 2016 22:29:32 mackowiakp wrote:
>>         pkod=(unsigned char)parametr;
>>         ftdi_write_data(&ftdic, &pkod, 1);}
>>
>>         ftdi_read_data(&ftdic, &c, 1);
>
> what happens if you add a "sleep(1);"
> between ftdi_write_data() and ftdi_read_data()?
>
> It might take some time until the receiving end
> actually acts on the "command".
>
> Cheers,
> Thomas
>
>
> --
> libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
> To unsubscribe send a mail to [hidden email]
> </user/SendEmail.jtp?type=node&node=4026121&i=0>  
>
>
>
> ------------------------------------------------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://libftdi.141977.n3.nabble.com/libFTDI-based-program-does-not-read-properly-pin-state-of-FT245-tp4026120p4026121.html
>
> To start a new topic under libFTDI, email
> [hidden email]
> To unsubscribe from libFTDI based program does not read properly pin
> state of FT245, click here
> <
> NAML
> <
http://libftdi.141977.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>
--

Z poważaniem

Piotr Maćkowiak

TEN MAIL JEST PODPISANY ELEKTRONICZNIE S/MIME


smime.p7s (5K) Download Attachment
Loading...