FT245R assistance

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks,

I have searched the forum for help on FTDIs FT245R but am still having some issues.

Story so far….
I’m using a FT245RL connect to a Mega32.
On the PC side I’m using Visual C++ (which I’m new to), I am using the D2XX drivers as supplied by FTDI and have installed them correctly (as I can communicate with usb, see below) I have also used FTDIs MProg to change the settings in my FT245RL to use the D2XX driver.

Problem…..
I can open my usb device, and get a handle for it.
I can write to my hardware (PC to hardware)
I can close my usb device
But reading my usb device only works occasionally most of the time I get an error :(

This is the code on my Mega32 to write to usb (FT245RL)

/*	Write out via usb	*/
void usb_write(unsigned char data)
{
	porta_out();
	if((PINC & TXE) == TXE)	/* if txe high wait */
	{
		Led2_off;	/*test*/
	}
	else
	{
		Led2_on;		/*test*/
		SET_WR;			/* WR high */
		PORTA = data;
		CLEAR_WR;		/* WR low, latch data */
	}
}

This is my read code in VC++, which I use after the usb has been opened

void CDRM_interfaceDlg::Read_usb (void)
{
	FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
	if (RxBytes > 0) {
		ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
		if (ftStatus == FT_OK) {
			m_status = "usb read\r\n";
		}
		else {
			m_status = "usb read error\r\n";
		}
	}
	else
	{
		m_status = "usb no data to read\r\n";
	}
}

When I get the error ftStatus = 4, corresponding to "FTID_IO_ERROR"
I'm wondering if anyone else has had similar problems?
I think the hardware side is ok as occasionally the read usb works.
I'm using 16MHZ clock (62.5ns period) and have checked timing in the FT245R data sheet and I'm sure time is ok.
Should I switch to the VCP driver? Although I don't beleive this is the problem as a can open, close, and write.

Thanks in advance for any and all help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi:

Hwo often are your writing to the FIFO from the microcontroller? It could possible that the you're trying to write too fast to the FIFO, and the device cannot keep up sending data to the USB, or the driver in the PC is discarding bytes, because your PC application is not reading them fast enough?

It looks that is the USB device is polled, therefore, you might loose bytes if it not read fast enough.

From the FTDI2XX Programmer's Guide:

Quote:
A return value of FT_IO_ERROR suggests an error in the parameters of the function, or a fatal
error like USB disconnect has occurred.

Regards,

Alex

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply Alex.

I am only writing to the FIFO (from micro) very slowly, when I push a button I write 1 byte of dummy data.

Basically I've been doing the following test proceedure;
1-connect usb cable
2-run application
3-click button in application to connect to my usb device
4-press button on hardware to send byte to FT245R FIFO
5-click button again in application to read the usb data

But that last step, the read is where I get the error.
Sometimes if I do the step fast it will read correctly, but when I repeat steps 4&5 I get a read error.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To what pin the TXE pin is connected to in port C? What is the definition of TXE in your code, is it correct (byte value instead of a bit number etc)?

Also your code does not do what it says, when you write a byte to FTDI, yout comments say to wait if the FTDI is busy, but if the FTDI is busy, you discard the byte. Should you wait the FTDI to be ready instead of discarding the byte?

Also try adding some delay to lengthen the write pulse.

  while (pinc&txe); // wait until txe=0, add a timeout if you wish.
  ddra=output;
  porta=data;
  wr=1;
  delay();
  wr=0;
  delay();
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the reply Jepeal.

TXE is defined as a byte, when void usb_write(unsigned char data) is exectued, if TXE is low (0) then the FIFO is ready and the data is written to it. if TXE high (1) fifo not ready don't write data to it.

So I only write data if the FIFO is ready when the function executes.

However the while statement is a good idea, but i'm not sure that I want the program to wait too long but can probably work around that, as suggested with timeout. So I'll give the while a go.
As for adding delays, I've tries that with no effect and I've checked the timing on the datasheet and it seems fine

Does anyone know if when using the FTDI chips the device can be opened for any period of time and then closed or does it have to be open and closed for every read operation? I assume it can be left open for periods of time?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I assume it can be left open for periods of time?

Yes, you don´t need to close your connection.

Quote:
I have also used FTDIs MProg to change the settings in my FT245RL to use the D2XX driver.

FTDI has a new driver with both vcp and d2xx capability. With the former drivers there was no need to
change the chip´s settings. With the "virgin" setting i never had problems (neither VCP nor d2xx).

But i´ve not yet tried the "R" chips. Maybe they are different...

Klaus
********************************
Look at: www.megausb.de (German)
********************************

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks Klaus,
on the PC side there is just 1 driver which includes VCP and D2XX in one. However you use MProg to tick a box to set the FT245R to use D2XX (thats my understanding anyway), so I fired up MProg and set it for D2XX.

I did as you suggested and tried it with the original settings but functionallity still the same, still get errors when I go to read usb.

Using a while statement as apposed to if as Jepael suggested alters the flow of the program (as expected) however does not assist with the error i'm still getting.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again,

i used the FTDI D2xx with VB6. At first I had some problems to declare the variables the right way.
(I´m not familiar with C, so there i have a lack of understanding... :( )

Some varaibles with the FT_Read i had to declare as: (VB-expressions)
* handle as long
* buffer as string (here i have used a string with a fixed length (string x 50))
* buffersize as long
* bytes returned as long (as pointer)
* ftStatus as long

And I remember i had some problems with the definition of the string.
There is a definition that uses 1 byte per character and one that uses two bytes per chracter.
Here maybe some experieced forum-user can get better help....

Klaus
********************************
Look at: www.megausb.de (German)
********************************

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Problem solved! :D

The problem was I was writing to the FT245R (from the micro) too quickly (but didn't realise), flooding the buffer and hence getting an error.

When I was pressing the button to send dummy data it would repeatedly send the data if held down after debouncing, even though I was not intentionally holding it down it had plenty of time to sent lots of data.

So I altered my button debounce code and now it works :)

The usb related code was actually correct

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The usb related code was actually correct

Quote:

The problem was I was writing to the FT245R (from the micro) too quickly

If you were honoring the TXE signal, then you couldn't overflow the buffer, right? So how can you say that the USB-related code was correct?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess thats a fair point, although I am monitoring TXE

TXE only goes high (busy) for a short period(min. 80ns) of time after each write, so if the button was activated for say 1ms there is still time for a lot of data to be written.

However when the buffer is full it does not raise the TXE to indicate the buffer is full. So it appears that there is no way to know when the buffer is full. Is this correct?

I think the FT245R requires/assumes the buffer is read/cleared fast enough on the PC side

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

Quote:
However when the buffer is full it does not raise the TXE to indicate the buffer is full.

That´s not true.

after every write it goes high a short time, but if the buffer has room to store data it goes low.
But if the buffer is full it remains high until there is room for new data.

Klaus
********************************
Look at: www.megausb.de (German)
********************************