Skip to content

Commit bbab4b5

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

File tree

11 files changed

+1311
-279
lines changed

11 files changed

+1311
-279
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ jobs:
244244
- export IDF_PATH=$(pwd)/esp-idf
245245
script:
246246
- make ${MAKEOPTS} -C mpy-cross
247-
- git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V3 :=" ports/esp32/Makefile | cut -d " " -f 3)
247+
- git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V3 :=" ports/esp32/esp32-common.mk | cut -d " " -f 3)
248248
- git -C esp-idf submodule update --init components/json/cJSON components/esp32/lib components/esptool_py/esptool components/expat/expat components/lwip/lwip components/mbedtls/mbedtls components/micro-ecc/micro-ecc components/nghttp/nghttp2 components/nimble components/bt
249249
- make ${MAKEOPTS} -C ports/esp32 submodules
250250
- make ${MAKEOPTS} -C ports/esp32
@@ -261,7 +261,7 @@ jobs:
261261
- export IDF_PATH=$(pwd)/esp-idf
262262
script:
263263
- make ${MAKEOPTS} -C mpy-cross
264-
- git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V4 :=" ports/esp32/Makefile | cut -d " " -f 3)
264+
- git -C esp-idf checkout $(grep "ESPIDF_SUPHASH_V4 :=" ports/esp32/esp32-common.mk | cut -d " " -f 3)
265265
- git -C esp-idf submodule update --init components/bt/controller/lib components/bt/host/nimble/nimble components/esp_wifi/lib_esp32 components/esptool_py/esptool components/lwip/lwip components/mbedtls/mbedtls
266266
- make ${MAKEOPTS} -C ports/esp32 submodules
267267
- make ${MAKEOPTS} -C ports/esp32

examples/natmod/esp32-heap/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 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+
}

0 commit comments

Comments
 (0)