public class SPI
extends java.lang.Object
Each object of this class is created for a determined SPI interface and SPI slave device.
To obtain an instance of the SPI interface you have to use the
SPIManager.createSPI(int, int)
method.
Modifier and Type | Field | Description |
---|---|---|
static int |
CPHA_CONFIG |
Clock line phase:
|
static int |
CPOL_CONFIG |
Clock line polarity:
|
static int |
CS_HIGH_CONFIG |
Chip select line active high:
|
static int |
CS_LOW_CONFIG |
Chip select line active low:
|
static int |
LOOP_CONFIG |
Loopback mode:
|
static int |
LSB_FIRST_CONFIG |
Message bit order (LSB first):
|
static int |
MODE_0 |
MODE_0 (0x00) -> Clock polarity = 0 / Clock phase = 0
|
static int |
MODE_1 |
MODE_1 (0x01) -> Clock polarity = 0 / Clock phase = 1
|
static int |
MODE_2 |
MODE_2 (0x02) -> Clock polarity = 1 / Clock phase = 0
|
static int |
MODE_3 |
MODE_3 (0x03) -> Clock polarity = 1 / Clock phase = 1
|
static int |
MSB_FIRST_CONFIG |
Message bit order (MSB first):
|
static int |
NO_CS_CONFIG |
No chip select mode:
|
static int |
READY_CONFIG |
Ready mode (slave pulls low to pause):
|
static int |
THREE_WIRE_CONFIG |
3 wire mode:
|
Modifier and Type | Method | Description |
---|---|---|
void |
close() |
Attempts to close the SPI interface.
|
protected void |
finalize() |
|
SPIConfig |
getConfig() |
Gets the SPI configuration.
|
int |
getInterface() |
Gets the SPI interface number.
|
int |
getSlaveDevice() |
Gets the SPI slave device number.
|
boolean |
isInterfaceOpen() |
Gets the status of the SPI interface.
|
void |
open(SPIConfig config) |
Opens the SPI interface with the given configuration.
|
byte[] |
read(int numBytes) |
Reads the specified number of bytes from the SPI slave device.
|
void |
setConfig(SPIConfig config) |
Changes the configuration of the SPI.
|
byte[] |
transfer(byte[] txData) |
Simultaneous write (of the given bytes) and read (of the same number of
bytes) using the default clock frequency and word length parameters.
|
byte[] |
transfer(byte[] txData,
int clockFrequency,
int wordLength) |
Simultaneous write (of the given bytes) and read (of the same number of
bytes) using the given clock frequency and word length parameters (these
parameters are only used for this transfer, but their default values
remain the same).
|
void |
write(byte[] txData) |
Writes the given bytes in the SPI slave device.
|
public static final int CPHA_CONFIG
public static final int CPOL_CONFIG
public static final int CS_LOW_CONFIG
public static final int CS_HIGH_CONFIG
public static final int MSB_FIRST_CONFIG
public static final int LSB_FIRST_CONFIG
public static final int THREE_WIRE_CONFIG
public static final int LOOP_CONFIG
public static final int NO_CS_CONFIG
public static final int READY_CONFIG
public static final int MODE_0
public static final int MODE_1
public static final int MODE_2
public static final int MODE_3
public void open(SPIConfig config) throws NoSuchInterfaceException, java.io.IOException
config
- SPI configuration.java.io.IOException
- If there is an error opening the SPI interface.NoSuchInterfaceException
- If the configured SPI interface
does not exist.java.lang.NullPointerException
- If config == null
.close()
,
isInterfaceOpen()
,
SPIConfig
public void close() throws java.io.IOException
java.io.IOException
- If there is an error closing the SPI interface.isInterfaceOpen()
,
open(SPIConfig)
public boolean isInterfaceOpen()
true
if interface is open, false
if not.close()
,
open(SPIConfig)
public void setConfig(SPIConfig config) throws java.io.IOException
config
- New SPI configuration.java.io.IOException
- If the interface is closed or there is an error
changing the configuration.java.lang.NullPointerException
- If config == null
.getConfig()
,
isInterfaceOpen()
,
open(SPIConfig)
,
SPIConfig
public SPIConfig getConfig()
setConfig(SPIConfig)
,
SPIConfig
public int getInterface()
public int getSlaveDevice()
public byte[] read(int numBytes) throws java.io.IOException
numBytes
- Amount of bytes to read.null
if error.java.lang.IllegalArgumentException
- If numBytes < 0
.java.io.IOException
- If the interface is closed or there is an error
reading from the SPI slave device.isInterfaceOpen()
,
open(SPIConfig)
public void write(byte[] txData) throws java.io.IOException
txData
- Bytes to write.java.io.IOException
- If the interface is closed or there is an error
writing in the SPI slave device.java.lang.NullPointerException
- If txData == null
.isInterfaceOpen()
,
open(SPIConfig)
public byte[] transfer(byte[] txData) throws java.io.IOException
txData
- Bytes to write.null
if error.java.io.IOException
- If the interface is closed or there is an error
transferring data.java.lang.NullPointerException
- If txData == null
.getConfig()
,
isInterfaceOpen()
,
transfer(byte[], int, int)
public byte[] transfer(byte[] txData, int clockFrequency, int wordLength) throws java.io.IOException
txData
- Bytes to write.clockFrequency
- The clock frequency in Hz.wordLength
- Number of bits per word.null
if error.java.lang.IllegalArgumentException
- If clockFrequency < 1
o
if wordLength < 1
.java.io.IOException
- If the interface is closed or there is an error
transferring data.java.lang.NullPointerException
- If txData == null
.getConfig()
,
isInterfaceOpen()
,
transfer(byte[])
,
SPIConfig.getClockFrequency()
,
SPIConfig.getWordLength()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable