1
0
mirror of https://github.com/kidoman/embd synced 2024-10-31 23:38:44 +01:00
embd/i2c.go

78 lines
2.0 KiB
Go
Raw Normal View History

2014-03-23 02:51:22 +01:00
// I2C support.
2014-03-02 20:21:23 +01:00
package embd
2013-12-07 18:41:06 +01:00
2014-03-23 02:51:22 +01:00
// I2CBus interface is used to interact with the I2C bus.
2014-03-02 20:21:23 +01:00
type I2CBus interface {
// ReadByte reads a byte from the given address.
2013-12-07 18:41:06 +01:00
ReadByte(addr byte) (value byte, err error)
// WriteByte writes a byte to the given address.
2013-12-07 18:41:06 +01:00
WriteByte(addr, value byte) error
// WriteBytes writes a slice bytes to the given address.
2013-12-07 18:41:06 +01:00
WriteBytes(addr byte, value []byte) error
// ReadFromReg reads n (len(value)) bytes from the given address and register.
ReadFromReg(addr, reg byte, value []byte) error
// ReadByteFromReg reads a byte from the given address and register.
2013-12-07 18:41:06 +01:00
ReadByteFromReg(addr, reg byte) (value byte, err error)
// ReadU16FromReg reads a unsigned 16 bit integer from the given address and register.
ReadWordFromReg(addr, reg byte) (value uint16, err error)
// WriteToReg writes len(value) bytes to the given address and register.
WriteToReg(addr, reg byte, value []byte) error
// WriteByteToReg writes a byte to the given address and register.
WriteByteToReg(addr, reg, value byte) error
// WriteU16ToReg
WriteWordToReg(addr, reg byte, value uint16) error
// Close releases the resources associated with the bus.
Close() error
2013-12-07 18:41:06 +01:00
}
2014-03-23 02:51:22 +01:00
// I2CDriver interface interacts with the host descriptors to allow us
// control of I2C communication.
type I2CDriver interface {
2014-03-02 20:21:23 +01:00
Bus(l byte) I2CBus
// Close releases the resources associated with the driver.
Close() error
}
var i2cDriverInitialized bool
var i2cDriverInstance I2CDriver
2014-03-23 02:51:22 +01:00
// InitI2C initializes the I2C driver.
2014-03-02 20:21:23 +01:00
func InitI2C() error {
if i2cDriverInitialized {
return nil
}
2014-03-02 20:21:23 +01:00
desc, err := DescribeHost()
if err != nil {
return err
}
if desc.I2CDriver == nil {
2014-03-23 02:11:51 +01:00
return ErrFeatureNotSupported
}
i2cDriverInstance = desc.I2CDriver()
i2cDriverInitialized = true
return nil
}
2014-03-23 09:39:31 +01:00
// CloseI2C releases resources associated with the I2C driver.
2014-03-02 20:21:23 +01:00
func CloseI2C() error {
return i2cDriverInstance.Close()
}
2014-03-23 09:39:31 +01:00
// NewI2CBus returns a I2CBus.
2014-03-02 20:21:23 +01:00
func NewI2CBus(l byte) I2CBus {
if err := InitI2C(); err != nil {
panic(err)
}
return i2cDriverInstance.Bus(l)
}