I2C from machine vs I2C from pyb

Great, thanks a lot!

Know this is a few months old now but did the machine.I2C file for hardware i2c ever get formally incorporated into the firmware? I could use it since I am porting the Pololu vl53l0x library to micropython as we speak.

Thanks
Mike

Ibrahim will know…

cagiva

Did you ever get the code working with the vl53l0x? I am just about ready to give up. I tried modifying the pololu code but having problems so I gave modifying the vl6810 code. Only returns 30mm.
tofSensorTest.py (5.92 KB)

Um, is the I2C not working? I use a bus probe to verify stuff like this.

I know the pyb.I2C class works. There are a few examples with the IDE.

Think bus is working. I do a i2c.scan and it sees the device a the correct address. Pretty sure it the implementation of the api that I did. The pololu conversion just hangs the system, nothing gets printed even when I do a i2c.scan() and it eventually disconnects from the usb. I am attaching it for reference. Not sure what is happening so I can’t even debug it. I know I should be ustruct to pack and unpack but I just can’t get around the formatting piece of it yet.

Mike
tofSensorTest2.py (43.6 KB)

There’s really too much code in that file… maybe, roll some simple I2C accesses by hand to check what’s working first?

Sorry, I can’t really help you as I don’t have that sensor.

I took your suggestion and start putting coding it in small chucks. Besides having numerous syntax errors that weren’t picked up, as well as just plain errors on my part, I kept having a problem with the following error on and off:

MemoryError: allocated xxx bytes
MicroPython v1.8-4376-g4de7fa54 on 2017-03-26; OPENMV3 with STM32F765
Type “help()” for more information

Managed to get rid of it by deleting most of the comments and reduced indents. In each case it appeared I had about 25k free space when I did a mem_free.

Got it running so it gives me a constant value like the class but I have to check I did coded the addresses correctly. Couldn’t sleep - so I worked on this all night.

Thanks
Mike

Yeah, there’s some script complexity limit with MP but I’m not quite sure on the details. Ibrahim would know more.

I was doing some googling and found that this appears to be a known issue with MP. Other people were having the same issues. Not sure if it was ever fixed though.

I am having a bit of a challenge in converting a few lines of code to python. Was wondering if you could help me out here:

  uint8_t ref_spad_map[6];   -----------------------------------> Have this defined as a bytearray(6)
  readMulti(GLOBAL_CONFIG_SPAD_ENABLES_REF_0, ref_spad_map, 6);  --------------> using readmem_into to get this

 can't figure out the following lines:
  for (uint8_t i = 0; i < 48; i++)
  {
    if (i < first_spad_to_enable || spads_enabled == spad_count)
    {
      // This bit is lower than the first one that should be enabled, or
      // (reference_spad_count) bits have already been enabled, so zero this bit
      ref_spad_map[i / 8] &= ~(1 << (i % 8));
    }
    else if ((ref_spad_map[i / 8] >> (i % 8)) & 0x1)
    {
      spads_enabled++;
    }
  }
for i in range(48): # i = 0 ... 47
    if (i < first_spad_to_enable) or (spads_enabled == spad_count):
        ref_spad_map[i / 8] &= ~(1 << (i % 8))
    elif ((ref_spad_map[i / 8] >> (i % 8)) & 0x1):
        spads_enabled += 1

?

You want i//8 not i/8 though. Or just i >> 3.

Thanks all. Knew it could be that easy. Stil working on getting the pololu library working. But in the mean time I found a much shorter version to read the distances from the sensor on GitHub, GitHub - popunder/VL53L0X: Tools to interface ToF ranging sensor from ST Microelectronics using the smbus. Converted to machine.i2c and seems to work for single mode operation. Anyway, I am attaching it in case anyone else comes across this thread.
tofSensorTest4.py (3.24 KB)

tofSensorTest3.py (16 KB)
All,
I want to thank everyone for their help with the vl53. I actually got the Pololu library ported over to micropython but while it is running it says I only have 15K free mem. Nothing left to do anything else. If you would all like I can post it here for any brave souls that may want to make it work better.

Mike

Updated. I added the file here.

Hi,

I’m trying to use your code to get the VL53L1X to work on a PyCom board.
Getting errors as below. Is there anybody that can shed some light onto this?

Traceback (most recent call last):
  File "<stdin>", line 418, in <module>
  File "<stdin>", line 16, in __init__
  File "<stdin>", line 32, in init
  File "<stdin>", line 178, in getSpadInfo
TypeError: function takes 3 positional arguments but 4 were given

I did alter the import pyb module into:

from machine import I2C, Pin

Any tips or anything?

Kindest regards,

Stijn

I mean, you have to re-write the driver code. It shouldn’t be that hard. Just fix each error one at a time. You just need to read the code and fix it.

Hi,

Well, I guess that there is some need of alteration needed. But I’m not that skilled.
I tried to figure all the values out, but don’t seem to get my head around it.
Up till now I haven’t found what this 4th argument is nor where it comes from.

If I Use your example, I get the errors as described earlier.
I’ve got an other example aswell that throws a timeout error.
The code is practically the same, only different errors and on the same lines. Spad_info.

Mostly it comes down to this error.

Traceback (most recent call last):
  File "<stdin>", line 418, in <module>
  File "<stdin>", line 16, in __init__
  File "<stdin>", line 32, in init
  File "<stdin>", line 178, in getSpadInfo
TypeError: function takes 3 positional arguments but 4 were given

I got it to work by altering some things but mainly I used this library: https://github.com/openmv/openmv/blob/m … vl53l1x.py
Still need to figure out the different configurations, but thats an issue of trial and error.