Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

FTDI FT245RL problem

Status
Not open for further replies.

STU_KNTU

Member level 1
Member level 1
Joined
Feb 29, 2016
Messages
33
Helped
3
Reputation
6
Reaction score
3
Trophy points
8
Activity points
286
hi every one.
I want to transmit data from spartan 3 to computer via usb port with FT245Rl.
but i am some problem when i'm sending data to computer.
I've write a code in fpga side that sends 0 then 1 until 255 and then 0 again and ..... constantly.
and also i've write a code in D2xx mode that when code detect 0 start to saving data until it get to 255.
but here is the problem :(
in computer side i dont receive correct data.
for example i receive 1,2,5,5,7,8,9,10,10,12 and .....
could you tell me whats wrong about this project and which part could be probably wrong???:bang:
 

You're apparently ignoring the concept of FIFO handshaking yet.
 
thanks for your response.
.So how can i solve it ?? any idea ??
Im really need to connect to pc.
 

As already mentioned, use handshake signals on the 245 interface and read the data as a stream on the PC side.

FT245 has different FIFO handshake modes, review the datasheet to find out which one is appropriate for your application.
 
do you mean wr,rd,txe and rxf ???
when txe is low i put data in D0...D7 and then active wr
interesting point that when im changing the buad rate with ft_set_buadrate in C++
the data i receive changes.
for example when buadrate is 45000 the receiving :1,2,5,5,7,9 and ...
but when set this to 60000 data is like this :1,1,2,2,3,4,4,5,5,7,8,8
 

You didn't yet tell what you are exactly doing. I would expect one write pulse for each data value, respectively you'll receive each value only once in the stream read on the PC side.

Baud rate has no meaning for a parallel FIFO interface.
 
I exactly do same thing.the period of write cycle is 400khz also
So why when I change buad rate the stream of receving data changes also??
Do you think the problem is on pc side ??can I put my code here??
 

Hi,

And best a wiring diagram...and scope pictures of Wr and Txe (dual channel)
If you have a third channel, then show D0 also.

Klaus
 
here you are ::thumbsup:
Code:
#include "stdafx.h"
#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
#include "ftd2xx.h"
#include <time.h>

using namespace std;

int * binary(int);
//void usleep(__int64);

int main(void)
{
	FT_HANDLE ftHandle;
	FT_STATUS ftstatus;
	ftstatus = FT_Open(0, &ftHandle);
	if (ftstatus != FT_OK){
		cout << "Opening failed!" << endl;
		int a;
		cin >> a;
		return 1;
	}
	else
		cout << "FT Open sucssesfull" << endl;

	//Set Baud Rate
	ftstatus = FT_SetBaudRate(ftHandle, 49000);
	if (ftstatus == FT_OK)
		cout << "FT_SetBaudRate:  OK!" << endl;
	else
		cout << "FT_SetBaudRate:  FAILED!" << endl;
	//Data Characteristics
	FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
	FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0);
	////////////////////////////////////////////////////////////////////
	int number = 0;
	int bit = 0;
	unsigned char CntrlSig[1];
	int FileNum = 0;
	unsigned char TST[2];
	unsigned int temp1 = 0;
	unsigned char RX[7296];
	/////////////////////////////////////////////////////////////////////////
	while (number != -1 || bit != -1){
		bool bitistrue = false;
		cout << "Please enter 0 or 1: ";
		while (bitistrue == false)
		{
			cin >> bit;
			if (bit == 0 || bit == 1)
				bitistrue = true;
			else
				cout << "Wrong number " << endl;
		}
		////////////////////////////////////////////////////////////////////
		cout << "Please enter a positive integer between 0 and 127: ";
		bool numberistrue = false;
		while (numberistrue == false)
		{
			cin >> number;
			if (number < 0 || number > 127)
				cout << "That is not a valid number" << endl;
			else
				numberistrue = true;
		}
		/////////////////////////////////////////////////////////////////////
		FileNum++;
		string FileName = "TCD_Data" + to_string(FileNum) + ".dat";
		cout << "File Name is : ";
		cout << FileName << endl;
		int tmp = bit * pow(2, 7) + binary(number)[6] * pow(2, 6)
			+ binary(number)[5] * pow(2, 5) + binary(number)[4] * pow(2, 4)
			+ binary(number)[3] * pow(2, 3) + binary(number)[2] * pow(2, 2)
			+ binary(number)[1] * 2 + binary(number)[0];
		CntrlSig[0] = (unsigned char)tmp;
		cout << "Control Byte is :";
		cout << tmp << endl;
		////////////////////////////////////////////////////////////////
		//Set Asynchronous Bit-bang Mode
		UCHAR Mask = 0xff;//set all pins output
		UCHAR mode = 0x1;//Set asynchronous bit-bang mode
		ftstatus = FT_SetBitMode(ftHandle, Mask, mode);
		if (ftstatus == FT_OK)
			cout << "ALL Pins output : OK! " << endl;
		else
			cout << "ALL Pins output : FAILED!" << endl;
		////////////////////////////////////////////////////////////////
		DWORD BytesWritten;
		DWORD EventDWord_wr;
		DWORD Txbytes_wr;
		DWORD RxBytes_wr;
		FT_GetStatus(ftHandle, &RxBytes_wr, &Txbytes_wr, &EventDWord_wr);
		cout << "RxBytes_wr is :";
		cout << RxBytes_wr << endl;
		cout << "TxBytes_wr is :";
		cout << Txbytes_wr << endl;
		ftstatus = FT_Write(ftHandle, CntrlSig, 1, &BytesWritten);
		if (ftstatus == FT_OK)
			cout << "FT_Write OK" << endl;
		else
			cout << "FT_Write Failed" << endl;
		////////////////////////////////////////////////////////////////
		//Set Asynchronous Bit-bang Mode
		Mask = 0x00;//set all pins input
		ftstatus = FT_SetBitMode(ftHandle, Mask, mode);
		if (ftstatus == FT_OK)
			cout << "ALL Pins input : OK! " << endl;
		else
			cout << "ALL Pins input : Failed!" << endl;
		////////////////////////////////////////////////////////////////
		DWORD EventDWord;
		DWORD Txbytes;
		DWORD RxBytes;
		DWORD BytesReceived;

		//int wait = (number + 1) * 125 * 2.4 + 800;
		//usleep(wait);
		bool chkRead = false;
		bool chkFile = false;
		//FOR WRITE TO FILE IF DATA IS VALID
		if (bit == 1){
			bool zeroisone = false;
			bool oneisone = false;
			while (zeroisone == false || oneisone == false){
				FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
				if (&RxBytes > 0){
					ftstatus = FT_Read(ftHandle, TST, 2, &BytesReceived);
				}
				if ((unsigned int)TST[0] == 1)
					zeroisone = true;
				else
					zeroisone = false;
				if ((unsigned int)TST[1] == 1)
					oneisone = true;
				else
					oneisone = false;
			}
			//////////////////---/-/-/-/--/-/-/-/--/////////////////////////////
			///Event_Read
			HANDLE hEvent;
			DWORD EventMask;
			hEvent = CreateEvent(NULL, false, false, NULL);
			EventMask = FT_EVENT_RXCHAR;
			DWORD EventDWord;
			DWORD Status;
			int y = 0;
			while (1){
				ftstatus = FT_SetEventNotification(ftHandle, EventMask, hEvent);
				WaitForSingleObject(hEvent, INFINITE);
				FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
				if (RxBytes > 0) {
					ftstatus = FT_Read(ftHandle, &RX[y], 1, &BytesReceived);
					if (ftstatus == FT_OK)
						y++;
					cout << y << endl;
					if (y == 200){
						y = 0;
						break;
					}
				}
			}
			///////////////-/-/--/-/-/////////////-------///////////////////////
			cout << "Protocol Bytes Recived" << endl;
			if (zeroisone == true && oneisone == true){
				FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
				cout << "RxBytes is :";
				cout << RxBytes << endl;
				cout << "TxBytes is :";
				cout << Txbytes << endl;
				/*if (&RxBytes > 0){
				ftstatus = FT_Read(ftHandle, RX, 7296, &BytesReceived);
				if (ftstatus == FT_OK){// && chkRead == false){
				cout << "FT_Read OK" << endl;
				//chkRead = true;
				}
				else if (ftstatus != FT_OK){
				cout << "FT_Read Failed" << endl;
				//chkRead = false;
				}
				}*/


				// open a file in write mode.
				ofstream outfile;
				outfile.open(FileName, ios::out);
				for (int i = 0; i < 7296; i++){
					//if (i % 2 == 0){
					temp1 = (unsigned int)RX[i];
					//}
					//else if (i % 2 == 1){
					//temp1 = temp1 << 8;
					//temp1 = temp1 + (unsigned int)RX[i];
					outfile << temp1 << endl;
					//}
				}// FOR
				outfile.close();
			}//WHILE ONEISONE .. IS FALSE
		}// if (bit==1)

		else if (bit == 0){
			unsigned char TST100[2];
			unsigned char RX[100][7296];
			unsigned int temp2 = 0;
			bool zeroisone100 = false;
			bool oneisone100 = false;
			// open a file in write mode.
			ofstream outfile;
			outfile.open(FileName, ios::out);
			for (int i = 0; i < 100; i++)
			{
				bool chkRead100 = false;
				bool chkFile100 = false;
				while (zeroisone100 == false || oneisone100 == false){
					FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
					if (&RxBytes > 0){
						ftstatus = FT_Read(ftHandle, TST100, 2, &BytesReceived);
					}
					if ((unsigned int)TST100[0] == 254)
						zeroisone100 = true;
					else
						zeroisone100 = false;
					if ((unsigned int)TST100[1] == 254)
						oneisone100 = true;
					else
						oneisone100 = false;
					zeroisone100 = true;
					oneisone100 = true;
				}
				if (zeroisone100 == true && oneisone100 == true){
					cout << "Protocol Bytes Recived" << endl;
					FT_GetStatus(ftHandle, &RxBytes, &Txbytes, &EventDWord);
					if (&RxBytes > 0){
						ftstatus = FT_Read(ftHandle, RX[i], 7296, &BytesReceived);
						if (ftstatus == FT_OK){// && chkRead100 == false){
							cout << "FT_Read OK" << endl;
							//chkRead100 = true;
						}
						else if (ftstatus != FT_OK){
							cout << "FT_Read Failed" << endl;
							//chkRead100 = false;
						}
					}
					for (int h = 0; h < 7296; h++){
						// if (chkFile100 == false){
						//cout << "Cannot Read! Data Not Received!!!" << endl;
						//chkFile100 = true;
						//}
						if (h % 2 == 0){
							temp1 = (unsigned int)RX[i][h];
						}
						else if (h % 2 == 1){
							temp1 = temp1 << 8;
							temp1 = temp1 + (unsigned int)RX[i][h];
							outfile << temp1 << endl;
						}
					}// FOR
					outfile << "			**************	End of Data	**************";
				}//WHILE ONEISONE .. IS FALSE
			}//for 100
			outfile.close();
		}//end if (bit = 0)
	}//while!=-1
	return 0;
}







