Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tool to generate native .mpy files from a .elf file (dynamically loadable native code) #5083

Open
wants to merge 3 commits into
base: master
from

Conversation

@dpgeorge
Copy link
Member

commented Sep 9, 2019

This PR adds a tool elf2mpy.py and 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:

  • go to the tools/elf2mpy/ex0 directory
  • edit Makefile to select the architecture (unix, stm32, esp32)
  • run make
  • copy the resulting ex0.mpy file to the board
  • run import ex0
  • run ex0.foo(1, 2) (should return 4)
  • run 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.