janos_dfc
janos_dfc

Reputation: 87

I2C Bus writable/readable flags on I2C file descriptor

I would like to connect multiple thermometers to a Raspberry board via the I2C bus on Raspbian, using a C++ application with an event loop containing pselect(). Opening the default i2c-0 file descriptor (and using the linux/i2c-dev.h interface ) from the application, I'd add the file descriptor to pselect()'s fd_set *readfds and fd_set *writefds. I wonder if this file descriptor would ever become FD_WRITEABLE or FD_READABLE ? Since I2C is a master/slave setup, it should be impossible for a thermometer to initiate communication, because both the read and write operations are initiated by the application in userland, so I suppose FD_READABLE should be out of the question, am I right?

I haven't found anything about using the I2C with pselect, what does readable/writable mean in a socket file descriptor? And why regular files don't bother with that? was the closest answer I stumbled upon: "Readable means there is data or a FIN present in the socket receive buffer. Writable means there is space available in the socket send buffer." Is an I2C device buffered? Can I depend on this mechanism?

Thanks a lot!

Upvotes: 0

Views: 816

Answers (1)

samuelnj
samuelnj

Reputation: 1637

So there's this:

The pselect() and select() functions shall support regular files, terminal and pseudo-terminal devices, STREAMS-based files, FIFOs, pipes, and sockets. The behavior of pselect() and select() on file descriptors that refer to other types of file is unspecified.

The i2c-0 is a character device file type: https://www.kernel.org/doc/Documentation/i2c/dev-interface

pselect() really doesn't guarantee anything for that file. So, no you can't depend on it.

Upvotes: 1

Related Questions