SPI – a 3-4 wire serial protocol

SPI is a serial protocol that has exclusive pins for data in and out of the master. It is typically faster than I2C because a separate pin is used to control the active slave rather than a transitted address. This class only manages three of the four SPI lines: clock, MOSI, MISO. Its up to the client to manage the appropriate slave select line. (This is common because multiple slaves can share the clock, MOSI and MISO lines and therefore the hardware.)

class busio.SPI(clock, MOSI=None, MISO=None)
Construct an SPI object on the given pins.

See also

Using this class directly requires careful lock management. Instead, use SPIDevice to manage locks.

See also

Using this class to directly read registers requires manual bit unpacking. Instead, use an existing driver or make one with Register data descriptors.

Parameters:
  • clock (Pin) – the pin to use for the clock.
  • MOSI (Pin) – the Master Out Slave In pin.
  • MISO (Pin) – the Master In Slave Out pin.
deinit()

Turn off the SPI bus.

__enter__()

No-op used by Context Managers.

__exit__()

Automatically deinitializes the hardware when exiting a context. See Lifetime and ContextManagers for more info.

configure(*, baudrate=100000, polarity=0, phase=0, bits=8)

Configures the SPI bus. Only valid when locked.

Parameters:
  • baudrate (int) – the desired clock rate in Hertz. The actual clock rate may be higher or lower due to the granularity of available clock settings. Check the frequency attribute for the actual clock rate. Note: on the SAMD21, it is possible to set the baud rate to 24 MHz, but that speed is not guaranteed to work. 12 MHz is the next available lower speed, and is within spec for the SAMD21.
  • polarity (int) – the base state of the clock line (0 or 1)
  • phase (int) – the edge of the clock that data is captured. First (0) or second (1). Rising or falling depends on clock polarity.
  • bits (int) – the number of bits per word
try_lock()

Attempts to grab the SPI lock. Returns True on success.

Returns:True when lock has been grabbed
Return type:bool
unlock()

Releases the SPI lock.

write(buffer, *, start=0, end=len(buffer))

Write the data contained in buffer. The SPI object must be locked. If the buffer is empty, nothing happens.

Parameters:
  • buffer (bytearray) – Write out the data in this buffer
  • start (int) – Start of the slice of buffer to write out: buffer[start:end]
  • end (int) – End of the slice; this index is not included
readinto(buffer, *, start=0, end=len(buffer), write_value=0)

Read into buffer while writing write_value for each byte read. The SPI object must be locked. If the number of bytes to read is 0, nothing happens.

Parameters:
  • buffer (bytearray) – Read data into this buffer
  • start (int) – Start of the slice of buffer to read into: buffer[start:end]
  • end (int) – End of the slice; this index is not included
  • write_value (int) – Value to write while reading. (Usually ignored.)
write_readinto(buffer_out, buffer_in, *, out_start=0, out_end=len(buffer_out), in_start=0, in_end=len(buffer_in))

Write out the data in buffer_out while simultaneously reading data into buffer_in. The SPI object must be locked. The lengths of the slices defined by buffer_out[out_start:out_end] and buffer_in[in_start:in_end] must be equal. If buffer slice lengths are both 0, nothing happens.

Parameters:
  • buffer_out (bytearray) – Write out the data in this buffer
  • buffer_in (bytearray) – Read data into this buffer
  • out_start (int) – Start of the slice of buffer_out to write out: buffer_out[out_start:out_end]
  • out_end (int) – End of the slice; this index is not included
  • in_start (int) – Start of the slice of buffer_in to read into: buffer_in[in_start:in_end]
  • in_end (int) – End of the slice; this index is not included
frequency

The actual SPI bus frequency. This may not match the frequency requested due to internal limitations.