SPI

class wpilib.SPI(port: wpilib._wpilib.SPI.Port)

Bases: pybind11_object

SPI bus interface class.

This class is intended to be used by sensor (and other SPI device) drivers. It probably should not be used directly.

Constructor

Parameters:

port – the physical SPI port

class Mode(value: int)

Bases: pybind11_object

Members:

kMode0

kMode1

kMode2

kMode3

kMode0 = <Mode.kMode0: 0>
kMode1 = <Mode.kMode1: 1>
kMode2 = <Mode.kMode2: 2>
kMode3 = <Mode.kMode3: 3>
property name
property value
class Port(value: int)

Bases: pybind11_object

Members:

kOnboardCS0

kOnboardCS1

kOnboardCS2

kOnboardCS3

kMXP

kMXP = <Port.kMXP: 4>
kOnboardCS0 = <Port.kOnboardCS0: 0>
kOnboardCS1 = <Port.kOnboardCS1: 1>
kOnboardCS2 = <Port.kOnboardCS2: 2>
kOnboardCS3 = <Port.kOnboardCS3: 3>
property name
property value
configureAutoStall(port: hal._wpiHal.SPIPort, csToSclkTicks: int, stallTicks: int, pow2BytesPerRead: int) None

Configure the Auto SPI Stall time between reads.

Parameters:
  • port – The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP.

  • csToSclkTicks – the number of ticks to wait before asserting the cs pin

  • stallTicks – the number of ticks to stall for

  • pow2BytesPerRead – the number of bytes to read before stalling

forceAutoRead() None

Force the engine to make a single transfer.

freeAccumulator() None

Frees the accumulator.

freeAuto() None

Frees the automatic SPI transfer engine.

getAccumulatorAverage() float

Read the average of the accumulated value.

Returns:

The accumulated average value (value / count).

getAccumulatorCount() int

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.

getAccumulatorIntegratedAverage() float

Read the average of the integrated value. This is the sum of (each value times the time between values), divided by the count.

Returns:

The average of the integrated value accumulated since the last Reset().

getAccumulatorIntegratedValue() float

Read the integrated value. This is the sum of (each value * time between values).

Returns:

The integrated value accumulated since the last Reset().

getAccumulatorLastValue() int

Read the last value read by the accumulator engine.

getAccumulatorOutput() Tuple[int, int]

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:
  • value – Pointer to the 64-bit accumulated output.

  • count – Pointer to the number of accumulation cycles.

getAccumulatorValue() int

Read the accumulated value.

Returns:

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

getAutoDroppedCount() int

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

Returns:

Number of bytes dropped

getPort() wpilib._wpilib.SPI.Port
initAccumulator(period: seconds, cmd: int, xferSize: int, validMask: int, validValue: int, dataShift: int, dataSize: int, isSigned: bool, bigEndian: bool) None

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 valid_mask 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?

initAuto(bufferSize: int) None

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 – buffer size in bytes

read(initiate: bool, dataReceived: buffer) int

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 – Buffer to receive data from the device

  • size – The length of the transaction, in bytes

readAutoReceivedData(buffer: buffer, timeout: seconds) int

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.

Each received data sequence consists of a timestamp followed by the received data bytes, one byte per word (in the least significant byte). The length of each received data sequence is the same as the combined size of the data and zeroSize set in SetAutoTransmitData().

Blocks until numToRead words have been read or timeout expires. May be called with numToRead=0 to retrieve how many words are available.

Parameters:
  • buffer – buffer where read words are stored

  • numToRead – number of words to read

  • timeout – timeout (ms resolution)

Returns:

Number of words remaining to be read

resetAccumulator() None

Resets the accumulator to zero.

setAccumulatorCenter(center: int) None

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(deadband: int) None

Set the accumulator’s deadband.

setAccumulatorIntegratedCenter(center: float) None

Set the center value of the accumulator integrator.

The center value is subtracted from each value*dt before it is added to the integrated value. This is used for the center value of devices like gyros and accelerometers to take the device offset into account when integrating.

setAutoTransmitData(dataToSend: buffer, zeroSize: int) None

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 – data to send (maximum 16 bytes)

  • zeroSize – number of zeros to send after the data

setChipSelectActiveHigh() None

Configure the chip select line to be active high.

setChipSelectActiveLow() None

Configure the chip select line to be active low.

setClockActiveHigh() None

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

Deprecated:

Use SetMode() instead.

setClockActiveLow() None

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

Deprecated:

Use SetMode() instead.

setClockRate(hz: int) None

Configure the rate of the generated clock signal.

The default value is 500,000Hz. The maximum value is 4,000,000Hz.

Parameters:

hz – The clock rate in Hertz.

setLSBFirst() None

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

Deprecated:

Does not work, will be removed.

setMSBFirst() None

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

Deprecated:

Does not work, will be removed.

setMode(mode: wpilib._wpilib.SPI.Mode) None

Sets the mode for the SPI device.

Mode 0 is Clock idle low, data sampled on rising edge

Mode 1 is Clock idle low, data sampled on falling edge

Mode 2 is Clock idle high, data sampled on falling edge

Mode 3 is Clock idle high, data sampled on rising edge

Parameters:

mode – The mode to set.

setSampleDataOnLeadingEdge() None

Configure that the data is stable on the leading edge and the data changes on the trailing edge.

Deprecated:

Use SetMode() instead.

setSampleDataOnTrailingEdge() None

Configure that the data is stable on the trailing edge and the data changes on the leading edge.

Deprecated:

Use SetMode() instead.

startAutoRate(period: seconds) None

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

InitAuto() and SetAutoTransmitData() must be called before calling this function.

Parameters:

period – period between transfers (us resolution)

startAutoTrigger(source: wpilib._wpilib.DigitalSource, rising: bool, falling: bool) None

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

InitAuto() and SetAutoTransmitData() must be called before calling this function.

Parameters:
  • source – digital source for the trigger (may be an analog trigger)

  • rising – trigger on the rising edge

  • falling – trigger on the falling edge

stopAuto() None

Stop running the automatic SPI transfer engine.

transaction(dataToSend: buffer, dataReceived: buffer) int

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

write(data: buffer) int

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

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