int * binary(int number)
{
	int remainder;
	int bi[8];
	for (int i = 0; i < 8; i++)
	{
		remainder = number % 2;
		number = (number >> 1);
		bi[i] = remainder;
	}
	return bi;
}
and also handshaking wires :
Capture.PNG
 

Hi,

your diagram is simulation only. We need real scope pictures.

In the diagram we don´t see the short "high" cycling of the TxE signal after the WR signal.
Also we don´t see what´s happening in case the fifo is full. ie when Txe is high for an undefined time.

Klaus
 

i agree but i havent any scope to take pictures .
i can put my vhdl code here.is this enough??
as mentioned in picture when txf is high fpga wont write on ft.
Code:
entity test1 is
port( 	Rxf,Txf	:	In  	std_logic;
			Wr,Rd		:	out 	std_logic:='0';
			data		:	inout	std_logic_vector(7 downto 0):="00000000";
			Clk		:	in		std_logic;--50 mhz
			lamp		:	out	std_logic
		);
end test1;
--/////////////////////////////////////////////
architecture Behavioral of test1 is
signal Counter	:	std_logic_vector(7 downto 0) :=(others => '0');
signal state	:	std_logic_vector(1 downto 0):="00";
begin
Counter <= Counter + '1' when clk'event and clk = '1';
Rd <= '1';
lamp <= '0';
process(Counter(7))
begin
If Counter(7)'event and Counter(7) = '1' then
Case State is
when "00" =>
	If Txf = '0' then 
		state <= state + "01";
		data <= data + "00000001";
		end if;
