From 605493d50877c41776faddd447dfddbd78f97647 Mon Sep 17 00:00:00 2001 From: Christian Walther Date: Wed, 3 May 2023 22:45:43 +0200 Subject: [PATCH] examples/embedding-full: Add example user C module. This works out of the box in the embed port. Signed-off-by: Christian Walther --- examples/embedding-full/Makefile | 2 +- examples/embedding-full/main.c | 3 +++ examples/embedding-full/micropython_embed.mk | 3 +++ .../modules/c_hello/micropython.mk | 2 ++ .../modules/c_hello/modc_hello.c | 23 +++++++++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 examples/embedding-full/modules/c_hello/micropython.mk create mode 100644 examples/embedding-full/modules/c_hello/modc_hello.c diff --git a/examples/embedding-full/Makefile b/examples/embedding-full/Makefile index 3e696857fd..b0ce3daf14 100644 --- a/examples/embedding-full/Makefile +++ b/examples/embedding-full/Makefile @@ -14,7 +14,7 @@ CFLAGS += -I$(EMBED_DIR) CFLAGS += -I$(EMBED_DIR)/port CFLAGS += -Wall -Og -fno-common -SRC += main.c mphal.c +SRC += main.c mphal.c modules/c_hello/modc_hello.c SRC += $(wildcard $(EMBED_DIR)/*/*.c) # Filter out lib because the files in there cannot be compiled separately, they # are #included by other .c files. diff --git a/examples/embedding-full/main.c b/examples/embedding-full/main.c index a24760afdb..73d7ebf45f 100644 --- a/examples/embedding-full/main.c +++ b/examples/embedding-full/main.c @@ -37,6 +37,9 @@ static const char *example_2 = "import frozenhello\n" "help(frozenhello)\n" "print('frozenhello.hello():', frozenhello.hello())\n" + "import c_hello\n" + "help(c_hello)\n" + "print('c_hello.hello():', c_hello.hello())\n" "\n" "print('finish')\n" ; diff --git a/examples/embedding-full/micropython_embed.mk b/examples/embedding-full/micropython_embed.mk index ee96a5f486..d4e76b609b 100644 --- a/examples/embedding-full/micropython_embed.mk +++ b/examples/embedding-full/micropython_embed.mk @@ -11,5 +11,8 @@ EMBED_EXTRA = extmod # Freeze Python modules. FROZEN_MANIFEST ?= manifest.py +# Add C modules. +USER_C_MODULES = modules + # Include the main makefile fragment to build the MicroPython component. include $(MICROPYTHON_TOP)/ports/embed/embed.mk diff --git a/examples/embedding-full/modules/c_hello/micropython.mk b/examples/embedding-full/modules/c_hello/micropython.mk new file mode 100644 index 0000000000..7d610d1edd --- /dev/null +++ b/examples/embedding-full/modules/c_hello/micropython.mk @@ -0,0 +1,2 @@ +C_HELLO_MOD_DIR := $(USERMOD_DIR) +SRC_USERMOD_C += $(C_HELLO_MOD_DIR)/modc_hello.c diff --git a/examples/embedding-full/modules/c_hello/modc_hello.c b/examples/embedding-full/modules/c_hello/modc_hello.c new file mode 100644 index 0000000000..548c7a3349 --- /dev/null +++ b/examples/embedding-full/modules/c_hello/modc_hello.c @@ -0,0 +1,23 @@ +// Check examples/usercmodule for more info about how this works. + +#include "py/runtime.h" +#include "py/objstr.h" + +static mp_obj_t c_hello_hello() { + static const MP_DEFINE_STR_OBJ(hello_str, "Hello from C!"); + return MP_OBJ_FROM_PTR(&hello_str); +} +static MP_DEFINE_CONST_FUN_OBJ_0(c_hello_hello_obj, c_hello_hello); + +static const mp_rom_map_elem_t c_hello_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_c_hello) }, + { MP_ROM_QSTR(MP_QSTR_hello), MP_ROM_PTR(&c_hello_hello_obj) }, +}; +static MP_DEFINE_CONST_DICT(c_hello_module_globals, c_hello_module_globals_table); + +const mp_obj_module_t c_hello_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t *)&c_hello_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_c_hello, c_hello_module);