This section describes functions which read and write to an I2C bus. Currently, these functions are available only on Linux computers.
Open an I2C bus.
id = i2copen id = i2copen(path)
Without input argument, i2copen opens the first I2C bus (/dev/i2c/0 on Linux). It returns a number which must be used with all the other I2C functions.
i2copen(path) opens the I2C bus associated with the specified device path, such as /dev/i2c/1.
Close an I2C bus.
i2cclose(id)
i2cclose(id) closes the I2C bus which has been opened with i2copen. Its argument must be the number returned by i2copen. I2C busses left open are closed automatically when LME is terminated.
Read a word from an I2C bus.
value = i2cread(id, slaveaddr, regaddr) value = i2cread(id, slaveaddr, regaddr, precision)
i2cread(id,slaveaddr,regaddr) reads the 8-bit register regaddr of device slaveaddr on the I2C bus identified by id. The result is of type uint8.
i2cread(id,slaveaddr,regaddr,precision) reads a register whose size, signedness and endianness is specified in string precision. The following values are accepted:
| precision | meaning |
|---|---|
| int8 | signed 8-bit integer (-128 |
| int16 | signed 16-bit integer (-32768 |
| int32 | signed 32-bit integer (-2147483648 |
| uint8 | unsigned 8-bit integer (0 |
| uint16 | unsigned 16-bit integer (0 |
| uint32 | unsigned 32-bit integer (0 |
By default, multibyte words are encoded with the least significant byte first (little endian). The characters ';b' can be appended to specify that they are encoded with the most significant byte first (big endian) (for symmetry, ';l' is accepted and ignored).
id = i2copen; version = i2cread(id, 0x62, 0x00); position = i2cread(id, 0x62, 0x10, 'uint32'); speed = i2cread(id, 0x62, 0x20, 'int16'); i2cclose(id);
Write a word to an I2C bus.
i2cwrite(id, slaveaddr, regaddr, value) i2cwrite(id, slaveaddr, regaddr, value, precision)
i2cwrite(id,slaveaddr,regaddr,value) writes the 8-bit word value to the register regaddr of device slaveaddr on the I2C bus identified by id. value is converted to an 8-bit integer if necessary.
i2cwrite(id,slaveaddr,regaddr,value,precision) writes the word value with size and endianness specified in string precision. See i2cread for a list of supported values. Signedness is irrelevant and ignored.