2018-06-18 02:29:22 +00:00
|
|
|
Example of embedding MicroPython in a standalone C application
|
|
|
|
==============================================================
|
2016-06-15 23:30:08 +00:00
|
|
|
|
|
|
|
This directory contains a (very simple!) example of how to embed a MicroPython
|
|
|
|
in an existing C application.
|
|
|
|
|
2018-06-18 02:29:22 +00:00
|
|
|
A C application is represented by the file `hello-embed.c`. It executes a simple
|
2016-06-15 23:30:08 +00:00
|
|
|
Python statement which prints to the standard output.
|
|
|
|
|
|
|
|
|
|
|
|
Building the example
|
|
|
|
--------------------
|
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
Building the example is as simple as running:
|
2016-06-15 23:30:08 +00:00
|
|
|
|
|
|
|
make
|
|
|
|
|
|
|
|
It's worth to trace what's happening behind the scenes though:
|
|
|
|
|
|
|
|
1. As a first step, a MicroPython library is built. This is handled by a
|
2018-06-18 02:29:22 +00:00
|
|
|
separate makefile, `Makefile.upylib`. It is more or less complex, but the
|
2016-06-15 23:30:08 +00:00
|
|
|
good news is that you won't need to change anything in it, just use it
|
2018-06-18 02:29:22 +00:00
|
|
|
as is, the main `Makefile` shows how. What may require editing though is
|
2017-02-15 10:12:23 +00:00
|
|
|
a MicroPython configuration file. MicroPython is highly configurable, so
|
2016-06-15 23:30:08 +00:00
|
|
|
you would need to build a library suiting your application well, while
|
2018-06-18 02:29:22 +00:00
|
|
|
not bloating its size. Check the options in the file `mpconfigport.h`.
|
2017-02-15 10:12:23 +00:00
|
|
|
Included is a copy of the "minimal" Unix port, which should be a good start
|
|
|
|
for minimal embedding. For the list of all available options, see
|
2018-06-18 02:29:22 +00:00
|
|
|
`py/mpconfig.h`.
|
2016-06-15 23:30:08 +00:00
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
2. Once the MicroPython library is built, your application is compiled
|
|
|
|
and linked it. The main Makefile is very simple and shows that the changes
|
2018-06-18 02:29:22 +00:00
|
|
|
you would need to do to your application's `Makefile` (or other build
|
2017-02-15 10:12:23 +00:00
|
|
|
configuration) are also simple:
|
2016-06-15 23:30:08 +00:00
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
a) You would need to use C99 standard (you're using this 15+ years old
|
|
|
|
standard already, not a 25+ years old one, right?).
|
2016-06-15 23:30:08 +00:00
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
b) You need to provide a path to MicroPython's top-level dir, for includes.
|
2016-06-15 23:30:08 +00:00
|
|
|
|
2018-06-18 02:29:22 +00:00
|
|
|
c) You need to include `-DNO_QSTR` compile-time flag.
|
2016-06-15 23:30:08 +00:00
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
d) Otherwise, just link with the MicroPython library produced in step 1.
|
2016-06-15 23:30:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
Out of tree build
|
|
|
|
-----------------
|
|
|
|
|
2017-02-15 10:12:23 +00:00
|
|
|
This example is set up to work out of the box, being part of the MicroPython
|
2016-06-15 23:30:08 +00:00
|
|
|
tree. Your application of course will be outside of its tree, but the
|
2018-06-18 02:29:22 +00:00
|
|
|
only thing you need to do is to pass `MPTOP` variable pointing to
|
2016-06-15 23:30:08 +00:00
|
|
|
MicroPython directory to both Makefiles (in this example, the main Makefile
|
2018-06-18 02:29:22 +00:00
|
|
|
automatically passes it to `Makefile.upylib`; in your own Makefile, don't forget
|
2017-02-15 10:12:23 +00:00
|
|
|
to use a suitable value).
|
2016-06-15 23:30:08 +00:00
|
|
|
|
|
|
|
A practical way to embed MicroPython in your application is to include it
|
2018-06-18 02:29:22 +00:00
|
|
|
as a git submodule. Suppose you included it as `libs/micropython`. Then in
|
2016-06-15 23:30:08 +00:00
|
|
|
your main Makefile you would have something like:
|
|
|
|
|
|
|
|
~~~
|
|
|
|
MPTOP = libs/micropython
|
|
|
|
|
|
|
|
my_app: $(MY_OBJS) -lmicropython
|
|
|
|
|
|
|
|
-lmicropython:
|
|
|
|
$(MAKE) -f $(MPTOP)/examples/embedding/Makefile.upylib MPTOP=$(MPTOP)
|
|
|
|
~~~
|