I2C Write to Register

I’m trying to write into a register of an I/O expander (MCP23016) but that doesn’t work. (nicely described here) With the old library and on an older WinCE it worked once.
Reading inputs works without problems (all GPIOs are inputs by default). So there is no hardware issue. But for reading I don’t need to set any regsiter address.

I can’t read what I wrote, but I don’t get any error message.

Attached the full VS 2013 project. Here the main part:

[OpenI2C();

	tVersion ver;

	I2c_GetVersion(&ver);  //< Get I2C library vaersion
	printf("I2CDemo (I2CLib Version %d.%d.%d)\n", ver.Major, ver.Minor, ver.Build);
	
	uchar gpioDir[1] = { 0x00 };

	//set 7bit address of MC23016
	if (!I2c_SetConfigInt(i2c, L"SlaveAddr", 0x23, StoreVolatile))			
		printf("Error: I2c_SetConfigInt Slave address\n");

	//set register address size to 8 bit
	I2c_SetConfigInt(i2c, L"RegisterAddrSize", 8, StoreVolatile);

	//set register address for IODIR0
	I2c_SetConfigInt(i2c, L"RegisterAddr", 0x06, StoreVolatile);

	//set all GPIOs as outputs
	if (I2c_Write(i2c, gpioDir, 1) != 1)
		printf("Error: write data\n");

	//read IODIR0 back to check if correctly set
	BYTE data[8] = { 0 };
	DWORD returnValue = 0;
	I2c_SetConfigInt(i2c, L"RegisterAddrSize", 8, StoreVolatile);
	I2c_SetConfigInt(i2c, L"RegisterAddr", 0x06, StoreVolatile);
	returnValue = I2c_Read(i2c, (DWORD*)data, 8);
	printf("value:  %#8x\n", returnValue);

	//set register address for IODIR1
	I2c_SetConfigInt(i2c, L"RegisterAddr", 0x07, StoreVolatile);
	
	//set all GPIOs as outputs
	if (I2c_Write(i2c, gpioDir, 1) != 1)
		printf("Error: write data\n");

	//set register address and size to 0
	I2c_SetConfigInt(i2c, L"RegisterAddrSize", 0, StoreVolatile);
	I2c_SetConfigInt(i2c, L"RegisterAddr", 0, StoreVolatile);

	//set all outputs on
	uchar i2cDataWrite[2] = { 0xFF, 0xFF };
	if(I2c_Write(i2c, i2cDataWrite, 2) != 2)
		printf("Error: write data\n");
       
	I2c_Close(i2c);
	I2c_Deinit(i2c);
	i2c = NULL;][2]

.

Dear @TriUrs,

Could you probe I2c signals? Did you see the device address after slave address? Please capture the i2c waveform and share with us.

I don’t have a similar slave here to probe and confirm but I quickly checked with default demo application with RTC and seems to be working and code is here

I2c probe

Uh oh, I’m so sorry. I found the problem. I wrote to the wrong register and after that the bus was blocked (SDA set to zero). Therefore I could not read/write anything. But I didn’t get any error, just read/write bytes response is -1 (not written/read). But I didn’t noted that first.

So everything works with the new/current Toradex Library. Thank you for your support.

@TriUrs,

Thanks for letting us know the update. I guess you are getting -1 (I2C_RW_FAILURE) error from I2c_Read and I2c_Write APIs if there are any errors.