8000 examples/embedding-full: Add a more full-featured embedding example. · micropython/micropython@a211d7c · GitHub
[go: up one dir, main page]

Skip to content

Commit a211d7c

Browse files
committed
examples/embedding-full: Add a more full-featured embedding example.
It compiles and runs in this state, but a lot of functionality is still missing, to be extended over the following commits. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent ed962f1 commit a211d7c

File tree

9 files changed

+204
-4
lines changed

9 files changed

+204
-4
lines changed

.github/workflows/examples.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@ jobs:
2323
run: make -C examples/embedding -f micropython_embed.mk && make -C examples/embedding
2424
- name: Run
2525
run: ./examples/embedding/embed | grep "hello world"
26+
27+
embedding-full:
28+
runs-on: ubuntu-latest
29+
steps:
30+
- uses: actions/checkout@v3
31+
- name: Build
32+
run: make -C examples/embedding-full -f micropython_embed.mk submodules && make -C examples/embedding-full -f micropython_embed.mk && make -C examples/embedding-full
33+
- name: Run
34+
run: ./examples/embedding-full/embed

examples/embedding-full/Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
# The MIT License (MIT)
3+
# Copyright (c) 2022-2023 Damien P. George
4+
#
5+
# This is a very simple makefile that demonstrates how to build the embed port.
6+
# All it needs to do is build all *.c files in the micropython_embed directory.
7+
# This makefile would be replaced with your custom build system.
8+
9+
EMBED_DIR = micropython_embed
10+
PROG = embed
11+
12+
CFLAGS += -I.
13+
CFLAGS += -I$(EMBED_DIR)
14+
CFLAGS += -I$(EMBED_DIR)/port
15+
CFLAGS += -Wall -Og
16+
17+
SRC += main.c mphal.c
18+
SRC += $(wildcard $(EMBED_DIR)/*/*.c) $(wildcard $(EMBED_DIR)/*/*/*.c)
19+
OBJ += $(SRC:.c=.o)
20+
21+
$(PROG): $(OBJ)
22+
$(CC) -o $@ $^
23+
24+
clean:
25+
/bin/rm -f $(OBJ) $(PROG)

examples/embedding-full/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Example of embedding MicroPython in a standalone C application (full)
2+
=====================================================================
3+
4+
This directory contains a simple example of how to embed a full-featured
5+
version of MicroPython in an existing C application.
6+
See also _embedding_ for a more minimal version.
7+
8+
A C application is represented here by the file `main.c`. It executes two
9+
simple Python scripts which print things to the standard output.
10+
Functions used by the MicroPython core that need to be provided by the
11+
application are implemented in `mphal.c`.
12+
13+
Building the example
14+
--------------------
15+
16+
First build the embed port using:
17+
18+
$ make -f micropython_embed.mk
19+
20+
This will generate the `micropython_embed` directory which is a self-contained
21+
copy of MicroPython suitable for embedding. The .c files in this directory need
22+
to be compiled into your project, in whatever way your project can do that. The
23+
example here uses make and a provided `Makefile`.
24+
25+
To build the example project, based on `main.c`, use:
26+
27+
$ make
28+
29+
That will create an executable called `embed` which you can run:
30+
31+
$ ./embed
32+
33+
Out of tree build
34+
-----------------
35+
36+
This example is set up to work out of the box, being part of the MicroPython
37+
tree. Your application will be outside of this tree, but the only thing you
38+
need to do for that is to change `MICROPYTHON_TOP` (found in `micropython_embed.mk`)
39+
to point to the location of the MicroPython repository. The MicroPython
40+
repository may, for example, be a git submodule in your project.

examples/embedding-full/main.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* This file is part of the MicroPython project, http://micropython.org/
2+
* The MIT License (MIT)
3+
* Copyright (c) 2022-2023 Damien P. George
4+
*/
5+
6+
#include "port/micropython_embed.h"
7+
#include "py/stackctrl.h"
8+
9+
// This is example 1 script, which will be compiled and executed.
10+
static const char *example_1 =
11+
"print('hello world!', list(x + 1 for x in range(10)), end='eol\\n')";
12+
13+
// This is example 2 script, which will be compiled and executed.
14+
static const char *example_2 =
15+
"for i in range(10):\n"
16+
" print('iter {:08}'.format(i))\n"
17+
"\n"
18+
"try:\n"
19+
" 1//0\n"
20+
"except Exception as er:\n"
21+
" print('caught exception', repr(er))\n"
22+
"\n"
23+
"import gc\n"
24+
"print('run GC collect')\n"
25+
"gc.collect()\n"
26+
"\n"
27+
"print('help(\\'modules\\'):')\n"
28+
"help('modules')\n"
29+
"import sys\n"
30+
"help(sys)\n"
31+
"\n"
32+
"print('finish')\n"
33+
;
34+
35+
// This array is the MicroPython GC heap.
36+
static char heap[8 * 1024];
37+
38+
int main() {
39+
#if MICROPY_STACK_CHECK
40+
// Set the stack limit, otherwise the default is zero and we will end up in
41+
// nlr_jump_fail() immediately.
42+
mp_stack_set_limit(10240);
43+
#endif
44+
// Initialise MicroPython.
45+
mp_embed_init(&heap[0], sizeof(heap));
46+
47+
// Run the example scripts (they will be compiled first).
48+
mp_embed_exec_str(example_1);
49+
mp_embed_exec_str(example_2);
50+
51+
// Deinitialise MicroPython.
52+
mp_embed_deinit();
53+
54+
return 0;
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file is part of the MicroPython project, http://micropython.org/
2+
# The MIT License (MIT)
3+
# Copyright (c) 2022-2023 Damien P. George
4+
5+
# Set the location of the top of the MicroPython repository.
6+
MICROPYTHON_TOP = ../..
7+
8+
# Include the main makefile fragment to build the MicroPython component.
9+
include $(MICROPYTHON_TOP)/ports/embed/embed.mk
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* This file is part of the MicroPython project, http://micropython.org/
2+
* The MIT License (MIT)
3+
* Copyright (c) 2022-2023 Damien P. George
4+
*/
5+
6+
// Include common MicroPython embed configuration.
7+
#include <port/mpconfigport_common.h>
8+
9+
// Use the same starting configuration as on most bare-metal targets.
10+
#define MICROPY_CONFIG_ROM_LEVEL (MICROPY_CONFIG_ROM_LEVEL_EXTRA_FEATURES)
11+
12+
// MicroPython configuration.
13+
#define MICROPY_ENABLE_COMPILER (1)
14+
#define MICROPY_ENABLE_GC (1)
15+
#define MICROPY_PY_GC (1)
16+
17+
#define MICROPY_PY_SYS_PLATFORM "embedded"
18+
19+
// Requires shared/readline/readline.h, don't bother as we have no input.
20+
#define MICROPY_PY_BUILTINS_INPUT (0)
21+
22+
// Can be enabled once extmod/moductypes.c is included in the build.
23+
#define MICROPY_PY_UCTYPES (0)
24+
25+
// Can be enabled once either shared/runtime/sys_stdio_mphal.c or
26+
// extmod/vfs_posix_file.c is included in the build.
27+
#define MICROPY_PY_SYS_STDFILES (0)
28+
29+
// Can be enabled if you provide an implementation of
30+
// mp_hal_set_interrupt_char() in mphal.c.
31+
#define MICROPY_KBD_EXCEPTION (0)

examples/embedding-full/mphal.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* This file is part of the MicroPython project, http://micropython.org/
2+
* The MIT License (MIT)
3+
* Copyright (c) 2022-2023 Damien P. George
4+
*/
5+
6+
#include "py/builtin.h"
7+
#include "py/compile.h"
8+
#include "py/mperrno.h"
9+
10+
#if !MICROPY_VFS
11+
12+
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
13+
mp_raise_OSError(MP_ENOENT);
14+
}
15+
16+
mp_import_stat_t mp_import_stat(const char *path) {
17+
(void)path;
18+
return MP_IMPORT_STAT_NO_EXIST;
19+
}
20+
21+
mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
22+
return mp_const_none;
23+
}
24+
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
25+
26+
#endif

examples/embedding/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
Example of embedding MicroPython in a standalone C application
2-
==============================================================
1+
Example of embedding MicroPython in a standalone C application (minimal)
2+
========================================================================
33

4-
This directory contains a simple example of how to embed MicroPython in an
5-
existing C application.
4+
This directory contains a simple example of how to embed a minimal version of
5+
MicroPython in an existing C application.
6+
See also _embedding-full_ for a more full-featured version.
67

78
A C application is represented here by the file `main.c`. It executes two
89
simple Python scripts which print things to the standard output.

ports/embed/port/mphalport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
// Define so there's no dependency on extmod/virtpin.h
22
#define mp_hal_pin_obj_t
3+
4+
#if MICROPY_KBD_EXCEPTION
5+
void mp_hal_set_interrupt_char(int c);
6+
#endif

0 commit comments

Comments
 (0)
0