See: Description
Interface | Description |
---|---|
SPICompositeMessage |
The
SPICompositeMessage interface provides methods for constructing a
composite message. |
SPIDevice |
The
SPIDevice interface provides methods for transmitting and receiving data to/from an SPI slave device. |
Class | Description |
---|---|
SPIDeviceConfig |
The
SPIDeviceConfig class encapsulates the hardware addressing information, and static and dynamic
configuration parameters of an SPI slave device. |
SPIDeviceConfig.Builder |
The
Builder class allows for creating and initializing
SPIDeviceConfig objects. |
SPIPermission |
The
SPIPermission class defines permissions for SPI slave device access. |
Exception | Description |
---|---|
InvalidWordLengthException |
Thrown by an instance of
SPIDevice in case of mismatch between the length of data to be exchanged and the
slave's word length as indicated by SPIDevice.getWordLength . |
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 numeric ID, name, type (interface) and/or properties:
- Using its ID
SPIDevice slave = (SPIDevice) DeviceManager.open(3);- Using its name and interface
SPIDevice slave = DeviceManager.open("RTC1", SPIDevice.class, null);
Once the device 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
Device.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 = DeviceManager.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]); rcvBuf.limit(1); slave.writeAndRead(sndBuf1, rcvBuf); //received data will be stored in rcvBuf[0] rcvBuf.limit(3); slave.writeAndRead(sndBuf2, rcvBuf); //received data will be stored in rcvBuf[1] and rcvBuf[2] } catch (IOException ioe) { // handle exception }
The preceding example is using a try-with-resources statement; 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, permission and security checks that may cause
a SecurityException
to be thrown must be performed
in priority to any other checks or operations once performed the checking of the input parameters
from which the permission target names and action lists are retrieved and assembled.
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.
Copyright © 2012, 2015, Oracle and/or its affiliates. All rights reserved.
Legal Notices