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: enum.IntEnum

An enumeration.

kMXP = 4
kOnboardCS0 = 0
kOnboardCS1 = 1
kOnboardCS2 = 2
kOnboardCS3 = 3
devices = 0
forceAutoRead()[source]

Force the engine to make a single transfer.

Return type:None
free()[source]
freeAccumulator()[source]

Frees the accumulator.

Return type:None
freeAuto()[source]

Frees the automatic SPI transfer engine.

Return type:None
getAccumulatorAverage()[source]

Read the average of the accumulated value.

Return type:float
Returns:The accumulated average value (value / count).
getAccumulatorCount()[source]

Read the number of accumulated values.

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

Return type:int
Returns:The number of times samples from the channel were accumulated.
getAccumulatorLastValue()[source]

Read the last value read by the accumulator engine.

Return type:int
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.

Return type:AccumulatorResult
Returns:tuple of (value, count)
getAccumulatorValue()[source]

Read the accumulated value.

Return type:int
Returns:The 64-bit value accumulated since the last Reset().
getAutoDroppedCount()[source]

Get the number of bytes dropped by the automatic SPI transfer engine due to the receive buffer being full.

Return type:int
Returns:Number of bytes dropped
initAccumulator(period, cmd, xferSize, validMask, validValue, dataShift, dataSize, isSigned, bigEndian)[source]

Initialize the accumulator.

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

None

initAuto(bufferSize)[source]

Initialize automatic SPI transfer engine.

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.

Parameters:bufferSize (int) – buffer size in bytes
Return type:None
port
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 (bool) – 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 (int) – Number of bytes to read.
Return type:

bytes

Returns:

received data bytes

readAutoReceivedData(buffer, numToRead, timeout)[source]

Read data that has been transferred by the automatic SPI transfer engine.

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.

Parameters:
  • buffer – A ctypes c_uint8 buffer to read the data into
  • numToRead (int) – number of bytes to read
  • timeout (float) – timeout in seconds (ms resolution)
Return type:

(<class ‘int’>, <class ‘bytes’>)

Returns:

Number of bytes remaining to be read

resetAccumulator()[source]

Resets the accumulator to zero.

Return type:None
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.

Return type:None
setAccumulatorDeadband(deadband)[source]

Set the accumulator’s deadband.

Return type:None
setAutoTransmitData(dataToSend, zeroSize)[source]

Set the data to be transmitted by the engine.

Up to 16 bytes are configurable, and may be followed by up to 127 zero bytes.

Parameters:
  • dataToSend (bytes) – data to send (maximum 16 bytes)
  • zeroSize (int) – number of zeros to send after the data
Return type:

None

setChipSelectActiveHigh()[source]

Configure the chip select line to be active high.

Return type:None
setChipSelectActiveLow()[source]

Configure the chip select line to be active low.

Return type:None
setClockActiveHigh()[source]

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

Return type:None
setClockActiveLow()[source]

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

Return type:None
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 (int) – The clock rate in Hertz.
Return type:None
setLSBFirst()[source]

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

Return type:None
setMSBFirst()[source]

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

Return type:None
setSampleDataOnFalling()[source]

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

Return type:None
setSampleDataOnRising()[source]

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

Return type:None
startAutoRate(period)[source]

Start running the automatic SPI transfer engine at a periodic rate.

initAuto() and setAutoTransmitData() must be called before calling this function.

Parameters:period (float) – period between transfers, in seconds (us resolution)
Return type:None
startAutoTrigger(source, rising, falling)[source]

Start running the automatic SPI transfer engine when a trigger occurs.

initAuto() and setAutoTransmitData() must be called before calling this function.

Parameters:
  • source (DigitalSource) – digital source for the trigger (may be an analog trigger)
  • rising (bool) – trigger on the rising edge
  • falling (bool) – trigger on the falling edge
Return type:

None

stopAuto()[source]

Stop running the automatic SPI transfer engine.

Return type:None
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
Return type:bytes
Returns:data received from the device

Usage:

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

# send list of integers
data = spi.transaction([0x01, 0x02])
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
Return type:int
Returns:Number of bytes written

Usage:

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

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