SPI — Serial Peripheral Interface

This module defines an object type that allows Serial Peripheral Interface (SPI) bus transactions on hosts running the Linux kernel. The host kernel must have SPI support and SPI device interface support.

Because the SPI device interface is opened R/W, users of this module usually must have root permissions or be members of a group with granted access rights.

Pins used for SPI0 and SPI1

PORT CS0 DO DI SCLK
SPI0 P9_17 P9_21 P9_18 P9_22
SPI1 P9_28 P9_29 P9_30 P9_31

Example:

import Adafruit_BBIO.SPI as SPI

from Adafruit_BBIO.SPI import SPI
# spi = SPI(bus, device) #/dev/spidev<bus>.<device>

# /dev/spidev0.0
spi = SPI(1, 0)
print(spi.xfer2([32, 11, 110, 22, 220]))
spi.close()

# /dev/spidev0.1
spi = SPI(1, 1)
print(spi.xfer2([32, 11, 110, 22, 220]))
spi.close()

# /dev/spidev1.0
spi = SPI(2, 0)
print(spi.xfer2([32, 11, 110, 22, 220]))
spi.close()

# /dev/spidev1.1
spi = SPI(2, 1)
print(spi.xfer2([32, 11, 110, 22, 220]))
spi.close()
class Adafruit_BBIO.SPI.SPI(bus, client)
Parameters:
  • bus – bus number.
  • client – client device number.
Returns:

a new SPI object, optionally connected to the specified SPI device interface.

Return type:

SPI

bpw

Bits per word.

cshigh

Chip Select (CS or Slave Select, SS) active high.

loop

Loopback configuration.

lsbfirst

Least Significant Bit (LSB) first.

mode

SPI mode as two bit pattern of Clock Polarity and Phase [CPOL|CPHA]; min– 0b00 = 0, max– 0b11 = 3.

msh

Maximum speed in Hz.

threewire

SI/SO signals are shared.

open(bus, device)

Connects the object to the specified SPI device. open(X, Y) will open /dev/spidev-X.Y

Parameters:
  • bus (int) – bus number
  • device (str) – device number
close()

Disconnects the object from the interface.

readbytes(len)

Read the specified length of bytes from the SPI device.

Parameters:len (int) – length of bytes to read, 1024 maximum.
Returns:values read
Return type:list[int]
writebytes(values)

Write bytes to the SPI device.

Parameters:values (list[int]) – list of values to write, with a maximum length of 1024.
xfer(values[, delay=0])

Perform an SPI transaction of values. Slave Select (SS or CS) will be released and reactivated between blocks.

Parameters:
  • values (list[int]) – list of values to transfer, with a maximum length of 1024.
  • delay – delay in microseconds between blocks.
Returns:

values transferred

Return type:

list[int]

xfer2(values)

Perform an SPI transaction of values. Slave Select (SS or CS) will be held active between blocks.

Parameters:values (list[int]) – list of values to transfer, with a maximum length of 1024.
Returns:values transferred
Return type:list[int]