i2c

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: MIT Imports: 7 Imported by: 2

README

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

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 Orange Pi Zero Plus 2 H5.

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) }
....
}

Tutorial

In its turn, go-i2c use logrus library to output debug and other notification's lines which produce all necessary levels of logging.

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 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 "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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

License

Go-i2c is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	// DefaultReadDelay 默认的写入指令与读取数据之间的延迟
	// DefaultReadDelay Default delay between write cmd and read from bus
	DefaultReadDelay = 0
)
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 I2C

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

I2C 此结构体用于存储访问的i2c设备的信息

I2C represents a connection to I2C-device.

func NewI2C

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

NewI2C 此方法用于打开一个i2c句柄 bus为系统的i2c接口的id addr为要访问的设备在bus上的地址

NewI2C 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 (*I2C) Close

func (v *I2C) Close() error

Close 关闭i2c连接 Close I2C-connection.

func (*I2C) GetAddr

func (v *I2C) GetAddr() uint8

GetAddr 返回访问的i2c接口上的器件地址 GetAddr return device occupied address in the bus.

func (*I2C) GetBus

func (v *I2C) GetBus() int

GetBus 返回访问的i2c接口id GetBus return bus line, where I2C-device is allocated.

func (*I2C) ReadBytes

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

ReadBytes 从器件读取数据,返回读取的数据长度 ReadBytes read bytes from I2C-device. Number of bytes read correspond to buf parameter length.

func (*I2C) ReadRegBytes

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

ReadRegBytes 从器件读取n字节数据 ReadRegBytes read count of n byte's sequence from I2C-device starting from reg address. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegBytesWithDelay

func (v *I2C) ReadRegBytesWithDelay(reg byte, n int, delay time.Duration) ([]byte, int, error)

ReadRegBytesWithDelay 从器件读取n字节数据,在发送读取指令和读取之间附带延迟 ReadRegBytesWithDelay read count of n byte's sequence from I2C-device starting from reg address with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegS16BE

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

ReadRegS16BE 从器件读取2字节有符号数据(大端优先) ReadRegS16BE reads signed big endian word (16 bits) from I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegS16BEWithDelay

func (v *I2C) ReadRegS16BEWithDelay(reg byte, delay time.Duration) (int16, error)

ReadRegS16BEWithDelay 从器件读取2字节有符号数据(大端优先),在发送读取指令和读取之间附带延迟 ReadRegS16BEWithDelay reads signed big endian word (16 bits) from I2C-device starting from address specified in reg with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegS16LE

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

ReadRegS16LE 从器件读取2字节有符号数据(小端优先) ReadRegS16LE reads signed little endian word (16 bits) from I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegS16LEWithDelay

func (v *I2C) ReadRegS16LEWithDelay(reg byte, delay time.Duration) (int16, error)

ReadRegS16LEWithDelay 从器件读取2字节有符号数据(小端优先),在发送读取指令和读取之间附带延迟 ReadRegS16LEWithDelay reads signed little endian word (16 bits) from I2C-device starting from address specified in reg with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU16BE

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

ReadRegU16BE 从器件读取2字节无符号数据(大端优先) ReadRegU16BE reads unsigned big endian word (16 bits) from I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU16BEWithDelay

func (v *I2C) ReadRegU16BEWithDelay(reg byte, delay time.Duration) (uint16, error)

ReadRegU16BEWithDelay 从器件读取2字节无符号数据(大端优先),在发送读取指令和读取之间附带延迟 ReadRegU16BEWithDelay reads unsigned big endian word (16 bits) from I2C-device starting from address specified in reg with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU16LE

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

ReadRegU16LE 从器件读取2字节无符号数据(小端优先) ReadRegU16LE reads unsigned little endian word (16 bits) from I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU16LEWithDelay

func (v *I2C) ReadRegU16LEWithDelay(reg byte, delay time.Duration) (uint16, error)

ReadRegU16LEWithDelay 从器件读取2字节无符号数据(小端优先),在发送读取指令和读取之间附带延迟 ReadRegU16LEWithDelay reads unsigned little endian word (16 bits) from I2C-device starting from address specified in reg with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU8

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

ReadRegU8 从器件读取1字节数据 ReadRegU8 reads byte from I2C-device register specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) ReadRegU8WithDelay

func (v *I2C) ReadRegU8WithDelay(reg byte, delay time.Duration) (byte, error)

ReadRegU8WithDelay 从器件读取1字节数据,在发送读取指令和读取之间附带延迟 ReadRegU8WithDelay reads byte from I2C-device register specified in reg with delay between send and read. SMBus (System Management Bus) protocol over I2C.

func (*I2C) WriteBytes

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

WriteBytes 向器件发送数据 WriteBytes send bytes to the remote I2C-device. The interpretation of the message is implementation-dependent.

func (*I2C) WriteRegS16BE

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

WriteRegS16BE 向器件写入2字节有符号数据(大端优先) WriteRegS16BE writes signed big endian word (16 bits) value to I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) WriteRegS16LE

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

WriteRegS16LE 向器件写入2字节有符号数据(小端优先) WriteRegS16LE writes signed little endian word (16 bits) value to I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) WriteRegU16BE

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

WriteRegU16BE 向器件写入2字节无符号数据(大端优先) WriteRegU16BE writes unsigned big endian word (16 bits) value to I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) WriteRegU16LE

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

WriteRegU16LE 向器件写入2字节无符号数据(小端优先) WriteRegU16LE writes unsigned little endian word (16 bits) value to I2C-device starting from address specified in reg. SMBus (System Management Bus) protocol over I2C.

func (*I2C) WriteRegU8

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

WriteRegU8 向器件写入1字节数据 WriteRegU8 writes byte to I2C-device register specified in reg. SMBus (System Management Bus) protocol over I2C.

Jump to

Keyboard shortcuts

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