SpiDevice
public
class
SpiDevice
extends Object
implements
Closeable
| java.lang.Object | |
| ↳ | com.google.android.things.pio.SpiDevice |
Class used to control a SpiDevice.
Opening a SPI device takes ownership of it for the whole system, preventing anyone else from
opening/accessing the device until you call close(). Forgetting to call close()
will prevent anyone (including the same process/app) from using the device.
Summary
Constants | |
|---|---|
int |
MODE0
|
int |
MODE1
|
int |
MODE2
|
int |
MODE3
|
Public methods | |
|---|---|
void
|
close()
Close and release the SPI device. |
void
|
read(byte[] buffer, int length)
Read a byte array from the device. |
void
|
setBitJustification(boolean lsbFirst)
Set the bit justification. |
void
|
setBitsPerWord(int bitsPerWord)
Set the number of bits per word. |
void
|
setCsChange(boolean change)
Set the chip select behavior. |
void
|
setDelay(int delayUs)
Set the delay between transfers. |
void
|
setFrequency(int frequencyHz)
Set the frequency of the bus. |
void
|
setMode(int mode)
Set the SPI mode. |
void
|
transfer(byte[] txBuffer, byte[] rxBuffer, int length)
Transfer data to and from the device. |
void
|
write(byte[] buffer, int length)
Write a byte array to the device. |
Inherited methods | |
|---|---|
java.lang.Object
| |
java.io.Closeable
| |
java.lang.AutoCloseable
| |
Constants
MODE0
int MODE0
Mode constant used to configure a SPI device in mode 0: Clock Polarity
(CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 0.
Constant Value: 0 (0x00000000)
MODE1
int MODE1
Mode constant used to configure a SPI device in mode 1: Clock Polarity
(CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 1.
Constant Value: 1 (0x00000001)
MODE2
int MODE2
Mode constant used to configure a SPI device in mode 2: Clock Polarity
(CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 0.
Constant Value: 2 (0x00000002)
MODE3
int MODE3
Mode constant used to configure a SPI device in mode 3: Clock Polarity
(CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 1.
Constant Value: 3 (0x00000003)
Public methods
close
</div>
void close ()
Close and release the SPI device.
Must be called in order for the SPI device to be released and be made available to other users.</p>
| Throws | |
|---|---|
IOException |
if the SpiDevice is already closed. |
</div>
read
</div>
void read (byte[] buffer,
int length)
Read a byte array from the device.
| Parameters | |
|---|---|
buffer |
byte:
Buffer to read into. |
length |
int:
Number of bytes to read, may not be larger than the buffer size. |
| Throws | |
|---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
|
See also:
write(byte[], int)transfer(byte[], byte[], int)
</div>
setBitJustification
</div>
void setBitJustification (boolean lsbFirst)
Set the bit justification.
The bit justification is the ordering of bits in a byte (also called bit endianness).</p>
| Parameters | |
|---|---|
lsbFirst |
boolean:
If true, use least significant bit first else use most significant bit first. |
| Throws | |
|---|---|
IOException |
|
</div>
setBitsPerWord
</div>
void setBitsPerWord (int bitsPerWord)
Set the number of bits per word.
| Parameters | |
|---|---|
bitsPerWord |
int:
Number of bits per word. |
| Throws | |
|---|---|
IOException |
|
</div>
setCsChange
</div>
void setCsChange (boolean change)
Set the chip select behavior.
Affects chipselect after this transfer completes.</p>
| Parameters | |
|---|---|
change |
boolean:
If true, the chip may stay selected until the next transfer. |
| Throws | |
|---|---|
IOException |
|
</div>
setDelay
</div>
void setDelay (int delayUs)
Set the delay between transfers.
The delay in microseconds after a transfer before (optionally) changing the chip select status and completing.</p>
| Parameters | |
|---|---|
delayUs |
int:
Delay in microseonds. |
| Throws | |
|---|---|
IOException |
|
</div>
setFrequency
</div>
void setFrequency (int frequencyHz)
Set the frequency of the bus.
The frequencies supported is board dependent and can usually be found in the board documentation.</p>
| Parameters | |
|---|---|
frequencyHz |
int:
Frequency to set in Hertz. Must be positive. |
| Throws | |
|---|---|
|
IOException |
IllegalArgumentException |
|
IOException |
|
</div>
setMode
</div>
void setMode (int mode)
Set the SPI mode.
| Parameters | |
|---|---|
mode |
int:
Mode to set. One of MODE0, MODE1, MODE2,
MODE3. |
| Throws | |
|---|---|
IOException |
|
</div>
transfer
</div>
void transfer (byte[] txBuffer,
byte[] rxBuffer,
int length)
Transfer data to and from the device.
| Parameters | |
|---|---|
txBuffer |
byte:
Data to send to the device, or null to receive only. |
rxBuffer |
byte:
Data read from the device, or null to send only. |
length |
int:
Number of bytes to transfer, may not be larger than the size of txBuffer or
rxBuffer if they are non-null. |
| Throws | |
|---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
|
See also:
read(byte[], int)write(byte[], int)
</div>
write
</div>
void write (byte[] buffer,
int length)
Write a byte array to the device.
| Parameters | |
|---|---|
buffer |
byte:
Buffer to write. |
length |
int:
Number of bytes to write, may not be larger than the buffer size. |
| Throws | |
|---|---|
|
IOException |
|
IllegalArgumentException |
IOException |
|
See also:
read(byte[], int)transfer(byte[], byte[], int)
</div>