PHPackages                             microscrap/spi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. microscrap/spi

ActiveLibrary[Framework](/categories/framework)

microscrap/spi
==============

Unix spidev Bindings for The PHP Posi Extension

0.4.0(2w ago)02↓100%MITPHPPHP ^8.3

Since May 25Pushed 2w agoCompare

[ Source](https://github.com/microscrap/spi)[ Packagist](https://packagist.org/packages/microscrap/spi)[ Docs](https://dosr.projectsaturnstudios.com)[ RSS](/packages/microscrap-spi/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

microscrap/spi — Linux SPI / spidev bindings for ScrapyardIO
============================================================

[](#microscrapspi--linux-spi--spidev-bindings-for-scrapyardio)

PHP library that wraps the [**posi**](https://github.com/php-io-extensions/posi) extension with global helpers, enums, and data objects. Every helper delegates to a facade class under `Microscrap\Bindings\SPI`.

This project provides PHP bindings to the Linux `spidev` character device API, mirroring the public surface of `` (the `SPI_IOC_*` ioctl family used by `spi-tools`, Python's `spidev`, and friends).

Highlights
----------

[](#highlights)

- Open a spidev device (`/dev/spidev0.0`, `/dev/spidev1.1`, etc.) and configure mode, clock speed, and word size in one call
- Half-duplex byte I/O via `spi_read` / `spi_write` (uses `posix_read` / `posix_write` under the hood)
- Full-duplex, chained transfers via `spi_transfer` and the `SPITransfer` data object — packs `spi_ioc_transfer` and issues `SPI_IOC_MESSAGE(n)`
- Per-segment overrides: clock speed, bits-per-word, inter-word delay, CS-change, dual/quad lane counts
- Inspect and mutate every spidev attribute — `mode`, `max_speed_hz`, `bits_per_word`, `lsb_first`
- Typed `SPIMode` bitmask covers CPOL/CPHA, CS polarity, 3-wire, loopback, no-CS, dual/quad/octal lanes, ready signal
- Automatically prefers a native `spi_transfer(int $fd, …)` from a low-level extension when one is loaded, otherwise falls back to the `ext-posi` memory path
- Thin global `spi_*` helper API — all functions are `function_exists`-guarded

Requirements
------------

[](#requirements)

- PHP 8.3+
- Linux kernel with `spidev` enabled and a populated `/dev/spidev*` device
- **ext-posi** ^0.4.0 — install from [php-io-extensions/posi](https://github.com/php-io-extensions/posi); the `posi_mem_*` helpers are required for `spi_transfer` unless a native `spi_transfer(int $fd, …)` is loaded
- **microscrap/posix** ^0.4.0

Installation
------------

[](#installation)

Confirm **ext-posi** is loaded:

```
php -m | grep posi
```

Confirm the spidev device is visible (and that your user can access it, or run as root):

```
ls /dev/spidev*
```

On a Raspberry Pi, enable SPI via `raspi-config` or by adding `dtparam=spi=on` to `/boot/config.txt`.

```
composer require microscrap/spi
```

Composer autoloads `src/Helpers/spi-device.php`, registering the global `spi_*` functions.

Usage
-----

[](#usage)

SPI transactions are driven through **global helper functions** (`spi_open`, `spi_read`, `spi_write`, `spi_transfer`, etc.). All helpers delegate to the `Device` facade and are only defined once (`function_exists` guard).

Enums live under `Microscrap\Bindings\SPI\Enums`. The device handle is `Microscrap\Bindings\SPI\DataObjects\SPIDevice`, and a single transfer segment is described by `Microscrap\Bindings\SPI\DataObjects\SPITransfer`.

---

### Example — read a register from an SPI peripheral

[](#example--read-a-register-from-an-spi-peripheral)

```
