8000 esp32: add dynamic linking of mp_port_fun_table · micropython/micropython@80c75f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 80c75f2

Browse files
committed
esp32: add dynamic linking of mp_port_fun_table
esp32: add auto-generation of mp_port_fun_table from esp-idf docs esp32: add makefiles to compile native modules with esp-idf includes esp32: add esp32heap example
1 parent fc111c1 commit 80c75f2

15 files changed

+1121
-320
lines changed

examples/natmod/esp32-heap/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
# Espressif ESP-IDF path
16+
IDF_PATH := $(abspath ../../../../esp-idf-micropython)
17+
# Board to get correct ESP-IDF config
18+
BOARD := GENERIC
19+
# xtensa toolchain bin dir
20+
PATH := $(IDF_PATH)/xtensa-esp32-elf/bin:$(PATH)
21+
22+
# Include to get the rules for compiling and linking the module
23+
include $(MPY_DIR)/py/dynruntime.mk
24+
include $(MPY_DIR)/ports/$(PORT)/dynruntime.mk
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 factorial(x)
13+
STATIC mp_obj_t free_heap(mp_obj_t x_obj) {
14+
// // Extract the integer from the MicroPython input object
15+
// mp_int_t x = mp_obj_get_int(x_obj);
16+
// // Calculate the factorial
17+
// mp_int_t result = factorial_helper(x);
18+
// // Convert the result to a MicroPython integer object and return it
19+
// return mp_obj_new_int(result);
20+
21+
return mp_obj_new_int(esp_get_free_heap_size());
22+
}
23+
// Define a Python reference to the function above
24+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(free_heap_obj, free_heap);
25+
26+
// This is the entry point and is called when the module is imported
27+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
28+
// This must be first, it sets up the globals dict and other things
29+
MP_DYNRUNTIME_INIT_ENTRY
30+
31+
// Make the function available in the module's namespace
32+
mp_store_global(MP_QSTR_free_heap, MP_OBJ_FROM_PTR(&free_heap_obj));
33+
34+
// This must be last, it restores the globals dict
35+
MP_DYNRUNTIME_INIT_EXIT
36+
}

examples/natmod/features0/Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ MOD = features0
88
SRC = features0.c
99

1010
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11-
ARCH = xtensawin
12-
13-
PORT = esp32
11+
ARCH = x64
1412

1513
# Include to get the rules for compiling and linking the module
1614
include $(MPY_DIR)/py/dynruntime.mk

examples/natmod/features0/features0.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@
77
// Include the header file to get access to the MicroPython API
88
#include "py/dynruntime.h"
99

10-
// // Helper function to compute factorial
11-
// STATIC mp_int_t factorial_helper(mp_int_t x) {
12-
// if (x == 0) {
13-
// return 1;
14-
// }
15-
// return x * factorial_helper(x - 1);
16-
// }
10+
// Helper function to compute factorial
11+
STATIC mp_int_t factorial_helper(mp_int_t x) {
12+
if (x == 0) {
13+
return 1;
14+
}
15+
return x * factorial_helper(x - 1);
16+
}
1717

1818
// This is the function which will be called from Python, as factorial(x)
1919
STATIC mp_obj_t factorial(mp_obj_t x_obj) {
20-
// // Extract the integer from the MicroPython input object
21-
// mp_int_t x = mp_obj_get_int(x_obj);
22-
// // Calculate the factorial
23-
// mp_int_t result = factorial_helper(x);
24-
// // Convert the result to a MicroPython integer object and return it
25-
// return mp_obj_new_int(result);
26-
27-
return mp_obj_new_int(mp_port_fun_table.esp_clk_cpu_freq());
20+
// Extract the integer from the MicroPython input object
21+
mp_int_t x = mp_obj_get_int(x_obj);
22+
// Calculate the factorial
23+
mp_int_t result = factorial_helper(x);
24+
// Convert the result to a MicroPython integer object and return it
25+
return mp_obj_new_int(result);
2826
}
2927
// Define a Python reference to the function above
3028
STATIC MP_DEFINE_CONST_FUN_OBJ_1(factorial_obj, factorial);

0 commit comments

Comments
 (0)
0