https://toitlang.org/ logo
I2C read from 16bit register address
# help
i
I want to implement the driver for the VL53L4CD ToF Sensor - I did this already in the past and somehow they have larger register addresses then usual. For example the register of the device ID is
0x010F
but if I try to read it with toit I get the following error
Copy code
******************************************************************************
Decoding by `jag`, device has version <2.0.0-alpha.143>
******************************************************************************
EXCEPTION error.
INVALID_ARGUMENT
  0: i2c-read-reg_             <sdk>\i2c.toit:314:3
  1: Bus.read-reg_             <sdk>\i2c.toit:115:10
  2: Device.read-reg           <sdk>\i2c.toit:242:17
  3: Device.read-reg           <sdk>\i2c.toit:235:12
  4: Registers.read-bytes      <sdk>\i2c.toit:291:20
  5: Registers.read-u16-be     <sdk>\serial\registers.toit:83:14
  6: VL53L4CD-DRIVER.get-identification-model-id toit\vl53l4cd-driver.toit:25:22
  7: VL53L4CD.get-id           toit\vl53l4cd.toit:26:20
  8: main                      toit\examples\05-vl534cd.toit:23:29
******************************************************************************
Is there a way of setting this address or still get to read this vlaue somehow using whats implemented in toit already?
f
I'm pretty sure Toit supports 16 bit addresses. Maybe not through
read-reg
, though. Let me check.
Try
device.read-address #[0x01, 0x0F] <length>
i
that seems to work
can I "cast" this
IDENTIFICATION_MODEL_ID ::= 0x010F
into an ByteArray or do I have to change this constants into ByteArrays? Whats better?
e
Perhaps
Copy code
cast value/int -> ByteArray
  return #[value >> 8, value]
r
Friend, I2C address is either 7 bit or 10 bit. There is no 16 bit I2C address. Please check Philips/NXP documents. either 0x7F or 0x3FF. Your device ID is 0x10F 10 bit address.
f
Register addresses can be off different sizes, though. Since accessing registers is a common operation Toit has a direct function for it. However, the
read-reg
function only takes 8-bit addresses. For accessing registers with 16 (or more) bits one needs to use
read-address
.
i
thats true for the I2C Address - but I am talking about the register address to read or write to a specific register of a device with its 7 or 10 bit address.
is there an equivalent in toit for this micropython struct https://docs.micropython.org/en/latest/library/struct.html ?
I wrote my own now but I was wondering if there might be something like that already
Copy code
import binary show LITTLE-ENDIAN BIG-ENDIAN

pack-16 value -> ByteArray:
  buffer := ByteArray 2
  BIG-ENDIAN.put-uint16 buffer 0 value
  return buffer

unpack-16 buffer/ByteArray:
  return BIG-ENDIAN.uint16 buffer 0

pack-32 value -> ByteArray:
  buffer := ByteArray 4
  BIG-ENDIAN.put-uint32 buffer 0 value
  return buffer

unpack-32 buffer/ByteArray:
  return BIG-ENDIAN.uint32 buffer 0
f
Not yet. We have a proposal for "byte classes", but so far we haven't had the time to experiment with/implement it: https://docs.google.com/document/d/1QGW0IJUojA2Wq0oeo7EC15pURMJNnY3kvxGanr9Z3-Y/edit
3 Views