1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00

i2c: lazy init

This commit is contained in:
Karan Misra 2014-02-08 01:50:56 +05:30
parent 3843fd4ddf
commit 96a92383e2

View File

@ -63,6 +63,7 @@ var busMapLock sync.Mutex
var Default Bus var Default Bus
type bus struct { type bus struct {
l byte
file *os.File file *os.File
addr byte addr byte
mu sync.Mutex mu sync.Mutex
@ -70,33 +71,37 @@ type bus struct {
func init() { func init() {
busMap = make(map[byte]*bus) busMap = make(map[byte]*bus)
var err error Default = NewBus(1)
Default, err = NewBus(1)
if err != nil {
panic(err)
}
} }
// NewBus creates a new I2C bus interface. The l variable // NewBus creates a new I2C bus interface. The l variable
// controls which bus we connect to. // controls which bus we connect to.
// //
// For the newer RaspberryPi, the number is 1 (earlier model uses 0.) // For the newer RaspberryPi, the number is 1 (earlier model uses 0.)
func NewBus(l byte) (Bus, error) { func NewBus(l byte) Bus {
busMapLock.Lock() busMapLock.Lock()
defer busMapLock.Unlock() defer busMapLock.Unlock()
var b *bus var b *bus
if b = busMap[l]; b == nil { if b = busMap[l]; b == nil {
b = new(bus) b = &bus{l: l}
var err error
if b.file, err = os.OpenFile(fmt.Sprintf("/dev/i2c-%v", l), os.O_RDWR, os.ModeExclusive); err != nil {
return nil, err
}
busMap[l] = b busMap[l] = b
} }
return b, nil return b
}
func (b *bus) init() (err error) {
if b.file != nil {
return
}
if b.file, err = os.OpenFile(fmt.Sprintf("/dev/i2c-%v", b.l), os.O_RDWR, os.ModeExclusive); err != nil {
return
}
return
} }
func (b *bus) setAddress(addr byte) (err error) { func (b *bus) setAddress(addr byte) (err error) {
@ -116,6 +121,10 @@ func (b *bus) ReadByte(addr byte) (value byte, err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }
@ -136,6 +145,10 @@ func (b *bus) WriteByte(addr, value byte) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }
@ -149,11 +162,15 @@ func (b *bus) WriteByte(addr, value byte) (err error) {
return return
} }
func (b *bus) WriteBytes(addr byte, value []byte) error { func (b *bus) WriteBytes(addr byte, value []byte) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err := b.setAddress(addr); err != nil { if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil {
return err return err
} }
@ -177,6 +194,10 @@ func (b *bus) ReadFromReg(addr, reg byte, value []byte) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }
@ -228,6 +249,10 @@ func (b *bus) WriteToReg(addr, reg byte, value []byte) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }
@ -259,6 +284,10 @@ func (b *bus) WriteByteToReg(addr, reg, value byte) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }
@ -291,6 +320,10 @@ func (b *bus) WriteWordToReg(addr, reg byte, value uint16) (err error) {
b.mu.Lock() b.mu.Lock()
defer b.mu.Unlock() defer b.mu.Unlock()
if err = b.init(); err != nil {
return
}
if err = b.setAddress(addr); err != nil { if err = b.setAddress(addr); err != nil {
return return
} }