when "01" =>
	Wr <= '1' ;	
	state <= "10";
when "10" =>
	Wr <= '0';
	state <="00";
when others => 
state <= "00";
end case;
end if;
end process;
 

In terms of FPGA design guidelines, it's a bad example of using a generated clock instead of a clock enable signal. But that's not causing problems for this simple design, it looks like you are sending a stream with exactly one sample per value to the FIFO. So the problem is probably with the receiver code.
 

thanks.so whats wrong with receiver code???
do you realized something??
 

Apparently the interface is operated asynchronously instead of reading from the FIFO.
 

Hi,

don´t use any bitbang mode.
to be on the safe side you could reset the bitbang mode:

***
To reset the IO bit mode:
ftStatus = FT_SetBitMode(ftHandle,0,0);
***

Klaus
 

thanks alot.i have little problems.:sad:
don´t use any bitbang mode.
Apparently the interface is operated asynchronously instead of reading from the FIFO.
so what mode i must use after i reset the Ft?
in data sheet this modes declared...
1.PNG
 

Hi,

What information do you need?

so what mode i must use after i reset the Ft?
Nothing. Keep it simple. Start your transmission.
(You don´t reset the "FT". You reset "bitbang mode" and automatically operate in normal mode)

Maybe best is NOT to use SetBitMode at all.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top