|
| 1 | +Example of embedding MicroPython in a standlone C application |
| 2 | +============================================================= |
| 3 | + |
| 4 | +This directory contains a (very simple!) example of how to embed a MicroPython |
| 5 | +in an existing C application. |
| 6 | + |
| 7 | +A C application is represented by the file hello-embed.c. It executes a simple |
| 8 | +Python statement which prints to the standard output. |
| 9 | + |
| 10 | + |
| 11 | +Building the example |
| 12 | +-------------------- |
| 13 | + |
| 14 | +Build the example is as simple as running: |
| 15 | + |
| 16 | + make |
| 17 | + |
| 18 | +It's worth to trace what's happening behind the scenes though: |
| 19 | + |
| 20 | +1. As a first step, a MicroPython library is built. This is handled by a |
| 21 | +seperate makefile, Makefile.upylib. It is more or less complex, but the |
| 22 | +good news is that you won't need to change anything in it, just use it |
| 23 | +as is, the main Makefile shows how. What may need editing though is |
| 24 | +MicroPython configuration file. MicroPython is highly configurable, so |
| 25 | +you would need to build a library suiting your application well, while |
| 26 | +not bloating its size. Check the options in the file "mpconfigport.h". |
| 27 | +Included is a copy of "minimal" Unix port, which should be good start |
| 28 | +for minimal embedding. For list of all available options, see py/mpconfig.h. |
| 29 | + |
| 30 | +2. Once the library is built, your application is compiled and linked with |
| 31 | +the MicroPython library produced in the previous step. The main Makefile |
| 32 | +is very simple and shows that changes you would need to do to your |
| 33 | +application's Makefile (or other build configuration) are also simple: |
| 34 | + |
| 35 | +a) You would need to use C99 standard (you're using 15+ years old standard |
| 36 | +already, not a 25+ years old one, right?). |
| 37 | + |
| 38 | +b) You need to provide path to MicroPython's top-level dir, for includes. |
| 39 | + |
| 40 | +c) You need to include -DNO_QSTR compile-time flag. |
| 41 | + |
| 42 | +d) Otherwise, just link with micropython library produced in step 1. |
| 43 | + |
| 44 | + |
| 45 | +Out of tree build |
| 46 | +----------------- |
| 47 | + |
| 48 | +This example set up to work out of the box, being part of the MicroPython |
| 49 | +tree. Your application of course will be outside of its tree, but the |
| 50 | +only thing you need to do is to pass MPTOP variable pointing to |
| 51 | +MicroPython directory to both Makefiles (in this example, the main Makefile |
| 52 | +automatically pass it to Makefile.upylib; in your own Makefile, don't forget |
| 53 | +to use suitable value). |
| 54 | + |
| 55 | +A practical way to embed MicroPython in your application is to include it |
| 56 | +as a git submodule. Suppose you included it as libs/micropython. Then in |
| 57 | +your main Makefile you would have something like: |
| 58 | + |
| 59 | +~~~ |
| 60 | +MPTOP = libs/micropython |
| 61 | + |
| 62 | +my_app: $(MY_OBJS) -lmicropython |
| 63 | + |
| 64 | +-lmicropython: |
| 65 | + $(MAKE) -f $(MPTOP)/examples/embedding/Makefile.upylib MPTOP=$(MPTOP) |
| 66 | +~~~ |
0 commit comments