Main Site Contents Up Previous Next

HiPi::Device::SPI

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

See : SPI Device Driver

Loading and configuring the kernel SPI driver is controlled by configuring the device tree in your /boot/config.txt file.

See : Using the Device Tree

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

HiPi::Interface::MCP23S17

Class Methods

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

my $result = $dev->transfer( $buffer );
    Writes $buffer to the connected SPI device and returns
    the length($buffer) bytes read. In SPI transfers the length
    of data returned is always the same as the length of data
    sent.

    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.

    For example, your SPI device expects a transfer message of 4 
    bytes in length.
    Arrange these 4 bytes on an array : @buffer.

    my @vals = unpack('C4',$spi->transfer(pack('C4',($addr,$reg,@buffer))));

    You will have to check the data sheets for your individual
    SPI device.
    
    For example, the MCP23S17 requires the first 2 bytes of a nessage
    to be a device address and a register address. It allows requests 
    to retrieve any number of bytes.

    If I want to retrieve 10 bytes of information, I have to send 12 
    bytes - 2 bytes of device and register address plus a 10 byte buffer.
    
    my @buffer = (0) x 10;
    my @vals = unpack('C12',$spi->transfer(pack('C12',($addr,$reg,@buffer))));
    
    The device doesn't populate $vals[0] or $vals[1] so the 10 bytes I 
    read start at $vals[2];

    The MCP3008 analogue to digital converter uses 3 byte SPI transfers. 
    In the request bytes the first byte and 4 high bits of the second byte 
    define the request. The ADC returns its 10 bit result as the low 2 bits 
    of the second byte + the third byte. In the request, we just send 0 as
    the third byte. Then we bitshift the value in the second byte.
  
    my @vals = unpack('C3',$spi->transfer(pack('C3',($byt1,$byt2,0))));
    my $result = (($vals[1] & 3) << 8) + $vals[2];
    
    Use the code for HiPi::Interface::MCP3008 and HiPi::Interface::MCP23S17 
    as guides.
$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. 




Contents Up Previous Next


HiPi Modules Copyright © 2013 - 2016 Mark Dootson