Skip to main content

SPI

SPI uses three or four wires for data transfer. A SPI bus has one master and one or more slaves. The master drives the clock on SCK, sends data on MOSI (Master Out Slave In), and reads incoming data on MISO (Master In Slave Out). The master picks which slave to talk to with a per-slave SSEL / CS (chip-select) pin.

In the simplest case the master always exchanges data with the slave simultaneously; you cannot write without reading at the same time. If the incoming bytes do not matter, use the plain Write method and discard the return.

tip

Endpoint is always the SPI master, not the slave.

tip

Some SPI devices (such as the VS1053 MP3 decoder) have more than one select pin, one for commands and one for data. Both share the same SCK / MOSI / MISO lines.

Endpoint uses the standard System.Device.Spi API. The only Endpoint-specific step is calling SpiController.Initialize(bus) first, which claims the bus's pins.

using System.Device.Spi;
using GHIElectronics.Endpoint.Core;

// Bring up SPI4 and claim its pins
SpiController.Initialize(EPDomino24.Spi.Spi4);

// Standard .NET IoT from here
var settings = new SpiConnectionSettings(EPDomino24.Spi.Spi4) {
ClockFrequency = 4_000_000,
Mode = SpiMode.Mode0,
ChipSelectLine = 0,
};
using var spi = SpiDevice.Create(settings);

var write = new byte[2];
var read = new byte[10];
spi.TransferFullDuplex(write, read);
tip

SpiController is chip-neutral; the same call works on any supported module. The bus constant (EPDomino24.Spi.Spi4) is board-specific and lists only the buses actually bonded on EP-Domino24.