SPI
Code Example
from periphery import SPI
# Open spidev1.0 with mode 0 and max speed 1MHz
spi = SPI("/dev/spidev1.0", 0, 1000000)
data_out = [0xaa, 0xbb, 0xcc, 0xdd]
data_in = spi.transfer(data_out)
print("shifted out [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_out))
print("shifted in [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_in))
spi.close()
API
- class periphery.SPI(devpath, mode, max_speed, bit_order='msb', bits_per_word=8, extra_flags=0)[source]
Bases:
objectInstantiate a SPI object and open the spidev device at the specified path with the specified SPI mode, max speed in hertz, and the defaults of “msb” bit order and 8 bits per word.
- Parameters:
devpath (str) – spidev device path.
mode (int) – SPI mode, can be 0, 1, 2, 3.
max_speed (int, float) – maximum speed in Hertz.
bit_order (str) – bit order, can be “msb” or “lsb”.
bits_per_word (int) – bits per word.
extra_flags (int) – extra spidev flags to be bitwise-ORed with the SPI mode.
- Returns:
SPI object.
- Return type:
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if devpath, mode, max_speed, bit_order, bits_per_word, or extra_flags types are invalid.
ValueError – if mode, bit_order, bits_per_word, or extra_flags values are invalid.
- transfer(data, inplace=False)[source]
Shift out data and return shifted in data.
- Parameters:
data (bytes, bytearray, list) – a byte array or list of 8-bit integers to shift out.
inplace (bool) – read into data in-place. data must be of type bytearray.
- Returns:
data shifted in.
- Return type:
bytes, bytearray, list
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if data type is invalid.
- transfer_advanced(messages, inplace=False)[source]
Transfer multiple messages, shifting out data of each message, while shifting the same length in, modifying the messages list with the shifted in data of each message. If deselect is true, deselect the device before reselecting it for the following transfer.
deselect_delay_us specifies a delay in microseconds before deselection when deselect is true. word_delay_us specifies a delay in microseconds between words within a message. Note that deselect_delay_us and word_delay_us may not be supported by all SPI controllers and may be silently ignored.
- Parameters:
messages (list, tuple) – list of SPI.Message messages to transfer.
inplace (bool) – read into messages in-place. data must be of type bytearray.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if messages or message types are invalid.
ValueError – if messages length is zero.
- property fd
Get the file descriptor of the underlying spidev device.
- Type:
int
- property devpath
Get the device path of the underlying spidev device.
- Type:
str
- property mode
Get or set the SPI mode. Can be 0, 1, 2, 3.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if mode type is not int.
ValueError – if mode value is invalid.
- Type:
int
- property max_speed
Get or set the maximum speed in Hertz.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if max_speed type is not int or float.
- Type:
int, float
- property bit_order
Get or set the SPI bit order. Can be “msb” or “lsb”.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if bit_order type is not str.
ValueError – if bit_order value is invalid.
- Type:
str
- property bits_per_word
Get or set the SPI bits per word.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if bits_per_word type is not int.
ValueError – if bits_per_word value is invalid.
- Type:
int
- property extra_flags
Get or set the spidev extra flags. Extra flags are bitwise-ORed with the SPI mode.
- Raises:
SPIError – if an I/O or OS error occurs.
TypeError – if extra_flags type is not int.
ValueError – if extra_flags value is invalid.
- Type:
int
- class Message(data, deselect=False, deselect_delay_us=0, word_delay_us=0)[source]
Bases:
objectInstantiate an SPI Message object.
- Parameters:
data (bytes, bytearray, list) – a byte array or list of 8-bit integers to shift out.
deselect (bool) – deselect the device before reselecting it for the following transfer.
deselect_delay_us (int) – delay in microseconds before deselection when deselect is true.
word_delay_us (int) – delay in microseconds between words within the transfer.
- Returns:
Message object.
- Return type: