Quantcast

write/read syncronization

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

write/read syncronization

Dr. Igor Nikitin
Hi people,

I have questions related to libftdi.
I use atmega8 communicating with PC via FTDI UB232R,
it's a little board with FT232RQ chip on it.
I am sending test messages ("commands") from PC to atmega
and want to receive a reply separately to every command
if possible immediately. The test program is:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ftdi.h>

unsigned char buf [1000];
struct ftdi_context ftdi;

void test(char* str){ int i,rsiz;
        printf("in: %s\n",str); fflush(stdout);
        ftdi_write_data(&ftdi, (unsigned char*)str, strlen(str)+1);       
        rsiz=ftdi_read_data(&ftdi, buf, 1000);
        printf("out(%d): ",rsiz);
        for(i=0;i<rsiz;i++)printf("%c",buf[i]);
        printf("\n"); fflush(stdout);
}

int main(int count, char *argv[])
{ int i;

        ftdi_init(&ftdi);
        ftdi_usb_open(&ftdi, 0x0403, 0x6001);

        for(i=0;i<3;i++){
       test("one"); test("two"); test("three");
    }

        ftdi_usb_close(&ftdi);
        ftdi_deinit(&ftdi);
}

On atmega side there is a loop

for(;;){ ugets(str,1000); uputs(str); }

where ugets reads incoming data until termination character '\0' is
encountered (and '\0' also goes to str), uputs sends the data until '\0'
('\0' itself is not sent).

The typical output is

in: one
out(0):
in: two
out(0):
in: three
out(0):
in: one
out(11): twothreeone
in: two
out(0):
in: three
out(8): twothree
....

The question is how to force the immediate reply to have

in: one
out(3): one
in: two
out(3): two
in: three
out(5): three

is there a kind of fflush?

The other question is why does the very first message disappear?
Restarting test program again and again, I see that the first
"one" is almost always missed, not appearing in further output.
Therefore attempt to wait until data are coming

for(rsiz=0;rsiz==0;)rsiz=ftdi_read_data(&ftdi, buf, 1000);

will end in an infinite loop. I have tested that microcontroller
in this situation is still awaiting for incoming string ("ugets"),
i.e. it has not received the first message. Must be there
an initial reset or something else? Should there be some
additional connections between atmega and FTDI chip
except of TXD, RXD, GND ?

I am a beginner in serial communication and will be grateful
for your help.

Cheers, Igor.



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: write/read syncronization

Uwe Bonnes
>>>>> "Igor" == Igor Nikitin <[hidden email]> writes:

...
    Igor> The question is how to force the immediate reply to have

Try to understand USB packets and how the FT stuffs these packets and the FT
latency.

The FT fill up a buffer and sends it out when its full  or when the latency
elapses. Set the latency low and sleep shortly between the
ftdi_write_data() and ftdi_read_data()

    Igor> The other question is why does the very first message disappear?
    Igor> Restarting test program again and again, I see that the first
    Igor> "one" is almost always missed, not appearing in further output.

I don't know. Maybe the uC is not out of reset? Maybe the FT takes some time
to setup?  What happens if you loopback tx/rx at the FT232 without the uC
...
    Igor> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    Igor> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Please, no HTML in mail.


--
Uwe Bonnes                [hidden email]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

--
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: write/read syncronization

Dr. Igor Nikitin
Hi Uwe,

thank you for helpful answer. Sorry for HTML, I'll try to fix it.

> The FT fill up a buffer and sends it out when its full  or when the latency
> elapses. Set the latency low and sleep shortly between the
> ftdi_write_data() and ftdi_read_data()

It works, with latency=1 and sleep=10ms or greater.

>     Igor> "one" is almost always missed, not appearing in further output.

> I don't know. Maybe the uC is not out of reset? Maybe the FT takes some time
> to setup?  What happens if you loopback tx/rx at the FT232 without the uC
> ...

The same behavior if tx and rx on FT232 are directly connected.

        ftdi_write_data(&ftdi, (unsigned char*)str, strlen(str)+1);       
        usleep(10000);
        rsiz=ftdi_read_data(&ftdi, buf, 1000);

The first message disappears, others are OK. Sleep 1sec
before the first write does not help. ftdi_usb_reset() before
the first write does not help. Replugging USB cable helps
for one run of test program. It shows the first message,
in further runs it disappears again.

I would not be so bothered with this problem, practically
I can send a dummy first command and ignore the reply.

One more question. If I choose the other way of interaction:
PC and uC send messages permanently to each other not waiting
the reply. Then temporal lag between sending and receiving
will be equal to timer latency, or fill time of the buffer,
minimal of them. Am I right?

Cheers, Igor.

--
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: write/read syncronization

Uwe Bonnes
>>>>> "Igor" == Igor Nikitin <[hidden email]> writes:

...

    Igor> The same behavior if tx and rx on FT232 are directly connected.

If you can send me the compilable code, I could test here.
...
    Igor> One more question. If I choose the other way of interaction: PC
    Igor> and uC send messages permanently to each other not waiting the
    Igor> reply. Then temporal lag between sending and receiving will be
    Igor> equal to timer latency, or fill time of the buffer, minimal of
    Igor> them. Am I right?

I read the docs the same way. But reality might be different...

Bye
--
Uwe Bonnes                [hidden email]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

