public class I2CBus extends Object implements AutoCloseable
It provides 3 sets of operations for doing IO :
read and
write.transactions (ie. uninterrupted reads and writes)
SMBUS operations are a subset of what I2C can achieve but propose common interactions. However,
getFunctionnalities should be used in order to understand what operations the underlying
driver support.
I2C is a standard but your master device and the slaves devices it talks do probably do not support everything properly. For instance, as is, the Raspberry Pi only supports a few SMBUS operations and do not support transactions involving more than one message at a time (which defeats the purpose of having them in the first place). Furthermore, talking to an Arduino will result in timing issues if it is too slow to answer. Communication between those two is very much possible but requires more preparation and testing. This is an example of how things are not always that simple.
In this API, `command` is just another word for what is sometimes called `register`. Essentially, it means that before reading or writing bytes, the master writes a byte specifying a command, something to do with the following byte(s). If an SMBUS functionality is not available, this can often be emulated by explicitly sending the command byte and then reading or writing as needed.
| Constructor and Description |
|---|
I2CBus(int busNumber)
Opens an I2C bus by number, `/dev/i2c-$busNumber`.
|
I2CBus(String path)
Opens an I2C bus on the given path.
|
| Modifier and Type | Method and Description |
|---|---|
int |
blockProcessCall(int command,
I2CBlock block)
SMBUS RPC-like operation, writing several bytes after specifying a command and then
reading several bytes as an answer.
|
int |
blockProcessCall(int command,
I2CBlock blockWrite,
I2CBlock blockRead)
SMBUS RPC-like operation, writing several bytes after specifying a command and then
reading several bytes as an answer.
|
void |
close()
Closes this bus.
|
void |
doTransaction(I2CTransaction transaction)
Do an uninterrupted transaction of several messages.
|
I2CFunctionalities |
getFunctionalities()
Finds out what this I2C bus can do.
|
int |
processCall(int command,
int word)
SMBUS RPC-like operation, writing a short after specifying a command and then
reading the answer.
|
void |
quick(boolean isWrite)
SMBUS operation sending only the READ or WRITE bit, no data is carried.
|
void |
read(I2CBuffer buffer)
Directly reads bytes from the slave device (length is specified by the buffer).
|
void |
read(I2CBuffer buffer,
int length)
Directly reads length bytes from the slave device.
|
int |
readBlock(int command,
I2CBlock block)
SMBUS operation reading several bytes after specifying a command.
|
int |
readByte(int command)
SMBUS operation reading a byte after specifying a command.
|
int |
readByteDirectly()
SMBUS operation reading a byte without a command.
|
void |
readI2CBlock(int command,
I2CBlock block,
int length)
SMBUS-like operation reading several bytes after specifying a command where the length
is part of the message.
|
int |
readWord(int command)
SMBUS operation reading a short after specifying a command.
|
void |
selectSlave(int address)
Selects an available slave device using a regular 7-bit address.
|
void |
selectSlave(int address,
boolean force,
boolean isTenBit)
Selects an available slave device.
|
void |
setRetries(int nRetries)
Sets the number of time a slave should be polled when not acknowledging.
|
void |
setTimeout(int milliseconds)
Sets the timeout in milliseconds.
|
void |
usePEC(boolean usePEC)
Enables or disables packet error checking for SMBUS commands.
|
void |
write(I2CBuffer buffer)
Directly writes bytes to the slave device (length is specified by the buffer).
|
void |
write(I2CBuffer buffer,
int length)
Directly writes length bytes to the slave device.
|
void |
writeBlock(int command,
I2CBlock block)
SMBUS operation writing several bytes after specifying a command.
|
void |
writeByte(int command,
int b)
SMBUS operation writing a byte after specifying a command.
|
void |
writeByteDirectly(int b)
SMBUS operation writing a byte without a command.
|
void |
writeI2CBlock(int command,
I2CBlock block)
SMBUS-like operation writing several bytes after specifying a command where the length
is part of the message.
|
void |
writeWord(int command,
int word)
SMBUS operation writing a short after specifying a command.
|
public I2CBus(int busNumber)
throws LinuxException
busNumber - The number of the bus.LinuxException - When something fails on the native side.public I2CBus(String path) throws LinuxException
path - A path to the bus such as `/dev/i2c-1`.LinuxException - When something fails on the native side.public void close()
throws LinuxException
close in interface AutoCloseableLinuxException - When something fails on the native side.public void doTransaction(I2CTransaction transaction) throws LinuxException
Not all devices support this feature, or support only one message at a time which is not very interesting.
transaction - The transaction that will be carried out.LinuxException - When something fails on the native side.public I2CFunctionalities getFunctionalities() throws LinuxException
LinuxException - When something fails on the native side.public void selectSlave(int address)
throws LinuxException
address - The address of the needed slave.LinuxException - When something fails on the native side.selectSlave( int, boolean, boolean )public void selectSlave(int address,
boolean force,
boolean isTenBit)
throws LinuxException
This needs to be called only once if several IO operations are performed on the same
slave and has no effect on transactions.
address - The address of the needed slave.force - Should the slave be selected even if it is already used somewhere else ?isTenBit - Is the given address in 10-bit resolution ?LinuxException - When something fails on the native side.public void setRetries(int nRetries)
throws LinuxException
Does not always have an effect depending on the underlying driver.
nRetries - the number of retries.LinuxException - When something fails on the native side.public void setTimeout(int milliseconds)
throws LinuxException
Does not always have an effect depending on the underlying driver.
milliseconds - The timeout in milliseconds.LinuxException - When something fails on the native side.public void usePEC(boolean usePEC)
throws LinuxException
Is ignored unless the underlying driver provides this functionality.
Slave devices do not necessarely support this feature either.
usePEC - Should PEC be enabled ?LinuxException - When something fails on the native side.public void quick(boolean isWrite)
throws LinuxException
isWrite - True if the WRITE bit must be set.LinuxException - When something fails on the native side.public int readByteDirectly()
throws LinuxException
LinuxException - When something fails on the native side.public int readByte(int command)
throws LinuxException
command - The command.LinuxException - When something fails on the native side.public int readWord(int command)
throws LinuxException
command - The command.LinuxException - When something fails on the native side.public int readBlock(int command,
I2CBlock block)
throws LinuxException
command - The command.block - Block of bytes the answer will be written to.LinuxException - When something fails on the native side.public void readI2CBlock(int command,
I2CBlock block,
int length)
throws LinuxException
This operation is not in the SMBUS standard but is often supported nonetheless.
command - The command.block - Block of bytes the answer will be written to.length - How many bytes should be read.LinuxException - When something fails on the native side.public void read(I2CBuffer buffer) throws LinuxException
buffer - Buffer the answer will be written to.LinuxException - When something fails on the native side.read( I2CBuffer, int )public void read(I2CBuffer buffer, int length) throws LinuxException
buffer - Buffer the answer will be written to.length - The number of bytes requested.LinuxException - When something fails on the native side.public void writeByteDirectly(int b)
throws LinuxException
b - The unsigned byte that needs to be sent.LinuxException - When something fails on the native side.public void writeByte(int command,
int b)
throws LinuxException
command - The command.b - The unsigned byte that need to be sent.LinuxException - When something fails on the native side.public void writeWord(int command,
int word)
throws LinuxException
command - The command.word - The unsigned short that needs to be sent.LinuxException - When something fails on the native side.public void writeBlock(int command,
I2CBlock block)
throws LinuxException
After the command byte, the master also sends a byte count, how many bytes will be written.
command - The command.block - Block of bytes to write.LinuxException - When something fails on the native side.public void writeI2CBlock(int command,
I2CBlock block)
throws LinuxException
This operation is not in the SMBUS standard but is often supported nonetheless.
Unlike writeBlock, this operation
does not send a byte count after the command.
command - The command.block - Block of bytes to write.LinuxException - When something fails on the native side.public void write(I2CBuffer buffer) throws LinuxException
buffer - Buffer to write.LinuxException - When something fails on the native side.write( I2CBuffer, int )public void write(I2CBuffer buffer, int length) throws LinuxException
buffer - Buffer to write.length - The number of bytes.LinuxException - When something fails on the native side.public int processCall(int command,
int word)
throws LinuxException
command - The command.word - The unsigned short to be sent.LinuxException - When something fails on the native side.public int blockProcessCall(int command,
I2CBlock block)
throws LinuxException
command - The command.block - Block of bytes to write, also where the answer will be written to.LinuxException - When something fails on the native side.public int blockProcessCall(int command,
I2CBlock blockWrite,
I2CBlock blockRead)
throws LinuxException
command - The command.blockWrite - Block of bytes to write.blockRead - Block of bytes the answer will be written to (can be the same as blockWrite
is it can be overwrittenLinuxException - When something fails on the native side.