Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upTool to generate native .mpy files from a .elf file (dynamically loadable native code) #5083
+2,257
−0
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
dpgeorge commentedSep 9, 2019
This PR adds a tool
elf2mpy.pyand associated build scripts/headers to build .mpy files from C source code. Such .mpy files can then be dynamically imported as though they were a normal Python module, even though they are implemented in C.Included in this PR are some examples: ex0, ex1 and ex2 are basic examples, and the qr example shows how to take an existing C library "off the shelf" and wrap it up into a .mpy file (to generate QR codes, work done in collaboration with @mattytrentini ).
Currently the tool supports targets of unix x86-64, ARM Thumb2 (Cortex M3 and above, eg stm32 boards), and partial support for esp32. esp32 support is non trivial and the qr example does not work (at this stage I'm unsure why). esp32 support relies on #5082
To try this PR out do the following:
tools/elf2mpy/ex0directoryMakefileto select the architecture (unix, stm32, esp32)makeex0.mpyfile to the boardimport ex0ex0.foo(1, 2)(should return 4)ex0.factorial(5)(should return 120)Then try out ex1, ex2 and qr. Also use one of these examples as a template to write your own C-based module.
There's still a bit of work to do to clean this up (eg tool/file naming, directory structure, docs). Also freezing the resulting mpy files won't work at the moment because the code relocates itself (writes to itself) when it is imported, and frozen code is read-only. Eventually this can be fixed.