--
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: write/read syncronization

Dr. Igor Nikitin

Uwe Bonnes <[hidden email]> hat am 5. Februar 2010 um
11:52 geschrieben:

> >>>>> "Igor" == Igor Nikitin <[hidden email]> writes:
>
> ...
>
>     Igor> The same behavior if tx and rx on FT232 are directly connected.
>
> If you can send me the compilable code, I could test here.


the code is attached. The output on my side:

g++ test.c -I. -L. -lftdi -lusb -o test
./test
latency=1
in: one
out(0):
in: two
out(4): two
in: three
out(6): three
in: one
out(4): one
in: two
out(4): two
in: three
out(6): three
in: one
out(4): one
in: two
out(4): two
in: three
out(6): three
Cheers, Igor.

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


test.c (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: write/read syncronization

Uwe Bonnes
>>>>> "Igor" == Igor Nikitin <[hidden email]> writes:

    Igor> Uwe Bonnes <[hidden email]> hat am
    Igor> 5. Februar 2010 um 11:52 geschrieben:

    >> >>>>> "Igor" == Igor Nikitin <[hidden email]> writes:
    >>
    >> ...
    >>
    >> �A  �A  �A Igor> The same behavior if tx and rx on FT232 are directly
    >> connected.
    >>
    >> If you can send me the compilable code, I could test here.


    Igor> the code is attached. The output on my side:

And on my side:

 g++ loopback.c -I ../git/libftdi/src/  -L. -lftdi -lusb -o loopback
loopback.c: In function �F!int main(int, char**)�F":
loopback.c:29: warning: deprecated conversion from string constant to �F!char*�F"
loopback.c:29: warning: deprecated conversion from string constant to �F!char*�F"
loopback.c:29: warning: deprecated conversion from string constant to �F!char*�F"
> ./loopback
latency=1
in: one
out(4): one
in: two
out(4): two
in: three
out(6): three
in: one
out(4): one
in: two
out(4): two
in: three
out(6): three
in: one
out(4): one
in: two
out(4): two
in: three
out(6): three
> ldd loopback |grep ftdi
        libftdi.so.1 => /usr/lib64/libftdi.so.1 (0x00007fa66d4ee000)

So the problem is not reproducable with a TTL232R I got sometimes from FTDI
with loopback.

Bye

--
Uwe Bonnes                [hidden email]

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

--
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: write/read syncronization

Dr. Igor Nikitin

Uwe Bonnes <[hidden email]> hat am 5. Februar 2010 um
15:28 geschrieben:

> So the problem is not reproducable with a TTL232R I got sometimes from FTDI
> with loopback.

I have TTL232R cable too, and it shows the same problem: loopback, the first
message is lost.

OK, I will try the workaround with the first dummy message.

Many thanks!

Cheers, Igor.

--
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: write/read syncronization

Matthias Janke
In reply to this post by Dr. Igor Nikitin

--
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: write/read syncronization

Matthias Janke
In reply to this post by Dr. Igor Nikitin
Hi,

sorry for the silence I had an imap problem.

Am Fri, 5 Feb 2010 10:11:25 +0100 (CET)
schrieb "Dr. Igor Nikitin" <[hidden email]>:


> >     Igor> "one" is almost always missed, not appearing in further
> >output.
>
> > I don't know. Maybe the uC is not out of reset? Maybe the FT takes
> > some time to setup?  What happens if you loopback tx/rx at the
> > FT232 without the uC ...
>
> The same behavior if tx and rx on FT232 are directly connected.
>
>         ftdi_write_data(&ftdi, (unsigned char*)str,
> strlen(str)+1); usleep(10000);
>         rsiz=ftdi_read_data(&ftdi, buf, 1000);
>
> The first message disappears, others are OK. Sleep 1sec
> before the first write does not help. ftdi_usb_reset() before
> the first write does not help. Replugging USB cable helps
> for one run of test program. It shows the first message,
> in further runs it disappears again.

I can confirm this behaviouted a program/script [2], but beware that
its kernel version dependant.

Another way to get the first byte written on a "old" device is to open
an close it with D2XX prior to libftdi stuff. But that's rather
inelegant.

I tried to investigate father with usbmon and wireshark ( including
Uwe's hint [3]), but didn't come to a solution. I think the difference
lies in the open function of D2XX. If I remember correctly I didn't
observe this behaviour with a FT245, but don't count on it. Hopefully
this helps someone with better usb debugging skills/tools.

Cheers,
Matthias

[1]
http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00002.html
[2]
http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00289.html
[3]
http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00293.html

--
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: write/read syncronization

Matthias Janke
Hi,
 
argh! sorry for the noise I have an imap problem.

Am Mon, 8 Feb 2010 17:04:45 +0100
schrieb Matthias Janke <[hidden email]>:

> I can confirm this behaviouted a program/script [2], but beware that
> its kernel version dependant.

I can confirm this behaviour. In my case the first byte of the first
write call, (with multiple bytes) on an not freshly HW reseted device
gets lost [1]. To trigger a HW reset by SW I posted a script/programm
[2], but beware that it's kernel version dependant.

Cheers,
Matthias
 
[1]
http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00002.html
[2]
http://developer.intra2net.com/mailarchive/html/libftdi/2009/msg00289.html

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

Loading...