Skip to content

Commit df20420

Browse files
committed
proof of concept for esp32 stubs to call platform-dependent functions
1 parent f020eac commit df20420

File tree

9 files changed

+1156
-2
lines changed

9 files changed

+1156
-2
lines changed

examples/natmod/esp-idf/Makefile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Espressif ESP-IDF path
5+
IDF_PATH := /home/src/esp32/esp-idf-micropython
6+
# Board to get correct ESP-IDF config
7+
BOARD := GENERIC
8+
# xtensa toolchain bin dir
9+
PATH := $(IDF_PATH)/xtensa-esp32-elf/bin:$(PATH)
10+
11+
# Name of module
12+
MOD = counter
13+
14+
# Source files (.c or .py)
15+
SRC = counter.c
16+
17+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
18+
ARCH = xtensawin
19+
20+
# Include to get the rules for compiling and linking the module
21+
include dyn_esp32.mk
22+
include $(MPY_DIR)/py/dynruntime.mk
23+
24+
#plat_stubs.c: $(MPY_DIR)/ports/esp32/plat_relo.py
25+
# $(ECHO) "GEN $@"
26+
# $(Q)mkdir -p build
27+
# $(Q)$< -c >$@
28+
29+
build/plat_stubs.s: $(MPY_DIR)/ports/esp32/plat_relo.py
30+
$(ECHO) "GEN $@"
31+
$(Q)mkdir -p build
32+
$(Q)$< >$@

examples/natmod/esp-idf/counter.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* This example demonstrates using an ESP32 esp-idf function
2+
*/
3+
4+
// Include the header file to get access to the MicroPython API
5+
#include "py/dynruntime.h"
6+
7+
// ESP-IDF imports
8+
#include <driver/pcnt.h>
9+
#include <esp_err.h>
10+
11+
// This is the function which will be called from Python as counter_init(x)
12+
STATIC mp_obj_t counter_init(mp_obj_t x_obj) {
13+
// Extract the integer from the MicroPython input object
14+
mp_int_t x = mp_obj_get_int(x_obj);
15+
// Call pcnt_unit_config
16+
pcnt_config_t cfg = {
17+
.unit = 0,
18+
.channel = 0,
19+
.pulse_gpio_num = x,
20+
.ctrl_gpio_num = -1, // not yet supported
21+
.counter_h_lim = 0,
22+
.counter_l_lim = 0,
23+
.pos_mode = PCNT_COUNT_DIS, // positive edge is no-op
24+
.neg_mode = PCNT_COUNT_DIS, // negative edge is no-op
25+
.lctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op
26+
.hctrl_mode = PCNT_MODE_KEEP, // ctrl pin is no-op
27+
};
28+
esp_err_t err = pcnt_unit_config(&cfg);
29+
// Convert the result to a MicroPython integer object and return it
30+
return mp_obj_new_int(err);
31+
}
32+
// Define a Python reference to the function above
33+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(counter_init_obj, counter_init);
34+
35+
STATIC mp_obj_t mem_info() {
36+
extern int mp_micropython_mem_info(int, int);
37+
mp_printf(&mp_plat_print, "mp_fun_table is at 0x%x\n", &mp_fun_table);
38+
mp_printf(&mp_plat_print, "plat_relo_tab is at 0x%x\n", mp_fun_table.plat_relo_tab);
39+
mp_printf(&mp_plat_print, "calling mp_micropython_mem_info\n");
40+
int i = mp_micropython_mem_info(0, 0);
41+
mp_printf(&mp_plat_print, "mp_micropython_mem_info: %d/0x%x\n", i, i);
42+
return mp_const_none;
43+
}
44+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info);
45+
46+
// This is the entry point and is called when the module is imported
47+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
48+
// This must be first, it sets up the globals dict and other things
49+
MP_DYNRUNTIME_INIT_ENTRY
50+
51+
// Make the function available in the module's namespace
52+
mp_store_global(MP_QSTR_counter_init, MP_OBJ_FROM_PTR(&counter_init_obj));
53+
mp_store_global(MP_QSTR_mem_info, MP_OBJ_FROM_PTR(&mem_info_obj));
54+
55+
// This must be last, it restores the globals dict
56+
MP_DYNRUNTIME_INIT_EXIT
57+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy