buses

package
v0.48.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2024 License: AGPL-3.0 Imports: 13 Imported by: 7

Documentation

Overview

Package buses is for I2C and SPI boards that run Linux. This file is for I2C support on those boards.

Package buses offers SPI and I2C buses for generic Linux systems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type I2C

type I2C interface {
	// OpenHandle locks returns a handle interface that MUST be closed when done.
	// you cannot have 2 open for the same addr
	OpenHandle(addr byte) (I2CHandle, error)
}

I2C represents a shareable I2C bus on the board.

func NewI2cBus

func NewI2cBus(deviceName string) (I2C, error)

NewI2cBus creates a new I2C (the public interface) object (implemented as the private i2cBus struct).

type I2CHandle

type I2CHandle interface {
	Write(ctx context.Context, tx []byte) error
	Read(ctx context.Context, count int) ([]byte, error)

	ReadByteData(ctx context.Context, register byte) (byte, error)
	WriteByteData(ctx context.Context, register, data byte) error

	ReadBlockData(ctx context.Context, register byte, numBytes uint8) ([]byte, error)
	WriteBlockData(ctx context.Context, register byte, data []byte) error

	// Close closes the handle and releases the lock on the bus.
	Close() error
}

I2CHandle is similar to an io handle. It MUST be closed to release the bus.

type I2CRegister

type I2CRegister struct {
	Handle   I2CHandle
	Register byte
}

An I2CRegister is a lightweight wrapper around a handle for a particular register.

func (*I2CRegister) ReadByteData

func (reg *I2CRegister) ReadByteData(ctx context.Context) (byte, error)

ReadByteData reads a byte from the I2C channel register.

func (*I2CRegister) WriteByteData

func (reg *I2CRegister) WriteByteData(ctx context.Context, data byte) error

WriteByteData writes a byte to the I2C channel register.

type I2cHandle

type I2cHandle struct {
	// contains filtered or unexported fields
}

I2cHandle represents a way to talk to a specific device on the I2C bus. Creating a handle locks the bus so nothing else can use it, and closing the handle unlocks it again.

func (*I2cHandle) Close

func (h *I2cHandle) Close() error

Close closes the handle to the device, and unlocks the I2C bus.

func (*I2cHandle) Read

func (h *I2cHandle) Read(ctx context.Context, count int) ([]byte, error)

Read reads the given number of bytes from the handle. For I2C devices that organize their data into registers, prefer using ReadBlockData instead.

func (*I2cHandle) ReadBlockData

func (h *I2cHandle) ReadBlockData(ctx context.Context, register byte, numBytes uint8) ([]byte, error)

ReadBlockData reads the given number of bytes from the I2C device, starting at the given register.

func (*I2cHandle) ReadByteData

func (h *I2cHandle) ReadByteData(ctx context.Context, register byte) (byte, error)

ReadByteData reads a single byte from the given register on this I2C device.

func (*I2cHandle) Write

func (h *I2cHandle) Write(ctx context.Context, tx []byte) error

Write writes the given bytes to the handle. For I2C devices that organize their data into registers, prefer using WriteBlockData instead.

func (*I2cHandle) WriteBlockData

func (h *I2cHandle) WriteBlockData(ctx context.Context, register byte, data []byte) error

WriteBlockData writes the given bytes into the given register on the I2C device.

func (*I2cHandle) WriteByteData

func (h *I2cHandle) WriteByteData(ctx context.Context, register, data byte) error

WriteByteData writes a single byte to the given register on this I2C device.

type SPI

type SPI interface {
	// OpenHandle locks the shared bus and returns a handle interface that MUST be closed when done.
	OpenHandle() (SPIHandle, error)
	Close(ctx context.Context) error
}

SPI represents a shareable SPI bus on a generic Linux board.

func NewSpiBus

func NewSpiBus(name string) SPI

NewSpiBus creates a new SPI bus. The name passed in should be the bus number, such as "0" or "1". We don't open this bus until you call spiHandle.Xfer(), so there are no errors to return immediately here.

type SPIHandle

type SPIHandle interface {
	// Xfer performs a single SPI transfer, that is, the complete transaction from chipselect
	// enable to chipselect disable. SPI transfers are synchronous, number of bytes received will
	// be equal to the number of bytes sent. Write-only transfers can usually just discard the
	// returned bytes. Read-only transfers usually transmit a request/address and continue with
	// some number of null bytes to equal the expected size of the returning data. Large
	// transmissions are usually broken up into multiple transfers. There are many different
	// paradigms for most of the above, and implementation details are chip/device specific.
	Xfer(
		ctx context.Context,
		baud uint,
		chipSelect string,
		mode uint,
		tx []byte,
	) ([]byte, error)

	// Close closes the handle and releases the lock on the bus.
	Close() error
}

SPIHandle is similar to an io handle. It MUST be closed to release the bus.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL