micropython and sys.implementation- different on XIAO SAMD21 and XIAO RP2040

224 views Asked by At

I was working with micropython on a SEEED XIAO (SAMD21) using version 1.18, and wanted to use 'compiled' libraries (.mpy) to conserve space. I had mpy-cross for version 5 which worked okay for me. I also wanted to have floating point so I built a new micropython with that option- from the latest (20220302) github version- and noticed that mpy-cross had updated to version 6. I thought I would check to see which versions of .mpy were used The micropython documentation suggests some code to check the version of .mpy using the sys.implementation object. I noticed a variety of sys.implementation implementations.

Version 1.18- released by micropython 20220117 on the XIAO SAMD21 gave the following results:

MicroPython v1.18 on 2022-01-17; Seeed Xiao with SAMD21G18A
Type "help()" for more information.
>>> import sys
>>> sys.implementation
('micropython', (1, 18, 0))
>>> type(sys.implementation)
<class 'tuple'>
>>> 

This returned a tuple with 2 items- the name and a tuple with the version info. But no information on the mpy version.

I then tried the released 1.18 version for the RP2040 NANO (and ran it on my XIAO RP2040):

MPY: soft reboot
MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> import sys
>>> sys.implementation
(name='micropython', version=(1, 18, 0), mpy=5637)
>>> type(sys.implementation)
<class 'tuple'>
>>> 

Here the result returned is tuple with 3 attributes- named attributes- and a mpy version (5637 & 0xff = 5). I then recompiled micropython using the #define MICROPY_PERSISTENT_CODE_LOAD (1) option to try and match the RP2040 output:

MPY: soft reboot
MicroPython v1.18-169-g665f0e2a6-dirty on 2022-03-02; Seeed Xiao with SAMD21G18A
Type "help()" for more information.
>>> import sys
>>> sys.implementation
('micropython', (1, 18, 0), 6)
>>> type(sys.implementation)
<class 'tuple'>
>>> 

Now a tuple with 3 items is returned with the mpy version (6), but not a named tuple, and no extra data with the mpy version.

My question is how can I get consistent results in my XIAO SAMD2 implementation? Which option should I include or exclude? Or should I not worry it? I am studying whether to switch to Python or stay with C(++) for this small processor - leaning to C(++).

Thank you

1

There are 1 answers

2
Jos Verlinde On BEST ANSWER

If you want to use precompiled/bytecode .mpy files then you will need to Decide to use either bytecode or compile to native code for your mcu platforms.

For bytecode you will need to distribute a bytecode version that matches the runtime firmware, 1.12-1.18 == 5, next version will increase

For native code you'll need to distribute versions for each platform that you want to support. An esp32c3 cannot run stm32 native code and vice-versa

Also the different ports/board will indeed have differences in some of the stdlib APIs. Your code must be robust enough to handle these differences.