## HiPi::Device::SPI

The HiPi::Device::SPI module provides access to the kernel driver for the SPI bus.

See : SPI Device Driver

The settings for the SPI device can be controlled in the HiPi Control GUI.

The following interface modules use HiPi::Device::SPI as a backend and may contain code that helps with your own usage.

HiPi::Interface::MCP3004

HiPi::Interface::MCP3008
HiPi::Interface::MCP49XX

## Class Methods

##### HiPi::Device::SPI->load_modules( $forceunload );

        Loads the kernel device driver modules for SPI
        spi_bcm2708
        spidev

        When $forceunload is true, unloads the kernel modules
        first if they are loaded (effectively forces a reload)

        spidev is loaded with the current bufsiz

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::SPI->unload_modules();

        Unloads the kernel device driver modules for SPI
        spi_bcm2708
        spidev

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::SPI->get_bufsiz();

        Returns the current bufsiz of a loaded spidev
        module. If the module is not loaded or the process
        does not have root permissions, returns the current
        default module setting for bufsiz.

##### HiPi::Device::SPI->set_bufsiz( $newsiz );

        Sets the bufsiz option of the spidev module. Must
        unload and load the kernel module to do this.

        Of course, the user the script is running as must have
        root permissions to load and unload kernel modules.

##### HiPi::Device::SPI->get_device_list();

        Returns an array containing the names of available
        devices.
        Will normally return ('/dev/spidev0.0', '/dev/spidev0.1')

## Object Constructor and Methods

##### my $dev = HiPi::Device::SPI->new();

        Returns a new instance of the HiPi::Device::SPI class.

        You can optionally specify several parameters as key
        value pairs in the constructor. Their default values
        are:

        my $dev = HiPi::Device::SPI->new(
            devicename   => '/dev/spidev0.0',
            speed        => SPI_SPEED_MHZ_1,
            bitsperword  => 8,
            delay        => 0,
        );

        In default setup mode the SPI driver for Raspberry Pi has two
        cable select pins assigned.

        For controlling a device connected to SPI0_CS0 you need
        devicename => '/dev/spidev0.0'

        For controlling a device connected to SPI0_CS1 you need
        devicename => '/dev/spidev0.1'

        You can control data transfer speed by specifying one of the
        following constants for the key value 'speed'

            SPI_SPEED_KHZ_500
            SPI_SPEED_MHZ_1
            SPI_SPEED_MHZ_2
            SPI_SPEED_MHZ_4
            SPI_SPEED_MHZ_8
            SPI_SPEED_MHZ_16
            SPI_SPEED_MHZ_32

        You can import these constants into your namespace with:

        use HiPi::Device::SPI qw( :spi );

        Consult SPI bus documentation if you find your devices need
        or would benefit from specifying none default values for
        'bitsperword' and 'delay'. (I haven't discovered a use for
        these)

### SPI Methods

##### $dev->transfer( $buffer );

        Writes $buffer to the connected SPI device and returns
        the length($buffer) bytes read.

        Uses the speed, bitsperword and delay settings specified in
        the constructor.

        Depending on your device you will probably have to pack /
        unpack the $buffer and return value and engage in some
        bit shifting to retrieve the returned value.

        Use the code for HiPi::Interface::MCP3008 as a simple guide.

##### $dev->set_bus_mode( $mode );

        $mode   one of

            SPI_MODE_0
            SPI_MODE_1
            SPI_MODE_2
            SPI_MODE_3

        You can import these constants into your namespace with:

        use HiPi::Device::SPI qw( :spi );

        The documentation for your SPI device should tell you which
        modes it supports. Default mode is SPI_MODE_0.

##### $dev->set_bus_maxspeed( $speed );

        $speed   one of

            SPI_SPEED_KHZ_500
            SPI_SPEED_MHZ_1
            SPI_SPEED_MHZ_2
            SPI_SPEED_MHZ_4
            SPI_SPEED_MHZ_8
            SPI_SPEED_MHZ_16
            SPI_SPEED_MHZ_32

        You can import these constants into your namespace with:

        use HiPi::Device::SPI qw( :spi );

        I have not yet investigated how this setting combines with
        the speed setting specified in the constructor. The commands
        are simply wrappers for the exposed kernel device driver
        calls.

HiPi Modules Copyright © 2013 Mark Dootson