我有一个项目,使用FTDI FT201X作为i2c从设备的USB,而i2c主设备是AVR微控制器。我在Windows 10计算机上使用WPF Core 3.1 C#。基本上,使用FTDI芯片的所有设备都可以正常工作,除了无论如何尝试,我都无法成功地将数据从PC发送到FTDI芯片。 D2XX Write函数说它成功并且没有返回错误,但是当我尝试读取时,缓冲区中没有任何数据。
此后,我编写了一个小型测试程序,试图找出问题所在,但问题仍然存在。基本上,当单击一个按钮时,我们将按序列号打开设备,然后将命令写入设备的缓冲区,与AVR握手以告知其读取信息,然后等待AVR将握手引脚驱动为低电平(表示已收到)数据。
public class USBLibrary
{
byte targetDeviceCount = 0;
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
public FTDI connectedUSBDevice;
// Called from button click event
public void ConnectUSB()
{
bool isOK = true;
byte numOfBytes = 1;
uint bytesWritten = 0;
bool usbInPinIsHigh = false; // Tracks USB In Pin
byte lowMask = 0b00010000; // CBUS 0 is output (4-7), all pins low (0-3) (Default Setting)
byte highMask = 0b00010001; // CBUS 0 is output (4-7), CBUS 3 is high
byte inPinMask = 0b00001000; // AND with pin states to get input pin value (Bus3)
byte pinStates = 0; // Used to get the current pin values
double timeout = 0;
// Create new instance of the FTDI device class
connectedUSBDevice = new FTDI();
// Determine the number of FTDI devices connected to the machine
ftStatus = connectedUSBDevice.OpenBySerialNumber("P00001");
/*** Write to Device ***/
byte[] firmwareCmd = new byte[numOfBytes];
firmwareCmd[0] = 128; // 128 is Get Firmware Command
// firmwareCmd[1] = 61; // Just Testing
// Write Firmware Command to Tx buffer
ftStatus = connectedUSBDevice.Write(firmwareCmd, numOfBytes, ref bytesWritten);
Trace.WriteLine(bytesWritten);
// Handshake with BW Device
isOK = DeviceHandshake(lowMask, highMask, inPinMask);
// Check if handshake failed
if (isOK == false)
{
return;
}
Task.Delay(10);
// Wait until message is sent
while ((usbInPinIsHigh == false) && (timeout <= 1000))
{
Task.Delay(1);
// Check for USB In pin to go high. Signals FW transfer is complete and to retrieve.
ftStatus = connectedUSBDevice.GetPinStates(ref pinStates);
// Is input pin high or low?
if ((pinStates & inPinMask) == inPinMask) // In pin high
{
usbInPinIsHigh = true; // Means uC finished sending data
}
timeout++;
}
// TEST: displays timeout amount for testing
Trace.WriteLine("Timeout=" + timeout);
ftStatus = connectedUSBDevice.Close();
}
}
注意:对于此代码,为清楚起见,我已经删除了许多错误检查代码。同样,握手代码也未显示,因为它不相关:升高输出引脚,监听AVR以升高输出引脚,降低输出引脚,监听AVR降低至输出引脚。
[在AVR方面,我们只是轮询FT201X的引脚变高,然后与芯片握手。然后我们简单地阅读。读取函数始终返回0。
我怀疑问题在于i2c,因为有3个IO扩展器芯片控制LED和按钮,我们可以对其进行读写。此外,FT芯片还具有“获取USB状态”功能,您可以在其中通过发送命令并通过i2c读取结果来检查设备状态。这样做时,我总是会返回正确的0x03“已配置”状态。这样我们就可以通过i2c从芯片读取数据。
还有一个函数将返回缓冲区中等待读取的字节数……当我这样做时,它总是说0字节。
[从好的方面来说,我用一块新的芯片替换了它,以防万一它很糟糕,我们再次得到相同的结果。
除了使用FT_Prog之外,在设置芯片方面还缺少什么,例如初始化过程或设置寄存器之类的东西?还是我需要以某种方式将写入的字节推到队列的开头或其他内容才能读取?有人以前看过这样的东西吗?
鉴于我没有影响结果,我或者缺少过程中的关键部分,或者他们的驱动程序/芯片版本有问题。已经过了三个星期,我没有主意了,因为扯掉了大块头发,所以我的头发不整齐。请保存我的头发。
通过示波器检查您的I2C主设备为从设备(FT201x)提供时钟。尝试仅控制I2C(剥离GPIO控件),然后检查是否可以通过这种方式隔离问题。我想您对FT201X datasheet非常熟悉。祝你好运!