Native PHP extension for SPI communication on RaspberryPi.
Extension is still in alpha state. Please don't hesitate to report bugs, make suggestions or ask questions.
Due to a open bug in PHP-CPP the extension is currently not working for PHP >= 7.3.
The extension uses the great pigpio library and is build on top of PHP-CPP.
Download the latest shared object (Replace the x chars by release and PHP version) and include it into php.ini:
cd /usr/lib/php/$(php -v | grep -i 'PHP [57]' | cut -c1-8 | sed s/'PHP '//g | cut -c1-3)
wget -O berry-spi.so https://github.com/Volantus/berry-spi/releases/download/x.x.x/php-x.x-berry-spi.so
sudo echo "extension=berry-spi.so" >> /etc/php/7.0/cli/php.ini
Please make sure you installed the PHP-CPP library (guide) + pigpio (Guide). Clone and compile the source code:
git clone https://github.com/Volantus/berry-spi
cd berry-spi
make
sudo make install
Communication is handled by the following classes
- RegularInterface: In case of using the native SPI pins (GPIO 07 - 11)
- BitBangingInterface: In case of using any other GPIO pins
Before creation of any SpiInterface instance, static initialization method needs to be called once (for the process lifetime) to initialize Pigpio library. Returns TRUE on success and FALSE on failure.
Please note that initialization takes around 200ms to execute on Raspberry Pi 3.
Gets called automatically on first instantiation if not called before.
use Volantus\BerrySpi\RegularInterface;
use Volantus\BerrySpi\BitBangingInterface;
RegularInterface::initialize();
// OR
BitBangingInterface::initialize();
Don't forget to close connection before object gets deleted. Otherwise new connection could fail.
use Volantus\BerrySpi\RegularInterface;
$interface = new RegularInterface(1, 32768, 0);
$interface->open();
$interface->close();
The constructor accept three parameters
Parameter | Description |
---|---|
channel | SPI channel (1 or 2) |
speed | Baud speed in bits per second |
32K-125M (values above 30M are unlikely to work) | |
flags | Additional configuration, see details |
Sending + retrieving data simultaneously. Same count of byte as send is read. Method accepts an array of words(bytes) to send.
$retrievedData = $interface->transfer([1, 2, 3]);
echo '1. received byte: ' . $retrievedData[0];
echo '2. received byte: ' . $retrievedData[1];
echo '3. received byte: ' . $retrievedData[2];
Reads given count of bytes
// Reading 8 bytes of data
$retrievedData = $interface->read(8);
Sends the given bytes
// Sending 3 bytes of data
$interface->write([16, 12, 8]);
Don't forget to close connection before object gets deleted. Otherwise new connection could fail.
use Volantus\BerrySpi\BitBangingInterface;
$interface = new BitBangingInterface(12, 16, 20, 21, 512, 0);
$interface->open();
$interface->close();
The constructor accept three parameters
Parameter | Description |
---|---|
csPin | The GPIO (0-31) used for the slave select signal *¹ |
misoPin | The GPIO (0-31) used for the MISO signal *² |
mosiPin | The GPIO (0-31) used for the MOSI signal *² |
sclkPin | The GPIO (0-31) used for the SCLK signal *² |
speed | Baud speed in bits per second |
50-250k | |
flags | Additional configuration, see details |
*¹ This pin has to be unique for each device
*² This pin can be shared with multiple slave devices, if no parallel data transfer is required
Sending + retrieving data simultaneously. Same count of byte as send is read. Method accepts an array of words(bytes) to send.
$retrievedData = $interface->transfer([1, 2, 3]);
echo '1. received byte: ' . $retrievedData[0];
echo '2. received byte: ' . $retrievedData[1];
echo '3. received byte: ' . $retrievedData[2];
All errors are wrapped in exceptions within the namespace Volantus\BerrySpi. To see which method throws which exception please consult the stubs.
In case of invalid parameters (e.g. negative channel or bad speed).
Pigpio library initialization failed (e.g. insufficient permissions).
In case of logically incorrect behaviour (e.g. trying to send data by an non-open connection).
In case of deeper problems (e.g. internal failure).
Contribution in form of bug reports, suggestions or pull requests is highly welcome!
For integration testing some GPIO pins need to be connected (e.g. by jumper cable) while running the test suite
- SPI_MISO (GPIO09) and SPI_MOSI (GPIO10)
- GPIO16 and GPIO20
All tests are managed by PHPUnit:
# Install PHPUnit if not already done
composer install
# Execution of the tests (sudo is required by pigpio)
sudo vendor/phpunit/phpunit/phpunit