Table of Contents

Class I2cBus

Namespace
Pamoja.Hal
Assembly
Pamoja.Hal.dll

One I2C bus, shared by the program and every driver built on it.

public sealed class I2cBus : IDisposable
Inheritance
I2cBus
Implements
Inherited Members

Remarks

Open(string) opens the kernel's adapter on a Linux board; Simulated(params SimulatedPart[]) puts SimulatedParts on a bus, each answering at its own address; Scripted(params I2cStep[]) plays I2cSteps in order and refuses any other transfer. A driver runs the same way over all three.

A failed transfer throws PamojaException with the reason: nothing answered at the address, the script expected something else, or the kernel's own words. Only Linux has the adapter; opening one anywhere else throws PlatformNotSupportedException.

Properties

Kind

What answers on the bus.

public I2cBusKind Kind { get; }

Property Value

I2cBusKind

Remaining

How many steps a script has left, or null when the bus is not scripted.

public int? Remaining { get; }

Property Value

int?

Transfers

How many transfers have been made on the bus, by the program and every driver on it, including any that failed.

public int Transfers { get; }

Property Value

int

WaitedMicros

How long the drivers on the bus have asked to wait, in microseconds, whether or not the process slept through it.

public ulong WaitedMicros { get; }

Property Value

ulong

Methods

Attach(SimulatedPart)

Puts a copy of a part on a simulated bus, in place of any part at its address. A driver keeps working across the change, which is how a test moves a reading on.

public void Attach(SimulatedPart part)

Parameters

part SimulatedPart

The part, of any kind.

Exceptions

PamojaException

The bus is not simulated.

Dispose()

Releases this share of the bus. The bus closes when no driver holds it either.

public void Dispose()

Open(string)

Opens the kernel's I2C adapter, such as /dev/i2c-1 on a Raspberry Pi.

public static I2cBus Open(string path)

Parameters

path string

The adapter's device file.

Returns

I2cBus

The bus, with the real parts wired to it on the other end.

Exceptions

PlatformNotSupportedException

The platform is not Linux.

PamojaException

The file cannot be opened as an adapter: the interface is not turned on, or the process may not use it.

Part(byte)

Copies what a simulated part holds now, with whatever drivers wrote to it.

public SimulatedPart? Part(byte address)

Parameters

address byte

The part's address.

Returns

SimulatedPart

The copy, as the kind of part it is, or null when the bus is not simulated or no part holds the address.

Part<TPart>(byte)

Copies what a simulated part of one kind holds now, with whatever drivers wrote to it.

public TPart? Part<TPart>(byte address) where TPart : SimulatedPart

Parameters

address byte

The part's address.

Returns

TPart

The copy, or null when the bus is not simulated, no part holds the address, or the part there is of another kind.

Type Parameters

TPart

I2cPart, WordPart, or CommandPart.

Read(byte, int)

Reads bytes from a part in one transaction.

public byte[] Read(byte address, int length)

Parameters

address byte

The part's 7-bit address.

length int

How many bytes to read.

Returns

byte[]

The bytes.

Exceptions

PamojaException

The transfer failed.

Scripted(params I2cStep[])

Makes a bus that plays the steps in order and refuses any other transfer.

public static I2cBus Scripted(params I2cStep[] steps)

Parameters

steps I2cStep[]

The transfers a driver is expected to make, and the replies.

Returns

I2cBus

The bus.

Exceptions

PamojaException

A step could not be added to the script.

Simulated(params SimulatedPart[])

Makes a bus of simulated parts, each answering at its own address.

public static I2cBus Simulated(params SimulatedPart[] parts)

Parameters

parts SimulatedPart[]

The parts, of any kind, copied onto the bus. A later part at an address an earlier one holds takes its place.

Returns

I2cBus

The bus. A transfer to an address no part holds throws, as nothing acknowledges it.

Use<TResult>(Func<nint, TResult>)

Runs a native call that needs this bus's handle.

public TResult Use<TResult>(Func<nint, TResult> call)

Parameters

call Func<nint, TResult>

The native call to make.

Returns

TResult

Whatever the native call returned.

Type Parameters

TResult

What the native call returns.

Remarks

A driver takes the bus's handle when it is built and holds its own share of the bus, so this bus may be disposed straight afterward.

Write(byte, ReadOnlySpan<byte>)

Writes bytes to a part in one transaction: usually a register and its value.

public void Write(byte address, ReadOnlySpan<byte> bytes)

Parameters

address byte

The part's 7-bit address.

bytes ReadOnlySpan<byte>

The bytes to write.

Exceptions

PamojaException

The transfer failed.

WriteRead(byte, ReadOnlySpan<byte>, int)

Writes bytes and then reads the reply in one transaction, with a repeated start between them, which is how a register is read.

public byte[] WriteRead(byte address, ReadOnlySpan<byte> bytes, int length)

Parameters

address byte

The part's 7-bit address.

bytes ReadOnlySpan<byte>

What to write first, usually the register address.

length int

How many bytes to read.

Returns

byte[]

The reply.

Exceptions

PamojaException

The transfer failed.