FTDI libMPSSE.dll I2C_DeviceRead()始终返回0xFF

时间:2018-09-19 13:40:42

标签: vb.net pinvoke i2c ftdi

我导入FTDI芯片libMPSSE.dll,并在I2C模式下使用它。我的测试设备是传感器BMP280。我尝试读取它的ID寄存器,其中包含0x58值。问题是方法 I2C_DeviceRead()始终返回0xFF,尽管我在逻辑分析仪中看到传感器正确回答: Logic analyzer shows correct answer of the sensor

这些方法的原始声明为:

FT_STATUS I2C_DeviceRead(FT_HANDLE handle, uint32 deviceAddress, uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransferred, uint32 options)
FT_STATUS I2C_DeviceWrite(FT_HANDLE handle, uint32 deviceAddress, uint32 sizeToTransfer, uint8 *buffer, uint32 *sizeTransferred, uint32 options)

我这样导入此方法:

<DllImport(DLL_MPSSE_PATH, SetLastError:=True, CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function I2C_DeviceRead(ByVal handle As Integer, ByVal deviceAddress As Integer, ByVal sizeToTransfer As Integer, ByVal buffer As Byte(), ByRef sizeTransfered As Integer, ByVal options As I2C_TRANSFER_OPTIONS) As Integer
End Function

<DllImport(DLL_MPSSE_PATH, SetLastError:=True, CallingConvention:=CallingConvention.Cdecl)>
Private Shared Function I2C_DeviceWrite(ByVal handle As Integer, ByVal deviceAddress As Integer, ByVal sizeToTransfer As Integer, ByVal buffer As Byte(), ByRef sizeTransfered As Integer, ByVal options As I2C_TRANSFER_OPTIONS) As Integer
End Function

I2C_TRANSFER_OPTIONS是带有控制位掩码的枚举。 并根据传感器数据表进一步称呼它(伪代码)。首先,写寄存器地址:

Dim sensorAddr as Integer = &H77
Dim idRegister as Byte() = {&HD0}
Dim options as Integer = &H13 'I2C_TRANSFER_OPTIONS_START_BIT | I2C_TRANSFER_OPTIONS_STOP_BIT | I2C_TRANSFER_OPTIONS_FAST_TRANSFER_BYTES
Dim transferred as Integer = 0
I2C_DeviceWrite(myhandle, sensorAddr, 1, idRegister, transferred, options)

然后读取传感器寄存器的内容:

Dim readLen as Integer = 1
Dim readBuf(readLen-1) as Byte
I2C_DeviceRead(myhandle, sensorAddr, readLen, readBuf, transferred, options)

如果是readLen,例如4,因此readBuf()包含四个0xFF,尽管传感器将其他值写入数据线(图片中的SDA): Each requested byte is 0xFF

我的代码有什么问题?为什么我可以正确书写,但阅读错误?谢谢!

0 个答案:

没有答案