ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

The following shows spi module APIs available for each platform.

  Linux
(Ubuntu)
Raspbian
(Raspberry Pi)
NuttX
(STM32F4-Discovery)
TizenRT
(Artik053)
spi.open O O O O
spibus.transfer O O O O
spibus.transferSync O O O O
spibus.close O O O O
spibus.closeSync O O O O

Class: SPI

SPI (Serial Peripheral Interface) is a communication protocol which defines a way to communicate between devices.

On NuttX, you have to know the number of pins that is defined on the target board module. For more information, please see the list below.

new SPI()

Returns a new SPI object which can open SPI bus.

SPI.MODE

The clock polarity and the clock phase can be specified as 0 or 1 to form four unique modes to provide flexibility in communication between devices. The SPI.MODE will specify which one to use (the combinations of the polarity and phase).

SPI.CHIPSELECT

The chip select is an access-enable switch. When the chip select pin is in the HIGH state, the device responds to changes on its input pins.

SPI.BITORDER

Sets the order of the bits shifted out of and into the SPI bus, either MSB (most-significant bit first) or LSB (least-significant bit first).

spi.open(configuration[, callback])

Opens an SPI device with the specified configuration.

Example


var Spi = require('spi');
var spi = new Spi();
var spi0 = spi.open({
  device: '/dev/spidev0.0'
  }, function(err) {
    if (err) {
      throw err;
    }
});

Class: SPIBus

The SPIBus is commonly used for communication.

spibus.transfer(txBuffer, rxBuffer[, callback])

Writes and reads data from the SPI device asynchronously. The txBuffer and rxBuffer must have equal length.

Example


var tx = new Buffer('Hello IoTjs');
var rx = new Buffer(tx.length);
spi0.transfer(tx, rx, function(err) {
  if (err) {
    throw err;
  }

  var value = '';
  for (var i = 0; i < tx.length; i++) {
    value += String.fromCharCode(rx[i]);
  }
  console.log(value);
});

spibus.transferSync(txBuffer, rxBuffer)

Writes and reads data from the SPI device synchronously. The txBuffer and rxBuffer must have equal length.

Example


var tx = new Buffer('Hello IoTjs');
var rx = new Buffer(tx.length);
spi0.transferSync(tx, rx);
var value = '';
for (var i = 0; i < tx.length; i++) {
  value += String.fromCharCode(rx[i]);
}
console.log(value);

spibus.close([callback])

Closes the SPI device asynchronously.

The callback function will be called after the SPI device is closed.

Example


spi0.close(function(err) {
  if (err) {
    throw err;
  }
  console.log('spi bus is closed');
});

spibus.closeSync()

Closes the SPI device synchronously.

Example


spi.closeSync();

console.log('spi bus is closed');