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)
Modifier and Type | Field and Description |
---|---|
static int |
CHANNEL_0 |
static int |
CHANNEL_1 |
static int |
MODE_0 |
static int |
MODE_1 |
static int |
MODE_2 |
static int |
MODE_3 |
Modifier and Type | Method and Description |
---|---|
static int |
wiringPiSPIDataRW(int channel,
byte[] data)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIDataRW(int channel,
byte[] data,
int len)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIDataRW(int channel,
short[] data)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIDataRW(int channel,
short[] data,
int len)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIDataRW(int channel,
String data)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIDataRW(int channel,
String data,
int len)
wiringPiSPIDataRW:
|
static int |
wiringPiSPIGetFd(int channel)
wiringPiSPIGetFd:
|
static int |
wiringPiSPISetup(int channel,
int speed)
wiringPiSPISetup:
|
static int |
wiringPiSPISetupMode(int channel,
int speed,
int mode)
wiringPiSPISetupMode:
|
public static int CHANNEL_0
public static int CHANNEL_1
public static int MODE_0
public static int MODE_1
public static int MODE_2
public static int MODE_3
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.
channel
- SPI channelspeed
- SPI speedpublic 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.
channel
- SPI channelspeed
- SPI speedmode
- SPI mode (Mode is 0, 1, 2 or 3; see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_numbers)public static int wiringPiSPIGetFd(int channel)
wiringPiSPIGetFd:
Return the file-descriptor for the given channel
channel
- SPI channelpublic 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.)
channel
- SPI channeldata
- string data payloadpublic 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.)
channel
- SPI channeldata
- string data payloadlen
- length of characters in string (must be total string length, not a substring)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.)
channel
- SPI channeldata
- byte array data payloadlen
- length of bytes in data array argumentpublic 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.)
channel
- SPI channeldata
- byte array data payloadpublic 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
channel
- SPI channeldata
- 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 yieldlen
- length of bytes in data array argumentpublic 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
channel
- SPI channeldata
- 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 yieldCopyright © 2012–2019 Pi4J. All rights reserved.