8000 esp32/mp_port_fun_table: expose esp-idf to native modules · micropython/micropython@5bde2b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bde2b9

Browse files
committed
esp32/mp_port_fun_table: expose esp-idf to native modules
1 parent 4a15c62 commit 5bde2b9

File tree

9 files changed

+1009
-26
lines changed

9 files changed

+1009
-26
lines changed

examples/natmod/esp32-heap/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+ 8000
# Location of top-level MicroPython directory
2+
MPY_DIR = $(abspath ../../..)
3+
4+
# Name of module
5+
MOD = esp32heap
6+
7+
# Source files (.c or .py)
8+
SRC = features0.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11+
ARCH = xtensawin
12+
13+
PORT = esp32
14+
15+
# Location of MicroPython source tree
16+
TOP := $(abspath ../../..)
17+
18+
# Espressif ESP-IDF path
19+
IDF_PATH ?= $(abspath $(TOP)/../esp-idf-micropython)
20+
21+
# xtensa toolchain bin dir
22+
PATH := $(abspath $(TOP)/../xtensa-esp32-elf-8.2.0/bin):$(PATH)
23+
24+
# Board to get correct ESP-IDF config
25+
BOARD ?= GENERIC
26+
27+
# Include to get the rules for compiling and linking the module
28+
include $(MPY_DIR)/py/dynruntime.mk
29+
include $(MPY_DIR)/ports/$(PORT)/dynruntime.mk
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* This example demonstrates the following features in a native module:
2+
- defining a simple function exposed to Python
3+
- defining a local, helper C function
4+
- getting and creating integer objects
5+
*/
6+
7+
// Include the header file to get access to the MicroPython API
8+
#include "py/dynruntime.h"
9+
// Include esp-idf include files
10+
#include "esp_system.h"
11+
12+
// This is the function which will be called from Python, as free_heap()
13+
STATIC mp_obj_t free_heap() {
14+
// calling an ESP-IDF function
15+
uint32_t f = esp_get_free_heap_size();
16+
// sample debug printf
17+
mp_printf(&mp_plat_print, "esp_get_free_heap_size returned %d\n", f);
18+
// return integer as MP small int object
19+
return mp_obj_new_int(f);
20+
}
21+
// Define a Python reference to the function above
22+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(free_heap_obj, free_heap);
23+
24+
// This a function that only exists in esp-idf-v4, it will raise in esp-idf-v3.
25+
STATIC mp_obj_t flash_encryption_mode() {
26+
// calling an ESP-IDF function that doesn't exist in v3
27+
extern uint32_t esp_get_flash_encryption_mode();
28+
uint32_t f = esp_get_flash_encryption_mode();
29+
// return integer as MP small int object
30+
return mp_obj_new_int(f);
31+
}
32+
// Define a Python reference to the function above
33+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(flash_encryption_mode_obj, flash_encryption_mode);
34+
35+
// This is the entry point and is called when the module is imported
36+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
37+
// This must be first, it sets up the globals dict and other things
38+
MP_DYNRUNTIME_INIT_ENTRY
39+
40+
// Make the functions available in the module's namespace
41+
mp_store_global(MP_QSTR_free_heap, MP_OBJ_FROM_PTR(&free_heap_obj));
42+
mp_store_global(MP_QSTR_flash_encryption_mode, MP_OBJ_FROM_PTR(&flash_encryption_mode_obj));
43+
44+
// This must be last, it restores the globals dict
45+
MP_DYNRUNTIME_INIT_EXIT
46+
}

ports/esp32/Makefile

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1+
# get the directory where this makefile resides
2+
# fails if the path has a space :-(, if that happens define PORT_DIR in the parent makefile
3+
PORT_DIR ?= $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
4+
PORT_DIR := $(PORT_DIR)
5+
$(info PORT_DIR=$(PORT_DIR))
6+
7+
# if the parent makefile defines MPY_DIR then use that also for TOP (normally generated in mkenv.mk)
8+
ifneq ($(MPY_DIR),)
9+
TOP ?= $(MPY_DIR)
10+
endif
11+
112
# Select the board to build for: if not given on the command line,
213
# then default to GENERIC.
314
BOARD ?= GENERIC
415

