i2c

package module
v0.0.0-...-8ecf0e0 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

README

i2c - Library to connect to I2C bus and allow access to I2C and SMBus devices

This package aims to provide a simple method to access I2C and SMBus devices. The functionality is limited to what I required at the time it was created but it should be commented well enough for others to extend it to meet their needs (have included quite a bit C code in the comments to make it easy to find the relevant bits in the kernel code).

Currently supports Linux only and has only been tested with a small number of devices connected to amd64 linux machines.

Note: While only linux is supported the code has been written so that it will compile on any platform (but will always return an error if used).

Thanks to the authors of periph.io (have copied/pasted a hunk of code) and github.com/go-daq/smbus (helped me work out the smbus calls).

Documentation

Overview

This file defines a range of constants used for IOCTL commands These are defined in https://github.com/torvalds/linux/blob/master/include/uapi/linux/i2c-dev.h (and i2c.h in the same folder) some code taken from periph.io (saved retyping!)

This is a pure go-lang library providing access to i2c devices. In addition it supports access to SMBus devices (over i2c). Accessing SMBus devices requires the use of slightly different system calls from I2C.

Currently this library currently supports LINUX only and has only been tested on the amd64 CPUs. It is pure go (no CGO) but relies upon ioctl calls that are linux specific. Because it is used in projects that support other OS's it will build successfully (but every function call will return an error).

This library is based (and borrows from) a number of other libraries periph.io - library supporting i2c (and heaps more) but not smbus github.com/go-daq/smbus - smbus library (no standard i2c support)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBusInfo

func GetBusInfo() ([]i2cBus, error)

GetBusInfo retrieves information on the I2C busses available to us This code is mostly taken from https://github.com/google/periph/blob/master/conn/i2c/i2creg/i2creg.go

Types

type I2cConnection

type I2cConnection interface {
	Close() error                // Close the connection (call this when done!)
	FunctionalityString() string // Returns a string detailing the bus functionality (for debugging)

	// Note: We use Get/Set instead of read/write to avoid using the signitures from io.ByteReader/io.ByteWriter as may want to implement thes in the future
	GetByte(slaveID uint16, address uint8) (byte, error)           // Read a single byte from the device (assumes address = command)
	GetBytes(slaveID uint16, startAddress uint8, buf []byte) error // Read a number of bytes from the device
	SetByte(slaveID uint16, address uint8, value byte) error       // Write a single byte to the device (assumes address = command)
}

I2cConnection provides a means to access devices on an I2C bus (including SMBus instances) It can be used to communicate with multiple devices from multiple goroutines. The interface has been kept limited and simple (I2C and SMBus capabilities differ somewhat)

func NewI2CConnectionByBusNumber

func NewI2CConnectionByBusNumber(busNumber int) (I2cConnection, error)

NewI2CConnectionByBusNumber - connect using a bus number, e.g. on linux the X in /dev/i2c-X (linux busnumber 0 = /dev/i2c-0)

func NewI2CConnectionByLocation

func NewI2CConnectionByLocation(busLocation string) (I2cConnection, error)

NewI2CConnectionByLocation - connect using a bus location (on linux "0" or "/dev/i2c-0"). String may be platform dependent

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