SPI

Class: SPI

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

  • On Tizen, the bus number is defined in this documentation.
  • 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.

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).

  • 0 Clock Polarity(0), Clock Phase(0), Clock Edge(1)
  • 1 Clock Polarity(0), Clock Phase(1), Clock Edge(0)
  • 2 Clock Polarity(1), Clock Phase(0), Clock Edge(1)
  • 3 Clock Polarity(1), Clock Phase(1), Clock Edge(0)

SPI.CHIPSELECT

  • NONE
  • HIGH

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

  • MSB The most significant bit.
  • LSB The least significant bit.

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)

  • configuration {Object}
    • device {string} The specified path for spidev. (only on Linux)
    • bus {number} The specified bus number. (Tizen, TizenRT and NuttX only)
    • mode {SPI.MODE} The combinations of the polarity and phase. Default: SPI.MODE[0].
    • chipSelect {SPI.CHIPSELECT} Chip select state. Default: SPI.CHIPSELECT.NONE.
    • maxSpeed {number} Maximum transfer speed. Default: 500000.
    • bitsPerWord {number} Bits per word to send (should be 8 or 9). Default: 8.
    • bitOrder {SPI.BITORDER} Order of the bits shifted out of and into the SPI bus. Default: SPI.BITORDER.MSB.
    • loopback {boolean} Using loopback. Default: false.
  • callback {Function}.
    • err {Error|null}.
    • spibus {Object} An instance of SPIBus.
  • Returns: {Object} An instance of SPIBus.

Opens an SPI device with the specified configuration.

Example

  1. var spi = require('spi');
  2. var spi0 = spi.open({
  3. device: '/dev/spidev0.0'
  4. }, function(err) {
  5. if (err) {
  6. throw err;
  7. }
  8. });

spi.openSync(configuration)

  • configuration {Object}
    • device {string} The specified path for spidev. (only on Linux)
    • bus {number} The specified bus number. (Tizen, TizenRT and NuttX only)
    • mode {SPI.MODE} The combinations of the polarity and phase. Default: SPI.MODE[0].
    • chipSelect {SPI.CHIPSELECT} Chip select state. Default: SPI.CHIPSELECT.NONE.
    • maxSpeed {number} Maximum transfer speed. Default: 500000.
    • bitsPerWord {number} Bits per word to send (should be 8 or 9). Default: 8.
    • bitOrder {SPI.BITORDER} Order of the bits shifted out of and into the SPI bus. Default: SPI.BITORDER.MSB.
    • loopback {boolean} Using loopback. Default: false.
  • Returns: {Object} An instance of SPIBus.

Opens an SPI device with the specified configuration.

Example

  1. var spi = require('spi');
  2. var spi0 = spi.openSync({
  3. device: '/dev/spidev0.0'
  4. });

Class: SPIBus

The SPIBus is commonly used for communication.

spibus.transfer(txBuffer, [, callback])

  • txBuffer {Array|Buffer}.
  • callback {Function}.
    • err {Error|null}.
    • rxBuffer {Array}.

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

Example

  1. var tx = new Buffer('Hello IoTjs');
  2. var rx = new Buffer(tx.length);
  3. spi0.transfer(tx, function(err, rx) {
  4. if (err) {
  5. throw err;
  6. }
  7. var value = '';
  8. for (var i = 0; i < tx.length; i++) {
  9. value += String.fromCharCode(rx[i]);
  10. }
  11. console.log(value);
  12. });

spibus.transferSync(txBuffer)

  • txBuffer {Array|Buffer}.
  • Returns: rxBuffer {Array}.

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

Example

  1. var tx = new Buffer('Hello IoTjs');
  2. var rx = spi0.transferSync(tx);
  3. var value = '';
  4. for (var i = 0; i < tx.length; i++) {
  5. value += String.fromCharCode(rx[i]);
  6. }
  7. console.log(value);

spibus.close([callback])

  • callback {Function}.
    • err {Error|null}.

Closes the SPI device asynchronously.

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

Example

  1. spi0.close(function(err) {
  2. if (err) {
  3. throw err;
  4. }
  5. console.log('spi bus is closed');
  6. });

spibus.closeSync()

Closes the SPI device synchronously.

Example

  1. spi.closeSync();
  2. console.log('spi bus is closed');