简体   繁体   中英

Interrupts using i2c and Python

Some background:

I have an i2c device (MCP23017), which has 6 switches connected to its GPIO ports. The MCP23017 is connected to a Raspberry Pi via i2c.

I'm able to read the state of each of the switches as required.

My issue is in regards to interrupts. I'm using the WiringPi2 library for Python, which allows me to interface with the MCP23017 under Python. From the research I've done, the WiringPiISR library allows for i2c interrupt protocols to be run, although it only seems to work (properly) under C.

My question is this: is there a simple solution for implementing i2c interrupts under Python?

I'm considering dropping Python for C for this particular project, but the GUI interface has already been written (in Python), so I'd like to keep that as a last option.

Any guidance/input/comments would be greatly appreciated!

Thanks!

As far as I understand, WiringPiISR library only allows you to configure a Pin to be an interrupt and define its type (ie, edge based or level based). Since you're talking about I2c interrupt, there is no way you can have I2C interrupt as in this case, Your Rpi works as a master device and other connected device(s) as slave(s). Since in I2C, communication is always initiated by the master, slaves can not interrupt you. (at least not via I2C channel)

Hope it helps.

There are devices that have an interrupt line in addition to I2C lines. For example I have a rotary encoder I2C interface board from duppa.net which has 5 pins - SDA, SCL, +, - and interrupt.

The python library that comes with it configures the Raspberry Pi GPIO 4 line as an input edge triggered interrupt and sets up callbacks. On a change (the rotary encoder is rotated for instance) the interrupt line wiggles, the interrupt handler reads the status register over I2C and calls the appropriate callback.

So this is completely conventional interrupt usage in the context of I2C doing to usual task of freeing the master from having to poll the bus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM