i2c

package
v0.0.0-...-6dba82e Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2019 License: BSD-3-Clause Imports: 4 Imported by: 0

README

I2C bus setting up and usage for linux on RPi device and respective clones

Build Status 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) and Banana PI (model M1).

Golang usage

func main() {
  // Create new connection to I2C bus on 2 line with address 0x27
  i2c, err := i2c.NewI2C(0x27, 2)
  if err != nil { log.Fatal(err) }
  // Free I2C connection on exit
  defer i2c.Close()
  ....
  // Here goes code specific for sending and reading data
  // to and from device connected via I2C bus, like:
  _, err := i2c.Write([]byte{0x1, 0xF3})
  if err != nil { log.Fatal(err) }
  ....
}

Getting help

GoDoc documentation

Troubleshoting

  • 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 mannualy from official Golang site. Download tar.gz file containing armv6l 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 "Interfaceing 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. i2detect -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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

License

Go-i2c is licensed inder MIT License.

Documentation

Overview

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 = 0x0703
)

Variables

This section is empty.

Functions

This section is empty.

Types

type I2C

type I2C struct {

	// Enable verbose output
	Debug bool
	// contains filtered or unexported fields
}

I2C represents a connection to an i2c device.

func NewI2C

func NewI2C(addr uint8, bus int) (*I2C, error)

New opens a connection to an i2c device.

func (*I2C) Close

func (v *I2C) Close() error

func (*I2C) ReadBytes

func (v *I2C) ReadBytes(buf []byte) (int, error)

func (*I2C) ReadRegBytes

func (v *I2C) ReadRegBytes(reg byte, n int) ([]byte, int, error)

SMBus (System Management Bus) protocol over I2C. Read count of n byte's sequence from i2c device starting from reg address.

func (*I2C) ReadRegS16BE

func (v *I2C) ReadRegS16BE(reg byte) (int16, error)

SMBus (System Management Bus) protocol over I2C. Read signed big endian word (16 bits) from i2c device starting from address specified in reg.

func (*I2C) ReadRegS16LE

func (v *I2C) ReadRegS16LE(reg byte) (int16, error)

SMBus (System Management Bus) protocol over I2C. Read unsigned little endian word (16 bits) from i2c device starting from address specified in reg.

func (*I2C) ReadRegU16BE

func (v *I2C) ReadRegU16BE(reg byte) (uint16, error)

SMBus (System Management Bus) protocol over I2C. Read unsigned big endian word (16 bits) from i2c device starting from address specified in reg.

func (*I2C) ReadRegU16LE

func (v *I2C) ReadRegU16LE(reg byte) (uint16, error)

SMBus (System Management Bus) protocol over I2C. Read unsigned little endian word (16 bits) from i2c device starting from address specified in reg.

func (*I2C) ReadRegU8

func (v *I2C) ReadRegU8(reg byte) (byte, error)

SMBus (System Management Bus) protocol over I2C. Read byte from i2c device register specified in reg.

func (*I2C) WriteBytes

func (v *I2C) WriteBytes(buf []byte) (int, error)

Write sends buf to the remote i2c device. The interpretation of the message is implementation dependant.

func (*I2C) WriteRegS16BE

func (v *I2C) WriteRegS16BE(reg byte, value int16) error

SMBus (System Management Bus) protocol over I2C. Write signed big endian word (16 bits) value to i2c device starting from address specified in reg.

func (*I2C) WriteRegS16LE

func (v *I2C) WriteRegS16LE(reg byte, value int16) error

SMBus (System Management Bus) protocol over I2C. Write signed big endian word (16 bits) value to i2c device starting from address specified in reg.

func (*I2C) WriteRegU16BE

func (v *I2C) WriteRegU16BE(reg byte, value uint16) error

SMBus (System Management Bus) protocol over I2C. Write unsigned big endian word (16 bits) value to i2c device starting from address specified in reg.

func (*I2C) WriteRegU16LE

func (v *I2C) WriteRegU16LE(reg byte, value uint16) error

SMBus (System Management Bus) protocol over I2C. Write unsigned big endian word (16 bits) value to i2c device starting from address specified in reg.

func (*I2C) WriteRegU8

func (v *I2C) WriteRegU8(reg byte, value byte) error

SMBus (System Management Bus) protocol over I2C. Write 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