2

When managing smbus/I2C devices using python with the RPi, is there a way to register a handler function so it is called when a byte is available for read, like in Arduino Wire.onReceive()?
Otherwise, is the only way to do this to read periodically from the device (and handle the exception that is thrown if there is nothing to read)?
Thanks!

2

The Raspberry Pi I2C drivers only support master mode (i.e. the Pi initiates all I2C transactions).

There is no point in a Wire.onReceive method as the Pi does not accept unsolicited messages.

EDITED TO ADD

Some I2C devices have interrupt lines. If your device does then this could be connected to a Pi gpio and could be used to trigger a Pi I2C transaction.

EDITED TO ADD

I thought the Raspberry Pi's I2C implementation was well documented. Apparently it isn't or I can't find it.

For I2C see http://en.wikipedia.org/wiki/I%C2%B2C

Do a search for raspberry pi acting in slave mode. There are no implementations.

1

This is in C, and I'm not promising it will get you what you want (see joan's comments below), but it does meet this criteria:

is there a way to register a handler function so it is called when a byte is available for read

If you use the native i2c-dev, ioctl() based interface, you get a regular file descriptor. You can then use regular old POSIX low levelread() and poll(), etc. The later is useful in implementing call back functions.1

More information at:

You may want to go through other things in that directory (Documentation/i2c/) but that's the best place to start. It's the same path as in a kernel source tarball, if you have one of those.

I've used the native userland interface via C and C++ quite a bit with a bunch of devices. The only caveat I have is that some of them respond better to plain read() and write() than to the i2c_smbus_ functions; if at first you don't succeed try the same thing only different.


1. If you don't know how to implement a callback with poll(), it is a fairly common practice but you may need a separate thread. If you just want to wait for other stuff at the same time and handle events as they happen, a single thread is ok; if you want to do unrelated things at the same time, that requires multiple threads.

  • For a byte to be available to read it must have been already read by the I2C driver during a Pi initiated I2C transaction. I'm not sure what scenario exists where getting a callback after polling for data is particularly useful. – joan Nov 12 '14 at 14:52
  • @joan The context you are implying (that this is not part of a transaction) is not clear in the question (perhaps it's packed into "Arduino Wire.onReceive()"?) -- in any case, I saw "When managing smbus/I2C devices...is there a way to register a handler function so it is called when a byte is available for read?" and the answer is yes. As to whether that's a useful tactic because of the transactional nature of the interface I can't say -- you might be right. I haven't found a need for it WRT I2C handles. – goldilocks Nov 12 '14 at 15:02
  • 1
    Yes, you are quite right. On the Arduino the onReceive method is only used when a slave device receives a message from the master. The Pi's have no provision to be a slave device (although the needed gpios are exposed on the A+/B+ I can't see anyone writing a driver). As a side note it would be helpful if people mention the model of device they are using when asking for help. – joan Nov 12 '14 at 15:18
  • @joan Okay. I'm unaware of the relevance of slave vs. master devices (the pi is my only i2c experience), so this would not have occurred to me! Again, it would be great if you could add a few sentences to your answer to make this context a bit more explicit ;) I'd guess the OP lacks the terminology to explain/understand the question better himself. – goldilocks Nov 12 '14 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.