rpi-softspi

    1.0.5 • Public • Published

    SoftSPI

    Codacy Badge Coverage Status npm npm npm

    This library contains a javascript class, which implements the SPI (Serial Peripheral Interface Bus) for a Raspberry Pi. In contrast to other libraries it is a software implementation. So it does not use the hardware SPI of the Raspberry Pi and you can choose any pin of the GPIO for the needed logic signals of SPI.

    Motivation

    I wanted to control some components with my Raspberry Pi, which are using SPI. But a couple of clients (e.g. WS2801 led stripe) do not support a client select. So I needed a software SPI and didn't find one for javascript. Now here is.

    Precondition

    The library is implemented in ECMAScript 2015, so your project should support at least this version.

    Installation

    Install it via npm:

    $ npm install rpi-softspi

    Usage

    Start with importing the module via:

    var SoftSPI = require("rpi-softspi")

    Create an instance for your client device specifying the used pins and needed configurations for the communication in an options object:

    let client = new SoftSPI({
       clock: 15,     // pin number of SCLK
       mosi: 11,      // pin number of MOSI
       miso: 13,      // pin number of MISO
       client: 16,    // pin number of CS
       clientSelect: rpio.LOW, // trigger signal for the client
       mode: 0,                // clock mode (0 - 3)
       bitOrder: SoftSPI.MSB   // bit order in communication
    })

    At least you have to specify the clock pin, all other can be left unspecified to use the default configuration

    This is the default configuration:

    default = {
       clock: null,
       miso: null,
       mosi: null,
       client: null,
       clientSelect: rpio.LOW,
       mode: 0,
       bitOrder: SoftSPI.MSB
    }

    Clock modes are:

    Mode Polarity Phase
    0 0 0
    1 0 1
    2 1 0
    3 1 1

    Start the communication:

    client.open()

    The SoftSPI is in an invalid state till you open the communication. If it is invalid you cannot transfer data.

    Close the communication:

    client.close()

    Once you close the communication the SoftSPI changes again to invalid state.

    Read (only) a number of bytes from the client:

    let bytes = client.read(5)
    // bytes[0] = first byte

    Write data (only) to the client:

    client.write([0xff, 0x01, 0xab]) //supply any data

    Transfer data to and from the client:

    let bytes = client.transfer([0xff, 0x01, 0xab])

    The number of returned bytes is the same number of bytes you have supplied

    Example

    We assume the connected client wants the numbers 1234 and continues to count by returning 5678.

    var SoftSPI = require("rpi-softspi")
     
    let client = new SoftSPI({
       clock: 15,     // pin number of SCLK
       mosi: 11,      // pin number of MOSI
       miso: 13,      // pin number of MISO
       client: 16,    // pin number of CS
    })
     
    client.open()
    let send = [1, 2, 3, 4]
    let answer = client.transfer(send)
    // Count: 1,2,3,4...5,6,7,8
    console.log("Count: " + send.toString() + "..." + answer.toString())
    client.close()

    API

    Check out the documentation for details.

    License

    MIT

    Additional Information

    Find a description on Wikipedia - Serial Peripheral Interface Bus or with the keywords SPI as well as Serial Peripheral Interface.

    Install

    npm i rpi-softspi

    DownloadsWeekly Downloads

    33

    Version

    1.0.5

    License

    MIT

    Last publish

    Collaborators

    • vertumnus