2014-03-23 07:21:22 +05:30
|
|
|
// I2C support.
|
|
|
|
|
2014-03-03 00:51:23 +05:30
|
|
|
package embd
|
2013-12-07 23:11:06 +05:30
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// I2CBus interface is used to interact with the I2C bus.
|
2014-03-03 00:51:23 +05:30
|
|
|
type I2CBus interface {
|
2014-01-05 14:27:29 +05:30
|
|
|
// ReadByte reads a byte from the given address.
|
2013-12-07 23:11:06 +05:30
|
|
|
ReadByte(addr byte) (value byte, err error)
|
2014-01-05 14:27:29 +05:30
|
|
|
// WriteByte writes a byte to the given address.
|
2013-12-07 23:11:06 +05:30
|
|
|
WriteByte(addr, value byte) error
|
2014-01-05 14:27:29 +05:30
|
|
|
// WriteBytes writes a slice bytes to the given address.
|
2013-12-07 23:11:06 +05:30
|
|
|
WriteBytes(addr byte, value []byte) error
|
|
|
|
|
2014-01-05 14:27:29 +05:30
|
|
|
// 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 23:11:06 +05:30
|
|
|
ReadByteFromReg(addr, reg byte) (value byte, err error)
|
2014-01-05 14:27:29 +05:30
|
|
|
// 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
|
2014-04-06 06:50:09 +05:30
|
|
|
|
|
|
|
// Close releases the resources associated with the bus.
|
|
|
|
Close() error
|
2013-12-07 23:11:06 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// I2CDriver interface interacts with the host descriptors to allow us
|
|
|
|
// control of I2C communication.
|
2014-03-23 06:32:24 +05:30
|
|
|
type I2CDriver interface {
|
2014-03-03 00:51:23 +05:30
|
|
|
Bus(l byte) I2CBus
|
2014-01-05 14:27:29 +05:30
|
|
|
|
2014-04-06 06:50:09 +05:30
|
|
|
// Close releases the resources associated with the driver.
|
2014-02-27 04:24:53 +05:30
|
|
|
Close() error
|
2014-01-05 14:27:29 +05:30
|
|
|
}
|
2014-03-01 20:19:44 +05:30
|
|
|
|
2014-04-11 09:19:03 +05:30
|
|
|
var i2cDriverInitialized bool
|
2014-03-23 06:32:24 +05:30
|
|
|
var i2cDriverInstance I2CDriver
|
2014-03-01 20:19:44 +05:30
|
|
|
|
2014-03-23 07:21:22 +05:30
|
|
|
// InitI2C initializes the I2C driver.
|
2014-03-03 00:51:23 +05:30
|
|
|
func InitI2C() error {
|
2014-04-11 09:19:03 +05:30
|
|
|
if i2cDriverInitialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-03 00:51:23 +05:30
|
|
|
desc, err := DescribeHost()
|
2014-03-01 20:19:44 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
if desc.I2CDriver == nil {
|
2014-03-23 06:41:51 +05:30
|
|
|
return ErrFeatureNotSupported
|
2014-03-23 06:25:32 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
i2cDriverInstance = desc.I2CDriver()
|
2014-04-11 09:19:03 +05:30
|
|
|
i2cDriverInitialized = true
|
2014-03-01 20:19:44 +05:30
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// CloseI2C releases resources associated with the I2C driver.
|
2014-03-03 00:51:23 +05:30
|
|
|
func CloseI2C() error {
|
2014-03-23 06:32:24 +05:30
|
|
|
return i2cDriverInstance.Close()
|
2014-03-01 20:19:44 +05:30
|
|
|
}
|
|
|
|
|
2014-03-23 14:09:31 +05:30
|
|
|
// NewI2CBus returns a I2CBus.
|
2014-03-03 00:51:23 +05:30
|
|
|
func NewI2CBus(l byte) I2CBus {
|
2014-04-11 09:19:03 +05:30
|
|
|
if err := InitI2C(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-03-23 06:32:24 +05:30
|
|
|
return i2cDriverInstance.Bus(l)
|
2014-03-01 20:19:44 +05:30
|
|
|
}
|