diff --git a/drivers/i2c/i2c.go b/drivers/i2c/i2c.go index 75cf9211..7a1521c1 100644 --- a/drivers/i2c/i2c.go +++ b/drivers/i2c/i2c.go @@ -103,14 +103,6 @@ func (c *i2cConnection) ReadWordData(reg uint8) (val uint16, err error) { return c.bus.ReadWordData(reg) } -// ReadBlockData reads a block of bytes for a register on the i2c device. -func (c *i2cConnection) ReadBlockData(reg uint8, b []byte) (n int, err error) { - if err := c.bus.SetAddress(c.address); err != nil { - return 0, err - } - return c.bus.ReadBlockData(reg, b) -} - // WriteByte writes a single byte to the i2c device. func (c *i2cConnection) WriteByte(val byte) (err error) { if err := c.bus.SetAddress(c.address); err != nil { diff --git a/drivers/i2c/i2c_test.go b/drivers/i2c/i2c_test.go index b8067373..7a54da2e 100644 --- a/drivers/i2c/i2c_test.go +++ b/drivers/i2c/i2c_test.go @@ -53,12 +53,6 @@ func TestI2CReadWordData(t *testing.T) { gobottest.Assert(t, v, uint16(0)) } -func TestI2CReadBlockData(t *testing.T) { - c := NewConnection(initI2CDevice(), 0x06) - i, _ := c.ReadBlockData(0x01, []byte{}) - gobottest.Assert(t, i, 0) -} - func TestI2CWriteByte(t *testing.T) { c := NewConnection(initI2CDevice(), 0x06) err := c.WriteByte(0x01) @@ -77,8 +71,8 @@ func TestI2CWriteWordData(t *testing.T) { gobottest.Assert(t, err, nil) } -func TestI2CWriteBlockDataErrNotSupported(t *testing.T) { +func TestI2CWriteBlockData(t *testing.T) { c := NewConnection(initI2CDevice(), 0x06) err := c.WriteBlockData(0x01, []byte{0x01, 0x02}) - gobottest.Refute(t, err, nil) + gobottest.Assert(t, err, nil) } diff --git a/sysfs/i2c_device.go b/sysfs/i2c_device.go index a133d9f7..bcf3ed84 100644 --- a/sysfs/i2c_device.go +++ b/sysfs/i2c_device.go @@ -20,17 +20,14 @@ const ( I2C_FUNC_SMBUS_READ_BLOCK_DATA = 0x01000000 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA = 0x02000000 // Transaction types - I2C_SMBUS_BYTE = 1 - I2C_SMBUS_BYTE_DATA = 2 - I2C_SMBUS_WORD_DATA = 3 - I2C_SMBUS_PROC_CALL = 4 - I2C_SMBUS_BLOCK_DATA = 5 - I2C_SMBUS_I2C_BLOCK_DATA = 6 - I2C_SMBUS_BLOCK_PROC_CALL = 7 /* SMBus 2.0 */ - I2C_SMBUS_BLOCK_DATA_PEC = 8 /* SMBus 2.0 */ - I2C_SMBUS_PROC_CALL_PEC = 9 /* SMBus 2.0 */ - I2C_SMBUS_BLOCK_PROC_CALL_PEC = 10 /* SMBus 2.0 */ - I2C_SMBUS_WORD_DATA_PEC = 11 /* SMBus 2.0 */ + I2C_SMBUS_BYTE = 1 + I2C_SMBUS_BYTE_DATA = 2 + I2C_SMBUS_WORD_DATA = 3 + I2C_SMBUS_PROC_CALL = 4 + I2C_SMBUS_BLOCK_DATA = 5 + I2C_SMBUS_I2C_BLOCK_BROKEN = 6 + I2C_SMBUS_BLOCK_PROC_CALL = 7 /* SMBus 2.0 */ + I2C_SMBUS_I2C_BLOCK_DATA = 8 /* SMBus 2.0 */ ) type i2cSmbusIoctlData struct { @@ -45,7 +42,6 @@ type I2cOperations interface { ReadByte() (val uint8, err error) ReadByteData(reg uint8) (val uint8, err error) ReadWordData(reg uint8) (val uint16, err error) - ReadBlockData(reg uint8, b []byte) (n int, err error) WriteByte(val uint8) (err error) WriteByteData(reg uint8, val uint8) (err error) WriteWordData(reg uint8, val uint16) (err error) @@ -129,19 +125,6 @@ func (d *i2cDevice) ReadWordData(reg uint8) (val uint16, err error) { return data, err } -func (d *i2cDevice) ReadBlockData(reg uint8, buf []byte) (n int, err error) { - if d.funcs&I2C_FUNC_SMBUS_READ_BLOCK_DATA == 0 { - return 0, fmt.Errorf("SMBus block data reading not supported") - } - - data := make([]byte, 32+1) // Max message + size as defined by SMBus standard - - err = d.smbusAccess(I2C_SMBUS_READ, reg, I2C_SMBUS_I2C_BLOCK_DATA, uintptr(unsafe.Pointer(&data[0]))) - - copy(buf, data[1:]) - return int(data[0]), err -} - func (d *i2cDevice) WriteByte(val uint8) (err error) { err = d.smbusAccess(I2C_SMBUS_WRITE, val, I2C_SMBUS_BYTE, uintptr(0)) return err @@ -160,21 +143,25 @@ func (d *i2cDevice) WriteWordData(reg uint8, val uint16) (err error) { } func (d *i2cDevice) WriteBlockData(reg uint8, data []byte) (err error) { - if d.funcs&I2C_FUNC_SMBUS_WRITE_BLOCK_DATA == 0 { - return fmt.Errorf("SMBus block data writing not supported") - } - if len(data) > 32 { - data = data[:32] + return fmt.Errorf("Writing blocks larger than 32 bytes (%v) not supported", len(data)) } buf := make([]byte, len(data)+1) - copy(buf[:1], data) - buf[0] = uint8(len(data)) + copy(buf[1:], data) + buf[0] = reg - err = d.smbusAccess(I2C_SMBUS_WRITE, reg, I2C_SMBUS_I2C_BLOCK_DATA, uintptr(unsafe.Pointer(&buf[0]))) + n, err := d.file.Write(buf) - return err + if err != nil { + return err + } + + if n != len(buf) { + return fmt.Errorf("Write to device truncated, %v of %v written", n, len(buf)) + } + + return nil } // Read implements the io.ReadWriteCloser method by direct I2C read operations.