guild icon
Toit
#I2C read from 16bit register address
Thread channel in help
theHuanter
theHuanter 04/03/2024 01:09 PM
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
****************************************************************************** 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?
theHuanterOPtheHuanter
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...
floitsch
floitsch 04/03/2024 01:22 PM
I'm pretty sure Toit supports 16 bit addresses. Maybe not through read-reg, though. Let me check.
floitsch
floitsch 04/03/2024 01:23 PM
Try device.read-address #[0x01, 0x0F] <length>
👍1
theHuanter
theHuanter 04/03/2024 01:25 PM
that seems to work
👍1
theHuanter
theHuanter 04/03/2024 01:28 PM
can I "cast" this IDENTIFICATION_MODEL_ID ::= 0x010F into an ByteArray or do I have to change this constants into ByteArrays? Whats better?
erikcorry
erikcorry 04/03/2024 01:43 PM
Perhaps
cast value/int -> ByteArray return #[value >> 8, value]
(edited)
👍1
theHuanterOPtheHuanter
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...
RaspPi Parts
RaspPi Parts 04/04/2024 08:15 AM
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.(edited)
🤓1
RaspPi PartsRaspPi Parts
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.(edited)
floitsch
floitsch 04/04/2024 08:18 AM
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.
RaspPi PartsRaspPi Parts
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.(edited)
theHuanter
theHuanter 04/04/2024 08:37 AM
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.
floitschfloitsch
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 take...
theHuanter
theHuanter 04/04/2024 08:38 AM
is there an equivalent in toit for this micropython struct https://docs.micropython.org/en/latest/library/struct.html ?
theHuanter
theHuanter 04/04/2024 08:39 AM
I wrote my own now but I was wondering if there might be something like that already
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
theHuanterOPtheHuanter
is there an equivalent in toit for this micropython struct https://docs.micropython.org/en/latest/library/struct.html ?
floitsch
floitsch 04/04/2024 09:32 AM
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
12 messages in total