Documentation
¶
Overview ¶
Package i2cm defines low level I2C master access and implements I2C transactions and an 24Cxx EEPROM driver.
As of April 2013, there is one I2C driver implementation using the bus pirate from Dangerous Prototypes. Get the package at github.com/distributed/bp.
Index ¶
Constants ¶
const (
MAX_EEPROM_SIZE = 1 << (16 + 3)
)
Variables ¶
var Conf_24C02 = EEPROM24Config{256, 8, 5 * time.Millisecond}
var Conf_24C128 = EEPROM24Config{16384, 64, 5 * time.Millisecond}
var NACKReceived = errors.New("NACK received")
NACKReceived signals that devices did not ACK.
var NoSuchDevice = errors.New("no such device")
NoSuchDevice signals that no device responded with an ACK at the desired address.
Functions ¶
func I2CMasterTransact8x8 ¶
func I2CMasterTransact8x8(m I2CMaster, addr Addr, regaddr uint8, w []byte, r []byte) (int, int, error)
I2CMasterTransact8x8 carries out a transaction as specified by Transactor8x8 by using the low level I2CMaster interface. This function can be used as a fallback for implementors of Transactor8x8 in case their I2C bus master only supports a limited set of 8x8 transactions.
Types ¶
type Addr ¶
type Addr interface {
// Get the device address. This does not encompass the R/W bit.
GetBaseAddr() uint16
// Get the device address' width in bits. Reasonable values are
// 7 and 10 bits.
GetAddrLen() int
}
Addr represents an I2C device address. It supports both 7 bit and 10 bit addressing. Other address width are possible but will not be supported by transaction drivers.
type Addr7 ¶
type Addr7 uint8
Addr7 represents a 7 bit I2C address. The device address must be right aligned.
func (Addr7) GetAddrLen ¶
func (Addr7) GetBaseAddr ¶
type Addr10 ¶
type Addr10 uint16
Addr7 represents a 10 bit I2C address. The device address must be right aligned.
func (Addr10) GetAddrLen ¶
func (Addr10) GetBaseAddr ¶
type EEPROM24 ¶
EEPROM24 represents an I2C EEPROM device. The memory array is made available via a file-like interface. The file's size is fixed to the memory array size and writes past the end of the array result in an error.
func NewEEPROM24 ¶
func NewEEPROM24(m I2CMaster, devaddr Addr, conf EEPROM24Config) (EEPROM24, error)
NewEEPROM24 constructs an I2C EEPROM driver for a device with base address devaddr residing on m's bus. The EEPROM driver parameters are passed in conf. Invalid configurations are rejected.
type EEPROM24Config ¶
type EEPROM24Config struct {
Size uint
PageSize uint
WriteDelay time.Duration // time to wait after a page write. Address polling is not implemented
}
EEPROM24Config is used to configure the EEPROM driver to use a specific device. There are two protocols in use with 24Cxx devices: 24C16 and smaller are addressed with 8+3 bits: 8 bits in the "register address" following the device address on the bus and up to 3 bits in the device address. 24C32 and larger are addressed with 16+3 bits: 16 bits in the two bytes following the device address and up to 3 bits in the device address. NewEEPROM24 switches between the protocols based on the above criteria.
type I2CMaster ¶
type I2CMaster interface {
// Start sends a start or repeated start condition,
// depending on bus state.
Start() error
// Sends a stop condition.
Stop() error
// ReadByte reads one byte and sends an ACK
// if ack is true. Application code is responsible
// to ensure that ack is false for the last read
// before a stop bit is sent.
ReadByte(ack bool) (recvb byte, err error)
// WriteByte writes one byte to the device. If
// device does not ACK, it returns NACKReceived.
WriteByte(b byte) error
}
I2CMaster offers low-level access to an I2C bus. The driver implementing this interface must be the only master on the bus.
type Transactor ¶
type Transactor interface {
Transactor8x8
Transactor16x8
}
Transactor encompasses all implemented I2C bus transaction types.
func NewTransactor ¶
func NewTransactor(m I2CMaster) Transactor
NewTransactor returns all implemented I2C transactors based on the argument I2CMaster. This is a convenience function which consolidates the results of the NewTransact*x* family of functions.
type Transactor8x8 ¶
type Transactor8x8 interface {
Transact8x8(addr Addr, regaddr uint8, w []byte, r []byte) (nw, nr int, err error)
}
Implements a write-then-read transaction with 8 bit register addresses and 8 bit data. The transaction always writes data to the device, as the register address is always written. The read part of the transaction is not executed if len(r) == 0. nw and nr specify the number of bytes written or read, respectively, before an error occured or the transaction finished. If err == nil, then nw == len(w) and nr == len(r).
A transaction with len(r) == 0 is carried out as follows:
[S] [(devaddr<<1)] A [regaddr] A [w[0]] A ... [P]
A transaction with len(r) is carried out as follows:
[S] [(devaddr<<1)] A [regaddr] A [w[0]] A ... [S] [(devaddr<<1)|1] r[0] [A] ... r[len(r)-1] [N] [P]
func NewTransact8x8 ¶
func NewTransact8x8(m I2CMaster) Transactor8x8
NewTransact8x8 returns a Transactor8x8 which is based on m. If the argument m is already a Transactor8x8, it returns the underlying Transactor8x8. If you want to make sure that transactions are carried out using the low level I2CMaster interface, see I2CMasterTransact8x8.
type Transactor16x8 ¶
type Transactor16x8 interface {
Transact16x8(addr Addr, regaddr uint16, w []byte, r []byte) (nw, nr int, err error)
}
Implements a write-then-read transaction with 16 bit register addresses and 8 bit data. The transaction always writes data to the device, as the register address is always written. The read part of the transaction is not executed if len(r) == 0. nw and nr specify the number of bytes written or read, respectively, before an error occured or the transaction finished. If err == nil, then nw == len(w) and nr == len(r).
A transaction with len(r) == 0 is carried out as follows:
[S] [(devaddr<<1)] A [hi8(regaddr)] A [lo8(regaddr)] A [w[0]] A ... [P]
A transaction with len(r) is carried out as follows:
[S] [(devaddr<<1)] A [hi8(regaddr)] A [lo8(regaddr)] A [w[0]] A ... [S] [(devaddr<<1)|1] r[0] [A] ... r[len(r)-1] [N] [P]
func NewTransact16x8 ¶
func NewTransact16x8(m I2CMaster) Transactor16x8
NewTransact16x8 returns a Transactor16x8 which is based on m. If the argument m is already a Transactor16x8, it returns the underlying Transactor16x8. If not, NewTransact16x8 creates a a Transactor8x8 from the supplied I2C Master and uses it to emulate 16x8 accesses.
In contrast to NewTransact8x8, there is no low-level implementation of a 16x8 transaction in this package.