跳转到主要内容

Linux spidev 的纯 Python 接口。

项目描述

Linux spidev 的纯 Python 接口。

功能

  • Pythonic API

  • 纯 Python 模块:无需编译,不仅限于 CPython。

要求

  • Linux >=3.14 支持 SPI_IOC_{RD,WR}_MODE32 ioctls

  • python3 >=3.7 (cpython, pypy, …)

示例

警告:此示例 应直接执行。根据连接到此 SPI 总线的设备(这完全取决于板子),可能会导致各种问题,包括永久性硬件损坏。

此仅为快速概述此模块 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)

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分布

spidev2-0.9.0.tar.gz (37.5 kB 查看散列值)

上传于 来源

由以下支持