public interface SPIDevice extends Peripheral, Transactional
SPIDevice
interface provides methods for transmitting and receiving data to/from an SPI slave device.
Each SPI slave device is identified by a numerical ID and by a name. SPIDevice
instance can be opened by a call to one of the PeripheralManager.open()
methods.
On an SPI bus, data is transferred between the SPI master device and an SPI slave device in full duplex. That is,
data is transmitted by the SPI master to the SPI slave device at the same time data is received from the SPI slave
device by the SPI master.
To perform such a bidirectional exchange of data with an SPI slave device, an application may use one of the
writeAndRead
methods. write
or the read
method, respectively. When writing
only, the data received from the SPI slave device will be ignored/discarded. When reading only, dummy data will be
sent to the slave.
A data exchange consists of words of a certain length which may vary from SPI slave device to SPI slave device. InvalidWordLengthException
will be thrown. For example, if the word length is 16bits and the
designated portion of buffer is only 1-byte long or is 3-byte long an InvalidWordLengthException
will be
thrown. SPIDevice.close()
method to release
the SPI slave device. Any further attempt to transmit/write or receive/read to/from an SPI slave device which has
been closed will result in a PeripheralNotAvailableException
been thrown.BIG_ENDIAN, LITTLE_ENDIAN, MIXED_ENDIAN, UNDEFINED_ID
Modifier and Type | Method and Description |
---|---|
void |
begin()
Demarcates the beginning of an SPI transaction so that this slave's Select line (SS) will be remain asserted
during the subsequent read and write operations and until the transaction ends.
|
void |
end()
Demarcates the end of a transaction hence ending the assertion of this slave's Select line (SS).
|
int |
getWordLength()
Gets the transfer word length in bits supported by this slave device.
|
int |
read()
Reads one data word of up to 32 bits from this slave device.
|
int |
read(byte[] rxBuf,
int rxOff,
int rxLen)
Receives/reads up to
rxLen bytes of data from this slave device into an array of bytes. |
int |
read(int skip,
byte[] rxBuf,
int rxOff,
int rxLen)
Receives/reads up to
rxLen bytes of data from this slave device into an array of bytes skipping the first
skip bytes read. |
void |
write(byte[] txBuf,
int txOff,
int txLen)
Transmits/writes data to this slave device.
|
void |
write(int txData)
Writes one data word of up to 32 bits to this slave device.
|
int |
writeAndRead(byte[] txBuf,
int txOff,
int txLen,
byte[] rxBuf,
int rxOff,
int rxLen)
Exchanges (transmits and receives) data with this slave device.
|
int |
writeAndRead(byte[] txBuf,
int txOff,
int txLen,
int skip,
byte[] rxBuf,
int rxOff,
int rxLen)
Exchanges (transmits and receives) data with this slave device skipping the specified number of bytes received.
|
int |
writeAndRead(int txData)
Exchanges (transmits and receives) one data word of up to 32 bits with this slave device.
|
close, getID, getName, getProperties, isOpen
void begin() throws java.io.IOException, PeripheralNotAvailableException
begin
in interface Transactional
PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).java.io.IOException
- if an IO error occurred.InvalidStateException
- if a transaction is already in progress.void end() throws java.io.IOException, PeripheralNotAvailableException
end
in interface Transactional
java.io.IOException
- if an IO error occurred.InvalidStateException
- if a transaction is not currently in progress.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int getWordLength() throws java.io.IOException, PeripheralNotAvailableException
If the length of data to be exchanged belies a slave's word length an InvalidWordLengthException
will be
thrown.
java.io.IOException
- if an IO error occurred.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int read() throws java.io.IOException, PeripheralNotAvailableException
java.io.IOException
- if an I/O error occurs.InvalidWordLengthException
- if the number of bytes to receive belies word length; that is this slave's word length is bigger than
32 bits.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int read(byte[] rxBuf, int rxOff, int rxLen) throws java.io.IOException, PeripheralNotAvailableException
rxLen
bytes of data from this slave device into an array of bytes.
Dummy data will be sent to this slave device by the platform.
The length of the designated portion of the receiving byte buffers must correspond to a (positive) integral
number of words.rxBuf
- buffer for the received bytes.rxOff
- the offset in rxBuf
where to start copying the bytes received.rxLen
- the number of bytes to receive.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the number of bytes to receive belies word length.java.lang.NullPointerException
- If rxBuf
is null
.java.lang.IndexOutOfBoundsException
- rxOff
or rxLen
points or results in pointing outside rxBuf
.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int read(int skip, byte[] rxBuf, int rxOff, int rxLen) throws java.io.IOException, PeripheralNotAvailableException
rxLen
bytes of data from this slave device into an array of bytes skipping the first
skip
bytes read.skip
- the number of read bytes that must be ignored/skipped before filling in the rxBuf
buffer.rxBuf
- buffer for the received bytes.rxOff
- the offset in rxBuf
where to start copying the bytes received.rxLen
- the number of bytes to receive.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the total number of bytes to receive (skip + rxLen
) belies word length.java.lang.NullPointerException
- If rxBuf
is null
.java.lang.IndexOutOfBoundsException
- rxOff
or rxLen
points or results in pointing outside rxBuf
.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).void write(byte[] txBuf, int txOff, int txLen) throws java.io.IOException, PeripheralNotAvailableException
txBuf
- the buffer containing the bytes to send.txOff
- the offset in txBuf
of the first byte to send.txLen
- the number of bytes from txBuf
to send.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the number of bytes to send belies word length.java.lang.NullPointerException
- If txBuf
is null
.java.lang.IndexOutOfBoundsException
- txOff
or txLen
points or results in pointing outside txBuf
.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).void write(int txData) throws java.io.IOException, PeripheralNotAvailableException
txData
- the data word to be writtenjava.io.IOException
- if an I/O error occurredInvalidWordLengthException
- if the number of bytes to send belies word length; that is this slave's word length is bigger than 32
bits.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int writeAndRead(byte[] txBuf, int txOff, int txLen, byte[] rxBuf, int rxOff, int rxLen) throws java.io.IOException, PeripheralNotAvailableException
txBuf
- the buffer containing the bytes to send.txOff
- the offset in txBuf
of the first byte to send.txLen
- the number of bytes from txBuf
to send.rxBuf
- the buffer for the received bytes.rxOff
- the offset in rxBuf
where to start copying the bytes received.rxLen
- the number of bytes to receive.rxBuf
.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the numbers of bytes to send or to receive bely word length.java.lang.NullPointerException
- If txBuf
or rxBuf
is null
.java.lang.IndexOutOfBoundsException
- txOff
, txLen
, rxOff
or rxLen
points or results in pointing outside
txBuf
or rxBuf
.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int writeAndRead(byte[] txBuf, int txOff, int txLen, int skip, byte[] rxBuf, int rxOff, int rxLen) throws java.io.IOException, PeripheralNotAvailableException
txBuf
- the buffer containing the bytes to send.txOff
- the offset in txBuf
of the first byte to send.txLen
- the number of bytes from txBuf
to send.skip
- the number of received bytes that must be ignored/skipped before filling in the rxBuf
buffer.rxBuf
- buffer for the received bytes.rxOff
- the offset in rxBuf
where to start copying the bytes received.rxLen
- the number of bytes to receive.rxBuf
.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the numbers of bytes to send or to receive bely word length.java.lang.NullPointerException
- If txBuf
or rxBuf
is null
.java.lang.IndexOutOfBoundsException
- txOff
, txLen
, rxOff
or rxLen
points or results in pointing outside
txBuf
or rxBuf
.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).int writeAndRead(int txData) throws java.io.IOException, PeripheralNotAvailableException
txData
- the word to send.java.io.IOException
- if an IO error occurred.InvalidWordLengthException
- if the numbers of bytes to send or to receive bely word length; that is this slave's word length is
bigger than 32 bits.PeripheralNotAvailableException
- if the peripheral is not currently available (has been closed).Copyright (c) 2012, Oracle and/or its affiliates. All Rights Reserved. Use of this specification is subject to license terms.