edu.wpi.first.wpilibj

## Class SPI

- java.lang.Object
- - edu.wpi.first.wpilibj.SensorBase
  - - edu.wpi.first.wpilibj.SPI

-     public class SPI
      extends SensorBase

  Represents a SPI bus port.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `SPI.Port`

  - ### Field Summary

    - ### Fields inherited from class edu.wpi.first.wpilibj.SensorBase

      `kAnalogInputChannels, kAnalogOutputChannels, kDigitalChannels, kPCMModules, kPDPChannels, kPDPModules, kPwmChannels, kRelayChannels, kSolenoidChannels, kSystemClockTicksPerMicrosecond`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SPI(SPI.Port port)` Constructor.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `free()` Free the resources used by this object.
    `void` | `freeAccumulator()` Frees the accumulator.
    `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.
    `void` | `initAccumulator(double period, int cmd, int xferSize, int validMask, int validValue, int dataShift, int dataSize, boolean isSigned, boolean bigEndian)` Initialize the accumulator.
    `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.
    `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` | `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.
    `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.

    - ### Methods inherited from class edu.wpi.first.wpilibj.SensorBase

      `checkAnalogInputChannel, checkAnalogOutputChannel, checkDigitalChannel, checkPDPChannel, checkPDPModule, checkPWMChannel, checkRelayChannel, checkSolenoidChannel, checkSolenoidModule, getDefaultSolenoidModule, setDefaultSolenoidModule`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SPI

          public SPI(SPI.Port port)

      Constructor.
      Parameters:
        `port` - the physical SPI port

  - ### Method Detail

    - #### free

          public void free()

      Free the resources used by this object.
      Overrides:
        `free` in class `SensorBase`

    - #### setClockRate

          public final void setClockRate(int hz)

      Configure the rate of the generated clock signal. The default value is 500,000 Hz. The maximum value is 4,000,000 Hz.
      Parameters:
        `hz` - The clock rate in Hertz.

    - #### setMSBFirst

          public final void setMSBFirst()

      Configure the order that bits are sent and received on the wire to be most significant bit first.

    - #### setLSBFirst

          public final void setLSBFirst()

      Configure the order that bits are sent and received on the wire to be least significant bit first.

    - #### setClockActiveLow

          public final void setClockActiveLow()

      Configure the clock output line to be active low. This is sometimes called clock polarity high or clock idle high.

    - #### setClockActiveHigh

          public final void setClockActiveHigh()

      Configure the clock output line to be active high. This is sometimes called clock polarity low or clock idle low.

    - #### setSampleDataOnFalling

          public final void setSampleDataOnFalling()

      Configure that the data is stable on the falling edge and the data changes on the rising edge.

    - #### setSampleDataOnRising

          public final void setSampleDataOnRising()

      Configure that the data is stable on the rising edge and the data changes on the falling edge.

    - #### setChipSelectActiveHigh

          public final void setChipSelectActiveHigh()

      Configure the chip select line to be active high.

    - #### setChipSelectActiveLow

          public final void setChipSelectActiveLow()

      Configure the chip select line to be active low.

    - #### write

          public int write(byte[] dataToSend,
                           int size)

      Write data to the slave device. Blocks until there is space in the output FIFO.

      If not running in output only mode, also saves the data received on the MISO input during the transfer into the receive FIFO.

    - #### write

          public int write(java.nio.ByteBuffer dataToSend,
                           int size)

      Write data to the slave device. Blocks until there is space in the output FIFO.

      If not running in output only mode, also saves the data received on the MISO input during the transfer into the receive FIFO.

      Parameters:
        `dataToSend` - The buffer containing the data to send. Must be created using ByteBuffer.allocateDirect().

    - #### read

          public int read(boolean initiate,
                          byte[] dataReceived,
                          int size)

      Read a word from the receive FIFO.

      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.

      Parameters:
        `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.

    - #### read

          public int read(boolean initiate,
                          java.nio.ByteBuffer dataReceived,
                          int size)

      Read a word from the receive FIFO.

      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.

      Parameters:
        `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. Must be created using ByteBuffer.allocateDirect().
        `size` - The length of the transaction, in bytes

    - #### transaction

          public int transaction(byte[] dataToSend,
                                 byte[] dataReceived,
                                 int size)

      Perform a simultaneous read/write transaction with the device.
      Parameters:
        `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 bytes

    - #### transaction

          public int transaction(java.nio.ByteBuffer dataToSend,
                                 java.nio.ByteBuffer dataReceived,
                                 int size)

      Perform a simultaneous read/write transaction with the device.
      Parameters:
        `dataToSend` - The data to be written out to the device. Must be created using ByteBuffer.allocateDirect().
        `dataReceived` - Buffer to receive data from the device. Must be created using ByteBuffer.allocateDirect().
        `size` - The length of the transaction, in bytes

    - #### initAccumulator

          public void initAccumulator(double period,
                                      int cmd,
                                      int xferSize,
                                      int validMask,
                                      int validValue,
                                      int dataShift,
                                      int dataSize,
                                      boolean isSigned,
                                      boolean bigEndian)

      Initialize the accumulator.
      Parameters:
        `period` - Time between reads
        `cmd` - SPI command to send to request data
        `xferSize` - SPI transfer size, in bytes
        `validMask` - Mask to apply to received data for validity checking
        `validValue` - After validMask is applied, required matching value for validity checking
        `dataShift` - Bit shift to apply to received data to get actual data value
        `dataSize` - Size (in bits) of data field
        `isSigned` - Is data field signed?
        `bigEndian` - Is device big endian?

    - #### freeAccumulator

          public void freeAccumulator()

      Frees the accumulator.

    - #### resetAccumulator

          public void resetAccumulator()

      Resets the accumulator to zero.

    - #### setAccumulatorCenter

          public void setAccumulatorCenter(int center)

      Set the center value of the accumulator.

      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.

    - #### setAccumulatorDeadband

          public void setAccumulatorDeadband(int deadband)

      Set the accumulator's deadband.

    - #### getAccumulatorLastValue

          public int getAccumulatorLastValue()

      Read the last value read by the accumulator engine.

    - #### getAccumulatorValue

          public long getAccumulatorValue()

      Read the accumulated value.
      Returns:
        The 64-bit value accumulated since the last Reset().

    - #### getAccumulatorCount

          public int getAccumulatorCount()

      Read the number of accumulated values.

      Read the count of the accumulated values since the accumulator was last Reset().

      Returns:
        The number of times samples from the channel were accumulated.

    - #### getAccumulatorAverage

          public double getAccumulatorAverage()

      Read the average of the accumulated value.
      Returns:
        The accumulated average value (value / count).

    - #### getAccumulatorOutput

          public void getAccumulatorOutput(AccumulatorResult result)

      Read the accumulated value and the number of accumulated values atomically.

      This function reads the value and count atomically. This can be used for averaging.

      Parameters:
        `result` - AccumulatorResult object to store the results in.