Device Access API
Proposal for Java ME 8

Package com.oracle.deviceaccess.spibus

Interfaces and classes for SPI (Serial Peripheral Interface Bus) device access.

See: Description

Package com.oracle.deviceaccess.spibus Description

Interfaces and classes for SPI (Serial Peripheral Interface Bus) device access.

The functionalities supported by this API are those of an SPI master.

In order to communicate with a specific slave, an application should first open and obtain an SPIDevice instance for the SPI slave device the application wants to exchange data with, using its numerical ID, name, type (interface) and/or properties:

Using its ID
 SPIDevice slave = (SPIDevice) PeripheralManager.open(3);
 
Using its name and interface
 SPIDevice slave = PeripheralManager.open("RTC1", SPIDevice.class, null);
 
Once the peripheral opened, the application can exchange data with the SPI slave device using methods of the SPIDevice interface such as the writeAndRead() method.
 slave.writeAndRead(sndBuf, 0, 1, rcvBuf, 0, 1);
 
When the data exchange is over, the application should call the Peripheral.close() method to close the SPI slave device.
 slave.close();
 
The following sample code gives an example of using the SPI API to communicate with SPI slaves:
 try (SPIDevice slave = PeripheralManager.open("SPI1", SPIDevice.class, null)) {
    ByteBuffer sndBuf1 = ByteBuffer.wrap(new byte[] {0x01});
    ByteBuffer sndBuf2 = ByteBuffer.wrap(new byte[] {0x02});
    ByteBuffer rcvBuf = ByteBuffer.wrap(new byte[3]);
    slave.writeAndRead(sndBuf1, rcvBuf); //received data will be stored in rcvBuf[0]
    slave.writeAndRead(sndBuf2, rcvBuf); //received data will be stored in rcvBuf[1] and rcvBuf[2]
 } catch (IOException ioe) {
     // handle exception
 }
 
Note that the preceding example is using a try-with-resources statement and that the SPIDevice.close() method is automatically invoked by the platform at the end of the statement.

Information about the SPI-bus specification can be found at http://www.freescale.com/files/microcontrollers/doc/ref_manual/M68HC11RM.pdf.

Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

Device Access API
Proposal for Java ME 8