PHPackages                             php-io-extensions/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-io-extensions/spi

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

php-io-extensions/spi
=====================

PHP-Controllable SPI Extension

v0.1.7(4w ago)010MITCPHP &gt;=8.3

Since May 13Pushed 4w agoCompare

[ Source](https://github.com/php-io-extensions/spi)[ Packagist](https://packagist.org/packages/php-io-extensions/spi)[ RSS](/packages/php-io-extensions-spi/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (8)Used By (0)

SPI
===

[](#spi)

[![PHP](https://camo.githubusercontent.com/91e2ff786d2fba1edf015025006e0156a071320b3662eaf2c50f39d4bb4b2369/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d626c7565)](https://www.php.net)[![Zephir](https://camo.githubusercontent.com/5add3cf7b546a9aa97c8f3d9f5caeda8c9fdf1bd0f90151d5b3e8e25e591ed83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7a65706869722d302e31392532422d6f72616e6765)](https://docs.zephir-lang.com/latest/en/installation)[![Platform](https://camo.githubusercontent.com/ec3ffbe89f8d1f152dec06cd451833acef4ded894b500c724cb70033418ec0be/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c696e75782d6c6967687467726579)](https://www.kernel.org)

PHP-controllable Linux SPI extension built with Zephir.

The SPI extension allows PHP to communicate with SPI devices on Linux by configuring a `spidev` file descriptor and performing read, write, and full-duplex transfer operations. Useful for sensors, DACs, ADCs, displays, EEPROMs, and other peripherals connected over SPI.

```
git clone https://github.com/DeptOfScrapyardRobotics/SPI
cd SPI
bash install.sh
```

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

[](#requirements)

- PHP 8.3+ with development headers (`php-dev` / `php-devel`)
- [Zephir](https://docs.zephir-lang.com/latest/en/installation) 0.19+
- [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) — provides raw integer file descriptors via `Fd\FD::open()`
- Linux with SPI userspace support (`/dev/spidevB.C`)
- `spidev` kernel support enabled

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

[](#installation)

Install Zephir if you haven't already:

```
composer global require phalcon/zephir
```

Install the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) first. SPI methods in this extension expect a raw integer file descriptor, which PHP's normal stream APIs do not provide:

```
git clone https://github.com/DeptOfScrapyardRobotics/FD
cd FD && bash install.sh
```

Make sure the `spidev` device exists on your system:

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

Then clone and build this extension:

```
git clone https://github.com/DeptOfScrapyardRobotics/SPI
cd SPI
bash install.sh
```

`install.sh` handles the full workflow: clean → build → copy `.so` → write `30-spi.ini` into detected `conf.d` directories → verify `php -m` → reload php-fpm if present.

To use a specific Zephir binary:

```
ZEPHIR_BIN=/path/to/zephir bash install.sh
```

API
---

[](#api)

All methods are static. File descriptors are plain integers. Use the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) to open and close them.

```
use Fd\FD;
use Spi\SPIRead;
use Spi\SPIWrite;

$fd = FD::open('/dev/spidev0.0', 2); // O_RDWR = 2
// ... use $fd with SPIRead and SPIWrite methods ...
FD::close($fd);
```

### `Spi\SPIWrite`

[](#spispiwrite)

Configuration setters and transfer methods for an SPI file descriptor.

---

#### `wrMode(int $fd, int $value): int`

[](#wrmodeint-fd-int-value-int)

Writes the 8-bit SPI mode flags using `SPI_IOC_WR_MODE`. Returns `0` on success, `-1` on failure.

---

#### `wrMode32(int $fd, int $value): int`

[](#wrmode32int-fd-int-value-int)

Writes the 32-bit SPI mode flags using `SPI_IOC_WR_MODE32`. Returns `0` on success, `-1` on failure.

---

#### `wrMaxSpeedHz(int $fd, int $value): int`

[](#wrmaxspeedhzint-fd-int-value-int)

Sets the SPI clock speed in Hz. Returns `0` on success, `-1` on failure.

---

#### `wrBitsPerWord(int $fd, int $value): int`

[](#wrbitsperwordint-fd-int-value-int)

Sets bits per word, commonly `8`. Returns `0` on success, `-1` on failure.

---

#### `wrLsbFirst(int $fd, int $value): int`

[](#wrlsbfirstint-fd-int-value-int)

Sets bit order. Use `0` for MSB-first and `1` for LSB-first. Returns `0` on success, `-1` on failure.

---

#### `write(int $fd, string $payload): int`

[](#writeint-fd-string-payload-int)

Performs a half-duplex write using the standard `write(2)` syscall. Returns the number of bytes written, or `-1` on failure.

---

#### `message(int $fd, string $tx): array`

[](#messageint-fd-string-tx-array)

Performs a single full-duplex SPI transfer using `SPI_IOC_MESSAGE(1)`.

Returns an associative array:

KeyTypeDescription`success``int``ioctl()` return value; negative means failure`data``string`Bytes received during the transferThe number of bytes received matches the length of `$tx`. If your device needs clock pulses to return data, include dummy bytes in `$tx`.

---

### `Spi\SPIRead`

[](#spispiread)

Configuration getters and half-duplex read operations for an SPI file descriptor.

---

#### `rdMode(int $fd): int`

[](#rdmodeint-fd-int)

Reads the current 8-bit SPI mode via `SPI_IOC_RD_MODE`. Returns the mode value, or `-1` on failure.

---

#### `rdMode32(int $fd): int`

[](#rdmode32int-fd-int)

Reads the current 32-bit SPI mode via `SPI_IOC_RD_MODE32`. Returns the mode value, or `-1` on failure.

---

#### `rdMaxSpeedHz(int $fd): int`

[](#rdmaxspeedhzint-fd-int)

Reads the configured SPI clock speed in Hz. Returns the value, or `-1` on failure.

---

#### `rdBitsPerWord(int $fd): int`

[](#rdbitsperwordint-fd-int)

Reads the configured bits-per-word value. Returns the value, or `-1` on failure.

---

#### `rdLsbFirst(int $fd): int`

[](#rdlsbfirstint-fd-int)

Reads the configured bit order. Returns `0` for MSB-first, `1` for LSB-first, or `-1` on failure.

---

#### `read(int $fd, int $num_bytes): string`

[](#readint-fd-int-num_bytes-string)

Performs a half-duplex read using the standard `read(2)` syscall. Returns the bytes read as a binary string, or `""` on failure.

Usage
-----

[](#usage)

```
use Fd\FD;
use Spi\SPIRead;
use Spi\SPIWrite;
```

Common mode values:

ModeValue`SPI_MODE_0``0``SPI_MODE_1``1``SPI_MODE_2``2``SPI_MODE_3``3`---

### Configure a device

[](#configure-a-device)

```
$fd = FD::open('/dev/spidev0.0', 2); // O_RDWR

SPIWrite::wrMode($fd, 0);            // SPI mode 0
SPIWrite::wrMaxSpeedHz($fd, 500000); // 500 kHz
SPIWrite::wrBitsPerWord($fd, 8);
SPIWrite::wrLsbFirst($fd, 0);        // MSB first

echo "Mode: " . SPIRead::rdMode($fd) . PHP_EOL;
echo "Speed: " . SPIRead::rdMaxSpeedHz($fd) . PHP_EOL;

FD::close($fd);
```

---

### Half-duplex write

[](#half-duplex-write)

```
$fd = FD::open('/dev/spidev0.0', 2);

SPIWrite::wrMode($fd, 0);
SPIWrite::wrMaxSpeedHz($fd, 1000000);
SPIWrite::wrBitsPerWord($fd, 8);

$written = SPIWrite::write($fd, "\x9A\xBC\xDE");
echo "Bytes written: {$written}\n";

FD::close($fd);
```

---

### Half-duplex read

[](#half-duplex-read)

```
$fd = FD::open('/dev/spidev0.0', 2);

SPIWrite::wrMode($fd, 0);
SPIWrite::wrMaxSpeedHz($fd, 1000000);
SPIWrite::wrBitsPerWord($fd, 8);

$data = SPIRead::read($fd, 4);
echo bin2hex($data) . PHP_EOL;

FD::close($fd);
```

---

### Full-duplex transfer

[](#full-duplex-transfer)

```
$fd = FD::open('/dev/spidev0.0', 2);

SPIWrite::wrMode($fd, 0);
SPIWrite::wrMaxSpeedHz($fd, 1000000);
SPIWrite::wrBitsPerWord($fd, 8);

$result = SPIWrite::message($fd, "\x9F\x00\x00\x00");

if ($result['success'] >= 0) {
    echo "RX: " . bin2hex($result['data']) . PHP_EOL;
}

FD::close($fd);
```

---

### Query current configuration

[](#query-current-configuration)

```
$fd = FD::open('/dev/spidev0.0', 2);

echo "Mode: " . SPIRead::rdMode($fd) . PHP_EOL;
echo "Mode32: " . SPIRead::rdMode32($fd) . PHP_EOL;
echo "Max speed: " . SPIRead::rdMaxSpeedHz($fd) . PHP_EOL;
echo "Bits/word: " . SPIRead::rdBitsPerWord($fd) . PHP_EOL;
echo "LSB first: " . SPIRead::rdLsbFirst($fd) . PHP_EOL;

FD::close($fd);
```

Notes
-----

[](#notes)

- This extension operates on Linux `spidev` device files such as `/dev/spidev0.0`.
- `message()` performs a single transfer whose RX length matches the TX length.
- Some SPI devices do not support plain `read()` and instead require command bytes or dummy bytes through `message()`.
- This extension does not open device files itself; use the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) for that.

License
-------

[](#license)

Copyright © Project Saturn Studios, LLC. All rights reserved.

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance94

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

7

Last Release

28d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10563160?v=4)[Angel Gonzalez](/maintainers/projectsaturnstudios)[@projectsaturnstudios](https://github.com/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (7 commits)")

### Embed Badge

![Health badge](/badges/php-io-extensions-spi/health.svg)

```
[![Health](https://phpackages.com/badges/php-io-extensions-spi/health.svg)](https://phpackages.com/packages/php-io-extensions-spi)
```

###  Alternatives

[kryptonit3/counter

Hit counter for your pages.

8424.2k](/packages/kryptonit3-counter)[neutron/signal-handler

A library to ease the use of signal handling.

12102.9k2](/packages/neutron-signal-handler)[bernardosilva/git-hooks-php

Composer git-hook package with hooks for your php projects.

2516.7k1](/packages/bernardosilva-git-hooks-php)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
