Page 1 of 1

I2C in new lego hubs [STM32]

Posted: Mon Aug 02, 2021 11:04 am
by domisol
Hi
While I have been using micropython in various platforms (ESP32, ESP8266, STM32 with Lego new hubs), I'm new in trying to compile my own version of micropython or even some specific libraries.
Today I was thinking about I2C implementation I'm missing in lego hubs (Spike or Mindstorms 51515). I miss to be able to connect directly a i2c device directly to the hub, as it should be technically possible.
While it seems that the adequate class was part of the Lego port a few monthes ago: https://www.facebook.com/groups/SPIKEco ... 8433527719
now, there is now i2c nor Pin class in current lego implementation (MicroPython v1.12-1529-gee25066ba on 2021-04-26; LEGO Technic Large Hub with STM32F413xx).
I thought I could try to compile the original i2c library from micropython into a .mpy module, but crosscompilation from C to mpy seems to be unsupported on STM platforms.
So may be someone has developed something to solve the problem ?

Thanks

Jean-David

Re: I2C in new lego hubs [STM32]

Posted: Wed Aug 04, 2021 6:02 am
by jimmo
domisol wrote:
Mon Aug 02, 2021 11:04 am
Today I was thinking about I2C implementation I'm missing in lego hubs (Spike or Mindstorms 51515). I miss to be able to connect directly a i2c device directly to the hub, as it should be technically possible.
While it seems that the adequate class was part of the Lego port a few monthes ago: https://www.facebook.com/groups/SPIKEco ... 8433527719
now, there is now i2c nor Pin class in current lego implementation (MicroPython v1.12-1529-gee25066ba on 2021-04-26; LEGO Technic Large Hub with STM32F413xx).
Unfortunately I don't have access to a Lego device to check for myself, but they use the STM32 port with only a few lego-specific modifications, so machine.I2C etc should just work.

What do you get if you type
>>> dir(machine)
at the REPL?

You might be interested that the support for these devices was just merged upstream -- see https://github.com/micropython/micropyt ... GO_HUB_NO6
domisol wrote:
Mon Aug 02, 2021 11:04 am
I thought I could try to compile the original i2c library from micropython into a .mpy module, but crosscompilation from C to mpy seems to be unsupported on STM platforms.
The I2C support is part of the core firmware, and not provided by .mpy or .py.
domisol wrote:
Mon Aug 02, 2021 11:04 am
current lego implementation (MicroPython v1.12-1529-gee25066ba on 2021-04-26; LEGO Technic Large Hub with STM32F413xx).
I'm surprised this is 1.12, but you can built the latest firmware using the instructions above.

Re: I2C in new lego hubs [STM32]

Posted: Wed Aug 04, 2021 7:25 pm
by domisol
Hi Jimmo,
thanks for all your comments, they are welcome!!!

Code: Select all

>>> dir(machine)
['__class__', 'Timer', 'reset']
Even the Pin class isn't present!

But if I build and install latest firmware, which would be great, I guess I would loose all the Lego functions and libraries?
The logic behind this is that they have pre-instanciated classes (Port, Device, Motor, Motorpair, Pin).

Code: Select all

>>> dir(hub)
['__class__', '__name__', '__version__', 'BACK', 'BOTTOM', 'BT_VCP', 'FRONT', 'Image', 'LEFT', 'RIGHT', 'TOP',
'USB_VCP', 'battery', 'bluetooth', 'button', 'config', 'display', 'file_transfer', 'info', 'led', 'motion', 'port',
'power_off', 'repl_restart', 'reset', 'sound', 'status', 'supervision', 'temperature']

>>> dir(hub.port.A)
['__class__', 'callback', 'device', 'info', 'mode', 'motor', 'pwm']

>>> dir(hub.port.A.motor)
['__class__', 'get', 'BUSY_MODE', 'BUSY_MOTOR', 'EVENT_COMPLETED', 'EVENT_INTERRUPTED', 'FORMAT_PCT', 'FORMAT_RAW',
'FORMAT_SI', 'PID_POSITION', 'PID_SPEED', 'STOP_BRAKE', 'STOP_FLOAT', 'STOP_HOLD',
'brake', 'busy', 'callback', 'default', 'float', 'hold', 'mode', 'pair', 'pid', 'preset', 'pwm', 'run_at_speed', 'run_for_degrees', 'run_for_time', 'run_to_position']

>>> dir(hub.port.A)
['__class__', 'callback', 'device', 'info', 'mode', 'motor', 'pwm']

>>> hub.port.A.mode(hub.port.MODE_GPIO)

>>> dir(hub.port.A)
['__class__', 'callback', 'device', 'info', 'mode', 'motor', 'p5', 'p6', 'pwm']

>>> dir(hub.port.A.p5)
['__class__', 'value', 'direction']
Jean-David

Re: I2C in new lego hubs [STM32]

Posted: Wed Aug 04, 2021 7:36 pm
by domisol
On the hardware side and quoting:
https://antonsmindstorms.com/product/ua ... ompatible/
LEGO smart hubs have internal resistors that mess with pull-up resistors in the most common i2c devices, so they are hard to connect directly
So maybe the problem is also on the hardware side.

Jean-David

Re: I2C in new lego hubs [STM32]

Posted: Fri Aug 06, 2021 3:13 am
by jimmo
domisol wrote:
Wed Aug 04, 2021 7:25 pm
But if I build and install latest firmware, which would be great, I guess I would loose all the Lego functions and libraries?
The logic behind this is that they have pre-instanciated classes (Port, Device, Motor, Motorpair, Pin).
I did some digging into this...

It seems like the factory lego firmware doesn't provide the full machine module -- like you say you only get the "hub" module.

The plan seems to make it behave much more like a regular MicroPython port, which will ultimately get you the full machine module. The GitHub PR I linked to is the first step in that direction.

However, at this stage, if you use the "upstream" firmware (i.e. the board that was just added in that PR):
a) you will no longer have the "hub" module
b) you lose the ability to go back to the factory Lego firmware

apparently both of these things are being actively worked on, so hopefully the situation changes soon. I'm hoping that at some point the Lego app will just get a v1.16 / v1.17 firmware update and it will include all this.

Re: I2C in new lego hubs [STM32]

Posted: Fri Aug 13, 2021 5:19 pm
by domisol
Thanks Jimmo, that's fine!
you lose the ability to go back to the factory Lego firmware
Why? Can't you always reflash the original firmware through the "Minstorms" Lego app ?
For example you can install Pybricks (https://pybricks.com) and go back to the original firmware as I said.

Re: I2C in new lego hubs [STM32]

Posted: Mon Sep 06, 2021 7:20 am
by domisol
Hi Jimmo, Hi others
Sorry to insist, but now I see that v1.17 supports Lego hub.
I want to be sure that I can install it and revert to original Lego firmware.
I don't see any reason why it shouldn't be the case, but your last message saying "you lose the ability to go back to the factory Lego firmware" put in doubt, and makes me worried about that.

Thanks!!!

Jean-David