The spi
module supports communication with devices using SPI (Serial Peripheral Interface) bus as the master device. Use require('spi')
to access this module. A chip select pin of slave device need to be controlled by GPIO output pin.
An instances of SPI
represents a SPI bus.
<number>
= 0
'SPI MODE0: CPOL = 0, CPHA = 0
<number>
= 1
'SPI MODE0: CPOL = 0, CPHA = 1
<number>
= 2
'SPI MODE0: CPOL = 1, CPHA = 0
<number>
= 3
'SPI MODE0: CPOL = 1, CPHA = 1
<number>
= 0
SPI bitorder, MSB is the first bit.
<number>
= 1
SPI bitorder, LSB is the first bit.
bus
<number>
SPI bus number.
options
<object>
The object of SPI options. Default values are depends on board (Check in a board page).
mode
<number>
SPI.MODE_0
(CPOL=0/CPHA=0), SPI.MODE_1
(CPOL=0/CPHA=1), SPI.MODE_2
(CPOL=1/CPHA=0), or SPI.MODE_3
(CPOL=1/CPHA=1). Default: SPI.MODE_0
.
baudrate
<number>
Baud rate. Default: 3000000
, 3 Mbit/s
bitorder
<number>
SPI.MSB (0)
or SPI.LSB (1)
Default: SPI.MSB (0)
.
sck
<number>
SPI SCK pin number.
mosi
<number>
SPI MOSI (TX) pin number.
miso
<number>
SPI MISO (RX) pin number.
Returns: <object>
An initialized SPI instance corresponds to the bus number. Once initialized, the same object will be returned.
Instances of the SPI
class can be created using the new keyword or by calling spi.SPI() as a function. A RangeError
will be thrown if bus
is not less than max bus number. Please see here for more about SPI modes.
// Create the SPI instance with master modevar GPIO = require('gpio').GPIO;var SPI = require('spi').SPI;var spi0cs = new GPIO(9, OUTPUT); // Set chip select to pin 9.var spiOptions = { // SPI optionsmode: SPI.MODE_0,baudrate: 800000,bitorder: SPI.MSB,};var spi0 = new SPI(0, spiOptions);// ChipSelect LOWspi0cs.write(LOW);// transfer data...spi0.close(); // Close SPI bus 0
data
<Uint8Array|string>
Data to write.
timeout
<number>
Timeout in milliseconds. Default: 5000
.
Returns: <Uint8Array>
Received data or null
if failed to transfer or timeout.
Send and receive data simultaneously via SPI bus.
var GPIO = require('gpio').GPIO;var SPI = require('spi').SPI;var spi0cs = new GPIO(9, OUTPUT); // Set chip select to pin 9.var spiOptions = { // SPI optionsmode: SPI.MODE_0,baudrate: 800000,bitorder: SPI.MSB,};var spi0 = new SPI(0, spiOptions);// ChipSelect LOWspi0cs.write(LOW);// Send two bytes and receive two bytesvar buf = spi0.transfer(new Uint8Array([0x88, 0x24])); // send and receive two byte data.if (buf) {console.log(data.length); // == 2console.log(data[0]); // first byteconsole.log(data[1]); // second byte}
data
<Uint8Array|string>
Data to write.
timeout
<number>
Timeout in milliseconds. Default: 5000
.
count
<number>
Indicates how many times to send data. Default: 1
.
Returns: <number>
The number of bytes written, -1
if it failed to write or timeout.
Send data via SPI bus
var GPIO = require('gpio').GPIO;var SPI = require('spi').SPI;var spi0cs = new GPIO(9, OUTPUT); // Set chip select to pin 9.var spiOptions = { // SPI optionsmode: SPI.MODE_0,baudrate: 800000,bitorder: SPI.MSB,};var spi0 = new SPI(0, spiOptions);// ChipSelect LOWspi0cs.write(LOW);// Send 2 bytes with an array of numbersvar array = new Uint8Array([0x6b, 0x00]);spi0.send(array);
length
<number>
Data length to read.
timeout
<number>
Timeout in milliseconds. Default: 5000
.
Returns: <Uint8Array>
Received data or null
if failed to transfer or timeout.
Receive data via SPI bus.
var GPIO = require('gpio').GPIO;var SPI = require('spi').SPI;var spi0cs = new GPIO(9, OUTPUT); // Set chip select to pin 9.var spiOptions = { // SPI optionsmode: SPI.MODE_0,baudrate: 800000,bitorder: SPI.MSB,};var spi0 = new SPI(0, spiOptions);// ChipSelect LOWspi0cs.write(LOW);// Receive 10 bytesvar buf = spi0.recv(10);if (buf) {console.log(data.length); // == 10console.log(data[0]); // first byteconsole.log(data[1]); // second byte// ...}spi0.close();
Closes the SPI bus.
var GPIO = require('gpio').GPIO;var SPI = require('spi').SPI;var spi0cs = new GPIO(9, OUTPUT); // Set chip select to pin 9.var spiOptions = { // SPI optionsmode: SPI.MODE_0,baudrate: 800000,bitorder: SPI.MSB,};var spi0 = new SPI(0, spiOptions);// transfer data...spi0.close(); // Close SPI bus 0