Hi there,
I am trying to get some I2C code to work: integrating an CAP1188 capacitive sensor to my RPi. Getting the I2C device works, receiving IRQ's through Gpio works, reading the values through I2C works, but during initialization reading and writing some device registers results in an IOException. I tested the read and write commands with i2cget and i2cset without problems.
here is the stacktrace:
java.io.IOException: Error reading from /dev/i2c-1 at address 0x29 to address 0x1f. Got -20001.
at com.pi4j.io.i2c.impl.I2CDeviceImpl.read(I2CDeviceImpl.java:219)
at name.buurmeijer.mile.ssu.physicalui.CapacitiveSensorController.init(CapacitiveSensorController.java:113)
Here is the initialization method.
public void init() {
// reset CAP1188 chip first
// make reset LOW
this.resetPin.setState(PinState.LOW);
// wait for 100mS
MainController.sleepAWhile( 100);
// make reset HIGH
this.resetPin.setState(PinState.HIGH);
// wait for 100mS
MainController.sleepAWhile( 100);
// make reset LOW again
this.resetPin.setState(PinState.LOW);
// wait for 100mS
MainController.sleepAWhile( 100);
try {
// configure CAP1188 registers according this physical application
// read settings first
int sensitivity = this.cap1188i2cDevice.read( CAP1188_SENSITIVITY_REG);
int multiTouch = this.cap1188i2cDevice.read( CAP1188_MULTITOUCH_REG);
int ledLink = this.cap1188i2cDevice.read( CAP1188_LEDLINK_REG);
System.out.println("sensitivity =" + sensitivity +", multiTouch =" + multiTouch + ", ledLink =" + ledLink);
// set sensitivity to a very low level
this.cap1188i2cDevice.write( CAP1188_SENSITIVITY_REG, (byte) 0x3F);
// enable multi touch circuitry for 2 simultanous touches
this.cap1188i2cDevice.write( CAP1188_MULTITOUCH_REG, (byte) 0b1000_01_00);
// link leds to touches
this.cap1188i2cDevice.write( CAP1188_LEDLINK_REG, (byte) 0b11111111);
//
} catch (IOException ex) {
Logger.getLogger(CapacitiveSensorController.class.getName()).log(Level.SEVERE, null, ex);
}
}
The bold part was missing at first resulting in the shown IOException. This exception seems to be thrown when the device is just coming out of a reset and not ready to communicate through I2C yet. After adding the delay of 100mS it all worked. I did not tune the 100mS yet.
I hope someone finds this useful, because I could not find anything on this specific exception. After solving it I have pretty clean Java code for such nifty low level device integration. Thanks to Robert Savage for giving such an elegant API to the Raspberry PI java-world. OK I got to thank Oracle as well for supporting Java properly on the RPi : I run java 8 SE on the RPi and debug remotely from Netbeans and are still amazed by the level of productivity you can get out of this setup.
Cheers, Milé