Sonny_Jim
Posts: 45
Joined: Tue Oct 23, 2012 4:15 pm

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 9:26 am

gordon@drogon.net wrote:
Sonny_Jim wrote:Just curious, why the recommendation to run the mcp23017 at 3.3v rather than 5v? I run mine at 5V with seemingly no problems.
Less chance of doing something silly and blowing up a Pi...

-Gordon
Right, makes sense. Running it at 5V with the I2C operating at 3.3v was one of the main reasons why I rejigged my hardware project to use an MCP23017, meant I could ditch a whole load of level shifters.

To get back on topic, a couple of questions:

1. With wiringPi, how can I change the I2C speed?
2. Gordon, do you have any plans to update wiringPi so the MCP23017 can be loaded up with data faster, rather than a bit at a time?
3. Could someone give me an example of when I would want to use the MCP23017 interrupts? Looks to me that it's only handy in keyboard/input matrixes, you just monitor one pin for a change rather than all 8/16 of them.

Massi
Posts: 1690
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 9:42 am

Sonny_Jim wrote:3. Could someone give me an example of when I would want to use the MCP23017 interrupts? Looks to me that it's only handy in keyboard/input matrixes, you just monitor one pin for a change rather than all 8/16 of them.
what do you need with "all 8/16 of them"?

i use interrupts to manage a pluviometer and some buttons connected to my outdoor mcp23017, so i can have multiple inputs to the raspi using only the mcp interrupt pin.

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 1:03 pm

Sonny_Jim wrote:
gordon@drogon.net wrote:
Sonny_Jim wrote:Just curious, why the recommendation to run the mcp23017 at 3.3v rather than 5v? I run mine at 5V with seemingly no problems.
Less chance of doing something silly and blowing up a Pi...

-Gordon
Right, makes sense. Running it at 5V with the I2C operating at 3.3v was one of the main reasons why I rejigged my hardware project to use an MCP23017, meant I could ditch a whole load of level shifters.
Yes, that will work well. Just don't mis-wire it...

To get back on topic, a couple of questions:

1. With wiringPi, how can I change the I2C speed?
No idea. You used to be able to do it by loading the module via the gpio command, but in the brave new dev-tree world this doesn't work anymore. You'll need to look-up the dev-tree approved methods
2. Gordon, do you have any plans to update wiringPi so the MCP23017 can be loaded up with data faster, rather than a bit at a time?
It's tricky. The biggest issue right now is adding new functions into the base system - that would mean an ABI change which means all existing programs needs re-linking. (or re compiling) I don't know the best way to go about this.. Open to suggestions.
3. Could someone give me an example of when I would want to use the MCP23017 interrupts? Looks to me that it's only handy in keyboard/input matrixes, you just monitor one pin for a change rather than all 8/16 of them.
Lots of reasons - as it saves you polling the chip to detect a pin-change input. I leave the implementation of this as an exercise to the user.

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 3:01 pm

about some points of this discussion I have a few question re-marks...:

1) about 3.3V vs. 5V: if both i2c lines are pulled up strongly enough to 3.3V on the Raspi-side (e.g., by 2-4.7k each), would it actually matter if the Vc is 5V?

2) about reading "whole" bytes instead of single bits: would this code I suggested above not be faster?

Code: Select all

uint8_t data;
#define MCP23017_0_addr  0x20

int mcp23017_0= wiringPiI2CSetupInterface("/dev/i2c-1", MCP23017_0_addr);
wiringPiI2CWriteReg8 (mcp23017_0, 0x00, 0x00) ;  // bank A:= output (read)
wiringPiI2CWriteReg8 (mcp23017_0, 0x01, 0xff) ;  // bank B:= input (write)

data  = wiringPiI2CReadReg8 (mcp23017_0, 0x12) ;   // read bank A data register (0x12)
//...
wiringPiI2CWriteReg8 (mcp23017_0, data, 0x13);   // write data to bank B (0x13)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 3:10 pm

davenull wrote:about some points of this discussion I have a few question re-marks...:

1) about 3.3V vs. 5V: if both i2c lines are pulled up strongly enough to 3.3V on the Raspi-side (e.g., by 2-4.7k each), would it actually matter if the Vc is 5V?
Only if you're careless and connect a 5v signal to a Pi's 3.3v GPIO line. I've seen it done. Pi no more. I suggested using 3.3v to start with...

2) about reading "whole" bytes instead of single bits: would this code I suggested above not be faster?
Yes, but it's not in wiringPi as a native function. wiringPi does support a "writeByte" function, but this is only for the internal Pi pins, not for external devices. I have plans (and code) to enable writeByte to work on external interfaces in a device independent and efficient manner, however it will break the current ABI.

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 3:43 pm

thank you for your clarification!

about
that would mean an ABI change which means all existing programs needs re-linking. (or re compiling) I don't know the best way to go about this.. Open to suggestions.
I would suggest to - perhaps - implement Arduino Wire-class equivalent functions, if possible...? 8-)
http://playground.arduino.cc/Main/WireL ... dReference
https://www.arduino.cc/en/Reference/Wire

