Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

spi-device

fivdi924MIT3.1.2TypeScript support: included

SPI serial bus access with Node.js

spi, iot, raspberry, raspi, rpi, pi, beaglebone, linux

readme

Build Status npm Version Downloads Per Month Mentioned in Awesome Node.js

spi-device

SPI serial bus access with Node.js on Linux boards like the Raspberry Pi or BeagleBone. All methods have asynchronous and synchronous forms.

spi-device supports Node.js versions 10, 12, 14, 15 and 16.

Contents

Installation

npm install spi-device

Usage

Determine the temperature using a TMP36 analog temperature sensor wired to channel 5 on an MCP3008 SPI A/D converter.

const spi = require('spi-device');

// The MCP3008 is on bus 0 and it's device 0
const mcp3008 = spi.open(0, 0, err => {
  // An SPI message is an array of one or more read+write transfers
  const message = [{
    sendBuffer: Buffer.from([0x01, 0xd0, 0x00]), // Sent to read channel 5
    receiveBuffer: Buffer.alloc(3),              // Raw data read from channel 5
    byteLength: 3,
    speedHz: 20000 // Use a low bus speed to get a good reading from the TMP36
  }];

  if (err) throw err;

  mcp3008.transfer(message, (err, message) => {
    if (err) throw err;

    // Convert raw value from sensor to celcius and log to console
    const rawValue = ((message[0].receiveBuffer[1] & 0x03) << 8) +
      message[0].receiveBuffer[2];
    const voltage = rawValue * 3.3 / 1023;
    const celcius = (voltage - 0.5) * 100;

    console.log(celcius);
  });
});

spi-device enables low-level access to SPI devices. Often, high-level access is required. When this is the case, high-level packages can be built using spi-device. An example of such a package is mcp-spi-adc which provides a high-level API for accessing an MCP3008 SPI A/D converter and will generally be more useful than the low-level demonstration code shown above.

API Documentation

All methods have asynchronous and synchronous forms.

The asynchronous form always take a completion callback as its last argument. The arguments passed to the completion callback depend on the method, but the first argument is always reserved for an exception. If the operation was completed successfully, then the first argument will be null or undefined.

When using the synchronous form any exceptions are immediately thrown. You can use try/catch to handle exceptions or allow them to bubble up.

Functions

Class SpiDevice

Constants

open(busNumber, deviceNumber[, options], cb)

  • busNumber - the number of the SPI bus to open, 0 for /dev/spidev0.n, 1 for /dev/spidev1.n, ...
  • deviceNumber - the number of the SPI device to open, 0 for /dev/spidevn.0, 1 for /dev/spidevn.1, ...
  • options - an optional object specifying device configuration options
  • cb - completion callback

Asynchronous open. Returns a new SpiDevice object. The completion callback gets one argument (err). The SpiDevice object returned should not be used before the completion callback is called.

openSync(busNumber, deviceNumber[, options])

  • busNumber - the number of the SPI bus to open, 0 for /dev/spidev0.n, 1 for /dev/spidev1.n, ...
  • deviceNumber - the number of the SPI device to open, 0 for /dev/spidevn.0, 1 for /dev/spidevn.1, ...
  • options - an optional object specifying device configuration options

Synchronous open. Returns a new SpiDevice object.

device.transfer(message, cb)

  • message - an array of one or more read+write transfers
  • cb - completion callback

Asynchronous message transfer. An SPI message is an array of one or more read+write transfers. The completion callback gets two arguments (err, message). Returns this.

device.transferSync(message)

  • message - an array of one or more read+write transfers

Synchronous message transfer. An SPI message is an array of one or more read+write transfers. Returns this.

device.getOptions(cb)

  • cb - completion callback

Asynchronously read device configuration options. The completion callback gets two arguments (err, options) where options is an object describing the device configuration options. Returns this.

device.getOptionsSync()

Synchronously read device configuration options. Returns an object describing the device configuration options.

device.setOptions(options, cb)

Asynchronously write device configuration options. The completion callback gets one argument (err). Returns this.

device.setOptionsSync(options)

Synchronously write device configuration options. Returns this.

device.close(cb)

  • cb - completion callback

Asynchronous close. Frees system resources used by this instance. The completion callback gets one argument (err). Returns null.

device.closeSync()

Synchronous close. Frees system resources used by this instance. Returns null.

MODE0

SPI mode number 0.

MODE1

SPI mode number 1.

MODE2

SPI mode number 2.

MODE3

SPI mode number 3.

Message

An SPI message is an array of one or more read+write transfers. A transfer is an object with the properties listed below. Most of the properties are optional. Note that although both sendBuffer and receiveBuffer are optional, at least one one of them must be specified.

  • byteLength - number, 32-bit, the number of bytes to transfer
  • sendBuffer - optional Buffer, transmit data
  • receiveBuffer - optional Buffer, receive data
  • speedHz - optional number, 32-bit, override of the device's clock frequency in Hertz
  • microSecondDelay - optional number, 16-bit, delay after the last bit transfer before optionally deselecting the device before the next transfer, default 0
  • bitsPerWord - optional number, 8-bit, override of the device's wordsize
  • chipSelectChange - optional boolean, true to deselect device before starting the next transfer, default false

