i2c

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2021 License: MIT Imports: 5 Imported by: 23

README

I2C-bus interaction of peripheral sensors with Raspberry PI embedded linux or respective clones

Go Report Card GoDoc MIT License

This library written in Go programming language intended to activate and interact with the I2C bus by reading and writing data.

Compatibility

Tested on Raspberry Pi 1 (model B), Raspberry Pi 3 B+, Banana Pi (model M1), Orange Pi Zero, Orange Pi One.

Golang usage

func main() {
  // Create new connection to I2C dev on /dev/i2c-0 with address 0x27
  i2cDevice, err := i2c.New(0x27, "/dev/i2c-0")
  if err != nil { 
      i2cDevice.Log.Fatal(err)
  }
  // Free I2C connection on exit
  defer i2cDevice.Close()

  // Set log level: 0 - Panic, 1 - Fatal, 2 - Error, 3 - Warning, 4 - Info, 5 - Debug
  i2cDevice.Log.SetLevel(5)

  // Here goes code specific for sending and reading data
  // to and from device connected via I2C bus, like:
  _, err = i2cDevice.WriteBytes([]byte{0x1, 0xF3})
  if err != nil { 
      i2cDevice.Log.Fatal(err)
  }
}

Tutorial

In repositories contain quite a lot projects, which use i2c library as a starting point to interact with various peripheral devices and sensors for use on embedded Linux devices. All these libraries start with a standard call to open I2C-connection to specific bus line and address, than pass i2c instance to device.

You will find here the list of all devices and sensors supported by me, that reference this library:

Getting help

GoDoc documentation

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing arm64 in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

License

Go-i2c is licensed under MIT License.

Documentation

Overview

+build linux,cgo

Package i2c provides low level control over the Linux i2c bus.

Before usage you should load the i2c-dev kernel module

sudo modprobe i2c-dev

Each i2c bus can address 127 independent i2c devices, and most Linux systems contain several buses.

Index

Constants

View Source
const (
	I2C_SLAVE = C.I2C_SLAVE
)

Get I2C_SLAVE constant value from Linux OS I2C declaration file.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options added in v0.0.5

type Options struct {
	Log *logrus.Logger
	// contains filtered or unexported fields
}

Options represents a connection to I2C-device.

func New added in v0.0.5

func New(addr uint8, dev string) (*Options, error)

New opens a connection for I2C-device. SMBus (System Management Bus) protocol over I2C supported as well: you should preliminary specify register address to read from, either write register together with the data in case of write operations.

func (*Options) Close added in v0.0.5

func (o *Options) Close() error

Close I2C-connection.

func (*Options) GetAddr added in v0.0.5

func (o *Options) GetAddr() uint8

GetAddr return device occupied address in the bus.

func (*Options) GetDev added in v0.1.0

func (o *Options) GetDev() string

GetDev return full device name.

func (*Options) ReadBytes added in v0.0.5

func (o *Options) ReadBytes(buf []byte) (int, error)

ReadBytes read bytes from I2C-device. Number of bytes read correspond to buf parameter length.

func (*Options) ReadRegBytes added in v0.0.5

func (o *Options) ReadRegBytes(reg byte, n int) ([]byte, int, error)

ReadRegBytes read count of n byte's sequence from I2C-device starting from reg address.

func (*Options) ReadRegS16BE added in v0.0.5

func (o *Options) ReadRegS16BE(reg byte) (int16, error)

ReadRegS16BE reads signed big endian word (16 bits) from I2C-device starting from address specified in reg.

func (*Options) ReadRegS16LE added in v0.0.5

func (o *Options) ReadRegS16LE(reg byte) (int16, error)

ReadRegS16LE reads signed little endian word (16 bits) from I2C-device starting from address specified in reg.

func (*Options) ReadRegU16BE added in v0.0.5

func (o *Options) ReadRegU16BE(reg byte) (uint16, error)

ReadRegU16BE reads unsigned big endian word (16 bits) from I2C-device starting from address specified in reg.

func (*Options) ReadRegU16LE added in v0.0.5

func (o *Options) ReadRegU16LE(reg byte) (uint16, error)

ReadRegU16LE reads unsigned little endian word (16 bits) from I2C-device starting from address specified in reg.

func (*Options) ReadRegU8 added in v0.0.5

func (o *Options) ReadRegU8(reg byte) (byte, error)

ReadRegU8 reads byte from I2C-device register specified in reg.

func (*Options) WriteBytes added in v0.0.5

func (o *Options) WriteBytes(buf []byte) (int, error)

WriteBytes send bytes to the remote I2C-device. The interpretation of the message is implementation-dependent.

func (*Options) WriteRegS16BE added in v0.0.5

func (o *Options) WriteRegS16BE(reg byte, value int16) error

WriteRegS16BE writes signed big endian word (16 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegS16LE added in v0.0.5

func (o *Options) WriteRegS16LE(reg byte, value int16) error

WriteRegS16LE writes signed little endian word (16 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegU16BE added in v0.0.5

func (o *Options) WriteRegU16BE(reg byte, value uint16) error

WriteRegU16BE writes unsigned big endian word (16 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegU16LE added in v0.0.5

func (o *Options) WriteRegU16LE(reg byte, value uint16) error

WriteRegU16LE writes unsigned little endian word (16 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegU24BE added in v0.1.0

func (v *Options) WriteRegU24BE(reg byte, value uint32) error

WriteRegU24BE writes unsigned big endian word (24 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegU32BE added in v0.1.0

func (v *Options) WriteRegU32BE(reg byte, value uint32) error

WriteRegU32BE writes unsigned big endian word (32 bits) value to I2C-device starting from address specified in reg.

func (*Options) WriteRegU8 added in v0.0.5

func (o *Options) WriteRegU8(reg byte, value byte) error

WriteRegU8 writes byte to I2C-device register specified in reg.

Jump to

Keyboard shortcuts

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