i2ctoit

How to use 16-bit register addresses on the I2C bus in Toit?


When using Toit's Registers for I2C devices, register addresses are only 8 bit.

I have a device that uses 16-bit addresses. How can I communicate with it?


Solution

  • When using 16-bit addresses, one has to use the I2C device directly. That class has write-address and read-address functions that take the address as byte-array.

    You can then use helper functions like the following:

    read-register16 address/int size/int -> ByteArray:
      return device.read-address #[address >> 8, address & 0xff] size
    
    write-register16 address/int bytes/ByteArray:
      device.write-address #[address >> 8, address & 0xff] bytes