M66 OpenCPU with IIC MCP23017 GPIO Extender

I am working on M66 OpenCPU. My application needs lots of GPIO’s.
Evantually I run out of M66 module GPIO’s. So I diceided to use GPIO extender.
I am having MCP23017 from Micrchip. Link here

I am using MCP23017 with IIC protocol.
M66 has openCPU API’s for IIC protocal.But I am not able to get any result out of it.
To start with GPIO extender, I tried it with arduino and played with GPIO extender. And it works
fine. Arduino code is here.

Now I have some doubts. And I will put them as comparitively to get
you idea.
I have used following code
void setup()
{
Wire.begin(); // starts and wake up I2C bus

Wire.beginTransmission(0x20); // MCP23017 slave address
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // set all of port A to outputs
Wire.endTransmission();
Wire.beginTransmission(0x20);
Wire.write(0x01); // IODIRB register
Wire.write(0x00); // set all of port B to outputs
Wire.endTransmission();
}

Above code sets up IIC,writes IODIRA register and make port A pin as output.

void loop()
{
Wire.beginTransmission(0x20);
Wire.write(0x12); // address port A
Wire.write(0x01); // Make Pin 1 HIGH
Wire.endTransmission();
delay(DELY);

Wire.beginTransmission(0x20);
Wire.write(0x12); // address port A
Wire.write(0x00); // Make Pin 1 LOW
Wire.endTransmission();
delay(DELY);

}

Now above code wites to port A and makes pin 1 of port HIFH and then LOW.

Now to implemet all this in M66 OpenCPU, I really dont know out it following API’s.

IF any one has any experience with working of M66 IIC or MCP23017 please help or altleast give some direction.

following API’s I need help about
1.Ql_IIC_Init
2.Ql_IIC_Config
3.Ql_IIC_Write
4.Ql_IIC_WriteRead

Dear shree,

Look in the file “example_iic.c” - it explains how to start.

I’m sure there is a MCP23017-driver in c for Arduino that explains the chip, also see datasheet.

All the best:

/Björn

very easy way is to test Arduino code with M66 :slight_smile:

1 Like

Dear BdeB,
Thank you for response.
I have tried example_iic.c. Still I am not getting working iic.
I used M66 API’s as follow

iResult = Ql_IIC_Init(0,PINNAME_RXD_AUX,PINNAME_TXD_AUX,1);
if (iResult != QL_RET_OK)
{
APP_DEBUG(“Fail to init I2C[%d]:%d\r\n”,iResult);
}
else
{
APP_DEBUG(“Init I2C pass”);
}

   	iResult = Ql_IIC_Config(0,TRUE, 0x20, 300);
   	if (iResult != QL_RET_OK)
       {
           APP_DEBUG("Fail to configure I2C[%d]:%d\r\n",iResult);
       }
   	else
   	{
   		APP_DEBUG("Config I2C pass");
   		u32 ret;
   		Ql_IIC_Write(1, 0x20, 0x00,1);
   		Ql_Sleep(100);
   		Ql_IIC_Write(1, 0x20, 0x00,1);
   		Ql_Sleep(100);
   		Ql_IIC_Write(1, 0x20, 0x12,1);
   		Ql_Sleep(100);
   		Ql_IIC_Write(1, 0x20, 0x01,1);
   		Ql_Sleep(100);
            Ql_IIC_Write(1, 0x20, write_buffer,4);

}

but sill not getting result as expected.
Follwing error apears in log though, detail log here.
[2019-11-11 15:44:45:396_R:] Fail to init I2C[-16]:268671953
[2019-11-11 15:44:45:396_R:] Fail to configure I2C[-307]:268671953

It will be great if you help with this.

1 Like

Dear WizIO,
Thank you for your response.
You do great work in open source and I follow it on github. Really appreciate your work.

Though if possible, you can give some of your expert guidance to me on iic problem.

try: shift left device slave address ( 0x20 << 1 ) to be 8-bits address