i2c: refactor

This commit is contained in:
Karan Misra 2014-03-23 05:09:13 +05:30
parent 33dfc0f4c4
commit 3a072d013b
1 changed files with 41 additions and 49 deletions

View File

@ -91,45 +91,42 @@ func (b *i2cBus) Close() error {
return b.file.Close() return b.file.Close()
} }
func (b *i2cBus) setAddress(addr byte) (err error) { func (b *i2cBus) setAddress(addr byte) error {
if addr != b.addr { if addr != b.addr {
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), slaveCmd, uintptr(addr)); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), slaveCmd, uintptr(addr)); errno != 0 {
err = syscall.Errno(errno) return syscall.Errno(errno)
return
} }
b.addr = addr b.addr = addr
} }
return return nil
} }
func (b *i2cBus) ReadByte(addr byte) (value byte, err error) { func (b *i2cBus) ReadByte(addr byte) (byte, 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.setAddress(addr); err != nil {
return return 0, err
} }
bytes := make([]byte, 1) bytes := make([]byte, 1)
n, err := b.file.Read(bytes) n, _ := b.file.Read(bytes)
if n != 1 { if n != 1 {
err = fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n) return 0, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n)
} }
value = bytes[0] return bytes[0], nil
return
} }
func (b *i2cBus) WriteByte(addr, value byte) (err error) { func (b *i2cBus) WriteByte(addr, value byte) 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.setAddress(addr); err != nil {
return return err
} }
n, err := b.file.Write([]byte{value}) n, err := b.file.Write([]byte{value})
@ -138,14 +135,14 @@ func (b *i2cBus) WriteByte(addr, value byte) (err error) {
err = fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteByte", n) err = fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteByte", n)
} }
return return err
} }
func (b *i2cBus) WriteBytes(addr byte, value []byte) (err error) { func (b *i2cBus) WriteBytes(addr byte, value []byte) 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.setAddress(addr); err != nil {
return err return err
} }
@ -165,12 +162,12 @@ func (b *i2cBus) WriteBytes(addr byte, value []byte) (err error) {
return nil return nil
} }
func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) (err error) { func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) 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.setAddress(addr); err != nil {
return return err
} }
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&value)) hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&value))
@ -198,30 +195,28 @@ func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) (err error) {
return nil return nil
} }
func (b *i2cBus) ReadByteFromReg(addr, reg byte) (value byte, err error) { func (b *i2cBus) ReadByteFromReg(addr, reg byte) (byte, error) {
buf := make([]byte, 1) buf := make([]byte, 1)
if err = b.ReadFromReg(addr, reg, buf); err != nil { if err := b.ReadFromReg(addr, reg, buf); err != nil {
return return 0, err
} }
value = buf[0] return buf[0], nil
return
} }
func (b *i2cBus) ReadWordFromReg(addr, reg byte) (value uint16, err error) { func (b *i2cBus) ReadWordFromReg(addr, reg byte) (uint16, error) {
buf := make([]byte, 2) buf := make([]byte, 2)
if err = b.ReadFromReg(addr, reg, buf); err != nil { if err := b.ReadFromReg(addr, reg, buf); err != nil {
return return 0, err
} }
value = uint16((uint16(buf[0]) << 8) | uint16(buf[1])) return uint16((uint16(buf[0]) << 8) | uint16(buf[1])), nil
return
} }
func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) (err error) { func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) 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.setAddress(addr); err != nil {
return return err
} }
outbuf := append([]byte{reg}, value...) outbuf := append([]byte{reg}, value...)
@ -240,19 +235,18 @@ func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) (err error) {
packets.nmsg = 1 packets.nmsg = 1
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
err = syscall.Errno(errno) return syscall.Errno(errno)
return
} }
return return nil
} }
func (b *i2cBus) WriteByteToReg(addr, reg, value byte) (err error) { func (b *i2cBus) WriteByteToReg(addr, reg, value byte) 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.setAddress(addr); err != nil {
return return err
} }
outbuf := [...]byte{ outbuf := [...]byte{
@ -272,19 +266,18 @@ func (b *i2cBus) WriteByteToReg(addr, reg, value byte) (err error) {
packets.nmsg = 1 packets.nmsg = 1
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
err = syscall.Errno(errno) return syscall.Errno(errno)
return
} }
return return nil
} }
func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) (err error) { func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) 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.setAddress(addr); err != nil {
return return err
} }
outbuf := [...]byte{ outbuf := [...]byte{
@ -305,9 +298,8 @@ func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) (err error) {
packets.nmsg = 1 packets.nmsg = 1
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 { if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
err = syscall.Errno(errno) return syscall.Errno(errno)
return
} }
return return nil
} }