SPIDev Transport

The SPIDev transport provides SPI communication with the TROPIC01 chip on Linux systems using the spidev kernel module with manual GPIO chip select control.

SPIDev Transport Implementation for Raspberry Pi

This module provides SPI transport implementation for Linux spidev interface with manual GPIO chip select control.

Recommended Setup:

Use general-purpose GPIO (e.g., GPIO 25, physical pin 22):

transport = SpiDevTransport(bus=0, device=0, cs_pin=25)
Alternative - Hardware CS Pins:

GPIO 8 (CE0, pin 24) or GPIO 7 (CE1, pin 26):

transport = SpiDevTransport(bus=0, device=0, cs_pin=8)

Note

Device tree overlay requirement depends on your hardware configuration:

No overlay needed if:

  • You have NO other SPI devices connected to hardware CS pins (GPIO 8/7)

  • The kernel will toggle GPIO 8 (for /dev/spidev0.0) or GPIO 7 (for /dev/spidev0.1) during transfers, but this is harmless if nothing is connected there

Overlay REQUIRED if:

  • You have other SPI devices connected to CE0 (GPIO 8) or CE1 (GPIO 7)

  • Without overlay, kernel will activate both your manual CS and hardware CS simultaneously, causing bus conflicts

To disable hardware CS, add to /boot/firmware/config.txt and reboot:

  • dtoverlay=spi0-0cs - Don’t claim any CS pins (recommended for manual CS)

  • dtoverlay=spi0-1cs,cs0_pin=<gpio> - Use only one CS pin (specify which)

  • dtoverlay=spi0-2cs,cs0_pin=<gpio>,cs1_pin=<gpio> - Remap CS pins

class tropicsquare.transports.spidev.SpiDevTransport(bus=0, device=1, cs_pin=25, max_speed_hz=1000000, gpio_chip='/dev/gpiochip0')[source]

Bases: L1Transport

L1 transport for Linux spidev interface with manual GPIO CS control.

This transport uses the spidev library for SPI communication and gpiod for manual chip select control, providing precise timing control over the CS line.

Parameters:
  • bus (int) – SPI bus number (default: 0 for /dev/spidev0.x)

  • device (int) – SPI device number (default: 1 for CE1, use 0 for CE0)

  • cs_pin (int) – GPIO pin number for chip select (default: 25 for CE2, use 8 for CE0 or 7 for CE1)

  • max_speed_hz (int) – SPI clock speed in Hz (default: 1000000 = 1 MHz)

  • gpio_chip (str) – GPIO chip device path (default: /dev/gpiochip0)

Example:

from tropicsquare.transports.spidev import SpiDevTransport
from tropicsquare import TropicSquareCPython

# Create transport for Raspberry Pi (using CE2 - no overlay needed)
transport = SpiDevTransport(
    bus=0,
    device=1,
    cs_pin=25  # GPIO 25 (physical pin 22, CE2)
)

# Create TropicSquare instance
ts = TropicSquareCPython(transport)

try:
    # Use the chip
    chip_id = ts.chip_id
    print(f"Chip ID: {chip_id}")
finally:
    # Always cleanup
    transport.close()
__init__(bus=0, device=1, cs_pin=25, max_speed_hz=1000000, gpio_chip='/dev/gpiochip0')[source]

Initialize SPIDev transport with manual GPIO CS control.

Parameters:
  • bus (int) – SPI bus number

  • device (int) – SPI device number

  • cs_pin (int) – GPIO pin number for chip select

  • max_speed_hz (int) – SPI clock speed in Hz

  • gpio_chip (str) – Path to GPIO chip device

Raises:

OSError – If SPI or GPIO device cannot be opened

close()[source]

Release SPI and GPIO resources.

This method should be called when done using the transport to properly cleanup hardware resources. It’s recommended to use the transport in a try/finally block or context manager.

Example:

transport = SpiDevTransport()
try:
    # Use transport
    pass
finally:
    transport.close()
Return type:

None

Platform Support

  • Raspberry Pi (all models): Full support via spidev and gpiod

  • Linux with spidev: Any Linux system with spidev kernel module

Requirements

  • Python packages: spidev>=3.5, gpiod>=2.0

  • User permissions: Add user to spi and gpio groups

  • Device tree overlay: Only required if other SPI devices on CE0/CE1 (see module docstring)

Hardware Setup

  • SPI Bus: Use /dev/spidev0.0 (SPI0)

  • Chip Select: Any free GPIO (default: GPIO 25, physical pin 22)

  • MISO/MOSI/SCK: Standard SPI0 pins (GPIO 9/10/11)

Example Usage

from tropicsquare import TropicSquare
from tropicsquare.transports.spidev import SpiDevTransport

# Create transport
transport = SpiDevTransport(
    bus=0,
    device=0,
    cs_pin=25,  # GPIO 25 (or any free GPIO)
    max_speed_hz=1000000
)

# Create TropicSquare instance
ts = TropicSquare(transport)

try:
    # Use the chip
    chip_id = ts.chip_id
    print(f"Chip ID: {chip_id}")
finally:
    # Always cleanup
    transport.close()

See Also