## SPI Device Driver

The SPI kernel device drivers provide access to the BCM2835 spi interface.

In common with many other device drivers it makes 'files' available in the /dev/ folder of your system that you can write to and read from using ioctl commands.

The module HiPi::Device::SPI supports communication with SPI devices using the kernel device driver.

You can have the device drivers loaded at system startup as described in the Loading Device Drivers Topic.

You can also load the device drivers dynamically with:

    modprobe spi_bcm2708
    modprobe spidev

The advantage of loading at startup is that you can configure the driver to allow none root users to access the device files. These configuration settings would also apply when using modprobe but your process would need root permissions to run modprobe anyway.

### Allowing None Root Device Permissions

The device manager on Linux is called udev and we can provide rules for udev that control the permissions on the device files that it creates.

The kernel drivers in Raspbian provide access to the SPI0 peripheral by default. The Raspberry Pi GPIO provides 2 SPI cable select pins and the device driver works by providing a different file for each cable select. You therefore get two device files created for the SPI0 peripheral:

    /dev/spidev0.0
    /dev/spidev0.1

With default settings these files will be owned by user root and group root. Only the user will have read/write permissions.

We can change this with a udev rule. We create a file in /etc/udev/rules.d that contains the line:

    KERNEL=="spidev*", SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"

The KERNEL and SUBSYSTEM settings define 'matches' that udev should check to determine which device driver the rule applies to while the GROUP and MODE settings provide the required group and permissions for the created device files.

With this rule users that are members of the spi group will be able to read and write from the SPI devices.

The HiPi Control GUI application will set or remove this rule for you and allow you to manage the spi group and device driver options.

### Setting Driver Options

The SPI driver accepts a single option - bufsiz - which defines the buffer size for SPI communication. It is unlikely that you would need to change the default of 4096.

If you do need to change it you could specify it as an option to modprobe:

    modprobe spidev bufsiz=8192

To apply a setting as a default to be used every time the spidev driver is loaded we need to create a file in /etc/modprobe.d/ that contains the line:

    options spidev bufsiz=8192

The HiPi Control GUI application will set or remove this option setting for you and allow you to manage the spi group and device driver udev rules.

HiPi Modules Copyright © 2013 - 2016 Mark Dootson