I am trying to access the I2C bus. Here is my code:
void TestI2C()
{
HANDLE hParent;
HANDLE hI2C;
hParent = CreateFile(L"BUS1:", 0, 0, NULL, 0, 0, NULL);
if (hParent == INVALID_HANDLE_VALUE)
{
_tprintf(_T("Error opening parent\n"));
hParent = NULL;
goto error;
}
for (int i=0; i<5; i++)
{
DWORD i2cDeviceID = SOCGetI2CDeviceByBus(i);
_tprintf(_T("Bus: %d, Device ID: %d\n"), i, i2cDeviceID);
if (i2cDeviceID == -1)
continue;
hI2C = I2COpen(i2cDeviceID);
if (hI2C == NULL)
{
_tprintf(_T("hI2C (%d) is NULL\n"), i2cDeviceID);
}
else
{
_tprintf(_T("hI2C (%d) is %d\n"), i2cDeviceID, hI2C);
break;
}
}
if (hI2C == NULL)
{
goto error;
}
error:
if (hI2C != NULL)
{
I2CClose(hI2C);
}
if (hParent != NULL)
{
CloseHandle(hParent);
}
}
When I get a valid device ID (when 1<=i<=3) and I2COpen is called the return value is always NULL. Can anyone tell me what I'm doing wrong?
David