Pure-python interface to Linux spidev.
Features
Pythonic API
Pure python module: no compilation needed, not limited to CPython.
Requirements
Linux >=3.14 for SPI_IOC_{RD,WR}_MODE32 ioctls
python3 >=3.7 (cpython, pypy, …)
Examples
Warning: this example is not meant to be executed as-is. Depending on what is connected to the SPI bus used here (which is entirely board-dependent), this could cause all sort of problems, including permanent hardware damage.
This is only to be taken as a quick overview of this module’s API.
from spidev2 import SPIBus, SPITransferList, SPIMode32
with SPIBus(
'/dev/spidev0.0',
'w+b',
bits_per_word=8,
speed_hz=16_000_000,
spi_mode=(
SPIMode32.SPI_MODE_0 |
SPIMode32.SPI_TX_OCTAL |
SPIMode32.SPI_RX_OCTAL
),
) as spi:
# Simple single-transfer full-duplex usage. Low performance: a reception
# buffer is allocated on every call, and as the tranmission buffer is
# immutable, it will be copied
received = spi.transfer(
tx_buf=b'\x12\x34\x00\x00',
speed_hz=1_000_000,
)[2:]
# The same transfer, with reusable buffers for better performance
# (the kernel will still copy memory both ways).
spi_tx_buffer = bytearray(4)
spi_rx_buffer = bytearray(len(spi_tx_buffer))
# Initialise the tx buffer for the upcomming transfer
spi_tx_buffer[0:2] = b'\x12\x34'
spi.transfer(
tx_buf=spi_tx_buffer,
rx_buf=spi_rx_buffer,
speed_hz=1_000_000,
)
received = spi_rx_buffer[2:]
# It is also possible to use the same transfer both ways:
spi_buffer = bytearray(4)
spi_buffer[0:2] = b'\x12\x34'
spi.transfer(
tx_buf=spi_buffer,
rx_buf=spi_buffer,
speed_hz=1_000_000,
)
received = spi_buffer[2:]
# Multi-transfer usage. Reduces the number of syscalls, reduces the
# need to slice buffers to access received values (reducing the
# number of memory copy operations) and allows transfer structure reuse
# (reducing memory allocation operations).
spi_tx_buffer = bytearray(b'\x12\x34')
received = bytearray(2)
transfer_list = SPITransgerList((
{
'tx_buf': spi_tx_buffer,
'speed_hz': 1_000_000,
},
{
'rx_buf': received,
'speed_hz': 1_000_000,
},
))
spi.submitTransferList(transfer_list)
# Half-duplex usage, chip-select being released between calls.
# Per-transfer options are not available, so the bus must be
# reconfigured if its current configuration is not suitable.
spi.speed_hz = 1_000_000
spi.write(b'\x12\x34')
spi.read(2)
Metadata
Release files for spidev2 0.9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spidev2-0.9.0.tar.gz | 37.5 kB | Without provenance | Details for spidev2-0.9.0.tar.gz |
Release files / spidev2-0.9.0.tar.gz
| Download URL | spidev2-0.9.0.tar.gz |
|---|---|
| Size | 37.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
152da2911a8660283ceac3a75dd869953379bcbcf079e5436af5aae736876086
|
|
BLAKE2b-256 checksum How to use checksums |
8e40e43163e764d0f97ab68c6e7db6c01affe85f199929027ecc25896a1c0f8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.6
|
Security for spidev2 0.9.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.