Skip to content

SPI

The SPI protocol (Serial peripheral interface) uses a 3-wire connection, a clock and the data-in and data-out signal. Other than this, it is similar to I²C, meaning, it also uses an address and reads and writes data serialized over this 2 data wires.

import { SPI } from 'miletus';

...

let spi = new SPI();
await spi.open('spi0');

Methods

new SPI()

Create a new SPI connection object.

open(port[, frequency])

  • port string - SPI port to connect to. Supported values are: spi0 and spi1.
  • frequency number (optional) - The frequency in Hz. Default value is 1000000.

Open the SPI connection.

Returns Promise - Resolves when the connection is opened.

close()

Close the SPI connection.

transfer(command)

  • command number - A byte command.

Writes command.

Returns Promise<boolean> - Resolves with the success of the command write.

readTransfer(buffer, wSize, rSize)

  • buffer ArrayBuffer | Uint8Array | number[] - The buffer to write.
  • wSize number - Write buffer size.
  • rSize number - Read buffer size.

Read a buffer of data.

Returns Promise<ArrayBuffer> - Resolves with the buffer read.

writeTransfer(buffer, wSize)

  • buffer ArrayBuffer | Uint8Array | number[] - The buffer to write.
  • wSize number - Size to write from the buffer.

Write a buffer of data. Returns Promise - Resolves when the writing finishes.

writeTransfer(command, buffer[, dcPin, csPin])

  • command number - Command to write.
  • buffer ArrayBuffer | Uint8Array | number[] - Data to write. The whole buffer length will be used.
  • dcPin number (optional) - DC (data command) pin.
  • csPin number (optional) - CS (chip select) pin.

Write command followed by buffer. If dcPin and/or csPin is defined the corresponding GPIO pin(s) will change to low/high the following way:
DC low → CS low → Write command → CS high → DC high → CS low → Write buffer → CS high

Returns Promise - Resolves when the writing finishes.

writeMemBuffer()

Write all data from memoryBuffer through SPI.

Returns Promise - Resolves when the SPI write finishes.