Class Spi


  • public class Spi
    extends Object

    WiringPi includes a library which can make it easier to use the Raspberry Pi’s on-board SPI interface.

    Before you can use SPI interface, you may need to use the gpio utility to load the SPI drivers into the kernel:

    $ gpio load spi

    If you need a buffer size of greater than 4KB, then you can specify the size (in KB) on the command line:

    $ gpio load spi 100

    will allocate a 100KB buffer. (You should rarely need this though, the default is more than enough for most applications).

    This library depends on the wiringPi native system library. (developed by Gordon Henderson @ http://wiringpi.com)

    Author:
    Robert Savage (http://www.savagehomeautomation.com)
    See Also:
    https://www.pi4j.com/, http://wiringpi.com/reference/spi-library
    • Field Detail

      • CHANNEL_0

        public static int CHANNEL_0
      • CHANNEL_1

        public static int CHANNEL_1
      • MODE_0

        public static int MODE_0
      • MODE_1

        public static int MODE_1
      • MODE_2

        public static int MODE_2
      • MODE_3

        public static int MODE_3
    • Method Detail

      • wiringPiSPISetup

        public static int wiringPiSPISetup​(int channel,
                                           int speed)

        wiringPiSPISetup:

        This is the way to initialise a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer in the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz.

        The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you may use the standard errno global variable to see why.

        Parameters:
        channel - SPI channel
        speed - SPI speed
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPISetupMode

        public static int wiringPiSPISetupMode​(int channel,
                                               int speed,
                                               int mode)

        wiringPiSPISetupMode:

        This is the way to initialise a channel (The Pi has 2 channels; 0 and 1). The speed parameter is an integer in the range 500,000 through 32,000,000 and represents the SPI clock speed in Hz.

        The returned value is the Linux file-descriptor for the device, or -1 on error. If an error has happened, you may use the standard errno global variable to see why.

        Parameters:
        channel - SPI channel
        speed - SPI speed
        mode - SPI mode (Mode is 0, 1, 2 or 3; see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers)
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library, http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers
      • wiringPiSPIGetFd

        public static int wiringPiSPIGetFd​(int channel)

        wiringPiSPIGetFd:

        Return the file-descriptor for the given channel

        Parameters:
        channel - SPI channel
        Returns:
        file-descriptor for the given channel
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            String data)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

        (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)

        Parameters:
        channel - SPI channel

        data - string data payload
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            String data,
                                            int len)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

        (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)

        Parameters:
        channel - SPI channel

        data - string data payload
        len - length of characters in string (must be total string length, not a substring)
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            byte[] data,
                                            int len)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

        (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)

        Parameters:
        channel - SPI channel
        data - byte array data payload
        len - length of bytes in data array argument
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            byte[] data)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. Data that was in your buffer is overwritten by data returned from the SPI bus.

        (ATTENTION: the 'data' argument can only be a maximum of 1024 characters.)

        Parameters:
        channel - SPI channel
        data - byte array data payload
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            short[] data,
                                            int len)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. The data argument is passed into the wiringPI function as the argument and the output from Spi is returned by this method

        Parameters:
        channel - SPI channel
        data - short array data payload. Note that wiringPi uses unsigned char for the data transmission. That is 8-bit. in other words values 0-255. So make sure the values in data do not exceed this range, otherwise the numbers would overflow in the native code and unexpected results would yield
        len - length of bytes in data array argument
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library
      • wiringPiSPIDataRW

        public static int wiringPiSPIDataRW​(int channel,
                                            short[] data)

        wiringPiSPIDataRW:

        This performs a simultaneous write/read transaction over the selected SPI bus. The data argument is passed into the wiringPI function as the argument and the output from Spi is returned by this method

        Parameters:
        channel - SPI channel
        data - short array data payload. Note that wiringPi uses unsigned char for the data transmission. That is 8-bit. in other words values 0-255. So make sure the values in data do not exceed this range, otherwise the numbers would overflow in the native code and unexpected results would yield
        Returns:
        return -1 on error
        See Also:
        http://wiringpi.com/reference/spi-library