pkg/micropython: incorporate port#22350
Conversation
Confirmation from my side on the relicense! |
Murdock results❌ FAILED aaea3a3 fixup! pkg/micropython: incorporate port
Build failures (1)
Artifacts |
|
@crasbe Thanks again for the review, I will process it tonight and start testing on actual hardware. |
This is version c7e947df88cf5b05f9e20dbeb10d5b4a9eae65a6 from https://github.com/kaspar030/micropython, upgraded to work with v1.28. Then, the code was modified to use built-in alternatives, that are now possible. License headers were added where missing. MIT licenses have been replaced with LGPL versions: these files started off as copies of existing ports, but they are significantly modified for RIOT-OS and they are not part of (a potential upstream of) RIOT-OS to MicroPython. Assisted-by: Claude Code Opus 4.8
- Replace mp_do_str with built-in alternative - Fix and extens tests
413d637 to
a6f6032
Compare
|
I was able to run the MicroPython test suite on the board, and it is looking promising. I could even remove the patch to slow down the serial communication, probably due to the stdin module that is now used. (out of 469 tests). For comparison, the official MicroPython port for the Raspberry Pi Pico had 9 failing tests (out of 925 tests - more hardware related tests). Will look into what I can fix in this PR, given that the previous version wasn't perfect either. |
|
@crasbe I still have to deal with all the defines and includes. Not sure how to handle that neatly, yet. |
|
Down to only 2 failed tests, and even more additional tests enabled. I did have to enable additional modules to make the basic set of tests pass. I'll find a balance between code size and feature parity. |
| #endif | ||
| #define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hw_spi_make_new | ||
| #ifdef CONFIG_NETWORKING | ||
| #define MICROPY_PY_UERRNO (1) |
There was a problem hiding this comment.
| #define MICROPY_PY_UERRNO (1) | |
| # define MICROPY_PY_UERRNO (1) |
| ## MicroPython modules | ||
|
|
||
| Currently, the port enables only a subset of the available MicroPython | ||
| modules. See `contrib/mpconfigport.h` for details. |
There was a problem hiding this comment.
Since you activated more modules, this can probably be more specific 🤔
| You can import these modules and then use `help(<module name>)` to get more | ||
| information. |
There was a problem hiding this comment.
>>> import riot
import riot
>>> help(riot)
help(riot)
object <module 'riot'> is of type module
__name__ -- riot
thread_getpid -- <function>
>>> import xtimer
import xtimer
>>> help(xtimer)
help(xtimer)
object <module 'xtimer'> is of type module
__name__ -- xtimer
xtimer -- <class 'xtimer'>
Is that how it's supposed to work? Not super helpful 👀
There was a problem hiding this comment.
Added __doc__, but it will still be limited.
>>> help(xtimer)
object <module 'xtimer'> is of type module
__name__ -- xtimer
__doc__ -- RIOT xtimer module - microsecond hardware timers
xtimer -- <class 'xtimer'>
>>> help(machine)
object <module 'umachine'> is of type module
__name__ -- umachine
__doc__ -- RIOT machine module - low-level hardware access (reset, Pin, SPI, ADC)
reset -- <function>
reset_cause -- <function>
Pin -- <class 'Pin'>
SPI -- <class 'SPI'>
ADC -- <class 'ADC'>
There was a problem hiding this comment.
For comparison:
>>> help(math)
object <module 'math'> is of type module
__name__ -- math
e -- 2.7182818
pi -- 3.1415928
sqrt -- <function>
...
trunc -- <function>
radians -- <function>
degrees -- <function>
| # Ensure the directory exists before any other package compiles with this INCLUDES | ||
| $(shell mkdir -p $(BINDIR)/pkg-build/micropython) |
There was a problem hiding this comment.
I think this should live in the pkg/micropython/Makefile, not the pkg/micropython/Makefile.include 🤔
|
MicroPython's own README.md stick with a minimum of 256 KiB and 16 KiB of RAM. Current builds for Cortex M0-4 range somewhere between 160-190 KiB. Original port tried to squeeze it in 128 KiB, but that was some years ago. I favor for a good first impression. MicroPython's own test suite is quite broad, but I can now pass almost all tests. Maybe in a follow-up version, we can do something like LWIP does, to more selectively enable/disable MicroPython modules. Then it should definitely be possible to shave many kilobytes off. |
|
I hit a small roadblock: the ISR part. The original port mentioned that xtimer callbacks (e.g. The 'fix' is using the scheduling module. The ISR schedules work, so the main loop handles this later. This is a bit problematic: the main loop in REPL mode will block on reading from stdin. During regular byte code execution, work is automatically picked up. For the REPL mode, I need to set a read timeout, then handle the work, and continue. But I hope I can make this somewhat smarter. Oh, and then there is the (synchronous) sleep methods too, which have to deal with interrupts too (or not). |
Contribution description
The MicroPython version was contributed by @kaspar030 in 2019. It is based on ~v1.11 of MicroPython. The latest version available is v1.28, which was release April 2026.
I chatted with @kaspar030 about this port, but he isn't going to update/maintain his version anymore. It was once created with the goal to upstream it (see micropython/micropython#1249). That PR got closed, where the maintainers stated that this port can better live inside RIOT-OS (or, at least not in MicroPython) for maintainability reasons.
This PR basically takes the existing port, upgrades it to be compatible with v1.28, stripped parts that have built-in alternatives nowadays and polished up the documentation. No upstream patches necessary (anymore). No new features have been added/enabled.
Most files did not have the proper copyright and doxygen headers. Files without copyright headers have the default LGPL 2.1 license added (discussed with @kaspar030). @bergzand contributed ADC and SPI support, and I got his permission to relicense it from MIT (MicroPython's default license) to LGPL 2.1. That leaves a few files that have started off as copies from other ports, but changed so significantly that I replaced these licenses with LGPL 2.1 too (
modmachine.c,machine_adc.candmpconfigport.h).My ultimate use-case is embedding MicroPython into my KNX actors, as a 'logic engine'. Whether that will work out or not: the current state is old and won't see any updates. This PR makes it at least more vanilla to work on.
Testing procedure
Compile
make -C examples/lang_support/community/micropython, then run the test withmake -C examples/lang_support/community/micropython test.I have yet to test on actual hardware, but so far so good.
Test output
Issues/PRs references
#22306 highlighted an issue when trying to compile the MicroPython example. MicroPython was still compiled with
-std=gnu99, which does not supportstatic_assert(..). This assertion was added to checkCONFIG_*values in headers. This triggered me to get started on this.Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are: