jcontrol.comm
Class I2C

java.lang.Object
  extended by jcontrol.comm.I2C
All Implemented Interfaces:
BasicInputStream, BasicOutputStream

public class I2C
extends Object
implements BasicInputStream, BasicOutputStream

Controls I2C devices connected to JControl. The I2C bus is a de facto standard for on board inter integrated circuit communication invented by Phillips (NXP). Many chips are supporting this bus, including the JControl RTC on some Boards.

Currently JControl only works as I2C busmaster, so it can only send data and send requests for data actively. It can't poll the bus as slave and reply for requests of other masters. It is a simple hardware layer implementation so any bus error and any lost arbitration results in an IOException after some retries. To avoid blocking there is contrary to the specification a bus timeout also resulting in an IOException.

Another bus system based on I2C is the SM (system management) bus available on some PC motherboards, for monitoring tempeartures and controlling fans.


Field Summary
static int MODE_10BIT
          Selects I2C mode with 10-bit addresses.
static int MODE_7BIT
          Selects I2C mode with 7-bit addresses (default).
 
Constructor Summary
I2C(int address)
          Constructs a new I2C object for 7-bit addresseing.
I2C(int address, int mode)
          Constructs a new I2C object.
 
Method Summary
 int available()
          Note: not supported
 void close()
          Closes this I2C instance, further reads or writes are not possible.
 void flush()
          Note: not supported
 void mark(int readlimit)
          Note: not supported
 boolean markSupported()
          Note: not supported, always returning false
 int read()
          Just reads a single byte from the I2C device.
 int read(byte[] data)
          Reads a data stream from a I2C device.
 int read(byte[] cmd, byte[] data, int startindex, int length)
          Writes a command stream to a I2C device and reads a reply in the same transaction.
 int read(byte[] data, int startindex, int length)
          Reads a data stream from a I2C device.
 void reset()
          Note: not supported
 int skip(int n)
          Note: not supported
 void write(byte[] data)
          Writes a data stream to a I2C device.
 void write(byte[] cmd, byte[] data, int startindex, int length)
          Writes a command stream and a data stream to a I2C device in the same transaction.
 void write(byte[] data, int startindex, int length)
          Writes a data stream to a I2C device.
 void write(int data)
          Just writes a single byte to the I2C device.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notifyAll, toString, wait
 

Field Detail

MODE_7BIT

public static final int MODE_7BIT
Selects I2C mode with 7-bit addresses (default).

See Also:
Constant Field Values

MODE_10BIT

public static final int MODE_10BIT
Selects I2C mode with 10-bit addresses. (Note: not all I2C devices support the 10-bit addressing mode, refer the data sheet.)

See Also:
Constant Field Values
Constructor Detail

I2C

public I2C(int address)
Constructs a new I2C object for 7-bit addresseing.

Parameters:
address - 7-bit address of the the device to send data to

I2C

public I2C(int address,
           int mode)
Constructs a new I2C object.

Parameters:
address - of the the device to send data to
mode - bus mode (addressing etc)
See Also:
MODE_7BIT, MODE_10BIT
Method Detail

write

public void write(byte[] data,
                  int startindex,
                  int length)
           throws IOException
Writes a data stream to a I2C device. This method blocks until the transaction is complete or aborted with an error.

Specified by:
write in interface BasicOutputStream
Parameters:
data - the buffer that holds the data to write
startindex - the index to start reading the array (and writing to I2C)
length - number of bytes to write; the array is read from startindex to startindex+length-1
Throws:
IOException - if an error occurs

read

public int read(byte[] data,
                int startindex,
                int length)
         throws IOException
Reads a data stream from a I2C device. This method blocks until the transaction is complete or aborted with an error.

Specified by:
read in interface BasicInputStream
Parameters:
data - the buffer to fill
startindex - the index to start filling the array (with data read from I2C)
length - number of bytes to read; the array is filled from startindex to startindex+length-1, it may be partially filled according to the I2C slave device
Returns:
the number of received bytes
Throws:
IOException - if an error occurs
See Also:
InputStream.read()

write

public void write(byte[] data)
           throws IOException
Writes a data stream to a I2C device. This method blocks until the transaction is complete or aborted with an error.

Specified by:
write in interface BasicOutputStream
Parameters:
data - the buffer that holds the data to write
Throws:
IOException - if an error occurs
See Also:
OutputStream.write(byte[], int, int)

read

public int read(byte[] data)
         throws IOException
Reads a data stream from a I2C device. This method blocks until the transaction is complete or aborted with an error.

Specified by:
read in interface BasicInputStream
Parameters:
data - the buffer to fill
Returns:
the number of received bytes
Throws:
IOException - if an error occurs
See Also:
InputStream.read(byte[], int, int)

write

public void write(byte[] cmd,
                  byte[] data,
                  int startindex,
                  int length)
           throws IOException
Writes a command stream and a data stream to a I2C device in the same transaction. This is an often used sequence for I2C devices that support multiple registers, so a command byte (=register address) is sent and then the register is written. This method is recommended for multi master systems. This method blocks until the transaction is complete or aborted with an error.

Parameters:
cmd - the buffer with command to write (all bytes of array, null for command omission)
data - the buffer to read data to transmit
startindex - the index to start reading the array (and writing to I2C)
length - number of bytes to read; the array is accessed from startindex to startindex+length-1
Throws:
IOException - if an error occurs

read

public int read(byte[] cmd,
                byte[] data,
                int startindex,
                int length)
         throws IOException
Writes a command stream to a I2C device and reads a reply in the same transaction. This is an often used sequence for I2C devices that support multiple registers, so a command byte (=register address) is sent and then the register is read. This method is recommended for multi master systems. This method blocks until the transaction is complete or aborted with an error.

Parameters:
cmd - the buffer with command to write (all bytes of array, null for command omission)
data - the buffer to write received data
startindex - the index to start writing the array (and reading from to I2C)
length - number of bytes to write; the array is accessed from startindex to startindex+length-1, it may be partially filled according to the I2C slave device
Returns:
the number of received bytes
Throws:
IOException - if an error occurs

close

public void close()
Closes this I2C instance, further reads or writes are not possible.

Specified by:
close in interface BasicInputStream
Specified by:
close in interface BasicOutputStream
See Also:
BasicOutputStream.close()

write

public void write(int data)
           throws IOException
Just writes a single byte to the I2C device.

Specified by:
write in interface BasicOutputStream
Parameters:
data - I2C Data to transmit (range 0...255)
Throws:
IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
See Also:
BasicOutputStream#write(char)

read

public int read()
         throws IOException
Just reads a single byte from the I2C device.

Specified by:
read in interface BasicInputStream
Returns:
received I2C Data (range 0...255)
Throws:
IOException - if an I/O error occurs.
See Also:
BasicInputStream.read()

skip

public int skip(int n)
         throws IOException
Note: not supported

Specified by:
skip in interface BasicInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

available

public int available()
              throws IOException
Note: not supported

Specified by:
available in interface BasicInputStream
Returns:
the number of bytes that can be read from this input stream without blocking.
Throws:
IOException - if an I/O error occurs.

mark

public void mark(int readlimit)
Note: not supported

Specified by:
mark in interface BasicInputStream
Parameters:
readlimit - the maximum limit of bytes that can be read before the mark position becomes invalid.

reset

public void reset()
           throws IOException
Note: not supported

Specified by:
reset in interface BasicInputStream
Throws:
IOException - if this stream has not been makred or if the mark has been invalidated.

markSupported

public boolean markSupported()
Note: not supported, always returning false

Specified by:
markSupported in interface BasicInputStream
Returns:
true if this stream instance supports the mark and reset methods; false otherwise.

flush

public void flush()
           throws IOException
Note: not supported

Specified by:
flush in interface BasicOutputStream
Throws:
IOException - if an I/O error occurs.