我做了FTDI芯片libMPSSE.dll的导入,并在I2C模式下使用它。我的测试设备是BMP280传感器。我试着读取它的ID寄存器,它包含0x58值。问题是方法
I2C_设备广告()
始终返回0xFF,尽管我在逻辑分析器中看到传感器正确应答:
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()包含4个0xFF,尽管传感器写入数据线(图片上的SDA)其他值:
我的代码有什么问题?为什么我能写得正确,但阅读是错误的?谢谢您!