Skip to main content

Python Spidev

This project contains a python module for interfacing with SPI devices from user space via the spidev linux kernel driver.

All code is MIT licensed unless explicitly stated otherwise.

Usage

import spidev
spi = spidev.SpiDev()
spi.open(bus, device)
to_send = [0x01, 0x02, 0x03]
spi.xfer(to_send)

Settings

import spidev
spi = spidev.SpiDev()
spi.open(bus, device)

# Settings (for example)
spi.max_speed_hz = 5000
spi.mode = 0b01

...
  • bits_per_word
  • cshigh
  • loop - Set the "SPI_LOOP" flag to enable loopback mode
  • no_cs - Set the "SPI_NO_CS" flag to disable use of the chip select (although the driver may still own the CS pin)
  • lsbfirst
  • max_speed_hz
  • mode - SPI mode as two bit pattern of clock polarity and phase [CPOL|CPHA], min: 0b00 = 0, max: 0b11 = 3
  • threewire - SI/SO signals shared
  • read0 - Read 0 bytes after transfer to lower CS if cshigh == True

Methods

open(bus, device)

Connects to the specified SPI device, opening /dev/spidev<bus>.<device>

readbytes(n)

Read n bytes from SPI device.

writebytes(list of values)

Writes a list of values to SPI device.

writebytes2(list of values)

Similar to writebytes but accepts arbitrary large lists. If list size exceeds buffer size (which is read from /sys/module/spidev/parameters/bufsiz), data will be split into smaller chunks and sent in multiple operations.

Also, writebytes2 understands buffer protocol so it can accept numpy byte arrays for example without need to convert them with tolist() first. This offers much better performance where you need to transfer frames to SPI-connected displays for instance.

xfer(list of values[, speed_hz, delay_usec, bits_per_word])

Performs an SPI transaction. Chip-select should be released and reactivated between blocks. Delay specifies the delay in usec between blocks.

xfer2(list of values[, speed_hz, delay_usec, bits_per_word])

Performs an SPI transaction. Chip-select should be held active between blocks.

xfer3(list of values[, speed_hz, delay_usec, bits_per_word])

Similar to xfer2 but accepts arbitrary large lists. If list size exceeds buffer size (which is read from /sys/module/spidev/parameters/bufsiz), data will be split into smaller chunks and sent in multiple operations.

close()

Disconnects from the SPI device.

Changelog

3.6

  • Added read0 flag to enable reading 0 bytes after transfer to lower CS when cshigh == True

3.5

  • Fixed memory leaks

3.4

  • Changed license to MIT

3.0.1

  • Fixed README.md and CHANGELOG.md formatting, hopefully

3.0

2.0

Code sourced from http://elk.informatik.fh-augsburg.de/da/da-49/trees/pyap7k/lang/py-spi and modified.

Pre 2.0

spimodule.c originally uathored by Volker Thoms, 2009.

Release files for spidev 3.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spidev 3.6
File Size Uploaded
spidev-3.6.tar.gz 11.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spidev 3.6
File Interpreter ABI Platform
spidev-3.6-cp39-cp39-linux_armv7l.whl CPython 3.9 CPython 3.9 linux armv7l Details

Total release size: 51.9 kB

Release files / spidev-3.6.tar.gz

Download URL spidev-3.6.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
14dbc37594a4aaef85403ab617985d3c3ef464d62bc9b769ef552db53701115b
BLAKE2b-256 checksum
How to use checksums
c7d9401c0a7be089e02826cf2c201f489876b601f15be100fe391ef9c2faed83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release files / spidev-3.6-cp39-cp39-linux_armv7l.whl

Download URL spidev-3.6-cp39-cp39-linux_armv7l.whl
Size 40.0 kB
Tags CPython 3.9 linux armv7l
SHA-256 checksum
How to use checksums
280abc00a1ef7780ef62c3f294f52a2527b6c47d8c269fea98664970bcaf6da5
BLAKE2b-256 checksum
How to use checksums
287348c2f27691b95dc7eff2263e92e62522b2f034186be5ecc294087b3d38aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.2

Release history Release notifications | RSS feed

3.8

2 release files

3.7

2 release files

This release

3.6 This release

2 release files

3.5

1 release file

3.4

1 release file

3.2

1 release file

3.1

1 release file

3.0

1 release file

2.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page