Skip to main content

Class SpiCommunications

Helper class for SPI communications, handles registers, endian, etc. This encapsulates and synchronizes the SPI bus and chip select ports

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class SpiCommunications : ISpiCommunications, IByteCommunications

Implements:
Meadow.Hardware.ISpiCommunications, Meadow.Hardware.IByteCommunications

Properties

ChipSelect

The SPI chip select port

View Source
Declaration
public IDigitalOutputPort? ChipSelect { get; }

Bus

the ISpiBus object

View Source
Declaration
public ISpiBus Bus { get; }

BusSpeed

SPI bus speed

View Source
Declaration
public Frequency BusSpeed { get; set; }

BusMode

SPI bus mode

View Source
Declaration
public SpiClockConfiguration.Mode BusMode { get; set; }

WriteBuffer

Internal write buffer. Used in methods in which the buffers aren't passed in.

View Source
Declaration
protected Memory<byte> WriteBuffer { get; }

ReadBuffer

Internal read buffer. Used in methods in which the buffers aren't passed in.

View Source
Declaration
protected Memory<byte> ReadBuffer { get; }

Methods

Read(Span<byte>)

Reads data from the peripheral.

View Source
Declaration
public virtual void Read(Span<byte> readBuffer)
Parameters
TypeNameDescription
System.Span<System.Byte>readBufferThe buffer to read from the peripheral into.

ReadRegister(byte, Span<byte>)

Reads data from the peripheral starting at the specified address.

View Source
Declaration
public virtual void ReadRegister(byte address, Span<byte> readBuffer)
Parameters
TypeNameDescription
System.ByteaddressThe register address
System.Span<System.Byte>readBufferThe buffer to hold the data

ReadRegister(byte)

Reads a single byte from the specified address of the peripheral

View Source
Declaration
public virtual byte ReadRegister(byte address)
Returns

System.Byte: The byte read

Parameters
TypeNameDescription
System.ByteaddressAddress to read

ReadRegisterAsUShort(byte, ByteOrder)

Reads a single ushort value from the specified address of the peripheral

View Source
Declaration
public virtual ushort ReadRegisterAsUShort(byte address, ByteOrder order = ByteOrder.LittleEndian)
Returns

System.UInt16: The value read

Parameters
TypeNameDescription
System.ByteaddressAddress of the read
Meadow.ByteOrderorderEndianness of the value read

Write(byte)

Writes a single byte to the peripheral

View Source
Declaration
public void Write(byte value)
Parameters
TypeNameDescription
System.BytevalueValue to write

Write(Span<byte>)

Write a span of bytes to the peripheral.

View Source
Declaration
public virtual void Write(Span<byte> data)
Parameters
TypeNameDescription
System.Span<System.Byte>dataData to be written.

WriteRegister(byte, byte)

Writes a single byte to the specified address of the peripheral

View Source
Declaration
public virtual void WriteRegister(byte address, byte value)
Parameters
TypeNameDescription
System.ByteaddressThe target write register address
System.BytevalueValue to write

WriteRegister(byte, ushort, ByteOrder)

Writes a single ushort value to a target register address on the peripheral (i.e. [address][ushort])

View Source
Declaration
public virtual void WriteRegister(byte address, ushort value, ByteOrder order = ByteOrder.LittleEndian)
Parameters
TypeNameDescription
System.ByteaddressThe target write register address
System.UInt16valueValue to write
Meadow.ByteOrderorderEndianness of the value to be written

WriteRegister(byte, uint, ByteOrder)

Write an unsigned integer to the peripheral.

View Source
Declaration
public virtual void WriteRegister(byte address, uint value, ByteOrder order = ByteOrder.LittleEndian)
Parameters
TypeNameDescription
System.ByteaddressAddress to write the first byte to.
System.UInt32valueValue to be written.
Meadow.ByteOrderorderIndicate if the data should be written as big or little endian.

WriteRegister(byte, ulong, ByteOrder)

Write an unsigned long to the peripheral.

View Source
Declaration
public virtual void WriteRegister(byte address, ulong value, ByteOrder order = ByteOrder.LittleEndian)
Parameters
TypeNameDescription
System.ByteaddressAddress to write the first byte to.
System.UInt64valueValue to be written.
Meadow.ByteOrderorderIndicate if the data should be written as big or little endian.

WriteRegister(byte, Span<byte>, ByteOrder)

Write data to a register in the peripheral.

View Source
Declaration
public virtual void WriteRegister(byte address, Span<byte> writeBuffer, ByteOrder order = ByteOrder.LittleEndian)
Parameters
TypeNameDescription
System.ByteaddressAddress of the register to write to.
System.Span<System.Byte>writeBufferA buffer of byte values to be written.
Meadow.ByteOrderorderIndicate if the data should be written as big or little endian.

Exchange(Span<byte>, Span<byte>, DuplexType)

Exchange data over the SPI bus

View Source
Declaration
public virtual void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
Parameters
TypeNameDescription
System.Span<System.Byte>writeBufferThe buffer holding the data to write
System.Span<System.Byte>readBufferThe buffer to receive data
Meadow.Hardware.DuplexTypeduplexThe duplex mode - half or full

Implements

  • Meadow.Hardware.ISpiCommunications
  • Meadow.Hardware.IByteCommunications