public class SPI extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
SPI.Port |
Modifier and Type | Method and Description |
---|---|
void |
forceAutoRead()
Force the engine to make a single transfer.
|
void |
free()
Free the resources used by this object.
|
void |
freeAccumulator()
Frees the accumulator.
|
void |
freeAuto()
Frees the automatic SPI transfer engine.
|
double |
getAccumulatorAverage()
Read the average of the accumulated value.
|
int |
getAccumulatorCount()
Read the number of accumulated values.
|
int |
getAccumulatorLastValue()
Read the last value read by the accumulator engine.
|
void |
getAccumulatorOutput(AccumulatorResult result)
Read the accumulated value and the number of accumulated values atomically.
|
long |
getAccumulatorValue()
Read the accumulated value.
|
int |
getAutoDroppedCount()
Get the number of bytes dropped by the automatic SPI transfer engine due
to the receive buffer being full.
|
void |
initAccumulator(double period,
int cmd,
int xferSize,
int validMask,
int validValue,
int dataShift,
int dataSize,
boolean isSigned,
boolean bigEndian)
Initialize the accumulator.
|
void |
initAuto(int bufferSize)
Initialize automatic SPI transfer engine.
|
int |
read(boolean initiate,
byte[] dataReceived,
int size)
Read a word from the receive FIFO.
|
int |
read(boolean initiate,
java.nio.ByteBuffer dataReceived,
int size)
Read a word from the receive FIFO.
|
int |
readAutoReceivedData(byte[] buffer,
int numToRead,
double timeout)
Read data that has been transferred by the automatic SPI transfer engine.
|
int |
readAutoReceivedData(java.nio.ByteBuffer buffer,
int numToRead,
double timeout)
Read data that has been transferred by the automatic SPI transfer engine.
|
void |
resetAccumulator()
Resets the accumulator to zero.
|
void |
setAccumulatorCenter(int center)
Set the center value of the accumulator.
|
void |
setAccumulatorDeadband(int deadband)
Set the accumulator's deadband.
|
void |
setAutoTransmitData(byte[] dataToSend,
int zeroSize)
Set the data to be transmitted by the engine.
|
void |
setChipSelectActiveHigh()
Configure the chip select line to be active high.
|
void |
setChipSelectActiveLow()
Configure the chip select line to be active low.
|
void |
setClockActiveHigh()
Configure the clock output line to be active high.
|
void |
setClockActiveLow()
Configure the clock output line to be active low.
|
void |
setClockRate(int hz)
Configure the rate of the generated clock signal.
|
void |
setLSBFirst()
Configure the order that bits are sent and received on the wire to be least significant bit
first.
|
void |
setMSBFirst()
Configure the order that bits are sent and received on the wire to be most significant bit
first.
|
void |
setSampleDataOnFalling()
Configure that the data is stable on the falling edge and the data changes on the rising edge.
|
void |
setSampleDataOnRising()
Configure that the data is stable on the rising edge and the data changes on the falling edge.
|
void |
startAutoRate(double period)
Start running the automatic SPI transfer engine at a periodic rate.
|
void |
startAutoTrigger(DigitalSource source,
boolean rising,
boolean falling)
Start running the automatic SPI transfer engine when a trigger occurs.
|
void |
stopAuto()
Stop running the automatic SPI transfer engine.
|
int |
transaction(byte[] dataToSend,
byte[] dataReceived,
int size)
Perform a simultaneous read/write transaction with the device.
|
int |
transaction(java.nio.ByteBuffer dataToSend,
java.nio.ByteBuffer dataReceived,
int size)
Perform a simultaneous read/write transaction with the device.
|
int |
write(byte[] dataToSend,
int size)
Write data to the slave device.
|
int |
write(java.nio.ByteBuffer dataToSend,
int size)
Write data to the slave device.
|
public void free()
public final void setClockRate(int hz)
hz
- The clock rate in Hertz.public final void setMSBFirst()
public final void setLSBFirst()
public final void setClockActiveLow()
public final void setClockActiveHigh()
public final void setSampleDataOnFalling()
public final void setSampleDataOnRising()
public final void setChipSelectActiveHigh()
public final void setChipSelectActiveLow()
public int write(byte[] dataToSend, int size)
If not running in output only mode, also saves the data received on the MISO input during the transfer into the receive FIFO.
public int write(java.nio.ByteBuffer dataToSend, int size)
If not running in output only mode, also saves the data received on the MISO input during the transfer into the receive FIFO.
dataToSend
- The buffer containing the data to send.public int read(boolean initiate, byte[] dataReceived, int size)
Waits for the current transfer to complete if the receive FIFO is empty.
If the receive FIFO is empty, there is no active transfer, and initiate is false, errors.
initiate
- If true, this function pushes "0" into the transmit buffer and initiates a
transfer. If false, this function assumes that data is already in the receive
FIFO from a previous write.public int read(boolean initiate, java.nio.ByteBuffer dataReceived, int size)
Waits for the current transfer to complete if the receive FIFO is empty.
If the receive FIFO is empty, there is no active transfer, and initiate is false, errors.
initiate
- If true, this function pushes "0" into the transmit buffer and initiates
a transfer. If false, this function assumes that data is already in the
receive FIFO from a previous write.dataReceived
- The buffer to be filled with the received data.size
- The length of the transaction, in bytespublic int transaction(byte[] dataToSend, byte[] dataReceived, int size)
dataToSend
- The data to be written out to the devicedataReceived
- Buffer to receive data from the devicesize
- The length of the transaction, in bytespublic int transaction(java.nio.ByteBuffer dataToSend, java.nio.ByteBuffer dataReceived, int size)
dataToSend
- The data to be written out to the device.dataReceived
- Buffer to receive data from the device.size
- The length of the transaction, in bytespublic void initAuto(int bufferSize)
Only a single engine is available, and use of it blocks use of all other chip select usage on the same physical SPI port while it is running.
bufferSize
- buffer size in bytespublic void freeAuto()
public void setAutoTransmitData(byte[] dataToSend, int zeroSize)
Up to 16 bytes are configurable, and may be followed by up to 127 zero bytes.
dataToSend
- data to send (maximum 16 bytes)zeroSize
- number of zeros to send after the datapublic void startAutoRate(double period)
initAuto(int)
and setAutoTransmitData(byte[], int)
must
be called before calling this function.
period
- period between transfers, in seconds (us resolution)public void startAutoTrigger(DigitalSource source, boolean rising, boolean falling)
initAuto(int)
and setAutoTransmitData(byte[], int)
must
be called before calling this function.
source
- digital source for the trigger (may be an analog trigger)rising
- trigger on the rising edgefalling
- trigger on the falling edgepublic void stopAuto()
public void forceAutoRead()
public int readAutoReceivedData(java.nio.ByteBuffer buffer, int numToRead, double timeout)
Transfers may be made a byte at a time, so it's necessary for the caller to handle cases where an entire transfer has not been completed.
Blocks until numToRead bytes have been read or timeout expires. May be called with numToRead=0 to retrieve how many bytes are available.
buffer
- buffer where read bytes are storednumToRead
- number of bytes to readtimeout
- timeout in seconds (ms resolution)public int readAutoReceivedData(byte[] buffer, int numToRead, double timeout)
Transfers may be made a byte at a time, so it's necessary for the caller to handle cases where an entire transfer has not been completed.
Blocks until numToRead bytes have been read or timeout expires. May be called with numToRead=0 to retrieve how many bytes are available.
buffer
- array where read bytes are storednumToRead
- number of bytes to readtimeout
- timeout in seconds (ms resolution)public int getAutoDroppedCount()
public void initAccumulator(double period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, boolean isSigned, boolean bigEndian)
period
- Time between readscmd
- SPI command to send to request dataxferSize
- SPI transfer size, in bytesvalidMask
- Mask to apply to received data for validity checkingvalidValue
- After validMask is applied, required matching value for validity checkingdataShift
- Bit shift to apply to received data to get actual data valuedataSize
- Size (in bits) of data fieldisSigned
- Is data field signed?bigEndian
- Is device big endian?public void freeAccumulator()
public void resetAccumulator()
public void setAccumulatorCenter(int center)
The center value is subtracted from each value before it is added to the accumulator. This is used for the center value of devices like gyros and accelerometers to make integration work and to take the device offset into account when integrating.
public void setAccumulatorDeadband(int deadband)
public int getAccumulatorLastValue()
public long getAccumulatorValue()
public int getAccumulatorCount()
Read the count of the accumulated values since the accumulator was last Reset().
public double getAccumulatorAverage()
public void getAccumulatorOutput(AccumulatorResult result)
This function reads the value and count atomically. This can be used for averaging.
result
- AccumulatorResult object to store the results in.