EmbDev.net

Forum: µC & Digital Electronics atmega8 ftdi usb communication


von Igor (Guest)


Rate this post
0 useful
not useful
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:
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <ftdi.h>
5
6
unsigned char buf [1000];
7
struct ftdi_context ftdi;
8
9
void test(char* str){ int i,rsiz;
10
        printf("in: %s\n",str); fflush(stdout);
11
        ftdi_write_data(&ftdi, (unsigned char*)str, strlen(str)+1);        
12
        rsiz=ftdi_read_data(&ftdi, buf, 1000);
13
        printf("out(%d): ",rsiz);
14
        for(i=0;i<rsiz;i++)printf("%c",buf[i]); 
15
        printf("\n"); fflush(stdout);
16
}
17
18
int main(int count, char *argv[])
19
{ int i;
20
21
        ftdi_init(&ftdi);
22
        ftdi_usb_open(&ftdi, 0x0403, 0x6001);
23
24
        for(i=0;i<3;i++){
25
     test("one"); test("two"); test("three");
26
  }
27
28
        ftdi_usb_close(&ftdi);
29
        ftdi_deinit(&ftdi);
30
}

On atmega side there is a loop
1
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
1
in: one
2
out(0):
3
in: two
4
out(0):
5
in: three
6
out(0):
7
in: one
8
out(11): twothreeone
9
in: two
10
out(0):
11
in: three
12
out(8): twothree
13
....

The question is how to force the immediate reply to have
1
in: one
2
out(3): one
3
in: two
4
out(3): two
5
in: three
6
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
1
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.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.