SPI

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')

Constructor

Parameters:
  • 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
SPI.free()[source]
SPI.freeAccumulator()[source]

Frees the accumulator.

SPI.getAccumulatorAverage()[source]

Read the average of the accumulated value.

Returns:The accumulated average value (value / count).
SPI.getAccumulatorCount()[source]

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.
SPI.getAccumulatorLastValue()[source]

Read the last value read by the accumulator engine.

SPI.getAccumulatorOutput()[source]

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)
SPI.getAccumulatorValue()[source]

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.

Parameters:
  • 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.port
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.

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

received data bytes

SPI.resetAccumulator()[source]

Resets the accumulator to zero.

SPI.setAccumulatorCenter(center)[source]

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.

SPI.setAccumulatorDeadband(deadband)[source]

Set the accumulator’s deadband.

SPI.setChipSelectActiveHigh()[source]

Configure the chip select line to be active high.

SPI.setChipSelectActiveLow()[source]

Configure the chip select line to be active low.

SPI.setClockActiveHigh()[source]

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

SPI.setClockActiveLow()[source]

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

SPI.setClockRate(hz)[source]

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.
SPI.setLSBFirst()[source]

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

SPI.setMSBFirst()[source]

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

SPI.setSampleDataOnFalling()[source]

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

SPI.setSampleDataOnRising()[source]

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

SPI.transaction(dataToSend)[source]

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

Usage:

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

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

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

Usage:

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

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