516
# If the build directory is not given, make it reflect the board name.
617
BUILD ?= build-$(BOARD)
718

8-
BOARD_DIR ?= boards/$(BOARD)
19+
BOARD_DIR ?= $(PORT_DIR)/boards/$(BOARD)
920
ifeq ($(wildcard $(BOARD_DIR)/.),)
1021
$(error Invalid BOARD specified: $(BOARD_DIR))
1122
endif
1223

13-
include ../../py/mkenv.mk
24+
include $(PORT_DIR)/../../py/mkenv.mk
1425

1526
# Optional (not currently used for ESP32)
16-
-include mpconfigport.mk
27+
-include $(PORT_DIR)/mpconfigport.mk
1728

1829
ifneq ($(SDKCONFIG),)
1930
$(error Use the BOARD variable instead of SDKCONFIG)
@@ -129,6 +140,7 @@ include $(TOP)/extmod/nimble/nimble.mk
129140
endif
130141

131142
# include sdkconfig to get needed configuration values
143+
SDKCONFIG := $(addprefix $(PORT_DIR)/, $(SDKCONFIG))
132144
include $(SDKCONFIG)
133145

134146
################################################################################
@@ -253,6 +265,32 @@ CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH=1
253265
CFLAGS_MOD += -DMICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE=1
254266
endif
255267

