class wpilib.SPI(port, simPort=None)[source]

Bases: object

Represents a SPI bus port

Example usage:

spi = wpilib.SPI(wpilib.SPI.Port.kOnboardCS0)

# Write bytes 'text', and receive something
data = spi.transaction(b'text')


  • port (SPI.Port) – the physical SPI port
  • simPort – This must be an object that implements all of the spi* functions from hal_impl that you use. See test_spi.py for an example.
class Port[source]

Bases: object

kMXP = 4
kOnboardCS0 = 0
kOnboardCS1 = 1
kOnboardCS2 = 2
kOnboardCS3 = 3
SPI.devices = 0

Frees the accumulator.


Read the average of the accumulated value.

Returns:The accumulated average value (value / count).

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.

Read the last value read by the accumulator engine.


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.

Returns:tuple of (value, count)

Read the accumulated value.

Returns:The 64-bit value accumulated since the last Reset().
SPI.initAccumulator(period, cmd, xfer_size, valid_mask, valid_value, data_shift, data_size, is_signed, big_endian)[source]

Initialize the accumulator.

  • period – Time between reads
  • cmd – SPI command to send to request data
  • xfer_size – SPI transfer size, in bytes
  • valid_mask – Mask to apply to received data for validity checking
  • valid_data – After valid_mask is applied, required matching value for validity checking
  • data_shift – Bit shift to apply to received data to get actual data value
  • data_size – Size (in bits) of data field
  • is_signed – Is data field signed?
  • big_endian – Is device big endian?
SPI.read(initiate, size)[source]

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.

  • 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.
  • size – Number of bytes to read.

received data bytes


Resets the accumulator to zero.


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.


Set the accumulator’s deadband.


Configure the chip select line to be active high.


Configure the chip select line to be active low.


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


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


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.

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


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


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


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


Perform a simultaneous read/write transaction with the device

Parameters:dataToSend (iterable of bytes) – The data to be written out to the device
Returns:data received from the device


# send byte string
data = spi.transaction(b'stuff')

# send list of integers
data = spi.transaction([0x01, 0x02])

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 (iterable of bytes) – Data to send
Returns:Number of bytes written


# send byte string
writeCount = spi.write(b'stuff')

# send list of integers
writeCount = spi.write([0x01, 0x02])