https://github.com/arduino/Arduino/tree ... aries/Wire
( https://github.com/arduino/Arduino/tree ... aries/Wire )

Code: Select all

Wire.
    begin()
    requestFrom()
    beginTransmission()
    endTransmission()
    write()
    available()
    read()
    onReceive()
    onRequest() 
    end()
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 4:58 pm

davenull wrote:thank you for your clarification!

about
that would mean an ABI change which means all existing programs needs re-linking. (or re compiling) I don't know the best way to go about this.. Open to suggestions.
I would suggest to - perhaps - implement Arduino Wire-class equivalent functions, if possible...? 8-)
I know what the Arduino Wire library is. I don't use it. I also don't do "classes" as that implys C++ and I don't do C++.

Why not do it yourself if you want it?

I was looking for suggestions to the current subject - namely how to implement register reads for GPIO expanders without breaking the ABI.

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 5:17 pm

I didn't know that you know it, and I don't know how to make it work with Raspbian
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 6:11 pm

davenull wrote:I didn't know that you know it, and I don't know how to make it work with Raspbian
It won't work with Raspbian - if you use the standard kernel interface. That's because the Pi + Raspbian is not a microcontroller, it's a multi-user, multi-tasking system and the kernel is the one that's is supposed to own all the hardware...

You can get lower level I2C access if you like and there are libraries that let you do this - you're already using PiGPIO, so all you need to do is read all the documentation for the I2C drivers there and you'll get something pretty close...

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 6:30 pm

I assumed it wouldn't work out of the box, although it's a C lib, but I assumed you would like it as a suggestion for your planned enhancements.
I'll soon be trying to use these following API functions if I once have got the mcp23017 from China (30 days shipping time, but anyway, at least free of shipping charge), and it would be great to be able to run it at 400kHz clock speed and also to use softPwm for it's pins.

Code: Select all

uint8_t data;
#define MCP23017_0_addr  0x20

int mcp23017_0= wiringPiI2CSetupInterface("/dev/i2c-1", MCP23017_0_addr);
wiringPiI2CWriteReg8 (mcp23017_0, 0x00, 0x00) ;  // bank A:= output (read)
wiringPiI2CWriteReg8 (mcp23017_0, 0x01, 0xff) ;  // bank B:= input (write)

data  = wiringPiI2CReadReg8 (mcp23017_0, 0x12) ;   // read bank A data register (0x12)
//...
wiringPiI2CWriteReg8 (mcp23017_0, data, 0x13);   // write data to bank B (0x13)
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 6:32 pm

davenull wrote:I assumed it wouldn't work out of the box, although it's a C lib, but I assumed you would like it as a suggestion for your planned enhancements.
I'll soon trying to use these API functions if I once have got the mcp23017 from China (30 days shipping time, but anyway, at least free of shipping charge)
You keep sending me this code (didn't I write it in the first place?) You keep missing the point. Go and lookup what an ABI is and how it would affect a few million Pi owners if I changed it.

-Gordon
--
Gordons projects: https://projects.drogon.net/

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Tue Mar 08, 2016 6:37 pm

I just wanted to show the code I wanted to try (not bit read/write), and I don't understand what an ABI is, sorry that I disturbed.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Sonny_Jim
Posts: 45
Joined: Tue Oct 23, 2012 4:15 pm

Re: reading 2x MCP23017: how long for reading 32 bits?

Fri Mar 11, 2016 8:10 am

You used to be able to do it by loading the module via the gpio command, but in the brave new dev-tree world this doesn't work anymore. You'll need to look-up the dev-tree approved methods
I found this on another thread:

Code: Select all

modprobe -r i2c_bcm2708
modprobe i2c_bcm2708 baudrate=1200000
I have no real way of testing that it has worked, but I get this in dmesg so *shrug*

Code: Select all

[  327.662899] bcm2708_i2c 3f804000.i2c: BSC1 Controller at 0x3f804000 (irq 79) (baudrate 1200000)

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: reading 2x MCP23017: how long for reading 32 bits?

Fri Mar 11, 2016 8:27 am

baudrate=1200000 (highspeed i2c ?) is surely amazing, but for my own purposes baudrate=400000 would be far enough fast.
I'll try that ASAP but actually also have no idea how to test the true data transfer speed then either way.
According to my TO question that would perhaps accelerate the transmission speed for writing to 2 devices from >1ms to about 300µs (edited), what would be powerful enough for my purposes.
Last edited by davenull on Fri Mar 11, 2016 11:26 am, edited 1 time in total.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
gordon@drogon.net
Posts: 1990
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: reading 2x MCP23017: how long for reading 32 bits?

Fri Mar 11, 2016 10:41 am

Sonny_Jim wrote:
You used to be able to do it by loading the module via the gpio command, but in the brave new dev-tree world this doesn't work anymore. You'll need to look-up the dev-tree approved methods
I found this on another thread:

Code: Select all

modprobe -r i2c_bcm2708
modprobe i2c_bcm2708 baudrate=1200000
I have no real way of testing that it has worked, but I get this in dmesg so *shrug*

Code: Select all

[  327.662899] bcm2708_i2c 3f804000.i2c: BSC1 Controller at 0x3f804000 (irq 79) (baudrate 1200000)
That's basically what gpio load i2c 1200 does - however it doesn't work in a device-tree enabled system.

-Gordon
--
Gordons projects: https://projects.drogon.net/

Return to “C/C++”