smbus

package module
v0.0.0-...-d22f265 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2016 License: GPL-2.0 Imports: 6 Imported by: 4

README

go-smbus

Go bindings for the System Management Bus (SMBus) kernel interface This package provides simple bindings for the SMBus interfaces provided by the i2c-dev driver. I wrote this for the Raspberry Pi platform.

This code is largely untested. I'll happily accept a pull request for any bugs you might find

Installation

go get github.com/corrupt/go-smbus

Usage

Create an instance of SMBus using the factory method. It takes two parameters, the interface index and the bus address. The former is the enumerated device index. If your I2C device is /dev/i2c-1, your index is 1. The latter is the bus address to connect to from 0x00 to 0x77. It can later be changed using the Set_addr method.

smb, err := smbus.New(1, 0x68)
if err != nil {
    fmt.Println(err)              
    os.Exit(1)  
}

You can now use the SMBus API to write to and read from the bus. All methods evaluate errno and return a go error accordingly. Block read/write methods also return the number of read/written bytes.

cmd := 0xD0
val := 0x10
err := smb.Write_byte_data(cmd, val)

buf := make ([]byte, 4)
i, err := smb.Read_i2c_block_data(0xD1, buf)
if err != nil {
    fmt.Println(err)              
    //error handling
}
if i != len(buf) {
    //error handling
}
Changing the Bus Device

The SMBus type provides two convenience methods to close the existing device and optionally open a new one

smb.Bus_close()
smb.Bus_open(0x71)

Documentation

Overview

Package smbus provides go bindings for the SMBus (System Management Bus) kernel interface SMBus is a subset of i2c suitable for a large number of devices Original domentation : https://www.kernel.org/doc/Documentation/i2c/smbus-protocol

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SMBus

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

Base type. Wraps a bus device and an address

func New

func New(bus uint, address byte) (*SMBus, error)

Factory method for SMBus

func (SMBus) Block_process_call

func (smb SMBus) Block_process_call(cmd byte, buf []byte) ([]byte, error)

This command selects a device register (through the cmd byte), sends 1 to 31 bytes of data to it, and reads 1 to 31 bytes of data in return.

func (*SMBus) Bus_close

func (smb *SMBus) Bus_close() error

Closes an open bus file

func (*SMBus) Bus_open

func (smb *SMBus) Bus_open(bus uint) error

Opens a new bus file with a given index. Will return an error if a bus is already open

func (SMBus) Process_call

func (smb SMBus) Process_call(cmd byte, value uint16) (uint16, error)

This command selects a device register (through the cmd byte), sends 16 bits of data to it, and reads 16 bits of data in return.

func (SMBus) Read_block_data

func (smb SMBus) Read_block_data(cmd byte, buf []byte) (int, error)

This command reads a block of up to 32 bytes from a device, from a designated register that is specified through the cmd byte. The amount of data in byte is specified by the length of the buf slice. To read 4 bytes of data, pass a slice created like this: make([]byte, 4)

func (SMBus) Read_byte

func (smb SMBus) Read_byte() (byte, error)

Reads a single byte from a device, without specifying a device register. Some devices are so simple that this interface is enough; for others, it is a shorthand if you want to read the same register as in the previous SMBus command.

func (SMBus) Read_byte_data

func (smb SMBus) Read_byte_data(cmd byte) (byte, error)

Reads a single byte from a device, from a designated register. The register is specified through the cmd byte

func (SMBus) Read_i2c_block_data

func (smb SMBus) Read_i2c_block_data(cmd byte, buf []byte) (int, error)

Block read method for devices without SMBus support. Uses plain i2c interface

func (*SMBus) Read_word_data

func (smb *SMBus) Read_word_data(cmd byte) (uint16, error)

This operation is very like Read Byte; again, data is read from a device, from a designated register that is specified through the cmd byte. But this time, the data is a complete word (16 bits).

func (*SMBus) Set_addr

func (smb *SMBus) Set_addr(addr byte) error

Set the device bus address to a value between 0x00 and 0x77

func (SMBus) Write_block_data

func (smb SMBus) Write_block_data(cmd byte, buf []byte) (int, error)

The opposite of the Block Read command, this writes up to 32 bytes to a device, to a designated register that is specified through the cmd byte. The amount of data is specified by the lengts of buf.

func (SMBus) Write_byte

func (smb SMBus) Write_byte(value byte) error

This operation is the reverse of Receive Byte: it sends a single byte to a device. See Receive Byte for more information.

func (SMBus) Write_byte_data

func (smb SMBus) Write_byte_data(cmd, value byte) error

Writes a single byte to a device, to a designated register. The register is specified through the cmd byte. This is the opposite of the Read Byte operation.

func (SMBus) Write_i2c_block_data

func (smb SMBus) Write_i2c_block_data(cmd byte, buf []byte) (int, error)

Block write method for devices without SMBus support. Uses plain i2c interface

func (SMBus) Write_quick

func (smb SMBus) Write_quick(value byte) error

Sends a single bit to the device, at the place of the Rd/Wr bit.

func (SMBus) Write_word_data

func (smb SMBus) Write_word_data(cmd byte, value uint16) error

This is the opposite of the Read Word operation. 16 bits of data is written to a device, to the designated register that is specified through the cmd byte.

Jump to

Keyboard shortcuts

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