Hello, I’m new here and was really hoping that some of you experts have some experience with what I’m doing because I am struggling.
Basically, I want to connect my i2c device to my umft200xd to be able to do i2c over USB. Currently this code is being used on Windows 7 but I’ll take any help I can get, linux, raspberry pi, Mac, etc.
The code succeeds until calls to FT_GetQueueStatus() are made. Anytime FT_GetQueueStatus() calls happen, it always returns FT_OK (success = 0), but there are 0 bytes to read in the buffer. I can write fine via FT_Write(), but I cannot read any information from the device.
I know this isn’t the prettiest code in the world but I’m really just trying to get things up and running right now and need to be able to communicate w/my i2c device and would appreciate ANY help you could give me. Thank you very much for taking the time to read this.
Respectfully.
int main(int argc, char** argv)
{
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
DWORD devIndex = 0; // first device
char Buffer[64]; // more than enough room!
// this gets number of devices currently connected
DWORD numDevs;
ftStatus = FT_ListDevices(&numDevs,NULL,FT_LIST_NUMBER_ONLY);
if (ftStatus == FT_OK)
{
printf("working %08x\n", numDevs);
}
else
{
printf("fail\n");
}
// get serial number of first device
ftStatus = FT_ListDevices((PVOID)devIndex,Buffer,FT_LIST_BY_INDEX|FT_OPEN_BY_SERIAL_NUMBER);
if (ftStatus == FT_OK)
{
printf("serial number - %s\n", Buffer);
}
else
{
printf("ftlistdev serial fail\n");
}
// get device descriptions
char *BufPtrs[3];
// pointer to array of 3 pointers
char Buffer1[64];
// buffer for description of first device
char Buffer2[64];
// buffer for description of second device
// initialize the array of poi
BufPtrs[0] = Buffer1;
BufPtrs[1] = Buffer2;
BufPtrs[2] = NULL;
// last entry should be NULL
ftStatus = FT_ListDevices(BufPtrs,&numDevs,FT_LIST_ALL|FT_OPEN_BY_DESCRIPTION);
if (ftStatus == FT_OK)
{
printf("description %s\n", Buffer1);
}
else
{
printf("device description fail\n");
}
ftStatus = FT_OpenEx((void*)"UMFT200XD", FT_OPEN_BY_DESCRIPTION, &ftHandle);
if (ftStatus != FT_OK)
{
printf("Can't open FT232H device! \n");
}
else
{
DWORD dwNumBytesRead, dwNumInputBuffer;
//char dwNumInputBuffer[256];
char InputBuffer[256];
printf("device opened succesfully\n");
/* EVERYTHING ABOVE THIS LINE WORKS CORRECTLY */
// Reset the FT232H
ftStatus |= FT_ResetDevice(ftHandle);
// Purge USB receive buffer ... Get the number of bytes in the FT232H receive buffer and then read them
ftStatus |= FT_GetQueueStatus(ftHandle, &dwNumInputBuffer);
if ((ftStatus == FT_OK) && (dwNumInputBuffer > 0)) // this returns FT_OK but dwNumInputBuffer == 0
{
printf("getqstatus success\n");
FT_Read(ftHandle, &InputBuffer, dwNumInputBuffer, &dwNumBytesRead);
}
ftStatus |= FT_SetUSBParameters(ftHandle, 65536, 65535);
// Set USB request transfer sizes
ftStatus |= FT_SetChars(ftHandle, false, 0, false, 0);
// Disable event error characters
ftStatus |= FT_SetTimeouts(ftHandle, 5000, 5000);
// Set rd/wr timeouts to 5 sec
ftStatus |= FT_SetLatencyTimer(ftHandle, 16);
// Latency timer at default 16ms
ftStatus |= FT_SetBitMode(ftHandle, 0x0, 0x00);
// Reset mode to setting in EEPROM
ftStatus |= FT_SetBitMode(ftHandle, 0x0, 0x02);
// Enable MPSSE mode
// Inform the user if any errors were encountered
if (ftStatus != FT_OK)
{
printf("failure to initialize FT232H device! \n");
}
else
{
printf("device initialized\n");
}
DWORD dwNumBytesToSend = 0, dwNumBytesSent = -1;
BYTE OutputBuffer[1024];
// this is to try to enable internal loopback, ie no i2c device attached to ft200xd
// however even when i try all this w/devcies attached behaviour of code is identical
// i cannot read any info from my umft200xd
printf("Enabling internal loopback...");
OutputBuffer[dwNumBytesToSend++] = 0x84;
// Enable internal loopback
ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent);
if (ftStatus != FT_OK || dwNumBytesSent != dwNumBytesToSend)
{
printf("failed");
}
else
{
// this call succeeds, all calls to FT_Write() succeed, I just cannot read anything via GetQueueStatus() nor FT_Read()
printf("loopback enabled\n");
}
dwNumBytesToSend = 0;
//#########################################################################################
// Synchronise the MPSSE by sending bad command AA to it
//#########################################################################################
dwNumBytesSent = -1;
//BYTE OutputBuffer[1024];
// Used as an index to the buffer
memset( &OutputBuffer, 0, sizeof (OutputBuffer) );
OutputBuffer[dwNumBytesToSend++] = 0xAA;
// Add an invalid command 0xAA
printf("sending [0] = %02x, [1] = %02x", OutputBuffer[0], OutputBuffer[1] );
ftStatus = FT_Write(ftHandle, OutputBuffer, dwNumBytesToSend, &dwNumBytesSent); // Send to FT232H
// Check if the bytes were sent off OK - this call succeeds
if(dwNumBytesToSend != dwNumBytesSent)
{
printf("Write timed out! \n");
FT_Close(ftHandle);
getchar();
return 1;
}
ftStatus = -1;
// Now read the response from the FT232H. It should return error code 0xFA
// followed by the actual bad command 0xAA
dwNumInputBuffer = 0;
bool bCommandEchod = false;
int ReadTimeoutCounter = 0;
ftStatus = FT_GetQueueStatus(ftHandle, &dwNumInputBuffer);
// Get # bytes in input buffer
while ((dwNumInputBuffer < 2) && (ftStatus == FT_OK) && (ReadTimeoutCounter < 500))
{
// Sit in this loop until
// (1) we receive the two bytes expected
// or (2) a hardware error occurs causing the GetQueueStatus to return an error code
// or (3) we have checked 500 times and the expected byte is not coming
ftStatus = FT_GetQueueStatus(ftHandle, &dwNumInputBuffer); // Check queue status
ReadTimeoutCounter += 1;
Sleep(1);
// short delay
}
printf("status = %08x, FT_OK = %08x, cnt = %i, dwNumInputBuf = %08x", ftStatus, FT_OK, ReadTimeoutCounter, dwNumInputBuffer);
// If the loop above exited due to the byte coming back (not an error code and not a timeout)
// then read the bytes available and check for the error code followed by the invalid character
if ((ftStatus == FT_OK) && (ReadTimeoutCounter <= 500))
{
ftStatus = FT_Read(ftHandle, &InputBuffer, dwNumInputBuffer, &dwNumBytesRead); // Now read data
printf("dwNumBytesRead = %08x, data read = %08x\n", dwNumBytesRead, InputBuffer );
// Check if we have two consecutive bytes in the buffer with value 0xFA and 0xAA
//bool bCommandEchod = false;
for (int dwCount = 0; dwCount < dwNumBytesRead - 1; dwCount++)
{
if ((InputBuffer[dwCount] == BYTE(0xFA)) && (InputBuffer[dwCount+1] == BYTE(0xAA)))
{
bCommandEchod = true;
break;
}
}
}
// If the device did not respond correctly, display error message and exit.
if (bCommandEchod == false)
{
printf("fail to synchronize MPSSE with command 0xAA \n");
//getchar();
//return 1;
}
else if (bCommandEchod == true)
{
printf("synchorinization complete\n");
}