268+
################################################################################
269+
# Generate sdkconfig.h from sdkconfig
270+
271+
$(SDKCONFIG_COMBINED): $(SDKCONFIG)
272+
$(Q)$(MKDIR) -p $(dir $@)
273+
$(Q)$(CAT) $^ > $@
274+
275+
$(SDKCONFIG_H): $(SDKCONFIG_COMBINED)
276+
$(ECHO) "GEN $@"
277+
$(Q)$(MKDIR) -p $(dir $@)
278+
$(Q)$(PYTHON) $(ESPIDF)/tools/kconfig_new/confgen.py \
279+
--output header $@ \
280+
--config $< \
281+
--kconfig $(ESPIDF)/Kconfig \
282+
--env "IDF_TARGET=esp32" \
283+
--env "IDF_CMAKE=n" \
284+
--env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \
285+
--env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \
286+
--env "IDF_PATH=$(ESPIDF)"
287+
$(Q)touch $@
288+
289+
$(HEADER_BUILD)/qstrdefs.generated.h: $(SDKCONFIG_H) $(BOARD_DIR)/mpconfigboard.h
290+
291+
# The rest of this Makefile is not needed to build dynamic native modules
292+
ifndef MPY_DYNRUNTIME
293+
256294
# these flags are common to C and C++ compilation
257295
CFLAGS_COMMON = -Os -ffunction-sections -fdata-sections -fstrict-volatile-bitfields \
258296
-mlongcalls -nostdlib \
@@ -354,6 +392,7 @@ SRC_C = \
354392
mpthreadport.c \
355393
machine_rtc.c \
356394
machine_sdcard.c \
395+
portnativeglue.c \
357396
$(wildcard $(BOARD_DIR)/*.c) \
358397
$(SRC_MOD)
359398

@@ -387,29 +426,6 @@ SRC_QSTR += $(SRC_C) $(EXTMOD_SRC_C) $(LIB_SRC_C) $(DRIVERS_SRC_C)
387426
# Append any auto-generated sources that are needed by sources listed in SRC_QSTR
388427
SRC_QSTR_AUTO_DEPS +=
389428

390-
################################################################################
391-
# Generate sdkconfig.h from sdkconfig
392-
393-
$(SDKCONFIG_COMBINED): $(SDKCONFIG)
394-
$(Q)$(MKDIR) -p $(dir $@)
395-
$(Q)$(CAT) $^ > $@
396-
397-
$(SDKCONFIG_H): $(SDKCONFIG_COMBINED)
398-
$(ECHO) "GEN $@"
399-
$(Q)$(MKDIR) -p $(dir $@)
400-
$(Q)$(PYTHON) $(ESPIDF)/tools/kconfig_new/confgen.py \
401-
--output header $@ \
402-
--config $< \
403-
--kconfig $(ESPIDF)/Kconfig \
404-
--env "IDF_TARGET=esp32" \
405-
--env "IDF_CMAKE=n" \
406-
--env "COMPONENT_KCONFIGS=$(ESPCOMP_KCONFIGS)" \
407-
--env "COMPONENT_KCONFIGS_PROJBUILD=$(ESPCOMP_KCONFIGS_PROJBUILD)" \
408-
--env "IDF_PATH=$(ESPIDF)"
409-
$(Q)touch $@
410-
411-
$(HEADER_BUILD)/qstrdefs.generated.h: $(SDKCONFIG_H) $(BOARD_DIR)/mpconfigboard.h
412-
413429
################################################################################
414430
# List of object files from the ESP32 IDF components
415431

@@ -777,6 +793,7 @@ $(BUILD)/application.elf: $(OBJ) $(LIB) $(BUILD)/esp32_out.ld $(BUILD)/esp32.pro
777793
$(ECHO) "LINK $@"
778794
$(Q)$(LD) $(LDFLAGS) -o $@ $(APP_LD_ARGS)
779795
$(Q)$(SIZE) $@
796+
$(Q)$(SIZE) -A $@ | egrep iram0.text | sed -e 's/ */: /' -e 's; *[^ ]*$$; of 131072;'
780797

781798
define compile_cxx
782799
$(ECHO) "CXX $<"
@@ -794,6 +811,12 @@ vpath %.cpp . $(TOP)
794811
$(BUILD)/%.o: %.cpp
795812
$(call compile_cxx)
796813

814+
ifeq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH_V3))
815+
$(BUILD)/portnativeglue.o: CFLAGS := $(filter-out -Werror, $(CFLAGS))
816+
else
817+
$(BUILD)/portnativeglue.o: CFLAGS += -Wno-builtin-declaration-mismatch
818+
endif
819+
797820
################################################################################
798821
# Declarations to build the bootloader
799822

@@ -979,3 +1002,5 @@ $(BUILD)/partitions.bin: $(PART_SRC)
9791002
################################################################################
9801003

9811004
include $(TOP)/py/mkrules.mk
1005+
1006+
endif # MPY_DYNRUNTIME

ports/esp32/dynruntime.mk

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Makefile to include after py/dynruntime.mk in order to add esp32-specific rules
2+
# for building native modules that include esp-idf header files and call esp-idf functions.
3+
4+
PORT_DIR := $(MPY_DIR)/ports/esp32
5+
6+
MPY_DYNRUNTIME := 1
7+
include $(PORT_DIR)/Makefile
8+
9+
CFLAGS += -std=gnu99 # override -std=c99 in dynruntime.mk
10+
CFLAGS += $(INC) $(INC_ESPCOMP) $(CFLAGS_MOD)
11+
12+
# Make all module object files depend on sdkconfig
13+
$(SRC_O): $(SDKCONFIG_H)
14+
15+
# Tell mpy_ld to load port-specific function table
16+
PORT_FUN=--portfun $(PORT_DIR)/port_fun.inc

ports/esp32/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#define MICROPY_ENABLE_SCHEDULER (1)
6060
#define MICROPY_SCHEDULER_DEPTH (8)
6161
#define MICROPY_VFS (1)
62+
#define MICROPY_PORT_FUN_TABLE (1)
6263

6364
// control over Python builtins
6465
#define MICROPY_PY_FUNCTION_ATTRS (1)

0 commit comments

Comments
 (0)
0