Skip to main content

Class I2cCommunications

Helper class for I2C communications, handles registers, endian, etc.

Assembly: Meadow.Foundation.dll
View Source
Declaration
public class I2cCommunications : II2cCommunications, IByteCommunications

Derived:
Meadow.Foundation.ICs.IOExpanders.Pca9671

Implements:
Meadow.Hardware.II2cCommunications, Meadow.Hardware.IByteCommunications

Properties

Address

The I2C address

View Source
Declaration
public byte Address { get; protected set; }

Bus

The I2C bus

View Source
Declaration
public II2cBus Bus { get; protected 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 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
TypeName
System.Byteaddress
System.Span<System.Byte>readBuffer

ReadRegister(byte)

Read a register from the peripheral.

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

System.Byte

Parameters
TypeNameDescription
System.ByteaddressAddress of the register to read.

ReadRegisterAsUShort(byte, ByteOrder)

Read an unsigned short from a register.

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

System.UInt16: Value read from the register.

Parameters
TypeNameDescription
System.ByteaddressRegister address of the low byte (the high byte will follow).
Meadow.ByteOrderorderOrder of the bytes in the register (little endian is the default).

Write(byte)

Write a single byte to the peripheral.

View Source
Declaration
public virtual void Write(byte value)
Parameters
TypeNameDescription
System.BytevalueValue to be written (8-bits).

Write(Span<byte>)

Write an array of bytes to the peripheral.

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

WriteRegister(byte, byte)

Write data a register in the peripheral.

View Source
Declaration
public virtual void WriteRegister(byte address, byte value)
Parameters
TypeNameDescription
System.ByteaddressAddress of the register to write to.
System.BytevalueData to write into the register.

WriteRegister(byte, ushort, ByteOrder)

Write an unsigned short to the peripheral.

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

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)

Exchanges data with an I2C device through a specified write and read buffer

View Source
Declaration
public virtual void Exchange(Span<byte> writeBuffer, Span<byte> readBuffer, DuplexType duplex = DuplexType.Half)
Parameters
TypeNameDescription
System.Span<System.Byte>writeBufferA span of bytes that represents the data to be written to the device
System.Span<System.Byte>readBufferA span of bytes where the data read from the device will be stored
Meadow.Hardware.DuplexTypeduplexAn optional parameter that specifies the duplex type of the communication.
It defaults to half-duplex.
Exceptions

System.ArgumentException
Thrown when duplex is set to full-duplex, as I2C only supports half-duplex communication

Implements

  • Meadow.Hardware.II2cCommunications
  • Meadow.Hardware.IByteCommunications