Configuration Options

Device configuration options can be optionally specified when a device is opened with the open or openSync methods. They can also be specified at a later point with the setOptions or setOptionsSync methods. When calling these methods, only the options that need to be set need to be specified in the options object passed to those methods. All options are optional and the appropriate defaults will be used for options that are not specified.

The options supported varies from system to system and will depend on the device drivers used on those systems.

Configurations options can be read with the getOptions and getOptionsSync methods.

IMPORTANT The semantics of chipSelectHigh have changed with Linux kernel 5. To the best of my knowledge, the chipSelectHigh option no longer serves any purpose when used from user space with Linux kernel 5 and should not be used. With Linux kernel 5, the chip select is assumed to be active low. With Linux kernel 5, if an SPI device has has active high chip select, it's chip select must be controlled manually with a GPIO using a module such as onoff. The chipSelectHigh option has been crossed out below but it's still available for usage on older kernels.

  • mode - number, 2-bit, MODE0, MODE1, MODE2, or MODE3, default MODE0
  • chipSelectHigh - boolean, true for active high chip select, default false
  • lsbFirst - boolean, true for least significant bit first transfer, default false
  • threeWire - boolean, true for shared MISO/MOSI signals, default false
  • loopback - boolean, true for loopback mode, default false
  • noChipSelect - boolean, true for 1 device per bus, no chip select, default false
  • ready - boolean, true if device pulls low to pause, default false
  • bitsPerWord - number, 8-bit, device word size, default 8
  • maxSpeedHz - number, 32-bit, device clock frequency in Hertz, default system specific

changelog

3.1.2 / Jun 23 2021

  • fix compile error occuring with gcc 10 (fixes #21)

3.1.1 / Apr 30 2021

  • drop support for node.js 8 and 13, add support for node.js 15 and 16
  • update dependencies
  • fix chipSelectHigh documentation and tests (fixes #16 and #18)

3.1.0 / Apr 25 2020

  • update dependencies (nan v2.14.1, jshint v2.11.0, @types/node v13.13.2)
  • drop support for node.js 6, add support for node.js 14

3.0.1 / Apr 08 2020

  • fix TypeScript type definition of SpiMessage

3.0.0 / Sep 22 2019

  • drop support for node.js v4

2.0.9 / Sep 07 2019

  • add type definitions for TypeScript (thank you @huming2207)
  • suppress cast-function-type warnings

2.0.8 / Jun 16 2019

  • remove node 11 from build
  • update dependencies
  • update npm keywords

2.0.7 / Mar 15 2019

  • update dependencies (nan v2.13.1, bindings v1.5.0)
  • document node 12 support
  • lint with jshint
  • only compile c++ code on linux

2.0.6 / Dec 19 2018

  • fix deprecation warnings on node.js v12 nightly
  • update dependencies (nan v2.12.1, bindings 1.3.1)

2.0.5 / Oct 13 2018

2.0.4 / Sep 29 2018

  • update dependencies (nan v2.11.1)
  • adapt to V8 7.0: replace v8Value->Uint32Value() with Nan::To<uint32_t>(v8Value).FromJust()
  • adapt to V8 7.0: replace v8Value->BooleanValue() with Nan::To<bool>(v8Value).FromJust()

2.0.3 / Jul 28 2018

  • code style

2.0.2 / Apr 14 2018

  • code style

2.0.1 / Apr 07 2018

  • update dependencies (nan v2.10.0)

2.0.0 / Feb 25 2018

  • update dependencies (nan v2.9.2)
  • drop support for node.js v0.10, v0.12, v5 and v7

1.0.2 / Dec 28 2017

  • don't suppress deprecated-declaration warnings
  • update dependencies (nan v2.8.0)

1.0.1 / Nov 04 2017

  • fix typos in history
  • suppress deprecated-declaration warnings
  • document node 9 support

1.0.0 / Oct 14 2017

  • update dependencies (bindings v1.3.0, nan v2.7.0)
  • document default values for microSecondDelay and chipSelectChange
  • document supported node versions

0.2.6 / Jan 09 2017

  • documentation

0.2.5 / Dec 28 2016

  • use maybe instances to fix compiler warnings
  • upgrade to nan v2.5.0

0.2.4 / Oct 02 2016

  • upgraded to nan v2.4.0

0.2.3 / May 31 2016

  • documentation

0.2.2 / May 30 2016

  • set default configuration options when open is called

0.2.1 / May 28 2016

  • pi-mcp3008-tmp36 Fritzing
  • mask out unused bits in raw value from mcp3008

0.2.0 / May 28 2016

  • transfer option speed renamed to speedHz

0.1.1 / May 26 2016

  • critical section for option modification
  • tests improved
  • check to see if requested transfer takes place

0.1.0 / May 25 2016

  • documentation
  • device configuration options
  • transfer options

0.0.0 / May 16 2016

  • initial release